blob: 587233c298fd458d6c97e2e49cae939097276464 [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);
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 virtual ~SkPictureRecord();
34
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
reed871872f2015-06-22 12:48:26 -070047 const SkTDArray<const SkImage* >& getImageRefs() const {
48 return fImageRefs;
49 }
bungemanf3c15b72015-08-19 11:56:48 -070050
mtklein4d9ff622016-04-29 14:45:36 -070051 sk_sp<SkData> opData() const {
robertphillips0bdbea72014-06-11 11:37:55 -070052 this->validate(fWriter.bytesWritten(), 0);
53
54 if (fWriter.bytesWritten() == 0) {
reedfde05112016-03-11 13:02:28 -080055 return SkData::MakeEmpty();
robertphillips0bdbea72014-06-11 11:37:55 -070056 }
robertphillips0bdbea72014-06-11 11:37:55 -070057 return fWriter.snapshotAsData();
58 }
59
robertphillips0bdbea72014-06-11 11:37:55 -070060 const SkPictureContentInfo& contentInfo() const {
61 return fContentInfo;
62 }
63
junov@chromium.org4866cc02012-06-01 21:23:07 +000064 void setFlags(uint32_t recordFlags) {
65 fRecordFlags = recordFlags;
66 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000067
68 const SkWriter32& writeStream() const {
69 return fWriter;
70 }
71
reed@google.comd86e7ab2012-09-27 20:31:31 +000072 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +000073 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +000074
senorblanco@chromium.org68250c82014-05-06 22:52:55 +000075protected:
76 void addNoOp();
77
reed@android.com8a1c16f2008-12-17 15:59:43 +000078private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +000079 void handleOptimization(int opt);
Mike Reedc1f77742016-12-09 09:00:50 -050080 size_t recordRestoreOffsetPlaceholder(SkClipOp);
robertphillips@google.com5a63f242014-02-04 20:07:50 +000081 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +000082
reed@google.comffacd3c2012-08-30 15:31:23 +000083 SkTDArray<int32_t> fRestoreOffsetStack;
reed@android.com8a1c16f2008-12-17 15:59:43 +000084
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +000085 SkTDArray<uint32_t> fCullOffsetStack;
86
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000087 /*
88 * Write the 'drawType' operation and chunk size to the skp. 'size'
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +000089 * can potentially be increased if the chunk size needs its own storage
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000090 * location (i.e., it overflows 24 bits).
91 * Returns the start offset of the chunk. This is the location at which
92 * the opcode & size are stored.
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +000093 * TODO: since we are handing the size into here we could call reserve
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000094 * and then return a pointer to the memory storage. This could decrease
95 * allocation overhead but could lead to more wasted space (the tail
96 * end of blocks could go unused). Possibly add a second addDraw that
97 * operates in this manner.
98 */
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +000099 size_t addDraw(DrawType drawType, size_t* size) {
reed@google.com44699382013-10-31 17:28:30 +0000100 size_t offset = fWriter.bytesWritten();
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000101
reed@google.com97af1a62012-08-28 12:19:02 +0000102 this->predrawNotify();
hendrikwafdada22014-08-08 10:44:33 -0700103 fContentInfo.addOperation();
reed@google.com97af1a62012-08-28 12:19:02 +0000104
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);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144
junov@chromium.org4866cc02012-06-01 21:23:07 +0000145 int find(const SkBitmap& bitmap);
146
robertphillips6162af82014-08-11 09:50:11 -0700147protected:
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000148 void validate(size_t initialOffset, size_t size) const {
reed@google.com44699382013-10-31 17:28:30 +0000149 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000150 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151
reede8f30622016-03-23 18:59:25 -0700152 sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
reed884e97c2015-05-26 11:31:54 -0700153 bool onPeekPixels(SkPixmap*) override { return false; }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000154
mtklein36352bf2015-03-25 18:17:31 -0700155 void willSave() override;
reed4960eee2015-12-18 07:09:18 -0800156 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
mtklein36352bf2015-03-25 18:17:31 -0700157 void willRestore() override;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000158
mtklein36352bf2015-03-25 18:17:31 -0700159 void didConcat(const SkMatrix&) override;
160 void didSetMatrix(const SkMatrix&) override;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000161
vjiaoblack95302da2016-07-21 10:25:54 -0700162#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -0700163 void didTranslateZ(SkScalar) override;
vjiaoblack5bfee982016-07-22 10:04:15 -0700164#else
vjiaoblack95302da2016-07-21 10:25:54 -0700165 void didTranslateZ(SkScalar);
166#endif
vjiaoblacke5de1302016-07-13 14:05:28 -0700167
mtklein36352bf2015-03-25 18:17:31 -0700168 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
reed@google.com76f10a32014-02-05 15:32:21 +0000169
reed3dfd1332015-06-25 14:26:11 -0700170 void onDrawText(const void* text, size_t, SkScalar x, SkScalar y, const SkPaint&) override;
171 void onDrawPosText(const void* text, size_t, const SkPoint pos[], const SkPaint&) override;
172 void onDrawPosTextH(const void* text, size_t, const SkScalar xpos[], SkScalar constY,
173 const SkPaint&) override;
174 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700175 const SkMatrix* matrix, const SkPaint&) override;
reed45561a02016-07-07 12:47:17 -0700176 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
177 const SkRect* cull, const SkPaint&) override;
reed3dfd1332015-06-25 14:26:11 -0700178 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700179 const SkPaint& paint) override;
mtklein46616af2014-09-30 14:47:10 -0700180
reed3dfd1332015-06-25 14:26:11 -0700181 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400182 const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
reed71c3c762015-06-24 10:29:17 -0700183 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
Mike Reedfaba3712016-11-03 14:45:31 -0400184 SkBlendMode, const SkRect*, const SkPaint*) override;
reed@google.come0d9ce82014-04-23 04:00:17 +0000185
mtklein36352bf2015-03-25 18:17:31 -0700186 void onDrawPaint(const SkPaint&) override;
187 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
188 void onDrawRect(const SkRect&, const SkPaint&) override;
msarett44df6512016-08-25 13:54:30 -0700189 void onDrawRegion(const SkRegion&, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700190 void onDrawOval(const SkRect&, const SkPaint&) override;
bsalomonac3aa242016-08-19 11:25:19 -0700191 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700192 void onDrawRRect(const SkRRect&, const SkPaint&) override;
193 void onDrawPath(const SkPath&, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700194 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800195 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700196 const SkPaint*, SrcRectConstraint) override;
reed4c21dc52015-06-25 12:32:03 -0700197 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
198 const SkPaint*) override;
msarett16882062016-08-16 09:31:08 -0700199 void onDrawImageLattice(const SkImage*, const SkCanvas::Lattice& lattice, const SkRect& dst,
200 const SkPaint*) override;
201
reed41af9662015-01-05 07:49:08 -0800202 void onDrawVertices(VertexMode vmode, int vertexCount,
203 const SkPoint vertices[], const SkPoint texs[],
Mike Reedfaba3712016-11-03 14:45:31 -0400204 const SkColor colors[], SkBlendMode,
reed41af9662015-01-05 07:49:08 -0800205 const uint16_t indices[], int indexCount,
mtklein36352bf2015-03-25 18:17:31 -0700206 const SkPaint&) override;
Brian Salomon199fb872017-02-06 09:41:10 -0500207 void onDrawVerticesObject(sk_sp<SkVertices> vertices, SkBlendMode mode, const SkPaint& paint,
208 uint32_t flags) override {
209 this->onDrawVerticesObjectFallback(std::move(vertices), mode, paint, flags);
210 }
reed41af9662015-01-05 07:49:08 -0800211
Mike Reedc1f77742016-12-09 09:00:50 -0500212 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
213 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
214 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
215 void onClipRegion(const SkRegion&, SkClipOp) override;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000216
mtklein36352bf2015-03-25 18:17:31 -0700217 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
vjiaoblack95302da2016-07-21 10:25:54 -0700218
219#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke6f5d562016-08-25 06:30:23 -0700220 void onDrawShadowedPicture(const SkPicture*, const SkMatrix*,
221 const SkPaint*, const SkShadowParams& params) override;
vjiaoblack95302da2016-07-21 10:25:54 -0700222#else
vjiaoblacke6f5d562016-08-25 06:30:23 -0700223 void onDrawShadowedPicture(const SkPicture*, const SkMatrix*,
224 const SkPaint*, const SkShadowParams& params);
vjiaoblack95302da2016-07-21 10:25:54 -0700225#endif
226
msarett95416f42016-04-27 13:51:20 -0700227 void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
reedf70b5312016-03-04 16:36:20 -0800228 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
robertphillips9b14f262014-06-04 05:40:44 -0700229
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000230 int addPathToHeap(const SkPath& path); // does not write to ops stream
231
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000232 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000233 // restores to be deferred (e.g., if the MC state is being collapsed and
234 // only written out as needed).
235 void recordConcat(const SkMatrix& matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000236 void recordTranslate(const SkMatrix& matrix);
237 void recordScale(const SkMatrix& matrix);
Mike Reedc1f77742016-12-09 09:00:50 -0500238 size_t recordClipRect(const SkRect& rect, SkClipOp op, bool doAA);
239 size_t recordClipRRect(const SkRRect& rrect, SkClipOp op, bool doAA);
240 size_t recordClipPath(int pathID, SkClipOp op, bool doAA);
241 size_t recordClipRegion(const SkRegion& region, SkClipOp op);
Florin Malita5f6102d2014-06-30 10:13:28 -0400242 void recordSave();
reed4960eee2015-12-18 07:09:18 -0800243 void recordSaveLayer(const SaveLayerRec&);
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000244 void recordRestore(bool fillInSkips = true);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000245
reeda9ca05c2016-08-11 03:55:15 -0700246 // SHOULD NEVER BE CALLED
247 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override {
248 sk_throw();
249 }
250 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
251 SrcRectConstraint) override {
252 sk_throw();
253 }
254 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
255 const SkPaint*) override {
256 sk_throw();
257 }
msarett16882062016-08-16 09:31:08 -0700258 void onDrawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice& lattice, const SkRect& dst,
259 const SkPaint*) override {
260 sk_throw();
261 }
reeda9ca05c2016-08-11 03:55:15 -0700262
robertphillips@google.com801cee12012-10-19 19:06:11 +0000263private:
robertphillips0bdbea72014-06-11 11:37:55 -0700264 SkPictureContentInfo fContentInfo;
robertphillips0bdbea72014-06-11 11:37:55 -0700265
mtklein71a23632014-11-12 10:24:55 -0800266 SkTArray<SkPaint> fPaints;
mtkleinc2e29772015-10-30 05:24:58 -0700267
268 struct PathHash {
269 uint32_t operator()(const SkPath& p) { return p.getGenerationID(); }
270 };
271 SkTHashMap<SkPath, int, PathHash> fPaths;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000272
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 SkWriter32 fWriter;
274
reed@android.com09b84a02009-06-26 20:22:26 +0000275 // we ref each item in these arrays
reed871872f2015-06-22 12:48:26 -0700276 SkTDArray<const SkImage*> fImageRefs;
fmalitab7425172014-08-26 07:56:44 -0700277 SkTDArray<const SkPicture*> fPictureRefs;
msarett95416f42016-04-27 13:51:20 -0700278 SkTDArray<SkDrawable*> fDrawableRefs;
fmalitab7425172014-08-26 07:56:44 -0700279 SkTDArray<const SkTextBlob*> fTextBlobRefs;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280
reed@android.comae814c82009-02-13 14:56:09 +0000281 uint32_t fRecordFlags;
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +0000282 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000283
robertphillips61426092014-07-10 09:35:12 -0700284 friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 typedef SkCanvas INHERITED;
287};
vjiaoblack5bfee982016-07-22 10:04:15 -0700288
289#endif