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