blob: eca42aa57ad32eb36a81906d233a53f6ece7231b [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 SkBBoxHierarchy;
commit-bot@chromium.org8016f792014-03-07 15:53:01 +000023class SkOffsetTable;
24class SkPictureStateTree;
rileya@google.com9f5898d2012-09-11 20:21:44 +000025
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000026// These macros help with packing and unpacking a single byte value and
27// a 3 byte value into/out of a uint32_t
28#define MASK_24 0x00FFFFFF
29#define UNPACK_8_24(combined, small, large) \
30 small = (combined >> 24) & 0xFF; \
31 large = combined & MASK_24;
32#define PACK_8_24(small, large) ((small << 24) | large)
33
34
reed@android.com8a1c16f2008-12-17 15:59:43 +000035class SkPictureRecord : public SkCanvas {
36public:
commit-bot@chromium.org19fafef2014-02-17 15:28:00 +000037 SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 virtual ~SkPictureRecord();
39
reed@google.com2d4297c2011-10-06 13:14:12 +000040 virtual void clear(SkColor) SK_OVERRIDE;
41 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000043 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000044 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000045 virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000046 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000047 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000049 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +000050 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +000051 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +000052 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000054 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000055 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000056 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000058 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000059 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000060 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000061 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000062 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 virtual void drawPosTextH(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000064 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000065 virtual void drawTextOnPath(const void* text, size_t byteLength,
66 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +000067 const SkPaint&) SK_OVERRIDE;
68 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 virtual void drawVertices(VertexMode, int vertexCount,
70 const SkPoint vertices[], const SkPoint texs[],
71 const SkColor colors[], SkXfermode*,
72 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +000073 const SkPaint&) SK_OVERRIDE;
74 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +000075 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
76 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
77 virtual void endCommentGroup() SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000078 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000079
junov@chromium.org3f5ecd62013-01-22 18:01:26 +000080 void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&,
reed@google.com45954262012-12-07 17:14:40 +000081 SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 const SkTDArray<SkPicture* >& getPictureRefs() const {
84 return fPictureRefs;
85 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000086
junov@chromium.org4866cc02012-06-01 21:23:07 +000087 void setFlags(uint32_t recordFlags) {
88 fRecordFlags = recordFlags;
89 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
91 const SkWriter32& writeStream() const {
92 return fWriter;
93 }
94
reed@google.comd86e7ab2012-09-27 20:31:31 +000095 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +000096 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +000097
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +000098 void internalOnly_EnableOpts(bool optsEnabled) {
99 fOptsEnabled = optsEnabled;
100 }
101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +0000103 void handleOptimization(int opt);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000104 int recordRestoreOffsetPlaceholder(SkRegion::Op);
105 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +0000106
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000107#ifndef SK_COLLAPSE_MATRIX_CLIP_STATE
reed@google.comffacd3c2012-08-30 15:31:23 +0000108 SkTDArray<int32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000109 int fFirstSavedLayerIndex;
110 enum {
111 kNoSavedLayerIndex = -1
112 };
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000113#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000115 SkTDArray<uint32_t> fCullOffsetStack;
116
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 */
robertphillips@google.com8b169312013-10-15 17:47:36 +0000129 size_t addDraw(DrawType drawType, uint32_t* size) {
reed@google.com44699382013-10-31 17:28:30 +0000130 size_t offset = fWriter.bytesWritten();
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000131
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
commit-bot@chromium.org8016f792014-03-07 15:53:01 +0000159 // The command at 'offset' in the skp uses the specified bitmap
160 void trackBitmapUse(int bitmapID, size_t offset);
161 int addBitmap(const SkBitmap& bitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 void addMatrix(const SkMatrix& matrix);
junov@chromium.orgf3b12232013-01-22 17:50:47 +0000163 const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
164 const SkFlatData* addPaintPtr(const SkPaint* paint);
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000165 void addFlatPaint(const SkFlatData* flatPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 void addPath(const SkPath& path);
167 void addPicture(SkPicture& picture);
168 void addPoint(const SkPoint& point);
169 void addPoints(const SkPoint pts[], int count);
170 void addRect(const SkRect& rect);
171 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000172 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 void addIRectPtr(const SkIRect* rect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000174 void addRRect(const SkRRect&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 void addRegion(const SkRegion& region);
176 void addText(const void* text, size_t byteLength);
177
junov@chromium.org4866cc02012-06-01 21:23:07 +0000178 int find(const SkBitmap& bitmap);
179
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180#ifdef SK_DEBUG_DUMP
181public:
182 void dumpMatrices();
183 void dumpPaints();
184#endif
185
186#ifdef SK_DEBUG_SIZE
187public:
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000188 size_t size() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 int bitmaps(size_t* size) const;
190 int matrices(size_t* size) const;
191 int paints(size_t* size) const;
192 int paths(size_t* size) const;
193 int regions(size_t* size) const;
194 size_t streamlen() const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000195
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 size_t fPointBytes, fRectBytes, fTextBytes;
197 int fPointWrites, fRectWrites, fTextWrites;
198#endif
199
200#ifdef SK_DEBUG_VALIDATE
201public:
robertphillips@google.com8b169312013-10-15 17:47:36 +0000202 void validate(size_t initialOffset, uint32_t size) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203private:
204 void validateBitmaps() const;
205 void validateMatrices() const;
206 void validatePaints() const;
207 void validatePaths() const;
208 void validateRegions() const;
209#else
210public:
robertphillips@google.com8b169312013-10-15 17:47:36 +0000211 void validate(size_t initialOffset, uint32_t size) const {
reed@google.com44699382013-10-31 17:28:30 +0000212 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000213 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214#endif
215
rileya@google.com9f5898d2012-09-11 20:21:44 +0000216protected:
reed@google.com76f10a32014-02-05 15:32:21 +0000217 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000218 const void* onPeekPixels(SkImageInfo*, size_t*) SK_OVERRIDE {
219 return NULL;
220 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000221
222 virtual void willSave(SaveFlags) SK_OVERRIDE;
223 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
224 virtual void willRestore() SK_OVERRIDE;
225
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000226 virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
227 virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
228
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000229 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000230 virtual void onPushCull(const SkRect&) SK_OVERRIDE;
231 virtual void onPopCull() SK_OVERRIDE;
reed@google.com76f10a32014-02-05 15:32:21 +0000232
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000233 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
234 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
235 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
236 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
237
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000238 // Return fontmetrics.fTop,fBottom in topbot[0,1], after they have been
239 // tweaked by paint.computeFastBounds().
240 static void ComputeFontMetricsTopBottom(const SkPaint& paint, SkScalar topbot[2]);
241
242 // Make sure that flat has fTopBot written.
243 static void WriteTopBot(const SkPaint& paint, const SkFlatData& flat) {
244 if (!flat.isTopBotWritten()) {
245 ComputeFontMetricsTopBottom(paint, flat.writableTopBot());
246 SkASSERT(flat.isTopBotWritten());
247 }
248 }
249 // Will return a cached version when possible.
250 const SkFlatData* getFlatPaintData(const SkPaint& paint);
251 /**
252 * SkBBoxRecord::drawPosTextH gets a flat paint and uses it,
253 * then it calls this, using the extra parameter, to avoid duplication.
254 */
255 void drawPosTextHImpl(const void* text, size_t byteLength,
256 const SkScalar xpos[], SkScalar constY,
257 const SkPaint& paint, const SkFlatData* flatPaintData);
rileya@google.com9f5898d2012-09-11 20:21:44 +0000258
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000259 int addPathToHeap(const SkPath& path); // does not write to ops stream
260
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000261 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000262 // restores to be deferred (e.g., if the MC state is being collapsed and
263 // only written out as needed).
264 void recordConcat(const SkMatrix& matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000265 void recordTranslate(const SkMatrix& matrix);
266 void recordScale(const SkMatrix& matrix);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000267 int recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
268 int recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
269 int recordClipPath(int pathID, SkRegion::Op op, bool doAA);
270 int recordClipRegion(const SkRegion& region, SkRegion::Op op);
271 void recordSave(SaveFlags flags);
272 void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000273 void recordRestore(bool fillInSkips = true);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000274
rileya@google.com9f5898d2012-09-11 20:21:44 +0000275 // These are set to NULL in our constructor, but may be changed by
276 // subclasses, in which case they will be SkSafeUnref'd in our destructor.
277 SkBBoxHierarchy* fBoundingHierarchy;
278 SkPictureStateTree* fStateTree;
279
robertphillips@google.com801cee12012-10-19 19:06:11 +0000280 // Allocated in the constructor and managed by this class.
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000281 SkBitmapHeap* fBitmapHeap;
robertphillips@google.com801cee12012-10-19 19:06:11 +0000282
283private:
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000284 friend class MatrixClipState; // for access to *Impl methods
285 friend class SkMatrixClipStateMgr; // for access to *Impl methods
286
djsollen@google.com21830d92012-08-07 19:49:41 +0000287 SkChunkFlatController fFlattenableHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000288
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000289 SkPaintDictionary fPaints;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000290
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291 SkPathHeap* fPathHeap; // reference counted
292 SkWriter32 fWriter;
293
reed@android.com09b84a02009-06-26 20:22:26 +0000294 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 SkTDArray<SkPicture*> fPictureRefs;
296
reed@android.comae814c82009-02-13 14:56:09 +0000297 uint32_t fRecordFlags;
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +0000298 bool fOptsEnabled;
299 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000300
commit-bot@chromium.org8016f792014-03-07 15:53:01 +0000301 SkAutoTUnref<SkOffsetTable> fBitmapUseOffsets;
302
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000304 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000306#ifdef SK_COLLAPSE_MATRIX_CLIP_STATE
307 SkMatrixClipStateMgr fMCMgr;
308#endif
309
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310 typedef SkCanvas INHERITED;
311};
312
313#endif