blob: 0df120b31a121b54f5681e6bed5dc3e75fc5cab3 [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:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000036 SkPictureRecord(uint32_t recordFlags, SkBaseDevice*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 virtual ~SkPictureRecord();
38
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000039 virtual SkBaseDevice* setDevice(SkBaseDevice* device) SK_OVERRIDE;
junov@chromium.org4e6dfa52012-07-16 14:04:59 +000040
reed@google.com2d4297c2011-10-06 13:14:12 +000041 virtual int save(SaveFlags) SK_OVERRIDE;
42 virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
43 virtual void restore() SK_OVERRIDE;
44 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
45 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
46 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
47 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
48 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
49 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
reed@google.com071eef92011-10-12 11:52:53 +000050 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000051 virtual bool clipRRect(const SkRRect&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com071eef92011-10-12 11:52:53 +000052 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000053 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
54 virtual void clear(SkColor) SK_OVERRIDE;
55 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000057 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000058 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000059 virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000060 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000061 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000063 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +000064 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +000065 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +000066 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000068 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000069 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000070 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000072 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000073 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000074 SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000075 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000076 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 virtual void drawPosTextH(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000078 const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000079 virtual void drawTextOnPath(const void* text, size_t byteLength,
80 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +000081 const SkPaint&) SK_OVERRIDE;
82 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 virtual void drawVertices(VertexMode, int vertexCount,
84 const SkPoint vertices[], const SkPoint texs[],
85 const SkColor colors[], SkXfermode*,
86 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +000087 const SkPaint&) SK_OVERRIDE;
88 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +000089 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
90 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
91 virtual void endCommentGroup() SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000092 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000093
junov@chromium.org3f5ecd62013-01-22 18:01:26 +000094 void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&,
reed@google.com45954262012-12-07 17:14:40 +000095 SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 const SkTDArray<SkPicture* >& getPictureRefs() const {
98 return fPictureRefs;
99 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000100
junov@chromium.org4866cc02012-06-01 21:23:07 +0000101 void setFlags(uint32_t recordFlags) {
102 fRecordFlags = recordFlags;
103 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104
105 const SkWriter32& writeStream() const {
106 return fWriter;
107 }
108
reed@google.comd86e7ab2012-09-27 20:31:31 +0000109 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000110 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +0000111
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +0000113 void handleOptimization(int opt);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000114 int recordRestoreOffsetPlaceholder(SkRegion::Op);
115 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +0000116
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000117#ifndef SK_COLLAPSE_MATRIX_CLIP_STATE
reed@google.comffacd3c2012-08-30 15:31:23 +0000118 SkTDArray<int32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000119 int fFirstSavedLayerIndex;
120 enum {
121 kNoSavedLayerIndex = -1
122 };
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000123#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000125 /*
126 * Write the 'drawType' operation and chunk size to the skp. 'size'
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000127 * can potentially be increased if the chunk size needs its own storage
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000128 * location (i.e., it overflows 24 bits).
129 * Returns the start offset of the chunk. This is the location at which
130 * the opcode & size are stored.
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000131 * TODO: since we are handing the size into here we could call reserve
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000132 * and then return a pointer to the memory storage. This could decrease
133 * allocation overhead but could lead to more wasted space (the tail
134 * end of blocks could go unused). Possibly add a second addDraw that
135 * operates in this manner.
136 */
robertphillips@google.com8b169312013-10-15 17:47:36 +0000137 size_t addDraw(DrawType drawType, uint32_t* size) {
reed@google.com44699382013-10-31 17:28:30 +0000138 size_t offset = fWriter.bytesWritten();
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000139
reed@google.com97af1a62012-08-28 12:19:02 +0000140 this->predrawNotify();
141
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000142 #ifdef SK_DEBUG_TRACE
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 SkDebugf("add %s\n", DrawTypeToString(drawType));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000144 #endif
145
146 SkASSERT(0 != *size);
147 SkASSERT(((uint8_t) drawType) == drawType);
148
149 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
150 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
151 *size += 1;
152 fWriter.writeInt(*size);
153 } else {
154 fWriter.writeInt(PACK_8_24(drawType, *size));
155 }
156
157 return offset;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000158 }
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000159
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 void addInt(int value) {
161 fWriter.writeInt(value);
162 }
163 void addScalar(SkScalar scalar) {
164 fWriter.writeScalar(scalar);
165 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000166
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 void addBitmap(const SkBitmap& bitmap);
168 void addMatrix(const SkMatrix& matrix);
junov@chromium.orgf3b12232013-01-22 17:50:47 +0000169 const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
170 const SkFlatData* addPaintPtr(const SkPaint* paint);
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000171 void addFlatPaint(const SkFlatData* flatPaint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 void addPath(const SkPath& path);
173 void addPicture(SkPicture& picture);
174 void addPoint(const SkPoint& point);
175 void addPoints(const SkPoint pts[], int count);
176 void addRect(const SkRect& rect);
177 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000178 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 void addIRectPtr(const SkIRect* rect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000180 void addRRect(const SkRRect&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 void addRegion(const SkRegion& region);
182 void addText(const void* text, size_t byteLength);
183
junov@chromium.org4866cc02012-06-01 21:23:07 +0000184 int find(const SkBitmap& bitmap);
185
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186#ifdef SK_DEBUG_DUMP
187public:
188 void dumpMatrices();
189 void dumpPaints();
190#endif
191
192#ifdef SK_DEBUG_SIZE
193public:
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000194 size_t size() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 int bitmaps(size_t* size) const;
196 int matrices(size_t* size) const;
197 int paints(size_t* size) const;
198 int paths(size_t* size) const;
199 int regions(size_t* size) const;
200 size_t streamlen() const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000201
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 size_t fPointBytes, fRectBytes, fTextBytes;
203 int fPointWrites, fRectWrites, fTextWrites;
204#endif
205
206#ifdef SK_DEBUG_VALIDATE
207public:
robertphillips@google.com8b169312013-10-15 17:47:36 +0000208 void validate(size_t initialOffset, uint32_t size) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000209private:
210 void validateBitmaps() const;
211 void validateMatrices() const;
212 void validatePaints() const;
213 void validatePaths() const;
214 void validateRegions() const;
215#else
216public:
robertphillips@google.com8b169312013-10-15 17:47:36 +0000217 void validate(size_t initialOffset, uint32_t size) const {
reed@google.com44699382013-10-31 17:28:30 +0000218 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000219 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220#endif
221
rileya@google.com9f5898d2012-09-11 20:21:44 +0000222protected:
reed@google.com76f10a32014-02-05 15:32:21 +0000223 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE;
224
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000225 // Return fontmetrics.fTop,fBottom in topbot[0,1], after they have been
226 // tweaked by paint.computeFastBounds().
227 static void ComputeFontMetricsTopBottom(const SkPaint& paint, SkScalar topbot[2]);
228
229 // Make sure that flat has fTopBot written.
230 static void WriteTopBot(const SkPaint& paint, const SkFlatData& flat) {
231 if (!flat.isTopBotWritten()) {
232 ComputeFontMetricsTopBottom(paint, flat.writableTopBot());
233 SkASSERT(flat.isTopBotWritten());
234 }
235 }
236 // Will return a cached version when possible.
237 const SkFlatData* getFlatPaintData(const SkPaint& paint);
238 /**
239 * SkBBoxRecord::drawPosTextH gets a flat paint and uses it,
240 * then it calls this, using the extra parameter, to avoid duplication.
241 */
242 void drawPosTextHImpl(const void* text, size_t byteLength,
243 const SkScalar xpos[], SkScalar constY,
244 const SkPaint& paint, const SkFlatData* flatPaintData);
rileya@google.com9f5898d2012-09-11 20:21:44 +0000245
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000246 int addPathToHeap(const SkPath& path); // does not write to ops stream
247
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000248 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000249 // restores to be deferred (e.g., if the MC state is being collapsed and
250 // only written out as needed).
251 void recordConcat(const SkMatrix& matrix);
252 int recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
253 int recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
254 int recordClipPath(int pathID, SkRegion::Op op, bool doAA);
255 int recordClipRegion(const SkRegion& region, SkRegion::Op op);
256 void recordSave(SaveFlags flags);
257 void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000258 void recordRestore(bool fillInSkips = true);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000259
rileya@google.com9f5898d2012-09-11 20:21:44 +0000260 // These are set to NULL in our constructor, but may be changed by
261 // subclasses, in which case they will be SkSafeUnref'd in our destructor.
262 SkBBoxHierarchy* fBoundingHierarchy;
263 SkPictureStateTree* fStateTree;
264
robertphillips@google.com801cee12012-10-19 19:06:11 +0000265 // Allocated in the constructor and managed by this class.
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000266 SkBitmapHeap* fBitmapHeap;
robertphillips@google.com801cee12012-10-19 19:06:11 +0000267
268private:
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000269 friend class MatrixClipState; // for access to *Impl methods
270 friend class SkMatrixClipStateMgr; // for access to *Impl methods
271
djsollen@google.com21830d92012-08-07 19:49:41 +0000272 SkChunkFlatController fFlattenableHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000273
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000274 SkPaintDictionary fPaints;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000275
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 SkPathHeap* fPathHeap; // reference counted
277 SkWriter32 fWriter;
278
reed@android.com09b84a02009-06-26 20:22:26 +0000279 // we ref each item in these arrays
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 SkTDArray<SkPicture*> fPictureRefs;
281
reed@android.comae814c82009-02-13 14:56:09 +0000282 uint32_t fRecordFlags;
junov@chromium.org4e6dfa52012-07-16 14:04:59 +0000283 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000284
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 friend class SkPicturePlayback;
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000286 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000288#ifdef SK_COLLAPSE_MATRIX_CLIP_STATE
289 SkMatrixClipStateMgr fMCMgr;
290#endif
291
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 typedef SkCanvas INHERITED;
293};
294
295#endif