blob: ca2aecd6780501f6bf70986ca3e8238d71d9ed1a [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
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000020// These macros help with packing and unpacking a single byte value and
21// a 3 byte value into/out of a uint32_t
22#define MASK_24 0x00FFFFFF
23#define UNPACK_8_24(combined, small, large) \
24 small = (combined >> 24) & 0xFF; \
25 large = combined & MASK_24;
26#define PACK_8_24(small, large) ((small << 24) | large)
27
28
reed@android.com8a1c16f2008-12-17 15:59:43 +000029class SkPictureRecord : public SkCanvas {
30public:
robertphillips0bdbea72014-06-11 11:37:55 -070031 SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 virtual ~SkPictureRecord();
33
reed@google.com2d4297c2011-10-06 13:14:12 +000034 virtual void clear(SkColor) SK_OVERRIDE;
35 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000037 const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000038 virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000039 virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000040 virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
bsalomon@google.com7ce564c2013-10-22 16:54:15 +000041 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000043 const SkPaint*) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +000044 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
skia.committer@gmail.com74758112013-08-17 07:01:54 +000045 const SkRect& dst, const SkPaint* paint,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +000046 DrawBitmapRectFlags flags) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
reed@google.com2d4297c2011-10-06 13:14:12 +000048 const SkPaint*) SK_OVERRIDE;
reed@google.comf0b5e112011-09-07 11:57:34 +000049 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
reed@google.com2d4297c2011-10-06 13:14:12 +000050 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 virtual void drawSprite(const SkBitmap&, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000052 const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 virtual void drawVertices(VertexMode, int vertexCount,
54 const SkPoint vertices[], const SkPoint texs[],
55 const SkColor colors[], SkXfermode*,
56 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +000057 const SkPaint&) SK_OVERRIDE;
58 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +000059 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
60 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
61 virtual void endCommentGroup() SK_OVERRIDE;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +000062 virtual bool isDrawingToLayer() const SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000063
robertphillips9b14f262014-06-04 05:40:44 -070064 const SkTDArray<const SkPicture* >& getPictureRefs() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 return fPictureRefs;
66 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000067
fmalitab7425172014-08-26 07:56:44 -070068 const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const {
69 return fTextBlobRefs;
70 }
71
robertphillips0bdbea72014-06-11 11:37:55 -070072 SkData* opData(bool deepCopy) const {
73 this->validate(fWriter.bytesWritten(), 0);
74
75 if (fWriter.bytesWritten() == 0) {
76 return SkData::NewEmpty();
77 }
78
79 if (deepCopy) {
80 return SkData::NewWithCopy(fWriter.contiguousArray(), fWriter.bytesWritten());
81 }
82
83 return fWriter.snapshotAsData();
84 }
85
robertphillipse26e65e2014-06-12 05:51:22 -070086 const SkPathHeap* pathHeap() const {
87 return fPathHeap.get();
robertphillips0bdbea72014-06-11 11:37:55 -070088 }
89
90 const SkPictureContentInfo& contentInfo() const {
91 return fContentInfo;
92 }
93
junov@chromium.org4866cc02012-06-01 21:23:07 +000094 void setFlags(uint32_t recordFlags) {
95 fRecordFlags = recordFlags;
96 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000097
98 const SkWriter32& writeStream() const {
99 return fWriter;
100 }
101
reed@google.comd86e7ab2012-09-27 20:31:31 +0000102 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +0000103 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +0000104
senorblanco@chromium.org68250c82014-05-06 22:52:55 +0000105protected:
106 void addNoOp();
107
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +0000109 void handleOptimization(int opt);
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000110 size_t recordRestoreOffsetPlaceholder(SkRegion::Op);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000111 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +0000112
reed@google.comffacd3c2012-08-30 15:31:23 +0000113 SkTDArray<int32_t> fRestoreOffsetStack;
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000114 int fFirstSavedLayerIndex;
115 enum {
116 kNoSavedLayerIndex = -1
117 };
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 */
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000133 size_t addDraw(DrawType drawType, size_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();
hendrikwafdada22014-08-08 10:44:33 -0700137 fContentInfo.addOperation();
reed@google.com97af1a62012-08-28 12:19:02 +0000138
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000139 SkASSERT(0 != *size);
140 SkASSERT(((uint8_t) drawType) == drawType);
141
142 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
143 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
144 *size += 1;
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000145 fWriter.writeInt(SkToU32(*size));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000146 } else {
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000147 fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000148 }
149
150 return offset;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000151 }
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 void addInt(int value) {
154 fWriter.writeInt(value);
155 }
156 void addScalar(SkScalar scalar) {
157 fWriter.writeScalar(scalar);
158 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000159
commit-bot@chromium.org8016f792014-03-07 15:53:01 +0000160 // The command at 'offset' in the skp uses the specified bitmap
commit-bot@chromium.org8016f792014-03-07 15:53:01 +0000161 int addBitmap(const SkBitmap& bitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 void addMatrix(const SkMatrix& matrix);
mtklein46616af2014-09-30 14:47:10 -0700163 void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
164 void addPaintPtr(const SkPaint* paint);
dandovb3c9d1c2014-08-12 08:34:29 -0700165 void addPatch(const SkPoint cubics[12]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 void addPath(const SkPath& path);
robertphillips9b14f262014-06-04 05:40:44 -0700167 void addPicture(const SkPicture* picture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 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);
fmalitab7425172014-08-26 07:56:44 -0700177 void addTextBlob(const SkTextBlob* blob);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178
junov@chromium.org4866cc02012-06-01 21:23:07 +0000179 int find(const SkBitmap& bitmap);
180
robertphillips6162af82014-08-11 09:50:11 -0700181protected:
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000182 void validate(size_t initialOffset, size_t size) const {
reed@google.com44699382013-10-31 17:28:30 +0000183 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000184 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185
reed4a8126e2014-09-22 07:29:03 -0700186 virtual SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) SK_OVERRIDE;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000187 const void* onPeekPixels(SkImageInfo*, size_t*) SK_OVERRIDE {
188 return NULL;
189 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000190
Florin Malita5f6102d2014-06-30 10:13:28 -0400191 virtual void willSave() SK_OVERRIDE;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000192 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
193 virtual void willRestore() SK_OVERRIDE;
194
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000195 virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
196 virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
197
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000198 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000199 virtual void onPushCull(const SkRect&) SK_OVERRIDE;
200 virtual void onPopCull() SK_OVERRIDE;
reed@google.com76f10a32014-02-05 15:32:21 +0000201
reed@google.come0d9ce82014-04-23 04:00:17 +0000202 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
203 const SkPaint&) SK_OVERRIDE;
204 virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
205 const SkPaint&) SK_OVERRIDE;
206 virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
207 SkScalar constY, const SkPaint&) SK_OVERRIDE;
208 virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
209 const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE;
fmalitab7425172014-08-26 07:56:44 -0700210 virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
211 const SkPaint& paint) SK_OVERRIDE;
mtklein46616af2014-09-30 14:47:10 -0700212
dandovb3c9d1c2014-08-12 08:34:29 -0700213 virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
214 const SkPoint texCoords[4], SkXfermode* xmode,
215 const SkPaint& paint) SK_OVERRIDE;
reed@google.come0d9ce82014-04-23 04:00:17 +0000216
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000217 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
218 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
219 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
220 virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
221
reedd5fa1a42014-08-09 11:08:05 -0700222 virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
robertphillips9b14f262014-06-04 05:40:44 -0700223
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000224 int addPathToHeap(const SkPath& path); // does not write to ops stream
225
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000226 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000227 // restores to be deferred (e.g., if the MC state is being collapsed and
228 // only written out as needed).
229 void recordConcat(const SkMatrix& matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000230 void recordTranslate(const SkMatrix& matrix);
231 void recordScale(const SkMatrix& matrix);
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000232 size_t recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
233 size_t recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
234 size_t recordClipPath(int pathID, SkRegion::Op op, bool doAA);
235 size_t recordClipRegion(const SkRegion& region, SkRegion::Op op);
Florin Malita5f6102d2014-06-30 10:13:28 -0400236 void recordSave();
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000237 void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000238 void recordRestore(bool fillInSkips = true);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000239
robertphillips@google.com801cee12012-10-19 19:06:11 +0000240 // Allocated in the constructor and managed by this class.
djsollen@google.comc9ab9872012-08-29 18:52:07 +0000241 SkBitmapHeap* fBitmapHeap;
robertphillips@google.com801cee12012-10-19 19:06:11 +0000242
243private:
robertphillips0bdbea72014-06-11 11:37:55 -0700244 SkPictureContentInfo fContentInfo;
245 SkAutoTUnref<SkPathHeap> fPathHeap;
246
djsollen@google.com21830d92012-08-07 19:49:41 +0000247 SkChunkFlatController fFlattenableHeap;
junov@chromium.org4866cc02012-06-01 21:23:07 +0000248
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000249 SkPaintDictionary fPaints;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000250
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 SkWriter32 fWriter;
252
reed@android.com09b84a02009-06-26 20:22:26 +0000253 // we ref each item in these arrays
fmalitab7425172014-08-26 07:56:44 -0700254 SkTDArray<const SkPicture*> fPictureRefs;
255 SkTDArray<const SkTextBlob*> fTextBlobRefs;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256
reed@android.comae814c82009-02-13 14:56:09 +0000257 uint32_t fRecordFlags;
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +0000258 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000259
robertphillips61426092014-07-10 09:35:12 -0700260 friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor
junov@chromium.org1cc8f6f2012-02-22 21:00:42 +0000261 friend class SkPictureTester; // for unit testing
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 typedef SkCanvas INHERITED;
264};
265
266#endif