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