Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkWriter32.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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
13
14#include <algorithm>
15
16class SkMatrix;
17
18void SkWriter32::writeMatrix(const SkMatrix& matrix) {
19 size_t size = SkMatrixPriv::WriteToMemory(matrix, nullptr);
20 SkASSERT(SkAlign4(size) == size);
21 SkMatrixPriv::WriteToMemory(matrix, this->reserve(size));
22}
23
25 this->write32(sampling.maxAniso);
26 if (!sampling.isAniso()) {
27 this->writeBool(sampling.useCubic);
28 if (sampling.useCubic) {
29 this->writeScalar(sampling.cubic.B);
30 this->writeScalar(sampling.cubic.C);
31 } else {
32 this->write32((unsigned)sampling.filter);
33 this->write32((unsigned)sampling.mipmap);
34 }
35 }
36}
37
38void SkWriter32::writeString(const char str[], size_t len) {
39 if (nullptr == str) {
40 str = "";
41 len = 0;
42 }
43 if ((long)len < 0) {
44 len = strlen(str);
45 }
46
47 // [ 4 byte len ] [ str ... ] [1 - 4 \0s]
48 uint32_t* ptr = this->reservePad(sizeof(uint32_t) + len + 1);
49 *ptr = SkToU32(len);
50 char* chars = (char*)(ptr + 1);
51 memcpy(chars, str, len);
52 chars[len] = '\0';
53}
54
55size_t SkWriter32::WriteStringSize(const char* str, size_t len) {
56 if ((long)len < 0) {
57 SkASSERT(str);
58 len = strlen(str);
59 }
60 const size_t lenBytes = 4; // we use 4 bytes to record the length
61 // add 1 since we also write a terminating 0
62 return SkAlign4(lenBytes + len + 1);
63}
64
65void SkWriter32::growToAtLeast(size_t size) {
66 const bool wasExternal = (fExternal != nullptr) && (fData == fExternal);
67
68 fCapacity = 4096 + std::max(size, fCapacity + (fCapacity / 2));
69 fInternal.realloc(fCapacity);
70 fData = fInternal.get();
71
72 if (wasExternal) {
73 // we were external, so copy in the data
74 memcpy(fData, fExternal, fUsed);
75 }
76}
77
79 return SkData::MakeWithCopy(fData, fUsed);
80}
static constexpr T SkAlign4(T x)
Definition SkAlign.h:16
#define SkASSERT(cond)
Definition SkAssert.h:116
constexpr uint32_t SkToU32(S x)
Definition SkTo.h:26
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition SkData.cpp:111
static size_t WriteToMemory(const SkMatrix &matrix, void *buffer)
void write32(int32_t value)
Definition SkWriter32.h:117
void writeSampling(const SkSamplingOptions &sampling)
uint32_t * reservePad(size_t size)
Definition SkWriter32.h:179
void writeScalar(SkScalar value)
Definition SkWriter32.h:121
void writeMatrix(const SkMatrix &matrix)
uint32_t * reserve(size_t size)
Definition SkWriter32.h:67
static size_t WriteStringSize(const char *str, size_t len=(size_t) -1)
bool writeBool(bool value)
Definition SkWriter32.h:100
sk_sp< SkData > snapshotAsData() const
void writeString(const char *str, size_t len=(size_t) -1)
void realloc(size_t count)
const SkCubicResampler cubic
const SkFilterMode filter
const SkMipmapMode mipmap