Flutter Engine
canvas_spy.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/common/canvas_spy.h"
6 
7 namespace flutter {
8 
9 CanvasSpy::CanvasSpy(SkCanvas* target_canvas) {
10  SkISize canvas_size = target_canvas->getBaseLayerSize();
11  n_way_canvas_ =
12  std::make_unique<SkNWayCanvas>(canvas_size.width(), canvas_size.height());
13  did_draw_canvas_ = std::make_unique<DidDrawCanvas>(canvas_size.width(),
14  canvas_size.height());
15  n_way_canvas_->addCanvas(target_canvas);
16  n_way_canvas_->addCanvas(did_draw_canvas_.get());
17 }
18 
20  return n_way_canvas_.get();
21 }
22 
24  : SkCanvasVirtualEnforcer<SkNoDrawCanvas>(width, height) {}
25 
27 
28 void DidDrawCanvas::MarkDrawIfNonTransparentPaint(const SkPaint& paint) {
29  bool isTransparent = paint.getAlpha() == 0;
30  did_draw_ |= !isTransparent;
31 }
32 
34  return did_draw_canvas_->DidDrawIntoCanvas();
35 }
36 
38  return did_draw_;
39 }
40 
41 void DidDrawCanvas::willSave() {}
42 
43 SkCanvas::SaveLayerStrategy DidDrawCanvas::getSaveLayerStrategy(
44  const SaveLayerRec& rec) {
45  return kNoLayer_SaveLayerStrategy;
46 }
47 
48 bool DidDrawCanvas::onDoSaveBehind(const SkRect* bounds) {
49  return false;
50 }
51 
52 void DidDrawCanvas::willRestore() {}
53 
54 void DidDrawCanvas::didConcat44(const SkM44&) {}
55 
56 void DidDrawCanvas::didScale(SkScalar, SkScalar) {}
57 
58 void DidDrawCanvas::didTranslate(SkScalar, SkScalar) {}
59 
60 void DidDrawCanvas::onClipRect(const SkRect& rect,
61  SkClipOp op,
62  ClipEdgeStyle edgeStyle) {}
63 
64 void DidDrawCanvas::onClipRRect(const SkRRect& rrect,
65  SkClipOp op,
66  ClipEdgeStyle edgeStyle) {}
67 
68 void DidDrawCanvas::onClipPath(const SkPath& path,
69  SkClipOp op,
70  ClipEdgeStyle edgeStyle) {}
71 
72 void DidDrawCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {}
73 
74 void DidDrawCanvas::onDrawPaint(const SkPaint& paint) {
75  MarkDrawIfNonTransparentPaint(paint);
76 }
77 
78 void DidDrawCanvas::onDrawBehind(const SkPaint& paint) {
79  MarkDrawIfNonTransparentPaint(paint);
80 }
81 
82 void DidDrawCanvas::onDrawPoints(PointMode mode,
83  size_t count,
84  const SkPoint pts[],
85  const SkPaint& paint) {
86  MarkDrawIfNonTransparentPaint(paint);
87 }
88 
89 void DidDrawCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
90  MarkDrawIfNonTransparentPaint(paint);
91 }
92 
93 void DidDrawCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
94  MarkDrawIfNonTransparentPaint(paint);
95 }
96 
97 void DidDrawCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
98  MarkDrawIfNonTransparentPaint(paint);
99 }
100 
101 void DidDrawCanvas::onDrawArc(const SkRect& rect,
102  SkScalar startAngle,
103  SkScalar sweepAngle,
104  bool useCenter,
105  const SkPaint& paint) {
106  MarkDrawIfNonTransparentPaint(paint);
107 }
108 
109 void DidDrawCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
110  MarkDrawIfNonTransparentPaint(paint);
111 }
112 
113 void DidDrawCanvas::onDrawDRRect(const SkRRect& outer,
114  const SkRRect& inner,
115  const SkPaint& paint) {
116  MarkDrawIfNonTransparentPaint(paint);
117 }
118 
119 void DidDrawCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
120  MarkDrawIfNonTransparentPaint(paint);
121 }
122 
123 #ifdef SK_SUPPORT_LEGACY_ONDRAWIMAGERECT
124 void DidDrawCanvas::onDrawImage(const SkImage* image,
125  SkScalar left,
126  SkScalar top,
127  const SkPaint* paint) {
128  did_draw_ = true;
129 }
130 
131 void DidDrawCanvas::onDrawImageRect(const SkImage* image,
132  const SkRect* src,
133  const SkRect& dst,
134  const SkPaint* paint,
135  SrcRectConstraint constraint) {
136  did_draw_ = true;
137 }
138 
139 void DidDrawCanvas::onDrawImageLattice(const SkImage* image,
140  const Lattice& lattice,
141  const SkRect& dst,
142  const SkPaint* paint) {
143  did_draw_ = true;
144 }
145 
146 void DidDrawCanvas::onDrawAtlas(const SkImage* image,
147  const SkRSXform xform[],
148  const SkRect tex[],
149  const SkColor colors[],
150  int count,
151  SkBlendMode bmode,
152  const SkRect* cull,
153  const SkPaint* paint) {
154  did_draw_ = true;
155 }
156 
157 void DidDrawCanvas::onDrawEdgeAAImageSet(const ImageSetEntry set[],
158  int count,
159  const SkPoint dstClips[],
160  const SkMatrix preViewMatrices[],
161  const SkPaint* paint,
162  SrcRectConstraint constraint) {
163  did_draw_ = true;
164 }
165 #endif
166 
167 void DidDrawCanvas::onDrawImage2(const SkImage* image,
168  SkScalar left,
169  SkScalar top,
170  const SkSamplingOptions&,
171  const SkPaint* paint) {
172  did_draw_ = true;
173 }
174 
175 void DidDrawCanvas::onDrawImageRect2(const SkImage* image,
176  const SkRect& src,
177  const SkRect& dst,
178  const SkSamplingOptions&,
179  const SkPaint* paint,
180  SrcRectConstraint constraint) {
181  did_draw_ = true;
182 }
183 
184 void DidDrawCanvas::onDrawImageLattice2(const SkImage* image,
185  const Lattice& lattice,
186  const SkRect& dst,
187  SkFilterMode,
188  const SkPaint* paint) {
189  did_draw_ = true;
190 }
191 
192 void DidDrawCanvas::onDrawTextBlob(const SkTextBlob* blob,
193  SkScalar x,
194  SkScalar y,
195  const SkPaint& paint) {
196  MarkDrawIfNonTransparentPaint(paint);
197 }
198 
199 void DidDrawCanvas::onDrawPicture(const SkPicture* picture,
200  const SkMatrix* matrix,
201  const SkPaint* paint) {
202  did_draw_ = true;
203 }
204 
205 void DidDrawCanvas::onDrawDrawable(SkDrawable* drawable,
206  const SkMatrix* matrix) {
207  did_draw_ = true;
208 }
209 
210 void DidDrawCanvas::onDrawVerticesObject(const SkVertices* vertices,
211  SkBlendMode bmode,
212  const SkPaint& paint) {
213  MarkDrawIfNonTransparentPaint(paint);
214 }
215 
216 void DidDrawCanvas::onDrawPatch(const SkPoint cubics[12],
217  const SkColor colors[4],
218  const SkPoint texCoords[4],
219  SkBlendMode bmode,
220  const SkPaint& paint) {
221  MarkDrawIfNonTransparentPaint(paint);
222 }
223 
224 void DidDrawCanvas::onDrawAtlas2(const SkImage* image,
225  const SkRSXform xform[],
226  const SkRect tex[],
227  const SkColor colors[],
228  int count,
229  SkBlendMode bmode,
230  const SkSamplingOptions&,
231  const SkRect* cull,
232  const SkPaint* paint) {
233  did_draw_ = true;
234 }
235 
236 void DidDrawCanvas::onDrawShadowRec(const SkPath& path,
237  const SkDrawShadowRec& rec) {
238  did_draw_ = true;
239 }
240 
241 void DidDrawCanvas::onDrawAnnotation(const SkRect& rect,
242  const char key[],
243  SkData* data) {
244  did_draw_ = true;
245 }
246 
247 void DidDrawCanvas::onDrawEdgeAAQuad(const SkRect& rect,
248  const SkPoint clip[4],
249  SkCanvas::QuadAAFlags aa,
250  const SkColor4f& color,
251  SkBlendMode mode) {
252  did_draw_ = true;
253 }
254 
255 void DidDrawCanvas::onDrawEdgeAAImageSet2(const ImageSetEntry set[],
256  int count,
257  const SkPoint dstClips[],
258  const SkMatrix preViewMatrices[],
259  const SkSamplingOptions&,
260  const SkPaint* paint,
261  SrcRectConstraint constraint) {
262  did_draw_ = true;
263 }
264 
265 void DidDrawCanvas::onFlush() {}
266 
267 } // namespace flutter
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
~DidDrawCanvas() override
Definition: canvas_spy.cc:26
DidDrawCanvas(int width, int height)
Definition: canvas_spy.cc:23
SkCanvas * GetSpyingCanvas()
The returned canvas delegate all operations to the target canvas while spying on them.
Definition: canvas_spy.cc:19
bool DidDrawIntoCanvas()
Returns true if any non transparent content has been drawn into the spying canvas. Note that this class does tries to detect empty canvases but in some cases may return true even for empty canvases (e.g when a transparent image is drawn into the canvas).
Definition: canvas_spy.cc:33
CanvasSpy(SkCanvas *target_canvas)
Definition: canvas_spy.cc:9
int32_t width
int32_t height
constexpr SkColor colors[]