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