blob: 1b62f3dadc0300d8cb24025dcbb51f1d7a4bbc7d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.com76f10a32014-02-05 15:32:21 +00007
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"
robertphillips@google.com105a4a52014-02-11 15:10:40 +000013#ifdef SK_COLLAPSE_MATRIX_CLIP_STATE
14#include "SkMatrixClipStateMgr.h"
15#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkPathHeap.h"
17#include "SkPicture.h"
18#include "SkPictureFlat.h"
19#include "SkTemplates.h"
20#include "SkWriter32.h"
21
rileya@google.com9f5898d2012-09-11 20:21:44 +000022class SkPictureStateTree;
23class SkBBoxHierarchy;
24
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000025// These macros help with packing and unpacking a single byte value and
26// a 3 byte value into/out of a uint32_t
27#define MASK_24 0x00FFFFFF
28#define UNPACK_8_24(combined, small, large) \
29 small = (combined >> 24) & 0xFF; \
30 large = combined & MASK_24;
31#define PACK_8_24(small, large) ((small << 24) | large)
32
33
reed@android.com8a1c16f2008-12-17 15:59:43 +000034class SkPictureRecord : public SkCanvas {
35public:
commit-bot@chromium.org19fafef2014-02-17 15:28:00 +000036 SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 virtual ~SkPictureRecord();
38
reed@google.com2d4297c2011-10-06 13:14:12 +000039 virtual int save(SaveFlags) SK_OVERRIDE;
40 virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
41 virtual void restore() SK_OVERRIDE;
42 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
43 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
44 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
45 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
46 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
47 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000048 virtual void clear(SkColor) SK_OVERRIDE;
49 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000051 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000052 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000053 virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000054 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000055 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000057 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +000058 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +000059 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +000060 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000062 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000063 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000064 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000066 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000067 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000068 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000069 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000070 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 virtual void drawPosTextH(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000072 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000073 virtual void drawTextOnPath(const void* text, size_t byteLength,
74 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +000075 const SkPaint&) SK_OVERRIDE;
76 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 virtual void drawVertices(VertexMode, int vertexCount,
78 const SkPoint vertices[], const SkPoint texs[],
79 const SkColor colors[], SkXfermode*,
80 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +000081 const SkPaint&) SK_OVERRIDE;
82 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +000083 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
84 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
85 virtual void endCommentGroup() SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000086 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000087
junov@chromium.org3f5ecd62013-01-22 18:01:26 +000088 void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&,
reed@google.com45954262012-12-07 17:14:40 +000089 SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 const SkTDArray<SkPicture* >& getPictureRefs() const {
92 return fPictureRefs;
93 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000094
junov@chromium.org4866cc02012-06-01 21:23:07 +000095 void setFlags(uint32_t recordFlags) {
96 fRecordFlags = recordFlags;
97 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000098
99 const SkWriter32& writeStream() const {
100 return fWriter;
101 }
102
reed@google.comd86e7ab2012-09-27 20:31:31 +0000103 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000104 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +0000107 void handleOptimization(int opt);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000108 int recordRestoreOffsetPlaceholder(SkRegion::Op);
109 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +0000110
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000111#ifndef SK_COLLAPSE_MATRIX_CLIP_STATE
reed@google.comffacd3c2012-08-30 15:31:23 +0000112 SkTDArray<int32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000113 int fFirstSavedLayerIndex;
114 enum {
115 kNoSavedLayerIndex = -1
116 };
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000117#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000119 SkTDArray<uint32_t> fCullOffsetStack;
120
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000121 /*
122 * Write the 'drawType' operation and chunk size to the skp. 'size'
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000123 * can potentially be increased if the chunk size needs its own storage
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000124 * location (i.e., it overflows 24 bits).
125 * Returns the start offset of the chunk. This is the location at which
126 * the opcode & size are stored.
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000127 * TODO: since we are handing the size into here we could call reserve
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000128 * and then return a pointer to the memory storage. This could decrease
129 * allocation overhead but could lead to more wasted space (the tail
130 * end of blocks could go unused). Possibly add a second addDraw that
131 * operates in this manner.
132 */
robertphillips@google.com8b169312013-10-15 17:47:36 +0000133 size_t addDraw(DrawType drawType, uint32_t* size) {
reed@google.com44699382013-10-31 17:28:30 +0000134 size_t offset = fWriter.bytesWritten();
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000135
reed@google.com97af1a62012-08-28 12:19:02 +0000136 this->predrawNotify();
137
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000138 #ifdef SK_DEBUG_TRACE
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 SkDebugf("add %s\n", DrawTypeToString(drawType));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000140 #endif
141
142 SkASSERT(0 != *size);
143 SkASSERT(((uint8_t) drawType) == drawType);
144
145 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
146 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
147 *size += 1;
148 fWriter.writeInt(*size);
149 } else {
150 fWriter.writeInt(PACK_8_24(drawType, *size));
151 }
152
153 return offset;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000154 }
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 void addInt(int value) {
157 fWriter.writeInt(value);
158 }
159 void addScalar(SkScalar scalar) {
160 fWriter.writeScalar(scalar);
161 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000162
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 void addBitmap(const SkBitmap& bitmap);
164 void addMatrix(const SkMatrix& matrix);
junov@chromium.orgf3b12232013-01-22 17:50:47 +0000165 const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
166 const SkFlatData* addPaintPtr(const SkPaint* paint);
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000167 void addFlatPaint(const SkFlatData* flatPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 void addPath(const SkPath& path);
169 void addPicture(SkPicture& picture);
170 void addPoint(const SkPoint& point);
171 void addPoints(const SkPoint pts[], int count);
172 void addRect(const SkRect& rect);
173 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000174 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 void addIRectPtr(const SkIRect* rect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000176 void addRRect(const SkRRect&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 void addRegion(const SkRegion& region);
178 void addText(const void* text, size_t byteLength);
179
junov@chromium.org4866cc02012-06-01 21:23:07 +0000180 int find(const SkBitmap& bitmap);
181
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182#ifdef SK_DEBUG_DUMP
183public:
184 void dumpMatrices();
185 void dumpPaints();
186#endif
187
188#ifdef SK_DEBUG_SIZE
189public:
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000190 size_t size() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 int bitmaps(size_t* size) const;
192 int matrices(size_t* size) const;
193 int paints(size_t* size) const;
194 int paths(size_t* size) const;
195 int regions(size_t* size) const;
196 size_t streamlen() const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000197
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198 size_t fPointBytes, fRectBytes, fTextBytes;
199 int fPointWrites, fRectWrites, fTextWrites;
200#endif
201
202#ifdef SK_DEBUG_VALIDATE
203public:
robertphillips@google.com8b169312013-10-15 17:47:36 +0000204 void validate(size_t initialOffset, uint32_t size) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205private:
206 void validateBitmaps() const;
207 void validateMatrices() const;
208 void validatePaints() const;
209 void validatePaths() const;
210 void validateRegions() const;
211#else
212public:
robertphillips@google.com8b169312013-10-15 17:47:36 +0000213 void validate(size_t initialOffset, uint32_t size) const {
reed@google.com44699382013-10-31 17:28:30 +0000214 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000215 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216#endif
217
rileya@google.com9f5898d2012-09-11 20:21:44 +0000218protected:
reed@google.com76f10a32014-02-05 15:32:21 +0000219 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000220 const void* onPeekPixels(SkImageInfo*, size_t*) SK_OVERRIDE {
221 return NULL;
222 }
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000223 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000224 virtual void onPushCull(const SkRect&) SK_OVERRIDE;
225 virtual void onPopCull() SK_OVERRIDE;
reed@google.com76f10a32014-02-05 15:32:21 +0000226
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000227 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
228 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
229 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
230 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
231
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000232 // Return fontmetrics.fTop,fBottom in topbot[0,1], after they have been
233 // tweaked by paint.computeFastBounds().
234 static void ComputeFontMetricsTopBottom(const SkPaint& paint, SkScalar topbot[2]);
235
236 // Make sure that flat has fTopBot written.
237 static void WriteTopBot(const SkPaint& paint, const SkFlatData& flat) {
238 if (!flat.isTopBotWritten()) {
239 ComputeFontMetricsTopBottom(paint, flat.writableTopBot());
240 SkASSERT(flat.isTopBotWritten());
241 }
242 }
243 // Will return a cached version when possible.
244 const SkFlatData* getFlatPaintData(const SkPaint& paint);
245 /**
246 * SkBBoxRecord::drawPosTextH gets a flat paint and uses it,
247 * then it calls this, using the extra parameter, to avoid duplication.
248 */
249 void drawPosTextHImpl(const void* text, size_t byteLength,
250 const SkScalar xpos[], SkScalar constY,
251 const SkPaint& paint, const SkFlatData* flatPaintData);
rileya@google.com9f5898d2012-09-11 20:21:44 +0000252
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000253 int addPathToHeap(const SkPath& path); // does not write to ops stream
254
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000255 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000256 // restores to be deferred (e.g., if the MC state is being collapsed and
257 // only written out as needed).
258 void recordConcat(const SkMatrix& matrix);
259 int recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
260 int recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
261 int recordClipPath(int pathID, SkRegion::Op op, bool doAA);
262 int recordClipRegion(const SkRegion& region, SkRegion::Op op);
263 void recordSave(SaveFlags flags);
264 void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000265 void recordRestore(bool fillInSkips = true);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000266
rileya@google.com9f5898d2012-09-11 20:21:44 +0000267 // These are set to NULL in our constructor, but may be changed by
268 // subclasses, in which case they will be SkSafeUnref'd in our destructor.
269 SkBBoxHierarchy* fBoundingHierarchy;
270 SkPictureStateTree* fStateTree;
271
robertphillips@google.com801cee12012-10-19 19:06:11 +0000272 // Allocated in the constructor and managed by this class.
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000273 SkBitmapHeap* fBitmapHeap;
robertphillips@google.com801cee12012-10-19 19:06:11 +0000274
275private:
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000276 friend class MatrixClipState; // for access to *Impl methods
277 friend class SkMatrixClipStateMgr; // for access to *Impl methods
278
djsollen@google.com21830d92012-08-07 19:49:41 +0000279 SkChunkFlatController fFlattenableHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000280
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000281 SkPaintDictionary fPaints;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000282
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 SkPathHeap* fPathHeap; // reference counted
284 SkWriter32 fWriter;
285
reed@android.com09b84a02009-06-26 20:22:26 +0000286 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 SkTDArray<SkPicture*> fPictureRefs;
288
reed@android.comae814c82009-02-13 14:56:09 +0000289 uint32_t fRecordFlags;
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000290 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000291
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000293 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000295#ifdef SK_COLLAPSE_MATRIX_CLIP_STATE
296 SkMatrixClipStateMgr fMCMgr;
297#endif
298
reed@android.com8a1c16f2008-12-17 15:59:43 +0000299 typedef SkCanvas INHERITED;
300};
301
302#endif