• 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 
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 onDrawBehind(const SkPaint&) override;
178     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
179     void onDrawRect(const SkRect&, const SkPaint&) 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 
193     void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
194     void onDrawVerticesObject(const SkVertices*, const SkVertices::Bone bones[], int boneCount,
195                               SkBlendMode, const SkPaint&) override;
196 
197     void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
198     void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
199     void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
200     void onClipRegion(const SkRegion&, SkClipOp) override;
201 
202     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
203 
204     void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
205     void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
206 
207     void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, SkColor,
208                           SkBlendMode) override;
209     void onDrawEdgeAAImageSet(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[],
210                               const SkPaint*, SrcRectConstraint) override;
211 
212     int addPathToHeap(const SkPath& path);  // does not write to ops stream
213 
214     // These entry points allow the writing of matrices, clips, saves &
215     // restores to be deferred (e.g., if the MC state is being collapsed and
216     // only written out as needed).
217     void recordConcat(const SkMatrix& matrix);
218     void recordTranslate(const SkMatrix& matrix);
219     void recordScale(const SkMatrix& matrix);
220     size_t recordClipRect(const SkRect& rect, SkClipOp op, bool doAA);
221     size_t recordClipRRect(const SkRRect& rrect, SkClipOp op, bool doAA);
222     size_t recordClipPath(int pathID, SkClipOp op, bool doAA);
223     size_t recordClipRegion(const SkRegion& region, SkClipOp op);
224     void recordSave();
225     void recordSaveLayer(const SaveLayerRec&);
226     void recordRestore(bool fillInSkips = true);
227 
228     // SHOULD NEVER BE CALLED
onDrawBitmap(const SkBitmap &,SkScalar left,SkScalar top,const SkPaint *)229     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override {
230         SK_ABORT("not reached");
231     }
onDrawBitmapRect(const SkBitmap &,const SkRect * src,const SkRect & dst,const SkPaint *,SrcRectConstraint)232     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
233                           SrcRectConstraint) override {
234         SK_ABORT("not reached");
235     }
onDrawBitmapNine(const SkBitmap &,const SkIRect & center,const SkRect & dst,const SkPaint *)236     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
237                           const SkPaint*) override {
238         SK_ABORT("not reached");
239     }
onDrawBitmapLattice(const SkBitmap &,const SkCanvas::Lattice & lattice,const SkRect & dst,const SkPaint *)240     void onDrawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice& lattice, const SkRect& dst,
241                              const SkPaint*) override {
242         SK_ABORT("not reached");
243     }
244 
245 private:
246     SkTArray<SkPaint>  fPaints;
247 
248     struct PathHash {
operatorPathHash249         uint32_t operator()(const SkPath& p) { return p.getGenerationID(); }
250     };
251     SkTHashMap<SkPath, int, PathHash> fPaths;
252 
253     SkWriter32 fWriter;
254 
255     SkTArray<sk_sp<const SkImage>>    fImages;
256     SkTArray<sk_sp<const SkPicture>>  fPictures;
257     SkTArray<sk_sp<SkDrawable>>       fDrawables;
258     SkTArray<sk_sp<const SkTextBlob>> fTextBlobs;
259     SkTArray<sk_sp<const SkVertices>> fVertices;
260 
261     uint32_t fRecordFlags;
262     int      fInitialSaveCount;
263 
264     friend class SkPictureData;   // for SkPictureData's SkPictureRecord-based constructor
265 
266     typedef SkCanvasVirtualEnforcer<SkCanvas> INHERITED;
267 };
268 
269 #endif
270