• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2012 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 #include "SkWriteBuffer.h"
10 #include "SkBitmap.h"
11 #include "SkBitmapHeap.h"
12 #include "SkData.h"
13 #include "SkPixelRef.h"
14 #include "SkPtrRecorder.h"
15 #include "SkStream.h"
16 #include "SkTypeface.h"
17 
SkWriteBuffer(uint32_t flags)18 SkWriteBuffer::SkWriteBuffer(uint32_t flags)
19     : fFlags(flags)
20     , fFactorySet(NULL)
21     , fNamedFactorySet(NULL)
22     , fBitmapHeap(NULL)
23     , fTFSet(NULL) {
24 }
25 
SkWriteBuffer(void * storage,size_t storageSize,uint32_t flags)26 SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags)
27     : fFlags(flags)
28     , fFactorySet(NULL)
29     , fNamedFactorySet(NULL)
30     , fWriter(storage, storageSize)
31     , fBitmapHeap(NULL)
32     , fTFSet(NULL) {
33 }
34 
~SkWriteBuffer()35 SkWriteBuffer::~SkWriteBuffer() {
36     SkSafeUnref(fFactorySet);
37     SkSafeUnref(fNamedFactorySet);
38     SkSafeUnref(fBitmapHeap);
39     SkSafeUnref(fTFSet);
40 }
41 
writeByteArray(const void * data,size_t size)42 void SkWriteBuffer::writeByteArray(const void* data, size_t size) {
43     fWriter.write32(SkToU32(size));
44     fWriter.writePad(data, size);
45 }
46 
writeBool(bool value)47 void SkWriteBuffer::writeBool(bool value) {
48     fWriter.writeBool(value);
49 }
50 
writeFixed(SkFixed value)51 void SkWriteBuffer::writeFixed(SkFixed value) {
52     fWriter.write32(value);
53 }
54 
writeScalar(SkScalar value)55 void SkWriteBuffer::writeScalar(SkScalar value) {
56     fWriter.writeScalar(value);
57 }
58 
writeScalarArray(const SkScalar * value,uint32_t count)59 void SkWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) {
60     fWriter.write32(count);
61     fWriter.write(value, count * sizeof(SkScalar));
62 }
63 
writeInt(int32_t value)64 void SkWriteBuffer::writeInt(int32_t value) {
65     fWriter.write32(value);
66 }
67 
writeIntArray(const int32_t * value,uint32_t count)68 void SkWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) {
69     fWriter.write32(count);
70     fWriter.write(value, count * sizeof(int32_t));
71 }
72 
writeUInt(uint32_t value)73 void SkWriteBuffer::writeUInt(uint32_t value) {
74     fWriter.write32(value);
75 }
76 
write32(int32_t value)77 void SkWriteBuffer::write32(int32_t value) {
78     fWriter.write32(value);
79 }
80 
writeString(const char * value)81 void SkWriteBuffer::writeString(const char* value) {
82     fWriter.writeString(value);
83 }
84 
writeEncodedString(const void * value,size_t byteLength,SkPaint::TextEncoding encoding)85 void SkWriteBuffer::writeEncodedString(const void* value, size_t byteLength,
86                                               SkPaint::TextEncoding encoding) {
87     fWriter.writeInt(encoding);
88     fWriter.writeInt(SkToU32(byteLength));
89     fWriter.write(value, byteLength);
90 }
91 
92 
writeColor(const SkColor & color)93 void SkWriteBuffer::writeColor(const SkColor& color) {
94     fWriter.write32(color);
95 }
96 
writeColorArray(const SkColor * color,uint32_t count)97 void SkWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) {
98     fWriter.write32(count);
99     fWriter.write(color, count * sizeof(SkColor));
100 }
101 
writePoint(const SkPoint & point)102 void SkWriteBuffer::writePoint(const SkPoint& point) {
103     fWriter.writeScalar(point.fX);
104     fWriter.writeScalar(point.fY);
105 }
106 
writePointArray(const SkPoint * point,uint32_t count)107 void SkWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) {
108     fWriter.write32(count);
109     fWriter.write(point, count * sizeof(SkPoint));
110 }
111 
writeMatrix(const SkMatrix & matrix)112 void SkWriteBuffer::writeMatrix(const SkMatrix& matrix) {
113     fWriter.writeMatrix(matrix);
114 }
115 
writeIRect(const SkIRect & rect)116 void SkWriteBuffer::writeIRect(const SkIRect& rect) {
117     fWriter.write(&rect, sizeof(SkIRect));
118 }
119 
writeRect(const SkRect & rect)120 void SkWriteBuffer::writeRect(const SkRect& rect) {
121     fWriter.writeRect(rect);
122 }
123 
writeRegion(const SkRegion & region)124 void SkWriteBuffer::writeRegion(const SkRegion& region) {
125     fWriter.writeRegion(region);
126 }
127 
writePath(const SkPath & path)128 void SkWriteBuffer::writePath(const SkPath& path) {
129     fWriter.writePath(path);
130 }
131 
writeStream(SkStream * stream,size_t length)132 size_t SkWriteBuffer::writeStream(SkStream* stream, size_t length) {
133     fWriter.write32(SkToU32(length));
134     size_t bytesWritten = fWriter.readFromStream(stream, length);
135     if (bytesWritten < length) {
136         fWriter.reservePad(length - bytesWritten);
137     }
138     return bytesWritten;
139 }
140 
writeToStream(SkWStream * stream)141 bool SkWriteBuffer::writeToStream(SkWStream* stream) {
142     return fWriter.writeToStream(stream);
143 }
144 
write_encoded_bitmap(SkWriteBuffer * buffer,SkData * data,const SkIPoint & origin)145 static void write_encoded_bitmap(SkWriteBuffer* buffer, SkData* data,
146                                  const SkIPoint& origin) {
147     buffer->writeUInt(SkToU32(data->size()));
148     buffer->getWriter32()->writePad(data->data(), data->size());
149     buffer->write32(origin.fX);
150     buffer->write32(origin.fY);
151 }
152 
writeBitmap(const SkBitmap & bitmap)153 void SkWriteBuffer::writeBitmap(const SkBitmap& bitmap) {
154     // Record the width and height. This way if readBitmap fails a dummy bitmap can be drawn at the
155     // right size.
156     this->writeInt(bitmap.width());
157     this->writeInt(bitmap.height());
158 
159     // Record information about the bitmap in one of three ways, in order of priority:
160     // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoid serializing the
161     //    bitmap entirely or serialize it later as desired. A boolean value of true will be written
162     //    to the stream to signify that a heap was used.
163     // 2. If there is a function for encoding bitmaps, use it to write an encoded version of the
164     //    bitmap. After writing a boolean value of false, signifying that a heap was not used, write
165     //    the size of the encoded data. A non-zero size signifies that encoded data was written.
166     // 3. Call SkBitmap::flatten. After writing a boolean value of false, signifying that a heap was
167     //    not used, write a zero to signify that the data was not encoded.
168     bool useBitmapHeap = fBitmapHeap != NULL;
169     // Write a bool: true if the SkBitmapHeap is to be used, in which case the reader must use an
170     // SkBitmapHeapReader to read the SkBitmap. False if the bitmap was serialized another way.
171     this->writeBool(useBitmapHeap);
172     if (useBitmapHeap) {
173         SkASSERT(NULL == fPixelSerializer);
174         int32_t slot = fBitmapHeap->insert(bitmap);
175         fWriter.write32(slot);
176         // crbug.com/155875
177         // The generation ID is not required information. We write it to prevent collisions
178         // in SkFlatDictionary.  It is possible to get a collision when a previously
179         // unflattened (i.e. stale) instance of a similar flattenable is in the dictionary
180         // and the instance currently being written is re-using the same slot from the
181         // bitmap heap.
182         fWriter.write32(bitmap.getGenerationID());
183         return;
184     }
185 
186     SkPixelRef* pixelRef = bitmap.pixelRef();
187     if (pixelRef) {
188         // see if the pixelref already has an encoded version
189         SkAutoDataUnref existingData(pixelRef->refEncodedData());
190         if (existingData.get() != NULL) {
191             // Assumes that if the client did not set a serializer, they are
192             // happy to get the encoded data.
193             if (!fPixelSerializer || fPixelSerializer->useEncodedData(existingData->data(),
194                                                                       existingData->size())) {
195                 write_encoded_bitmap(this, existingData, bitmap.pixelRefOrigin());
196                 return;
197             }
198         }
199 
200         // see if the caller wants to manually encode
201         if (fPixelSerializer) {
202             SkASSERT(NULL == fBitmapHeap);
203             SkAutoLockPixels alp(bitmap);
204             SkAutoDataUnref data(fPixelSerializer->encodePixels(bitmap.info(),
205                                                                 bitmap.getPixels(),
206                                                                 bitmap.rowBytes()));
207             if (data.get() != NULL) {
208                 // if we have to "encode" the bitmap, then we assume there is no
209                 // offset to share, since we are effectively creating a new pixelref
210                 write_encoded_bitmap(this, data, SkIPoint::Make(0, 0));
211                 return;
212             }
213         }
214     }
215 
216     this->writeUInt(0); // signal raw pixels
217     SkBitmap::WriteRawPixels(this, bitmap);
218 }
219 
writeTypeface(SkTypeface * obj)220 void SkWriteBuffer::writeTypeface(SkTypeface* obj) {
221     if (NULL == obj || NULL == fTFSet) {
222         fWriter.write32(0);
223     } else {
224         fWriter.write32(fTFSet->add(obj));
225     }
226 }
227 
setFactoryRecorder(SkFactorySet * rec)228 SkFactorySet* SkWriteBuffer::setFactoryRecorder(SkFactorySet* rec) {
229     SkRefCnt_SafeAssign(fFactorySet, rec);
230     if (fNamedFactorySet != NULL) {
231         fNamedFactorySet->unref();
232         fNamedFactorySet = NULL;
233     }
234     return rec;
235 }
236 
setNamedFactoryRecorder(SkNamedFactorySet * rec)237 SkNamedFactorySet* SkWriteBuffer::setNamedFactoryRecorder(SkNamedFactorySet* rec) {
238     SkRefCnt_SafeAssign(fNamedFactorySet, rec);
239     if (fFactorySet != NULL) {
240         fFactorySet->unref();
241         fFactorySet = NULL;
242     }
243     return rec;
244 }
245 
setTypefaceRecorder(SkRefCntSet * rec)246 SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) {
247     SkRefCnt_SafeAssign(fTFSet, rec);
248     return rec;
249 }
250 
setBitmapHeap(SkBitmapHeap * bitmapHeap)251 void SkWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) {
252     SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap);
253     if (bitmapHeap != NULL) {
254         SkASSERT(NULL == fPixelSerializer);
255         fPixelSerializer.reset(NULL);
256     }
257 }
258 
setPixelSerializer(SkPixelSerializer * serializer)259 void SkWriteBuffer::setPixelSerializer(SkPixelSerializer* serializer) {
260     fPixelSerializer.reset(serializer);
261     if (serializer) {
262         serializer->ref();
263         SkASSERT(NULL == fBitmapHeap);
264         SkSafeUnref(fBitmapHeap);
265         fBitmapHeap = NULL;
266     }
267 }
268 
writeFlattenable(const SkFlattenable * flattenable)269 void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
270     /*
271      *  If we have a factoryset, then the first 32bits tell us...
272      *       0: failure to write the flattenable
273      *      >0: (1-based) index into the SkFactorySet or SkNamedFactorySet
274      *  If we don't have a factoryset, then the first "ptr" is either the
275      *  factory, or null for failure.
276      *
277      *  The distinction is important, since 0-index is 32bits (always), but a
278      *  0-functionptr might be 32 or 64 bits.
279      */
280     if (NULL == flattenable) {
281         if (this->isValidating()) {
282             this->writeString("");
283         } else if (fFactorySet != NULL || fNamedFactorySet != NULL) {
284             this->write32(0);
285         } else {
286             this->writeFunctionPtr(NULL);
287         }
288         return;
289     }
290 
291     SkFlattenable::Factory factory = flattenable->getFactory();
292     SkASSERT(factory != NULL);
293 
294     /*
295      *  We can write 1 of 3 versions of the flattenable:
296      *  1.  function-ptr : this is the fastest for the reader, but assumes that
297      *      the writer and reader are in the same process.
298      *  2.  index into fFactorySet : This is assumes the writer will later
299      *      resolve the function-ptrs into strings for its reader. SkPicture
300      *      does exactly this, by writing a table of names (matching the indices)
301      *      up front in its serialized form.
302      *  3.  index into fNamedFactorySet. fNamedFactorySet will also store the
303      *      name. SkGPipe uses this technique so it can write the name to its
304      *      stream before writing the flattenable.
305      */
306     if (this->isValidating()) {
307         this->writeString(flattenable->getTypeName());
308     } else if (fFactorySet) {
309         this->write32(fFactorySet->add(factory));
310     } else if (fNamedFactorySet) {
311         int32_t index = fNamedFactorySet->find(factory);
312         this->write32(index);
313         if (0 == index) {
314             return;
315         }
316     } else {
317         this->writeFunctionPtr((void*)factory);
318     }
319 
320     // make room for the size of the flattened object
321     (void)fWriter.reserve(sizeof(uint32_t));
322     // record the current size, so we can subtract after the object writes.
323     size_t offset = fWriter.bytesWritten();
324     // now flatten the object
325     flattenable->flatten(*this);
326     size_t objSize = fWriter.bytesWritten() - offset;
327     // record the obj's size
328     fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize));
329 }
330