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     void reset(SkRecord*, const SkRect& bounds, 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;
60     bool onDoSaveBehind(const SkRect*) override;
willRestore()61     void willRestore() override {}
62     void didRestore() override;
63 
64     void onMarkCTM(const char*) override;
65     void didConcat44(const SkM44&) override;
66     void didConcat(const SkMatrix&) override;
67     void didSetMatrix(const SkMatrix&) override;
68     void didScale(SkScalar, SkScalar) override;
69     void didTranslate(SkScalar, SkScalar) override;
70 
71     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
72     void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
73     void onDrawTextBlob(const SkTextBlob* blob,
74                         SkScalar x,
75                         SkScalar y,
76                         const SkPaint& paint) override;
77     void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
78                      const SkPoint texCoords[4], SkBlendMode,
79                      const SkPaint& paint) override;
80 
81     void onDrawPaint(const SkPaint&) override;
82     void onDrawBehind(const SkPaint&) override;
83     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
84     void onDrawRect(const SkRect&, const SkPaint&) override;
85     void onDrawRegion(const SkRegion&, const SkPaint&) override;
86     void onDrawOval(const SkRect&, const SkPaint&) override;
87     void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
88     void onDrawRRect(const SkRRect&, const SkPaint&) override;
89     void onDrawPath(const SkPath&, const SkPaint&) override;
90     void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
91     void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
92                          const SkPaint*, SrcRectConstraint) override;
93     void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
94                          const SkPaint*) override;
95     void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst,
96                             const SkPaint*) override;
97     void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
98     void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
99                      int count, SkBlendMode, const SkRect* cull, const SkPaint*) override;
100     void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
101 
102     void onClipRect(const SkRect& rect, SkClipOp, ClipEdgeStyle) override;
103     void onClipRRect(const SkRRect& rrect, SkClipOp, ClipEdgeStyle) override;
104     void onClipPath(const SkPath& path, SkClipOp, ClipEdgeStyle) override;
105     void onClipShader(sk_sp<SkShader>, SkClipOp) override;
106     void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override;
107 
108     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
109 
110     void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
111 
112     void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, const SkColor4f&,
113                           SkBlendMode) override;
114     void onDrawEdgeAAImageSet(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[],
115                               const SkPaint*, SrcRectConstraint) override;
116 
117     sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
118 
119     void flushMiniRecorder();
120 
121 private:
122     template <typename T>
123     T* copy(const T*);
124 
125     template <typename T>
126     T* copy(const T[], size_t count);
127 
128     template<typename T, typename... Args>
129     void append(Args&&...);
130 
131     size_t fApproxBytesUsedBySubPictures;
132     SkRecord* fRecord;
133     std::unique_ptr<SkDrawableList> fDrawableList;
134 
135     SkMiniRecorder* fMiniRecorder;
136 };
137 
138 #endif//SkRecorder_DEFINED
139