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 "SkDeduper.h" | |
11 #include "SkPixelRef.h" | 12 #include "SkPixelRef.h" |
12 #include "SkPtrRecorder.h" | 13 #include "SkPtrRecorder.h" |
13 #include "SkStream.h" | 14 #include "SkStream.h" |
14 #include "SkTypeface.h" | 15 #include "SkTypeface.h" |
15 | 16 |
17 bool SkWriteBuffer::newWriteImage(const SkImage* image) { | |
18 if (fDeduper) { | |
19 this->write32(fDeduper->findOrDefineImage(const_cast<SkImage*>(image))); | |
20 return true; | |
21 } | |
22 return false; | |
23 } | |
24 | |
25 bool SkWriteBuffer::newWriteTypeface(SkTypeface* typeface) { | |
26 if (fDeduper) { | |
27 this->write32(fDeduper->findOrDefineTypeface(typeface)); | |
28 return true; | |
29 } | |
30 return false; | |
31 } | |
32 | |
33 //////////////////////////////////////////////////////////////////////////////// /////////////////// | |
34 | |
16 SkBinaryWriteBuffer::SkBinaryWriteBuffer(uint32_t flags) | 35 SkBinaryWriteBuffer::SkBinaryWriteBuffer(uint32_t flags) |
17 : fFlags(flags) | 36 : fFlags(flags) |
18 , fFactorySet(nullptr) | 37 , fFactorySet(nullptr) |
19 , fTFSet(nullptr) { | 38 , fTFSet(nullptr) { |
20 } | 39 } |
21 | 40 |
22 SkBinaryWriteBuffer::SkBinaryWriteBuffer(void* storage, size_t storageSize, uint 32_t flags) | 41 SkBinaryWriteBuffer::SkBinaryWriteBuffer(void* storage, size_t storageSize, uint 32_t flags) |
23 : fFlags(flags) | 42 : fFlags(flags) |
24 , fFactorySet(nullptr) | 43 , fFactorySet(nullptr) |
25 , fWriter(storage, storageSize) | 44 , fWriter(storage, storageSize) |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 return; | 185 return; |
167 } | 186 } |
168 } | 187 } |
169 } | 188 } |
170 | 189 |
171 this->writeUInt(0); // signal raw pixels | 190 this->writeUInt(0); // signal raw pixels |
172 SkBitmap::WriteRawPixels(this, bitmap); | 191 SkBitmap::WriteRawPixels(this, bitmap); |
173 } | 192 } |
174 | 193 |
175 void SkBinaryWriteBuffer::writeImage(const SkImage* image) { | 194 void SkBinaryWriteBuffer::writeImage(const SkImage* image) { |
195 if (this->newWriteImage(image)) { | |
mtklein_C
2016/09/12 19:47:58
Doesn't really seem like newWriteImage() and newWr
reed1
2016/09/12 21:35:23
Done.
| |
196 return; | |
197 } | |
198 | |
176 this->writeInt(image->width()); | 199 this->writeInt(image->width()); |
177 this->writeInt(image->height()); | 200 this->writeInt(image->height()); |
178 | 201 |
179 sk_sp<SkData> encoded(image->encode(this->getPixelSerializer())); | 202 sk_sp<SkData> encoded(image->encode(this->getPixelSerializer())); |
180 if (encoded && encoded->size() > 0) { | 203 if (encoded && encoded->size() > 0) { |
181 write_encoded_bitmap(this, encoded.get(), SkIPoint::Make(0, 0)); | 204 write_encoded_bitmap(this, encoded.get(), SkIPoint::Make(0, 0)); |
182 return; | 205 return; |
183 } | 206 } |
184 | 207 |
185 SkBitmap bm; | 208 SkBitmap bm; |
186 if (image->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode)) { | 209 if (image->asLegacyBitmap(&bm, SkImage::kRO_LegacyBitmapMode)) { |
187 this->writeUInt(1); // signal raw pixels. | 210 this->writeUInt(1); // signal raw pixels. |
188 SkBitmap::WriteRawPixels(this, bm); | 211 SkBitmap::WriteRawPixels(this, bm); |
189 return; | 212 return; |
190 } | 213 } |
191 | 214 |
192 this->writeUInt(0); // signal no pixels (in place of the size of the encoded data) | 215 this->writeUInt(0); // signal no pixels (in place of the size of the encoded data) |
193 } | 216 } |
194 | 217 |
195 void SkBinaryWriteBuffer::writeTypeface(SkTypeface* obj) { | 218 void SkBinaryWriteBuffer::writeTypeface(SkTypeface* obj) { |
219 if (this->newWriteTypeface(obj)) { | |
220 return; | |
221 } | |
222 | |
196 if (nullptr == obj || nullptr == fTFSet) { | 223 if (nullptr == obj || nullptr == fTFSet) { |
197 fWriter.write32(0); | 224 fWriter.write32(0); |
198 } else { | 225 } else { |
199 fWriter.write32(fTFSet->add(obj)); | 226 fWriter.write32(fTFSet->add(obj)); |
200 } | 227 } |
201 } | 228 } |
202 | 229 |
203 void SkBinaryWriteBuffer::writePaint(const SkPaint& paint) { | 230 void SkBinaryWriteBuffer::writePaint(const SkPaint& paint) { |
204 paint.flatten(*this); | 231 paint.flatten(*this); |
205 } | 232 } |
206 | 233 |
207 SkFactorySet* SkBinaryWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { | 234 SkFactorySet* SkBinaryWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { |
208 SkRefCnt_SafeAssign(fFactorySet, rec); | 235 SkRefCnt_SafeAssign(fFactorySet, rec); |
209 return rec; | 236 return rec; |
210 } | 237 } |
211 | 238 |
212 SkRefCntSet* SkBinaryWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { | 239 SkRefCntSet* SkBinaryWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { |
213 SkRefCnt_SafeAssign(fTFSet, rec); | 240 SkRefCnt_SafeAssign(fTFSet, rec); |
214 return rec; | 241 return rec; |
215 } | 242 } |
216 | 243 |
217 void SkBinaryWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { | 244 void SkBinaryWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) { |
218 fPixelSerializer.reset(serializer); | 245 fPixelSerializer.reset(serializer); |
219 if (serializer) { | 246 if (serializer) { |
220 serializer->ref(); | 247 serializer->ref(); |
221 } | 248 } |
222 } | 249 } |
223 | 250 |
224 void SkBinaryWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { | 251 void SkBinaryWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { |
225 /* | |
226 * The first 32 bits tell us... | |
227 * 0: failure to write the flattenable | |
228 * >0: index (1-based) into fFactorySet or fFlattenableDict or | |
229 * the first character of a string | |
230 */ | |
231 if (nullptr == flattenable) { | 252 if (nullptr == flattenable) { |
232 this->write32(0); | 253 this->write32(0); |
233 return; | 254 return; |
234 } | 255 } |
235 | 256 |
236 /* | 257 if (fDeduper) { |
237 * We can write 1 of 2 versions of the flattenable: | 258 this->write32(fDeduper->findOrDefineFactory(const_cast<SkFlattenable*>(f lattenable))); |
238 * 1. index into fFactorySet : This assumes the writer will later | |
239 * resolve the function-ptrs into strings for its reader. SkPicture | |
240 * does exactly this, by writing a table of names (matching the indices ) | |
241 * up front in its serialized form. | |
242 * 2. string name of the flattenable or index into fFlattenableDict: We | |
243 * store the string to allow the reader to specify its own factories | |
244 * after write time. In order to improve compression, if we have | |
245 * already written the string, we write its index instead. | |
246 */ | |
247 if (fFactorySet) { | |
248 SkFlattenable::Factory factory = flattenable->getFactory(); | |
249 SkASSERT(factory); | |
250 this->write32(fFactorySet->add(factory)); | |
251 } else { | 259 } else { |
252 const char* name = flattenable->getTypeName(); | 260 /* |
253 SkASSERT(name); | 261 * We can write 1 of 2 versions of the flattenable: |
254 SkString key(name); | 262 * 1. index into fFactorySet : This assumes the writer will later |
255 if (uint32_t* indexPtr = fFlattenableDict.find(key)) { | 263 * resolve the function-ptrs into strings for its reader. SkPicture |
256 // We will write the index as a 32-bit int. We want the first byte | 264 * does exactly this, by writing a table of names (matching the ind ices) |
257 // that we send to be zero - this will act as a sentinel that we | 265 * up front in its serialized form. |
258 // have an index (not a string). This means that we will send the | 266 * 2. string name of the flattenable or index into fFlattenableDict: We |
259 // the index shifted left by 8. The remaining 24-bits should be | 267 * store the string to allow the reader to specify its own factorie s |
260 // plenty to store the index. Note that this strategy depends on | 268 * after write time. In order to improve compression, if we have |
261 // being little endian. | 269 * already written the string, we write its index instead. |
262 SkASSERT(0 == *indexPtr >> 24); | 270 */ |
263 this->write32(*indexPtr << 8); | 271 if (fFactorySet) { |
272 SkFlattenable::Factory factory = flattenable->getFactory(); | |
273 SkASSERT(factory); | |
274 this->write32(fFactorySet->add(factory)); | |
264 } else { | 275 } else { |
265 // Otherwise write the string. Clients should not use the empty | 276 const char* name = flattenable->getTypeName(); |
266 // string as a name, or we will have a problem. | 277 SkASSERT(name); |
267 SkASSERT(strcmp("", name)); | 278 SkString key(name); |
268 this->writeString(name); | 279 if (uint32_t* indexPtr = fFlattenableDict.find(key)) { |
280 // We will write the index as a 32-bit int. We want the first b yte | |
281 // that we send to be zero - this will act as a sentinel that we | |
282 // have an index (not a string). This means that we will send t he | |
283 // the index shifted left by 8. The remaining 24-bits should be | |
284 // plenty to store the index. Note that this strategy depends o n | |
285 // being little endian. | |
286 SkASSERT(0 == *indexPtr >> 24); | |
287 this->write32(*indexPtr << 8); | |
288 } else { | |
289 // Otherwise write the string. Clients should not use the empty | |
290 // string as a name, or we will have a problem. | |
291 SkASSERT(strcmp("", name)); | |
292 this->writeString(name); | |
269 | 293 |
270 // Add key to dictionary. | 294 // Add key to dictionary. |
271 fFlattenableDict.set(key, fFlattenableDict.count() + 1); | 295 fFlattenableDict.set(key, fFlattenableDict.count() + 1); |
296 } | |
272 } | 297 } |
273 } | 298 } |
274 | 299 |
275 // make room for the size of the flattened object | 300 // make room for the size of the flattened object |
276 (void)fWriter.reserve(sizeof(uint32_t)); | 301 (void)fWriter.reserve(sizeof(uint32_t)); |
277 // record the current size, so we can subtract after the object writes. | 302 // record the current size, so we can subtract after the object writes. |
278 size_t offset = fWriter.bytesWritten(); | 303 size_t offset = fWriter.bytesWritten(); |
279 // now flatten the object | 304 // now flatten the object |
280 flattenable->flatten(*this); | 305 flattenable->flatten(*this); |
281 size_t objSize = fWriter.bytesWritten() - offset; | 306 size_t objSize = fWriter.bytesWritten() - offset; |
282 // record the obj's size | 307 // record the obj's size |
283 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); | 308 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); |
284 } | 309 } |
OLD | NEW |