blob: da9ca54cfe3b69f62bb7e97b6e8a5c7ad5a89866 [file] [log] [blame]
Mike Klein8f4e2242019-03-20 11:59:00 -05001/*
2 * Copyright 2012 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
Brian Salomon8f7d9532020-12-23 09:16:59 -05008#include "tools/debugger/DebugCanvas.h"
9
Nathaniel Nifong642a10b2020-08-20 12:33:46 -040010#include "include/core/SkPaint.h"
11#include "include/core/SkPath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPicture.h"
Nathaniel Nifong642a10b2020-08-20 12:33:46 -040013#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkTextBlob.h"
Robert Phillips2c21a112020-11-20 13:49:37 -050015#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/utils/SkPaintFilterCanvas.h"
17#include "src/core/SkCanvasPriv.h"
18#include "src/core/SkClipOpPriv.h"
19#include "src/core/SkRectPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrAuditTrail.h"
Robert Phillipsb27b38b2020-07-10 16:23:47 -040021#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050022#include "src/gpu/GrSurfaceDrawContext.h"
Brian Salomon8f7d9532020-12-23 09:16:59 -050023#include "src/utils/SkJSONWriter.h"
24#include "tools/debugger/DebugLayerManager.h"
25#include "tools/debugger/DrawCommand.h"
Mike Klein8f4e2242019-03-20 11:59:00 -050026
Nathaniel Nifong20b177a2019-12-12 11:05:10 -050027#include <string>
28
Mike Klein8f4e2242019-03-20 11:59:00 -050029#define SKDEBUGCANVAS_VERSION 1
30#define SKDEBUGCANVAS_ATTRIBUTE_VERSION "version"
31#define SKDEBUGCANVAS_ATTRIBUTE_COMMANDS "commands"
32#define SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL "auditTrail"
33
Nathaniel Nifong20b177a2019-12-12 11:05:10 -050034namespace {
35 // Constants used in Annotations by Android for keeping track of layers
36 static constexpr char kOffscreenLayerDraw[] = "OffscreenLayerDraw";
37 static constexpr char kSurfaceID[] = "SurfaceID";
Nathaniel Nifong12b2c272020-01-10 14:38:00 -050038 static constexpr char kAndroidClip[] = "AndroidDeviceClipRestriction";
Nathaniel Nifong642a10b2020-08-20 12:33:46 -040039
40 static SkPath arrowHead = SkPath::Polygon({
41 { 0, 0},
42 { 6, -15},
43 { 0, -12},
44 {-6, -15},
45 }, true);
46
47 void drawArrow(SkCanvas* canvas, const SkPoint& a, const SkPoint& b, const SkPaint& paint) {
Nathaniel Nifong0f7aa542020-09-02 15:40:37 -040048 canvas->translate(0.5, 0.5);
Nathaniel Nifong642a10b2020-08-20 12:33:46 -040049 canvas->drawLine(a, b, paint);
50 canvas->save();
51 canvas->translate(b.fX, b.fY);
52 SkScalar angle = SkScalarATan2((b.fY - a.fY), b.fX - a.fX);
53 canvas->rotate(angle * 180 / SK_ScalarPI - 90);
54 // arrow head
55 canvas->drawPath(arrowHead, paint);
56 canvas->restore();
Nathaniel Nifong0f7aa542020-09-02 15:40:37 -040057 canvas->restore();
Nathaniel Nifong642a10b2020-08-20 12:33:46 -040058 }
Nathaniel Nifong20b177a2019-12-12 11:05:10 -050059} // namespace
60
Mike Klein8f4e2242019-03-20 11:59:00 -050061class DebugPaintFilterCanvas : public SkPaintFilterCanvas {
62public:
Nathaniel Nifongc84ad832019-12-11 15:24:48 -050063 DebugPaintFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) {}
Mike Klein8f4e2242019-03-20 11:59:00 -050064
65protected:
Ben Wagnerf55fa0d2018-08-27 18:11:57 -040066 bool onFilter(SkPaint& paint) const override {
Nathaniel Nifongc84ad832019-12-11 15:24:48 -050067 paint.setColor(SK_ColorRED);
68 paint.setAlpha(0x08);
69 paint.setBlendMode(SkBlendMode::kSrcOver);
Mike Klein8f4e2242019-03-20 11:59:00 -050070 return true;
71 }
72
73 void onDrawPicture(const SkPicture* picture,
74 const SkMatrix* matrix,
75 const SkPaint* paint) override {
76 // We need to replay the picture onto this canvas in order to filter its internal paints.
77 this->SkCanvas::onDrawPicture(picture, matrix, paint);
78 }
79
80private:
Mike Klein8f4e2242019-03-20 11:59:00 -050081
John Stiles7571f9e2020-09-02 22:42:33 -040082 using INHERITED = SkPaintFilterCanvas;
Mike Klein8f4e2242019-03-20 11:59:00 -050083};
84
85DebugCanvas::DebugCanvas(int width, int height)
86 : INHERITED(width, height)
87 , fOverdrawViz(false)
88 , fClipVizColor(SK_ColorTRANSPARENT)
Nathaniel Nifong20b177a2019-12-12 11:05:10 -050089 , fDrawGpuOpBounds(false)
Nathaniel Nifong12b2c272020-01-10 14:38:00 -050090 , fShowAndroidClip(false)
Nathaniel Nifong642a10b2020-08-20 12:33:46 -040091 , fShowOrigin(false)
Nathaniel Nifong20b177a2019-12-12 11:05:10 -050092 , fnextDrawPictureLayerId(-1)
Nathaniel Nifong12b2c272020-01-10 14:38:00 -050093 , fnextDrawImageRectLayerId(-1)
94 , fAndroidClip(SkRect::MakeEmpty()) {
Mike Klein8f4e2242019-03-20 11:59:00 -050095 // SkPicturePlayback uses the base-class' quickReject calls to cull clipped
96 // operations. This can lead to problems in the debugger which expects all
97 // the operations in the captured skp to appear in the debug canvas. To
98 // circumvent this we create a wide open clip here (an empty clip rect
99 // is not sufficient).
100 // Internally, the SkRect passed to clipRect is converted to an SkIRect and
101 // rounded out. The following code creates a nearly maximal rect that will
102 // not get collapsed by the coming conversions (Due to precision loss the
103 // inset has to be surprisingly large).
104 SkIRect largeIRect = SkRectPriv::MakeILarge();
105 largeIRect.inset(1024, 1024);
106 SkRect large = SkRect::Make(largeIRect);
107#ifdef SK_DEBUG
108 SkASSERT(!large.roundOut().isEmpty());
109#endif
110 // call the base class' version to avoid adding a draw command
Michael Ludwige4b79692020-09-16 13:55:05 -0400111 this->INHERITED::onClipRect(large, SkClipOp::kIntersect, kHard_ClipEdgeStyle);
Mike Klein8f4e2242019-03-20 11:59:00 -0500112}
113
John Stiles214de112020-07-29 17:26:28 -0400114DebugCanvas::DebugCanvas(SkIRect bounds)
115 : DebugCanvas(bounds.width(), bounds.height()) {}
Mike Klein8f4e2242019-03-20 11:59:00 -0500116
117DebugCanvas::~DebugCanvas() { fCommandVector.deleteAll(); }
118
119void DebugCanvas::addDrawCommand(DrawCommand* command) { fCommandVector.push_back(command); }
120
121void DebugCanvas::draw(SkCanvas* canvas) {
122 if (!fCommandVector.isEmpty()) {
123 this->drawTo(canvas, fCommandVector.count() - 1);
124 }
125}
126
127void DebugCanvas::drawTo(SkCanvas* originalCanvas, int index, int m) {
128 SkASSERT(!fCommandVector.isEmpty());
129 SkASSERT(index < fCommandVector.count());
130
131 int saveCount = originalCanvas->save();
132
133 SkRect windowRect = SkRect::MakeWH(SkIntToScalar(originalCanvas->getBaseLayerSize().width()),
134 SkIntToScalar(originalCanvas->getBaseLayerSize().height()));
135
Mike Klein8f4e2242019-03-20 11:59:00 -0500136 originalCanvas->resetMatrix();
137 if (!windowRect.isEmpty()) {
138 originalCanvas->clipRect(windowRect, kReplace_SkClipOp);
139 }
140
Nathaniel Nifongc84ad832019-12-11 15:24:48 -0500141 DebugPaintFilterCanvas filterCanvas(originalCanvas);
142 SkCanvas* finalCanvas = fOverdrawViz ? &filterCanvas : originalCanvas;
Mike Klein8f4e2242019-03-20 11:59:00 -0500143
Robert Phillips2c21a112020-11-20 13:49:37 -0500144 auto dContext = GrAsDirectContext(finalCanvas->recordingContext());
145
Mike Klein8f4e2242019-03-20 11:59:00 -0500146 // If we have a GPU backend we can also visualize the op information
147 GrAuditTrail* at = nullptr;
148 if (fDrawGpuOpBounds || m != -1) {
149 // The audit trail must be obtained from the original canvas.
150 at = this->getAuditTrail(originalCanvas);
151 }
152
153 for (int i = 0; i <= index; i++) {
Mike Klein8f4e2242019-03-20 11:59:00 -0500154 GrAuditTrail::AutoCollectOps* acb = nullptr;
155 if (at) {
Nathaniel Nifongc84ad832019-12-11 15:24:48 -0500156 // We need to flush any pending operations, or they might combine with commands below.
157 // Previous operations were not registered with the audit trail when they were
158 // created, so if we allow them to combine, the audit trail will fail to find them.
Robert Phillips2c21a112020-11-20 13:49:37 -0500159 if (dContext) {
160 dContext->flush();
161 }
Mike Klein8f4e2242019-03-20 11:59:00 -0500162 acb = new GrAuditTrail::AutoCollectOps(at, i);
163 }
Mike Klein8f4e2242019-03-20 11:59:00 -0500164 if (fCommandVector[i]->isVisible()) {
Nathaniel Nifongc84ad832019-12-11 15:24:48 -0500165 fCommandVector[i]->execute(finalCanvas);
Mike Klein8f4e2242019-03-20 11:59:00 -0500166 }
167 if (at && acb) {
168 delete acb;
169 }
170 }
171
172 if (SkColorGetA(fClipVizColor) != 0) {
Nathaniel Nifongc84ad832019-12-11 15:24:48 -0500173 finalCanvas->save();
Mike Klein8f4e2242019-03-20 11:59:00 -0500174 SkPaint clipPaint;
175 clipPaint.setColor(fClipVizColor);
Nathaniel Nifongc84ad832019-12-11 15:24:48 -0500176 finalCanvas->drawPaint(clipPaint);
177 finalCanvas->restore();
Mike Klein8f4e2242019-03-20 11:59:00 -0500178 }
179
Mike Reed1a4140e2020-12-03 11:21:31 -0500180 fMatrix = finalCanvas->getLocalToDevice();
Nathaniel Nifongc84ad832019-12-11 15:24:48 -0500181 fClip = finalCanvas->getDeviceClipBounds();
Nathaniel Nifong642a10b2020-08-20 12:33:46 -0400182 if (fShowOrigin) {
183 const SkPaint originXPaint = SkPaint({1.0, 0, 0, 1.0});
184 const SkPaint originYPaint = SkPaint({0, 1.0, 0, 1.0});
185 // Draw an origin cross at the origin before restoring to assist in visualizing the
186 // current matrix.
187 drawArrow(finalCanvas, {-50, 0}, {50, 0}, originXPaint);
188 drawArrow(finalCanvas, {0, -50}, {0, 50}, originYPaint);
189 }
Nathaniel Nifongc84ad832019-12-11 15:24:48 -0500190 finalCanvas->restoreToCount(saveCount);
Mike Klein8f4e2242019-03-20 11:59:00 -0500191
Nathaniel Nifong12b2c272020-01-10 14:38:00 -0500192 if (fShowAndroidClip) {
193 // Draw visualization of android device clip restriction
194 SkPaint androidClipPaint;
195 androidClipPaint.setARGB(80, 255, 100, 0);
196 finalCanvas->drawRect(fAndroidClip, androidClipPaint);
197 }
198
Mike Klein8f4e2242019-03-20 11:59:00 -0500199 // draw any ops if required and issue a full reset onto GrAuditTrail
200 if (at) {
201 // just in case there is global reordering, we flush the canvas before querying
202 // GrAuditTrail
203 GrAuditTrail::AutoEnable ae(at);
Robert Phillips2c21a112020-11-20 13:49:37 -0500204 if (dContext) {
205 dContext->flush();
206 }
Mike Klein8f4e2242019-03-20 11:59:00 -0500207
208 // we pick three colorblind-safe colors, 75% alpha
209 static const SkColor kTotalBounds = SkColorSetARGB(0xC0, 0x6A, 0x3D, 0x9A);
210 static const SkColor kCommandOpBounds = SkColorSetARGB(0xC0, 0xE3, 0x1A, 0x1C);
211 static const SkColor kOtherOpBounds = SkColorSetARGB(0xC0, 0xFF, 0x7F, 0x00);
212
213 // get the render target of the top device (from the original canvas) so we can ignore ops
214 // drawn offscreen
Brian Salomon8f7d9532020-12-23 09:16:59 -0500215 GrSurfaceDrawContext* sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(originalCanvas);
216 GrSurfaceProxy::UniqueID proxyID = sdc->asSurfaceProxy()->uniqueID();
Mike Klein8f4e2242019-03-20 11:59:00 -0500217
218 // get the bounding boxes to draw
219 SkTArray<GrAuditTrail::OpInfo> childrenBounds;
220 if (m == -1) {
221 at->getBoundsByClientID(&childrenBounds, index);
222 } else {
223 // the client wants us to draw the mth op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400224 at->getBoundsByOpsTaskID(&childrenBounds.push_back(), m);
Mike Klein8f4e2242019-03-20 11:59:00 -0500225 }
Nathaniel Nifongc84ad832019-12-11 15:24:48 -0500226 // Shift the rects half a pixel, so they appear as exactly 1px thick lines.
227 finalCanvas->save();
228 finalCanvas->translate(0.5, -0.5);
Mike Klein8f4e2242019-03-20 11:59:00 -0500229 SkPaint paint;
230 paint.setStyle(SkPaint::kStroke_Style);
231 paint.setStrokeWidth(1);
232 for (int i = 0; i < childrenBounds.count(); i++) {
233 if (childrenBounds[i].fProxyUniqueID != proxyID) {
234 // offscreen draw, ignore for now
235 continue;
236 }
237 paint.setColor(kTotalBounds);
Nathaniel Nifongc84ad832019-12-11 15:24:48 -0500238 finalCanvas->drawRect(childrenBounds[i].fBounds, paint);
Mike Klein8f4e2242019-03-20 11:59:00 -0500239 for (int j = 0; j < childrenBounds[i].fOps.count(); j++) {
240 const GrAuditTrail::OpInfo::Op& op = childrenBounds[i].fOps[j];
241 if (op.fClientID != index) {
242 paint.setColor(kOtherOpBounds);
243 } else {
244 paint.setColor(kCommandOpBounds);
245 }
Nathaniel Nifongc84ad832019-12-11 15:24:48 -0500246 finalCanvas->drawRect(op.fBounds, paint);
Mike Klein8f4e2242019-03-20 11:59:00 -0500247 }
248 }
Nathaniel Nifongc84ad832019-12-11 15:24:48 -0500249 finalCanvas->restore();
Mike Klein8f4e2242019-03-20 11:59:00 -0500250 }
251 this->cleanupAuditTrail(originalCanvas);
252}
253
254void DebugCanvas::deleteDrawCommandAt(int index) {
255 SkASSERT(index < fCommandVector.count());
256 delete fCommandVector[index];
257 fCommandVector.remove(index);
258}
259
Nathaniel Nifong0b448da2020-09-16 10:35:22 -0400260DrawCommand* DebugCanvas::getDrawCommandAt(int index) const {
Mike Klein8f4e2242019-03-20 11:59:00 -0500261 SkASSERT(index < fCommandVector.count());
262 return fCommandVector[index];
263}
264
265GrAuditTrail* DebugCanvas::getAuditTrail(SkCanvas* canvas) {
266 GrAuditTrail* at = nullptr;
Robert Phillipsb27b38b2020-07-10 16:23:47 -0400267 auto ctx = canvas->recordingContext();
Mike Klein8f4e2242019-03-20 11:59:00 -0500268 if (ctx) {
269 at = ctx->priv().auditTrail();
270 }
271 return at;
272}
273
Nathaniel Nifonga072b7b2019-12-13 13:51:14 -0500274void DebugCanvas::drawAndCollectOps(SkCanvas* canvas) {
Mike Klein8f4e2242019-03-20 11:59:00 -0500275 GrAuditTrail* at = this->getAuditTrail(canvas);
276 if (at) {
277 // loop over all of the commands and draw them, this is to collect reordering
278 // information
Nathaniel Nifonga072b7b2019-12-13 13:51:14 -0500279 for (int i = 0; i < this->getSize(); i++) {
Mike Klein8f4e2242019-03-20 11:59:00 -0500280 GrAuditTrail::AutoCollectOps enable(at, i);
281 fCommandVector[i]->execute(canvas);
282 }
283
284 // in case there is some kind of global reordering
285 {
286 GrAuditTrail::AutoEnable ae(at);
Robert Phillips2c21a112020-11-20 13:49:37 -0500287
288 auto dContext = GrAsDirectContext(canvas->recordingContext());
289 if (dContext) {
290 dContext->flush();
291 }
Mike Klein8f4e2242019-03-20 11:59:00 -0500292 }
293 }
294}
295
296void DebugCanvas::cleanupAuditTrail(SkCanvas* canvas) {
297 GrAuditTrail* at = this->getAuditTrail(canvas);
298 if (at) {
299 GrAuditTrail::AutoEnable ae(at);
300 at->fullReset();
301 }
302}
303
304void DebugCanvas::toJSON(SkJSONWriter& writer,
305 UrlDataManager& urlDataManager,
Mike Klein8f4e2242019-03-20 11:59:00 -0500306 SkCanvas* canvas) {
Nathaniel Nifonga072b7b2019-12-13 13:51:14 -0500307 this->drawAndCollectOps(canvas);
Mike Klein8f4e2242019-03-20 11:59:00 -0500308
309 // now collect json
310 GrAuditTrail* at = this->getAuditTrail(canvas);
311 writer.appendS32(SKDEBUGCANVAS_ATTRIBUTE_VERSION, SKDEBUGCANVAS_VERSION);
312 writer.beginArray(SKDEBUGCANVAS_ATTRIBUTE_COMMANDS);
313
Nathaniel Nifonga072b7b2019-12-13 13:51:14 -0500314 for (int i = 0; i < this->getSize(); i++) {
Mike Klein8f4e2242019-03-20 11:59:00 -0500315 writer.beginObject(); // command
316 this->getDrawCommandAt(i)->toJSON(writer, urlDataManager);
317
318 if (at) {
319 writer.appendName(SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL);
320 at->toJson(writer, i);
321 }
322 writer.endObject(); // command
323 }
324
325 writer.endArray(); // commands
326 this->cleanupAuditTrail(canvas);
327}
328
Nathaniel Nifonga072b7b2019-12-13 13:51:14 -0500329void DebugCanvas::toJSONOpsTask(SkJSONWriter& writer, SkCanvas* canvas) {
330 this->drawAndCollectOps(canvas);
Mike Klein8f4e2242019-03-20 11:59:00 -0500331
332 GrAuditTrail* at = this->getAuditTrail(canvas);
333 if (at) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400334 GrAuditTrail::AutoManageOpsTask enable(at);
Mike Klein8f4e2242019-03-20 11:59:00 -0500335 at->toJson(writer);
336 } else {
337 writer.beginObject();
338 writer.endObject();
339 }
340 this->cleanupAuditTrail(canvas);
341}
342
343void DebugCanvas::setOverdrawViz(bool overdrawViz) { fOverdrawViz = overdrawViz; }
344
345void DebugCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
Mike Klein17428132019-03-20 13:02:32 -0500346 this->addDrawCommand(new ClipPathCommand(path, op, kSoft_ClipEdgeStyle == edgeStyle));
Mike Klein8f4e2242019-03-20 11:59:00 -0500347}
348
349void DebugCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
Mike Klein17428132019-03-20 13:02:32 -0500350 this->addDrawCommand(new ClipRectCommand(rect, op, kSoft_ClipEdgeStyle == edgeStyle));
Mike Klein8f4e2242019-03-20 11:59:00 -0500351}
352
353void DebugCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
Mike Klein17428132019-03-20 13:02:32 -0500354 this->addDrawCommand(new ClipRRectCommand(rrect, op, kSoft_ClipEdgeStyle == edgeStyle));
Mike Klein8f4e2242019-03-20 11:59:00 -0500355}
356
357void DebugCanvas::onClipRegion(const SkRegion& region, SkClipOp op) {
Mike Klein17428132019-03-20 13:02:32 -0500358 this->addDrawCommand(new ClipRegionCommand(region, op));
Mike Klein8f4e2242019-03-20 11:59:00 -0500359}
360
Mike Reed121c2af2020-03-10 14:02:56 -0400361void DebugCanvas::onClipShader(sk_sp<SkShader> cs, SkClipOp op) {
362 this->addDrawCommand(new ClipShaderCommand(std::move(cs), op));
363}
364
Mike Reed7d45a7a2020-04-18 10:27:52 -0400365void DebugCanvas::didConcat44(const SkM44& m) {
Nathaniel Nifong642a10b2020-08-20 12:33:46 -0400366 this->addDrawCommand(new Concat44Command(m));
Mike Reeda3a704a2020-01-10 17:21:40 -0500367 this->INHERITED::didConcat44(m);
368}
369
370void DebugCanvas::didScale(SkScalar x, SkScalar y) {
Mike Reed420a9ba2020-11-25 13:37:30 -0500371 this->didConcat44(SkM44::Scale(x, y));
Mike Reeda3a704a2020-01-10 17:21:40 -0500372}
373
374void DebugCanvas::didTranslate(SkScalar x, SkScalar y) {
Mike Reed420a9ba2020-11-25 13:37:30 -0500375 this->didConcat44(SkM44::Translate(x, y));
Mike Reeda3a704a2020-01-10 17:21:40 -0500376}
377
Mike Klein8f4e2242019-03-20 11:59:00 -0500378void DebugCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
Nathaniel Nifong20b177a2019-12-12 11:05:10 -0500379 // Parse layer-releated annotations added in SkiaPipeline.cpp and RenderNodeDrawable.cpp
380 // the format of the annotations is <Indicator|RenderNodeId>
381 SkTArray<SkString> tokens;
382 SkStrSplit(key, "|", kStrict_SkStrSplitMode, &tokens);
383 if (tokens.size() == 2) {
384 if (tokens[0].equals(kOffscreenLayerDraw)) {
Nathaniel Nifong642a10b2020-08-20 12:33:46 -0400385 // Indicates that the next drawPicture command contains the SkPicture to render the
386 // node at this id in an offscreen buffer.
Nathaniel Nifong20b177a2019-12-12 11:05:10 -0500387 fnextDrawPictureLayerId = std::stoi(tokens[1].c_str());
388 fnextDrawPictureDirtyRect = rect.roundOut();
389 return; // don't record it
390 } else if (tokens[0].equals(kSurfaceID)) {
391 // Indicates that the following drawImageRect should draw the offscreen buffer.
392 fnextDrawImageRectLayerId = std::stoi(tokens[1].c_str());
393 return; // don't record it
394 }
395 }
Nathaniel Nifong12b2c272020-01-10 14:38:00 -0500396 if (strcmp(kAndroidClip, key) == 0) {
397 // Store this frame's android device clip restriction for visualization later.
398 // This annotation stands in place of the androidFramework_setDeviceClipRestriction
399 // which is unrecordable.
400 fAndroidClip = rect;
401 }
Mike Klein17428132019-03-20 13:02:32 -0500402 this->addDrawCommand(new DrawAnnotationCommand(rect, key, sk_ref_sp(value)));
Mike Klein8f4e2242019-03-20 11:59:00 -0500403}
404
Mike Reed4f23dec2020-12-30 14:22:42 +0000405void DebugCanvas::onDrawImage2(const SkImage* image,
406 SkScalar left,
407 SkScalar top,
Mike Reed18aeb572021-01-19 17:58:25 -0500408 const SkSamplingOptions& sampling,
Mike Reed4f23dec2020-12-30 14:22:42 +0000409 const SkPaint* paint) {
Mike Reed18aeb572021-01-19 17:58:25 -0500410 this->addDrawCommand(new DrawImageCommand(image, left, top, sampling, paint));
Mike Reed4f23dec2020-12-30 14:22:42 +0000411}
412
413void DebugCanvas::onDrawImageLattice2(const SkImage* image,
414 const Lattice& lattice,
415 const SkRect& dst,
416 SkFilterMode filter, // todo
417 const SkPaint* paint) {
Mike Reed99116302021-01-25 11:37:10 -0500418 this->addDrawCommand(new DrawImageLatticeCommand(image, lattice, dst, filter, paint));
Mike Reed4f23dec2020-12-30 14:22:42 +0000419}
420
421void DebugCanvas::onDrawImageRect2(const SkImage* image,
422 const SkRect& src,
423 const SkRect& dst,
Mike Reed18aeb572021-01-19 17:58:25 -0500424 const SkSamplingOptions& sampling,
Mike Reed4f23dec2020-12-30 14:22:42 +0000425 const SkPaint* paint,
426 SrcRectConstraint constraint) {
427 if (fnextDrawImageRectLayerId != -1 && fLayerManager) {
428 // This drawImageRect command would have drawn the offscreen buffer for a layer.
429 // On Android, we recorded an SkPicture of the commands that drew to the layer.
430 // To render the layer as it would have looked on the frame this DebugCanvas draws, we need
431 // to call fLayerManager->getLayerAsImage(id). This must be done just before
432 // drawTo(command), since it depends on the index into the layer's commands
433 // (managed by fLayerManager)
434 // Instead of adding a DrawImageRectCommand, we need a deferred command, that when
435 // executed, will call drawImageRect(fLayerManager->getLayerAsImage())
436 this->addDrawCommand(new DrawImageRectLayerCommand(
Mike Reede02d7f82021-01-21 22:25:21 -0500437 fLayerManager, fnextDrawImageRectLayerId, fFrame, src, dst, sampling,
438 paint, constraint));
Mike Reed4f23dec2020-12-30 14:22:42 +0000439 } else {
Mike Reed18aeb572021-01-19 17:58:25 -0500440 this->addDrawCommand(new DrawImageRectCommand(image, src, dst, sampling, paint, constraint));
Mike Reed4f23dec2020-12-30 14:22:42 +0000441 }
442 // Reset expectation so next drawImageRect is not special.
443 fnextDrawImageRectLayerId = -1;
444}
Mike Klein8f4e2242019-03-20 11:59:00 -0500445
Mike Klein8f4e2242019-03-20 11:59:00 -0500446void DebugCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
Mike Klein17428132019-03-20 13:02:32 -0500447 this->addDrawCommand(new DrawOvalCommand(oval, paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500448}
449
450void DebugCanvas::onDrawArc(const SkRect& oval,
451 SkScalar startAngle,
452 SkScalar sweepAngle,
453 bool useCenter,
454 const SkPaint& paint) {
Mike Klein17428132019-03-20 13:02:32 -0500455 this->addDrawCommand(new DrawArcCommand(oval, startAngle, sweepAngle, useCenter, paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500456}
457
458void DebugCanvas::onDrawPaint(const SkPaint& paint) {
Mike Klein17428132019-03-20 13:02:32 -0500459 this->addDrawCommand(new DrawPaintCommand(paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500460}
461
Mike Reedd5674082019-04-19 15:00:47 -0400462void DebugCanvas::onDrawBehind(const SkPaint& paint) {
463 this->addDrawCommand(new DrawBehindCommand(paint));
464}
465
Mike Klein8f4e2242019-03-20 11:59:00 -0500466void DebugCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
Mike Klein17428132019-03-20 13:02:32 -0500467 this->addDrawCommand(new DrawPathCommand(path, paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500468}
469
470void DebugCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
Mike Klein17428132019-03-20 13:02:32 -0500471 this->addDrawCommand(new DrawRegionCommand(region, paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500472}
473
474void DebugCanvas::onDrawPicture(const SkPicture* picture,
475 const SkMatrix* matrix,
476 const SkPaint* paint) {
Nathaniel Nifong20b177a2019-12-12 11:05:10 -0500477 if (fnextDrawPictureLayerId != -1 && fLayerManager) {
478 fLayerManager->storeSkPicture(fnextDrawPictureLayerId, fFrame, sk_ref_sp(picture),
479 fnextDrawPictureDirtyRect);
480 } else {
481 this->addDrawCommand(new BeginDrawPictureCommand(picture, matrix, paint));
482 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
483 picture->playback(this);
484 this->addDrawCommand(new EndDrawPictureCommand(SkToBool(matrix) || SkToBool(paint)));
485 }
486 fnextDrawPictureLayerId = -1;
Mike Klein8f4e2242019-03-20 11:59:00 -0500487}
488
489void DebugCanvas::onDrawPoints(PointMode mode,
490 size_t count,
491 const SkPoint pts[],
492 const SkPaint& paint) {
Mike Klein17428132019-03-20 13:02:32 -0500493 this->addDrawCommand(new DrawPointsCommand(mode, count, pts, paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500494}
495
496void DebugCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
497 // NOTE(chudy): Messing up when renamed to DrawRect... Why?
Mike Klein17428132019-03-20 13:02:32 -0500498 addDrawCommand(new DrawRectCommand(rect, paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500499}
500
501void DebugCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
Mike Klein17428132019-03-20 13:02:32 -0500502 this->addDrawCommand(new DrawRRectCommand(rrect, paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500503}
504
505void DebugCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
Mike Klein17428132019-03-20 13:02:32 -0500506 this->addDrawCommand(new DrawDRRectCommand(outer, inner, paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500507}
508
509void DebugCanvas::onDrawTextBlob(const SkTextBlob* blob,
510 SkScalar x,
511 SkScalar y,
512 const SkPaint& paint) {
513 this->addDrawCommand(
Mike Klein17428132019-03-20 13:02:32 -0500514 new DrawTextBlobCommand(sk_ref_sp(const_cast<SkTextBlob*>(blob)), x, y, paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500515}
516
517void DebugCanvas::onDrawPatch(const SkPoint cubics[12],
518 const SkColor colors[4],
519 const SkPoint texCoords[4],
520 SkBlendMode bmode,
521 const SkPaint& paint) {
Mike Klein17428132019-03-20 13:02:32 -0500522 this->addDrawCommand(new DrawPatchCommand(cubics, colors, texCoords, bmode, paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500523}
524
Mike Reeda2cf8ae2020-03-02 17:08:01 -0500525void DebugCanvas::onDrawVerticesObject(const SkVertices* vertices,
526 SkBlendMode bmode,
527 const SkPaint& paint) {
528 this->addDrawCommand(
529 new DrawVerticesCommand(sk_ref_sp(const_cast<SkVertices*>(vertices)), bmode, paint));
530}
Mike Klein8f4e2242019-03-20 11:59:00 -0500531
Mike Reed4f23dec2020-12-30 14:22:42 +0000532void DebugCanvas::onDrawAtlas2(const SkImage* image,
533 const SkRSXform xform[],
534 const SkRect tex[],
535 const SkColor colors[],
536 int count,
537 SkBlendMode bmode,
538 const SkSamplingOptions& sampling,
539 const SkRect* cull,
540 const SkPaint* paint) {
Mike Klein8f4e2242019-03-20 11:59:00 -0500541 this->addDrawCommand(
Mike Reed99116302021-01-25 11:37:10 -0500542 new DrawAtlasCommand(image, xform, tex, colors, count, bmode, sampling, cull, paint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500543}
544
545void DebugCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
Mike Klein17428132019-03-20 13:02:32 -0500546 this->addDrawCommand(new DrawShadowCommand(path, rec));
Mike Klein8f4e2242019-03-20 11:59:00 -0500547}
548
549void DebugCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
Mike Klein17428132019-03-20 13:02:32 -0500550 this->addDrawCommand(new DrawDrawableCommand(drawable, matrix));
Mike Klein8f4e2242019-03-20 11:59:00 -0500551}
552
Michael Ludwiga595f862019-08-27 15:25:49 -0400553void DebugCanvas::onDrawEdgeAAQuad(const SkRect& rect,
554 const SkPoint clip[4],
555 QuadAAFlags aa,
556 const SkColor4f& color,
557 SkBlendMode mode) {
Mike Klein17428132019-03-20 13:02:32 -0500558 this->addDrawCommand(new DrawEdgeAAQuadCommand(rect, clip, aa, color, mode));
Mike Klein8f4e2242019-03-20 11:59:00 -0500559}
560
Mike Reed4f509342021-01-05 12:03:47 -0500561void DebugCanvas::onDrawEdgeAAImageSet2(const ImageSetEntry set[],
562 int count,
563 const SkPoint dstClips[],
564 const SkMatrix preViewMatrices[],
565 const SkSamplingOptions& sampling,
566 const SkPaint* paint,
567 SrcRectConstraint constraint) {
Mike Klein17428132019-03-20 13:02:32 -0500568 this->addDrawCommand(new DrawEdgeAAImageSetCommand(
Mike Reed4f509342021-01-05 12:03:47 -0500569 set, count, dstClips, preViewMatrices, sampling, paint, constraint));
Mike Klein8f4e2242019-03-20 11:59:00 -0500570}
571
572void DebugCanvas::willRestore() {
Mike Klein17428132019-03-20 13:02:32 -0500573 this->addDrawCommand(new RestoreCommand());
Mike Klein8f4e2242019-03-20 11:59:00 -0500574 this->INHERITED::willRestore();
575}
576
577void DebugCanvas::willSave() {
Mike Klein17428132019-03-20 13:02:32 -0500578 this->addDrawCommand(new SaveCommand());
Mike Klein8f4e2242019-03-20 11:59:00 -0500579 this->INHERITED::willSave();
580}
581
582SkCanvas::SaveLayerStrategy DebugCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
Mike Klein17428132019-03-20 13:02:32 -0500583 this->addDrawCommand(new SaveLayerCommand(rec));
Mike Klein8f4e2242019-03-20 11:59:00 -0500584 (void)this->INHERITED::getSaveLayerStrategy(rec);
585 // No need for a full layer.
586 return kNoLayer_SaveLayerStrategy;
587}
588
589bool DebugCanvas::onDoSaveBehind(const SkRect* subset) {
590 // TODO
591 return false;
592}
593
Mike Reed420a9ba2020-11-25 13:37:30 -0500594void DebugCanvas::didSetM44(const SkM44& matrix) {
595 this->addDrawCommand(new SetM44Command(matrix));
596 this->INHERITED::didSetM44(matrix);
597}
598
Mike Klein8f4e2242019-03-20 11:59:00 -0500599void DebugCanvas::toggleCommand(int index, bool toggle) {
600 SkASSERT(index < fCommandVector.count());
601 fCommandVector[index]->setVisible(toggle);
602}
Nathaniel Nifong0b448da2020-09-16 10:35:22 -0400603
604std::map<int, std::vector<int>> DebugCanvas::getImageIdToCommandMap(UrlDataManager& udm) const {
605 // map from image ids to list of commands that reference them.
606 std::map<int, std::vector<int>> m;
607
608 for (int i = 0; i < this->getSize(); i++) {
609 const DrawCommand* command = this->getDrawCommandAt(i);
610 int imageIndex = -1;
611 // this is not an exaustive list of where images can be used, they show up in paints too.
612 switch (command->getOpType()) {
613 case DrawCommand::OpType::kDrawImage_OpType: {
614 imageIndex = static_cast<const DrawImageCommand*>(command)->imageId(udm);
615 break;
616 }
617 case DrawCommand::OpType::kDrawImageRect_OpType: {
618 imageIndex = static_cast<const DrawImageRectCommand*>(command)->imageId(udm);
619 break;
620 }
Nathaniel Nifong0b448da2020-09-16 10:35:22 -0400621 case DrawCommand::OpType::kDrawImageLattice_OpType: {
622 imageIndex = static_cast<const DrawImageLatticeCommand*>(command)->imageId(udm);
623 break;
624 }
625 default: break;
626 }
627 if (imageIndex >= 0) {
628 m[imageIndex].push_back(i);
629 }
630 }
631 return m;
632}