SkPictureRecord.h revision f3c15b7cfc4eed2528f7db87ea6c1444b55ee856
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 "SkWriter32.h"
18
19// These macros help with packing and unpacking a single byte value and
20// a 3 byte value into/out of a uint32_t
21#define MASK_24 0x00FFFFFF
22#define UNPACK_8_24(combined, small, large)             \
23    small = (combined >> 24) & 0xFF;                    \
24    large = combined & MASK_24;
25#define PACK_8_24(small, large) ((small << 24) | large)
26
27
28class SkPictureRecord : public SkCanvas {
29public:
30    SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
31    virtual ~SkPictureRecord();
32
33    const SkTDArray<const SkPicture* >& getPictureRefs() const {
34        return fPictureRefs;
35    }
36
37    const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const {
38        return fTextBlobRefs;
39    }
40
41    const SkTDArray<const SkImage* >& getImageRefs() const {
42        return fImageRefs;
43    }
44
45    SkData* opData(bool deepCopy) const {
46        this->validate(fWriter.bytesWritten(), 0);
47
48        if (fWriter.bytesWritten() == 0) {
49            return SkData::NewEmpty();
50        }
51
52        if (deepCopy) {
53            return SkData::NewWithCopy(fWriter.contiguousArray(), fWriter.bytesWritten());
54        }
55
56        return fWriter.snapshotAsData();
57    }
58
59    const SkPictureContentInfo& contentInfo() const {
60        return fContentInfo;
61    }
62
63    void setFlags(uint32_t recordFlags) {
64        fRecordFlags = recordFlags;
65    }
66
67    const SkWriter32& writeStream() const {
68        return fWriter;
69    }
70
71    void beginRecording();
72    void endRecording();
73
74protected:
75    void addNoOp();
76
77private:
78    void handleOptimization(int opt);
79    size_t recordRestoreOffsetPlaceholder(SkRegion::Op);
80    void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
81
82    SkTDArray<int32_t> fRestoreOffsetStack;
83
84    SkTDArray<uint32_t> fCullOffsetStack;
85
86    /*
87     * Write the 'drawType' operation and chunk size to the skp. 'size'
88     * can potentially be increased if the chunk size needs its own storage
89     * location (i.e., it overflows 24 bits).
90     * Returns the start offset of the chunk. This is the location at which
91     * the opcode & size are stored.
92     * TODO: since we are handing the size into here we could call reserve
93     * and then return a pointer to the memory storage. This could decrease
94     * allocation overhead but could lead to more wasted space (the tail
95     * end of blocks could go unused). Possibly add a second addDraw that
96     * operates in this manner.
97     */
98    size_t addDraw(DrawType drawType, size_t* size) {
99        size_t offset = fWriter.bytesWritten();
100
101        this->predrawNotify();
102        fContentInfo.addOperation();
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 addBitmap(const SkBitmap& bitmap);
126    void addImage(const SkImage*);
127    void addMatrix(const SkMatrix& matrix);
128    void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
129    void addPaintPtr(const SkPaint* paint);
130    void addPatch(const SkPoint cubics[12]);
131    void addPath(const SkPath& path);
132    void addPicture(const SkPicture* 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
144    int find(const SkBitmap& bitmap);
145
146protected:
147    void validate(size_t initialOffset, size_t size) const {
148        SkASSERT(fWriter.bytesWritten() == initialOffset + size);
149    }
150
151    SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
152    bool onPeekPixels(SkPixmap*) override { return false; }
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    void onDrawText(const void* text, size_t, SkScalar x, SkScalar y, const SkPaint&) override;
164    void onDrawPosText(const void* text, size_t, const SkPoint pos[], const SkPaint&) override;
165    void onDrawPosTextH(const void* text, size_t, const SkScalar xpos[], SkScalar constY,
166                        const SkPaint&) override;
167    void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
168                                  const SkMatrix* matrix, const SkPaint&) override;
169    void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
170                                const SkPaint& paint) override;
171
172    void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
173                             const SkPoint texCoords[4], SkXfermode* xmode,
174                             const SkPaint& paint) override;
175    void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
176                     SkXfermode::Mode, const SkRect*, const SkPaint*) 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                          SrcRectConstraint) 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*, SrcRectConstraint) override;
190    void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
191                         const SkPaint*) override;
192    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
193                          const SkPaint*) override;
194    void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override;
195    void onDrawVertices(VertexMode vmode, int vertexCount,
196                        const SkPoint vertices[], const SkPoint texs[],
197                        const SkColor colors[], SkXfermode* xmode,
198                        const uint16_t indices[], int indexCount,
199                        const SkPaint&) override;
200
201    void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
202    void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
203    void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
204    void onClipRegion(const SkRegion&, SkRegion::Op) override;
205
206    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
207
208    int addPathToHeap(const SkPath& path);  // does not write to ops stream
209
210    // These entry points allow the writing of matrices, clips, saves &
211    // restores to be deferred (e.g., if the MC state is being collapsed and
212    // only written out as needed).
213    void recordConcat(const SkMatrix& matrix);
214    void recordTranslate(const SkMatrix& matrix);
215    void recordScale(const SkMatrix& matrix);
216    size_t recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
217    size_t recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
218    size_t recordClipPath(int pathID, SkRegion::Op op, bool doAA);
219    size_t recordClipRegion(const SkRegion& region, SkRegion::Op op);
220    void recordSave();
221    void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
222    void recordRestore(bool fillInSkips = true);
223
224private:
225    SkPictureContentInfo fContentInfo;
226
227    SkTArray<SkBitmap> fBitmaps;
228    SkTArray<SkPaint>  fPaints;
229    SkTArray<SkPath>   fPaths;
230
231    SkWriter32 fWriter;
232
233    // we ref each item in these arrays
234    SkTDArray<const SkImage*>    fImageRefs;
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