• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/core/SkColor.h"
13 #include "include/core/SkM44.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSamplingOptions.h"
16 #include "include/core/SkScalar.h"
17 #include "include/private/base/SkNoncopyable.h"
18 #include "include/private/base/SkTDArray.h"
19 #include "include/utils/SkNoDrawCanvas.h"
20 #include "src/core/SkBigPicture.h"
21 
22 #include <cstddef>
23 #include <memory>
24 #include <utility>
25 
26 class SkBlender;
27 class SkData;
28 class SkDrawable;
29 class SkImage;
30 class SkMatrix;
31 class SkMesh;
32 class SkPaint;
33 class SkPath;
34 class SkPicture;
35 class SkRRect;
36 class SkRecord;
37 class SkRegion;
38 class SkShader;
39 class SkSurface;
40 class SkSurfaceProps;
41 class SkTextBlob;
42 class SkVertices;
43 enum class SkBlendMode;
44 enum class SkClipOp;
45 struct SkDrawShadowRec;
46 struct SkImageInfo;
47 struct SkPoint;
48 struct SkRSXform;
49 struct SkRect;
50 
51 namespace sktext {
52     class GlyphRunList;
53     namespace gpu { class Slug; }
54 }
55 
56 class SkDrawableList : SkNoncopyable {
57 public:
SkDrawableList()58     SkDrawableList() {}
59     ~SkDrawableList();
60 
count()61     int count() const { return fArray.size(); }
begin()62     SkDrawable* const* begin() const { return fArray.begin(); }
end()63     SkDrawable* const* end() const { return fArray.end(); }
64 
65     void append(SkDrawable* drawable);
66 
67     // Return a new or ref'd array of pictures that were snapped from our drawables.
68     SkBigPicture::SnapshotArray* newDrawableSnapshot();
69 
70 private:
71     SkTDArray<SkDrawable*> fArray;
72 };
73 
74 // SkRecorder provides an SkCanvas interface for recording into an SkRecord.
75 
76 class SkRecorder final : public SkCanvasVirtualEnforcer<SkNoDrawCanvas> {
77 public:
78     // Does not take ownership of the SkRecord.
79     SkRecorder(SkRecord*, int width, int height);   // TODO: remove
80     SkRecorder(SkRecord*, const SkRect& bounds);
81 
82     void reset(SkRecord*, const SkRect& bounds);
83 
approxBytesUsedBySubPictures()84     size_t approxBytesUsedBySubPictures() const { return fApproxBytesUsedBySubPictures; }
85 
getDrawableList()86     SkDrawableList* getDrawableList() const { return fDrawableList.get(); }
detachDrawableList()87     std::unique_ptr<SkDrawableList> detachDrawableList() { return std::move(fDrawableList); }
88 
89     // Make SkRecorder forget entirely about its SkRecord*; all calls to SkRecorder will fail.
90     void forgetRecord();
91 
92     void onFlush() override;
93 
94     void willSave() override;
95     SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
96     bool onDoSaveBehind(const SkRect*) override;
willRestore()97     void willRestore() override {}
98     void didRestore() override;
99 
100     void didConcat44(const SkM44&) override;
101     void didSetM44(const SkM44&) override;
102     void didScale(SkScalar, SkScalar) override;
103     void didTranslate(SkScalar, SkScalar) override;
104 
105     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
106     void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
107     void onDrawTextBlob(const SkTextBlob* blob,
108                         SkScalar x,
109                         SkScalar y,
110                         const SkPaint& paint) override;
111 #if defined(SK_GANESH)
112     void onDrawSlug(const sktext::gpu::Slug* slug) override;
113 #endif
114     void onDrawGlyphRunList(
115             const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) override;
116     void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
117                      const SkPoint texCoords[4], SkBlendMode,
118                      const SkPaint& paint) override;
119 
120     void onDrawPaint(const SkPaint&) override;
121     void onDrawBehind(const SkPaint&) override;
122     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
123     void onDrawRect(const SkRect&, const SkPaint&) override;
124     void onDrawRegion(const SkRegion&, const SkPaint&) override;
125     void onDrawOval(const SkRect&, const SkPaint&) override;
126     void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
127     void onDrawRRect(const SkRRect&, const SkPaint&) override;
128     void onDrawPath(const SkPath&, const SkPaint&) override;
129 
130     void onDrawImage2(const SkImage*, SkScalar, SkScalar, const SkSamplingOptions&,
131                       const SkPaint*) override;
132     void onDrawImageRect2(const SkImage*, const SkRect&, const SkRect&, const SkSamplingOptions&,
133                           const SkPaint*, SrcRectConstraint) override;
134     void onDrawImageLattice2(const SkImage*, const Lattice&, const SkRect&, SkFilterMode,
135                              const SkPaint*) override;
136     void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
137                      SkBlendMode, const SkSamplingOptions&, const SkRect*, const SkPaint*) override;
138 
139     void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
140 #ifdef SK_ENABLE_SKSL
141     void onDrawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) override;
142 #endif
143     void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
144 
145     void onClipRect(const SkRect& rect, SkClipOp, ClipEdgeStyle) override;
146     void onClipRRect(const SkRRect& rrect, SkClipOp, ClipEdgeStyle) override;
147     void onClipPath(const SkPath& path, SkClipOp, ClipEdgeStyle) override;
148     void onClipShader(sk_sp<SkShader>, SkClipOp) override;
149     void onClipRegion(const SkRegion& deviceRgn, SkClipOp) override;
150     void onResetClip() override;
151 
152     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
153 
154     void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
155 
156     void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, const SkColor4f&,
157                           SkBlendMode) override;
158     void onDrawEdgeAAImageSet2(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[],
159                                const SkSamplingOptions&, const SkPaint*,
160                                SrcRectConstraint) override;
161 
162     sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
163 
164 private:
165     template <typename T>
166     T* copy(const T*);
167 
168     template <typename T>
169     T* copy(const T[], size_t count);
170 
171     template<typename T, typename... Args>
172     void append(Args&&...);
173 
174     size_t fApproxBytesUsedBySubPictures;
175     SkRecord* fRecord;
176     std::unique_ptr<SkDrawableList> fDrawableList;
177 };
178 
179 #endif//SkRecorder_DEFINED
180