SkPictureRecord.h revision 9b051a375ba6d6b61cea98f35834cd032aaa5347
1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#ifndef SkPictureRecord_DEFINED
9#define SkPictureRecord_DEFINED
10
11#include "SkCanvas.h"
12#include "SkFlattenable.h"
13#include "SkPathHeap.h"
14#include "SkPicture.h"
15#include "SkPictureFlat.h"
16#include "SkTemplates.h"
17#include "SkWriter32.h"
18
19class SkPictureStateTree;
20class SkBBoxHierarchy;
21
22// These macros help with packing and unpacking a single byte value and
23// a 3 byte value into/out of a uint32_t
24#define MASK_24 0x00FFFFFF
25#define UNPACK_8_24(combined, small, large)             \
26    small = (combined >> 24) & 0xFF;                    \
27    large = combined & MASK_24;
28#define PACK_8_24(small, large) ((small << 24) | large)
29
30
31class SkPictureRecord : public SkCanvas {
32public:
33    SkPictureRecord(uint32_t recordFlags, SkDevice*);
34    virtual ~SkPictureRecord();
35
36    virtual SkDevice* setDevice(SkDevice* device) SK_OVERRIDE;
37
38    virtual int save(SaveFlags) SK_OVERRIDE;
39    virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
40    virtual void restore() SK_OVERRIDE;
41    virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
42    virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
43    virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
44    virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
45    virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
46    virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
47    virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
48    virtual bool clipRRect(const SkRRect&, SkRegion::Op, bool) SK_OVERRIDE;
49    virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
50    virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
51    virtual void clear(SkColor) SK_OVERRIDE;
52    virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
53    virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
54                            const SkPaint&) SK_OVERRIDE;
55    virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
56    virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
57    virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
58    virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
59    virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
60                            const SkPaint*) SK_OVERRIDE;
61    virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
62                                      const SkRect& dst, const SkPaint* paint,
63                                      DrawBitmapRectFlags flags) SK_OVERRIDE;
64    virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
65                                  const SkPaint*) SK_OVERRIDE;
66    virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
67                                const SkRect& dst, const SkPaint*) SK_OVERRIDE;
68    virtual void drawSprite(const SkBitmap&, int left, int top,
69                            const SkPaint*) SK_OVERRIDE;
70    virtual void drawText(const void* text, size_t byteLength, SkScalar x,
71                          SkScalar y, const SkPaint&) SK_OVERRIDE;
72    virtual void drawPosText(const void* text, size_t byteLength,
73                             const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
74    virtual void drawPosTextH(const void* text, size_t byteLength,
75                      const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
76    virtual void drawTextOnPath(const void* text, size_t byteLength,
77                            const SkPath& path, const SkMatrix* matrix,
78                                const SkPaint&) SK_OVERRIDE;
79    virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
80    virtual void drawVertices(VertexMode, int vertexCount,
81                          const SkPoint vertices[], const SkPoint texs[],
82                          const SkColor colors[], SkXfermode*,
83                          const uint16_t indices[], int indexCount,
84                              const SkPaint&) SK_OVERRIDE;
85    virtual void drawData(const void*, size_t) SK_OVERRIDE;
86    virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
87    virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
88    virtual void endCommentGroup() SK_OVERRIDE;
89    virtual bool isDrawingToLayer() const SK_OVERRIDE;
90
91    void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&,
92                                 SkScalar minY, SkScalar maxY);
93
94    const SkTDArray<SkPicture* >& getPictureRefs() const {
95        return fPictureRefs;
96    }
97
98    void setFlags(uint32_t recordFlags) {
99        fRecordFlags = recordFlags;
100    }
101
102    const SkWriter32& writeStream() const {
103        return fWriter;
104    }
105
106    void beginRecording();
107    void endRecording();
108
109private:
110    void handleOptimization(int opt);
111    void recordRestoreOffsetPlaceholder(SkRegion::Op);
112    void fillRestoreOffsetPlaceholdersForCurrentStackLevel(
113        uint32_t restoreOffset);
114
115    SkTDArray<int32_t> fRestoreOffsetStack;
116    int fFirstSavedLayerIndex;
117    enum {
118        kNoSavedLayerIndex = -1
119    };
120
121    /*
122     * Write the 'drawType' operation and chunk size to the skp. 'size'
123     * can potentially be increased if the chunk size needs its own storage
124     * location (i.e., it overflows 24 bits).
125     * Returns the start offset of the chunk. This is the location at which
126     * the opcode & size are stored.
127     * TODO: since we are handing the size into here we could call reserve
128     * and then return a pointer to the memory storage. This could decrease
129     * allocation overhead but could lead to more wasted space (the tail
130     * end of blocks could go unused). Possibly add a second addDraw that
131     * operates in this manner.
132     */
133    uint32_t addDraw(DrawType drawType, uint32_t* size) {
134        uint32_t offset = fWriter.size();
135
136        this->predrawNotify();
137
138    #ifdef SK_DEBUG_TRACE
139        SkDebugf("add %s\n", DrawTypeToString(drawType));
140    #endif
141
142        SkASSERT(0 != *size);
143        SkASSERT(((uint8_t) drawType) == drawType);
144
145        if (0 != (*size & ~MASK_24) || *size == MASK_24) {
146            fWriter.writeInt(PACK_8_24(drawType, MASK_24));
147            *size += 1;
148            fWriter.writeInt(*size);
149        } else {
150            fWriter.writeInt(PACK_8_24(drawType, *size));
151        }
152
153        return offset;
154    }
155
156    void addInt(int value) {
157        fWriter.writeInt(value);
158    }
159    void addScalar(SkScalar scalar) {
160        fWriter.writeScalar(scalar);
161    }
162
163    void addBitmap(const SkBitmap& bitmap);
164    void addMatrix(const SkMatrix& matrix);
165    void addMatrixPtr(const SkMatrix* matrix);
166    const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
167    const SkFlatData* addPaintPtr(const SkPaint* paint);
168    void addPath(const SkPath& path);
169    void addPicture(SkPicture& picture);
170    void addPoint(const SkPoint& point);
171    void addPoints(const SkPoint pts[], int count);
172    void addRect(const SkRect& rect);
173    void addRectPtr(const SkRect* rect);
174    void addIRect(const SkIRect& rect);
175    void addIRectPtr(const SkIRect* rect);
176    void addRRect(const SkRRect&);
177    void addRegion(const SkRegion& region);
178    void addText(const void* text, size_t byteLength);
179
180    int find(const SkBitmap& bitmap);
181
182#ifdef SK_DEBUG_DUMP
183public:
184    void dumpMatrices();
185    void dumpPaints();
186#endif
187
188#ifdef SK_DEBUG_SIZE
189public:
190    size_t size() const;
191    int bitmaps(size_t* size) const;
192    int matrices(size_t* size) const;
193    int paints(size_t* size) const;
194    int paths(size_t* size) const;
195    int regions(size_t* size) const;
196    size_t streamlen() const;
197
198    size_t fPointBytes, fRectBytes, fTextBytes;
199    int fPointWrites, fRectWrites, fTextWrites;
200#endif
201
202#ifdef SK_DEBUG_VALIDATE
203public:
204    void validate(uint32_t initialOffset, uint32_t size) const;
205private:
206    void validateBitmaps() const;
207    void validateMatrices() const;
208    void validatePaints() const;
209    void validatePaths() const;
210    void validateRegions() const;
211#else
212public:
213    void validate(uint32_t initialOffset, uint32_t size) const {
214        SkASSERT(fWriter.size() == initialOffset + size);
215    }
216#endif
217
218protected:
219
220    // These are set to NULL in our constructor, but may be changed by
221    // subclasses, in which case they will be SkSafeUnref'd in our destructor.
222    SkBBoxHierarchy* fBoundingHierarchy;
223    SkPictureStateTree* fStateTree;
224
225    // Allocated in the constructor and managed by this class.
226    SkBitmapHeap* fBitmapHeap;
227
228private:
229    SkChunkFlatController fFlattenableHeap;
230
231    SkMatrixDictionary fMatrices;
232    SkPaintDictionary fPaints;
233    SkRegionDictionary fRegions;
234
235    SkPathHeap* fPathHeap;  // reference counted
236    SkWriter32 fWriter;
237
238    // we ref each item in these arrays
239    SkTDArray<SkPicture*> fPictureRefs;
240
241    uint32_t fRecordFlags;
242    int fInitialSaveCount;
243
244    friend class SkPicturePlayback;
245    friend class SkPictureTester; // for unit testing
246
247    typedef SkCanvas INHERITED;
248};
249
250#endif
251