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    void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
188    void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
189                         const SkPaint*) override;
190    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
191                          const SkPaint*) override;
192    void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override;
193    void onDrawVertices(VertexMode vmode, int vertexCount,
194                        const SkPoint vertices[], const SkPoint texs[],
195                        const SkColor colors[], SkXfermode* xmode,
196                        const uint16_t indices[], int indexCount,
197                        const SkPaint&) override;
198
199    void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
200    void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
201    void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
202    void onClipRegion(const SkRegion&, SkRegion::Op) override;
203
204    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
205
206    int addPathToHeap(const SkPath& path);  // does not write to ops stream
207
208    // These entry points allow the writing of matrices, clips, saves &
209    // restores to be deferred (e.g., if the MC state is being collapsed and
210    // only written out as needed).
211    void recordConcat(const SkMatrix& matrix);
212    void recordTranslate(const SkMatrix& matrix);
213    void recordScale(const SkMatrix& matrix);
214    size_t recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
215    size_t recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
216    size_t recordClipPath(int pathID, SkRegion::Op op, bool doAA);
217    size_t recordClipRegion(const SkRegion& region, SkRegion::Op op);
218    void recordSave();
219    void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
220    void recordRestore(bool fillInSkips = true);
221
222private:
223    SkPictureContentInfo fContentInfo;
224
225    SkTArray<SkBitmap> fBitmaps;
226    SkTArray<SkPaint>  fPaints;
227    SkTArray<SkPath>   fPaths;
228
229    SkWriter32 fWriter;
230
231    // we ref each item in these arrays
232    SkTDArray<const SkPicture*>  fPictureRefs;
233    SkTDArray<const SkTextBlob*> fTextBlobRefs;
234
235    uint32_t fRecordFlags;
236    int      fInitialSaveCount;
237
238    friend class SkPictureData;   // for SkPictureData's SkPictureRecord-based constructor
239
240    typedef SkCanvas INHERITED;
241};
242
243#endif
244