SkPictureRecord.h revision 801cee1d4cad2c382059c0f367edd77298b05caa
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, SkScalar minY, SkScalar maxY);
76
77    const SkTDArray<SkPicture* >& getPictureRefs() const {
78        return fPictureRefs;
79    }
80
81    void setFlags(uint32_t recordFlags) {
82        fRecordFlags = recordFlags;
83    }
84
85    const SkWriter32& writeStream() const {
86        return fWriter;
87    }
88
89    void beginRecording();
90    void endRecording();
91
92private:
93    void recordRestoreOffsetPlaceholder(SkRegion::Op);
94    void fillRestoreOffsetPlaceholdersForCurrentStackLevel(
95        uint32_t restoreOffset);
96
97    SkTDArray<int32_t> fRestoreOffsetStack;
98    int fFirstSavedLayerIndex;
99    enum {
100        kNoSavedLayerIndex = -1
101    };
102
103    void addDraw(DrawType drawType) {
104        this->predrawNotify();
105
106#ifdef SK_DEBUG_TRACE
107        SkDebugf("add %s\n", DrawTypeToString(drawType));
108#endif
109        fWriter.writeInt(drawType);
110    }
111    void addInt(int value) {
112        fWriter.writeInt(value);
113    }
114    void addScalar(SkScalar scalar) {
115        fWriter.writeScalar(scalar);
116    }
117
118    void addBitmap(const SkBitmap& bitmap);
119    void addMatrix(const SkMatrix& matrix);
120    void addMatrixPtr(const SkMatrix* matrix);
121    void addPaint(const SkPaint& paint);
122    void addPaintPtr(const SkPaint* paint);
123    void addPath(const SkPath& path);
124    void addPicture(SkPicture& picture);
125    void addPoint(const SkPoint& point);
126    void addPoints(const SkPoint pts[], int count);
127    void addRect(const SkRect& rect);
128    void addRectPtr(const SkRect* rect);
129    void addIRect(const SkIRect& rect);
130    void addIRectPtr(const SkIRect* rect);
131    void addRegion(const SkRegion& region);
132    void addText(const void* text, size_t byteLength);
133
134    int find(const SkBitmap& bitmap);
135
136#ifdef SK_DEBUG_DUMP
137public:
138    void dumpMatrices();
139    void dumpPaints();
140#endif
141
142#ifdef SK_DEBUG_SIZE
143public:
144    size_t size() const;
145    int bitmaps(size_t* size) const;
146    int matrices(size_t* size) const;
147    int paints(size_t* size) const;
148    int paths(size_t* size) const;
149    int regions(size_t* size) const;
150    size_t streamlen() const;
151
152    size_t fPointBytes, fRectBytes, fTextBytes;
153    int fPointWrites, fRectWrites, fTextWrites;
154#endif
155
156#ifdef SK_DEBUG_VALIDATE
157public:
158    void validate() const;
159private:
160    void validateBitmaps() const;
161    void validateMatrices() const;
162    void validatePaints() const;
163    void validatePaths() const;
164    void validateRegions() const;
165#else
166public:
167    void validate() const {}
168#endif
169
170protected:
171
172    // These are set to NULL in our constructor, but may be changed by
173    // subclasses, in which case they will be SkSafeUnref'd in our destructor.
174    SkBBoxHierarchy* fBoundingHierarchy;
175    SkPictureStateTree* fStateTree;
176
177    // Allocated in the constructor and managed by this class.
178    SkBitmapHeap* fBitmapHeap;
179
180private:
181    SkChunkFlatController fFlattenableHeap;
182
183    SkMatrixDictionary fMatrices;
184    SkPaintDictionary fPaints;
185    SkRegionDictionary fRegions;
186
187    SkPathHeap* fPathHeap;  // reference counted
188    SkWriter32 fWriter;
189
190    // we ref each item in these arrays
191    SkTDArray<SkPicture*> fPictureRefs;
192
193    uint32_t fRecordFlags;
194    int fInitialSaveCount;
195
196    friend class SkPicturePlayback;
197    friend class SkPictureTester; // for unit testing
198
199    typedef SkCanvas INHERITED;
200};
201
202#endif
203