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 class SkPictureRecord : public SkCanvas { 23 public: 24 SkPictureRecord(uint32_t recordFlags, SkDevice*); 25 virtual ~SkPictureRecord(); 26 27 virtual SkDevice* setDevice(SkDevice* device) SK_OVERRIDE; 28 29 virtual int save(SaveFlags) SK_OVERRIDE; 30 virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE; 31 virtual void restore() SK_OVERRIDE; 32 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE; 33 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE; 34 virtual bool rotate(SkScalar degrees) SK_OVERRIDE; 35 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE; 36 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE; 37 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE; 38 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE; 39 virtual bool clipRRect(const SkRRect&, SkRegion::Op, bool) SK_OVERRIDE; 40 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE; 41 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE; 42 virtual void clear(SkColor) SK_OVERRIDE; 43 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; 44 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[], 45 const SkPaint&) SK_OVERRIDE; 46 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE; 47 virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE; 48 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE; 49 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE; 50 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, 51 const SkPaint*) SK_OVERRIDE; 52 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src, 53 const SkRect& dst, const SkPaint*) SK_OVERRIDE; 54 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&, 55 const SkPaint*) SK_OVERRIDE; 56 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, 57 const SkRect& dst, const SkPaint*) SK_OVERRIDE; 58 virtual void drawSprite(const SkBitmap&, int left, int top, 59 const SkPaint*) SK_OVERRIDE; 60 virtual void drawText(const void* text, size_t byteLength, SkScalar x, 61 SkScalar y, const SkPaint&) SK_OVERRIDE; 62 virtual void drawPosText(const void* text, size_t byteLength, 63 const SkPoint pos[], const SkPaint&) SK_OVERRIDE; 64 virtual void drawPosTextH(const void* text, size_t byteLength, 65 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE; 66 virtual void drawTextOnPath(const void* text, size_t byteLength, 67 const SkPath& path, const SkMatrix* matrix, 68 const SkPaint&) SK_OVERRIDE; 69 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE; 70 virtual void drawVertices(VertexMode, int vertexCount, 71 const SkPoint vertices[], const SkPoint texs[], 72 const SkColor colors[], SkXfermode*, 73 const uint16_t indices[], int indexCount, 74 const SkPaint&) SK_OVERRIDE; 75 virtual void drawData(const void*, size_t) SK_OVERRIDE; 76 virtual bool isDrawingToLayer() const SK_OVERRIDE; 77 78 void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&, 79 SkScalar minY, SkScalar maxY); 80 getPictureRefs()81 const SkTDArray<SkPicture* >& getPictureRefs() const { 82 return fPictureRefs; 83 } 84 setFlags(uint32_t recordFlags)85 void setFlags(uint32_t recordFlags) { 86 fRecordFlags = recordFlags; 87 } 88 writeStream()89 const SkWriter32& writeStream() const { 90 return fWriter; 91 } 92 93 void beginRecording(); 94 void endRecording(); 95 96 private: 97 void recordRestoreOffsetPlaceholder(SkRegion::Op); 98 void fillRestoreOffsetPlaceholdersForCurrentStackLevel( 99 uint32_t restoreOffset); 100 101 SkTDArray<int32_t> fRestoreOffsetStack; 102 int fFirstSavedLayerIndex; 103 enum { 104 kNoSavedLayerIndex = -1 105 }; 106 addDraw(DrawType drawType)107 void addDraw(DrawType drawType) { 108 this->predrawNotify(); 109 110 #ifdef SK_DEBUG_TRACE 111 SkDebugf("add %s\n", DrawTypeToString(drawType)); 112 #endif 113 fWriter.writeInt(drawType); 114 } addInt(int value)115 void addInt(int value) { 116 fWriter.writeInt(value); 117 } addScalar(SkScalar scalar)118 void addScalar(SkScalar scalar) { 119 fWriter.writeScalar(scalar); 120 } 121 122 void addBitmap(const SkBitmap& bitmap); 123 void addMatrix(const SkMatrix& matrix); 124 void addMatrixPtr(const SkMatrix* matrix); addPaint(const SkPaint & paint)125 const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); } 126 const SkFlatData* addPaintPtr(const SkPaint* paint); 127 void addPath(const SkPath& path); 128 void addPicture(SkPicture& picture); 129 void addPoint(const SkPoint& point); 130 void addPoints(const SkPoint pts[], int count); 131 void addRect(const SkRect& rect); 132 void addRectPtr(const SkRect* rect); 133 void addIRect(const SkIRect& rect); 134 void addIRectPtr(const SkIRect* rect); 135 void addRRect(const SkRRect&); 136 void addRegion(const SkRegion& region); 137 void addText(const void* text, size_t byteLength); 138 139 int find(const SkBitmap& bitmap); 140 141 #ifdef SK_DEBUG_DUMP 142 public: 143 void dumpMatrices(); 144 void dumpPaints(); 145 #endif 146 147 #ifdef SK_DEBUG_SIZE 148 public: 149 size_t size() const; 150 int bitmaps(size_t* size) const; 151 int matrices(size_t* size) const; 152 int paints(size_t* size) const; 153 int paths(size_t* size) const; 154 int regions(size_t* size) const; 155 size_t streamlen() const; 156 157 size_t fPointBytes, fRectBytes, fTextBytes; 158 int fPointWrites, fRectWrites, fTextWrites; 159 #endif 160 161 #ifdef SK_DEBUG_VALIDATE 162 public: 163 void validate() const; 164 private: 165 void validateBitmaps() const; 166 void validateMatrices() const; 167 void validatePaints() const; 168 void validatePaths() const; 169 void validateRegions() const; 170 #else 171 public: validate()172 void validate() const {} 173 #endif 174 175 protected: 176 177 // These are set to NULL in our constructor, but may be changed by 178 // subclasses, in which case they will be SkSafeUnref'd in our destructor. 179 SkBBoxHierarchy* fBoundingHierarchy; 180 SkPictureStateTree* fStateTree; 181 182 // Allocated in the constructor and managed by this class. 183 SkBitmapHeap* fBitmapHeap; 184 185 private: 186 SkChunkFlatController fFlattenableHeap; 187 188 SkMatrixDictionary fMatrices; 189 SkPaintDictionary fPaints; 190 SkRegionDictionary fRegions; 191 192 SkPathHeap* fPathHeap; // reference counted 193 SkWriter32 fWriter; 194 195 // we ref each item in these arrays 196 SkTDArray<SkPicture*> fPictureRefs; 197 198 uint32_t fRecordFlags; 199 int fInitialSaveCount; 200 201 friend class SkPicturePlayback; 202 friend class SkPictureTester; // for unit testing 203 204 typedef SkCanvas INHERITED; 205 }; 206 207 #endif 208