2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkPictureRecord_DEFINED
9 #define SkPictureRecord_DEFINED
12 #include "SkFlattenable.h"
13 #include "SkPicture.h"
14 #include "SkPictureData.h"
15 #include "SkTemplates.h"
16 #include "SkWriter32.h"
18 // 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)
27 class SkPictureRecord : public SkCanvas {
29 SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
30 virtual ~SkPictureRecord();
32 void beginCommentGroup(const char* description) override;
33 void addComment(const char* kywd, const char* value) override;
34 void endCommentGroup() override;
36 const SkTDArray<const SkPicture* >& getPictureRefs() const {
40 const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const {
44 SkData* opData(bool deepCopy) const {
45 this->validate(fWriter.bytesWritten(), 0);
47 if (fWriter.bytesWritten() == 0) {
48 return SkData::NewEmpty();
52 return SkData::NewWithCopy(fWriter.contiguousArray(), fWriter.bytesWritten());
55 return fWriter.snapshotAsData();
58 const SkPictureContentInfo& contentInfo() const {
62 void setFlags(uint32_t recordFlags) {
63 fRecordFlags = recordFlags;
66 const SkWriter32& writeStream() const {
70 void beginRecording();
77 void handleOptimization(int opt);
78 size_t recordRestoreOffsetPlaceholder(SkRegion::Op);
79 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
81 SkTDArray<int32_t> fRestoreOffsetStack;
83 SkTDArray<uint32_t> fCullOffsetStack;
86 * Write the 'drawType' operation and chunk size to the skp. 'size'
87 * can potentially be increased if the chunk size needs its own storage
88 * 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.
91 * TODO: since we are handing the size into here we could call reserve
92 * 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.
97 size_t addDraw(DrawType drawType, size_t* size) {
98 size_t offset = fWriter.bytesWritten();
100 this->predrawNotify();
101 fContentInfo.addOperation();
103 SkASSERT(0 != *size);
104 SkASSERT(((uint8_t) drawType) == drawType);
106 if (0 != (*size & ~MASK_24) || *size == MASK_24) {
107 fWriter.writeInt(PACK_8_24(drawType, MASK_24));
109 fWriter.writeInt(SkToU32(*size));
111 fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
117 void addInt(int value) {
118 fWriter.writeInt(value);
120 void addScalar(SkScalar scalar) {
121 fWriter.writeScalar(scalar);
124 void addBitmap(const SkBitmap& bitmap);
125 void addMatrix(const SkMatrix& matrix);
126 void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
127 void addPaintPtr(const SkPaint* paint);
128 void addPatch(const SkPoint cubics[12]);
129 void addPath(const SkPath& path);
130 void addPicture(const SkPicture* picture);
131 void addPoint(const SkPoint& point);
132 void addPoints(const SkPoint pts[], int count);
133 void addRect(const SkRect& rect);
134 void addRectPtr(const SkRect* rect);
135 void addIRect(const SkIRect& rect);
136 void addIRectPtr(const SkIRect* rect);
137 void addRRect(const SkRRect&);
138 void addRegion(const SkRegion& region);
139 void addText(const void* text, size_t byteLength);
140 void addTextBlob(const SkTextBlob* blob);
142 int find(const SkBitmap& bitmap);
145 void validate(size_t initialOffset, size_t size) const {
146 SkASSERT(fWriter.bytesWritten() == initialOffset + size);
149 SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
150 const void* onPeekPixels(SkImageInfo*, size_t*) override {
154 void willSave() override;
155 SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) override;
156 void willRestore() override;
158 void didConcat(const SkMatrix&) override;
159 void didSetMatrix(const SkMatrix&) override;
161 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
163 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
164 const SkPaint&) override;
165 virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
166 const SkPaint&) override;
167 virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
168 SkScalar constY, const SkPaint&) override;
169 virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
170 const SkMatrix* matrix, const SkPaint&) override;
171 virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
172 const SkPaint& paint) override;
174 virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
175 const SkPoint texCoords[4], SkXfermode* xmode,
176 const SkPaint& paint) override;
178 void onDrawPaint(const SkPaint&) override;
179 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
180 void onDrawRect(const SkRect&, const SkPaint&) override;
181 void onDrawOval(const SkRect&, const SkPaint&) override;
182 void onDrawRRect(const SkRRect&, const SkPaint&) override;
183 void onDrawPath(const SkPath&, const SkPaint&) override;
184 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
185 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
186 DrawBitmapRectFlags flags) override;
188 // rely on conversion to bitmap (for now)
189 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
190 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
191 const SkPaint*) override;
193 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
194 const SkPaint*) override;
195 void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override;
196 void onDrawVertices(VertexMode vmode, int vertexCount,
197 const SkPoint vertices[], const SkPoint texs[],
198 const SkColor colors[], SkXfermode* xmode,
199 const uint16_t indices[], int indexCount,
200 const SkPaint&) override;
202 void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
203 void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
204 void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
205 void onClipRegion(const SkRegion&, SkRegion::Op) override;
207 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
209 int addPathToHeap(const SkPath& path); // does not write to ops stream
211 // These entry points allow the writing of matrices, clips, saves &
212 // restores to be deferred (e.g., if the MC state is being collapsed and
213 // only written out as needed).
214 void recordConcat(const SkMatrix& matrix);
215 void recordTranslate(const SkMatrix& matrix);
216 void recordScale(const SkMatrix& matrix);
217 size_t recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
218 size_t recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
219 size_t recordClipPath(int pathID, SkRegion::Op op, bool doAA);
220 size_t recordClipRegion(const SkRegion& region, SkRegion::Op op);
222 void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
223 void recordRestore(bool fillInSkips = true);
226 SkPictureContentInfo fContentInfo;
228 SkTArray<SkBitmap> fBitmaps;
229 SkTArray<SkPaint> fPaints;
230 SkTArray<SkPath> fPaths;
234 // we ref each item in these arrays
235 SkTDArray<const SkPicture*> fPictureRefs;
236 SkTDArray<const SkTextBlob*> fTextBlobRefs;
238 uint32_t fRecordFlags;
239 int fInitialSaveCount;
241 friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor
243 typedef SkCanvas INHERITED;