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