• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 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 #include "src/core/SkWriteBuffer.h"
9 
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkM44.h"
13 #include "include/core/SkStream.h"
14 #include "include/core/SkTypeface.h"
15 #include "include/private/SkTo.h"
16 #include "src/core/SkImagePriv.h"
17 #include "src/core/SkMatrixPriv.h"
18 #include "src/core/SkPaintPriv.h"
19 #include "src/core/SkPtrRecorder.h"
20 
21 ///////////////////////////////////////////////////////////////////////////////////////////////////
22 
SkBinaryWriteBuffer()23 SkBinaryWriteBuffer::SkBinaryWriteBuffer()
24     : fFactorySet(nullptr)
25     , fTFSet(nullptr) {
26 }
27 
SkBinaryWriteBuffer(void * storage,size_t storageSize)28 SkBinaryWriteBuffer::SkBinaryWriteBuffer(void* storage, size_t storageSize)
29     : fFactorySet(nullptr)
30     , fTFSet(nullptr)
31     , fWriter(storage, storageSize)
32 {}
33 
~SkBinaryWriteBuffer()34 SkBinaryWriteBuffer::~SkBinaryWriteBuffer() {}
35 
usingInitialStorage() const36 bool SkBinaryWriteBuffer::usingInitialStorage() const {
37     return fWriter.usingInitialStorage();
38 }
39 
writeByteArray(const void * data,size_t size)40 void SkBinaryWriteBuffer::writeByteArray(const void* data, size_t size) {
41     fWriter.write32(SkToU32(size));
42     fWriter.writePad(data, size);
43 }
44 
writeBool(bool value)45 void SkBinaryWriteBuffer::writeBool(bool value) {
46     fWriter.writeBool(value);
47 }
48 
writeScalar(SkScalar value)49 void SkBinaryWriteBuffer::writeScalar(SkScalar value) {
50     fWriter.writeScalar(value);
51 }
52 
writeScalarArray(const SkScalar * value,uint32_t count)53 void SkBinaryWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) {
54     fWriter.write32(count);
55     fWriter.write(value, count * sizeof(SkScalar));
56 }
57 
writeInt(int32_t value)58 void SkBinaryWriteBuffer::writeInt(int32_t value) {
59     fWriter.write32(value);
60 }
61 
writeIntArray(const int32_t * value,uint32_t count)62 void SkBinaryWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) {
63     fWriter.write32(count);
64     fWriter.write(value, count * sizeof(int32_t));
65 }
66 
writeUInt(uint32_t value)67 void SkBinaryWriteBuffer::writeUInt(uint32_t value) {
68     fWriter.write32(value);
69 }
70 
writeString(const char * value)71 void SkBinaryWriteBuffer::writeString(const char* value) {
72     fWriter.writeString(value);
73 }
74 
writeColor(SkColor color)75 void SkBinaryWriteBuffer::writeColor(SkColor color) {
76     fWriter.write32(color);
77 }
78 
writeColorArray(const SkColor * color,uint32_t count)79 void SkBinaryWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) {
80     fWriter.write32(count);
81     fWriter.write(color, count * sizeof(SkColor));
82 }
83 
writeColor4f(const SkColor4f & color)84 void SkBinaryWriteBuffer::writeColor4f(const SkColor4f& color) {
85     fWriter.write(&color, sizeof(SkColor4f));
86 }
87 
writeColor4fArray(const SkColor4f * color,uint32_t count)88 void SkBinaryWriteBuffer::writeColor4fArray(const SkColor4f* color, uint32_t count) {
89     fWriter.write32(count);
90     fWriter.write(color, count * sizeof(SkColor4f));
91 }
92 
writePoint(const SkPoint & point)93 void SkBinaryWriteBuffer::writePoint(const SkPoint& point) {
94     fWriter.writeScalar(point.fX);
95     fWriter.writeScalar(point.fY);
96 }
97 
writePoint3(const SkPoint3 & point)98 void SkBinaryWriteBuffer::writePoint3(const SkPoint3& point) {
99     this->writePad32(&point, sizeof(SkPoint3));
100 }
101 
writePointArray(const SkPoint * point,uint32_t count)102 void SkBinaryWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) {
103     fWriter.write32(count);
104     fWriter.write(point, count * sizeof(SkPoint));
105 }
106 
write(const SkM44 & matrix)107 void SkBinaryWriteBuffer::write(const SkM44& matrix) {
108     fWriter.write(SkMatrixPriv::M44ColMajor(matrix), sizeof(float) * 16);
109 }
110 
writeMatrix(const SkMatrix & matrix)111 void SkBinaryWriteBuffer::writeMatrix(const SkMatrix& matrix) {
112     fWriter.writeMatrix(matrix);
113 }
114 
writeIRect(const SkIRect & rect)115 void SkBinaryWriteBuffer::writeIRect(const SkIRect& rect) {
116     fWriter.write(&rect, sizeof(SkIRect));
117 }
118 
writeRect(const SkRect & rect)119 void SkBinaryWriteBuffer::writeRect(const SkRect& rect) {
120     fWriter.writeRect(rect);
121 }
122 
writeRegion(const SkRegion & region)123 void SkBinaryWriteBuffer::writeRegion(const SkRegion& region) {
124     fWriter.writeRegion(region);
125 }
126 
writePath(const SkPath & path)127 void SkBinaryWriteBuffer::writePath(const SkPath& path) {
128     fWriter.writePath(path);
129 }
130 
writeStream(SkStream * stream,size_t length)131 size_t SkBinaryWriteBuffer::writeStream(SkStream* stream, size_t length) {
132     fWriter.write32(SkToU32(length));
133     size_t bytesWritten = fWriter.readFromStream(stream, length);
134     if (bytesWritten < length) {
135         fWriter.reservePad(length - bytesWritten);
136     }
137     return bytesWritten;
138 }
139 
writeToStream(SkWStream * stream) const140 bool SkBinaryWriteBuffer::writeToStream(SkWStream* stream) const {
141     return fWriter.writeToStream(stream);
142 }
143 
144 #include "src/image/SkImage_Base.h"
145 
146 /*  Format:
147  *      flags: U32
148  *      encoded : size_32 + data[]
149  *      [subset: IRect]
150  *      [mips]  : size_32 + data[]
151  */
writeImage(const SkImage * image)152 void SkBinaryWriteBuffer::writeImage(const SkImage* image) {
153     uint32_t flags = 0;
154     const SkMipmap* mips = as_IB(image)->onPeekMips();
155     if (mips) {
156         flags |= SkWriteBufferImageFlags::kHasMipmap;
157     }
158 
159     this->write32(flags);
160 
161     sk_sp<SkData> data;
162     if (fProcs.fImageProc) {
163         data = fProcs.fImageProc(const_cast<SkImage*>(image), fProcs.fImageCtx);
164     }
165     if (!data) {
166         data = image->encodeToData();
167     }
168     this->writeDataAsByteArray(data.get());
169 
170     if (flags & SkWriteBufferImageFlags::kHasMipmap) {
171         this->writeDataAsByteArray(mips->serialize().get());
172     }
173 }
174 
writeTypeface(SkTypeface * obj)175 void SkBinaryWriteBuffer::writeTypeface(SkTypeface* obj) {
176     // Write 32 bits (signed)
177     //   0 -- default font
178     //  >0 -- index
179     //  <0 -- custom (serial procs)
180 
181     if (obj == nullptr) {
182         fWriter.write32(0);
183     } else if (fProcs.fTypefaceProc) {
184         auto data = fProcs.fTypefaceProc(obj, fProcs.fTypefaceCtx);
185         if (data) {
186             size_t size = data->size();
187             if (!SkTFitsIn<int32_t>(size)) {
188                 size = 0;               // fall back to default font
189             }
190             int32_t ssize = SkToS32(size);
191             fWriter.write32(-ssize);    // negative to signal custom
192             if (size) {
193                 this->writePad32(data->data(), size);
194             }
195             return;
196         }
197         // no data means fall through for std behavior
198     }
199     fWriter.write32(fTFSet ? fTFSet->add(obj) : 0);
200 }
201 
writePaint(const SkPaint & paint)202 void SkBinaryWriteBuffer::writePaint(const SkPaint& paint) {
203     SkPaintPriv::Flatten(paint, *this);
204 }
205 
setFactoryRecorder(sk_sp<SkFactorySet> rec)206 void SkBinaryWriteBuffer::setFactoryRecorder(sk_sp<SkFactorySet> rec) {
207     fFactorySet = std::move(rec);
208 }
209 
setTypefaceRecorder(sk_sp<SkRefCntSet> rec)210 void SkBinaryWriteBuffer::setTypefaceRecorder(sk_sp<SkRefCntSet> rec) {
211     fTFSet = std::move(rec);
212 }
213 
writeFlattenable(const SkFlattenable * flattenable)214 void SkBinaryWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
215     if (nullptr == flattenable) {
216         this->write32(0);
217         return;
218     }
219 
220     /*
221      *  We can write 1 of 2 versions of the flattenable:
222      *
223      *  1. index into fFactorySet: This assumes the writer will later resolve the function-ptrs
224      *     into strings for its reader. SkPicture does exactly this, by writing a table of names
225      *     (matching the indices) up front in its serialized form.
226      *
227      *  2. string name of the flattenable or index into fFlattenableDict:  We store the string to
228      *     allow the reader to specify its own factories after write time. In order to improve
229      *     compression, if we have already written the string, we write its index instead.
230      */
231 
232     if (SkFlattenable::Factory factory = flattenable->getFactory(); factory && fFactorySet) {
233         this->write32(fFactorySet->add(factory));
234     } else {
235         const char* name = flattenable->getTypeName();
236         SkASSERT(name);
237         SkASSERT(0 != strcmp("", name));
238 
239         if (uint32_t* indexPtr = fFlattenableDict.find(name)) {
240             // We will write the index as a 32-bit int.  We want the first byte
241             // that we send to be zero - this will act as a sentinel that we
242             // have an index (not a string).  This means that we will send the
243             // the index shifted left by 8.  The remaining 24-bits should be
244             // plenty to store the index.  Note that this strategy depends on
245             // being little endian, and type names being non-empty.
246             SkASSERT(0 == *indexPtr >> 24);
247             this->write32(*indexPtr << 8);
248         } else {
249             this->writeString(name);
250             fFlattenableDict.set(name, fFlattenableDict.count() + 1);
251         }
252     }
253 
254     // make room for the size of the flattened object
255     (void)fWriter.reserve(sizeof(uint32_t));
256     // record the current size, so we can subtract after the object writes.
257     size_t offset = fWriter.bytesWritten();
258     // now flatten the object
259     flattenable->flatten(*this);
260     size_t objSize = fWriter.bytesWritten() - offset;
261     // record the obj's size
262     fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize));
263 }
264