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