michael@0: michael@0: /* michael@0: * Copyright 2011 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: #ifndef SkWriteBuffer_DEFINED michael@0: #define SkWriteBuffer_DEFINED michael@0: michael@0: #include "SkBitmapHeap.h" michael@0: #include "SkData.h" michael@0: #include "SkPath.h" michael@0: #include "SkPicture.h" michael@0: #include "SkRefCnt.h" michael@0: #include "SkWriter32.h" michael@0: michael@0: class SkBitmap; michael@0: class SkFactorySet; michael@0: class SkFlattenable; michael@0: class SkNamedFactorySet; michael@0: class SkRefCntSet; michael@0: michael@0: class SkWriteBuffer { michael@0: public: michael@0: enum Flags { michael@0: kCrossProcess_Flag = 1 << 0, michael@0: kValidation_Flag = 1 << 1, michael@0: }; michael@0: michael@0: SkWriteBuffer(uint32_t flags = 0); michael@0: SkWriteBuffer(void* initialStorage, size_t storageSize, uint32_t flags = 0); michael@0: ~SkWriteBuffer(); michael@0: michael@0: bool isCrossProcess() const { michael@0: return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag); michael@0: } michael@0: michael@0: SkWriter32* getWriter32() { return &fWriter; } michael@0: void reset(void* storage = NULL, size_t storageSize = 0) { michael@0: fWriter.reset(storage, storageSize); michael@0: } michael@0: michael@0: uint32_t* reserve(size_t size) { return fWriter.reserve(size); } michael@0: michael@0: size_t bytesWritten() const { return fWriter.bytesWritten(); } michael@0: michael@0: void writeByteArray(const void* data, size_t size); michael@0: void writeDataAsByteArray(SkData* data) { this->writeByteArray(data->data(), data->size()); } michael@0: void writeBool(bool value); michael@0: void writeFixed(SkFixed value); michael@0: void writeScalar(SkScalar value); michael@0: void writeScalarArray(const SkScalar* value, uint32_t count); michael@0: void writeInt(int32_t value); michael@0: void writeIntArray(const int32_t* value, uint32_t count); michael@0: void writeUInt(uint32_t value); michael@0: void write32(int32_t value); michael@0: void writeString(const char* value); michael@0: void writeEncodedString(const void* value, size_t byteLength, SkPaint::TextEncoding encoding); michael@0: void writeFunctionPtr(void* ptr) { this->writeByteArray(&ptr, sizeof(ptr)); } michael@0: michael@0: void writeFlattenable(const SkFlattenable* flattenable); michael@0: void writeColor(const SkColor& color); michael@0: void writeColorArray(const SkColor* color, uint32_t count); michael@0: void writePoint(const SkPoint& point); michael@0: void writePointArray(const SkPoint* point, uint32_t count); michael@0: void writeMatrix(const SkMatrix& matrix); michael@0: void writeIRect(const SkIRect& rect); michael@0: void writeRect(const SkRect& rect); michael@0: void writeRegion(const SkRegion& region); michael@0: void writePath(const SkPath& path); michael@0: size_t writeStream(SkStream* stream, size_t length); michael@0: void writeBitmap(const SkBitmap& bitmap); michael@0: void writeTypeface(SkTypeface* typeface); michael@0: void writePaint(const SkPaint& paint) { paint.flatten(*this); } michael@0: michael@0: bool writeToStream(SkWStream*); michael@0: void writeToMemory(void* dst) { fWriter.flatten(dst); } michael@0: michael@0: SkFactorySet* setFactoryRecorder(SkFactorySet*); michael@0: SkNamedFactorySet* setNamedFactoryRecorder(SkNamedFactorySet*); michael@0: michael@0: SkRefCntSet* getTypefaceRecorder() const { return fTFSet; } michael@0: SkRefCntSet* setTypefaceRecorder(SkRefCntSet*); michael@0: michael@0: /** michael@0: * Set an SkBitmapHeap to store bitmaps rather than flattening. michael@0: * michael@0: * Incompatible with an EncodeBitmap function. If an EncodeBitmap function is set, setting an michael@0: * SkBitmapHeap will set the function to NULL in release mode and crash in debug. michael@0: */ michael@0: void setBitmapHeap(SkBitmapHeap*); michael@0: michael@0: /** michael@0: * Provide a function to encode an SkBitmap to an SkData. writeBitmap will attempt to use michael@0: * bitmapEncoder to store the SkBitmap. If the reader does not provide a function to decode, it michael@0: * will not be able to restore SkBitmaps, but will still be able to read the rest of the stream. michael@0: * bitmapEncoder will never be called with a NULL pixelRefOffset. michael@0: * michael@0: * Incompatible with the SkBitmapHeap. If an encoder is set fBitmapHeap will be set to NULL in michael@0: * release and crash in debug. michael@0: */ michael@0: void setBitmapEncoder(SkPicture::EncodeBitmap bitmapEncoder); michael@0: michael@0: private: michael@0: bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } michael@0: michael@0: const uint32_t fFlags; michael@0: SkFactorySet* fFactorySet; michael@0: SkNamedFactorySet* fNamedFactorySet; michael@0: SkWriter32 fWriter; michael@0: michael@0: SkBitmapHeap* fBitmapHeap; michael@0: SkRefCntSet* fTFSet; michael@0: michael@0: SkPicture::EncodeBitmap fBitmapEncoder; michael@0: }; michael@0: michael@0: #endif // SkWriteBuffer_DEFINED