SkPictureRecord.h revision 80b10518a27a47b25a7dbf3591b425a741ebf406
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#ifdef SK_COLLAPSE_MATRIX_CLIP_STATE
14#include "SkMatrixClipStateMgr.h"
15#endif
16#include "SkPathHeap.h"
17#include "SkPicture.h"
18#include "SkPictureFlat.h"
19#include "SkTemplates.h"
20#include "SkWriter32.h"
21
22class SkBBoxHierarchy;
23class SkOffsetTable;
24class SkPictureStateTree;
25
26// These macros help with packing and unpacking a single byte value and
27// a 3 byte value into/out of a uint32_t
28#define MASK_24 0x00FFFFFF
29#define UNPACK_8_24(combined, small, large)             \
30    small = (combined >> 24) & 0xFF;                    \
31    large = combined & MASK_24;
32#define PACK_8_24(small, large) ((small << 24) | large)
33
34
35class SkPictureRecord : public SkCanvas {
36public:
37    SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
38    virtual ~SkPictureRecord();
39
40    virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
41    virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
42    virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
43    virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
44    virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
45    virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
46    virtual void clear(SkColor) SK_OVERRIDE;
47    virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
48    virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
49                            const SkPaint&) SK_OVERRIDE;
50    virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
51    virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
52    virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
53    virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
54    virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
55                            const SkPaint*) SK_OVERRIDE;
56    virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
57                                      const SkRect& dst, const SkPaint* paint,
58                                      DrawBitmapRectFlags flags) SK_OVERRIDE;
59    virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
60                                  const SkPaint*) SK_OVERRIDE;
61    virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
62                                const SkRect& dst, const SkPaint*) SK_OVERRIDE;
63    virtual void drawSprite(const SkBitmap&, int left, int top,
64                            const SkPaint*) SK_OVERRIDE;
65    virtual void drawText(const void* text, size_t byteLength, SkScalar x,
66                          SkScalar y, const SkPaint&) SK_OVERRIDE;
67    virtual void drawPosText(const void* text, size_t byteLength,
68                             const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
69    virtual void drawPosTextH(const void* text, size_t byteLength,
70                      const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
71    virtual void drawTextOnPath(const void* text, size_t byteLength,
72                            const SkPath& path, const SkMatrix* matrix,
73                                const SkPaint&) SK_OVERRIDE;
74    virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
75    virtual void drawVertices(VertexMode, int vertexCount,
76                          const SkPoint vertices[], const SkPoint texs[],
77                          const SkColor colors[], SkXfermode*,
78                          const uint16_t indices[], int indexCount,
79                              const SkPaint&) SK_OVERRIDE;
80    virtual void drawData(const void*, size_t) SK_OVERRIDE;
81    virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
82    virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
83    virtual void endCommentGroup() SK_OVERRIDE;
84    virtual bool isDrawingToLayer() const SK_OVERRIDE;
85
86    void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&,
87                                 SkScalar minY, SkScalar maxY);
88
89    const SkTDArray<SkPicture* >& getPictureRefs() const {
90        return fPictureRefs;
91    }
92
93    void setFlags(uint32_t recordFlags) {
94        fRecordFlags = recordFlags;
95    }
96
97    const SkWriter32& writeStream() const {
98        return fWriter;
99    }
100
101    void beginRecording();
102    void endRecording();
103
104    void internalOnly_EnableOpts(bool optsEnabled) {
105        fOptsEnabled = optsEnabled;
106    }
107
108private:
109    void handleOptimization(int opt);
110    int recordRestoreOffsetPlaceholder(SkRegion::Op);
111    void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
112
113#ifndef SK_COLLAPSE_MATRIX_CLIP_STATE
114    SkTDArray<int32_t> fRestoreOffsetStack;
115    int fFirstSavedLayerIndex;
116    enum {
117        kNoSavedLayerIndex = -1
118    };
119#endif
120
121    SkTDArray<uint32_t> fCullOffsetStack;
122
123    /*
124     * Write the 'drawType' operation and chunk size to the skp. 'size'
125     * can potentially be increased if the chunk size needs its own storage
126     * location (i.e., it overflows 24 bits).
127     * Returns the start offset of the chunk. This is the location at which
128     * the opcode & size are stored.
129     * TODO: since we are handing the size into here we could call reserve
130     * and then return a pointer to the memory storage. This could decrease
131     * allocation overhead but could lead to more wasted space (the tail
132     * end of blocks could go unused). Possibly add a second addDraw that
133     * operates in this manner.
134     */
135    size_t addDraw(DrawType drawType, uint32_t* size) {
136        size_t offset = fWriter.bytesWritten();
137
138        this->predrawNotify();
139
140    #ifdef SK_DEBUG_TRACE
141        SkDebugf("add %s\n", DrawTypeToString(drawType));
142    #endif
143
144        SkASSERT(0 != *size);
145        SkASSERT(((uint8_t) drawType) == drawType);
146
147        if (0 != (*size & ~MASK_24) || *size == MASK_24) {
148            fWriter.writeInt(PACK_8_24(drawType, MASK_24));
149            *size += 1;
150            fWriter.writeInt(*size);
151        } else {
152            fWriter.writeInt(PACK_8_24(drawType, *size));
153        }
154
155        return offset;
156    }
157
158    void addInt(int value) {
159        fWriter.writeInt(value);
160    }
161    void addScalar(SkScalar scalar) {
162        fWriter.writeScalar(scalar);
163    }
164
165    // The command at 'offset' in the skp uses the specified bitmap
166    void trackBitmapUse(int bitmapID, size_t offset);
167    int addBitmap(const SkBitmap& bitmap);
168    void addMatrix(const SkMatrix& matrix);
169    const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
170    const SkFlatData* addPaintPtr(const SkPaint* paint);
171    void addFlatPaint(const SkFlatData* flatPaint);
172    void addPath(const SkPath& path);
173    void addPicture(SkPicture& picture);
174    void addPoint(const SkPoint& point);
175    void addPoints(const SkPoint pts[], int count);
176    void addRect(const SkRect& rect);
177    void addRectPtr(const SkRect* rect);
178    void addIRect(const SkIRect& rect);
179    void addIRectPtr(const SkIRect* rect);
180    void addRRect(const SkRRect&);
181    void addRegion(const SkRegion& region);
182    void addText(const void* text, size_t byteLength);
183
184    int find(const SkBitmap& bitmap);
185
186#ifdef SK_DEBUG_DUMP
187public:
188    void dumpMatrices();
189    void dumpPaints();
190#endif
191
192#ifdef SK_DEBUG_SIZE
193public:
194    size_t size() const;
195    int bitmaps(size_t* size) const;
196    int matrices(size_t* size) const;
197    int paints(size_t* size) const;
198    int paths(size_t* size) const;
199    int regions(size_t* size) const;
200    size_t streamlen() const;
201
202    size_t fPointBytes, fRectBytes, fTextBytes;
203    int fPointWrites, fRectWrites, fTextWrites;
204#endif
205
206#ifdef SK_DEBUG_VALIDATE
207public:
208    void validate(size_t initialOffset, uint32_t size) const;
209private:
210    void validateBitmaps() const;
211    void validateMatrices() const;
212    void validatePaints() const;
213    void validatePaths() const;
214    void validateRegions() const;
215#else
216public:
217    void validate(size_t initialOffset, uint32_t size) const {
218        SkASSERT(fWriter.bytesWritten() == initialOffset + size);
219    }
220#endif
221
222protected:
223    virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE;
224    const void* onPeekPixels(SkImageInfo*, size_t*) SK_OVERRIDE {
225        return NULL;
226    }
227
228    virtual void onSave(SaveFlags) SK_OVERRIDE;
229    virtual bool onSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
230    virtual void onRestore() SK_OVERRIDE;
231
232    virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
233    virtual void onPushCull(const SkRect&) SK_OVERRIDE;
234    virtual void onPopCull() SK_OVERRIDE;
235
236    virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
237    virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
238    virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
239    virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
240
241    // Return fontmetrics.fTop,fBottom in topbot[0,1], after they have been
242    // tweaked by paint.computeFastBounds().
243    static void ComputeFontMetricsTopBottom(const SkPaint& paint, SkScalar topbot[2]);
244
245    // Make sure that flat has fTopBot written.
246    static void WriteTopBot(const SkPaint& paint, const SkFlatData& flat) {
247        if (!flat.isTopBotWritten()) {
248            ComputeFontMetricsTopBottom(paint, flat.writableTopBot());
249            SkASSERT(flat.isTopBotWritten());
250        }
251    }
252    // Will return a cached version when possible.
253    const SkFlatData* getFlatPaintData(const SkPaint& paint);
254    /**
255     * SkBBoxRecord::drawPosTextH gets a flat paint and uses it,
256     * then it calls this, using the extra parameter, to avoid duplication.
257     */
258    void drawPosTextHImpl(const void* text, size_t byteLength,
259                          const SkScalar xpos[], SkScalar constY,
260                          const SkPaint& paint, const SkFlatData* flatPaintData);
261
262    int addPathToHeap(const SkPath& path);  // does not write to ops stream
263
264    // These entry points allow the writing of matrices, clips, saves &
265    // restores to be deferred (e.g., if the MC state is being collapsed and
266    // only written out as needed).
267    void recordConcat(const SkMatrix& matrix);
268    int recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
269    int recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
270    int recordClipPath(int pathID, SkRegion::Op op, bool doAA);
271    int recordClipRegion(const SkRegion& region, SkRegion::Op op);
272    void recordSave(SaveFlags flags);
273    void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
274    void recordRestore(bool fillInSkips = true);
275
276    // These are set to NULL in our constructor, but may be changed by
277    // subclasses, in which case they will be SkSafeUnref'd in our destructor.
278    SkBBoxHierarchy* fBoundingHierarchy;
279    SkPictureStateTree* fStateTree;
280
281    // Allocated in the constructor and managed by this class.
282    SkBitmapHeap* fBitmapHeap;
283
284private:
285    friend class MatrixClipState; // for access to *Impl methods
286    friend class SkMatrixClipStateMgr; // for access to *Impl methods
287
288    SkChunkFlatController fFlattenableHeap;
289
290    SkPaintDictionary fPaints;
291
292    SkPathHeap* fPathHeap;  // reference counted
293    SkWriter32 fWriter;
294
295    // we ref each item in these arrays
296    SkTDArray<SkPicture*> fPictureRefs;
297
298    uint32_t fRecordFlags;
299    bool     fOptsEnabled;
300    int      fInitialSaveCount;
301
302    SkAutoTUnref<SkOffsetTable> fBitmapUseOffsets;
303
304    friend class SkPicturePlayback;
305    friend class SkPictureTester; // for unit testing
306
307#ifdef SK_COLLAPSE_MATRIX_CLIP_STATE
308    SkMatrixClipStateMgr fMCMgr;
309#endif
310
311    typedef SkCanvas INHERITED;
312};
313
314#endif
315