blob: ed5d065b1724bdd1fc37cc0fcdcb82a4c54b1f70 [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"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkTemplates.h"
16#include "SkWriter32.h"
17
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000018// These macros help with packing and unpacking a single byte value and
19// a 3 byte value into/out of a uint32_t
20#define MASK_24 0x00FFFFFF
21#define UNPACK_8_24(combined, small, large) \
22 small = (combined >> 24) & 0xFF; \
23 large = combined & MASK_24;
24#define PACK_8_24(small, large) ((small << 24) | large)
25
26
reed@android.com8a1c16f2008-12-17 15:59:43 +000027class SkPictureRecord : public SkCanvas {
28public:
robertphillips0bdbea72014-06-11 11:37:55 -070029 SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 virtual ~SkPictureRecord();
31
robertphillips9b14f262014-06-04 05:40:44 -070032 const SkTDArray<const SkPicture* >& getPictureRefs() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 return fPictureRefs;
34 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000035
fmalitab7425172014-08-26 07:56:44 -070036 const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const {
37 return fTextBlobRefs;
38 }
39
reed871872f2015-06-22 12:48:26 -070040 const SkTDArray<const SkImage* >& getImageRefs() const {
41 return fImageRefs;
42 }
43
robertphillips0bdbea72014-06-11 11:37:55 -070044 SkData* opData(bool deepCopy) const {
45 this->validate(fWriter.bytesWritten(), 0);
46
47 if (fWriter.bytesWritten() == 0) {
48 return SkData::NewEmpty();
49 }
50
51 if (deepCopy) {
52 return SkData::NewWithCopy(fWriter.contiguousArray(), fWriter.bytesWritten());
53 }
54
55 return fWriter.snapshotAsData();
56 }
57
robertphillips0bdbea72014-06-11 11:37:55 -070058 const SkPictureContentInfo& contentInfo() const {
59 return fContentInfo;
60 }
61
junov@chromium.org4866cc02012-06-01 21:23:07 +000062 void setFlags(uint32_t recordFlags) {
63 fRecordFlags = recordFlags;
64 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000065
66 const SkWriter32& writeStream() const {
67 return fWriter;
68 }
69
reed@google.comd86e7ab2012-09-27 20:31:31 +000070 void beginRecording();
junov@chromium.orga6c9e0e2012-07-12 17:47:34 +000071 void endRecording();
reed@google.comd86e7ab2012-09-27 20:31:31 +000072
senorblanco@chromium.org68250c82014-05-06 22:52:55 +000073protected:
74 void addNoOp();
75
reed@android.com8a1c16f2008-12-17 15:59:43 +000076private:
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +000077 void handleOptimization(int opt);
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +000078 size_t recordRestoreOffsetPlaceholder(SkRegion::Op);
robertphillips@google.com5a63f242014-02-04 20:07:50 +000079 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
junov@chromium.orge3dbedb2012-07-09 16:03:55 +000080
reed@google.comffacd3c2012-08-30 15:31:23 +000081 SkTDArray<int32_t> fRestoreOffsetStack;
reed@android.com8a1c16f2008-12-17 15:59:43 +000082
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +000083 SkTDArray<uint32_t> fCullOffsetStack;
84
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000085 /*
86 * Write the 'drawType' operation and chunk size to the skp. 'size'
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +000087 * can potentially be increased if the chunk size needs its own storage
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000088 * location (i.e., it overflows 24 bits).
89 * Returns the start offset of the chunk. This is the location at which
90 * the opcode & size are stored.
skia.committer@gmail.comce8343d2013-02-16 07:01:31 +000091 * TODO: since we are handing the size into here we could call reserve
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000092 * and then return a pointer to the memory storage. This could decrease
93 * allocation overhead but could lead to more wasted space (the tail
94 * end of blocks could go unused). Possibly add a second addDraw that
95 * operates in this manner.
96 */
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +000097 size_t addDraw(DrawType drawType, size_t* size) {
reed@google.com44699382013-10-31 17:28:30 +000098 size_t offset = fWriter.bytesWritten();
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +000099
reed@google.com97af1a62012-08-28 12:19:02 +0000100 this->predrawNotify();
hendrikwafdada22014-08-08 10:44:33 -0700101 fContentInfo.addOperation();
reed@google.com97af1a62012-08-28 12:19:02 +0000102
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000103 SkASSERT(0 != *size);
104 SkASSERT(((uint8_t) drawType) == drawType);
105
106 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
107 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
108 *size += 1;
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000109 fWriter.writeInt(SkToU32(*size));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000110 } else {
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000111 fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000112 }
113
114 return offset;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000115 }
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 void addInt(int value) {
118 fWriter.writeInt(value);
119 }
120 void addScalar(SkScalar scalar) {
121 fWriter.writeScalar(scalar);
122 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000123
mtkleine0694002014-11-12 12:49:47 -0800124 void addBitmap(const SkBitmap& bitmap);
reed871872f2015-06-22 12:48:26 -0700125 void addImage(const SkImage*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 void addMatrix(const SkMatrix& matrix);
mtklein46616af2014-09-30 14:47:10 -0700127 void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
128 void addPaintPtr(const SkPaint* paint);
dandovb3c9d1c2014-08-12 08:34:29 -0700129 void addPatch(const SkPoint cubics[12]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 void addPath(const SkPath& path);
robertphillips9b14f262014-06-04 05:40:44 -0700131 void addPicture(const SkPicture* picture);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 void addPoint(const SkPoint& point);
133 void addPoints(const SkPoint pts[], int count);
134 void addRect(const SkRect& rect);
135 void addRectPtr(const SkRect* rect);
reed@google.comf0b5e112011-09-07 11:57:34 +0000136 void addIRect(const SkIRect& rect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 void addIRectPtr(const SkIRect* rect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000138 void addRRect(const SkRRect&);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 void addRegion(const SkRegion& region);
140 void addText(const void* text, size_t byteLength);
fmalitab7425172014-08-26 07:56:44 -0700141 void addTextBlob(const SkTextBlob* blob);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142
junov@chromium.org4866cc02012-06-01 21:23:07 +0000143 int find(const SkBitmap& bitmap);
144
robertphillips6162af82014-08-11 09:50:11 -0700145protected:
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000146 void validate(size_t initialOffset, size_t size) const {
reed@google.com44699382013-10-31 17:28:30 +0000147 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
robertphillips@google.com2ca1aaa2013-02-15 13:47:37 +0000148 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
mtklein36352bf2015-03-25 18:17:31 -0700150 SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
reed884e97c2015-05-26 11:31:54 -0700151 bool onPeekPixels(SkPixmap*) override { return false; }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000152
mtklein36352bf2015-03-25 18:17:31 -0700153 void willSave() override;
154 SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) override;
155 void willRestore() override;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000156
mtklein36352bf2015-03-25 18:17:31 -0700157 void didConcat(const SkMatrix&) override;
158 void didSetMatrix(const SkMatrix&) override;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000159
mtklein36352bf2015-03-25 18:17:31 -0700160 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
reed@google.com76f10a32014-02-05 15:32:21 +0000161
reed3dfd1332015-06-25 14:26:11 -0700162 void onDrawText(const void* text, size_t, SkScalar x, SkScalar y, const SkPaint&) override;
163 void onDrawPosText(const void* text, size_t, const SkPoint pos[], const SkPaint&) override;
164 void onDrawPosTextH(const void* text, size_t, const SkScalar xpos[], SkScalar constY,
165 const SkPaint&) override;
166 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700167 const SkMatrix* matrix, const SkPaint&) override;
reed3dfd1332015-06-25 14:26:11 -0700168 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700169 const SkPaint& paint) override;
mtklein46616af2014-09-30 14:47:10 -0700170
reed3dfd1332015-06-25 14:26:11 -0700171 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
dandovb3c9d1c2014-08-12 08:34:29 -0700172 const SkPoint texCoords[4], SkXfermode* xmode,
mtklein36352bf2015-03-25 18:17:31 -0700173 const SkPaint& paint) override;
reed71c3c762015-06-24 10:29:17 -0700174 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
175 SkXfermode::Mode, const SkRect*, const SkPaint*) override;
reed@google.come0d9ce82014-04-23 04:00:17 +0000176
mtklein36352bf2015-03-25 18:17:31 -0700177 void onDrawPaint(const SkPaint&) override;
178 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
179 void onDrawRect(const SkRect&, const SkPaint&) override;
180 void onDrawOval(const SkRect&, const SkPaint&) override;
181 void onDrawRRect(const SkRRect&, const SkPaint&) override;
182 void onDrawPath(const SkPath&, const SkPaint&) override;
183 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800184 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
mtklein36352bf2015-03-25 18:17:31 -0700185 DrawBitmapRectFlags flags) override;
mtklein36352bf2015-03-25 18:17:31 -0700186 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800187 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
mtklein36352bf2015-03-25 18:17:31 -0700188 const SkPaint*) override;
reed4c21dc52015-06-25 12:32:03 -0700189 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
190 const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800191 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
mtklein36352bf2015-03-25 18:17:31 -0700192 const SkPaint*) override;
193 void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800194 void onDrawVertices(VertexMode vmode, int vertexCount,
195 const SkPoint vertices[], const SkPoint texs[],
196 const SkColor colors[], SkXfermode* xmode,
197 const uint16_t indices[], int indexCount,
mtklein36352bf2015-03-25 18:17:31 -0700198 const SkPaint&) override;
reed41af9662015-01-05 07:49:08 -0800199
mtklein36352bf2015-03-25 18:17:31 -0700200 void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
201 void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
202 void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
203 void onClipRegion(const SkRegion&, SkRegion::Op) override;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000204
mtklein36352bf2015-03-25 18:17:31 -0700205 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
robertphillips9b14f262014-06-04 05:40:44 -0700206
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000207 int addPathToHeap(const SkPath& path); // does not write to ops stream
208
skia.committer@gmail.com1dab4032014-02-05 03:01:48 +0000209 // These entry points allow the writing of matrices, clips, saves &
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000210 // restores to be deferred (e.g., if the MC state is being collapsed and
211 // only written out as needed).
212 void recordConcat(const SkMatrix& matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000213 void recordTranslate(const SkMatrix& matrix);
214 void recordScale(const SkMatrix& matrix);
commit-bot@chromium.orgdcecb162014-04-22 17:54:29 +0000215 size_t recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
216 size_t recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
217 size_t recordClipPath(int pathID, SkRegion::Op op, bool doAA);
218 size_t recordClipRegion(const SkRegion& region, SkRegion::Op op);
Florin Malita5f6102d2014-06-30 10:13:28 -0400219 void recordSave();
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000220 void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
robertphillips@google.com105a4a52014-02-11 15:10:40 +0000221 void recordRestore(bool fillInSkips = true);
robertphillips@google.com5a63f242014-02-04 20:07:50 +0000222
robertphillips@google.com801cee12012-10-19 19:06:11 +0000223private:
robertphillips0bdbea72014-06-11 11:37:55 -0700224 SkPictureContentInfo fContentInfo;
robertphillips0bdbea72014-06-11 11:37:55 -0700225
mtklein71a23632014-11-12 10:24:55 -0800226 SkTArray<SkBitmap> fBitmaps;
227 SkTArray<SkPaint> fPaints;
228 SkTArray<SkPath> fPaths;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000229
reed@android.com8a1c16f2008-12-17 15:59:43 +0000230 SkWriter32 fWriter;
231
reed@android.com09b84a02009-06-26 20:22:26 +0000232 // we ref each item in these arrays
reed871872f2015-06-22 12:48:26 -0700233 SkTDArray<const SkImage*> fImageRefs;
fmalitab7425172014-08-26 07:56:44 -0700234 SkTDArray<const SkPicture*> fPictureRefs;
235 SkTDArray<const SkTextBlob*> fTextBlobRefs;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236
reed@android.comae814c82009-02-13 14:56:09 +0000237 uint32_t fRecordFlags;
commit-bot@chromium.orge494dbd2014-03-04 19:08:57 +0000238 int fInitialSaveCount;
reed@android.comae814c82009-02-13 14:56:09 +0000239
robertphillips61426092014-07-10 09:35:12 -0700240 friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 typedef SkCanvas INHERITED;
243};
244
245#endif