blob: dcdfebe984d757b0c1dff8ecd228be52c7311367 [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070014#include "SkPictureData.h"
bungemanf3c15b72015-08-19 11:56:48 -070015#include "SkTArray.h"
16#include "SkTDArray.h"
mtkleinc2e29772015-10-30 05:24:58 -070017#include "SkTHash.h"
Brian Salomon199fb872017-02-06 09:41:10 -050018#include "SkVertices.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkWriter32.h"
20
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000021// These macros help with packing and unpacking a single byte value and
22// a 3 byte value into/out of a uint32_t
23#define MASK_24 0x00FFFFFF
24#define UNPACK_8_24(combined, small, large) \
25 small = (combined >> 24) & 0xFF; \
26 large = combined & MASK_24;
27#define PACK_8_24(small, large) ((small << 24) | large)
28
29
reed@android.com8a1c16f2008-12-17 15:59:43 +000030class SkPictureRecord : public SkCanvas {
31public:
robertphillips0bdbea72014-06-11 11:37:55 -070032 SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
Brian Salomond3b65972017-03-22 12:05:03 -040033 ~SkPictureRecord() override;
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
robertphillips9b14f262014-06-04 05:40:44 -070035 const SkTDArray<const SkPicture* >& getPictureRefs() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 return fPictureRefs;
37 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000038
msarett95416f42016-04-27 13:51:20 -070039 const SkTDArray<SkDrawable* >& getDrawableRefs() const {
40 return fDrawableRefs;
41 }
42
fmalitab7425172014-08-26 07:56:44 -070043 const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const {
44 return fTextBlobRefs;
45 }
46
Mike Reede88a1cb2017-03-17 09:50:46 -040047 const SkTDArray<const SkVertices* >& getVerticesRefs() const {
48 return fVerticesRefs;
49 }
50
51 const SkTDArray<const SkImage* >& getImageRefs() const {
reed871872f2015-06-22 12:48:26 -070052 return fImageRefs;
53 }
bungemanf3c15b72015-08-19 11:56:48 -070054
mtklein4d9ff622016-04-29 14:45:36 -070055 sk_sp<SkData> opData() const {
robertphillips0bdbea72014-06-11 11:37:55 -070056 this->validate(fWriter.bytesWritten(), 0);
57
58 if (fWriter.bytesWritten() == 0) {
reedfde05112016-03-11 13:02:28 -080059 return SkData::MakeEmpty();
robertphillips0bdbea72014-06-11 11:37:55 -070060 }
robertphillips0bdbea72014-06-11 11:37:55 -070061 return fWriter.snapshotAsData();
62 }
63
robertphillips0bdbea72014-06-11 11:37:55 -070064 const SkPictureContentInfo& contentInfo() const {
65 return fContentInfo;
66 }
67
junov@chromium.org4866cc02012-06-01 21:23:07 +000068 void setFlags(uint32_t recordFlags) {
69 fRecordFlags = recordFlags;
70 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000071
72 const SkWriter32& writeStream() const {
73 return fWriter;
74 }
75
reed@google.comd86e7ab2012-09-27 20:31:31 +000076 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +000077 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +000078
senorblanco@chromium.org68250c82014-05-06 22:52:55 +000079protected:
80 void addNoOp();
81
reed@android.com8a1c16f2008-12-17 15:59:43 +000082private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +000083 void handleOptimization(int opt);
Mike Reedc1f77742016-12-09 09:00:50 -050084 size_t recordRestoreOffsetPlaceholder(SkClipOp);
robertphillips@google.com5a63f242014-02-04 20:07:50 +000085 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +000086
reed@google.comffacd3c2012-08-30 15:31:23 +000087 SkTDArray<int32_t> fRestoreOffsetStack;
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +000089 SkTDArray<uint32_t> fCullOffsetStack;
90
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000091 /*
92 * Write the 'drawType' operation and chunk size to the skp. 'size'
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +000093 * can potentially be increased if the chunk size needs its own storage
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000094 * location (i.e., it overflows 24 bits).
95 * Returns the start offset of the chunk. This is the location at which
96 * the opcode & size are stored.
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +000097 * TODO: since we are handing the size into here we could call reserve
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000098 * and then return a pointer to the memory storage. This could decrease
99 * allocation overhead but could lead to more wasted space (the tail
100 * end of blocks could go unused). Possibly add a second addDraw that
101 * operates in this manner.
102 */
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000103 size_t addDraw(DrawType drawType, size_t* size) {
reed@google.com44699382013-10-31 17:28:30 +0000104 size_t offset = fWriter.bytesWritten();
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000105
reed@google.com97af1a62012-08-28 12:19:02 +0000106 this->predrawNotify();
hendrikwafdada22014-08-08 10:44:33 -0700107 fContentInfo.addOperation();
reed@google.com97af1a62012-08-28 12:19:02 +0000108
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000109 SkASSERT(0 != *size);
110 SkASSERT(((uint8_t) drawType) == drawType);
111
112 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
113 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
114 *size += 1;
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000115 fWriter.writeInt(SkToU32(*size));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000116 } else {
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000117 fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000118 }
119
120 return offset;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000121 }
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 void addInt(int value) {
124 fWriter.writeInt(value);
125 }
126 void addScalar(SkScalar scalar) {
127 fWriter.writeScalar(scalar);
128 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000129
reed871872f2015-06-22 12:48:26 -0700130 void addImage(const SkImage*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 void addMatrix(const SkMatrix& matrix);
mtklein46616af2014-09-30 14:47:10 -0700132 void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
133 void addPaintPtr(const SkPaint* paint);
dandovb3c9d1c2014-08-12 08:34:29 -0700134 void addPatch(const SkPoint cubics[12]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 void addPath(const SkPath& path);
robertphillips9b14f262014-06-04 05:40:44 -0700136 void addPicture(const SkPicture* picture);
msarett95416f42016-04-27 13:51:20 -0700137 void addDrawable(SkDrawable* picture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 void addPoint(const SkPoint& point);
139 void addPoints(const SkPoint pts[], int count);
140 void addRect(const SkRect& rect);
141 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000142 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 void addIRectPtr(const SkIRect* rect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000144 void addRRect(const SkRRect&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 void addRegion(const SkRegion& region);
146 void addText(const void* text, size_t byteLength);
fmalitab7425172014-08-26 07:56:44 -0700147 void addTextBlob(const SkTextBlob* blob);
Mike Reede88a1cb2017-03-17 09:50:46 -0400148 void addVertices(const SkVertices*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
junov@chromium.org4866cc02012-06-01 21:23:07 +0000150 int find(const SkBitmap& bitmap);
151
robertphillips6162af82014-08-11 09:50:11 -0700152protected:
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000153 void validate(size_t initialOffset, size_t size) const {
reed@google.com44699382013-10-31 17:28:30 +0000154 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000155 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156
reede8f30622016-03-23 18:59:25 -0700157 sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
reed884e97c2015-05-26 11:31:54 -0700158 bool onPeekPixels(SkPixmap*) override { return false; }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000159
mtklein36352bf2015-03-25 18:17:31 -0700160 void willSave() override;
reed4960eee2015-12-18 07:09:18 -0800161 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
mtklein36352bf2015-03-25 18:17:31 -0700162 void willRestore() override;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000163
mtklein36352bf2015-03-25 18:17:31 -0700164 void didConcat(const SkMatrix&) override;
165 void didSetMatrix(const SkMatrix&) override;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000166
vjiaoblack95302da2016-07-21 10:25:54 -0700167#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -0700168 void didTranslateZ(SkScalar) override;
vjiaoblack5bfee982016-07-22 10:04:15 -0700169#else
vjiaoblack95302da2016-07-21 10:25:54 -0700170 void didTranslateZ(SkScalar);
171#endif
vjiaoblacke5de1302016-07-13 14:05:28 -0700172
mtklein36352bf2015-03-25 18:17:31 -0700173 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
reed@google.com76f10a32014-02-05 15:32:21 +0000174
reed3dfd1332015-06-25 14:26:11 -0700175 void onDrawText(const void* text, size_t, SkScalar x, SkScalar y, const SkPaint&) override;
176 void onDrawPosText(const void* text, size_t, const SkPoint pos[], const SkPaint&) override;
177 void onDrawPosTextH(const void* text, size_t, const SkScalar xpos[], SkScalar constY,
178 const SkPaint&) override;
179 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700180 const SkMatrix* matrix, const SkPaint&) override;
reed45561a02016-07-07 12:47:17 -0700181 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
182 const SkRect* cull, const SkPaint&) override;
reed3dfd1332015-06-25 14:26:11 -0700183 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700184 const SkPaint& paint) override;
mtklein46616af2014-09-30 14:47:10 -0700185
reed3dfd1332015-06-25 14:26:11 -0700186 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400187 const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
reed71c3c762015-06-24 10:29:17 -0700188 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
Mike Reedfaba3712016-11-03 14:45:31 -0400189 SkBlendMode, const SkRect*, const SkPaint*) override;
reed@google.come0d9ce82014-04-23 04:00:17 +0000190
mtklein36352bf2015-03-25 18:17:31 -0700191 void onDrawPaint(const SkPaint&) override;
192 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
193 void onDrawRect(const SkRect&, const SkPaint&) override;
msarett44df6512016-08-25 13:54:30 -0700194 void onDrawRegion(const SkRegion&, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700195 void onDrawOval(const SkRect&, const SkPaint&) override;
bsalomonac3aa242016-08-19 11:25:19 -0700196 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700197 void onDrawRRect(const SkRRect&, const SkPaint&) override;
198 void onDrawPath(const SkPath&, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700199 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800200 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700201 const SkPaint*, SrcRectConstraint) override;
reed4c21dc52015-06-25 12:32:03 -0700202 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
203 const SkPaint*) override;
msarett16882062016-08-16 09:31:08 -0700204 void onDrawImageLattice(const SkImage*, const SkCanvas::Lattice& lattice, const SkRect& dst,
205 const SkPaint*) override;
Mike Reede88a1cb2017-03-17 09:50:46 -0400206 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
reed41af9662015-01-05 07:49:08 -0800207
Mike Reedc1f77742016-12-09 09:00:50 -0500208 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
209 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
210 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
211 void onClipRegion(const SkRegion&, SkClipOp) override;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000212
mtklein36352bf2015-03-25 18:17:31 -0700213 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
vjiaoblack95302da2016-07-21 10:25:54 -0700214
msarett95416f42016-04-27 13:51:20 -0700215 void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
reedf70b5312016-03-04 16:36:20 -0800216 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
robertphillips9b14f262014-06-04 05:40:44 -0700217
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000218 int addPathToHeap(const SkPath& path); // does not write to ops stream
219
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000220 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000221 // restores to be deferred (e.g., if the MC state is being collapsed and
222 // only written out as needed).
223 void recordConcat(const SkMatrix& matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000224 void recordTranslate(const SkMatrix& matrix);
225 void recordScale(const SkMatrix& matrix);
Mike Reedc1f77742016-12-09 09:00:50 -0500226 size_t recordClipRect(const SkRect& rect, SkClipOp op, bool doAA);
227 size_t recordClipRRect(const SkRRect& rrect, SkClipOp op, bool doAA);
228 size_t recordClipPath(int pathID, SkClipOp op, bool doAA);
229 size_t recordClipRegion(const SkRegion& region, SkClipOp op);
Florin Malita5f6102d2014-06-30 10:13:28 -0400230 void recordSave();
reed4960eee2015-12-18 07:09:18 -0800231 void recordSaveLayer(const SaveLayerRec&);
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000232 void recordRestore(bool fillInSkips = true);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000233
reeda9ca05c2016-08-11 03:55:15 -0700234 // SHOULD NEVER BE CALLED
235 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override {
236 sk_throw();
237 }
238 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
239 SrcRectConstraint) override {
240 sk_throw();
241 }
242 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
243 const SkPaint*) override {
244 sk_throw();
245 }
msarett16882062016-08-16 09:31:08 -0700246 void onDrawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice& lattice, const SkRect& dst,
247 const SkPaint*) override {
248 sk_throw();
249 }
reeda9ca05c2016-08-11 03:55:15 -0700250
robertphillips@google.com801cee12012-10-19 19:06:11 +0000251private:
robertphillips0bdbea72014-06-11 11:37:55 -0700252 SkPictureContentInfo fContentInfo;
robertphillips0bdbea72014-06-11 11:37:55 -0700253
mtklein71a23632014-11-12 10:24:55 -0800254 SkTArray<SkPaint> fPaints;
mtkleinc2e29772015-10-30 05:24:58 -0700255
256 struct PathHash {
257 uint32_t operator()(const SkPath& p) { return p.getGenerationID(); }
258 };
259 SkTHashMap<SkPath, int, PathHash> fPaths;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000260
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261 SkWriter32 fWriter;
262
reed@android.com09b84a02009-06-26 20:22:26 +0000263 // we ref each item in these arrays
reed871872f2015-06-22 12:48:26 -0700264 SkTDArray<const SkImage*> fImageRefs;
fmalitab7425172014-08-26 07:56:44 -0700265 SkTDArray<const SkPicture*> fPictureRefs;
msarett95416f42016-04-27 13:51:20 -0700266 SkTDArray<SkDrawable*> fDrawableRefs;
fmalitab7425172014-08-26 07:56:44 -0700267 SkTDArray<const SkTextBlob*> fTextBlobRefs;
Mike Reede88a1cb2017-03-17 09:50:46 -0400268 SkTDArray<const SkVertices*> fVerticesRefs;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269
reed@android.comae814c82009-02-13 14:56:09 +0000270 uint32_t fRecordFlags;
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +0000271 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000272
robertphillips61426092014-07-10 09:35:12 -0700273 friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor
reed@android.com8a1c16f2008-12-17 15:59:43 +0000274
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 typedef SkCanvas INHERITED;
276};
vjiaoblack5bfee982016-07-22 10:04:15 -0700277
278#endif