• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 class SkPictureRecord : public SkCanvas {
20 public:
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 
getBitmaps()72     const SkTDArray<const SkFlatBitmap* >& getBitmaps() const {
73         return fBitmaps;
74     }
getMatrices()75     const SkTDArray<const SkFlatMatrix* >& getMatrices() const {
76         return fMatrices;
77     }
getPaints()78     const SkTDArray<const SkFlatPaint* >& getPaints() const {
79         return fPaints;
80     }
getPictureRefs()81     const SkTDArray<SkPicture* >& getPictureRefs() const {
82         return fPictureRefs;
83     }
getRegions()84     const SkTDArray<const SkFlatRegion* >& getRegions() const {
85         return fRegions;
86     }
87 
88     void reset();
89 
writeStream()90     const SkWriter32& writeStream() const {
91         return fWriter;
92     }
93 
94 private:
95     SkTDArray<uint32_t> fRestoreOffsetStack;
96     int fFirstSavedLayerIndex;
97     enum {
98         kNoSavedLayerIndex = -1
99     };
100 
addDraw(DrawType drawType)101     void addDraw(DrawType drawType) {
102 #ifdef SK_DEBUG_TRACE
103         SkDebugf("add %s\n", DrawTypeToString(drawType));
104 #endif
105         fWriter.writeInt(drawType);
106     }
addInt(int value)107     void addInt(int value) {
108         fWriter.writeInt(value);
109     }
addScalar(SkScalar scalar)110     void addScalar(SkScalar scalar) {
111         fWriter.writeScalar(scalar);
112     }
113 
114     void addBitmap(const SkBitmap& bitmap);
115     void addMatrix(const SkMatrix& matrix);
116     void addMatrixPtr(const SkMatrix* matrix);
117     void addPaint(const SkPaint& paint);
118     void addPaintPtr(const SkPaint* paint);
119     void addPath(const SkPath& path);
120     void addPicture(SkPicture& picture);
121     void addPoint(const SkPoint& point);
122     void addPoints(const SkPoint pts[], int count);
123     void addRect(const SkRect& rect);
124     void addRectPtr(const SkRect* rect);
125     void addIRect(const SkIRect& rect);
126     void addIRectPtr(const SkIRect* rect);
127     void addRegion(const SkRegion& region);
128     void addText(const void* text, size_t byteLength);
129 
130     int find(SkTDArray<const SkFlatBitmap* >& bitmaps,
131                    const SkBitmap& bitmap);
132     int find(SkTDArray<const SkFlatMatrix* >& matrices,
133                    const SkMatrix* matrix);
134     int find(SkTDArray<const SkFlatPaint* >& paints, const SkPaint* paint);
135     int find(SkTDArray<const SkFlatRegion* >& regions, const SkRegion& region);
136 
137 #ifdef SK_DEBUG_DUMP
138 public:
139     void dumpMatrices();
140     void dumpPaints();
141 #endif
142 
143 #ifdef SK_DEBUG_SIZE
144 public:
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
158 public:
159     void validate() const;
160 private:
161     void validateBitmaps() const;
162     void validateMatrices() const;
163     void validatePaints() const;
164     void validatePaths() const;
165     void validateRegions() const;
166 #else
167 public:
validate()168     void validate() const {}
169 #endif
170 
171 private:
172     SkChunkAlloc fHeap;
173     int fBitmapIndex;
174     SkTDArray<const SkFlatBitmap* > fBitmaps;
175     int fMatrixIndex;
176     SkTDArray<const SkFlatMatrix* > fMatrices;
177     int fPaintIndex;
178     SkTDArray<const SkFlatPaint* > fPaints;
179     int fRegionIndex;
180     SkTDArray<const SkFlatRegion* > fRegions;
181     SkPathHeap* fPathHeap;  // reference counted
182     SkWriter32 fWriter;
183 
184     // we ref each item in these arrays
185     SkTDArray<SkPicture*> fPictureRefs;
186 
187     SkRefCntSet fRCSet;
188     SkRefCntSet fTFSet;
189 
190     uint32_t fRecordFlags;
191 
192     // helper function to handle save/restore culling offsets
193     void recordOffsetForRestore(SkRegion::Op op);
194 
195     friend class SkPicturePlayback;
196     friend class SkPictureTester; // for unit testing
197 
198     typedef SkCanvas INHERITED;
199 };
200 
201 #endif
202