• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkPictureRecord_DEFINED
9 #define SkPictureRecord_DEFINED
10 
11 #include "SkCanvas.h"
12 #include "SkFlattenable.h"
13 #include "SkPicture.h"
14 #include "SkPictureData.h"
15 #include "SkTArray.h"
16 #include "SkTDArray.h"
17 #include "SkTHash.h"
18 #include "SkVertices.h"
19 #include "SkWriter32.h"
20 
21 // These macros help with packing and unpacking a single byte value and
22 // a 3 byte value into/out of a uint32_t
23 #define MASK_24 0x00FFFFFF
24 #define UNPACK_8_24(combined, small, large)             \
25     small = (combined >> 24) & 0xFF;                    \
26     large = combined & MASK_24;
27 #define PACK_8_24(small, large) ((small << 24) | large)
28 
29 
30 class SkPictureRecord : public SkCanvas {
31 public:
32     SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
33     ~SkPictureRecord() override;
34 
getPictureRefs()35     const SkTDArray<const SkPicture* >& getPictureRefs() const {
36         return fPictureRefs;
37     }
38 
getDrawableRefs()39     const SkTDArray<SkDrawable* >& getDrawableRefs() const {
40         return fDrawableRefs;
41     }
42 
getTextBlobRefs()43     const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const {
44         return fTextBlobRefs;
45     }
46 
getVerticesRefs()47     const SkTDArray<const SkVertices* >& getVerticesRefs() const {
48         return fVerticesRefs;
49     }
50 
getImageRefs()51    const SkTDArray<const SkImage* >& getImageRefs() const {
52         return fImageRefs;
53     }
54 
opData()55     sk_sp<SkData> opData() const {
56         this->validate(fWriter.bytesWritten(), 0);
57 
58         if (fWriter.bytesWritten() == 0) {
59             return SkData::MakeEmpty();
60         }
61         return fWriter.snapshotAsData();
62     }
63 
contentInfo()64     const SkPictureContentInfo& contentInfo() const {
65         return fContentInfo;
66     }
67 
setFlags(uint32_t recordFlags)68     void setFlags(uint32_t recordFlags) {
69         fRecordFlags = recordFlags;
70     }
71 
writeStream()72     const SkWriter32& writeStream() const {
73         return fWriter;
74     }
75 
76     void beginRecording();
77     void endRecording();
78 
79 protected:
80     void addNoOp();
81 
82 private:
83     void handleOptimization(int opt);
84     size_t recordRestoreOffsetPlaceholder(SkClipOp);
85     void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
86 
87     SkTDArray<int32_t> fRestoreOffsetStack;
88 
89     SkTDArray<uint32_t> fCullOffsetStack;
90 
91     /*
92      * Write the 'drawType' operation and chunk size to the skp. 'size'
93      * can potentially be increased if the chunk size needs its own storage
94      * location (i.e., it overflows 24 bits).
95      * Returns the start offset of the chunk. This is the location at which
96      * the opcode & size are stored.
97      * TODO: since we are handing the size into here we could call reserve
98      * and then return a pointer to the memory storage. This could decrease
99      * allocation overhead but could lead to more wasted space (the tail
100      * end of blocks could go unused). Possibly add a second addDraw that
101      * operates in this manner.
102      */
addDraw(DrawType drawType,size_t * size)103     size_t addDraw(DrawType drawType, size_t* size) {
104         size_t offset = fWriter.bytesWritten();
105 
106         this->predrawNotify();
107         fContentInfo.addOperation();
108 
109         SkASSERT(0 != *size);
110         SkASSERT(((uint8_t) drawType) == drawType);
111 
112         if (0 != (*size & ~MASK_24) || *size == MASK_24) {
113             fWriter.writeInt(PACK_8_24(drawType, MASK_24));
114             *size += 1;
115             fWriter.writeInt(SkToU32(*size));
116         } else {
117             fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
118         }
119 
120         return offset;
121     }
122 
addInt(int value)123     void addInt(int value) {
124         fWriter.writeInt(value);
125     }
addScalar(SkScalar scalar)126     void addScalar(SkScalar scalar) {
127         fWriter.writeScalar(scalar);
128     }
129 
130     void addImage(const SkImage*);
131     void addMatrix(const SkMatrix& matrix);
addPaint(const SkPaint & paint)132     void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
133     void addPaintPtr(const SkPaint* paint);
134     void addPatch(const SkPoint cubics[12]);
135     void addPath(const SkPath& path);
136     void addPicture(const SkPicture* picture);
137     void addDrawable(SkDrawable* picture);
138     void addPoint(const SkPoint& point);
139     void addPoints(const SkPoint pts[], int count);
140     void addRect(const SkRect& rect);
141     void addRectPtr(const SkRect* rect);
142     void addIRect(const SkIRect& rect);
143     void addIRectPtr(const SkIRect* rect);
144     void addRRect(const SkRRect&);
145     void addRegion(const SkRegion& region);
146     void addText(const void* text, size_t byteLength);
147     void addTextBlob(const SkTextBlob* blob);
148     void addVertices(const SkVertices*);
149 
150     int find(const SkBitmap& bitmap);
151 
152 protected:
validate(size_t initialOffset,size_t size)153     void validate(size_t initialOffset, size_t size) const {
154         SkASSERT(fWriter.bytesWritten() == initialOffset + size);
155     }
156 
157     sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
onPeekPixels(SkPixmap *)158     bool onPeekPixels(SkPixmap*) override { return false; }
159 
160     void willSave() override;
161     SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
162     void willRestore() override;
163 
164     void didConcat(const SkMatrix&) override;
165     void didSetMatrix(const SkMatrix&) override;
166 
167     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
168 
169     void onDrawText(const void* text, size_t, SkScalar x, SkScalar y, const SkPaint&) override;
170     void onDrawPosText(const void* text, size_t, const SkPoint pos[], const SkPaint&) override;
171     void onDrawPosTextH(const void* text, size_t, const SkScalar xpos[], SkScalar constY,
172                         const SkPaint&) override;
173     void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
174                                   const SkMatrix* matrix, const SkPaint&) override;
175     void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
176                            const SkRect* cull, const SkPaint&) override;
177     void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
178                                 const SkPaint& paint) override;
179 
180     void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
181                      const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
182     void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
183                      SkBlendMode, const SkRect*, const SkPaint*) override;
184 
185     void onDrawPaint(const SkPaint&) override;
186     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
187     void onDrawRect(const SkRect&, const SkPaint&) override;
188     void onDrawRegion(const SkRegion&, const SkPaint&) override;
189     void onDrawOval(const SkRect&, const SkPaint&) override;
190     void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
191     void onDrawRRect(const SkRRect&, const SkPaint&) override;
192     void onDrawPath(const SkPath&, const SkPaint&) override;
193     void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
194     void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
195                          const SkPaint*, SrcRectConstraint) override;
196     void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
197                          const SkPaint*) override;
198     void onDrawImageLattice(const SkImage*, const SkCanvas::Lattice& lattice, const SkRect& dst,
199                             const SkPaint*) override;
200     void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
201     void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
202 
203     void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
204     void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
205     void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
206     void onClipRegion(const SkRegion&, SkClipOp) override;
207 
208     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
209 
210     void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
211     void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
212 
213     int addPathToHeap(const SkPath& path);  // does not write to ops stream
214 
215     // These entry points allow the writing of matrices, clips, saves &
216     // restores to be deferred (e.g., if the MC state is being collapsed and
217     // only written out as needed).
218     void recordConcat(const SkMatrix& matrix);
219     void recordTranslate(const SkMatrix& matrix);
220     void recordScale(const SkMatrix& matrix);
221     size_t recordClipRect(const SkRect& rect, SkClipOp op, bool doAA);
222     size_t recordClipRRect(const SkRRect& rrect, SkClipOp op, bool doAA);
223     size_t recordClipPath(int pathID, SkClipOp op, bool doAA);
224     size_t recordClipRegion(const SkRegion& region, SkClipOp op);
225     void recordSave();
226     void recordSaveLayer(const SaveLayerRec&);
227     void recordRestore(bool fillInSkips = true);
228 
229     // SHOULD NEVER BE CALLED
onDrawBitmap(const SkBitmap &,SkScalar left,SkScalar top,const SkPaint *)230     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override {
231         sk_throw();
232     }
onDrawBitmapRect(const SkBitmap &,const SkRect * src,const SkRect & dst,const SkPaint *,SrcRectConstraint)233     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
234                           SrcRectConstraint) override {
235         sk_throw();
236     }
onDrawBitmapNine(const SkBitmap &,const SkIRect & center,const SkRect & dst,const SkPaint *)237     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
238                           const SkPaint*) override {
239         sk_throw();
240     }
onDrawBitmapLattice(const SkBitmap &,const SkCanvas::Lattice & lattice,const SkRect & dst,const SkPaint *)241     void onDrawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice& lattice, const SkRect& dst,
242                              const SkPaint*) override {
243         sk_throw();
244     }
245 
246 private:
247     SkPictureContentInfo fContentInfo;
248 
249     SkTArray<SkPaint>  fPaints;
250 
251     struct PathHash {
operatorPathHash252         uint32_t operator()(const SkPath& p) { return p.getGenerationID(); }
253     };
254     SkTHashMap<SkPath, int, PathHash> fPaths;
255 
256     SkWriter32 fWriter;
257 
258     // we ref each item in these arrays
259     SkTDArray<const SkImage*>    fImageRefs;
260     SkTDArray<const SkPicture*>  fPictureRefs;
261     SkTDArray<SkDrawable*>       fDrawableRefs;
262     SkTDArray<const SkTextBlob*> fTextBlobRefs;
263     SkTDArray<const SkVertices*> fVerticesRefs;
264 
265     uint32_t fRecordFlags;
266     int      fInitialSaveCount;
267 
268     friend class SkPictureData;   // for SkPictureData's SkPictureRecord-based constructor
269 
270     typedef SkCanvas INHERITED;
271 };
272 
273 #endif
274