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 SkWriteBuffer_DEFINED 10 #define SkWriteBuffer_DEFINED 11 12 #include "SkData.h" 13 #include "SkPath.h" 14 #include "SkPicture.h" 15 #include "SkPixelSerializer.h" 16 #include "SkRefCnt.h" 17 #include "SkWriter32.h" 18 19 class SkBitmap; 20 class SkBitmapHeap; 21 class SkFactorySet; 22 class SkFlattenable; 23 class SkNamedFactorySet; 24 class SkRefCntSet; 25 26 class SkWriteBuffer { 27 public: 28 enum Flags { 29 kCrossProcess_Flag = 1 << 0, 30 kValidation_Flag = 1 << 1, 31 }; 32 33 SkWriteBuffer(uint32_t flags = 0); 34 SkWriteBuffer(void* initialStorage, size_t storageSize, uint32_t flags = 0); 35 ~SkWriteBuffer(); 36 isCrossProcess()37 bool isCrossProcess() const { 38 return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag); 39 } 40 getWriter32()41 SkWriter32* getWriter32() { return &fWriter; } 42 void reset(void* storage = NULL, size_t storageSize = 0) { 43 fWriter.reset(storage, storageSize); 44 } 45 reserve(size_t size)46 uint32_t* reserve(size_t size) { return fWriter.reserve(size); } 47 bytesWritten()48 size_t bytesWritten() const { return fWriter.bytesWritten(); } 49 50 void writeByteArray(const void* data, size_t size); writeDataAsByteArray(SkData * data)51 void writeDataAsByteArray(SkData* data) { this->writeByteArray(data->data(), data->size()); } 52 void writeBool(bool value); 53 void writeFixed(SkFixed value); 54 void writeScalar(SkScalar value); 55 void writeScalarArray(const SkScalar* value, uint32_t count); 56 void writeInt(int32_t value); 57 void writeIntArray(const int32_t* value, uint32_t count); 58 void writeUInt(uint32_t value); 59 void write32(int32_t value); 60 void writeString(const char* value); 61 void writeEncodedString(const void* value, size_t byteLength, SkPaint::TextEncoding encoding); writeFunctionPtr(void * ptr)62 void writeFunctionPtr(void* ptr) { fWriter.writePtr(ptr); } 63 64 void writeFlattenable(const SkFlattenable* flattenable); 65 void writeColor(const SkColor& color); 66 void writeColorArray(const SkColor* color, uint32_t count); 67 void writePoint(const SkPoint& point); 68 void writePointArray(const SkPoint* point, uint32_t count); 69 void writeMatrix(const SkMatrix& matrix); 70 void writeIRect(const SkIRect& rect); 71 void writeRect(const SkRect& rect); 72 void writeRegion(const SkRegion& region); 73 void writePath(const SkPath& path); 74 size_t writeStream(SkStream* stream, size_t length); 75 void writeBitmap(const SkBitmap& bitmap); 76 void writeTypeface(SkTypeface* typeface); writePaint(const SkPaint & paint)77 void writePaint(const SkPaint& paint) { paint.flatten(*this); } 78 79 bool writeToStream(SkWStream*); writeToMemory(void * dst)80 void writeToMemory(void* dst) { fWriter.flatten(dst); } 81 82 SkFactorySet* setFactoryRecorder(SkFactorySet*); 83 SkNamedFactorySet* setNamedFactoryRecorder(SkNamedFactorySet*); 84 getTypefaceRecorder()85 SkRefCntSet* getTypefaceRecorder() const { return fTFSet; } 86 SkRefCntSet* setTypefaceRecorder(SkRefCntSet*); 87 88 /** 89 * Set an SkBitmapHeap to store bitmaps rather than flattening. 90 * 91 * Incompatible with an SkPixelSerializer. If an SkPixelSerializer is set, 92 * setting an SkBitmapHeap will set the SkPixelSerializer to NULL in release 93 * and crash in debug. 94 */ 95 void setBitmapHeap(SkBitmapHeap*); 96 97 /** 98 * Set an SkPixelSerializer to store an encoded representation of pixels, 99 * e.g. SkBitmaps. 100 * 101 * Calls ref() on the serializer. 102 * 103 * TODO: Encode SkImage pixels as well. 104 * 105 * Incompatible with the SkBitmapHeap. If an encoder is set fBitmapHeap will 106 * be set to NULL in release and crash in debug. 107 */ 108 void setPixelSerializer(SkPixelSerializer*); 109 110 private: isValidating()111 bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); } 112 113 const uint32_t fFlags; 114 SkFactorySet* fFactorySet; 115 SkNamedFactorySet* fNamedFactorySet; 116 SkWriter32 fWriter; 117 118 SkBitmapHeap* fBitmapHeap; 119 SkRefCntSet* fTFSet; 120 121 SkAutoTUnref<SkPixelSerializer> fPixelSerializer; 122 }; 123 124 #endif // SkWriteBuffer_DEFINED 125