blob: 88ff302b7c82326f028529af8275639f513bd056 [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
rileya@google.com9f5898d2012-09-11 20:21:44 +000019class SkPictureStateTree;
20class SkBBoxHierarchy;
21
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000022// 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
reed@android.com8a1c16f2008-12-17 15:59:43 +000031class SkPictureRecord : public SkCanvas {
32public:
reed@google.comd86e7ab2012-09-27 20:31:31 +000033 SkPictureRecord(uint32_t recordFlags, SkDevice*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 virtual ~SkPictureRecord();
35
junov@chromium.org4e6dfa52012-07-16 14:04:59 +000036 virtual SkDevice* setDevice(SkDevice* device) SK_OVERRIDE;
37
reed@google.com2d4297c2011-10-06 13:14:12 +000038 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;
reed@google.com071eef92011-10-12 11:52:53 +000047 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000048 virtual bool clipRRect(const SkRRect&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com071eef92011-10-12 11:52:53 +000049 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000050 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;
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000054 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000055 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;
reed@google.com2d4297c2011-10-06 13:14:12 +000058 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000060 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +000061 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
62 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000064 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000065 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000066 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000068 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000069 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000070 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000071 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000072 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 virtual void drawPosTextH(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000074 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000075 virtual void drawTextOnPath(const void* text, size_t byteLength,
76 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +000077 const SkPaint&) SK_OVERRIDE;
78 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 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,
reed@google.com2d4297c2011-10-06 13:14:12 +000083 const SkPaint&) SK_OVERRIDE;
84 virtual void drawData(const void*, size_t) SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000085 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000086
junov@chromium.org3f5ecd62013-01-22 18:01:26 +000087 void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&,
reed@google.com45954262012-12-07 17:14:40 +000088 SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000089
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 const SkTDArray<SkPicture* >& getPictureRefs() const {
91 return fPictureRefs;
92 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000093
junov@chromium.org4866cc02012-06-01 21:23:07 +000094 void setFlags(uint32_t recordFlags) {
95 fRecordFlags = recordFlags;
96 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000097
98 const SkWriter32& writeStream() const {
99 return fWriter;
100 }
101
reed@google.comd86e7ab2012-09-27 20:31:31 +0000102 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000103 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +0000104
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +0000106 void handleOptimization(int opt);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +0000107 void recordRestoreOffsetPlaceholder(SkRegion::Op);
108 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(
109 uint32_t restoreOffset);
110
reed@google.comffacd3c2012-08-30 15:31:23 +0000111 SkTDArray<int32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000112 int fFirstSavedLayerIndex;
113 enum {
114 kNoSavedLayerIndex = -1
115 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000117 /*
118 * Write the 'drawType' operation and chunk size to the skp. 'size'
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000119 * can potentially be increased if the chunk size needs its own storage
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000120 * location (i.e., it overflows 24 bits).
121 * Returns the start offset of the chunk. This is the location at which
122 * the opcode & size are stored.
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000123 * TODO: since we are handing the size into here we could call reserve
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000124 * and then return a pointer to the memory storage. This could decrease
125 * allocation overhead but could lead to more wasted space (the tail
126 * end of blocks could go unused). Possibly add a second addDraw that
127 * operates in this manner.
128 */
129 uint32_t addDraw(DrawType drawType, uint32_t* size) {
130 uint32_t offset = fWriter.size();
131
reed@google.com97af1a62012-08-28 12:19:02 +0000132 this->predrawNotify();
133
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000134 #ifdef SK_DEBUG_TRACE
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 SkDebugf("add %s\n", DrawTypeToString(drawType));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000136 #endif
137
138 SkASSERT(0 != *size);
139 SkASSERT(((uint8_t) drawType) == drawType);
140
141 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
142 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
143 *size += 1;
144 fWriter.writeInt(*size);
145 } else {
146 fWriter.writeInt(PACK_8_24(drawType, *size));
147 }
148
149 return offset;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000150 }
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000151
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 void addInt(int value) {
153 fWriter.writeInt(value);
154 }
155 void addScalar(SkScalar scalar) {
156 fWriter.writeScalar(scalar);
157 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000158
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 void addBitmap(const SkBitmap& bitmap);
160 void addMatrix(const SkMatrix& matrix);
161 void addMatrixPtr(const SkMatrix* matrix);
junov@chromium.orgf3b12232013-01-22 17:50:47 +0000162 const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
163 const SkFlatData* addPaintPtr(const SkPaint* paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 void addPath(const SkPath& path);
165 void addPicture(SkPicture& picture);
166 void addPoint(const SkPoint& point);
167 void addPoints(const SkPoint pts[], int count);
168 void addRect(const SkRect& rect);
169 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000170 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 void addIRectPtr(const SkIRect* rect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000172 void addRRect(const SkRRect&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 void addRegion(const SkRegion& region);
174 void addText(const void* text, size_t byteLength);
175
junov@chromium.org4866cc02012-06-01 21:23:07 +0000176 int find(const SkBitmap& bitmap);
177
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178#ifdef SK_DEBUG_DUMP
179public:
180 void dumpMatrices();
181 void dumpPaints();
182#endif
183
184#ifdef SK_DEBUG_SIZE
185public:
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000186 size_t size() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 int bitmaps(size_t* size) const;
188 int matrices(size_t* size) const;
189 int paints(size_t* size) const;
190 int paths(size_t* size) const;
191 int regions(size_t* size) const;
192 size_t streamlen() const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000193
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 size_t fPointBytes, fRectBytes, fTextBytes;
195 int fPointWrites, fRectWrites, fTextWrites;
196#endif
197
198#ifdef SK_DEBUG_VALIDATE
199public:
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000200 void validate(uint32_t initialOffset, uint32_t size) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201private:
202 void validateBitmaps() const;
203 void validateMatrices() const;
204 void validatePaints() const;
205 void validatePaths() const;
206 void validateRegions() const;
207#else
208public:
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000209 void validate(uint32_t initialOffset, uint32_t size) const {
210 SkASSERT(fWriter.size() == initialOffset + size);
211 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212#endif
213
rileya@google.com9f5898d2012-09-11 20:21:44 +0000214protected:
215
216 // These are set to NULL in our constructor, but may be changed by
217 // subclasses, in which case they will be SkSafeUnref'd in our destructor.
218 SkBBoxHierarchy* fBoundingHierarchy;
219 SkPictureStateTree* fStateTree;
220
robertphillips@google.com801cee12012-10-19 19:06:11 +0000221 // Allocated in the constructor and managed by this class.
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000222 SkBitmapHeap* fBitmapHeap;
robertphillips@google.com801cee12012-10-19 19:06:11 +0000223
224private:
djsollen@google.com21830d92012-08-07 19:49:41 +0000225 SkChunkFlatController fFlattenableHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000226
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000227 SkMatrixDictionary fMatrices;
228 SkPaintDictionary fPaints;
229 SkRegionDictionary fRegions;
230
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 SkPathHeap* fPathHeap; // reference counted
232 SkWriter32 fWriter;
233
reed@android.com09b84a02009-06-26 20:22:26 +0000234 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 SkTDArray<SkPicture*> fPictureRefs;
236
reed@android.comae814c82009-02-13 14:56:09 +0000237 uint32_t fRecordFlags;
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000238 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000239
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000241 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242
243 typedef SkCanvas INHERITED;
244};
245
246#endif