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