SkPictureRecord.h revision 06dad4b92f1fcb593bc686f9537d3e0d866b2241
1/*
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 */
7
8#ifndef SkPictureRecord_DEFINED
9#define SkPictureRecord_DEFINED
10
11#include "SkCanvas.h"
12#include "SkFlattenable.h"
13#include "SkPicture.h"
14#include "SkPictureData.h"
15#include "SkTemplates.h"
16#include "SkWriter32.h"
17
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)
25
26
27class SkPictureRecord : public SkCanvas {
28public:
29    SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
30    virtual ~SkPictureRecord();
31
32    void beginCommentGroup(const char* description) override;
33    void addComment(const char* kywd, const char* value) override;
34    void endCommentGroup() override;
35
36    const SkTDArray<const SkPicture* >& getPictureRefs() const {
37        return fPictureRefs;
38    }
39
40    const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const {
41        return fTextBlobRefs;
42    }
43
44    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
58    const SkPictureContentInfo& contentInfo() const {
59        return fContentInfo;
60    }
61
62    void setFlags(uint32_t recordFlags) {
63        fRecordFlags = recordFlags;
64    }
65
66    const SkWriter32& writeStream() const {
67        return fWriter;
68    }
69
70    void beginRecording();
71    void endRecording();
72
73protected:
74    void addNoOp();
75
76private:
77    void handleOptimization(int opt);
78    size_t recordRestoreOffsetPlaceholder(SkRegion::Op);
79    void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
80
81    SkTDArray<int32_t> fRestoreOffsetStack;
82
83    SkTDArray<uint32_t> fCullOffsetStack;
84
85    /*
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.
96     */
97    size_t addDraw(DrawType drawType, size_t* size) {
98        size_t offset = fWriter.bytesWritten();
99
100        this->predrawNotify();
101        fContentInfo.addOperation();
102
103        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;
109            fWriter.writeInt(SkToU32(*size));
110        } else {
111            fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
112        }
113
114        return offset;
115    }
116
117    void addInt(int value) {
118        fWriter.writeInt(value);
119    }
120    void addScalar(SkScalar scalar) {
121        fWriter.writeScalar(scalar);
122    }
123
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);
141
142    int find(const SkBitmap& bitmap);
143
144protected:
145    void validate(size_t initialOffset, size_t size) const {
146        SkASSERT(fWriter.bytesWritten() == initialOffset + size);
147    }
148
149    SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
150    const void* onPeekPixels(SkImageInfo*, size_t*) override {
151        return NULL;
152    }
153
154    void willSave() override;
155    SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) override;
156    void willRestore() override;
157
158    void didConcat(const SkMatrix&) override;
159    void didSetMatrix(const SkMatrix&) override;
160
161    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
162
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;
173
174    virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
175                             const SkPoint texCoords[4], SkXfermode* xmode,
176                             const SkPaint& paint) override;
177
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;
187#if 0
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;
192#endif
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;
201
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;
206
207    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
208
209    int addPathToHeap(const SkPath& path);  // does not write to ops stream
210
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);
221    void recordSave();
222    void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
223    void recordRestore(bool fillInSkips = true);
224
225private:
226    SkPictureContentInfo fContentInfo;
227
228    SkTArray<SkBitmap> fBitmaps;
229    SkTArray<SkPaint>  fPaints;
230    SkTArray<SkPath>   fPaths;
231
232    SkWriter32 fWriter;
233
234    // we ref each item in these arrays
235    SkTDArray<const SkPicture*>  fPictureRefs;
236    SkTDArray<const SkTextBlob*> fTextBlobRefs;
237
238    uint32_t fRecordFlags;
239    int      fInitialSaveCount;
240
241    friend class SkPictureData;   // for SkPictureData's SkPictureRecord-based constructor
242
243    typedef SkCanvas INHERITED;
244};
245
246#endif
247