blob: 59bd92dc77d27562dbdfc023023bfc724c3243f6 [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#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
robertphillips9b14f262014-06-04 05:40:44 -070034 const SkTDArray<const SkPicture* >& getPictureRefs() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 return fPictureRefs;
36 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000037
msarett95416f42016-04-27 13:51:20 -070038 const SkTDArray<SkDrawable* >& getDrawableRefs() const {
39 return fDrawableRefs;
40 }
41
fmalitab7425172014-08-26 07:56:44 -070042 const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const {
43 return fTextBlobRefs;
44 }
45
reed871872f2015-06-22 12:48:26 -070046 const SkTDArray<const SkImage* >& getImageRefs() const {
47 return fImageRefs;
48 }
bungemanf3c15b72015-08-19 11:56:48 -070049
reedfde05112016-03-11 13:02:28 -080050 sk_sp<SkData> opData(bool deepCopy) const {
robertphillips0bdbea72014-06-11 11:37:55 -070051 this->validate(fWriter.bytesWritten(), 0);
52
53 if (fWriter.bytesWritten() == 0) {
reedfde05112016-03-11 13:02:28 -080054 return SkData::MakeEmpty();
robertphillips0bdbea72014-06-11 11:37:55 -070055 }
56
57 if (deepCopy) {
reedfde05112016-03-11 13:02:28 -080058 return SkData::MakeWithCopy(fWriter.contiguousArray(), fWriter.bytesWritten());
robertphillips0bdbea72014-06-11 11:37:55 -070059 }
60
61 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);
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +000084 size_t recordRestoreOffsetPlaceholder(SkRegion::Op);
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
mtkleine0694002014-11-12 12:49:47 -0800130 void addBitmap(const SkBitmap& bitmap);
reed871872f2015-06-22 12:48:26 -0700131 void addImage(const SkImage*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 void addMatrix(const SkMatrix& matrix);
mtklein46616af2014-09-30 14:47:10 -0700133 void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
134 void addPaintPtr(const SkPaint* paint);
dandovb3c9d1c2014-08-12 08:34:29 -0700135 void addPatch(const SkPoint cubics[12]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 void addPath(const SkPath& path);
robertphillips9b14f262014-06-04 05:40:44 -0700137 void addPicture(const SkPicture* picture);
msarett95416f42016-04-27 13:51:20 -0700138 void addDrawable(SkDrawable* picture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 void addPoint(const SkPoint& point);
140 void addPoints(const SkPoint pts[], int count);
141 void addRect(const SkRect& rect);
142 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000143 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 void addIRectPtr(const SkIRect* rect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000145 void addRRect(const SkRRect&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 void addRegion(const SkRegion& region);
147 void addText(const void* text, size_t byteLength);
fmalitab7425172014-08-26 07:56:44 -0700148 void addTextBlob(const SkTextBlob* blob);
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
mtklein36352bf2015-03-25 18:17:31 -0700167 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
reed@google.com76f10a32014-02-05 15:32:21 +0000168
reed3dfd1332015-06-25 14:26:11 -0700169 void onDrawText(const void* text, size_t, SkScalar x, SkScalar y, const SkPaint&) override;
170 void onDrawPosText(const void* text, size_t, const SkPoint pos[], const SkPaint&) override;
171 void onDrawPosTextH(const void* text, size_t, const SkScalar xpos[], SkScalar constY,
172 const SkPaint&) override;
173 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700174 const SkMatrix* matrix, 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],
dandovb3c9d1c2014-08-12 08:34:29 -0700179 const SkPoint texCoords[4], SkXfermode* xmode,
mtklein36352bf2015-03-25 18:17:31 -0700180 const SkPaint& paint) override;
reed71c3c762015-06-24 10:29:17 -0700181 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
182 SkXfermode::Mode, const SkRect*, const SkPaint*) override;
reed@google.come0d9ce82014-04-23 04:00:17 +0000183
mtklein36352bf2015-03-25 18:17:31 -0700184 void onDrawPaint(const SkPaint&) override;
185 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
186 void onDrawRect(const SkRect&, const SkPaint&) override;
187 void onDrawOval(const SkRect&, const SkPaint&) override;
188 void onDrawRRect(const SkRRect&, const SkPaint&) override;
189 void onDrawPath(const SkPath&, const SkPaint&) override;
190 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800191 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
reed562fe472015-07-28 07:35:14 -0700192 SrcRectConstraint) override;
mtklein36352bf2015-03-25 18:17:31 -0700193 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800194 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700195 const SkPaint*, SrcRectConstraint) override;
reed4c21dc52015-06-25 12:32:03 -0700196 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
197 const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800198 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
mtklein36352bf2015-03-25 18:17:31 -0700199 const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800200 void onDrawVertices(VertexMode vmode, int vertexCount,
201 const SkPoint vertices[], const SkPoint texs[],
202 const SkColor colors[], SkXfermode* xmode,
203 const uint16_t indices[], int indexCount,
mtklein36352bf2015-03-25 18:17:31 -0700204 const SkPaint&) override;
reed41af9662015-01-05 07:49:08 -0800205
mtklein36352bf2015-03-25 18:17:31 -0700206 void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
207 void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
208 void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
209 void onClipRegion(const SkRegion&, SkRegion::Op) override;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000210
mtklein36352bf2015-03-25 18:17:31 -0700211 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
msarett95416f42016-04-27 13:51:20 -0700212 void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
reedf70b5312016-03-04 16:36:20 -0800213 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
robertphillips9b14f262014-06-04 05:40:44 -0700214
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000215 int addPathToHeap(const SkPath& path); // does not write to ops stream
216
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000217 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000218 // restores to be deferred (e.g., if the MC state is being collapsed and
219 // only written out as needed).
220 void recordConcat(const SkMatrix& matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000221 void recordTranslate(const SkMatrix& matrix);
222 void recordScale(const SkMatrix& matrix);
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000223 size_t recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
224 size_t recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
225 size_t recordClipPath(int pathID, SkRegion::Op op, bool doAA);
226 size_t recordClipRegion(const SkRegion& region, SkRegion::Op op);
Florin Malita5f6102d2014-06-30 10:13:28 -0400227 void recordSave();
reed4960eee2015-12-18 07:09:18 -0800228 void recordSaveLayer(const SaveLayerRec&);
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000229 void recordRestore(bool fillInSkips = true);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000230
robertphillips@google.com801cee12012-10-19 19:06:11 +0000231private:
robertphillips0bdbea72014-06-11 11:37:55 -0700232 SkPictureContentInfo fContentInfo;
robertphillips0bdbea72014-06-11 11:37:55 -0700233
mtklein71a23632014-11-12 10:24:55 -0800234 SkTArray<SkBitmap> fBitmaps;
235 SkTArray<SkPaint> fPaints;
mtkleinc2e29772015-10-30 05:24:58 -0700236
237 struct PathHash {
238 uint32_t operator()(const SkPath& p) { return p.getGenerationID(); }
239 };
240 SkTHashMap<SkPath, int, PathHash> fPaths;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000241
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 SkWriter32 fWriter;
243
reed@android.com09b84a02009-06-26 20:22:26 +0000244 // we ref each item in these arrays
reed871872f2015-06-22 12:48:26 -0700245 SkTDArray<const SkImage*> fImageRefs;
fmalitab7425172014-08-26 07:56:44 -0700246 SkTDArray<const SkPicture*> fPictureRefs;
msarett95416f42016-04-27 13:51:20 -0700247 SkTDArray<SkDrawable*> fDrawableRefs;
fmalitab7425172014-08-26 07:56:44 -0700248 SkTDArray<const SkTextBlob*> fTextBlobRefs;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249
reed@android.comae814c82009-02-13 14:56:09 +0000250 uint32_t fRecordFlags;
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +0000251 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000252
robertphillips61426092014-07-10 09:35:12 -0700253 friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 typedef SkCanvas INHERITED;
256};
257
258#endif