1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkWriteBuffer_DEFINED
9#define SkWriteBuffer_DEFINED
10
11#include "include/core/SkData.h"
12#include "include/core/SkFlattenable.h"
13#include "include/core/SkSerialProcs.h"
14#include "include/private/SkTHash.h"
15#include "src/core/SkWriter32.h"
16
17class SkFactorySet;
18class SkImage;
19class SkPath;
20class SkRefCntSet;
21
22class SkWriteBuffer {
23public:
24 SkWriteBuffer() {}
25 virtual ~SkWriteBuffer() {}
26
27 virtual void writePad32(const void* buffer, size_t bytes) = 0;
28
29 virtual void writeByteArray(const void* data, size_t size) = 0;
30 void writeDataAsByteArray(SkData* data) {
31 this->writeByteArray(data->data(), data->size());
32 }
33 virtual void writeBool(bool value) = 0;
34 virtual void writeScalar(SkScalar value) = 0;
35 virtual void writeScalarArray(const SkScalar* value, uint32_t count) = 0;
36 virtual void writeInt(int32_t value) = 0;
37 virtual void writeIntArray(const int32_t* value, uint32_t count) = 0;
38 virtual void writeUInt(uint32_t value) = 0;
39 void write32(int32_t value) {
40 this->writeInt(value);
41 }
42 virtual void writeString(const char* value) = 0;
43
44 virtual void writeFlattenable(const SkFlattenable* flattenable) = 0;
45 virtual void writeColor(SkColor color) = 0;
46 virtual void writeColorArray(const SkColor* color, uint32_t count) = 0;
47 virtual void writeColor4f(const SkColor4f& color) = 0;
48 virtual void writeColor4fArray(const SkColor4f* color, uint32_t count) = 0;
49 virtual void writePoint(const SkPoint& point) = 0;
50 virtual void writePointArray(const SkPoint* point, uint32_t count) = 0;
51 virtual void writePoint3(const SkPoint3& point) = 0;
52 virtual void writeMatrix(const SkMatrix& matrix) = 0;
53 virtual void writeIRect(const SkIRect& rect) = 0;
54 virtual void writeRect(const SkRect& rect) = 0;
55 virtual void writeRegion(const SkRegion& region) = 0;
56 virtual void writePath(const SkPath& path) = 0;
57 virtual size_t writeStream(SkStream* stream, size_t length) = 0;
58 virtual void writeImage(const SkImage*) = 0;
59 virtual void writeTypeface(SkTypeface* typeface) = 0;
60 virtual void writePaint(const SkPaint& paint) = 0;
61
62 void setSerialProcs(const SkSerialProcs& procs) { fProcs = procs; }
63
64protected:
65 SkSerialProcs fProcs;
66
67 friend class SkPicturePriv; // fProcs
68};
69
70/**
71 * Concrete implementation that serializes to a flat binary blob.
72 */
73class SkBinaryWriteBuffer : public SkWriteBuffer {
74public:
75 SkBinaryWriteBuffer();
76 SkBinaryWriteBuffer(void* initialStorage, size_t storageSize);
77 ~SkBinaryWriteBuffer() override;
78
79 void write(const void* buffer, size_t bytes) {
80 fWriter.write(buffer, bytes);
81 }
82 void writePad32(const void* buffer, size_t bytes) override {
83 fWriter.writePad(buffer, bytes);
84 }
85
86 void reset(void* storage = nullptr, size_t storageSize = 0) {
87 fWriter.reset(storage, storageSize);
88 }
89
90 size_t bytesWritten() const { return fWriter.bytesWritten(); }
91
92 // Returns true iff all of the bytes written so far are stored in the initial storage
93 // buffer provided in the constructor or the most recent call to reset.
94 bool usingInitialStorage() const;
95
96 void writeByteArray(const void* data, size_t size) override;
97 void writeBool(bool value) override;
98 void writeScalar(SkScalar value) override;
99 void writeScalarArray(const SkScalar* value, uint32_t count) override;
100 void writeInt(int32_t value) override;
101 void writeIntArray(const int32_t* value, uint32_t count) override;
102 void writeUInt(uint32_t value) override;
103 void writeString(const char* value) override;
104
105 void writeFlattenable(const SkFlattenable* flattenable) override;
106 void writeColor(SkColor color) override;
107 void writeColorArray(const SkColor* color, uint32_t count) override;
108 void writeColor4f(const SkColor4f& color) override;
109 void writeColor4fArray(const SkColor4f* color, uint32_t count) override;
110 void writePoint(const SkPoint& point) override;
111 void writePointArray(const SkPoint* point, uint32_t count) override;
112 void writePoint3(const SkPoint3& point) override;
113 void writeMatrix(const SkMatrix& matrix) override;
114 void writeIRect(const SkIRect& rect) override;
115 void writeRect(const SkRect& rect) override;
116 void writeRegion(const SkRegion& region) override;
117 void writePath(const SkPath& path) override;
118 size_t writeStream(SkStream* stream, size_t length) override;
119 void writeImage(const SkImage*) override;
120 void writeTypeface(SkTypeface* typeface) override;
121 void writePaint(const SkPaint& paint) override;
122
123 bool writeToStream(SkWStream*) const;
124 void writeToMemory(void* dst) const { fWriter.flatten(dst); }
125
126 void setFactoryRecorder(sk_sp<SkFactorySet>);
127 void setTypefaceRecorder(sk_sp<SkRefCntSet>);
128
129private:
130 sk_sp<SkFactorySet> fFactorySet;
131 sk_sp<SkRefCntSet> fTFSet;
132
133 SkWriter32 fWriter;
134
135 // Only used if we do not have an fFactorySet
136 SkTHashMap<SkFlattenable::Factory, uint32_t> fFlattenableDict;
137};
138
139#endif // SkWriteBuffer_DEFINED
140