blob: 4d0e96296fb98ccea79750f7abed62cc4b4d902d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#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:
reed@android.comae814c82009-02-13 14:56:09 +000021 SkPictureRecord(uint32_t recordFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 virtual ~SkPictureRecord();
23
reed@google.com2d4297c2011-10-06 13:14:12 +000024 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.com071eef92011-10-12 11:52:53 +000033 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
34 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000035 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.com8a1c16f2008-12-17 15:59:43 +000038 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000039 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.com8a1c16f2008-12-17 15:59:43 +000042 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000043 const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
reed@google.com2d4297c2011-10-06 13:14:12 +000045 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000047 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000048 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000049 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000051 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000052 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000053 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000054 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000055 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 virtual void drawPosTextH(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000057 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000058 virtual void drawTextOnPath(const void* text, size_t byteLength,
59 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +000060 const SkPaint&) SK_OVERRIDE;
61 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 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.com2d4297c2011-10-06 13:14:12 +000066 const SkPaint&) SK_OVERRIDE;
67 virtual void drawData(const void*, size_t) SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000068 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000069
reed@google.com9efd9a02012-01-30 15:41:43 +000070 void addFontMetricsTopBottom(const SkPaint& paint, SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000071
djsollen@google.comd2700ee2012-05-30 16:54:13 +000072 const SkBitmapDictionary& getBitmaps() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 return fBitmaps;
74 }
djsollen@google.comd2700ee2012-05-30 16:54:13 +000075 const SkMatrixDictionary& getMatrices() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 return fMatrices;
77 }
djsollen@google.comd2700ee2012-05-30 16:54:13 +000078 const SkPaintDictionary& getPaints() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 return fPaints;
80 }
81 const SkTDArray<SkPicture* >& getPictureRefs() const {
82 return fPictureRefs;
83 }
djsollen@google.comd2700ee2012-05-30 16:54:13 +000084 const SkRegionDictionary& getRegions() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 return fRegions;
86 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000087
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 void reset();
junov@chromium.org4866cc02012-06-01 21:23:07 +000089 void setFlags(uint32_t recordFlags) {
90 fRecordFlags = recordFlags;
91 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000092
93 const SkWriter32& writeStream() const {
94 return fWriter;
95 }
96
junov@chromium.org4866cc02012-06-01 21:23:07 +000097 bool shouldFlattenPixels(const SkBitmap&) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000098private:
junov@chromium.org4866cc02012-06-01 21:23:07 +000099 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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 SkTDArray<uint32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000111 int fFirstSavedLayerIndex;
112 enum {
113 kNoSavedLayerIndex = -1
114 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115
116 void addDraw(DrawType drawType) {
117#ifdef SK_DEBUG_TRACE
118 SkDebugf("add %s\n", DrawTypeToString(drawType));
119#endif
120 fWriter.writeInt(drawType);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000121 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122 void addInt(int value) {
123 fWriter.writeInt(value);
124 }
125 void addScalar(SkScalar scalar) {
126 fWriter.writeScalar(scalar);
127 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 void addBitmap(const SkBitmap& bitmap);
130 void addMatrix(const SkMatrix& matrix);
131 void addMatrixPtr(const SkMatrix* matrix);
132 void addPaint(const SkPaint& paint);
133 void addPaintPtr(const SkPaint* paint);
134 void addPath(const SkPath& path);
135 void addPicture(SkPicture& picture);
136 void addPoint(const SkPoint& point);
137 void addPoints(const SkPoint pts[], int count);
138 void addRect(const SkRect& rect);
139 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000140 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 void addIRectPtr(const SkIRect* rect);
142 void addRegion(const SkRegion& region);
143 void addText(const void* text, size_t byteLength);
144
junov@chromium.org4866cc02012-06-01 21:23:07 +0000145 int find(const SkBitmap& bitmap);
146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147#ifdef SK_DEBUG_DUMP
148public:
149 void dumpMatrices();
150 void dumpPaints();
151#endif
152
153#ifdef SK_DEBUG_SIZE
154public:
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000155 size_t size() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 int bitmaps(size_t* size) const;
157 int matrices(size_t* size) const;
158 int paints(size_t* size) const;
159 int paths(size_t* size) const;
160 int regions(size_t* size) const;
161 size_t streamlen() const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000162
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 size_t fPointBytes, fRectBytes, fTextBytes;
164 int fPointWrites, fRectWrites, fTextWrites;
165#endif
166
167#ifdef SK_DEBUG_VALIDATE
168public:
169 void validate() const;
170private:
171 void validateBitmaps() const;
172 void validateMatrices() const;
173 void validatePaints() const;
174 void validatePaths() const;
175 void validateRegions() const;
176#else
177public:
178 void validate() const {}
179#endif
180
181private:
182 SkChunkAlloc fHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000183
184 SkTDArray<PixelRefDictionaryEntry> fPixelRefDictionary;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000185 SkBitmapDictionary fBitmaps;
186 SkMatrixDictionary fMatrices;
187 SkPaintDictionary fPaints;
188 SkRegionDictionary fRegions;
189
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 SkPathHeap* fPathHeap; // reference counted
191 SkWriter32 fWriter;
192
reed@android.com09b84a02009-06-26 20:22:26 +0000193 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 SkTDArray<SkPicture*> fPictureRefs;
195
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +0000196 SkRefCntSet fRCSet;
197 SkRefCntSet fTFSet;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000198
reed@android.comae814c82009-02-13 14:56:09 +0000199 uint32_t fRecordFlags;
200
reed@google.com45482d12011-08-29 19:02:39 +0000201 // helper function to handle save/restore culling offsets
202 void recordOffsetForRestore(SkRegion::Op op);
203
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000205 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206
207 typedef SkCanvas INHERITED;
208};
209
210#endif