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 SkOrderedReadBuffer_DEFINED 10 #define SkOrderedReadBuffer_DEFINED 11 12 #include "SkRefCnt.h" 13 #include "SkBitmap.h" 14 #include "SkBitmapHeap.h" 15 #include "SkFlattenableBuffers.h" 16 #include "SkPath.h" 17 #include "SkReader32.h" 18 #include "SkSerializationHelpers.h" 19 20 class SkOrderedReadBuffer : public SkFlattenableReadBuffer { 21 public: 22 SkOrderedReadBuffer(); 23 SkOrderedReadBuffer(const void* data, size_t size); 24 SkOrderedReadBuffer(SkStream* stream); 25 virtual ~SkOrderedReadBuffer(); 26 27 virtual SkOrderedReadBuffer* getOrderedBinaryBuffer() SK_OVERRIDE { return this; } 28 29 SkReader32* getReader32() { return &fReader; } 30 31 uint32_t size() { return fReader.size(); } 32 uint32_t offset() { return fReader.offset(); } 33 bool eof() { return fReader.eof(); } 34 const void* skip(size_t size) { return fReader.skip(size); } 35 36 // primitives 37 virtual bool readBool() SK_OVERRIDE; 38 virtual SkColor readColor() SK_OVERRIDE; 39 virtual SkFixed readFixed() SK_OVERRIDE; 40 virtual int32_t readInt() SK_OVERRIDE; 41 virtual SkScalar readScalar() SK_OVERRIDE; 42 virtual uint32_t readUInt() SK_OVERRIDE; 43 virtual int32_t read32() SK_OVERRIDE; 44 45 // strings -- the caller is responsible for freeing the string contents 46 virtual char* readString() SK_OVERRIDE; 47 virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding) SK_OVERRIDE; 48 49 // common data structures 50 virtual SkFlattenable* readFlattenable() SK_OVERRIDE; 51 virtual void readPoint(SkPoint* point) SK_OVERRIDE; 52 virtual void readMatrix(SkMatrix* matrix) SK_OVERRIDE; 53 virtual void readIRect(SkIRect* rect) SK_OVERRIDE; 54 virtual void readRect(SkRect* rect) SK_OVERRIDE; 55 virtual void readRegion(SkRegion* region) SK_OVERRIDE; 56 virtual void readPath(SkPath* path) SK_OVERRIDE; 57 58 // binary data and arrays 59 virtual uint32_t readByteArray(void* value) SK_OVERRIDE; 60 virtual uint32_t readColorArray(SkColor* colors) SK_OVERRIDE; 61 virtual uint32_t readIntArray(int32_t* values) SK_OVERRIDE; 62 virtual uint32_t readPointArray(SkPoint* points) SK_OVERRIDE; 63 virtual uint32_t readScalarArray(SkScalar* values) SK_OVERRIDE; 64 65 // helpers to get info about arrays and binary data 66 virtual uint32_t getArrayCount() SK_OVERRIDE; 67 68 virtual void readBitmap(SkBitmap* bitmap) SK_OVERRIDE; 69 virtual SkTypeface* readTypeface() SK_OVERRIDE; 70 71 void setBitmapStorage(SkBitmapHeapReader* bitmapStorage) { 72 SkRefCnt_SafeAssign(fBitmapStorage, bitmapStorage); 73 } 74 75 void setTypefaceArray(SkTypeface* array[], int count) { 76 fTFArray = array; 77 fTFCount = count; 78 } 79 80 /** 81 * Call this with a pre-loaded array of Factories, in the same order as 82 * were created/written by the writer. SkPicture uses this. 83 */ 84 void setFactoryPlayback(SkFlattenable::Factory array[], int count) { 85 fFactoryTDArray = NULL; 86 fFactoryArray = array; 87 fFactoryCount = count; 88 } 89 90 /** 91 * Call this with an initially empty array, so the reader can cache each 92 * factory it sees by name. Used by the pipe code in conjunction with 93 * SkOrderedWriteBuffer::setNamedFactoryRecorder. 94 */ 95 void setFactoryArray(SkTDArray<SkFlattenable::Factory>* array) { 96 fFactoryTDArray = array; 97 fFactoryArray = NULL; 98 fFactoryCount = 0; 99 } 100 101 /** 102 * Provide a function to decode an SkBitmap from an SkStream. Only used if the writer encoded 103 * the SkBitmap. If the proper decoder cannot be used, a red bitmap with the appropriate size 104 * will be used. 105 */ 106 void setBitmapDecoder(SkSerializationHelpers::DecodeBitmap bitmapDecoder) { 107 fBitmapDecoder = bitmapDecoder; 108 } 109 110 private: 111 SkReader32 fReader; 112 void* fMemoryPtr; 113 114 SkBitmapHeapReader* fBitmapStorage; 115 SkTypeface** fTFArray; 116 int fTFCount; 117 118 SkTDArray<SkFlattenable::Factory>* fFactoryTDArray; 119 SkFlattenable::Factory* fFactoryArray; 120 int fFactoryCount; 121 122 SkSerializationHelpers::DecodeBitmap fBitmapDecoder; 123 124 typedef SkFlattenableReadBuffer INHERITED; 125 }; 126 127 #endif // SkOrderedReadBuffer_DEFINED 128