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
8#include "include/private/SkTo.h"
9#include "src/core/SkMatrixPriv.h"
10#include "src/core/SkWriter32.h"
11
12void SkWriter32::writeMatrix(const SkMatrix& matrix) {
13 size_t size = SkMatrixPriv::WriteToMemory(matrix, nullptr);
14 SkASSERT(SkAlign4(size) == size);
15 SkMatrixPriv::WriteToMemory(matrix, this->reserve(size));
16}
17
18void SkWriter32::writeString(const char str[], size_t len) {
19 if (nullptr == str) {
20 str = "";
21 len = 0;
22 }
23 if ((long)len < 0) {
24 len = strlen(str);
25 }
26
27 // [ 4 byte len ] [ str ... ] [1 - 4 \0s]
28 uint32_t* ptr = this->reservePad(sizeof(uint32_t) + len + 1);
29 *ptr = SkToU32(len);
30 char* chars = (char*)(ptr + 1);
31 memcpy(chars, str, len);
32 chars[len] = '\0';
33}
34
35size_t SkWriter32::WriteStringSize(const char* str, size_t len) {
36 if ((long)len < 0) {
37 SkASSERT(str);
38 len = strlen(str);
39 }
40 const size_t lenBytes = 4; // we use 4 bytes to record the length
41 // add 1 since we also write a terminating 0
42 return SkAlign4(lenBytes + len + 1);
43}
44
45void SkWriter32::growToAtLeast(size_t size) {
46 const bool wasExternal = (fExternal != nullptr) && (fData == fExternal);
47
48 fCapacity = 4096 + std::max(size, fCapacity + (fCapacity / 2));
49 fInternal.realloc(fCapacity);
50 fData = fInternal.get();
51
52 if (wasExternal) {
53 // we were external, so copy in the data
54 memcpy(fData, fExternal, fUsed);
55 }
56}
57
58sk_sp<SkData> SkWriter32::snapshotAsData() const {
59 return SkData::MakeWithCopy(fData, fUsed);
60}
61