blob: f8895c950e8ba99904c6083fec477b253e91a51a [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"
13#include "SkPathHeap.h"
14#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070015#include "SkPictureData.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkPictureFlat.h"
17#include "SkTemplates.h"
18#include "SkWriter32.h"
19
rileya@google.com9f5898d2012-09-11 20:21:44 +000020class SkBBoxHierarchy;
commit-bot@chromium.org8016f792014-03-07 15:53:01 +000021class SkPictureStateTree;
rileya@google.com9f5898d2012-09-11 20:21:44 +000022
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000023// These macros help with packing and unpacking a single byte value and
24// a 3 byte value into/out of a uint32_t
25#define MASK_24 0x00FFFFFF
26#define UNPACK_8_24(combined, small, large) \
27 small = (combined >> 24) & 0xFF; \
28 large = combined & MASK_24;
29#define PACK_8_24(small, large) ((small << 24) | large)
30
31
reed@android.com8a1c16f2008-12-17 15:59:43 +000032class SkPictureRecord : public SkCanvas {
33public:
robertphillips0bdbea72014-06-11 11:37:55 -070034 SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 virtual ~SkPictureRecord();
36
reed@google.com2d4297c2011-10-06 13:14:12 +000037 virtual void clear(SkColor) SK_OVERRIDE;
38 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000040 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000041 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000042 virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000043 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000044 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000046 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +000047 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +000048 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +000049 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000051 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000052 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000053 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000055 const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 virtual void drawVertices(VertexMode, int vertexCount,
57 const SkPoint vertices[], const SkPoint texs[],
58 const SkColor colors[], SkXfermode*,
59 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +000060 const SkPaint&) SK_OVERRIDE;
61 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +000062 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
63 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
64 virtual void endCommentGroup() SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000065 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000066
junov@chromium.org3f5ecd62013-01-22 18:01:26 +000067 void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&,
reed@google.com45954262012-12-07 17:14:40 +000068 SkScalar minY, SkScalar maxY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000069
robertphillips9b14f262014-06-04 05:40:44 -070070 const SkTDArray<const SkPicture* >& getPictureRefs() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 return fPictureRefs;
72 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000073
fmalitab7425172014-08-26 07:56:44 -070074 const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const {
75 return fTextBlobRefs;
76 }
77
robertphillips0bdbea72014-06-11 11:37:55 -070078 SkData* opData(bool deepCopy) const {
79 this->validate(fWriter.bytesWritten(), 0);
80
81 if (fWriter.bytesWritten() == 0) {
82 return SkData::NewEmpty();
83 }
84
85 if (deepCopy) {
86 return SkData::NewWithCopy(fWriter.contiguousArray(), fWriter.bytesWritten());
87 }
88
89 return fWriter.snapshotAsData();
90 }
91
robertphillipse26e65e2014-06-12 05:51:22 -070092 const SkPathHeap* pathHeap() const {
93 return fPathHeap.get();
robertphillips0bdbea72014-06-11 11:37:55 -070094 }
95
96 const SkPictureContentInfo& contentInfo() const {
97 return fContentInfo;
98 }
99
junov@chromium.org4866cc02012-06-01 21:23:07 +0000100 void setFlags(uint32_t recordFlags) {
101 fRecordFlags = recordFlags;
102 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103
104 const SkWriter32& writeStream() const {
105 return fWriter;
106 }
107
reed@google.comd86e7ab2012-09-27 20:31:31 +0000108 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000109 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +0000110
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +0000111 void internalOnly_EnableOpts(bool optsEnabled) {
112 fOptsEnabled = optsEnabled;
113 }
114
senorblanco@chromium.org68250c82014-05-06 22:52:55 +0000115protected:
116 void addNoOp();
117
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +0000119 void handleOptimization(int opt);
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000120 size_t recordRestoreOffsetPlaceholder(SkRegion::Op);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000121 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +0000122
reed@google.comffacd3c2012-08-30 15:31:23 +0000123 SkTDArray<int32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000124 int fFirstSavedLayerIndex;
125 enum {
126 kNoSavedLayerIndex = -1
127 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000129 SkTDArray<uint32_t> fCullOffsetStack;
130
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000131 /*
132 * Write the 'drawType' operation and chunk size to the skp. 'size'
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000133 * can potentially be increased if the chunk size needs its own storage
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000134 * location (i.e., it overflows 24 bits).
135 * Returns the start offset of the chunk. This is the location at which
136 * the opcode & size are stored.
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +0000137 * TODO: since we are handing the size into here we could call reserve
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000138 * and then return a pointer to the memory storage. This could decrease
139 * allocation overhead but could lead to more wasted space (the tail
140 * end of blocks could go unused). Possibly add a second addDraw that
141 * operates in this manner.
142 */
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000143 size_t addDraw(DrawType drawType, size_t* size) {
reed@google.com44699382013-10-31 17:28:30 +0000144 size_t offset = fWriter.bytesWritten();
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000145
reed@google.com97af1a62012-08-28 12:19:02 +0000146 this->predrawNotify();
hendrikwafdada22014-08-08 10:44:33 -0700147 fContentInfo.addOperation();
reed@google.com97af1a62012-08-28 12:19:02 +0000148
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000149 SkASSERT(0 != *size);
150 SkASSERT(((uint8_t) drawType) == drawType);
151
152 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
153 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
154 *size += 1;
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000155 fWriter.writeInt(SkToU32(*size));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000156 } else {
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000157 fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000158 }
159
160 return offset;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000161 }
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000162
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 void addInt(int value) {
164 fWriter.writeInt(value);
165 }
166 void addScalar(SkScalar scalar) {
167 fWriter.writeScalar(scalar);
168 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000169
commit-bot@chromium.org8016f792014-03-07 15:53:01 +0000170 // The command at 'offset' in the skp uses the specified bitmap
commit-bot@chromium.org8016f792014-03-07 15:53:01 +0000171 int addBitmap(const SkBitmap& bitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 void addMatrix(const SkMatrix& matrix);
junov@chromium.orgf3b12232013-01-22 17:50:47 +0000173 const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
174 const SkFlatData* addPaintPtr(const SkPaint* paint);
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000175 void addFlatPaint(const SkFlatData* flatPaint);
dandovb3c9d1c2014-08-12 08:34:29 -0700176 void addPatch(const SkPoint cubics[12]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 void addPath(const SkPath& path);
robertphillips9b14f262014-06-04 05:40:44 -0700178 void addPicture(const SkPicture* picture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 void addPoint(const SkPoint& point);
180 void addPoints(const SkPoint pts[], int count);
181 void addRect(const SkRect& rect);
182 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000183 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 void addIRectPtr(const SkIRect* rect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000185 void addRRect(const SkRRect&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 void addRegion(const SkRegion& region);
187 void addText(const void* text, size_t byteLength);
fmalitab7425172014-08-26 07:56:44 -0700188 void addTextBlob(const SkTextBlob* blob);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189
junov@chromium.org4866cc02012-06-01 21:23:07 +0000190 int find(const SkBitmap& bitmap);
191
robertphillips6162af82014-08-11 09:50:11 -0700192protected:
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000193 void validate(size_t initialOffset, size_t size) const {
reed@google.com44699382013-10-31 17:28:30 +0000194 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000195 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196
reed3716fd02014-09-21 09:39:55 -0700197 virtual SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) SK_OVERRIDE;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000198 const void* onPeekPixels(SkImageInfo*, size_t*) SK_OVERRIDE {
199 return NULL;
200 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000201
Florin Malita5f6102d2014-06-30 10:13:28 -0400202 virtual void willSave() SK_OVERRIDE;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000203 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
204 virtual void willRestore() SK_OVERRIDE;
205
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000206 virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
207 virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
208
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000209 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000210 virtual void onPushCull(const SkRect&) SK_OVERRIDE;
211 virtual void onPopCull() SK_OVERRIDE;
reed@google.com76f10a32014-02-05 15:32:21 +0000212
reed@google.come0d9ce82014-04-23 04:00:17 +0000213 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
214 const SkPaint&) SK_OVERRIDE;
215 virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
216 const SkPaint&) SK_OVERRIDE;
217 virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
218 SkScalar constY, const SkPaint&) SK_OVERRIDE;
219 virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
220 const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE;
fmalitab7425172014-08-26 07:56:44 -0700221 virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
222 const SkPaint& paint) SK_OVERRIDE;
dandovb3c9d1c2014-08-12 08:34:29 -0700223
224 virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
225 const SkPoint texCoords[4], SkXfermode* xmode,
226 const SkPaint& paint) SK_OVERRIDE;
reed@google.come0d9ce82014-04-23 04:00:17 +0000227
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000228 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
229 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
230 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
231 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
232
reedd5fa1a42014-08-09 11:08:05 -0700233 virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
robertphillips9b14f262014-06-04 05:40:44 -0700234
commit-bot@chromium.orgcf7be952013-08-22 17:19:52 +0000235 // Return fontmetrics.fTop,fBottom in topbot[0,1], after they have been
236 // tweaked by paint.computeFastBounds().
237 static void ComputeFontMetricsTopBottom(const SkPaint& paint, SkScalar topbot[2]);
238
239 // Make sure that flat has fTopBot written.
240 static void WriteTopBot(const SkPaint& paint, const SkFlatData& flat) {
241 if (!flat.isTopBotWritten()) {
242 ComputeFontMetricsTopBottom(paint, flat.writableTopBot());
243 SkASSERT(flat.isTopBotWritten());
244 }
245 }
246 // Will return a cached version when possible.
247 const SkFlatData* getFlatPaintData(const SkPaint& paint);
248 /**
249 * SkBBoxRecord::drawPosTextH gets a flat paint and uses it,
250 * then it calls this, using the extra parameter, to avoid duplication.
251 */
252 void drawPosTextHImpl(const void* text, size_t byteLength,
253 const SkScalar xpos[], SkScalar constY,
254 const SkPaint& paint, const SkFlatData* flatPaintData);
rileya@google.com9f5898d2012-09-11 20:21:44 +0000255
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000256 int addPathToHeap(const SkPath& path); // does not write to ops stream
257
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000258 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000259 // restores to be deferred (e.g., if the MC state is being collapsed and
260 // only written out as needed).
261 void recordConcat(const SkMatrix& matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000262 void recordTranslate(const SkMatrix& matrix);
263 void recordScale(const SkMatrix& matrix);
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000264 size_t recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
265 size_t recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
266 size_t recordClipPath(int pathID, SkRegion::Op op, bool doAA);
267 size_t recordClipRegion(const SkRegion& region, SkRegion::Op op);
Florin Malita5f6102d2014-06-30 10:13:28 -0400268 void recordSave();
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000269 void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000270 void recordRestore(bool fillInSkips = true);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000271
rileya@google.com9f5898d2012-09-11 20:21:44 +0000272 // These are set to NULL in our constructor, but may be changed by
273 // subclasses, in which case they will be SkSafeUnref'd in our destructor.
274 SkBBoxHierarchy* fBoundingHierarchy;
275 SkPictureStateTree* fStateTree;
276
robertphillips@google.com801cee12012-10-19 19:06:11 +0000277 // Allocated in the constructor and managed by this class.
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000278 SkBitmapHeap* fBitmapHeap;
robertphillips@google.com801cee12012-10-19 19:06:11 +0000279
280private:
robertphillips0bdbea72014-06-11 11:37:55 -0700281 SkPictureContentInfo fContentInfo;
282 SkAutoTUnref<SkPathHeap> fPathHeap;
283
djsollen@google.com21830d92012-08-07 19:49:41 +0000284 SkChunkFlatController fFlattenableHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000285
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000286 SkPaintDictionary fPaints;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000287
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288 SkWriter32 fWriter;
289
reed@android.com09b84a02009-06-26 20:22:26 +0000290 // we ref each item in these arrays
fmalitab7425172014-08-26 07:56:44 -0700291 SkTDArray<const SkPicture*> fPictureRefs;
292 SkTDArray<const SkTextBlob*> fTextBlobRefs;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293
reed@android.comae814c82009-02-13 14:56:09 +0000294 uint32_t fRecordFlags;
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +0000295 bool fOptsEnabled;
296 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000297
robertphillips61426092014-07-10 09:35:12 -0700298 friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000299 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000300
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301 typedef SkCanvas INHERITED;
302};
303
304#endif