blob: 0a28bdcd0c99867b180dc09a8d8d18021310fb30 [file] [log] [blame]
chudy@google.com902ebe52012-06-29 14:21:22 +00001/*
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
ethannicholas891ad662016-02-12 07:15:45 -08008#include "SkCanvasPriv.h"
bungemand3ebb482015-08-05 13:57:49 -07009#include "SkClipStack.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000010#include "SkDebugCanvas.h"
11#include "SkDrawCommand.h"
fmalita37283c22016-09-13 10:00:23 -070012#include "SkPaintFilterCanvas.h"
13#include "SkTextBlob.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050014#include "SkClipOpPriv.h"
fmalita65cdb572015-03-26 07:24:48 -070015
joshualitt10d8fc22016-02-29 11:15:06 -080016#if SK_SUPPORT_GPU
17#include "GrAuditTrail.h"
18#include "GrContext.h"
Robert Phillips22f4a1f2016-12-20 08:57:26 -050019#include "GrRenderTargetContext.h"
joshualitt10d8fc22016-02-29 11:15:06 -080020#endif
21
joshualitte43f7e62016-03-04 10:45:05 -080022#define SKDEBUGCANVAS_VERSION 1
23#define SKDEBUGCANVAS_ATTRIBUTE_VERSION "version"
24#define SKDEBUGCANVAS_ATTRIBUTE_COMMANDS "commands"
25#define SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL "auditTrail"
ethannicholas402cd912016-02-10 12:57:30 -080026
fmalita65cdb572015-03-26 07:24:48 -070027class DebugPaintFilterCanvas : public SkPaintFilterCanvas {
28public:
Ben Wagnerc03e1c52016-10-17 15:20:02 -040029 DebugPaintFilterCanvas(SkCanvas* canvas,
halcanary385fe4d2015-08-26 13:07:48 -070030 bool overdrawViz,
31 bool overrideFilterQuality,
fmalita65cdb572015-03-26 07:24:48 -070032 SkFilterQuality quality)
Ben Wagnerc03e1c52016-10-17 15:20:02 -040033 : INHERITED(canvas)
34 , fOverdrawViz(overdrawViz)
fmalita65cdb572015-03-26 07:24:48 -070035 , fOverrideFilterQuality(overrideFilterQuality)
halcanary385fe4d2015-08-26 13:07:48 -070036 , fFilterQuality(quality) {}
fmalita65cdb572015-03-26 07:24:48 -070037
38protected:
fmalita32cdc322016-01-12 07:21:11 -080039 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
40 if (*paint) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040041 if (fOverdrawViz) {
42 paint->writable()->setColor(SK_ColorRED);
43 paint->writable()->setAlpha(0x08);
44 paint->writable()->setBlendMode(SkBlendMode::kSrcOver);
fmalitabad23dc2016-01-11 13:58:29 -080045 }
fmalita65cdb572015-03-26 07:24:48 -070046
fmalitabad23dc2016-01-11 13:58:29 -080047 if (fOverrideFilterQuality) {
fmalita32cdc322016-01-12 07:21:11 -080048 paint->writable()->setFilterQuality(fFilterQuality);
fmalitabad23dc2016-01-11 13:58:29 -080049 }
fmalita65cdb572015-03-26 07:24:48 -070050 }
fmalitabad23dc2016-01-11 13:58:29 -080051 return true;
fmalita65cdb572015-03-26 07:24:48 -070052 }
53
mtkleinf0599002015-07-13 06:18:39 -070054 void onDrawPicture(const SkPicture* picture,
55 const SkMatrix* matrix,
56 const SkPaint* paint) override {
fmalita65cdb572015-03-26 07:24:48 -070057 // We need to replay the picture onto this canvas in order to filter its internal paints.
58 this->SkCanvas::onDrawPicture(picture, matrix, paint);
59 }
60
vjiaoblack95302da2016-07-21 10:25:54 -070061 void onDrawShadowedPicture(const SkPicture* picture,
62 const SkMatrix* matrix,
vjiaoblacke6f5d562016-08-25 06:30:23 -070063 const SkPaint* paint,
64 const SkShadowParams& params) {
vjiaoblack95302da2016-07-21 10:25:54 -070065#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke6f5d562016-08-25 06:30:23 -070066 this->SkCanvas::onDrawShadowedPicture(picture, matrix, paint, params);
vjiaoblack95302da2016-07-21 10:25:54 -070067#else
68 this->SkCanvas::onDrawPicture(picture, matrix, paint);
69#endif
70 }
71
fmalita65cdb572015-03-26 07:24:48 -070072private:
Ben Wagnerc03e1c52016-10-17 15:20:02 -040073 bool fOverdrawViz;
fmalita65cdb572015-03-26 07:24:48 -070074 bool fOverrideFilterQuality;
75 SkFilterQuality fFilterQuality;
76
77 typedef SkPaintFilterCanvas INHERITED;
78};
79
kkinnunen26e54002015-01-05 12:58:56 -080080SkDebugCanvas::SkDebugCanvas(int width, int height)
81 : INHERITED(width, height)
halcanary96fcdcc2015-08-27 07:41:13 -070082 , fPicture(nullptr)
commit-bot@chromium.org1735d662013-12-04 13:42:46 +000083 , fFilter(false)
commit-bot@chromium.org768ac852014-03-03 16:32:17 +000084 , fMegaVizMode(false)
robertphillips@google.comf4741c12013-02-06 20:13:54 +000085 , fOverdrawViz(false)
fmalita65cdb572015-03-26 07:24:48 -070086 , fOverrideFilterQuality(false)
ethannicholas0a0520a2016-02-12 12:06:53 -080087 , fFilterQuality(kNone_SkFilterQuality)
joshualitt10d8fc22016-02-29 11:15:06 -080088 , fClipVizColor(SK_ColorTRANSPARENT)
Brian Salomon144a5c52016-12-20 16:48:59 -050089 , fDrawGpuOpBounds(false) {
bungeman@google.come8cc6e82013-01-17 16:30:56 +000090 fUserMatrix.reset();
robertphillips@google.com8b157172013-11-07 22:20:31 +000091
92 // SkPicturePlayback uses the base-class' quickReject calls to cull clipped
93 // operations. This can lead to problems in the debugger which expects all
94 // the operations in the captured skp to appear in the debug canvas. To
95 // circumvent this we create a wide open clip here (an empty clip rect
96 // is not sufficient).
97 // Internally, the SkRect passed to clipRect is converted to an SkIRect and
98 // rounded out. The following code creates a nearly maximal rect that will
99 // not get collapsed by the coming conversions (Due to precision loss the
100 // inset has to be surprisingly large).
101 SkIRect largeIRect = SkIRect::MakeLargest();
102 largeIRect.inset(1024, 1024);
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +0000103 SkRect large = SkRect::Make(largeIRect);
robertphillips@google.com8b157172013-11-07 22:20:31 +0000104#ifdef SK_DEBUG
reedb07a94f2014-11-19 05:03:18 -0800105 SkASSERT(!large.roundOut().isEmpty());
robertphillips@google.com8b157172013-11-07 22:20:31 +0000106#endif
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000107 // call the base class' version to avoid adding a draw command
Mike Reedc1f77742016-12-09 09:00:50 -0500108 this->INHERITED::onClipRect(large, kReplace_SkClipOp, kHard_ClipEdgeStyle);
chudy@google.com902ebe52012-06-29 14:21:22 +0000109}
110
chudy@google.com9cda6f72012-08-07 15:08:33 +0000111SkDebugCanvas::~SkDebugCanvas() {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000112 fCommandVector.deleteAll();
chudy@google.com9cda6f72012-08-07 15:08:33 +0000113}
chudy@google.com902ebe52012-06-29 14:21:22 +0000114
115void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000116 fCommandVector.push(command);
chudy@google.com902ebe52012-06-29 14:21:22 +0000117}
118
119void SkDebugCanvas::draw(SkCanvas* canvas) {
commit-bot@chromium.org1735d662013-12-04 13:42:46 +0000120 if (!fCommandVector.isEmpty()) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000121 this->drawTo(canvas, fCommandVector.count() - 1);
chudy@google.com902ebe52012-06-29 14:21:22 +0000122 }
123}
124
chudy@google.com830b8792012-08-01 15:57:52 +0000125void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) {
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000126 canvas->concat(fUserMatrix);
chudy@google.com830b8792012-08-01 15:57:52 +0000127}
128
129int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000130 SkBitmap bitmap;
reed@google.com9ebcac52014-01-24 18:53:42 +0000131 bitmap.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
chudy@google.com902ebe52012-06-29 14:21:22 +0000132
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000133 SkCanvas canvas(bitmap);
robertphillips@google.com94acc702012-09-06 18:43:21 +0000134 canvas.translate(SkIntToScalar(-x), SkIntToScalar(-y));
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700135 this->applyUserTransform(&canvas);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000136
137 int layer = 0;
chudy@google.com751961d2012-07-31 20:07:42 +0000138 SkColor prev = bitmap.getColor(0,0);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000139 for (int i = 0; i < index; i++) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000140 if (fCommandVector[i]->isVisible()) {
robertphillips70171682014-10-16 14:28:28 -0700141 fCommandVector[i]->setUserMatrix(fUserMatrix);
robertphillips@google.com67baba42013-01-02 20:20:31 +0000142 fCommandVector[i]->execute(&canvas);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000143 }
144 if (prev != bitmap.getColor(0,0)) {
145 layer = i;
146 }
147 prev = bitmap.getColor(0,0);
148 }
149 return layer;
150}
151
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000152class SkDebugClipVisitor : public SkCanvas::ClipVisitor {
153public:
154 SkDebugClipVisitor(SkCanvas* canvas) : fCanvas(canvas) {}
155
Mike Reedc1f77742016-12-09 09:00:50 -0500156 void clipRect(const SkRect& r, SkClipOp, bool doAA) override {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000157 SkPaint p;
158 p.setColor(SK_ColorRED);
159 p.setStyle(SkPaint::kStroke_Style);
160 p.setAntiAlias(doAA);
161 fCanvas->drawRect(r, p);
162 }
Mike Reedc1f77742016-12-09 09:00:50 -0500163 void clipRRect(const SkRRect& rr, SkClipOp, bool doAA) override {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000164 SkPaint p;
165 p.setColor(SK_ColorGREEN);
166 p.setStyle(SkPaint::kStroke_Style);
167 p.setAntiAlias(doAA);
168 fCanvas->drawRRect(rr, p);
169 }
Mike Reedc1f77742016-12-09 09:00:50 -0500170 void clipPath(const SkPath& path, SkClipOp, bool doAA) override {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000171 SkPaint p;
172 p.setColor(SK_ColorBLUE);
173 p.setStyle(SkPaint::kStroke_Style);
174 p.setAntiAlias(doAA);
175 fCanvas->drawPath(path, p);
176 }
177
178protected:
179 SkCanvas* fCanvas;
180
181private:
182 typedef SkCanvas::ClipVisitor INHERITED;
183};
184
185// set up the saveLayer commands so that the active ones
186// return true in their 'active' method
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000187void SkDebugCanvas::markActiveCommands(int index) {
188 fActiveLayers.rewind();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000189
190 for (int i = 0; i < fCommandVector.count(); ++i) {
191 fCommandVector[i]->setActive(false);
192 }
193
194 for (int i = 0; i < index; ++i) {
195 SkDrawCommand::Action result = fCommandVector[i]->action();
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000196 if (SkDrawCommand::kPushLayer_Action == result) {
197 fActiveLayers.push(fCommandVector[i]);
198 } else if (SkDrawCommand::kPopLayer_Action == result) {
199 fActiveLayers.pop();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000200 }
201 }
202
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000203 for (int i = 0; i < fActiveLayers.count(); ++i) {
204 fActiveLayers[i]->setActive(true);
205 }
206
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000207}
208
Ravi Mistry99c97962016-12-21 22:41:03 +0000209void SkDebugCanvas::drawTo(SkCanvas* originalCanvas, int index, int m) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000210 SkASSERT(!fCommandVector.isEmpty());
211 SkASSERT(index < fCommandVector.count());
kkinnunen26a00de2015-01-13 23:09:19 -0800212
Ravi Mistry99c97962016-12-21 22:41:03 +0000213 int saveCount = originalCanvas->save();
kkinnunen26a00de2015-01-13 23:09:19 -0800214
Ravi Mistry99c97962016-12-21 22:41:03 +0000215 SkRect windowRect = SkRect::MakeWH(SkIntToScalar(originalCanvas->getBaseLayerSize().width()),
216 SkIntToScalar(originalCanvas->getBaseLayerSize().height()));
chudy@google.com830b8792012-08-01 15:57:52 +0000217
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000218 bool pathOpsMode = getAllowSimplifyClip();
Ravi Mistry99c97962016-12-21 22:41:03 +0000219 originalCanvas->setAllowSimplifyClip(pathOpsMode);
220 originalCanvas->clear(SK_ColorWHITE);
221 originalCanvas->resetMatrix();
kkinnunen26a00de2015-01-13 23:09:19 -0800222 if (!windowRect.isEmpty()) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000223 originalCanvas->clipRect(windowRect, kReplace_SkClipOp);
commit-bot@chromium.orga27622c2013-08-05 16:31:27 +0000224 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000225 this->applyUserTransform(originalCanvas);
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000226
Ravi Mistry99c97962016-12-21 22:41:03 +0000227 DebugPaintFilterCanvas filterCanvas(originalCanvas, fOverdrawViz, fOverrideFilterQuality,
228 fFilterQuality);
chudy@google.com830b8792012-08-01 15:57:52 +0000229
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000230 if (fMegaVizMode) {
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000231 this->markActiveCommands(index);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000232 }
halcanary9d524f22016-03-29 09:03:52 -0700233
joshualitt40836102016-03-11 11:45:53 -0800234#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500235 // If we have a GPU backend we can also visualize the op information
joshualitt10d8fc22016-02-29 11:15:06 -0800236 GrAuditTrail* at = nullptr;
Brian Salomon144a5c52016-12-20 16:48:59 -0500237 if (fDrawGpuOpBounds || m != -1) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000238 // The audit trail must be obtained from the original canvas.
239 at = this->getAuditTrail(originalCanvas);
joshualitt10d8fc22016-02-29 11:15:06 -0800240 }
joshualitt40836102016-03-11 11:45:53 -0800241#endif
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000242
kkinnunen26a00de2015-01-13 23:09:19 -0800243 for (int i = 0; i <= index; i++) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000244 if (i == index && fFilter) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000245 filterCanvas.clear(0xAAFFFFFF);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000246 }
halcanary9d524f22016-03-29 09:03:52 -0700247
joshualitt10d8fc22016-02-29 11:15:06 -0800248#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500249 // We need to flush any pending operations, or they might combine with commands below.
brianosman1c9f9222016-04-15 11:00:51 -0700250 // Previous operations were not registered with the audit trail when they were
251 // created, so if we allow them to combine, the audit trail will fail to find them.
Ravi Mistry99c97962016-12-21 22:41:03 +0000252 filterCanvas.flush();
brianosman1c9f9222016-04-15 11:00:51 -0700253
Brian Salomon42ad83a2016-12-20 16:14:45 -0500254 GrAuditTrail::AutoCollectOps* acb = nullptr;
joshualitt10d8fc22016-02-29 11:15:06 -0800255 if (at) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500256 acb = new GrAuditTrail::AutoCollectOps(at, i);
joshualitt10d8fc22016-02-29 11:15:06 -0800257 }
258#endif
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000259
robertphillips@google.com67baba42013-01-02 20:20:31 +0000260 if (fCommandVector[i]->isVisible()) {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000261 if (fMegaVizMode && fCommandVector[i]->active()) {
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000262 // "active" commands execute their visualization behaviors:
263 // All active saveLayers get replaced with saves so all draws go to the
264 // visible canvas.
265 // All active culls draw their cull box
Ravi Mistry99c97962016-12-21 22:41:03 +0000266 fCommandVector[i]->vizExecute(&filterCanvas);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000267 } else {
robertphillips70171682014-10-16 14:28:28 -0700268 fCommandVector[i]->setUserMatrix(fUserMatrix);
Ravi Mistry99c97962016-12-21 22:41:03 +0000269 fCommandVector[i]->execute(&filterCanvas);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000270 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000271 }
joshualitt10d8fc22016-02-29 11:15:06 -0800272#if SK_SUPPORT_GPU
273 if (at && acb) {
274 delete acb;
275 }
276#endif
chudy@google.com902ebe52012-06-29 14:21:22 +0000277 }
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000278
ethannicholas0a0520a2016-02-12 12:06:53 -0800279 if (SkColorGetA(fClipVizColor) != 0) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000280 filterCanvas.save();
ethannicholas0a0520a2016-02-12 12:06:53 -0800281 #define LARGE_COORD 1000000000
Ravi Mistry99c97962016-12-21 22:41:03 +0000282 filterCanvas.clipRect(
283 SkRect::MakeLTRB(-LARGE_COORD, -LARGE_COORD, LARGE_COORD, LARGE_COORD),
284 kReverseDifference_SkClipOp);
ethannicholas0a0520a2016-02-12 12:06:53 -0800285 SkPaint clipPaint;
286 clipPaint.setColor(fClipVizColor);
Ravi Mistry99c97962016-12-21 22:41:03 +0000287 filterCanvas.drawPaint(clipPaint);
288 filterCanvas.restore();
ethannicholas0a0520a2016-02-12 12:06:53 -0800289 }
290
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000291 if (fMegaVizMode) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000292 filterCanvas.save();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000293 // nuke the CTM
Ravi Mistry99c97962016-12-21 22:41:03 +0000294 filterCanvas.resetMatrix();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000295 // turn off clipping
kkinnunen26e54002015-01-05 12:58:56 -0800296 if (!windowRect.isEmpty()) {
297 SkRect r = windowRect;
298 r.outset(SK_Scalar1, SK_Scalar1);
Ravi Mistry99c97962016-12-21 22:41:03 +0000299 filterCanvas.clipRect(r, kReplace_SkClipOp);
kkinnunen26e54002015-01-05 12:58:56 -0800300 }
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000301 // visualize existing clips
Ravi Mistry99c97962016-12-21 22:41:03 +0000302 SkDebugClipVisitor visitor(&filterCanvas);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000303
Ravi Mistry99c97962016-12-21 22:41:03 +0000304 filterCanvas.replayClips(&visitor);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000305
Ravi Mistry99c97962016-12-21 22:41:03 +0000306 filterCanvas.restore();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000307 }
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000308 if (pathOpsMode) {
309 this->resetClipStackData();
Ravi Mistry99c97962016-12-21 22:41:03 +0000310 const SkClipStack* clipStack = filterCanvas.getClipStack();
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000311 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
312 const SkClipStack::Element* element;
313 SkPath devPath;
314 while ((element = iter.next())) {
315 SkClipStack::Element::Type type = element->getType();
316 SkPath operand;
317 if (type != SkClipStack::Element::kEmpty_Type) {
318 element->asPath(&operand);
319 }
Mike Reedc1f77742016-12-09 09:00:50 -0500320 SkClipOp elementOp = element->getOp();
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000321 this->addClipStackData(devPath, operand, elementOp);
Mike Reedc1f77742016-12-09 09:00:50 -0500322 if (elementOp == kReplace_SkClipOp) {
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000323 devPath = operand;
324 } else {
325 Op(devPath, operand, (SkPathOp) elementOp, &devPath);
326 }
327 }
328 this->lastClipStackData(devPath);
329 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000330 fMatrix = filterCanvas.getTotalMatrix();
331 if (!filterCanvas.getClipDeviceBounds(&fClip)) {
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000332 fClip.setEmpty();
333 }
kkinnunen26a00de2015-01-13 23:09:19 -0800334
Ravi Mistry99c97962016-12-21 22:41:03 +0000335 filterCanvas.restoreToCount(saveCount);
fmalita65cdb572015-03-26 07:24:48 -0700336
joshualitt10d8fc22016-02-29 11:15:06 -0800337#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500338 // draw any ops if required and issue a full reset onto GrAuditTrail
joshualitt10d8fc22016-02-29 11:15:06 -0800339 if (at) {
joshualitte43f7e62016-03-04 10:45:05 -0800340 // just in case there is global reordering, we flush the canvas before querying
341 // GrAuditTrail
joshualittb0666ad2016-03-08 10:43:41 -0800342 GrAuditTrail::AutoEnable ae(at);
Ravi Mistry99c97962016-12-21 22:41:03 +0000343 filterCanvas.flush();
joshualitte43f7e62016-03-04 10:45:05 -0800344
joshualittbdc6b2b2016-03-01 14:22:02 -0800345 // we pick three colorblind-safe colors, 75% alpha
346 static const SkColor kTotalBounds = SkColorSetARGB(0xC0, 0x6A, 0x3D, 0x9A);
Brian Salomon144a5c52016-12-20 16:48:59 -0500347 static const SkColor kCommandOpBounds = SkColorSetARGB(0xC0, 0xE3, 0x1A, 0x1C);
348 static const SkColor kOtherOpBounds = SkColorSetARGB(0xC0, 0xFF, 0x7F, 0x00);
joshualittbdc6b2b2016-03-01 14:22:02 -0800349
Ravi Mistry99c97962016-12-21 22:41:03 +0000350 // get the render target of the top device (from the original canvas) so we can ignore ops
351 // drawn offscreen
352 GrRenderTargetContext* rtc =
353 originalCanvas->internal_private_accessTopLayerRenderTargetContext();
Robert Phillips22f4a1f2016-12-20 08:57:26 -0500354 GrGpuResource::UniqueID rtID = rtc->accessRenderTarget()->uniqueID();
joshualitt1d7decf2016-03-01 07:15:52 -0800355
356 // get the bounding boxes to draw
Brian Salomon42ad83a2016-12-20 16:14:45 -0500357 SkTArray<GrAuditTrail::OpInfo> childrenBounds;
joshualitt46b301d2016-03-02 08:32:37 -0800358 if (m == -1) {
359 at->getBoundsByClientID(&childrenBounds, index);
360 } else {
Brian Salomon144a5c52016-12-20 16:48:59 -0500361 // the client wants us to draw the mth op
Brian Salomon42ad83a2016-12-20 16:14:45 -0500362 at->getBoundsByOpListID(&childrenBounds.push_back(), m);
joshualitt46b301d2016-03-02 08:32:37 -0800363 }
joshualitt10d8fc22016-02-29 11:15:06 -0800364 SkPaint paint;
365 paint.setStyle(SkPaint::kStroke_Style);
366 paint.setStrokeWidth(1);
367 for (int i = 0; i < childrenBounds.count(); i++) {
joshualitt1d7decf2016-03-01 07:15:52 -0800368 if (childrenBounds[i].fRenderTargetUniqueID != rtID) {
369 // offscreen draw, ignore for now
370 continue;
371 }
joshualittbdc6b2b2016-03-01 14:22:02 -0800372 paint.setColor(kTotalBounds);
Ravi Mistry99c97962016-12-21 22:41:03 +0000373 filterCanvas.drawRect(childrenBounds[i].fBounds, paint);
Brian Salomon42ad83a2016-12-20 16:14:45 -0500374 for (int j = 0; j < childrenBounds[i].fOps.count(); j++) {
Brian Salomon144a5c52016-12-20 16:48:59 -0500375 const GrAuditTrail::OpInfo::Op& op = childrenBounds[i].fOps[j];
376 if (op.fClientID != index) {
377 paint.setColor(kOtherOpBounds);
joshualitt10d8fc22016-02-29 11:15:06 -0800378 } else {
Brian Salomon144a5c52016-12-20 16:48:59 -0500379 paint.setColor(kCommandOpBounds);
joshualitt10d8fc22016-02-29 11:15:06 -0800380 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000381 filterCanvas.drawRect(op.fBounds, paint);
joshualitt10d8fc22016-02-29 11:15:06 -0800382 }
383 }
joshualitt10d8fc22016-02-29 11:15:06 -0800384 }
joshualitt10d8fc22016-02-29 11:15:06 -0800385#endif
Ravi Mistry99c97962016-12-21 22:41:03 +0000386 this->cleanupAuditTrail(originalCanvas);
chudy@google.com902ebe52012-06-29 14:21:22 +0000387}
388
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000389void SkDebugCanvas::deleteDrawCommandAt(int index) {
390 SkASSERT(index < fCommandVector.count());
391 delete fCommandVector[index];
392 fCommandVector.remove(index);
393}
394
chudy@google.com902ebe52012-06-29 14:21:22 +0000395SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000396 SkASSERT(index < fCommandVector.count());
397 return fCommandVector[index];
chudy@google.com902ebe52012-06-29 14:21:22 +0000398}
399
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000400void SkDebugCanvas::setDrawCommandAt(int index, SkDrawCommand* command) {
401 SkASSERT(index < fCommandVector.count());
402 delete fCommandVector[index];
403 fCommandVector[index] = command;
404}
405
fmalita8c89c522014-11-08 16:18:56 -0800406const SkTDArray<SkString*>* SkDebugCanvas::getCommandInfo(int index) const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000407 SkASSERT(index < fCommandVector.count());
408 return fCommandVector[index]->Info();
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000409}
chudy@google.com902ebe52012-06-29 14:21:22 +0000410
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000411bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000412 SkASSERT(index < fCommandVector.count());
413 return fCommandVector[index]->isVisible();
chudy@google.com902ebe52012-06-29 14:21:22 +0000414}
415
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000416const SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000417 return fCommandVector;
chudy@google.com902ebe52012-06-29 14:21:22 +0000418}
419
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000420SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() {
421 return fCommandVector;
422}
423
joshualittae47aee2016-03-10 13:29:36 -0800424GrAuditTrail* SkDebugCanvas::getAuditTrail(SkCanvas* canvas) {
425 GrAuditTrail* at = nullptr;
joshualitte43f7e62016-03-04 10:45:05 -0800426#if SK_SUPPORT_GPU
robertphillips175dd9b2016-04-28 14:32:04 -0700427 GrContext* ctx = canvas->getGrContext();
428 if (ctx) {
429 at = ctx->getAuditTrail();
joshualitte43f7e62016-03-04 10:45:05 -0800430 }
431#endif
joshualittae47aee2016-03-10 13:29:36 -0800432 return at;
433}
434
Brian Salomon144a5c52016-12-20 16:48:59 -0500435void SkDebugCanvas::drawAndCollectOps(int n, SkCanvas* canvas) {
joshualitt40836102016-03-11 11:45:53 -0800436#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800437 GrAuditTrail* at = this->getAuditTrail(canvas);
438 if (at) {
joshualittae47aee2016-03-10 13:29:36 -0800439 // loop over all of the commands and draw them, this is to collect reordering
440 // information
441 for (int i = 0; i < this->getSize() && i <= n; i++) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500442 GrAuditTrail::AutoCollectOps enable(at, i);
joshualittae47aee2016-03-10 13:29:36 -0800443 fCommandVector[i]->execute(canvas);
444 }
445
446 // in case there is some kind of global reordering
447 {
448 GrAuditTrail::AutoEnable ae(at);
449 canvas->flush();
450 }
joshualittae47aee2016-03-10 13:29:36 -0800451 }
joshualitt40836102016-03-11 11:45:53 -0800452#endif
joshualittae47aee2016-03-10 13:29:36 -0800453}
454
455void SkDebugCanvas::cleanupAuditTrail(SkCanvas* canvas) {
456 GrAuditTrail* at = this->getAuditTrail(canvas);
457 if (at) {
458#if SK_SUPPORT_GPU
459 GrAuditTrail::AutoEnable ae(at);
460 at->fullReset();
461#endif
462 }
463}
464
465Json::Value SkDebugCanvas::toJSON(UrlDataManager& urlDataManager, int n, SkCanvas* canvas) {
Brian Salomon144a5c52016-12-20 16:48:59 -0500466 this->drawAndCollectOps(n, canvas);
halcanary9d524f22016-03-29 09:03:52 -0700467
joshualitte43f7e62016-03-04 10:45:05 -0800468 // now collect json
joshualitt40836102016-03-11 11:45:53 -0800469#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800470 GrAuditTrail* at = this->getAuditTrail(canvas);
joshualitt40836102016-03-11 11:45:53 -0800471#endif
ethannicholas402cd912016-02-10 12:57:30 -0800472 Json::Value result = Json::Value(Json::objectValue);
473 result[SKDEBUGCANVAS_ATTRIBUTE_VERSION] = Json::Value(SKDEBUGCANVAS_VERSION);
474 Json::Value commands = Json::Value(Json::arrayValue);
ethannicholas0a0520a2016-02-12 12:06:53 -0800475 for (int i = 0; i < this->getSize() && i <= n; i++) {
joshualitte43f7e62016-03-04 10:45:05 -0800476 commands[i] = this->getDrawCommandAt(i)->toJSON(urlDataManager);
477#if SK_SUPPORT_GPU
478 if (at) {
479 // TODO if this is inefficient we could add a method to GrAuditTrail which takes
480 // a Json::Value and is only compiled in this file
481 Json::Value parsedFromString;
482 Json::Reader reader;
483 SkAssertResult(reader.parse(at->toJson(i).c_str(), parsedFromString));
484
485 commands[i][SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL] = parsedFromString;
486 }
487#endif
ethannicholas402cd912016-02-10 12:57:30 -0800488 }
joshualittae47aee2016-03-10 13:29:36 -0800489 this->cleanupAuditTrail(canvas);
ethannicholas402cd912016-02-10 12:57:30 -0800490 result[SKDEBUGCANVAS_ATTRIBUTE_COMMANDS] = commands;
491 return result;
492}
493
Brian Salomon144a5c52016-12-20 16:48:59 -0500494Json::Value SkDebugCanvas::toJSONOpList(int n, SkCanvas* canvas) {
495 this->drawAndCollectOps(n, canvas);
joshualittae47aee2016-03-10 13:29:36 -0800496
497 Json::Value parsedFromString;
joshualittae47aee2016-03-10 13:29:36 -0800498#if SK_SUPPORT_GPU
joshualitt40836102016-03-11 11:45:53 -0800499 GrAuditTrail* at = this->getAuditTrail(canvas);
joshualittae47aee2016-03-10 13:29:36 -0800500 if (at) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500501 GrAuditTrail::AutoManageOpList enable(at);
joshualittae47aee2016-03-10 13:29:36 -0800502 Json::Reader reader;
503 SkAssertResult(reader.parse(at->toJson().c_str(), parsedFromString));
504 }
505#endif
506 this->cleanupAuditTrail(canvas);
507 return parsedFromString;
508}
509
fmalita65cdb572015-03-26 07:24:48 -0700510void SkDebugCanvas::setOverdrawViz(bool overdrawViz) {
fmalita65cdb572015-03-26 07:24:48 -0700511 fOverdrawViz = overdrawViz;
fmalita65cdb572015-03-26 07:24:48 -0700512}
513
514void SkDebugCanvas::overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality quality) {
fmalita65cdb572015-03-26 07:24:48 -0700515 fOverrideFilterQuality = overrideTexFiltering;
516 fFilterQuality = quality;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000517}
518
Mike Reedc1f77742016-12-09 09:00:50 -0500519void SkDebugCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000520 this->addDrawCommand(new SkClipPathCommand(path, op, kSoft_ClipEdgeStyle == edgeStyle));
chudy@google.com902ebe52012-06-29 14:21:22 +0000521}
522
Mike Reedc1f77742016-12-09 09:00:50 -0500523void SkDebugCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000524 this->addDrawCommand(new SkClipRectCommand(rect, op, kSoft_ClipEdgeStyle == edgeStyle));
chudy@google.com902ebe52012-06-29 14:21:22 +0000525}
526
Mike Reedc1f77742016-12-09 09:00:50 -0500527void SkDebugCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000528 this->addDrawCommand(new SkClipRRectCommand(rrect, op, kSoft_ClipEdgeStyle == edgeStyle));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000529}
530
Mike Reedc1f77742016-12-09 09:00:50 -0500531void SkDebugCanvas::onClipRegion(const SkRegion& region, SkClipOp op) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000532 this->addDrawCommand(new SkClipRegionCommand(region, op));
chudy@google.com902ebe52012-06-29 14:21:22 +0000533}
534
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000535void SkDebugCanvas::didConcat(const SkMatrix& matrix) {
robertphillips9bafc302015-02-13 11:13:00 -0800536 this->addDrawCommand(new SkConcatCommand(matrix));
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000537 this->INHERITED::didConcat(matrix);
chudy@google.com902ebe52012-06-29 14:21:22 +0000538}
539
reed97660cc2016-06-28 18:54:19 -0700540void SkDebugCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
541 this->addDrawCommand(new SkDrawAnnotationCommand(rect, key, sk_ref_sp(value)));
542}
543
reed41af9662015-01-05 07:49:08 -0800544void SkDebugCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar left,
545 SkScalar top, const SkPaint* paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000546 this->addDrawCommand(new SkDrawBitmapCommand(bitmap, left, top, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000547}
548
reed41af9662015-01-05 07:49:08 -0800549void SkDebugCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700550 const SkPaint* paint, SrcRectConstraint constraint) {
reeda5517e22015-07-14 10:54:12 -0700551 this->addDrawCommand(new SkDrawBitmapRectCommand(bitmap, src, dst, paint,
552 (SrcRectConstraint)constraint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000553}
554
reed41af9662015-01-05 07:49:08 -0800555void SkDebugCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
556 const SkRect& dst, const SkPaint* paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000557 this->addDrawCommand(new SkDrawBitmapNineCommand(bitmap, center, dst, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000558}
559
reed41af9662015-01-05 07:49:08 -0800560void SkDebugCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
561 const SkPaint* paint) {
fmalita651c9202015-07-22 10:23:01 -0700562 this->addDrawCommand(new SkDrawImageCommand(image, left, top, paint));
reed41af9662015-01-05 07:49:08 -0800563}
564
565void SkDebugCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700566 const SkPaint* paint, SrcRectConstraint constraint) {
fmalita651c9202015-07-22 10:23:01 -0700567 this->addDrawCommand(new SkDrawImageRectCommand(image, src, dst, paint, constraint));
reed41af9662015-01-05 07:49:08 -0800568}
569
reed41af9662015-01-05 07:49:08 -0800570void SkDebugCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000571 this->addDrawCommand(new SkDrawOvalCommand(oval, paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000572}
573
bsalomonac3aa242016-08-19 11:25:19 -0700574void SkDebugCanvas::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
575 bool useCenter, const SkPaint& paint) {
576 this->addDrawCommand(new SkDrawArcCommand(oval, startAngle, sweepAngle, useCenter, paint));
577}
578
reed41af9662015-01-05 07:49:08 -0800579void SkDebugCanvas::onDrawPaint(const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000580 this->addDrawCommand(new SkDrawPaintCommand(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000581}
582
reed41af9662015-01-05 07:49:08 -0800583void SkDebugCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000584 this->addDrawCommand(new SkDrawPathCommand(path, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000585}
586
mtkleinf0f14112014-12-12 08:46:25 -0800587void SkDebugCanvas::onDrawPicture(const SkPicture* picture,
588 const SkMatrix* matrix,
robertphillipsb3f319f2014-08-13 10:46:23 -0700589 const SkPaint* paint) {
fmalita160ebb22015-04-01 20:58:37 -0700590 this->addDrawCommand(new SkBeginDrawPictureCommand(picture, matrix, paint));
ethannicholas891ad662016-02-12 07:15:45 -0800591 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
592 picture->playback(this);
fmalita160ebb22015-04-01 20:58:37 -0700593 this->addDrawCommand(new SkEndDrawPictureCommand(SkToBool(matrix) || SkToBool(paint)));
chudy@google.com902ebe52012-06-29 14:21:22 +0000594}
595
vjiaoblack95302da2016-07-21 10:25:54 -0700596void SkDebugCanvas::onDrawShadowedPicture(const SkPicture* picture,
597 const SkMatrix* matrix,
vjiaoblacke6f5d562016-08-25 06:30:23 -0700598 const SkPaint* paint,
599 const SkShadowParams& params) {
600 this->addDrawCommand(new SkBeginDrawShadowedPictureCommand(picture, matrix, paint, params));
vjiaoblack95302da2016-07-21 10:25:54 -0700601 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
602 picture->playback(this);
603 this->addDrawCommand(new SkEndDrawShadowedPictureCommand(SkToBool(matrix) || SkToBool(paint)));
604}
605
reed41af9662015-01-05 07:49:08 -0800606void SkDebugCanvas::onDrawPoints(PointMode mode, size_t count,
607 const SkPoint pts[], const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000608 this->addDrawCommand(new SkDrawPointsCommand(mode, count, pts, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000609}
610
reed@google.come0d9ce82014-04-23 04:00:17 +0000611void SkDebugCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
612 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000613 this->addDrawCommand(new SkDrawPosTextCommand(text, byteLength, pos, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000614}
615
reed@google.come0d9ce82014-04-23 04:00:17 +0000616void SkDebugCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
617 SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000618 this->addDrawCommand(
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000619 new SkDrawPosTextHCommand(text, byteLength, xpos, constY, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000620}
621
reed41af9662015-01-05 07:49:08 -0800622void SkDebugCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
chudy@google.com902ebe52012-06-29 14:21:22 +0000623 // NOTE(chudy): Messing up when renamed to DrawRect... Why?
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000624 addDrawCommand(new SkDrawRectCommand(rect, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000625}
626
reed41af9662015-01-05 07:49:08 -0800627void SkDebugCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000628 this->addDrawCommand(new SkDrawRRectCommand(rrect, paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000629}
630
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000631void SkDebugCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
632 const SkPaint& paint) {
commit-bot@chromium.org3d305202014-02-24 17:28:55 +0000633 this->addDrawCommand(new SkDrawDRRectCommand(outer, inner, paint));
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000634}
635
reed@google.come0d9ce82014-04-23 04:00:17 +0000636void SkDebugCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
637 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000638 this->addDrawCommand(new SkDrawTextCommand(text, byteLength, x, y, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000639}
640
reed@google.come0d9ce82014-04-23 04:00:17 +0000641void SkDebugCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
642 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000643 this->addDrawCommand(
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000644 new SkDrawTextOnPathCommand(text, byteLength, path, matrix, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000645}
646
reed45561a02016-07-07 12:47:17 -0700647void SkDebugCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
648 const SkRect* cull, const SkPaint& paint) {
649 this->addDrawCommand(new SkDrawTextRSXformCommand(text, byteLength, xform, cull, paint));
650}
651
fmalitab7425172014-08-26 07:56:44 -0700652void SkDebugCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
653 const SkPaint& paint) {
fmalita37283c22016-09-13 10:00:23 -0700654 this->addDrawCommand(new SkDrawTextBlobCommand(sk_ref_sp(const_cast<SkTextBlob*>(blob)),
655 x, y, paint));
fmalitab7425172014-08-26 07:56:44 -0700656}
657
robertphillips9bafc302015-02-13 11:13:00 -0800658void SkDebugCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400659 const SkPoint texCoords[4], SkBlendMode bmode,
robertphillips9bafc302015-02-13 11:13:00 -0800660 const SkPaint& paint) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400661 this->addDrawCommand(new SkDrawPatchCommand(cubics, colors, texCoords, bmode, paint));
robertphillips9bafc302015-02-13 11:13:00 -0800662}
663
reed41af9662015-01-05 07:49:08 -0800664void SkDebugCanvas::onDrawVertices(VertexMode vmode, int vertexCount, const SkPoint vertices[],
665 const SkPoint texs[], const SkColor colors[],
Mike Reedfaba3712016-11-03 14:45:31 -0400666 SkBlendMode bmode, const uint16_t indices[], int indexCount,
reed41af9662015-01-05 07:49:08 -0800667 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000668 this->addDrawCommand(new SkDrawVerticesCommand(vmode, vertexCount, vertices,
Mike Reed7d954ad2016-10-28 15:42:34 -0400669 texs, colors, bmode, indices, indexCount, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000670}
671
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000672void SkDebugCanvas::willRestore() {
673 this->addDrawCommand(new SkRestoreCommand());
674 this->INHERITED::willRestore();
chudy@google.com902ebe52012-06-29 14:21:22 +0000675}
676
Florin Malita5f6102d2014-06-30 10:13:28 -0400677void SkDebugCanvas::willSave() {
678 this->addDrawCommand(new SkSaveCommand());
679 this->INHERITED::willSave();
chudy@google.com902ebe52012-06-29 14:21:22 +0000680}
681
reed4960eee2015-12-18 07:09:18 -0800682SkCanvas::SaveLayerStrategy SkDebugCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
683 this->addDrawCommand(new SkSaveLayerCommand(rec));
684 (void)this->INHERITED::getSaveLayerStrategy(rec);
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000685 // No need for a full layer.
686 return kNoLayer_SaveLayerStrategy;
chudy@google.com902ebe52012-06-29 14:21:22 +0000687}
688
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000689void SkDebugCanvas::didSetMatrix(const SkMatrix& matrix) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000690 this->addDrawCommand(new SkSetMatrixCommand(matrix));
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000691 this->INHERITED::didSetMatrix(matrix);
chudy@google.com902ebe52012-06-29 14:21:22 +0000692}
693
vjiaoblacke5de1302016-07-13 14:05:28 -0700694void SkDebugCanvas::didTranslateZ(SkScalar z) {
vjiaoblack95302da2016-07-21 10:25:54 -0700695#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -0700696 this->addDrawCommand(new SkTranslateZCommand(z));
697 this->INHERITED::didTranslateZ(z);
vjiaoblack95302da2016-07-21 10:25:54 -0700698#endif
vjiaoblacke5de1302016-07-13 14:05:28 -0700699}
700
chudy@google.com902ebe52012-06-29 14:21:22 +0000701void SkDebugCanvas::toggleCommand(int index, bool toggle) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000702 SkASSERT(index < fCommandVector.count());
703 fCommandVector[index]->setVisible(toggle);
chudy@google.com902ebe52012-06-29 14:21:22 +0000704}
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000705
706static const char* gFillTypeStrs[] = {
707 "kWinding_FillType",
708 "kEvenOdd_FillType",
709 "kInverseWinding_FillType",
710 "kInverseEvenOdd_FillType"
711};
712
713static const char* gOpStrs[] = {
scroggo5965b732015-04-07 06:53:21 -0700714 "kDifference_PathOp",
715 "kIntersect_PathOp",
716 "kUnion_PathOp",
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000717 "kXor_PathOp",
scroggo5965b732015-04-07 06:53:21 -0700718 "kReverseDifference_PathOp",
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000719};
720
721static const char kHTML4SpaceIndent[] = "&nbsp;&nbsp;&nbsp;&nbsp;";
722
723void SkDebugCanvas::outputScalar(SkScalar num) {
724 if (num == (int) num) {
725 fClipStackData.appendf("%d", (int) num);
726 } else {
727 SkString str;
728 str.printf("%1.9g", num);
729 int width = (int) str.size();
730 const char* cStr = str.c_str();
731 while (cStr[width - 1] == '0') {
732 --width;
733 }
734 str.resize(width);
735 fClipStackData.appendf("%sf", str.c_str());
736 }
737}
738
739void SkDebugCanvas::outputPointsCommon(const SkPoint* pts, int count) {
740 for (int index = 0; index < count; ++index) {
741 this->outputScalar(pts[index].fX);
742 fClipStackData.appendf(", ");
743 this->outputScalar(pts[index].fY);
744 if (index + 1 < count) {
745 fClipStackData.appendf(", ");
746 }
747 }
748}
749
750void SkDebugCanvas::outputPoints(const SkPoint* pts, int count) {
751 this->outputPointsCommon(pts, count);
752 fClipStackData.appendf(");<br>");
753}
754
755void SkDebugCanvas::outputConicPoints(const SkPoint* pts, SkScalar weight) {
756 this->outputPointsCommon(pts, 2);
757 fClipStackData.appendf(", ");
758 this->outputScalar(weight);
759 fClipStackData.appendf(");<br>");
760}
761
762void SkDebugCanvas::addPathData(const SkPath& path, const char* pathName) {
763 SkPath::RawIter iter(path);
764 SkPath::FillType fillType = path.getFillType();
765 fClipStackData.appendf("%sSkPath %s;<br>", kHTML4SpaceIndent, pathName);
766 fClipStackData.appendf("%s%s.setFillType(SkPath::%s);<br>", kHTML4SpaceIndent, pathName,
767 gFillTypeStrs[fillType]);
768 iter.setPath(path);
769 uint8_t verb;
770 SkPoint pts[4];
771 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
772 switch (verb) {
773 case SkPath::kMove_Verb:
774 fClipStackData.appendf("%s%s.moveTo(", kHTML4SpaceIndent, pathName);
775 this->outputPoints(&pts[0], 1);
776 continue;
777 case SkPath::kLine_Verb:
778 fClipStackData.appendf("%s%s.lineTo(", kHTML4SpaceIndent, pathName);
779 this->outputPoints(&pts[1], 1);
780 break;
781 case SkPath::kQuad_Verb:
782 fClipStackData.appendf("%s%s.quadTo(", kHTML4SpaceIndent, pathName);
783 this->outputPoints(&pts[1], 2);
784 break;
785 case SkPath::kConic_Verb:
786 fClipStackData.appendf("%s%s.conicTo(", kHTML4SpaceIndent, pathName);
787 this->outputConicPoints(&pts[1], iter.conicWeight());
788 break;
789 case SkPath::kCubic_Verb:
790 fClipStackData.appendf("%s%s.cubicTo(", kHTML4SpaceIndent, pathName);
791 this->outputPoints(&pts[1], 3);
792 break;
793 case SkPath::kClose_Verb:
794 fClipStackData.appendf("%s%s.close();<br>", kHTML4SpaceIndent, pathName);
795 break;
796 default:
797 SkDEBUGFAIL("bad verb");
798 return;
799 }
800 }
801}
802
803void SkDebugCanvas::addClipStackData(const SkPath& devPath, const SkPath& operand,
Mike Reedc1f77742016-12-09 09:00:50 -0500804 SkClipOp elementOp) {
805 if (elementOp == kReplace_SkClipOp) {
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000806 if (!lastClipStackData(devPath)) {
807 fSaveDevPath = operand;
808 }
809 fCalledAddStackData = false;
810 } else {
811 fClipStackData.appendf("<br>static void test(skiatest::Reporter* reporter,"
812 " const char* filename) {<br>");
813 addPathData(fCalledAddStackData ? devPath : fSaveDevPath, "path");
814 addPathData(operand, "pathB");
815 fClipStackData.appendf("%stestPathOp(reporter, path, pathB, %s, filename);<br>",
Mike Reedebfce6d2016-12-12 10:02:12 -0500816 kHTML4SpaceIndent, gOpStrs[static_cast<int>(elementOp)]);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000817 fClipStackData.appendf("}<br>");
818 fCalledAddStackData = true;
819 }
820}
821
822bool SkDebugCanvas::lastClipStackData(const SkPath& devPath) {
823 if (fCalledAddStackData) {
824 fClipStackData.appendf("<br>");
825 addPathData(devPath, "pathOut");
826 return true;
827 }
828 return false;
829}