• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #ifndef SkWriteBuffer_DEFINED
10 #define SkWriteBuffer_DEFINED
11 
12 #include "SkData.h"
13 #include "SkImage.h"
14 #include "SkPath.h"
15 #include "SkPicture.h"
16 #include "SkPixelSerializer.h"
17 #include "SkRefCnt.h"
18 #include "SkWriter32.h"
19 #include "../private/SkTHash.h"
20 
21 class SkBitmap;
22 class SkDeduper;
23 class SkFactorySet;
24 class SkFlattenable;
25 class SkRefCntSet;
26 
27 class SK_API SkWriteBuffer {
28 public:
SkWriteBuffer()29     SkWriteBuffer() {}
~SkWriteBuffer()30     virtual ~SkWriteBuffer() {}
31 
32     virtual bool isCrossProcess() const = 0;
33 
34     virtual void writeByteArray(const void* data, size_t size) = 0;
writeDataAsByteArray(SkData * data)35     void writeDataAsByteArray(SkData* data) {
36         this->writeByteArray(data->data(), data->size());
37     }
38     virtual void writeBool(bool value) = 0;
39     virtual void writeScalar(SkScalar value) = 0;
40     virtual void writeScalarArray(const SkScalar* value, uint32_t count) = 0;
41     virtual void writeInt(int32_t value) = 0;
42     virtual void writeIntArray(const int32_t* value, uint32_t count) = 0;
43     virtual void writeUInt(uint32_t value) = 0;
write32(int32_t value)44     void write32(int32_t value) {
45         this->writeInt(value);
46     }
47     virtual void writeString(const char* value) = 0;
48 
49     virtual void writeFlattenable(const SkFlattenable* flattenable) = 0;
50     virtual void writeColor(SkColor color) = 0;
51     virtual void writeColorArray(const SkColor* color, uint32_t count) = 0;
52     virtual void writeColor4f(const SkColor4f& color) = 0;
53     virtual void writeColor4fArray(const SkColor4f* color, uint32_t count) = 0;
54     virtual void writePoint(const SkPoint& point) = 0;
55     virtual void writePointArray(const SkPoint* point, uint32_t count) = 0;
56     virtual void writeMatrix(const SkMatrix& matrix) = 0;
57     virtual void writeIRect(const SkIRect& rect) = 0;
58     virtual void writeRect(const SkRect& rect) = 0;
59     virtual void writeRegion(const SkRegion& region) = 0;
60     virtual void writePath(const SkPath& path) = 0;
61     virtual size_t writeStream(SkStream* stream, size_t length) = 0;
62     virtual void writeBitmap(const SkBitmap& bitmap) = 0;
63     virtual void writeImage(const SkImage*) = 0;
64     virtual void writeTypeface(SkTypeface* typeface) = 0;
65     virtual void writePaint(const SkPaint& paint) = 0;
66 
setDeduper(SkDeduper * deduper)67     void setDeduper(SkDeduper* deduper) { fDeduper = deduper; }
68 
69 protected:
70     SkDeduper* fDeduper = nullptr;
71 };
72 
73 /**
74  * Concrete implementation that serializes to a flat binary blob.
75  */
76 class SK_API SkBinaryWriteBuffer : public SkWriteBuffer {
77 public:
78     enum Flags {
79         kCrossProcess_Flag = 1 << 0,
80     };
81 
82     SkBinaryWriteBuffer(uint32_t flags = 0);
83     SkBinaryWriteBuffer(void* initialStorage, size_t storageSize, uint32_t flags = 0);
84     ~SkBinaryWriteBuffer() override;
85 
isCrossProcess()86     bool isCrossProcess() const override {
87         return SkToBool(fFlags & kCrossProcess_Flag);
88     }
89 
write(const void * buffer,size_t bytes)90     void write(const void* buffer, size_t bytes) {
91         fWriter.write(buffer, bytes);
92     }
93 
94     void reset(void* storage = NULL, size_t storageSize = 0) {
95         fWriter.reset(storage, storageSize);
96     }
97 
bytesWritten()98     size_t bytesWritten() const { return fWriter.bytesWritten(); }
99 
100     void writeByteArray(const void* data, size_t size) override;
101     void writeBool(bool value) override;
102     void writeScalar(SkScalar value) override;
103     void writeScalarArray(const SkScalar* value, uint32_t count) override;
104     void writeInt(int32_t value) override;
105     void writeIntArray(const int32_t* value, uint32_t count) override;
106     void writeUInt(uint32_t value) override;
107     void writeString(const char* value) override;
108 
109     void writeFlattenable(const SkFlattenable* flattenable) override;
110     void writeColor(SkColor color) override;
111     void writeColorArray(const SkColor* color, uint32_t count) override;
112     void writeColor4f(const SkColor4f& color) override;
113     void writeColor4fArray(const SkColor4f* color, uint32_t count) override;
114     void writePoint(const SkPoint& point) override;
115     void writePointArray(const SkPoint* point, uint32_t count) override;
116     void writeMatrix(const SkMatrix& matrix) override;
117     void writeIRect(const SkIRect& rect) override;
118     void writeRect(const SkRect& rect) override;
119     void writeRegion(const SkRegion& region) override;
120     void writePath(const SkPath& path) override;
121     size_t writeStream(SkStream* stream, size_t length) override;
122     void writeBitmap(const SkBitmap& bitmap) override;
123     void writeImage(const SkImage*) override;
124     void writeTypeface(SkTypeface* typeface) override;
125     void writePaint(const SkPaint& paint) override;
126 
127     bool writeToStream(SkWStream*);
writeToMemory(void * dst)128     void writeToMemory(void* dst) { fWriter.flatten(dst); }
129 
130     SkFactorySet* setFactoryRecorder(SkFactorySet*);
131     SkRefCntSet* setTypefaceRecorder(SkRefCntSet*);
132 
133     /**
134      * Set an SkPixelSerializer to store an encoded representation of pixels,
135      * e.g. SkBitmaps.
136      *
137      * TODO: Encode SkImage pixels as well.
138      */
139     void setPixelSerializer(sk_sp<SkPixelSerializer>);
getPixelSerializer()140     SkPixelSerializer* getPixelSerializer() const { return fPixelSerializer.get(); }
141 
142 private:
143     const uint32_t fFlags;
144     SkFactorySet* fFactorySet;
145     SkWriter32 fWriter;
146 
147     SkRefCntSet* fTFSet;
148 
149     sk_sp<SkPixelSerializer> fPixelSerializer;
150 
151     // Only used if we do not have an fFactorySet
152     SkTHashMap<SkString, uint32_t> fFlattenableDict;
153 };
154 
155 #endif // SkWriteBuffer_DEFINED
156