1 2 /* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 #ifndef SkOrderedWriteBuffer_DEFINED 10 #define SkOrderedWriteBuffer_DEFINED 11 12 #include "SkFlattenableBuffers.h" 13 14 #include "SkRefCnt.h" 15 #include "SkBitmapHeap.h" 16 #include "SkPath.h" 17 #include "SkSerializationHelpers.h" 18 #include "SkWriter32.h" 19 20 class SkBitmap; 21 class SkFlattenable; 22 class SkFactorySet; 23 class SkNamedFactorySet; 24 class SkRefCntSet; 25 26 class SkOrderedWriteBuffer : public SkFlattenableWriteBuffer { 27 public: 28 SkOrderedWriteBuffer(size_t minSize); 29 SkOrderedWriteBuffer(size_t minSize, void* initialStorage, 30 size_t storageSize); 31 virtual ~SkOrderedWriteBuffer(); 32 isOrderedBinaryBuffer()33 virtual bool isOrderedBinaryBuffer() SK_OVERRIDE { return true; } getOrderedBinaryBuffer()34 virtual SkOrderedWriteBuffer* getOrderedBinaryBuffer() SK_OVERRIDE { return this; } 35 getWriter32()36 SkWriter32* getWriter32() { return &fWriter; } 37 writeToMemory(void * dst)38 void writeToMemory(void* dst) { fWriter.flatten(dst); } reserve(size_t size)39 uint32_t* reserve(size_t size) { return fWriter.reserve(size); } size()40 uint32_t size() { return fWriter.size(); } 41 42 virtual void writeByteArray(const void* data, size_t size) SK_OVERRIDE; 43 virtual void writeBool(bool value) SK_OVERRIDE; 44 virtual void writeFixed(SkFixed value) SK_OVERRIDE; 45 virtual void writeScalar(SkScalar value) SK_OVERRIDE; 46 virtual void writeScalarArray(const SkScalar* value, uint32_t count) SK_OVERRIDE; 47 virtual void writeInt(int32_t value) SK_OVERRIDE; 48 virtual void writeIntArray(const int32_t* value, uint32_t count) SK_OVERRIDE; 49 virtual void writeUInt(uint32_t value) SK_OVERRIDE; 50 virtual void write32(int32_t value) SK_OVERRIDE; 51 virtual void writeString(const char* value) SK_OVERRIDE; 52 virtual void writeEncodedString(const void* value, size_t byteLength, 53 SkPaint::TextEncoding encoding) SK_OVERRIDE; 54 55 virtual void writeFlattenable(SkFlattenable* flattenable) SK_OVERRIDE; 56 virtual void writeColor(const SkColor& color) SK_OVERRIDE; 57 virtual void writeColorArray(const SkColor* color, uint32_t count) SK_OVERRIDE; 58 virtual void writePoint(const SkPoint& point) SK_OVERRIDE; 59 virtual void writePointArray(const SkPoint* point, uint32_t count) SK_OVERRIDE; 60 virtual void writeMatrix(const SkMatrix& matrix) SK_OVERRIDE; 61 virtual void writeIRect(const SkIRect& rect)SK_OVERRIDE; 62 virtual void writeRect(const SkRect& rect) SK_OVERRIDE; 63 virtual void writeRegion(const SkRegion& region) SK_OVERRIDE; 64 virtual void writePath(const SkPath& path) SK_OVERRIDE; 65 virtual size_t writeStream(SkStream* stream, size_t length) SK_OVERRIDE; 66 67 virtual void writeBitmap(const SkBitmap& bitmap) SK_OVERRIDE; 68 virtual void writeTypeface(SkTypeface* typeface) SK_OVERRIDE; 69 70 virtual bool writeToStream(SkWStream*) SK_OVERRIDE; 71 72 SkFactorySet* setFactoryRecorder(SkFactorySet*); 73 SkNamedFactorySet* setNamedFactoryRecorder(SkNamedFactorySet*); 74 getTypefaceRecorder()75 SkRefCntSet* getTypefaceRecorder() const { return fTFSet; } 76 SkRefCntSet* setTypefaceRecorder(SkRefCntSet*); 77 78 /** 79 * Set an SkBitmapHeap to store bitmaps rather than flattening. 80 * 81 * Incompatible with an EncodeBitmap function. If an EncodeBitmap function is set, setting an 82 * SkBitmapHeap will set the function to NULL in release mode and crash in debug. 83 */ 84 void setBitmapHeap(SkBitmapHeap*); 85 86 /** 87 * Provide a function to encode an SkBitmap to an SkStream. writeBitmap will attempt to use 88 * bitmapEncoder to store the SkBitmap. If the reader does not provide a function to decode, it 89 * will not be able to restore SkBitmaps, but will still be able to read the rest of the stream. 90 * 91 * Incompatible with the SkBitmapHeap. If an encoder is set fBitmapHeap will be set to NULL in 92 * release and crash in debug. 93 */ 94 void setBitmapEncoder(SkSerializationHelpers::EncodeBitmap); 95 96 private: 97 SkFactorySet* fFactorySet; 98 SkNamedFactorySet* fNamedFactorySet; 99 SkWriter32 fWriter; 100 101 SkBitmapHeap* fBitmapHeap; 102 SkRefCntSet* fTFSet; 103 104 SkSerializationHelpers::EncodeBitmap fBitmapEncoder; 105 106 typedef SkFlattenableWriteBuffer INHERITED; 107 }; 108 109 #endif // SkOrderedWriteBuffer_DEFINED 110