michael@0: michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkWriteBuffer.h" michael@0: #include "SkBitmap.h" michael@0: #include "SkData.h" michael@0: #include "SkPixelRef.h" michael@0: #include "SkPtrRecorder.h" michael@0: #include "SkStream.h" michael@0: #include "SkTypeface.h" michael@0: michael@0: SkWriteBuffer::SkWriteBuffer(uint32_t flags) michael@0: : fFlags(flags) michael@0: , fFactorySet(NULL) michael@0: , fNamedFactorySet(NULL) michael@0: , fBitmapHeap(NULL) michael@0: , fTFSet(NULL) michael@0: , fBitmapEncoder(NULL) { michael@0: } michael@0: michael@0: SkWriteBuffer::SkWriteBuffer(void* storage, size_t storageSize, uint32_t flags) michael@0: : fFlags(flags) michael@0: , fFactorySet(NULL) michael@0: , fNamedFactorySet(NULL) michael@0: , fWriter(storage, storageSize) michael@0: , fBitmapHeap(NULL) michael@0: , fTFSet(NULL) michael@0: , fBitmapEncoder(NULL) { michael@0: } michael@0: michael@0: SkWriteBuffer::~SkWriteBuffer() { michael@0: SkSafeUnref(fFactorySet); michael@0: SkSafeUnref(fNamedFactorySet); michael@0: SkSafeUnref(fBitmapHeap); michael@0: SkSafeUnref(fTFSet); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeByteArray(const void* data, size_t size) { michael@0: fWriter.write32(SkToU32(size)); michael@0: fWriter.writePad(data, size); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeBool(bool value) { michael@0: fWriter.writeBool(value); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeFixed(SkFixed value) { michael@0: fWriter.write32(value); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeScalar(SkScalar value) { michael@0: fWriter.writeScalar(value); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) { michael@0: fWriter.write32(count); michael@0: fWriter.write(value, count * sizeof(SkScalar)); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeInt(int32_t value) { michael@0: fWriter.write32(value); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) { michael@0: fWriter.write32(count); michael@0: fWriter.write(value, count * sizeof(int32_t)); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeUInt(uint32_t value) { michael@0: fWriter.write32(value); michael@0: } michael@0: michael@0: void SkWriteBuffer::write32(int32_t value) { michael@0: fWriter.write32(value); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeString(const char* value) { michael@0: fWriter.writeString(value); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeEncodedString(const void* value, size_t byteLength, michael@0: SkPaint::TextEncoding encoding) { michael@0: fWriter.writeInt(encoding); michael@0: fWriter.writeInt(SkToU32(byteLength)); michael@0: fWriter.write(value, byteLength); michael@0: } michael@0: michael@0: michael@0: void SkWriteBuffer::writeColor(const SkColor& color) { michael@0: fWriter.write32(color); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) { michael@0: fWriter.write32(count); michael@0: fWriter.write(color, count * sizeof(SkColor)); michael@0: } michael@0: michael@0: void SkWriteBuffer::writePoint(const SkPoint& point) { michael@0: fWriter.writeScalar(point.fX); michael@0: fWriter.writeScalar(point.fY); michael@0: } michael@0: michael@0: void SkWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) { michael@0: fWriter.write32(count); michael@0: fWriter.write(point, count * sizeof(SkPoint)); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeMatrix(const SkMatrix& matrix) { michael@0: fWriter.writeMatrix(matrix); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeIRect(const SkIRect& rect) { michael@0: fWriter.write(&rect, sizeof(SkIRect)); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeRect(const SkRect& rect) { michael@0: fWriter.writeRect(rect); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeRegion(const SkRegion& region) { michael@0: fWriter.writeRegion(region); michael@0: } michael@0: michael@0: void SkWriteBuffer::writePath(const SkPath& path) { michael@0: fWriter.writePath(path); michael@0: } michael@0: michael@0: size_t SkWriteBuffer::writeStream(SkStream* stream, size_t length) { michael@0: fWriter.write32(SkToU32(length)); michael@0: size_t bytesWritten = fWriter.readFromStream(stream, length); michael@0: if (bytesWritten < length) { michael@0: fWriter.reservePad(length - bytesWritten); michael@0: } michael@0: return bytesWritten; michael@0: } michael@0: michael@0: bool SkWriteBuffer::writeToStream(SkWStream* stream) { michael@0: return fWriter.writeToStream(stream); michael@0: } michael@0: michael@0: static void write_encoded_bitmap(SkWriteBuffer* buffer, SkData* data, michael@0: const SkIPoint& origin) { michael@0: buffer->writeUInt(SkToU32(data->size())); michael@0: buffer->getWriter32()->writePad(data->data(), data->size()); michael@0: buffer->write32(origin.fX); michael@0: buffer->write32(origin.fY); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeBitmap(const SkBitmap& bitmap) { michael@0: // Record the width and height. This way if readBitmap fails a dummy bitmap can be drawn at the michael@0: // right size. michael@0: this->writeInt(bitmap.width()); michael@0: this->writeInt(bitmap.height()); michael@0: michael@0: // Record information about the bitmap in one of three ways, in order of priority: michael@0: // 1. If there is an SkBitmapHeap, store it in the heap. The client can avoid serializing the michael@0: // bitmap entirely or serialize it later as desired. A boolean value of true will be written michael@0: // to the stream to signify that a heap was used. michael@0: // 2. If there is a function for encoding bitmaps, use it to write an encoded version of the michael@0: // bitmap. After writing a boolean value of false, signifying that a heap was not used, write michael@0: // the size of the encoded data. A non-zero size signifies that encoded data was written. michael@0: // 3. Call SkBitmap::flatten. After writing a boolean value of false, signifying that a heap was michael@0: // not used, write a zero to signify that the data was not encoded. michael@0: bool useBitmapHeap = fBitmapHeap != NULL; michael@0: // Write a bool: true if the SkBitmapHeap is to be used, in which case the reader must use an michael@0: // SkBitmapHeapReader to read the SkBitmap. False if the bitmap was serialized another way. michael@0: this->writeBool(useBitmapHeap); michael@0: if (useBitmapHeap) { michael@0: SkASSERT(NULL == fBitmapEncoder); michael@0: int32_t slot = fBitmapHeap->insert(bitmap); michael@0: fWriter.write32(slot); michael@0: // crbug.com/155875 michael@0: // The generation ID is not required information. We write it to prevent collisions michael@0: // in SkFlatDictionary. It is possible to get a collision when a previously michael@0: // unflattened (i.e. stale) instance of a similar flattenable is in the dictionary michael@0: // and the instance currently being written is re-using the same slot from the michael@0: // bitmap heap. michael@0: fWriter.write32(bitmap.getGenerationID()); michael@0: return; michael@0: } michael@0: michael@0: // see if the pixelref already has an encoded version michael@0: if (bitmap.pixelRef()) { michael@0: SkAutoDataUnref data(bitmap.pixelRef()->refEncodedData()); michael@0: if (data.get() != NULL) { michael@0: write_encoded_bitmap(this, data, bitmap.pixelRefOrigin()); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // see if the caller wants to manually encode michael@0: if (fBitmapEncoder != NULL) { michael@0: SkASSERT(NULL == fBitmapHeap); michael@0: size_t offset = 0; // this parameter is deprecated/ignored michael@0: // if we have to "encode" the bitmap, then we assume there is no michael@0: // offset to share, since we are effectively creating a new pixelref michael@0: SkAutoDataUnref data(fBitmapEncoder(&offset, bitmap)); michael@0: if (data.get() != NULL) { michael@0: write_encoded_bitmap(this, data, SkIPoint::Make(0, 0)); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // Bitmap was not encoded. Record a zero, implying that the reader need not decode. michael@0: this->writeUInt(0); michael@0: bitmap.flatten(*this); michael@0: } michael@0: michael@0: void SkWriteBuffer::writeTypeface(SkTypeface* obj) { michael@0: if (NULL == obj || NULL == fTFSet) { michael@0: fWriter.write32(0); michael@0: } else { michael@0: fWriter.write32(fTFSet->add(obj)); michael@0: } michael@0: } michael@0: michael@0: SkFactorySet* SkWriteBuffer::setFactoryRecorder(SkFactorySet* rec) { michael@0: SkRefCnt_SafeAssign(fFactorySet, rec); michael@0: if (fNamedFactorySet != NULL) { michael@0: fNamedFactorySet->unref(); michael@0: fNamedFactorySet = NULL; michael@0: } michael@0: return rec; michael@0: } michael@0: michael@0: SkNamedFactorySet* SkWriteBuffer::setNamedFactoryRecorder(SkNamedFactorySet* rec) { michael@0: SkRefCnt_SafeAssign(fNamedFactorySet, rec); michael@0: if (fFactorySet != NULL) { michael@0: fFactorySet->unref(); michael@0: fFactorySet = NULL; michael@0: } michael@0: return rec; michael@0: } michael@0: michael@0: SkRefCntSet* SkWriteBuffer::setTypefaceRecorder(SkRefCntSet* rec) { michael@0: SkRefCnt_SafeAssign(fTFSet, rec); michael@0: return rec; michael@0: } michael@0: michael@0: void SkWriteBuffer::setBitmapHeap(SkBitmapHeap* bitmapHeap) { michael@0: SkRefCnt_SafeAssign(fBitmapHeap, bitmapHeap); michael@0: if (bitmapHeap != NULL) { michael@0: SkASSERT(NULL == fBitmapEncoder); michael@0: fBitmapEncoder = NULL; michael@0: } michael@0: } michael@0: michael@0: void SkWriteBuffer::setBitmapEncoder(SkPicture::EncodeBitmap bitmapEncoder) { michael@0: fBitmapEncoder = bitmapEncoder; michael@0: if (bitmapEncoder != NULL) { michael@0: SkASSERT(NULL == fBitmapHeap); michael@0: SkSafeUnref(fBitmapHeap); michael@0: fBitmapHeap = NULL; michael@0: } michael@0: } michael@0: michael@0: void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { michael@0: /* michael@0: * If we have a factoryset, then the first 32bits tell us... michael@0: * 0: failure to write the flattenable michael@0: * >0: (1-based) index into the SkFactorySet or SkNamedFactorySet michael@0: * If we don't have a factoryset, then the first "ptr" is either the michael@0: * factory, or null for failure. michael@0: * michael@0: * The distinction is important, since 0-index is 32bits (always), but a michael@0: * 0-functionptr might be 32 or 64 bits. michael@0: */ michael@0: if (NULL == flattenable) { michael@0: if (this->isValidating()) { michael@0: this->writeString(""); michael@0: } else if (fFactorySet != NULL || fNamedFactorySet != NULL) { michael@0: this->write32(0); michael@0: } else { michael@0: this->writeFunctionPtr(NULL); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: SkFlattenable::Factory factory = flattenable->getFactory(); michael@0: SkASSERT(factory != NULL); michael@0: michael@0: /* michael@0: * We can write 1 of 3 versions of the flattenable: michael@0: * 1. function-ptr : this is the fastest for the reader, but assumes that michael@0: * the writer and reader are in the same process. michael@0: * 2. index into fFactorySet : This is assumes the writer will later michael@0: * resolve the function-ptrs into strings for its reader. SkPicture michael@0: * does exactly this, by writing a table of names (matching the indices) michael@0: * up front in its serialized form. michael@0: * 3. index into fNamedFactorySet. fNamedFactorySet will also store the michael@0: * name. SkGPipe uses this technique so it can write the name to its michael@0: * stream before writing the flattenable. michael@0: */ michael@0: if (this->isValidating()) { michael@0: this->writeString(flattenable->getTypeName()); michael@0: } else if (fFactorySet) { michael@0: this->write32(fFactorySet->add(factory)); michael@0: } else if (fNamedFactorySet) { michael@0: int32_t index = fNamedFactorySet->find(factory); michael@0: this->write32(index); michael@0: if (0 == index) { michael@0: return; michael@0: } michael@0: } else { michael@0: this->writeFunctionPtr((void*)factory); michael@0: } michael@0: michael@0: // make room for the size of the flattened object michael@0: (void)fWriter.reserve(sizeof(uint32_t)); michael@0: // record the current size, so we can subtract after the object writes. michael@0: size_t offset = fWriter.bytesWritten(); michael@0: // now flatten the object michael@0: flattenable->flatten(*this); michael@0: size_t objSize = fWriter.bytesWritten() - offset; michael@0: // record the obj's size michael@0: fWriter.overwriteTAt(offset - sizeof(uint32_t), SkToU32(objSize)); michael@0: }