OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkWriteBuffer.h" | 8 #include "SkWriteBuffer.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkData.h" | 10 #include "SkData.h" |
11 #include "SkPixelRef.h" | 11 #include "SkPixelRef.h" |
12 #include "SkPtrRecorder.h" | 12 #include "SkPtrRecorder.h" |
13 #include "SkStream.h" | 13 #include "SkStream.h" |
14 #include "SkTypeface.h" | 14 #include "SkTypeface.h" |
15 | 15 |
16 SkWriteBuffer::SkWriteBuffer(uint32_t flags) | 16 SkBinaryWriteBuffer::SkBinaryWriteBuffer(uint32_t flags) |
17 : fFlags(flags) | 17 : fFlags(flags) |
18 , fFactorySet(nullptr) | 18 , fFactorySet(nullptr) |
19 , fTFSet(nullptr) { | 19 , fTFSet(nullptr) { |
20 } | 20 } |
21 | 21 |
22 SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) | 22 SkBinaryWriteBuffer::SkBinaryWriteBuffer(void* storage, size_t storageSize, uint
32_t flags) |
23 : fFlags(flags) | 23 : fFlags(flags) |
24 , fFactorySet(nullptr) | 24 , fFactorySet(nullptr) |
25 , fWriter(storage, storageSize) | 25 , fWriter(storage, storageSize) |
26 , fTFSet(nullptr) { | 26 , fTFSet(nullptr) { |
27 } | 27 } |
28 | 28 |
29 SkWriteBuffer::~SkWriteBuffer() { | 29 SkBinaryWriteBuffer::~SkBinaryWriteBuffer() { |
30 SkSafeUnref(fFactorySet); | 30 SkSafeUnref(fFactorySet); |
31 SkSafeUnref(fTFSet); | 31 SkSafeUnref(fTFSet); |
32 } | 32 } |
33 | 33 |
34 void SkWriteBuffer::writeByteArray(const void* data, size_t size) { | 34 void SkBinaryWriteBuffer::writeByteArray(const void* data, size_t size) { |
35 fWriter.write32(SkToU32(size)); | 35 fWriter.write32(SkToU32(size)); |
36 fWriter.writePad(data, size); | 36 fWriter.writePad(data, size); |
37 } | 37 } |
38 | 38 |
39 void SkWriteBuffer::writeBool(bool value) { | 39 void SkBinaryWriteBuffer::writeBool(bool value) { |
40 fWriter.writeBool(value); | 40 fWriter.writeBool(value); |
41 } | 41 } |
42 | 42 |
43 void SkWriteBuffer::writeScalar(SkScalar value) { | 43 void SkBinaryWriteBuffer::writeScalar(SkScalar value) { |
44 fWriter.writeScalar(value); | 44 fWriter.writeScalar(value); |
45 } | 45 } |
46 | 46 |
47 void SkWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) { | 47 void SkBinaryWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count
) { |
48 fWriter.write32(count); | 48 fWriter.write32(count); |
49 fWriter.write(value, count * sizeof(SkScalar)); | 49 fWriter.write(value, count * sizeof(SkScalar)); |
50 } | 50 } |
51 | 51 |
52 void SkWriteBuffer::writeInt(int32_t value) { | 52 void SkBinaryWriteBuffer::writeInt(int32_t value) { |
53 fWriter.write32(value); | 53 fWriter.write32(value); |
54 } | 54 } |
55 | 55 |
56 void SkWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) { | 56 void SkBinaryWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) { |
57 fWriter.write32(count); | 57 fWriter.write32(count); |
58 fWriter.write(value, count * sizeof(int32_t)); | 58 fWriter.write(value, count * sizeof(int32_t)); |
59 } | 59 } |
60 | 60 |
61 void SkWriteBuffer::writeUInt(uint32_t value) { | 61 void SkBinaryWriteBuffer::writeUInt(uint32_t value) { |
62 fWriter.write32(value); | 62 fWriter.write32(value); |
63 } | 63 } |
64 | 64 |
65 void SkWriteBuffer::write32(int32_t value) { | 65 void SkBinaryWriteBuffer::writeString(const char* value) { |
66 fWriter.write32(value); | |
67 } | |
68 | |
69 void SkWriteBuffer::writeString(const char* value) { | |
70 fWriter.writeString(value); | 66 fWriter.writeString(value); |
71 } | 67 } |
72 | 68 |
73 void SkWriteBuffer::writeColor(const SkColor& color) { | 69 void SkBinaryWriteBuffer::writeColor(SkColor color) { |
74 fWriter.write32(color); | 70 fWriter.write32(color); |
75 } | 71 } |
76 | 72 |
77 void SkWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) { | 73 void SkBinaryWriteBuffer::writeColorArray(const SkColor* color, uint32_t count)
{ |
78 fWriter.write32(count); | 74 fWriter.write32(count); |
79 fWriter.write(color, count * sizeof(SkColor)); | 75 fWriter.write(color, count * sizeof(SkColor)); |
80 } | 76 } |
81 | 77 |
82 void SkWriteBuffer::writePoint(const SkPoint& point) { | 78 void SkBinaryWriteBuffer::writePoint(const SkPoint& point) { |
83 fWriter.writeScalar(point.fX); | 79 fWriter.writeScalar(point.fX); |
84 fWriter.writeScalar(point.fY); | 80 fWriter.writeScalar(point.fY); |
85 } | 81 } |
86 | 82 |
87 void SkWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) { | 83 void SkBinaryWriteBuffer::writePointArray(const SkPoint* point, uint32_t count)
{ |
88 fWriter.write32(count); | 84 fWriter.write32(count); |
89 fWriter.write(point, count * sizeof(SkPoint)); | 85 fWriter.write(point, count * sizeof(SkPoint)); |
90 } | 86 } |
91 | 87 |
92 void SkWriteBuffer::writeMatrix(const SkMatrix& matrix) { | 88 void SkBinaryWriteBuffer::writeMatrix(const SkMatrix& matrix) { |
93 fWriter.writeMatrix(matrix); | 89 fWriter.writeMatrix(matrix); |
94 } | 90 } |
95 | 91 |
96 void SkWriteBuffer::writeIRect(const SkIRect& rect) { | 92 void SkBinaryWriteBuffer::writeIRect(const SkIRect& rect) { |
97 fWriter.write(&rect, sizeof(SkIRect)); | 93 fWriter.write(&rect, sizeof(SkIRect)); |
98 } | 94 } |
99 | 95 |
100 void SkWriteBuffer::writeRect(const SkRect& rect) { | 96 void SkBinaryWriteBuffer::writeRect(const SkRect& rect) { |
101 fWriter.writeRect(rect); | 97 fWriter.writeRect(rect); |
102 } | 98 } |
103 | 99 |
104 void SkWriteBuffer::writeRegion(const SkRegion& region) { | 100 void SkBinaryWriteBuffer::writeRegion(const SkRegion& region) { |
105 fWriter.writeRegion(region); | 101 fWriter.writeRegion(region); |
106 } | 102 } |
107 | 103 |
108 void SkWriteBuffer::writePath(const SkPath& path) { | 104 void SkBinaryWriteBuffer::writePath(const SkPath& path) { |
109 fWriter.writePath(path); | 105 fWriter.writePath(path); |
110 } | 106 } |
111 | 107 |
112 size_t SkWriteBuffer::writeStream(SkStream* stream, size_t length) { | 108 size_t SkBinaryWriteBuffer::writeStream(SkStream* stream, size_t length) { |
113 fWriter.write32(SkToU32(length)); | 109 fWriter.write32(SkToU32(length)); |
114 size_t bytesWritten = fWriter.readFromStream(stream, length); | 110 size_t bytesWritten = fWriter.readFromStream(stream, length); |
115 if (bytesWritten < length) { | 111 if (bytesWritten < length) { |
116 fWriter.reservePad(length - bytesWritten); | 112 fWriter.reservePad(length - bytesWritten); |
117 } | 113 } |
118 return bytesWritten; | 114 return bytesWritten; |
119 } | 115 } |
120 | 116 |
121 bool SkWriteBuffer::writeToStream(SkWStream* stream) { | 117 bool SkBinaryWriteBuffer::writeToStream(SkWStream* stream) { |
122 return fWriter.writeToStream(stream); | 118 return fWriter.writeToStream(stream); |
123 } | 119 } |
124 | 120 |
125 static void write_encoded_bitmap(SkWriteBuffer* buffer, SkData* data, | 121 static void write_encoded_bitmap(SkBinaryWriteBuffer* buffer, SkData* data, |
126 const SkIPoint& origin) { | 122 const SkIPoint& origin) { |
127 buffer->writeDataAsByteArray(data); | 123 buffer->writeDataAsByteArray(data); |
128 buffer->write32(origin.fX); | 124 buffer->write32(origin.fX); |
129 buffer->write32(origin.fY); | 125 buffer->write32(origin.fY); |
130 } | 126 } |
131 | 127 |
132 void SkWriteBuffer::writeBitmap(const SkBitmap& bitmap) { | 128 void SkBinaryWriteBuffer::writeBitmap(const SkBitmap& bitmap) { |
133 // Record the width and height. This way if readBitmap fails a dummy bitmap
can be drawn at the | 129 // Record the width and height. This way if readBitmap fails a dummy bitmap
can be drawn at the |
134 // right size. | 130 // right size. |
135 this->writeInt(bitmap.width()); | 131 this->writeInt(bitmap.width()); |
136 this->writeInt(bitmap.height()); | 132 this->writeInt(bitmap.height()); |
137 | 133 |
138 // Record information about the bitmap in one of two ways, in order of prior
ity: | 134 // Record information about the bitmap in one of two ways, in order of prior
ity: |
139 // 1. If there is a function for encoding bitmaps, use it to write an encode
d version of the | 135 // 1. If there is a function for encoding bitmaps, use it to write an encode
d version of the |
140 // bitmap. After writing a boolean value of false, signifying that a heap
was not used, write | 136 // bitmap. After writing a boolean value of false, signifying that a heap
was not used, write |
141 // the size of the encoded data. A non-zero size signifies that encoded d
ata was written. | 137 // the size of the encoded data. A non-zero size signifies that encoded d
ata was written. |
142 // 2. Call SkBitmap::flatten. After writing a boolean value of false, signif
ying that a heap was | 138 // 2. Call SkBitmap::flatten. After writing a boolean value of false, signif
ying that a heap was |
(...skipping 26 matching lines...) Expand all Loading... |
169 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); | 165 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); |
170 return; | 166 return; |
171 } | 167 } |
172 } | 168 } |
173 } | 169 } |
174 | 170 |
175 this->writeUInt(0); // signal raw pixels | 171 this->writeUInt(0); // signal raw pixels |
176 SkBitmap::WriteRawPixels(this, bitmap); | 172 SkBitmap::WriteRawPixels(this, bitmap); |
177 } | 173 } |
178 | 174 |
179 void SkWriteBuffer::writeImage(const SkImage* image) { | 175 void SkBinaryWriteBuffer::writeImage(const SkImage* image) { |
180 this->writeInt(image->width()); | 176 this->writeInt(image->width()); |
181 this->writeInt(image->height()); | 177 this->writeInt(image->height()); |
182 | 178 |
183 SkAutoTUnref<SkData> encoded(image->encode(this->getPixelSerializer())); | 179 SkAutoTUnref<SkData> encoded(image->encode(this->getPixelSerializer())); |
184 if (encoded && encoded->size() > 0) { | 180 if (encoded && encoded->size() > 0) { |
185 write_encoded_bitmap(this, encoded, SkIPoint::Make(0, 0)); | 181 write_encoded_bitmap(this, encoded, SkIPoint::Make(0, 0)); |
186 return; | 182 return; |
187 } | 183 } |
188 | 184 |
189 this->writeUInt(0); // signal no pixels (in place of the size of the encoded
data) | 185 this->writeUInt(0); // signal no pixels (in place of the size of the encoded
data) |
190 } | 186 } |
191 | 187 |
192 void SkWriteBuffer::writeTypeface(SkTypeface* obj) { | 188 void SkBinaryWriteBuffer::writeTypeface(SkTypeface* obj) { |
193 if (nullptr == obj || nullptr == fTFSet) { | 189 if (nullptr == obj || nullptr == fTFSet) { |
194 fWriter.write32(0); | 190 fWriter.write32(0); |
195 } else { | 191 } else { |
196 fWriter.write32(fTFSet->add(obj)); | 192 fWriter.write32(fTFSet->add(obj)); |
197 } | 193 } |
198 } | 194 } |
199 | 195 |
200 SkFactorySet* SkWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { | 196 void SkBinaryWriteBuffer::writePaint(const SkPaint& paint) { |
| 197 paint.flatten(*this); |
| 198 } |
| 199 |
| 200 SkFactorySet* SkBinaryWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { |
201 SkRefCnt_SafeAssign(fFactorySet, rec); | 201 SkRefCnt_SafeAssign(fFactorySet, rec); |
202 return rec; | 202 return rec; |
203 } | 203 } |
204 | 204 |
205 SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { | 205 SkRefCntSet* SkBinaryWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { |
206 SkRefCnt_SafeAssign(fTFSet, rec); | 206 SkRefCnt_SafeAssign(fTFSet, rec); |
207 return rec; | 207 return rec; |
208 } | 208 } |
209 | 209 |
210 void SkWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { | 210 void SkBinaryWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { |
211 fPixelSerializer.reset(serializer); | 211 fPixelSerializer.reset(serializer); |
212 if (serializer) { | 212 if (serializer) { |
213 serializer->ref(); | 213 serializer->ref(); |
214 } | 214 } |
215 } | 215 } |
216 | 216 |
217 void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { | 217 void SkBinaryWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { |
218 /* | 218 /* |
219 * The first 32 bits tell us... | 219 * The first 32 bits tell us... |
220 * 0: failure to write the flattenable | 220 * 0: failure to write the flattenable |
221 * >0: index (1-based) into fFactorySet or fFlattenableDict or | 221 * >0: index (1-based) into fFactorySet or fFlattenableDict or |
222 * the first character of a string | 222 * the first character of a string |
223 */ | 223 */ |
224 if (nullptr == flattenable) { | 224 if (nullptr == flattenable) { |
225 this->write32(0); | 225 this->write32(0); |
226 return; | 226 return; |
227 } | 227 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 // make room for the size of the flattened object | 268 // make room for the size of the flattened object |
269 (void)fWriter.reserve(sizeof(uint32_t)); | 269 (void)fWriter.reserve(sizeof(uint32_t)); |
270 // record the current size, so we can subtract after the object writes. | 270 // record the current size, so we can subtract after the object writes. |
271 size_t offset = fWriter.bytesWritten(); | 271 size_t offset = fWriter.bytesWritten(); |
272 // now flatten the object | 272 // now flatten the object |
273 flattenable->flatten(*this); | 273 flattenable->flatten(*this); |
274 size_t objSize = fWriter.bytesWritten() - offset; | 274 size_t objSize = fWriter.bytesWritten() - offset; |
275 // record the obj's size | 275 // record the obj's size |
276 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); | 276 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); |
277 } | 277 } |
OLD | NEW |