1 #ifndef SkPictureFlat_DEFINED 2 #define SkPictureFlat_DEFINED 3 4 #include "SkChunkAlloc.h" 5 #include "SkBitmap.h" 6 #include "SkPicture.h" 7 #include "SkMatrix.h" 8 #include "SkPaint.h" 9 #include "SkPath.h" 10 #include "SkRegion.h" 11 12 enum DrawType { 13 UNUSED, 14 CLIP_PATH, 15 CLIP_REGION, 16 CLIP_RECT, 17 CONCAT, 18 DRAW_BITMAP, 19 DRAW_BITMAP_MATRIX, 20 DRAW_BITMAP_RECT, 21 DRAW_DATA, 22 DRAW_PAINT, 23 DRAW_PATH, 24 DRAW_PICTURE, 25 DRAW_POINTS, 26 DRAW_POS_TEXT, 27 DRAW_POS_TEXT_H, 28 DRAW_POS_TEXT_H_TOP_BOTTOM, // fast variant of DRAW_POS_TEXT_H 29 DRAW_RECT, 30 DRAW_SHAPE, 31 DRAW_SPRITE, 32 DRAW_TEXT, 33 DRAW_TEXT_ON_PATH, 34 DRAW_TEXT_TOP_BOTTOM, // fast variant of DRAW_TEXT 35 DRAW_VERTICES, 36 RESTORE, 37 ROTATE, 38 SAVE, 39 SAVE_LAYER, 40 SCALE, 41 SET_MATRIX, 42 SKEW, 43 TRANSLATE 44 }; 45 46 enum DrawVertexFlags { 47 DRAW_VERTICES_HAS_TEXS = 0x01, 48 DRAW_VERTICES_HAS_COLORS = 0x02, 49 DRAW_VERTICES_HAS_INDICES = 0x04 50 }; 51 52 class SkRefCntPlayback { 53 public: 54 SkRefCntPlayback(); 55 virtual ~SkRefCntPlayback(); 56 count()57 int count() const { return fCount; } 58 59 void reset(const SkRefCntRecorder*); 60 61 void setCount(int count); 62 SkRefCnt* set(int index, SkRefCnt*); 63 setupBuffer(SkFlattenableReadBuffer & buffer)64 virtual void setupBuffer(SkFlattenableReadBuffer& buffer) const { 65 buffer.setRefCntArray(fArray, fCount); 66 } 67 68 protected: 69 int fCount; 70 SkRefCnt** fArray; 71 }; 72 73 class SkTypefacePlayback : public SkRefCntPlayback { 74 public: setupBuffer(SkFlattenableReadBuffer & buffer)75 virtual void setupBuffer(SkFlattenableReadBuffer& buffer) const { 76 buffer.setTypefaceArray((SkTypeface**)fArray, fCount); 77 } 78 }; 79 80 class SkFactoryPlayback { 81 public: SkFactoryPlayback(int count)82 SkFactoryPlayback(int count) : fCount(count) { 83 fArray = SkNEW_ARRAY(SkFlattenable::Factory, count); 84 } 85 ~SkFactoryPlayback()86 ~SkFactoryPlayback() { 87 SkDELETE_ARRAY(fArray); 88 } 89 base()90 SkFlattenable::Factory* base() const { return fArray; } 91 setupBuffer(SkFlattenableReadBuffer & buffer)92 void setupBuffer(SkFlattenableReadBuffer& buffer) const { 93 buffer.setFactoryPlayback(fArray, fCount); 94 } 95 96 private: 97 int fCount; 98 SkFlattenable::Factory* fArray; 99 }; 100 101 class SkFlatData { 102 public: Compare(const SkFlatData * a,const SkFlatData * b)103 static int Compare(const SkFlatData* a, const SkFlatData* b) { 104 return memcmp(&a->fAllocSize, &b->fAllocSize, a->fAllocSize); 105 } 106 index()107 int index() const { return fIndex; } 108 109 #ifdef SK_DEBUG_SIZE size()110 size_t size() const { return sizeof(fIndex) + fAllocSize; } 111 #endif 112 113 protected: 114 static SkFlatData* Alloc(SkChunkAlloc* heap, int32_t size, int index); 115 116 int fIndex; 117 int32_t fAllocSize; 118 }; 119 120 class SkFlatBitmap : public SkFlatData { 121 public: 122 static SkFlatBitmap* Flatten(SkChunkAlloc*, const SkBitmap&, int index, 123 SkRefCntRecorder*); 124 unflatten(SkBitmap * bitmap,SkRefCntPlayback * rcp)125 void unflatten(SkBitmap* bitmap, SkRefCntPlayback* rcp) const { 126 SkFlattenableReadBuffer buffer(fBitmapData); 127 if (rcp) { 128 rcp->setupBuffer(buffer); 129 } 130 bitmap->unflatten(buffer); 131 } 132 133 #ifdef SK_DEBUG_VALIDATE validate()134 void validate() const { 135 // to be written 136 } 137 #endif 138 139 private: 140 char fBitmapData[1]; 141 typedef SkFlatData INHERITED; 142 }; 143 144 class SkFlatMatrix : public SkFlatData { 145 public: 146 static SkFlatMatrix* Flatten(SkChunkAlloc* heap, const SkMatrix& matrix, int index); 147 unflatten(SkMatrix * result)148 void unflatten(SkMatrix* result) const { 149 memcpy(result, fMatrixData, sizeof(SkMatrix)); 150 } 151 152 #ifdef SK_DEBUG_DUMP 153 void dump() const; 154 #endif 155 156 #ifdef SK_DEBUG_VALIDATE validate()157 void validate() const { 158 // to be written 159 } 160 #endif 161 162 private: 163 char fMatrixData[1]; 164 typedef SkFlatData INHERITED; 165 }; 166 167 class SkFlatPaint : public SkFlatData { 168 public: 169 static SkFlatPaint* Flatten(SkChunkAlloc* heap, const SkPaint& paint, 170 int index, SkRefCntRecorder*, 171 SkRefCntRecorder* faceRecorder); 172 unflatten(SkPaint * result,SkRefCntPlayback * rcp,SkTypefacePlayback * facePlayback)173 void unflatten(SkPaint* result, SkRefCntPlayback* rcp, 174 SkTypefacePlayback* facePlayback) const { 175 Read(fPaintData, result, rcp, facePlayback); 176 } 177 178 static void Read(const void* storage, SkPaint* paint, SkRefCntPlayback*, 179 SkTypefacePlayback* facePlayback); 180 181 #ifdef SK_DEBUG_DUMP 182 void dump() const; 183 #endif 184 185 private: 186 char fPaintData[1]; 187 typedef SkFlatData INHERITED; 188 }; 189 190 class SkFlatRegion : public SkFlatData { 191 public: 192 static SkFlatRegion* Flatten(SkChunkAlloc* heap, const SkRegion& region, int index); 193 unflatten(SkRegion * result)194 void unflatten(SkRegion* result) const { 195 result->unflatten(fRegionData); 196 } 197 198 #ifdef SK_DEBUG_VALIDATE validate()199 void validate() const { 200 // to be written 201 } 202 #endif 203 204 private: 205 char fRegionData[1]; 206 typedef SkFlatData INHERITED; 207 }; 208 209 #endif 210