blob: ad2dbdfd24a3d485bfd4940794d89eafe9f841f3 [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"
Brian Osman37886ce2018-03-09 13:40:31 -050012#include "SkCanvasVirtualEnforcer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkFlattenable.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070015#include "SkPictureData.h"
bungemanf3c15b72015-08-19 11:56:48 -070016#include "SkTArray.h"
17#include "SkTDArray.h"
mtkleinc2e29772015-10-30 05:24:58 -070018#include "SkTHash.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040019#include "SkTo.h"
Brian Salomon199fb872017-02-06 09:41:10 -050020#include "SkVertices.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#include "SkWriter32.h"
22
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
Brian Osman37886ce2018-03-09 13:40:31 -050032class SkPictureRecord : public SkCanvasVirtualEnforcer<SkCanvas> {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033public:
robertphillips0bdbea72014-06-11 11:37:55 -070034 SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000035
Florin Malita8fd15d82018-05-15 14:57:12 -040036 const SkTArray<sk_sp<const SkPicture>>& getPictures() const {
37 return fPictures;
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000039
Florin Malita8fd15d82018-05-15 14:57:12 -040040 const SkTArray<sk_sp<SkDrawable>>& getDrawables() const {
41 return fDrawables;
msarett95416f42016-04-27 13:51:20 -070042 }
43
Florin Malita8fd15d82018-05-15 14:57:12 -040044 const SkTArray<sk_sp<const SkTextBlob>>& getTextBlobs() const {
45 return fTextBlobs;
fmalitab7425172014-08-26 07:56:44 -070046 }
47
Florin Malita8fd15d82018-05-15 14:57:12 -040048 const SkTArray<sk_sp<const SkVertices>>& getVertices() const {
49 return fVertices;
Mike Reede88a1cb2017-03-17 09:50:46 -040050 }
51
Florin Malita8fd15d82018-05-15 14:57:12 -040052 const SkTArray<sk_sp<const SkImage>>& getImages() const {
53 return fImages;
reed871872f2015-06-22 12:48:26 -070054 }
bungemanf3c15b72015-08-19 11:56:48 -070055
mtklein4d9ff622016-04-29 14:45:36 -070056 sk_sp<SkData> opData() const {
robertphillips0bdbea72014-06-11 11:37:55 -070057 this->validate(fWriter.bytesWritten(), 0);
58
59 if (fWriter.bytesWritten() == 0) {
reedfde05112016-03-11 13:02:28 -080060 return SkData::MakeEmpty();
robertphillips0bdbea72014-06-11 11:37:55 -070061 }
robertphillips0bdbea72014-06-11 11:37:55 -070062 return fWriter.snapshotAsData();
63 }
64
junov@chromium.org4866cc02012-06-01 21:23:07 +000065 void setFlags(uint32_t recordFlags) {
66 fRecordFlags = recordFlags;
67 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000068
69 const SkWriter32& writeStream() const {
70 return fWriter;
71 }
72
reed@google.comd86e7ab2012-09-27 20:31:31 +000073 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +000074 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +000075
senorblanco@chromium.org68250c82014-05-06 22:52:55 +000076protected:
77 void addNoOp();
78
reed@android.com8a1c16f2008-12-17 15:59:43 +000079private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +000080 void handleOptimization(int opt);
Mike Reedc1f77742016-12-09 09:00:50 -050081 size_t recordRestoreOffsetPlaceholder(SkClipOp);
robertphillips@google.com5a63f242014-02-04 20:07:50 +000082 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +000083
reed@google.comffacd3c2012-08-30 15:31:23 +000084 SkTDArray<int32_t> fRestoreOffsetStack;
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +000086 SkTDArray<uint32_t> fCullOffsetStack;
87
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000088 /*
89 * Write the 'drawType' operation and chunk size to the skp. 'size'
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +000090 * can potentially be increased if the chunk size needs its own storage
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000091 * location (i.e., it overflows 24 bits).
92 * Returns the start offset of the chunk. This is the location at which
93 * the opcode & size are stored.
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +000094 * TODO: since we are handing the size into here we could call reserve
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000095 * and then return a pointer to the memory storage. This could decrease
96 * allocation overhead but could lead to more wasted space (the tail
97 * end of blocks could go unused). Possibly add a second addDraw that
98 * operates in this manner.
99 */
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000100 size_t addDraw(DrawType drawType, size_t* size) {
reed@google.com44699382013-10-31 17:28:30 +0000101 size_t offset = fWriter.bytesWritten();
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000102
reed@google.com97af1a62012-08-28 12:19:02 +0000103 this->predrawNotify();
104
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000105 SkASSERT(0 != *size);
106 SkASSERT(((uint8_t) drawType) == drawType);
107
108 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
109 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
110 *size += 1;
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000111 fWriter.writeInt(SkToU32(*size));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000112 } else {
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000113 fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000114 }
115
116 return offset;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000117 }
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000118
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 void addInt(int value) {
120 fWriter.writeInt(value);
121 }
122 void addScalar(SkScalar scalar) {
123 fWriter.writeScalar(scalar);
124 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000125
reed871872f2015-06-22 12:48:26 -0700126 void addImage(const SkImage*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 void addMatrix(const SkMatrix& matrix);
mtklein46616af2014-09-30 14:47:10 -0700128 void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
129 void addPaintPtr(const SkPaint* paint);
dandovb3c9d1c2014-08-12 08:34:29 -0700130 void addPatch(const SkPoint cubics[12]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 void addPath(const SkPath& path);
robertphillips9b14f262014-06-04 05:40:44 -0700132 void addPicture(const SkPicture* picture);
msarett95416f42016-04-27 13:51:20 -0700133 void addDrawable(SkDrawable* picture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 void addPoint(const SkPoint& point);
135 void addPoints(const SkPoint pts[], int count);
136 void addRect(const SkRect& rect);
137 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000138 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 void addIRectPtr(const SkIRect* rect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000140 void addRRect(const SkRRect&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 void addRegion(const SkRegion& region);
142 void addText(const void* text, size_t byteLength);
fmalitab7425172014-08-26 07:56:44 -0700143 void addTextBlob(const SkTextBlob* blob);
Mike Reede88a1cb2017-03-17 09:50:46 -0400144 void addVertices(const SkVertices*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145
junov@chromium.org4866cc02012-06-01 21:23:07 +0000146 int find(const SkBitmap& bitmap);
147
robertphillips6162af82014-08-11 09:50:11 -0700148protected:
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000149 void validate(size_t initialOffset, size_t size) const {
reed@google.com44699382013-10-31 17:28:30 +0000150 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000151 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152
reede8f30622016-03-23 18:59:25 -0700153 sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
reed884e97c2015-05-26 11:31:54 -0700154 bool onPeekPixels(SkPixmap*) override { return false; }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000155
Mike Klein7cc49d62017-08-14 10:39:28 -0400156 void onFlush() override;
157
mtklein36352bf2015-03-25 18:17:31 -0700158 void willSave() override;
reed4960eee2015-12-18 07:09:18 -0800159 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
mtklein36352bf2015-03-25 18:17:31 -0700160 void willRestore() override;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000161
mtklein36352bf2015-03-25 18:17:31 -0700162 void didConcat(const SkMatrix&) override;
163 void didSetMatrix(const SkMatrix&) override;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000164
mtklein36352bf2015-03-25 18:17:31 -0700165 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
reed@google.com76f10a32014-02-05 15:32:21 +0000166
reed3dfd1332015-06-25 14:26:11 -0700167 void onDrawText(const void* text, size_t, SkScalar x, SkScalar y, const SkPaint&) override;
168 void onDrawPosText(const void* text, size_t, const SkPoint pos[], const SkPaint&) override;
169 void onDrawPosTextH(const void* text, size_t, const SkScalar xpos[], SkScalar constY,
170 const SkPaint&) override;
171 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700172 const SkMatrix* matrix, const SkPaint&) override;
reed45561a02016-07-07 12:47:17 -0700173 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
174 const SkRect* cull, const SkPaint&) override;
reed3dfd1332015-06-25 14:26:11 -0700175 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700176 const SkPaint& paint) override;
mtklein46616af2014-09-30 14:47:10 -0700177
reed3dfd1332015-06-25 14:26:11 -0700178 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400179 const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
reed71c3c762015-06-24 10:29:17 -0700180 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
Mike Reedfaba3712016-11-03 14:45:31 -0400181 SkBlendMode, const SkRect*, const SkPaint*) override;
reed@google.come0d9ce82014-04-23 04:00:17 +0000182
mtklein36352bf2015-03-25 18:17:31 -0700183 void onDrawPaint(const SkPaint&) override;
184 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
185 void onDrawRect(const SkRect&, const SkPaint&) override;
msarett44df6512016-08-25 13:54:30 -0700186 void onDrawRegion(const SkRegion&, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700187 void onDrawOval(const SkRect&, const SkPaint&) override;
bsalomonac3aa242016-08-19 11:25:19 -0700188 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700189 void onDrawRRect(const SkRRect&, const SkPaint&) override;
190 void onDrawPath(const SkPath&, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700191 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800192 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700193 const SkPaint*, SrcRectConstraint) override;
reed4c21dc52015-06-25 12:32:03 -0700194 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
195 const SkPaint*) override;
msarett16882062016-08-16 09:31:08 -0700196 void onDrawImageLattice(const SkImage*, const SkCanvas::Lattice& lattice, const SkRect& dst,
197 const SkPaint*) override;
Jim Van Verth4123d0f2017-05-22 12:02:21 -0400198 void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
Mike Reede88a1cb2017-03-17 09:50:46 -0400199 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
reed41af9662015-01-05 07:49:08 -0800200
Mike Reedc1f77742016-12-09 09:00:50 -0500201 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
202 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
203 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
204 void onClipRegion(const SkRegion&, SkClipOp) override;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000205
mtklein36352bf2015-03-25 18:17:31 -0700206 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
vjiaoblack95302da2016-07-21 10:25:54 -0700207
msarett95416f42016-04-27 13:51:20 -0700208 void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
reedf70b5312016-03-04 16:36:20 -0800209 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
robertphillips9b14f262014-06-04 05:40:44 -0700210
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000211 int addPathToHeap(const SkPath& path); // does not write to ops stream
212
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000213 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000214 // restores to be deferred (e.g., if the MC state is being collapsed and
215 // only written out as needed).
216 void recordConcat(const SkMatrix& matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000217 void recordTranslate(const SkMatrix& matrix);
218 void recordScale(const SkMatrix& matrix);
Mike Reedc1f77742016-12-09 09:00:50 -0500219 size_t recordClipRect(const SkRect& rect, SkClipOp op, bool doAA);
220 size_t recordClipRRect(const SkRRect& rrect, SkClipOp op, bool doAA);
221 size_t recordClipPath(int pathID, SkClipOp op, bool doAA);
222 size_t recordClipRegion(const SkRegion& region, SkClipOp op);
Florin Malita5f6102d2014-06-30 10:13:28 -0400223 void recordSave();
reed4960eee2015-12-18 07:09:18 -0800224 void recordSaveLayer(const SaveLayerRec&);
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000225 void recordRestore(bool fillInSkips = true);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000226
reeda9ca05c2016-08-11 03:55:15 -0700227 // SHOULD NEVER BE CALLED
228 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override {
Ben Wagner7ca9a742017-08-17 14:05:04 -0400229 SK_ABORT("not reached");
reeda9ca05c2016-08-11 03:55:15 -0700230 }
231 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
232 SrcRectConstraint) override {
Ben Wagner7ca9a742017-08-17 14:05:04 -0400233 SK_ABORT("not reached");
reeda9ca05c2016-08-11 03:55:15 -0700234 }
235 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
236 const SkPaint*) override {
Ben Wagner7ca9a742017-08-17 14:05:04 -0400237 SK_ABORT("not reached");
reeda9ca05c2016-08-11 03:55:15 -0700238 }
msarett16882062016-08-16 09:31:08 -0700239 void onDrawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice& lattice, const SkRect& dst,
240 const SkPaint*) override {
Ben Wagner7ca9a742017-08-17 14:05:04 -0400241 SK_ABORT("not reached");
msarett16882062016-08-16 09:31:08 -0700242 }
reeda9ca05c2016-08-11 03:55:15 -0700243
robertphillips@google.com801cee12012-10-19 19:06:11 +0000244private:
mtklein71a23632014-11-12 10:24:55 -0800245 SkTArray<SkPaint> fPaints;
mtkleinc2e29772015-10-30 05:24:58 -0700246
247 struct PathHash {
248 uint32_t operator()(const SkPath& p) { return p.getGenerationID(); }
249 };
250 SkTHashMap<SkPath, int, PathHash> fPaths;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000251
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 SkWriter32 fWriter;
253
Florin Malita8fd15d82018-05-15 14:57:12 -0400254 SkTArray<sk_sp<const SkImage>> fImages;
255 SkTArray<sk_sp<const SkPicture>> fPictures;
256 SkTArray<sk_sp<SkDrawable>> fDrawables;
257 SkTArray<sk_sp<const SkTextBlob>> fTextBlobs;
258 SkTArray<sk_sp<const SkVertices>> fVertices;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259
reed@android.comae814c82009-02-13 14:56:09 +0000260 uint32_t fRecordFlags;
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +0000261 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000262
robertphillips61426092014-07-10 09:35:12 -0700263 friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264
Brian Osman37886ce2018-03-09 13:40:31 -0500265 typedef SkCanvasVirtualEnforcer<SkCanvas> INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266};
vjiaoblack5bfee982016-07-22 10:04:15 -0700267
268#endif