blob: aba14e3ff32b85411c64bf82f410ccd43e51f5af [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 SkPictureStateTree;
rileya@google.com9f5898d2012-09-11 20:21:44 +000024
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 void clear(SkColor) SK_OVERRIDE;
40 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000042 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000043 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000044 virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000045 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000046 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000048 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +000049 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +000050 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +000051 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000053 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000054 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000055 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000057 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000058 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000059 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000060 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000061 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 virtual void drawPosTextH(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000063 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000064 virtual void drawTextOnPath(const void* text, size_t byteLength,
65 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +000066 const SkPaint&) SK_OVERRIDE;
67 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 virtual void drawVertices(VertexMode, int vertexCount,
69 const SkPoint vertices[], const SkPoint texs[],
70 const SkColor colors[], SkXfermode*,
71 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +000072 const SkPaint&) SK_OVERRIDE;
73 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +000074 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
75 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
76 virtual void endCommentGroup() SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000077 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000078
junov@chromium.org3f5ecd62013-01-22 18:01:26 +000079 void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&,
reed@google.com45954262012-12-07 17:14:40 +000080 SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000081
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 const SkTDArray<SkPicture* >& getPictureRefs() const {
83 return fPictureRefs;
84 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000085
junov@chromium.org4866cc02012-06-01 21:23:07 +000086 void setFlags(uint32_t recordFlags) {
87 fRecordFlags = recordFlags;
88 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000089
90 const SkWriter32& writeStream() const {
91 return fWriter;
92 }
93
reed@google.comd86e7ab2012-09-27 20:31:31 +000094 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +000095 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +000096
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +000097 void internalOnly_EnableOpts(bool optsEnabled) {
98 fOptsEnabled = optsEnabled;
99 }
100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +0000102 void handleOptimization(int opt);
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000103 size_t recordRestoreOffsetPlaceholder(SkRegion::Op);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000104 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +0000105
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000106#ifndef SK_COLLAPSE_MATRIX_CLIP_STATE
reed@google.comffacd3c2012-08-30 15:31:23 +0000107 SkTDArray<int32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000108 int fFirstSavedLayerIndex;
109 enum {
110 kNoSavedLayerIndex = -1
111 };
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000112#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000114 SkTDArray<uint32_t> fCullOffsetStack;
115
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000116 /*
117 * Write the 'drawType' operation and chunk size to the skp. 'size'
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000118 * can potentially be increased if the chunk size needs its own storage
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000119 * location (i.e., it overflows 24 bits).
120 * Returns the start offset of the chunk. This is the location at which
121 * the opcode & size are stored.
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000122 * TODO: since we are handing the size into here we could call reserve
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000123 * and then return a pointer to the memory storage. This could decrease
124 * allocation overhead but could lead to more wasted space (the tail
125 * end of blocks could go unused). Possibly add a second addDraw that
126 * operates in this manner.
127 */
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000128 size_t addDraw(DrawType drawType, size_t* size) {
reed@google.com44699382013-10-31 17:28:30 +0000129 size_t offset = fWriter.bytesWritten();
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000130
reed@google.com97af1a62012-08-28 12:19:02 +0000131 this->predrawNotify();
132
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000133 #ifdef SK_DEBUG_TRACE
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 SkDebugf("add %s\n", DrawTypeToString(drawType));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000135 #endif
136
137 SkASSERT(0 != *size);
138 SkASSERT(((uint8_t) drawType) == drawType);
139
140 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
141 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
142 *size += 1;
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000143 fWriter.writeInt(SkToU32(*size));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000144 } else {
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000145 fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000146 }
147
148 return offset;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000149 }
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000150
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 void addInt(int value) {
152 fWriter.writeInt(value);
153 }
154 void addScalar(SkScalar scalar) {
155 fWriter.writeScalar(scalar);
156 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000157
commit-bot@chromium.org8016f792014-03-07 15:53:01 +0000158 // The command at 'offset' in the skp uses the specified bitmap
commit-bot@chromium.org8016f792014-03-07 15:53:01 +0000159 int addBitmap(const SkBitmap& bitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 void addMatrix(const SkMatrix& matrix);
junov@chromium.orgf3b12232013-01-22 17:50:47 +0000161 const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
162 const SkFlatData* addPaintPtr(const SkPaint* paint);
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000163 void addFlatPaint(const SkFlatData* flatPaint);
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.com8b169312013-10-15 17:47:36 +0000200 void validate(size_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:
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000209 void validate(size_t initialOffset, size_t size) const {
reed@google.com44699382013-10-31 17:28:30 +0000210 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000211 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212#endif
213
rileya@google.com9f5898d2012-09-11 20:21:44 +0000214protected:
reed@google.com76f10a32014-02-05 15:32:21 +0000215 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000216 const void* onPeekPixels(SkImageInfo*, size_t*) SK_OVERRIDE {
217 return NULL;
218 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000219
220 virtual void willSave(SaveFlags) SK_OVERRIDE;
221 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
222 virtual void willRestore() SK_OVERRIDE;
223
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000224 virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
225 virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
226
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000227 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000228 virtual void onPushCull(const SkRect&) SK_OVERRIDE;
229 virtual void onPopCull() SK_OVERRIDE;
reed@google.com76f10a32014-02-05 15:32:21 +0000230
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000231 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
232 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
233 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
234 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
235
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000236 // Return fontmetrics.fTop,fBottom in topbot[0,1], after they have been
237 // tweaked by paint.computeFastBounds().
238 static void ComputeFontMetricsTopBottom(const SkPaint& paint, SkScalar topbot[2]);
239
240 // Make sure that flat has fTopBot written.
241 static void WriteTopBot(const SkPaint& paint, const SkFlatData& flat) {
242 if (!flat.isTopBotWritten()) {
243 ComputeFontMetricsTopBottom(paint, flat.writableTopBot());
244 SkASSERT(flat.isTopBotWritten());
245 }
246 }
247 // Will return a cached version when possible.
248 const SkFlatData* getFlatPaintData(const SkPaint& paint);
249 /**
250 * SkBBoxRecord::drawPosTextH gets a flat paint and uses it,
251 * then it calls this, using the extra parameter, to avoid duplication.
252 */
253 void drawPosTextHImpl(const void* text, size_t byteLength,
254 const SkScalar xpos[], SkScalar constY,
255 const SkPaint& paint, const SkFlatData* flatPaintData);
rileya@google.com9f5898d2012-09-11 20:21:44 +0000256
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000257 int addPathToHeap(const SkPath& path); // does not write to ops stream
258
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000259 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000260 // restores to be deferred (e.g., if the MC state is being collapsed and
261 // only written out as needed).
262 void recordConcat(const SkMatrix& matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000263 void recordTranslate(const SkMatrix& matrix);
264 void recordScale(const SkMatrix& matrix);
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000265 size_t recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
266 size_t recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
267 size_t recordClipPath(int pathID, SkRegion::Op op, bool doAA);
268 size_t recordClipRegion(const SkRegion& region, SkRegion::Op op);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000269 void recordSave(SaveFlags flags);
270 void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000271 void recordRestore(bool fillInSkips = true);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000272
rileya@google.com9f5898d2012-09-11 20:21:44 +0000273 // These are set to NULL in our constructor, but may be changed by
274 // subclasses, in which case they will be SkSafeUnref'd in our destructor.
275 SkBBoxHierarchy* fBoundingHierarchy;
276 SkPictureStateTree* fStateTree;
277
robertphillips@google.com801cee12012-10-19 19:06:11 +0000278 // Allocated in the constructor and managed by this class.
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000279 SkBitmapHeap* fBitmapHeap;
robertphillips@google.com801cee12012-10-19 19:06:11 +0000280
281private:
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000282 friend class MatrixClipState; // for access to *Impl methods
283 friend class SkMatrixClipStateMgr; // for access to *Impl methods
284
djsollen@google.com21830d92012-08-07 19:49:41 +0000285 SkChunkFlatController fFlattenableHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000286
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000287 SkPaintDictionary fPaints;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000288
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289 SkPathHeap* fPathHeap; // reference counted
290 SkWriter32 fWriter;
291
reed@android.com09b84a02009-06-26 20:22:26 +0000292 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293 SkTDArray<SkPicture*> fPictureRefs;
294
reed@android.comae814c82009-02-13 14:56:09 +0000295 uint32_t fRecordFlags;
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +0000296 bool fOptsEnabled;
297 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000298
reed@android.com8a1c16f2008-12-17 15:59:43 +0000299 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000300 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000302#ifdef SK_COLLAPSE_MATRIX_CLIP_STATE
303 SkMatrixClipStateMgr fMCMgr;
304#endif
305
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306 typedef SkCanvas INHERITED;
307};
308
309#endif