1 /*
2  * Copyright 2014 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 SkRecorder_DEFINED
9 #define SkRecorder_DEFINED
10 
11 #include "SkBigPicture.h"
12 #include "SkMiniRecorder.h"
13 #include "SkNoDrawCanvas.h"
14 #include "SkRecord.h"
15 #include "SkRecords.h"
16 #include "SkTDArray.h"
17 
18 class SkBBHFactory;
19 
20 class SkDrawableList : SkNoncopyable {
21 public:
SkDrawableList()22     SkDrawableList() {}
23     ~SkDrawableList();
24 
count()25     int count() const { return fArray.count(); }
begin()26     SkDrawable* const* begin() const { return fArray.begin(); }
27 
28     void append(SkDrawable* drawable);
29 
30     // Return a new or ref'd array of pictures that were snapped from our drawables.
31     SkBigPicture::SnapshotArray* newDrawableSnapshot();
32 
33 private:
34     SkTDArray<SkDrawable*> fArray;
35 };
36 
37 // SkRecorder provides an SkCanvas interface for recording into an SkRecord.
38 
39 class SkRecorder final : public SkNoDrawCanvas {
40 public:
41     // Does not take ownership of the SkRecord.
42     SkRecorder(SkRecord*, int width, int height, SkMiniRecorder* = nullptr);   // legacy version
43     SkRecorder(SkRecord*, const SkRect& bounds, SkMiniRecorder* = nullptr);
44 
45     enum DrawPictureMode { Record_DrawPictureMode, Playback_DrawPictureMode };
46     void reset(SkRecord*, const SkRect& bounds, DrawPictureMode, SkMiniRecorder* = nullptr);
47 
approxBytesUsedBySubPictures()48     size_t approxBytesUsedBySubPictures() const { return fApproxBytesUsedBySubPictures; }
49 
getDrawableList()50     SkDrawableList* getDrawableList() const { return fDrawableList.get(); }
detachDrawableList()51     std::unique_ptr<SkDrawableList> detachDrawableList() { return std::move(fDrawableList); }
52 
53     // Make SkRecorder forget entirely about its SkRecord*; all calls to SkRecorder will fail.
54     void forgetRecord();
55 
56     void onFlush() override;
57 
58     void willSave() override;
59     SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
willRestore()60     void willRestore() override {}
61     void didRestore() override;
62 
63     void didConcat(const SkMatrix&) override;
64     void didSetMatrix(const SkMatrix&) override;
65     void didTranslate(SkScalar, SkScalar) override;
66 
67     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
68     void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
69     void onDrawText(const void* text,
70                     size_t byteLength,
71                     SkScalar x,
72                     SkScalar y,
73                     const SkPaint& paint) override;
74     void onDrawPosText(const void* text,
75                        size_t byteLength,
76                        const SkPoint pos[],
77                        const SkPaint& paint) override;
78     void onDrawPosTextH(const void* text,
79                         size_t byteLength,
80                         const SkScalar xpos[],
81                         SkScalar constY,
82                         const SkPaint& paint) override;
83     void onDrawTextOnPath(const void* text,
84                           size_t byteLength,
85                           const SkPath& path,
86                           const SkMatrix* matrix,
87                           const SkPaint& paint) override;
88     void onDrawTextRSXform(const void* text,
89                            size_t byteLength,
90                            const SkRSXform[],
91                            const SkRect* cull,
92                            const SkPaint& paint) override;
93     void onDrawTextBlob(const SkTextBlob* blob,
94                         SkScalar x,
95                         SkScalar y,
96                         const SkPaint& paint) override;
97     void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
98                      const SkPoint texCoords[4], SkBlendMode,
99                      const SkPaint& paint) override;
100 
101     void onDrawPaint(const SkPaint&) override;
102     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
103     void onDrawRect(const SkRect&, const SkPaint&) override;
104     void onDrawRegion(const SkRegion&, const SkPaint&) override;
105     void onDrawOval(const SkRect&, const SkPaint&) override;
106     void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
107     void onDrawRRect(const SkRRect&, const SkPaint&) override;
108     void onDrawPath(const SkPath&, const SkPaint&) override;
109     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
110     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
111                           SrcRectConstraint) override;
112     void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
113     void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
114                          const SkPaint*, SrcRectConstraint) override;
115     void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
116                          const SkPaint*) override;
117     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
118                           const SkPaint*) override;
119     void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst,
120                             const SkPaint*) override;
121     void onDrawBitmapLattice(const SkBitmap&, const Lattice& lattice, const SkRect& dst,
122                              const SkPaint*) override;
123     void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
124     void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
125                      int count, SkBlendMode, const SkRect* cull, const SkPaint*) override;
126     void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
127 
128     void onClipRect(const SkRect& rect, SkClipOp, ClipEdgeStyle) override;
129     void onClipRRect(const SkRRect& rrect, SkClipOp, ClipEdgeStyle) override;
130     void onClipPath(const SkPath& path, SkClipOp, ClipEdgeStyle) override;
131     void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override;
132 
133     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
134 
135     void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
136 
137     sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
138 
139     void flushMiniRecorder();
140 
141 private:
142     template <typename T>
143     T* copy(const T*);
144 
145     template <typename T>
146     T* copy(const T[], size_t count);
147 
148     DrawPictureMode fDrawPictureMode;
149     size_t fApproxBytesUsedBySubPictures;
150     SkRecord* fRecord;
151     std::unique_ptr<SkDrawableList> fDrawableList;
152 
153     SkMiniRecorder* fMiniRecorder;
154 };
155 
156 #endif//SkRecorder_DEFINED
157