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::didConcat(const SkMatrix& matrix) {}
55 
56 void DidDrawCanvas::didConcat44(const SkM44&) {}
57 
58 void DidDrawCanvas::didScale(SkScalar, SkScalar) {}
59 
60 void DidDrawCanvas::didTranslate(SkScalar, SkScalar) {}
61 
62 void DidDrawCanvas::didSetMatrix(const SkMatrix& matrix) {}
63 
64 void DidDrawCanvas::onClipRect(const SkRect& rect,
65  SkClipOp op,
66  ClipEdgeStyle edgeStyle) {}
67 
68 void DidDrawCanvas::onClipRRect(const SkRRect& rrect,
69  SkClipOp op,
70  ClipEdgeStyle edgeStyle) {}
71 
72 void DidDrawCanvas::onClipPath(const SkPath& path,
73  SkClipOp op,
74  ClipEdgeStyle edgeStyle) {}
75 
76 void DidDrawCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {}
77 
78 void DidDrawCanvas::onDrawPaint(const SkPaint& paint) {
79  MarkDrawIfNonTransparentPaint(paint);
80 }
81 
82 void DidDrawCanvas::onDrawBehind(const SkPaint& paint) {
83  MarkDrawIfNonTransparentPaint(paint);
84 }
85 
86 void DidDrawCanvas::onDrawPoints(PointMode mode,
87  size_t count,
88  const SkPoint pts[],
89  const SkPaint& paint) {
90  MarkDrawIfNonTransparentPaint(paint);
91 }
92 
93 void DidDrawCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
94  MarkDrawIfNonTransparentPaint(paint);
95 }
96 
97 void DidDrawCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
98  MarkDrawIfNonTransparentPaint(paint);
99 }
100 
101 void DidDrawCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
102  MarkDrawIfNonTransparentPaint(paint);
103 }
104 
105 void DidDrawCanvas::onDrawArc(const SkRect& rect,
106  SkScalar startAngle,
107  SkScalar sweepAngle,
108  bool useCenter,
109  const SkPaint& paint) {
110  MarkDrawIfNonTransparentPaint(paint);
111 }
112 
113 void DidDrawCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
114  MarkDrawIfNonTransparentPaint(paint);
115 }
116 
117 void DidDrawCanvas::onDrawDRRect(const SkRRect& outer,
118  const SkRRect& inner,
119  const SkPaint& paint) {
120  MarkDrawIfNonTransparentPaint(paint);
121 }
122 
123 void DidDrawCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
124  MarkDrawIfNonTransparentPaint(paint);
125 }
126 
127 void DidDrawCanvas::onDrawImage(const SkImage* image,
128  SkScalar left,
129  SkScalar top,
130  const SkPaint* paint) {
131  did_draw_ = true;
132 }
133 
134 void DidDrawCanvas::onDrawImageRect(const SkImage* image,
135  const SkRect* src,
136  const SkRect& dst,
137  const SkPaint* paint,
138  SrcRectConstraint constraint) {
139  did_draw_ = true;
140 }
141 
142 void DidDrawCanvas::onDrawImageNine(const SkImage* image,
143  const SkIRect& center,
144  const SkRect& dst,
145  const SkPaint* paint) {
146  did_draw_ = true;
147 }
148 
149 void DidDrawCanvas::onDrawImageLattice(const SkImage* image,
150  const Lattice& lattice,
151  const SkRect& dst,
152  const SkPaint* paint) {
153  did_draw_ = true;
154 }
155 
156 void DidDrawCanvas::onDrawTextBlob(const SkTextBlob* blob,
157  SkScalar x,
158  SkScalar y,
159  const SkPaint& paint) {
160  MarkDrawIfNonTransparentPaint(paint);
161 }
162 
163 void DidDrawCanvas::onDrawPicture(const SkPicture* picture,
164  const SkMatrix* matrix,
165  const SkPaint* paint) {
166  did_draw_ = true;
167 }
168 
169 void DidDrawCanvas::onDrawDrawable(SkDrawable* drawable,
170  const SkMatrix* matrix) {
171  did_draw_ = true;
172 }
173 
174 void DidDrawCanvas::onDrawVerticesObject(const SkVertices* vertices,
175  SkBlendMode bmode,
176  const SkPaint& paint) {
177  MarkDrawIfNonTransparentPaint(paint);
178 }
179 
180 void DidDrawCanvas::onDrawPatch(const SkPoint cubics[12],
181  const SkColor colors[4],
182  const SkPoint texCoords[4],
183  SkBlendMode bmode,
184  const SkPaint& paint) {
185  MarkDrawIfNonTransparentPaint(paint);
186 }
187 
188 void DidDrawCanvas::onDrawAtlas(const SkImage* image,
189  const SkRSXform xform[],
190  const SkRect tex[],
191  const SkColor colors[],
192  int count,
193  SkBlendMode bmode,
194  const SkRect* cull,
195  const SkPaint* paint) {
196  did_draw_ = true;
197 }
198 
199 void DidDrawCanvas::onDrawShadowRec(const SkPath& path,
200  const SkDrawShadowRec& rec) {
201  did_draw_ = true;
202 }
203 
204 void DidDrawCanvas::onDrawAnnotation(const SkRect& rect,
205  const char key[],
206  SkData* data) {
207  did_draw_ = true;
208 }
209 
210 void DidDrawCanvas::onDrawEdgeAAQuad(const SkRect& rect,
211  const SkPoint clip[4],
212  SkCanvas::QuadAAFlags aa,
213  const SkColor4f& color,
214  SkBlendMode mode) {
215  did_draw_ = true;
216 }
217 
218 void DidDrawCanvas::onDrawEdgeAAImageSet(const ImageSetEntry set[],
219  int count,
220  const SkPoint dstClips[],
221  const SkMatrix preViewMatrices[],
222  const SkPaint* paint,
223  SrcRectConstraint constraint) {
224  did_draw_ = true;
225 }
226 
227 void DidDrawCanvas::onFlush() {}
228 
229 } // 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
int32_t height
int32_t width
CanvasSpy(SkCanvas *target_canvas)
Definition: canvas_spy.cc:9