SkPictureRecord.h revision 25c1408c3da9ca90509b84f21a1161ef40052bd1
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
22class SkPictureRecord : public SkCanvas {
23public:
24    SkPictureRecord(uint32_t recordFlags, SkDevice*);
25    virtual ~SkPictureRecord();
26
27    virtual SkDevice* setDevice(SkDevice* device) SK_OVERRIDE;
28
29    virtual int save(SaveFlags) SK_OVERRIDE;
30    virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
31    virtual void restore() SK_OVERRIDE;
32    virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
33    virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
34    virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
35    virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
36    virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
37    virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
38    virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
39    virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
40    virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
41    virtual void clear(SkColor) SK_OVERRIDE;
42    virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
43    virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
44                            const SkPaint&) SK_OVERRIDE;
45    virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
46    virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
47    virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
48                            const SkPaint*) SK_OVERRIDE;
49    virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
50                                      const SkRect& dst, const SkPaint*) SK_OVERRIDE;
51    virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
52                                  const SkPaint*) SK_OVERRIDE;
53    virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
54                                const SkRect& dst, const SkPaint*) SK_OVERRIDE;
55    virtual void drawSprite(const SkBitmap&, int left, int top,
56                            const SkPaint*) SK_OVERRIDE;
57    virtual void drawText(const void* text, size_t byteLength, SkScalar x,
58                          SkScalar y, const SkPaint&) SK_OVERRIDE;
59    virtual void drawPosText(const void* text, size_t byteLength,
60                             const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
61    virtual void drawPosTextH(const void* text, size_t byteLength,
62                      const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
63    virtual void drawTextOnPath(const void* text, size_t byteLength,
64                            const SkPath& path, const SkMatrix* matrix,
65                                const SkPaint&) SK_OVERRIDE;
66    virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
67    virtual void drawVertices(VertexMode, int vertexCount,
68                          const SkPoint vertices[], const SkPoint texs[],
69                          const SkColor colors[], SkXfermode*,
70                          const uint16_t indices[], int indexCount,
71                              const SkPaint&) SK_OVERRIDE;
72    virtual void drawData(const void*, size_t) SK_OVERRIDE;
73    virtual bool isDrawingToLayer() const SK_OVERRIDE;
74
75    void addFontMetricsTopBottom(const SkPaint& paint, int paintIndex,
76                                 SkScalar minY, SkScalar maxY);
77
78    const SkTDArray<SkPicture* >& getPictureRefs() const {
79        return fPictureRefs;
80    }
81
82    void setFlags(uint32_t recordFlags) {
83        fRecordFlags = recordFlags;
84    }
85
86    const SkWriter32& writeStream() const {
87        return fWriter;
88    }
89
90    void beginRecording();
91    void endRecording();
92
93private:
94    void recordRestoreOffsetPlaceholder(SkRegion::Op);
95    void fillRestoreOffsetPlaceholdersForCurrentStackLevel(
96        uint32_t restoreOffset);
97
98    SkTDArray<int32_t> fRestoreOffsetStack;
99    int fFirstSavedLayerIndex;
100    enum {
101        kNoSavedLayerIndex = -1
102    };
103
104    void addDraw(DrawType drawType) {
105        this->predrawNotify();
106
107#ifdef SK_DEBUG_TRACE
108        SkDebugf("add %s\n", DrawTypeToString(drawType));
109#endif
110        fWriter.writeInt(drawType);
111    }
112    void addInt(int value) {
113        fWriter.writeInt(value);
114    }
115    void addScalar(SkScalar scalar) {
116        fWriter.writeScalar(scalar);
117    }
118
119    void addBitmap(const SkBitmap& bitmap);
120    void addMatrix(const SkMatrix& matrix);
121    void addMatrixPtr(const SkMatrix* matrix);
122    int  addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); }
123    int  addPaintPtr(const SkPaint* paint);
124    void addPath(const SkPath& path);
125    void addPicture(SkPicture& picture);
126    void addPoint(const SkPoint& point);
127    void addPoints(const SkPoint pts[], int count);
128    void addRect(const SkRect& rect);
129    void addRectPtr(const SkRect* rect);
130    void addIRect(const SkIRect& rect);
131    void addIRectPtr(const SkIRect* rect);
132    void addRegion(const SkRegion& region);
133    void addText(const void* text, size_t byteLength);
134
135    int find(const SkBitmap& bitmap);
136
137#ifdef SK_DEBUG_DUMP
138public:
139    void dumpMatrices();
140    void dumpPaints();
141#endif
142
143#ifdef SK_DEBUG_SIZE
144public:
145    size_t size() const;
146    int bitmaps(size_t* size) const;
147    int matrices(size_t* size) const;
148    int paints(size_t* size) const;
149    int paths(size_t* size) const;
150    int regions(size_t* size) const;
151    size_t streamlen() const;
152
153    size_t fPointBytes, fRectBytes, fTextBytes;
154    int fPointWrites, fRectWrites, fTextWrites;
155#endif
156
157#ifdef SK_DEBUG_VALIDATE
158public:
159    void validate() const;
160private:
161    void validateBitmaps() const;
162    void validateMatrices() const;
163    void validatePaints() const;
164    void validatePaths() const;
165    void validateRegions() const;
166#else
167public:
168    void validate() const {}
169#endif
170
171protected:
172
173    // These are set to NULL in our constructor, but may be changed by
174    // subclasses, in which case they will be SkSafeUnref'd in our destructor.
175    SkBBoxHierarchy* fBoundingHierarchy;
176    SkPictureStateTree* fStateTree;
177
178    // Allocated in the constructor and managed by this class.
179    SkBitmapHeap* fBitmapHeap;
180
181private:
182    SkChunkFlatController fFlattenableHeap;
183
184    SkMatrixDictionary fMatrices;
185    SkPaintDictionary fPaints;
186    SkRegionDictionary fRegions;
187
188    SkPathHeap* fPathHeap;  // reference counted
189    SkWriter32 fWriter;
190
191    // we ref each item in these arrays
192    SkTDArray<SkPicture*> fPictureRefs;
193
194    uint32_t fRecordFlags;
195    int fInitialSaveCount;
196
197    friend class SkPicturePlayback;
198    friend class SkPictureTester; // for unit testing
199
200    typedef SkCanvas INHERITED;
201};
202
203#endif
204