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 "SkTArray.h"
16#include "SkTDArray.h"
17#include "SkTHash.h"
18#include "SkVertices.h"
19#include "SkWriter32.h"
20
21// These macros help with packing and unpacking a single byte value and
22// a 3 byte value into/out of a uint32_t
23#define MASK_24 0x00FFFFFF
24#define UNPACK_8_24(combined, small, large)             \
25    small = (combined >> 24) & 0xFF;                    \
26    large = combined & MASK_24;
27#define PACK_8_24(small, large) ((small << 24) | large)
28
29
30class SkPictureRecord : public SkCanvas {
31public:
32    SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
33    ~SkPictureRecord() override;
34
35    const SkTDArray<const SkPicture* >& getPictureRefs() const {
36        return fPictureRefs;
37    }
38
39    const SkTDArray<SkDrawable* >& getDrawableRefs() const {
40        return fDrawableRefs;
41    }
42
43    const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const {
44        return fTextBlobRefs;
45    }
46
47    const SkTDArray<const SkVertices* >& getVerticesRefs() const {
48        return fVerticesRefs;
49    }
50
51   const SkTDArray<const SkImage* >& getImageRefs() const {
52        return fImageRefs;
53    }
54
55    sk_sp<SkData> opData() const {
56        this->validate(fWriter.bytesWritten(), 0);
57
58        if (fWriter.bytesWritten() == 0) {
59            return SkData::MakeEmpty();
60        }
61        return fWriter.snapshotAsData();
62    }
63
64    void setFlags(uint32_t recordFlags) {
65        fRecordFlags = recordFlags;
66    }
67
68    const SkWriter32& writeStream() const {
69        return fWriter;
70    }
71
72    void beginRecording();
73    void endRecording();
74
75protected:
76    void addNoOp();
77
78private:
79    void handleOptimization(int opt);
80    size_t recordRestoreOffsetPlaceholder(SkClipOp);
81    void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
82
83    SkTDArray<int32_t> fRestoreOffsetStack;
84
85    SkTDArray<uint32_t> fCullOffsetStack;
86
87    /*
88     * Write the 'drawType' operation and chunk size to the skp. 'size'
89     * can potentially be increased if the chunk size needs its own storage
90     * location (i.e., it overflows 24 bits).
91     * Returns the start offset of the chunk. This is the location at which
92     * the opcode & size are stored.
93     * TODO: since we are handing the size into here we could call reserve
94     * and then return a pointer to the memory storage. This could decrease
95     * allocation overhead but could lead to more wasted space (the tail
96     * end of blocks could go unused). Possibly add a second addDraw that
97     * operates in this manner.
98     */
99    size_t addDraw(DrawType drawType, size_t* size) {
100        size_t offset = fWriter.bytesWritten();
101
102        this->predrawNotify();
103
104        SkASSERT(0 != *size);
105        SkASSERT(((uint8_t) drawType) == drawType);
106
107        if (0 != (*size & ~MASK_24) || *size == MASK_24) {
108            fWriter.writeInt(PACK_8_24(drawType, MASK_24));
109            *size += 1;
110            fWriter.writeInt(SkToU32(*size));
111        } else {
112            fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
113        }
114
115        return offset;
116    }
117
118    void addInt(int value) {
119        fWriter.writeInt(value);
120    }
121    void addScalar(SkScalar scalar) {
122        fWriter.writeScalar(scalar);
123    }
124
125    void addImage(const SkImage*);
126    void addMatrix(const SkMatrix& matrix);
127    void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
128    void addPaintPtr(const SkPaint* paint);
129    void addPatch(const SkPoint cubics[12]);
130    void addPath(const SkPath& path);
131    void addPicture(const SkPicture* picture);
132    void addDrawable(SkDrawable* picture);
133    void addPoint(const SkPoint& point);
134    void addPoints(const SkPoint pts[], int count);
135    void addRect(const SkRect& rect);
136    void addRectPtr(const SkRect* rect);
137    void addIRect(const SkIRect& rect);
138    void addIRectPtr(const SkIRect* rect);
139    void addRRect(const SkRRect&);
140    void addRegion(const SkRegion& region);
141    void addText(const void* text, size_t byteLength);
142    void addTextBlob(const SkTextBlob* blob);
143    void addVertices(const SkVertices*);
144
145    int find(const SkBitmap& bitmap);
146
147protected:
148    void validate(size_t initialOffset, size_t size) const {
149        SkASSERT(fWriter.bytesWritten() == initialOffset + size);
150    }
151
152    sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
153    bool onPeekPixels(SkPixmap*) override { return false; }
154
155    void onFlush() override;
156
157    void willSave() override;
158    SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
159    void willRestore() override;
160
161    void didConcat(const SkMatrix&) override;
162    void didSetMatrix(const SkMatrix&) override;
163
164    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
165
166    void onDrawText(const void* text, size_t, SkScalar x, SkScalar y, const SkPaint&) override;
167    void onDrawPosText(const void* text, size_t, const SkPoint pos[], const SkPaint&) override;
168    void onDrawPosTextH(const void* text, size_t, const SkScalar xpos[], SkScalar constY,
169                        const SkPaint&) override;
170    void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
171                                  const SkMatrix* matrix, const SkPaint&) override;
172    void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
173                           const SkRect* cull, const SkPaint&) override;
174    void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
175                                const SkPaint& paint) override;
176
177    void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
178                     const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
179    void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
180                     SkBlendMode, const SkRect*, const SkPaint*) override;
181
182    void onDrawPaint(const SkPaint&) override;
183    void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
184    void onDrawRect(const SkRect&, const SkPaint&) override;
185    void onDrawRegion(const SkRegion&, const SkPaint&) override;
186    void onDrawOval(const SkRect&, const SkPaint&) override;
187    void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
188    void onDrawRRect(const SkRRect&, const SkPaint&) override;
189    void onDrawPath(const SkPath&, const SkPaint&) override;
190    void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
191    void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
192                         const SkPaint*, SrcRectConstraint) override;
193    void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
194                         const SkPaint*) override;
195    void onDrawImageLattice(const SkImage*, const SkCanvas::Lattice& lattice, const SkRect& dst,
196                            const SkPaint*) override;
197    void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
198    void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
199
200    void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
201    void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
202    void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
203    void onClipRegion(const SkRegion&, SkClipOp) override;
204
205    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
206
207    void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
208    void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
209
210    int addPathToHeap(const SkPath& path);  // does not write to ops stream
211
212    // These entry points allow the writing of matrices, clips, saves &
213    // restores to be deferred (e.g., if the MC state is being collapsed and
214    // only written out as needed).
215    void recordConcat(const SkMatrix& matrix);
216    void recordTranslate(const SkMatrix& matrix);
217    void recordScale(const SkMatrix& matrix);
218    size_t recordClipRect(const SkRect& rect, SkClipOp op, bool doAA);
219    size_t recordClipRRect(const SkRRect& rrect, SkClipOp op, bool doAA);
220    size_t recordClipPath(int pathID, SkClipOp op, bool doAA);
221    size_t recordClipRegion(const SkRegion& region, SkClipOp op);
222    void recordSave();
223    void recordSaveLayer(const SaveLayerRec&);
224    void recordRestore(bool fillInSkips = true);
225
226    // SHOULD NEVER BE CALLED
227    void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override {
228        SK_ABORT("not reached");
229    }
230    void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
231                          SrcRectConstraint) override {
232        SK_ABORT("not reached");
233    }
234    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
235                          const SkPaint*) override {
236        SK_ABORT("not reached");
237    }
238    void onDrawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice& lattice, const SkRect& dst,
239                             const SkPaint*) override {
240        SK_ABORT("not reached");
241    }
242
243private:
244    SkTArray<SkPaint>  fPaints;
245
246    struct PathHash {
247        uint32_t operator()(const SkPath& p) { return p.getGenerationID(); }
248    };
249    SkTHashMap<SkPath, int, PathHash> fPaths;
250
251    SkWriter32 fWriter;
252
253    // we ref each item in these arrays
254    SkTDArray<const SkImage*>    fImageRefs;
255    SkTDArray<const SkPicture*>  fPictureRefs;
256    SkTDArray<SkDrawable*>       fDrawableRefs;
257    SkTDArray<const SkTextBlob*> fTextBlobRefs;
258    SkTDArray<const SkVertices*> fVerticesRefs;
259
260    uint32_t fRecordFlags;
261    int      fInitialSaveCount;
262
263    friend class SkPictureData;   // for SkPictureData's SkPictureRecord-based constructor
264
265    typedef SkCanvas INHERITED;
266};
267
268#endif
269