| 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 "src/core/SkWriter32.h" |
| 9 |
|
| 10 |
#include "include/core/SkSamplingOptions.h" |
| 11 |
#include "include/private/base/SkTo.h" |
| 12 |
#include "src/core/SkMatrixPriv.h" |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
|
| 16 |
class SkMatrix; |
| 17 |
|
| 18 |
void 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 |
|
| 24 |
void SkWriter32::writeSampling(const SkSamplingOptions& sampling) { |
| 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 |
|
| 38 |
void 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 |
|
| 55 |
size_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 |
|
| 65 |
void 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 |
|
| 78 |
sk_sp<SkData> SkWriter32::snapshotAsData() const { |
| 79 |
return SkData::MakeWithCopy(fData, fUsed); |
| 80 |
} |