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