epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 SkPictureRecord : public SkCanvas { |
| 20 | public: |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 21 | SkPictureRecord(uint32_t recordFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | virtual ~SkPictureRecord(); |
| 23 | |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 24 | virtual int save(SaveFlags) SK_OVERRIDE; |
| 25 | virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE; |
| 26 | virtual void restore() SK_OVERRIDE; |
| 27 | virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE; |
| 28 | virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE; |
| 29 | virtual bool rotate(SkScalar degrees) SK_OVERRIDE; |
| 30 | virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE; |
| 31 | virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE; |
| 32 | virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE; |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 33 | virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE; |
| 34 | virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE; |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 35 | virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE; |
| 36 | virtual void clear(SkColor) SK_OVERRIDE; |
| 37 | virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | virtual void drawPoints(PointMode, size_t count, const SkPoint pts[], |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 39 | const SkPaint&) SK_OVERRIDE; |
| 40 | virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE; |
| 41 | virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 43 | const SkPaint*) SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src, |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 45 | const SkRect& dst, const SkPaint*) SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&, |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 47 | const SkPaint*) SK_OVERRIDE; |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 48 | virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 49 | const SkRect& dst, const SkPaint*) SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | virtual void drawSprite(const SkBitmap&, int left, int top, |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 51 | const SkPaint*) SK_OVERRIDE; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 52 | virtual void drawText(const void* text, size_t byteLength, SkScalar x, |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 53 | SkScalar y, const SkPaint&) SK_OVERRIDE; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 54 | virtual void drawPosText(const void* text, size_t byteLength, |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 55 | const SkPoint pos[], const SkPaint&) SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | virtual void drawPosTextH(const void* text, size_t byteLength, |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 57 | const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 58 | virtual void drawTextOnPath(const void* text, size_t byteLength, |
| 59 | const SkPath& path, const SkMatrix* matrix, |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 60 | const SkPaint&) SK_OVERRIDE; |
| 61 | virtual void drawPicture(SkPicture& picture) SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | virtual void drawVertices(VertexMode, int vertexCount, |
| 63 | const SkPoint vertices[], const SkPoint texs[], |
| 64 | const SkColor colors[], SkXfermode*, |
| 65 | const uint16_t indices[], int indexCount, |
reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 66 | const SkPaint&) SK_OVERRIDE; |
| 67 | virtual void drawData(const void*, size_t) SK_OVERRIDE; |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 68 | virtual bool isDrawingToLayer() const SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 70 | void addFontMetricsTopBottom(const SkPaint& paint, SkScalar minY, SkScalar maxY); |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 71 | |
djsollen@google.com | d2700ee | 2012-05-30 16:54:13 +0000 | [diff] [blame] | 72 | const SkBitmapDictionary& getBitmaps() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | return fBitmaps; |
| 74 | } |
djsollen@google.com | d2700ee | 2012-05-30 16:54:13 +0000 | [diff] [blame] | 75 | const SkMatrixDictionary& getMatrices() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | return fMatrices; |
| 77 | } |
djsollen@google.com | d2700ee | 2012-05-30 16:54:13 +0000 | [diff] [blame] | 78 | const SkPaintDictionary& getPaints() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | return fPaints; |
| 80 | } |
| 81 | const SkTDArray<SkPicture* >& getPictureRefs() const { |
| 82 | return fPictureRefs; |
| 83 | } |
djsollen@google.com | d2700ee | 2012-05-30 16:54:13 +0000 | [diff] [blame] | 84 | const SkRegionDictionary& getRegions() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | return fRegions; |
| 86 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 87 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | void reset(); |
junov@chromium.org | 4866cc0 | 2012-06-01 21:23:07 +0000 | [diff] [blame] | 89 | void setFlags(uint32_t recordFlags) { |
| 90 | fRecordFlags = recordFlags; |
| 91 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | |
| 93 | const SkWriter32& writeStream() const { |
| 94 | return fWriter; |
| 95 | } |
| 96 | |
junov@chromium.org | 4866cc0 | 2012-06-01 21:23:07 +0000 | [diff] [blame] | 97 | bool shouldFlattenPixels(const SkBitmap&) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | private: |
junov@chromium.org | 4866cc0 | 2012-06-01 21:23:07 +0000 | [diff] [blame] | 99 | struct PixelRefDictionaryEntry { |
| 100 | uint32_t fKey; // SkPixelRef GenerationID. |
| 101 | uint32_t fIndex; // Index of corresponding flattened bitmap in fBitmaps. |
| 102 | bool operator < (const PixelRefDictionaryEntry& other) const { |
| 103 | return this->fKey < other.fKey; |
| 104 | } |
| 105 | bool operator != (const PixelRefDictionaryEntry& other) const { |
| 106 | return this->fKey != other.fKey; |
| 107 | } |
| 108 | }; |
| 109 | |
junov@chromium.org | e3dbedb | 2012-07-09 16:03:55 +0000 | [diff] [blame^] | 110 | void recordRestoreOffsetPlaceholder(SkRegion::Op); |
| 111 | void fillRestoreOffsetPlaceholdersForCurrentStackLevel( |
| 112 | uint32_t restoreOffset); |
| 113 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | SkTDArray<uint32_t> fRestoreOffsetStack; |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 115 | int fFirstSavedLayerIndex; |
| 116 | enum { |
| 117 | kNoSavedLayerIndex = -1 |
| 118 | }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | |
| 120 | void addDraw(DrawType drawType) { |
| 121 | #ifdef SK_DEBUG_TRACE |
| 122 | SkDebugf("add %s\n", DrawTypeToString(drawType)); |
| 123 | #endif |
| 124 | fWriter.writeInt(drawType); |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 125 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | void addInt(int value) { |
| 127 | fWriter.writeInt(value); |
| 128 | } |
| 129 | void addScalar(SkScalar scalar) { |
| 130 | fWriter.writeScalar(scalar); |
| 131 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 132 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | void addBitmap(const SkBitmap& bitmap); |
| 134 | void addMatrix(const SkMatrix& matrix); |
| 135 | void addMatrixPtr(const SkMatrix* matrix); |
| 136 | void addPaint(const SkPaint& paint); |
| 137 | void addPaintPtr(const SkPaint* paint); |
| 138 | void addPath(const SkPath& path); |
| 139 | void addPicture(SkPicture& picture); |
| 140 | void addPoint(const SkPoint& point); |
| 141 | void addPoints(const SkPoint pts[], int count); |
| 142 | void addRect(const SkRect& rect); |
| 143 | void addRectPtr(const SkRect* rect); |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 144 | void addIRect(const SkIRect& rect); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | void addIRectPtr(const SkIRect* rect); |
| 146 | void addRegion(const SkRegion& region); |
| 147 | void addText(const void* text, size_t byteLength); |
| 148 | |
junov@chromium.org | 4866cc0 | 2012-06-01 21:23:07 +0000 | [diff] [blame] | 149 | int find(const SkBitmap& bitmap); |
| 150 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | #ifdef SK_DEBUG_DUMP |
| 152 | public: |
| 153 | void dumpMatrices(); |
| 154 | void dumpPaints(); |
| 155 | #endif |
| 156 | |
| 157 | #ifdef SK_DEBUG_SIZE |
| 158 | public: |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 159 | size_t size() const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | int bitmaps(size_t* size) const; |
| 161 | int matrices(size_t* size) const; |
| 162 | int paints(size_t* size) const; |
| 163 | int paths(size_t* size) const; |
| 164 | int regions(size_t* size) const; |
| 165 | size_t streamlen() const; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 166 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | size_t fPointBytes, fRectBytes, fTextBytes; |
| 168 | int fPointWrites, fRectWrites, fTextWrites; |
| 169 | #endif |
| 170 | |
| 171 | #ifdef SK_DEBUG_VALIDATE |
| 172 | public: |
| 173 | void validate() const; |
| 174 | private: |
| 175 | void validateBitmaps() const; |
| 176 | void validateMatrices() const; |
| 177 | void validatePaints() const; |
| 178 | void validatePaths() const; |
| 179 | void validateRegions() const; |
| 180 | #else |
| 181 | public: |
| 182 | void validate() const {} |
| 183 | #endif |
| 184 | |
| 185 | private: |
| 186 | SkChunkAlloc fHeap; |
junov@chromium.org | 4866cc0 | 2012-06-01 21:23:07 +0000 | [diff] [blame] | 187 | |
| 188 | SkTDArray<PixelRefDictionaryEntry> fPixelRefDictionary; |
djsollen@google.com | d2700ee | 2012-05-30 16:54:13 +0000 | [diff] [blame] | 189 | SkBitmapDictionary fBitmaps; |
| 190 | SkMatrixDictionary fMatrices; |
| 191 | SkPaintDictionary fPaints; |
| 192 | SkRegionDictionary fRegions; |
| 193 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | SkPathHeap* fPathHeap; // reference counted |
| 195 | SkWriter32 fWriter; |
| 196 | |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 197 | // we ref each item in these arrays |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | SkTDArray<SkPicture*> fPictureRefs; |
| 199 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 200 | SkRefCntSet fRCSet; |
| 201 | SkRefCntSet fTFSet; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 202 | |
reed@android.com | ae814c8 | 2009-02-13 14:56:09 +0000 | [diff] [blame] | 203 | uint32_t fRecordFlags; |
| 204 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 205 | friend class SkPicturePlayback; |
junov@chromium.org | 1cc8f6f | 2012-02-22 21:00:42 +0000 | [diff] [blame] | 206 | friend class SkPictureTester; // for unit testing |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | |
| 208 | typedef SkCanvas INHERITED; |
| 209 | }; |
| 210 | |
| 211 | #endif |