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 "SkPicture.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, size_t storageSize); 30 virtual ~SkOrderedWriteBuffer(); 31 32 virtual bool isOrderedBinaryBuffer() SK_OVERRIDE { return true; } 33 virtual SkOrderedWriteBuffer* getOrderedBinaryBuffer() SK_OVERRIDE { return this; } 34 35 SkWriter32* getWriter32() { return &fWriter; } 36 void reset(void* storage, size_t storageSize) { fWriter.reset(storage, storageSize); } 37 38 // Returns true if we've written only into the storage passed into constructor or reset. 39 // (You may be able to use this to avoid a call to writeToMemory.) 40 bool wroteOnlyToStorage() const { return fWriter.wroteOnlyToStorage(); } 41 42 void writeToMemory(void* dst) { fWriter.flatten(dst); } 43 uint32_t* reserve(size_t size) { return fWriter.reserve(size); } 44 45 size_t bytesWritten() const { return fWriter.bytesWritten(); } 46 // Deprecated. Please call bytesWritten instead. TODO(mtklein): clean up 47 size_t size() const { return this->bytesWritten(); } 48 49 virtual void writeByteArray(const void* data, size_t size) SK_OVERRIDE; 50 virtual void writeBool(bool value) SK_OVERRIDE; 51 virtual void writeFixed(SkFixed value) SK_OVERRIDE; 52 virtual void writeScalar(SkScalar value) SK_OVERRIDE; 53 virtual void writeScalarArray(const SkScalar* value, uint32_t count) SK_OVERRIDE; 54 virtual void writeInt(int32_t value) SK_OVERRIDE; 55 virtual void writeIntArray(const int32_t* value, uint32_t count) SK_OVERRIDE; 56 virtual void writeUInt(uint32_t value) SK_OVERRIDE; 57 virtual void write32(int32_t value) SK_OVERRIDE; 58 virtual void writeString(const char* value) SK_OVERRIDE; 59 virtual void writeEncodedString(const void* value, size_t byteLength, 60 SkPaint::TextEncoding encoding) SK_OVERRIDE; 61 62 virtual void writeFlattenable(const SkFlattenable* flattenable) SK_OVERRIDE; 63 virtual void writeColor(const SkColor& color) SK_OVERRIDE; 64 virtual void writeColorArray(const SkColor* color, uint32_t count) SK_OVERRIDE; 65 virtual void writePoint(const SkPoint& point) SK_OVERRIDE; 66 virtual void writePointArray(const SkPoint* point, uint32_t count) SK_OVERRIDE; 67 virtual void writeMatrix(const SkMatrix& matrix) SK_OVERRIDE; 68 virtual void writeIRect(const SkIRect& rect)SK_OVERRIDE; 69 virtual void writeRect(const SkRect& rect) SK_OVERRIDE; 70 virtual void writeRegion(const SkRegion& region) SK_OVERRIDE; 71 virtual void writePath(const SkPath& path) SK_OVERRIDE; 72 virtual size_t writeStream(SkStream* stream, size_t length) SK_OVERRIDE; 73 74 virtual void writeBitmap(const SkBitmap& bitmap) SK_OVERRIDE; 75 virtual void writeTypeface(SkTypeface* typeface) SK_OVERRIDE; 76 77 virtual bool writeToStream(SkWStream*) SK_OVERRIDE; 78 79 SkFactorySet* setFactoryRecorder(SkFactorySet*); 80 SkNamedFactorySet* setNamedFactoryRecorder(SkNamedFactorySet*); 81 82 SkRefCntSet* getTypefaceRecorder() const { return fTFSet; } 83 SkRefCntSet* setTypefaceRecorder(SkRefCntSet*); 84 85 /** 86 * Set an SkBitmapHeap to store bitmaps rather than flattening. 87 * 88 * Incompatible with an EncodeBitmap function. If an EncodeBitmap function is set, setting an 89 * SkBitmapHeap will set the function to NULL in release mode and crash in debug. 90 */ 91 void setBitmapHeap(SkBitmapHeap*); 92 93 /** 94 * Provide a function to encode an SkBitmap to an SkData. writeBitmap will attempt to use 95 * bitmapEncoder to store the SkBitmap. If the reader does not provide a function to decode, it 96 * will not be able to restore SkBitmaps, but will still be able to read the rest of the stream. 97 * bitmapEncoder will never be called with a NULL pixelRefOffset. 98 * 99 * Incompatible with the SkBitmapHeap. If an encoder is set fBitmapHeap will be set to NULL in 100 * release and crash in debug. 101 */ 102 void setBitmapEncoder(SkPicture::EncodeBitmap bitmapEncoder); 103 104 private: 105 SkFactorySet* fFactorySet; 106 SkNamedFactorySet* fNamedFactorySet; 107 SkWriter32 fWriter; 108 109 SkBitmapHeap* fBitmapHeap; 110 SkRefCntSet* fTFSet; 111 112 SkPicture::EncodeBitmap fBitmapEncoder; 113 114 typedef SkFlattenableWriteBuffer INHERITED; 115 }; 116 117 #endif // SkOrderedWriteBuffer_DEFINED 118