Flutter Engine
The Flutter Engine
SkWriteBuffer.cpp
Go to the documentation of this file.
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
9
11#include "include/core/SkData.h"
16#include "include/core/SkRect.h"
22#include "src/core/SkMipmap.h"
26
27#if !defined(SK_DISABLE_LEGACY_PNG_WRITEBUFFER)
31#endif
32
33#include <cstring>
34#include <utility>
35
36class SkMatrix;
37class SkPaint;
38class SkRegion;
39
40///////////////////////////////////////////////////////////////////////////////////////////////////
41
43 : SkWriteBuffer(p), fFactorySet(nullptr), fTFSet(nullptr) {}
44
45SkBinaryWriteBuffer::SkBinaryWriteBuffer(void* storage, size_t storageSize, const SkSerialProcs& p)
46 : SkWriteBuffer(p), fFactorySet(nullptr), fTFSet(nullptr), fWriter(storage, storageSize) {}
47
49
51 return fWriter.usingInitialStorage();
52}
53
55 fWriter.write32(SkToU32(size));
56 fWriter.writePad(data, size);
57}
58
60 fWriter.writeBool(value);
61}
62
64 fWriter.writeScalar(value);
65}
66
68 fWriter.write32(count);
69 fWriter.write(value, count * sizeof(SkScalar));
70}
71
73 fWriter.write32(value);
74}
75
76void SkBinaryWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) {
77 fWriter.write32(count);
78 fWriter.write(value, count * sizeof(int32_t));
79}
80
82 fWriter.write32(value);
83}
84
86 fWriter.writeString(value.data(), value.size());
87}
88
90 fWriter.write32(color);
91}
92
94 fWriter.write32(count);
95 fWriter.write(color, count * sizeof(SkColor));
96}
97
99 fWriter.write(&color, sizeof(SkColor4f));
100}
101
103 fWriter.write32(count);
104 fWriter.write(color, count * sizeof(SkColor4f));
105}
106
108 fWriter.writeScalar(point.fX);
109 fWriter.writeScalar(point.fY);
110}
111
113 this->writePad32(&point, sizeof(SkPoint3));
114}
115
117 fWriter.write32(count);
118 fWriter.write(point, count * sizeof(SkPoint));
119}
120
122 fWriter.write(SkMatrixPriv::M44ColMajor(matrix), sizeof(float) * 16);
123}
124
126 fWriter.writeMatrix(matrix);
127}
128
130 fWriter.write(&rect, sizeof(SkIRect));
131}
132
134 fWriter.writeRect(rect);
135}
136
138 fWriter.writeRegion(region);
139}
140
142 fWriter.writeSampling(sampling);
143}
144
146 fWriter.writePath(path);
147}
148
150 fWriter.write32(SkToU32(length));
151 size_t bytesWritten = fWriter.readFromStream(stream, length);
152 if (bytesWritten < length) {
153 fWriter.reservePad(length - bytesWritten);
154 }
155 return bytesWritten;
156}
157
159 return fWriter.writeToStream(stream);
160}
161
164 if (procs.fImageProc) {
165 data = procs.fImageProc(const_cast<SkImage*>(image), procs.fImageCtx);
166 }
167 if (data) {
168 return data;
169 }
170 // Check to see if the image's source was an encoded block of data.
171 // If so, just use that.
173 if (data) {
174 return data;
175 }
176#if !defined(SK_DISABLE_LEGACY_PNG_WRITEBUFFER)
177 SkBitmap bm;
178 auto ib = as_IB(image);
179 if (!ib->getROPixels(ib->directContext(), &bm)) {
180 return nullptr;
181 }
184 return stream.detachAsData();
185 }
186#endif
187 return nullptr;
188}
189
191 /* Format
192 count_levels:32
193 for each level, starting with the biggest (index 0 in our iterator)
194 encoded_size:32
195 encoded_data (padded)
196 */
197 const int count = mipmap->countLevels();
198
199 // This buffer does not need procs because it is just writing SkDatas
201 buffer.write32(count);
202 for (int i = 0; i < count; ++i) {
204 if (mipmap->getLevel(i, &level)) {
205 sk_sp<SkImage> levelImage = SkImages::RasterFromPixmap(level.fPixmap, nullptr, nullptr);
206 sk_sp<SkData> levelData = serialize_image(levelImage.get(), procs);
207 buffer.writeDataAsByteArray(levelData.get());
208 } else {
209 return nullptr;
210 }
211 }
212 return buffer.snapshotAsData();
213}
214
215/* Format:
216 * flags: U32
217 * encoded : size_32 + data[]
218 * [subset: IRect]
219 * [mips] : size_32 + data[]
220 */
222 uint32_t flags = 0;
223 const SkMipmap* mips = as_IB(image)->onPeekMips();
224 if (mips) {
226 }
229 }
230
231 this->write32(flags);
232
234 SkASSERT(data);
235 this->writeDataAsByteArray(data.get());
236
238 sk_sp<SkData> mipData = serialize_mipmap(mips, fProcs);
239 this->writeDataAsByteArray(mipData.get());
240 }
241}
242
244 // Write 32 bits (signed)
245 // 0 -- empty font
246 // >0 -- index
247 // <0 -- custom (serial procs)
248
249 if (obj == nullptr) {
250 fWriter.write32(0);
251 } else if (fProcs.fTypefaceProc) {
253 if (data) {
254 size_t size = data->size();
255 if (!SkTFitsIn<int32_t>(size)) {
256 size = 0; // fall back to default font
257 }
258 int32_t ssize = SkToS32(size);
259 fWriter.write32(-ssize); // negative to signal custom
260 if (size) {
261 this->writePad32(data->data(), size);
262 }
263 return;
264 }
265 // no data means fall through for std behavior
266 }
267 fWriter.write32(fTFSet ? fTFSet->add(obj) : 0);
268}
269
272}
273
275 fFactorySet = std::move(rec);
276}
277
279 fTFSet = std::move(rec);
280}
281
283 if (nullptr == flattenable) {
284 this->write32(0);
285 return;
286 }
287
288 /*
289 * We can write 1 of 2 versions of the flattenable:
290 *
291 * 1. index into fFactorySet: This assumes the writer will later resolve the function-ptrs
292 * into strings for its reader. SkPicture does exactly this, by writing a table of names
293 * (matching the indices) up front in its serialized form.
294 *
295 * 2. string name of the flattenable or index into fFlattenableDict: We store the string to
296 * allow the reader to specify its own factories after write time. In order to improve
297 * compression, if we have already written the string, we write its index instead.
298 */
299
300 if (SkFlattenable::Factory factory = flattenable->getFactory(); factory && fFactorySet) {
301 this->write32(fFactorySet->add(factory));
302 } else {
303 const char* name = flattenable->getTypeName();
304 SkASSERT(name);
305 SkASSERT(0 != strcmp("", name));
306
307 if (uint32_t* indexPtr = fFlattenableDict.find(name)) {
308 // We will write the index as a 32-bit int. We want the first byte
309 // that we send to be zero - this will act as a sentinel that we
310 // have an index (not a string). This means that we will send the
311 // the index shifted left by 8. The remaining 24-bits should be
312 // plenty to store the index. Note that this strategy depends on
313 // being little endian, and type names being non-empty.
314 SkASSERT(0 == *indexPtr >> 24);
315 this->write32(*indexPtr << 8);
316 } else {
317 this->writeString(name);
318 fFlattenableDict.set(name, fFlattenableDict.count() + 1);
319 }
320 }
321
322 // make room for the size of the flattened object
323 (void)fWriter.reserve(sizeof(uint32_t));
324 // record the current size, so we can subtract after the object writes.
325 size_t offset = fWriter.bytesWritten();
326 // now flatten the object
327 flattenable->flatten(*this);
328 size_t objSize = fWriter.bytesWritten() - offset;
329 // record the obj's size
330 fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize));
331}
int count
Definition: FontMgrTest.cpp:50
kUnpremul_SkAlphaType
#define SkASSERT(cond)
Definition: SkAssert.h:116
uint32_t SkColor
Definition: SkColor.h:37
static SkImage_Base * as_IB(SkImage *image)
Definition: SkImage_Base.h:201
constexpr int32_t SkToS32(S x)
Definition: SkTo.h:25
constexpr uint32_t SkToU32(S x)
Definition: SkTo.h:26
static sk_sp< SkData > serialize_image(const SkImage *image, SkSerialProcs procs)
static sk_sp< SkData > serialize_mipmap(const SkMipmap *mipmap, SkSerialProcs procs)
@ kUnpremul
@ kHasMipmap
void writeBool(bool value) override
void writeByteArray(const void *data, size_t size) override
void writeFlattenable(const SkFlattenable *flattenable) override
void writeColor(SkColor color) override
void writeIntArray(const int32_t *value, uint32_t count) override
void writePoint(const SkPoint &point) override
void writeColor4f(const SkColor4f &color) override
void writeIRect(const SkIRect &rect) override
size_t writeStream(SkStream *stream, size_t length) override
void writeColorArray(const SkColor *color, uint32_t count) override
void writeTypeface(SkTypeface *typeface) override
void writeScalar(SkScalar value) override
void writeScalarArray(const SkScalar *value, uint32_t count) override
size_t bytesWritten() const
void writePointArray(const SkPoint *point, uint32_t count) override
bool writeToStream(SkWStream *) const
void writeString(std::string_view value) override
void writePoint3(const SkPoint3 &point) override
void writePath(const SkPath &path) override
void writeColor4fArray(const SkColor4f *color, uint32_t count) override
SkBinaryWriteBuffer(const SkSerialProcs &)
void writePaint(const SkPaint &paint) override
void writeUInt(uint32_t value) override
void setTypefaceRecorder(sk_sp< SkRefCntSet >)
void setFactoryRecorder(sk_sp< SkFactorySet >)
~SkBinaryWriteBuffer() override
void writeRect(const SkRect &rect) override
void writeMatrix(const SkMatrix &matrix) override
void writeInt(int32_t value) override
void writeSampling(const SkSamplingOptions &) override
bool usingInitialStorage() const
void writeImage(const SkImage *) override
void writeRegion(const SkRegion &region) override
void write(const void *buffer, size_t bytes)
void writePad32(const void *buffer, size_t bytes) override
const SkPixmap & pixmap() const
Definition: SkBitmap.h:133
virtual void flatten(SkWriteBuffer &) const
Definition: SkFlattenable.h:67
sk_sp< SkFlattenable >(* Factory)(SkReadBuffer &)
Definition: SkFlattenable.h:41
virtual const char * getTypeName() const =0
virtual Factory getFactory() const =0
virtual SkMipmap * onPeekMips() const
Definition: SkImage_Base.h:84
SkAlphaType alphaType() const
Definition: SkImage.cpp:154
sk_sp< SkData > refEncodedData() const
Definition: SkImage.cpp:214
Definition: SkM44.h:150
static const SkScalar * M44ColMajor(const SkM44 &m)
Definition: SkMatrixPriv.h:185
int countLevels() const
Definition: SkMipmap.cpp:276
bool getLevel(int index, Level *) const
Definition: SkMipmap.cpp:280
static void Flatten(const SkPaint &paint, SkWriteBuffer &buffer)
Definition: SkPath.h:59
uint32_t add(T ptr)
void writeDataAsByteArray(const SkData *data)
Definition: SkWriteBuffer.h:49
SkSerialProcs fProcs
Definition: SkWriteBuffer.h:91
void write32(int32_t value)
Definition: SkWriteBuffer.h:63
void overwriteTAt(size_t offset, const T &value)
Definition: SkWriter32.h:94
void write32(int32_t value)
Definition: SkWriter32.h:117
void write(const void *values, size_t size)
Definition: SkWriter32.h:170
bool usingInitialStorage() const
Definition: SkWriter32.h:52
void writeSampling(const SkSamplingOptions &sampling)
Definition: SkWriter32.cpp:24
uint32_t * reservePad(size_t size)
Definition: SkWriter32.h:179
void writeRect(const SkRect &rect)
Definition: SkWriter32.h:133
void writeScalar(SkScalar value)
Definition: SkWriter32.h:121
size_t readFromStream(SkStream *stream, size_t length)
Definition: SkWriter32.h:245
void writeMatrix(const SkMatrix &matrix)
Definition: SkWriter32.cpp:18
uint32_t * reserve(size_t size)
Definition: SkWriter32.h:67
void writePad(const void *src, size_t size)
Definition: SkWriter32.h:192
void writePath(const SkPath &path)
Definition: SkWriter32.h:145
bool writeToStream(SkWStream *stream) const
Definition: SkWriter32.h:239
bool writeBool(bool value)
Definition: SkWriter32.h:100
void writeRegion(const SkRegion &rgn)
Definition: SkWriter32.h:153
void writeString(const char *str, size_t len=(size_t) -1)
Definition: SkWriter32.cpp:38
size_t bytesWritten() const
Definition: SkWriter32.h:48
T * get() const
Definition: SkRefCnt.h:303
int count() const
Definition: SkTHash.h:471
V * find(const K &key) const
Definition: SkTHash.h:494
V * set(K key, V val)
Definition: SkTHash.h:487
const Paint & paint
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
FlutterSemanticsFlag flags
uint8_t value
size_t length
SK_API sk_sp< SkImage > RasterFromPixmap(const SkPixmap &pixmap, RasterReleaseProc rasterReleaseProc, ReleaseContext releaseContext)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< const SkImage > image
Definition: SkRecords.h:269
ClipOpAndAA opAA SkRegion region
Definition: SkRecords.h:238
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
SkSamplingOptions sampling
Definition: SkRecords.h:337
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
SeparatedVector2 offset
Definition: SkRect.h:32
float fX
x-axis value
Definition: SkPoint_impl.h:164
float fY
y-axis value
Definition: SkPoint_impl.h:165
void * fImageCtx
Definition: SkSerialProcs.h:91
SkSerialImageProc fImageProc
Definition: SkSerialProcs.h:90
void * fTypefaceCtx
Definition: SkSerialProcs.h:94
SkSerialTypefaceProc fTypefaceProc
Definition: SkSerialProcs.h:93
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63