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 #ifndef SkPictureRecord_DEFINED 9 #define SkPictureRecord_DEFINED 10 11 #include "SkCanvas.h" 12 #include "SkFlattenable.h" 13 #include "SkPathHeap.h" 14 #include "SkPicture.h" 15 #include "SkPictureFlat.h" 16 #include "SkTemplates.h" 17 #include "SkWriter32.h" 18 19 class SkPictureStateTree; 20 class SkBBoxHierarchy; 21 22 // These macros help with packing and unpacking a single byte value and 23 // a 3 byte value into/out of a uint32_t 24 #define MASK_24 0x00FFFFFF 25 #define UNPACK_8_24(combined, small, large) \ 26 small = (combined >> 24) & 0xFF; \ 27 large = combined & MASK_24; 28 #define PACK_8_24(small, large) ((small << 24) | large) 29 30 31 class SkPictureRecord : public SkCanvas { 32 public: 33 SkPictureRecord(uint32_t recordFlags, SkDevice*); 34 virtual ~SkPictureRecord(); 35 36 virtual SkDevice* setDevice(SkDevice* device) SK_OVERRIDE; 37 38 virtual int save(SaveFlags) SK_OVERRIDE; 39 virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE; 40 virtual void restore() SK_OVERRIDE; 41 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE; 42 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE; 43 virtual bool rotate(SkScalar degrees) SK_OVERRIDE; 44 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE; 45 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE; 46 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE; 47 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE; 48 virtual bool clipRRect(const SkRRect&, SkRegion::Op, bool) SK_OVERRIDE; 49 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE; 50 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE; 51 virtual void clear(SkColor) SK_OVERRIDE; 52 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; 53 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[], 54 const SkPaint&) SK_OVERRIDE; 55 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE; 56 virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE; 57 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE; 58 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE; 59 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, 60 const SkPaint*) SK_OVERRIDE; 61 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src, 62 const SkRect& dst, const SkPaint*) SK_OVERRIDE; 63 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&, 64 const SkPaint*) SK_OVERRIDE; 65 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, 66 const SkRect& dst, const SkPaint*) SK_OVERRIDE; 67 virtual void drawSprite(const SkBitmap&, int left, int top, 68 const SkPaint*) SK_OVERRIDE; 69 virtual void drawText(const void* text, size_t byteLength, SkScalar x, 70 SkScalar y, const SkPaint&) SK_OVERRIDE; 71 virtual void drawPosText(const void* text, size_t byteLength, 72 const SkPoint pos[], const SkPaint&) SK_OVERRIDE; 73 virtual void drawPosTextH(const void* text, size_t byteLength, 74 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE; 75 virtual void drawTextOnPath(const void* text, size_t byteLength, 76 const SkPath& path, const SkMatrix* matrix, 77 const SkPaint&) SK_OVERRIDE; 78 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE; 79 virtual void drawVertices(VertexMode, int vertexCount, 80 const SkPoint vertices[], const SkPoint texs[], 81 const SkColor colors[], SkXfermode*, 82 const uint16_t indices[], int indexCount, 83 const SkPaint&) SK_OVERRIDE; 84 virtual void drawData(const void*, size_t) SK_OVERRIDE; 85 virtual void beginCommentGroup(const char* description) SK_OVERRIDE; 86 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE; 87 virtual void endCommentGroup() SK_OVERRIDE; 88 virtual bool isDrawingToLayer() const SK_OVERRIDE; 89 90 void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&, 91 SkScalar minY, SkScalar maxY); 92 93 const SkTDArray<SkPicture* >& getPictureRefs() const { 94 return fPictureRefs; 95 } 96 97 void setFlags(uint32_t recordFlags) { 98 fRecordFlags = recordFlags; 99 } 100 101 const SkWriter32& writeStream() const { 102 return fWriter; 103 } 104 105 void beginRecording(); 106 void endRecording(); 107 108 private: 109 void handleOptimization(int opt); 110 void recordRestoreOffsetPlaceholder(SkRegion::Op); 111 void fillRestoreOffsetPlaceholdersForCurrentStackLevel( 112 uint32_t restoreOffset); 113 114 SkTDArray<int32_t> fRestoreOffsetStack; 115 int fFirstSavedLayerIndex; 116 enum { 117 kNoSavedLayerIndex = -1 118 }; 119 120 /* 121 * Write the 'drawType' operation and chunk size to the skp. 'size' 122 * can potentially be increased if the chunk size needs its own storage 123 * location (i.e., it overflows 24 bits). 124 * Returns the start offset of the chunk. This is the location at which 125 * the opcode & size are stored. 126 * TODO: since we are handing the size into here we could call reserve 127 * and then return a pointer to the memory storage. This could decrease 128 * allocation overhead but could lead to more wasted space (the tail 129 * end of blocks could go unused). Possibly add a second addDraw that 130 * operates in this manner. 131 */ 132 uint32_t addDraw(DrawType drawType, uint32_t* size) { 133 uint32_t offset = fWriter.size(); 134 135 this->predrawNotify(); 136 137 #ifdef SK_DEBUG_TRACE 138 SkDebugf("add %s\n", DrawTypeToString(drawType)); 139 #endif 140 141 SkASSERT(0 != *size); 142 SkASSERT(((uint8_t) drawType) == drawType); 143 144 if (0 != (*size & ~MASK_24) || *size == MASK_24) { 145 fWriter.writeInt(PACK_8_24(drawType, MASK_24)); 146 *size += 1; 147 fWriter.writeInt(*size); 148 } else { 149 fWriter.writeInt(PACK_8_24(drawType, *size)); 150 } 151 152 return offset; 153 } 154 155 void addInt(int value) { 156 fWriter.writeInt(value); 157 } 158 void addScalar(SkScalar scalar) { 159 fWriter.writeScalar(scalar); 160 } 161 162 void addBitmap(const SkBitmap& bitmap); 163 void addMatrix(const SkMatrix& matrix); 164 void addMatrixPtr(const SkMatrix* matrix); 165 const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); } 166 const SkFlatData* addPaintPtr(const SkPaint* paint); 167 void addPath(const SkPath& path); 168 void addPicture(SkPicture& picture); 169 void addPoint(const SkPoint& point); 170 void addPoints(const SkPoint pts[], int count); 171 void addRect(const SkRect& rect); 172 void addRectPtr(const SkRect* rect); 173 void addIRect(const SkIRect& rect); 174 void addIRectPtr(const SkIRect* rect); 175 void addRRect(const SkRRect&); 176 void addRegion(const SkRegion& region); 177 void addText(const void* text, size_t byteLength); 178 179 int find(const SkBitmap& bitmap); 180 181 #ifdef SK_DEBUG_DUMP 182 public: 183 void dumpMatrices(); 184 void dumpPaints(); 185 #endif 186 187 #ifdef SK_DEBUG_SIZE 188 public: 189 size_t size() const; 190 int bitmaps(size_t* size) const; 191 int matrices(size_t* size) const; 192 int paints(size_t* size) const; 193 int paths(size_t* size) const; 194 int regions(size_t* size) const; 195 size_t streamlen() const; 196 197 size_t fPointBytes, fRectBytes, fTextBytes; 198 int fPointWrites, fRectWrites, fTextWrites; 199 #endif 200 201 #ifdef SK_DEBUG_VALIDATE 202 public: 203 void validate(uint32_t initialOffset, uint32_t size) const; 204 private: 205 void validateBitmaps() const; 206 void validateMatrices() const; 207 void validatePaints() const; 208 void validatePaths() const; 209 void validateRegions() const; 210 #else 211 public: 212 void validate(uint32_t initialOffset, uint32_t size) const { 213 SkASSERT(fWriter.size() == initialOffset + size); 214 } 215 #endif 216 217 protected: 218 219 // These are set to NULL in our constructor, but may be changed by 220 // subclasses, in which case they will be SkSafeUnref'd in our destructor. 221 SkBBoxHierarchy* fBoundingHierarchy; 222 SkPictureStateTree* fStateTree; 223 224 // Allocated in the constructor and managed by this class. 225 SkBitmapHeap* fBitmapHeap; 226 227 private: 228 SkChunkFlatController fFlattenableHeap; 229 230 SkMatrixDictionary fMatrices; 231 SkPaintDictionary fPaints; 232 SkRegionDictionary fRegions; 233 234 SkPathHeap* fPathHeap; // reference counted 235 SkWriter32 fWriter; 236 237 // we ref each item in these arrays 238 SkTDArray<SkPicture*> fPictureRefs; 239 240 uint32_t fRecordFlags; 241 int fInitialSaveCount; 242 243 friend class SkPicturePlayback; 244 friend class SkPictureTester; // for unit testing 245 246 typedef SkCanvas INHERITED; 247 }; 248 249 #endif 250