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