• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 class SkFactorySet;
18 class SkImage;
19 class SkM44;
20 class SkPath;
21 class SkRefCntSet;
22 
23 class SkWriteBuffer {
24 public:
SkWriteBuffer()25     SkWriteBuffer() {}
~SkWriteBuffer()26     virtual ~SkWriteBuffer() {}
27 
28     virtual void writePad32(const void* buffer, size_t bytes) = 0;
29 
30     virtual void writeByteArray(const void* data, size_t size) = 0;
writeDataAsByteArray(SkData * data)31     void writeDataAsByteArray(SkData* data) {
32         if (!data) {
33             this->write32(0);
34         } else {
35             this->writeByteArray(data->data(), data->size());
36         }
37     }
38 
39     virtual void writeBool(bool value) = 0;
40     virtual void writeScalar(SkScalar value) = 0;
41     virtual void writeScalarArray(const SkScalar* value, uint32_t count) = 0;
42     virtual void writeInt(int32_t value) = 0;
43     virtual void writeIntArray(const int32_t* value, uint32_t count) = 0;
44     virtual void writeUInt(uint32_t value) = 0;
write32(int32_t value)45     void write32(int32_t value) {
46         this->writeInt(value);
47     }
48     virtual void writeString(const char* value) = 0;
49 
50     virtual void writeFlattenable(const SkFlattenable* flattenable) = 0;
51     virtual void writeColor(SkColor color) = 0;
52     virtual void writeColorArray(const SkColor* color, uint32_t count) = 0;
53     virtual void writeColor4f(const SkColor4f& color) = 0;
54     virtual void writeColor4fArray(const SkColor4f* color, uint32_t count) = 0;
55     virtual void writePoint(const SkPoint& point) = 0;
56     virtual void writePointArray(const SkPoint* point, uint32_t count) = 0;
57     virtual void writePoint3(const SkPoint3& point) = 0;
58     virtual void write(const SkM44&) = 0;
59     virtual void writeMatrix(const SkMatrix& matrix) = 0;
60     virtual void writeIRect(const SkIRect& rect) = 0;
61     virtual void writeRect(const SkRect& rect) = 0;
62     virtual void writeRegion(const SkRegion& region) = 0;
63     virtual void writePath(const SkPath& path) = 0;
64     virtual size_t writeStream(SkStream* stream, size_t length) = 0;
65     virtual void writeImage(const SkImage*) = 0;
66     virtual void writeTypeface(SkTypeface* typeface) = 0;
67     virtual void writePaint(const SkPaint& paint) = 0;
68 
setSerialProcs(const SkSerialProcs & procs)69     void setSerialProcs(const SkSerialProcs& procs) { fProcs = procs; }
70 
71 protected:
72     SkSerialProcs   fProcs;
73 
74     friend class SkPicturePriv; // fProcs
75 };
76 
77 /**
78  * Concrete implementation that serializes to a flat binary blob.
79  */
80 class SK_API SkBinaryWriteBuffer : public SkWriteBuffer {
81 public:
82     SkBinaryWriteBuffer();
83     SkBinaryWriteBuffer(void* initialStorage, size_t storageSize);
84     ~SkBinaryWriteBuffer() override;
85 
write(const void * buffer,size_t bytes)86     void write(const void* buffer, size_t bytes) {
87         fWriter.write(buffer, bytes);
88     }
writePad32(const void * buffer,size_t bytes)89     void writePad32(const void* buffer, size_t bytes) override {
90         fWriter.writePad(buffer, bytes);
91     }
92 
93     void reset(void* storage = nullptr, size_t storageSize = 0) {
94         fWriter.reset(storage, storageSize);
95     }
96 
bytesWritten()97     size_t bytesWritten() const { return fWriter.bytesWritten(); }
98 
99     // Returns true iff all of the bytes written so far are stored in the initial storage
100     // buffer provided in the constructor or the most recent call to reset.
101     bool usingInitialStorage() const;
102 
103     void writeByteArray(const void* data, size_t size) override;
104     void writeBool(bool value) override;
105     void writeScalar(SkScalar value) override;
106     void writeScalarArray(const SkScalar* value, uint32_t count) override;
107     void writeInt(int32_t value) override;
108     void writeIntArray(const int32_t* value, uint32_t count) override;
109     void writeUInt(uint32_t value) override;
110     void writeString(const char* value) override;
111 
112     void writeFlattenable(const SkFlattenable* flattenable) override;
113     void writeColor(SkColor color) override;
114     void writeColorArray(const SkColor* color, uint32_t count) override;
115     void writeColor4f(const SkColor4f& color) override;
116     void writeColor4fArray(const SkColor4f* color, uint32_t count) override;
117     void writePoint(const SkPoint& point) override;
118     void writePointArray(const SkPoint* point, uint32_t count) override;
119     void writePoint3(const SkPoint3& point) override;
120     void write(const SkM44&) override;
121     void writeMatrix(const SkMatrix& matrix) override;
122     void writeIRect(const SkIRect& rect) override;
123     void writeRect(const SkRect& rect) override;
124     void writeRegion(const SkRegion& region) override;
125     void writePath(const SkPath& path) override;
126     size_t writeStream(SkStream* stream, size_t length) override;
127     void writeImage(const SkImage*) override;
128     void writeTypeface(SkTypeface* typeface) override;
129     void writePaint(const SkPaint& paint) override;
130 
131     bool writeToStream(SkWStream*) const;
writeToMemory(void * dst)132     void writeToMemory(void* dst) const { fWriter.flatten(dst); }
snapshotAsData()133     sk_sp<SkData> snapshotAsData() const { return fWriter.snapshotAsData(); }
134 
135     void setFactoryRecorder(sk_sp<SkFactorySet>);
136     void setTypefaceRecorder(sk_sp<SkRefCntSet>);
137 
138 private:
139     sk_sp<SkFactorySet> fFactorySet;
140     sk_sp<SkRefCntSet> fTFSet;
141 
142     SkWriter32 fWriter;
143 
144     // Only used if we do not have an fFactorySet
145     SkTHashMap<const char*, uint32_t> fFlattenableDict;
146 };
147 
148 enum SkWriteBufferImageFlags {
149     kVersion_bits   = 8,
150     kCurrVersion    = 0,
151 
152     kHasSubsetRect  = 1 << 8,
153     kHasMipmap      = 1 << 9,
154 };
155 
156 
157 #endif // SkWriteBuffer_DEFINED
158