SkPictureRecord.h revision ce8343d7dde40e509317708de239be1fa72b28f9
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*) SK_OVERRIDE;
63    virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
64                                  const SkPaint*) SK_OVERRIDE;
65    virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
66                                const SkRect& dst, const SkPaint*) SK_OVERRIDE;
67    virtual void drawSprite(const SkBitmap&, int left, int top,
68                            const SkPaint*) SK_OVERRIDE;
69    virtual void drawText(const void* text, size_t byteLength, SkScalar x,
70                          SkScalar y, const SkPaint&) SK_OVERRIDE;
71    virtual void drawPosText(const void* text, size_t byteLength,
72                             const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
73    virtual void drawPosTextH(const void* text, size_t byteLength,
74                      const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
75    virtual void drawTextOnPath(const void* text, size_t byteLength,
76                            const SkPath& path, const SkMatrix* matrix,
77                                const SkPaint&) SK_OVERRIDE;
78    virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
79    virtual void drawVertices(VertexMode, int vertexCount,
80                          const SkPoint vertices[], const SkPoint texs[],
81                          const SkColor colors[], SkXfermode*,
82                          const uint16_t indices[], int indexCount,
83                              const SkPaint&) SK_OVERRIDE;
84    virtual void drawData(const void*, size_t) SK_OVERRIDE;
85    virtual bool isDrawingToLayer() const SK_OVERRIDE;
86
87    void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&,
88                                 SkScalar minY, SkScalar maxY);
89
90    const SkTDArray<SkPicture* >& getPictureRefs() const {
91        return fPictureRefs;
92    }
93
94    void setFlags(uint32_t recordFlags) {
95        fRecordFlags = recordFlags;
96    }
97
98    const SkWriter32& writeStream() const {
99        return fWriter;
100    }
101
102    void beginRecording();
103    void endRecording();
104
105private:
106    void recordRestoreOffsetPlaceholder(SkRegion::Op);
107    void fillRestoreOffsetPlaceholdersForCurrentStackLevel(
108        uint32_t restoreOffset);
109
110    SkTDArray<int32_t> fRestoreOffsetStack;
111    int fFirstSavedLayerIndex;
112    enum {
113        kNoSavedLayerIndex = -1
114    };
115
116    /*
117     * Write the 'drawType' operation and chunk size to the skp. 'size'
118     * can potentially be increased if the chunk size needs its own storage
119     * location (i.e., it overflows 24 bits).
120     * Returns the start offset of the chunk. This is the location at which
121     * the opcode & size are stored.
122     * TODO: since we are handing the size into here we could call reserve
123     * and then return a pointer to the memory storage. This could decrease
124     * allocation overhead but could lead to more wasted space (the tail
125     * end of blocks could go unused). Possibly add a second addDraw that
126     * operates in this manner.
127     */
128    uint32_t addDraw(DrawType drawType, uint32_t* size) {
129        uint32_t offset = fWriter.size();
130
131        this->predrawNotify();
132
133    #ifdef SK_DEBUG_TRACE
134        SkDebugf("add %s\n", DrawTypeToString(drawType));
135    #endif
136
137        SkASSERT(0 != *size);
138        SkASSERT(((uint8_t) drawType) == drawType);
139
140        if (0 != (*size & ~MASK_24) || *size == MASK_24) {
141            fWriter.writeInt(PACK_8_24(drawType, MASK_24));
142            *size += 1;
143            fWriter.writeInt(*size);
144        } else {
145            fWriter.writeInt(PACK_8_24(drawType, *size));
146        }
147
148        return offset;
149    }
150
151    void addInt(int value) {
152        fWriter.writeInt(value);
153    }
154    void addScalar(SkScalar scalar) {
155        fWriter.writeScalar(scalar);
156    }
157
158    void addBitmap(const SkBitmap& bitmap);
159    void addMatrix(const SkMatrix& matrix);
160    void addMatrixPtr(const SkMatrix* matrix);
161    const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
162    const SkFlatData* addPaintPtr(const SkPaint* paint);
163    void addPath(const SkPath& path);
164    void addPicture(SkPicture& picture);
165    void addPoint(const SkPoint& point);
166    void addPoints(const SkPoint pts[], int count);
167    void addRect(const SkRect& rect);
168    void addRectPtr(const SkRect* rect);
169    void addIRect(const SkIRect& rect);
170    void addIRectPtr(const SkIRect* rect);
171    void addRRect(const SkRRect&);
172    void addRegion(const SkRegion& region);
173    void addText(const void* text, size_t byteLength);
174
175    int find(const SkBitmap& bitmap);
176
177#ifdef SK_DEBUG_DUMP
178public:
179    void dumpMatrices();
180    void dumpPaints();
181#endif
182
183#ifdef SK_DEBUG_SIZE
184public:
185    size_t size() const;
186    int bitmaps(size_t* size) const;
187    int matrices(size_t* size) const;
188    int paints(size_t* size) const;
189    int paths(size_t* size) const;
190    int regions(size_t* size) const;
191    size_t streamlen() const;
192
193    size_t fPointBytes, fRectBytes, fTextBytes;
194    int fPointWrites, fRectWrites, fTextWrites;
195#endif
196
197#ifdef SK_DEBUG_VALIDATE
198public:
199    void validate(uint32_t initialOffset, uint32_t size) const;
200private:
201    void validateBitmaps() const;
202    void validateMatrices() const;
203    void validatePaints() const;
204    void validatePaths() const;
205    void validateRegions() const;
206#else
207public:
208    void validate(uint32_t initialOffset, uint32_t size) const {
209        SkASSERT(fWriter.size() == initialOffset + size);
210    }
211#endif
212
213protected:
214
215    // These are set to NULL in our constructor, but may be changed by
216    // subclasses, in which case they will be SkSafeUnref'd in our destructor.
217    SkBBoxHierarchy* fBoundingHierarchy;
218    SkPictureStateTree* fStateTree;
219
220    // Allocated in the constructor and managed by this class.
221    SkBitmapHeap* fBitmapHeap;
222
223private:
224    SkChunkFlatController fFlattenableHeap;
225
226    SkMatrixDictionary fMatrices;
227    SkPaintDictionary fPaints;
228    SkRegionDictionary fRegions;
229
230    SkPathHeap* fPathHeap;  // reference counted
231    SkWriter32 fWriter;
232
233    // we ref each item in these arrays
234    SkTDArray<SkPicture*> fPictureRefs;
235
236    uint32_t fRecordFlags;
237    int fInitialSaveCount;
238
239    friend class SkPicturePlayback;
240    friend class SkPictureTester; // for unit testing
241
242    typedef SkCanvas INHERITED;
243};
244
245#endif
246