blob: ace39a471ba4a17ef60e081170c917596381f8a1 [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 +0000152// set up the saveLayer commands so that the active ones
153// return true in their 'active' method
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000154void SkDebugCanvas::markActiveCommands(int index) {
155 fActiveLayers.rewind();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000156
157 for (int i = 0; i < fCommandVector.count(); ++i) {
158 fCommandVector[i]->setActive(false);
159 }
160
161 for (int i = 0; i < index; ++i) {
162 SkDrawCommand::Action result = fCommandVector[i]->action();
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000163 if (SkDrawCommand::kPushLayer_Action == result) {
164 fActiveLayers.push(fCommandVector[i]);
165 } else if (SkDrawCommand::kPopLayer_Action == result) {
166 fActiveLayers.pop();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000167 }
168 }
169
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000170 for (int i = 0; i < fActiveLayers.count(); ++i) {
171 fActiveLayers[i]->setActive(true);
172 }
173
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000174}
175
Ravi Mistry99c97962016-12-21 22:41:03 +0000176void SkDebugCanvas::drawTo(SkCanvas* originalCanvas, int index, int m) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000177 SkASSERT(!fCommandVector.isEmpty());
178 SkASSERT(index < fCommandVector.count());
kkinnunen26a00de2015-01-13 23:09:19 -0800179
Ravi Mistry99c97962016-12-21 22:41:03 +0000180 int saveCount = originalCanvas->save();
kkinnunen26a00de2015-01-13 23:09:19 -0800181
Ravi Mistry99c97962016-12-21 22:41:03 +0000182 SkRect windowRect = SkRect::MakeWH(SkIntToScalar(originalCanvas->getBaseLayerSize().width()),
183 SkIntToScalar(originalCanvas->getBaseLayerSize().height()));
chudy@google.com830b8792012-08-01 15:57:52 +0000184
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000185 bool pathOpsMode = getAllowSimplifyClip();
Ravi Mistry99c97962016-12-21 22:41:03 +0000186 originalCanvas->setAllowSimplifyClip(pathOpsMode);
187 originalCanvas->clear(SK_ColorWHITE);
188 originalCanvas->resetMatrix();
kkinnunen26a00de2015-01-13 23:09:19 -0800189 if (!windowRect.isEmpty()) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000190 originalCanvas->clipRect(windowRect, kReplace_SkClipOp);
commit-bot@chromium.orga27622c2013-08-05 16:31:27 +0000191 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000192 this->applyUserTransform(originalCanvas);
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000193
Ravi Mistry99c97962016-12-21 22:41:03 +0000194 DebugPaintFilterCanvas filterCanvas(originalCanvas, fOverdrawViz, fOverrideFilterQuality,
195 fFilterQuality);
chudy@google.com830b8792012-08-01 15:57:52 +0000196
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000197 if (fMegaVizMode) {
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000198 this->markActiveCommands(index);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000199 }
halcanary9d524f22016-03-29 09:03:52 -0700200
joshualitt40836102016-03-11 11:45:53 -0800201#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500202 // If we have a GPU backend we can also visualize the op information
joshualitt10d8fc22016-02-29 11:15:06 -0800203 GrAuditTrail* at = nullptr;
Brian Salomon144a5c52016-12-20 16:48:59 -0500204 if (fDrawGpuOpBounds || m != -1) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000205 // The audit trail must be obtained from the original canvas.
206 at = this->getAuditTrail(originalCanvas);
joshualitt10d8fc22016-02-29 11:15:06 -0800207 }
joshualitt40836102016-03-11 11:45:53 -0800208#endif
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000209
kkinnunen26a00de2015-01-13 23:09:19 -0800210 for (int i = 0; i <= index; i++) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000211 if (i == index && fFilter) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000212 filterCanvas.clear(0xAAFFFFFF);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000213 }
halcanary9d524f22016-03-29 09:03:52 -0700214
joshualitt10d8fc22016-02-29 11:15:06 -0800215#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500216 // We need to flush any pending operations, or they might combine with commands below.
brianosman1c9f9222016-04-15 11:00:51 -0700217 // Previous operations were not registered with the audit trail when they were
218 // created, so if we allow them to combine, the audit trail will fail to find them.
Ravi Mistry99c97962016-12-21 22:41:03 +0000219 filterCanvas.flush();
brianosman1c9f9222016-04-15 11:00:51 -0700220
Brian Salomon42ad83a2016-12-20 16:14:45 -0500221 GrAuditTrail::AutoCollectOps* acb = nullptr;
joshualitt10d8fc22016-02-29 11:15:06 -0800222 if (at) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500223 acb = new GrAuditTrail::AutoCollectOps(at, i);
joshualitt10d8fc22016-02-29 11:15:06 -0800224 }
225#endif
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000226
robertphillips@google.com67baba42013-01-02 20:20:31 +0000227 if (fCommandVector[i]->isVisible()) {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000228 if (fMegaVizMode && fCommandVector[i]->active()) {
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000229 // "active" commands execute their visualization behaviors:
230 // All active saveLayers get replaced with saves so all draws go to the
231 // visible canvas.
232 // All active culls draw their cull box
Ravi Mistry99c97962016-12-21 22:41:03 +0000233 fCommandVector[i]->vizExecute(&filterCanvas);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000234 } else {
robertphillips70171682014-10-16 14:28:28 -0700235 fCommandVector[i]->setUserMatrix(fUserMatrix);
Ravi Mistry99c97962016-12-21 22:41:03 +0000236 fCommandVector[i]->execute(&filterCanvas);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000237 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000238 }
joshualitt10d8fc22016-02-29 11:15:06 -0800239#if SK_SUPPORT_GPU
240 if (at && acb) {
241 delete acb;
242 }
243#endif
chudy@google.com902ebe52012-06-29 14:21:22 +0000244 }
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000245
ethannicholas0a0520a2016-02-12 12:06:53 -0800246 if (SkColorGetA(fClipVizColor) != 0) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000247 filterCanvas.save();
ethannicholas0a0520a2016-02-12 12:06:53 -0800248 #define LARGE_COORD 1000000000
Ravi Mistry99c97962016-12-21 22:41:03 +0000249 filterCanvas.clipRect(
250 SkRect::MakeLTRB(-LARGE_COORD, -LARGE_COORD, LARGE_COORD, LARGE_COORD),
251 kReverseDifference_SkClipOp);
ethannicholas0a0520a2016-02-12 12:06:53 -0800252 SkPaint clipPaint;
253 clipPaint.setColor(fClipVizColor);
Ravi Mistry99c97962016-12-21 22:41:03 +0000254 filterCanvas.drawPaint(clipPaint);
255 filterCanvas.restore();
ethannicholas0a0520a2016-02-12 12:06:53 -0800256 }
257
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000258 if (fMegaVizMode) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000259 filterCanvas.save();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000260 // nuke the CTM
Ravi Mistry99c97962016-12-21 22:41:03 +0000261 filterCanvas.resetMatrix();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000262 // turn off clipping
kkinnunen26e54002015-01-05 12:58:56 -0800263 if (!windowRect.isEmpty()) {
264 SkRect r = windowRect;
265 r.outset(SK_Scalar1, SK_Scalar1);
Ravi Mistry99c97962016-12-21 22:41:03 +0000266 filterCanvas.clipRect(r, kReplace_SkClipOp);
kkinnunen26e54002015-01-05 12:58:56 -0800267 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000268 filterCanvas.restore();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000269 }
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000270 if (pathOpsMode) {
271 this->resetClipStackData();
Mike Reeda1361362017-03-07 09:37:29 -0500272 const SkClipStack* clipStack = nullptr;//HACK filterCanvas.getClipStack();
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000273 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
274 const SkClipStack::Element* element;
275 SkPath devPath;
276 while ((element = iter.next())) {
277 SkClipStack::Element::Type type = element->getType();
278 SkPath operand;
279 if (type != SkClipStack::Element::kEmpty_Type) {
280 element->asPath(&operand);
281 }
Mike Reedc1f77742016-12-09 09:00:50 -0500282 SkClipOp elementOp = element->getOp();
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000283 this->addClipStackData(devPath, operand, elementOp);
Mike Reedc1f77742016-12-09 09:00:50 -0500284 if (elementOp == kReplace_SkClipOp) {
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000285 devPath = operand;
286 } else {
287 Op(devPath, operand, (SkPathOp) elementOp, &devPath);
288 }
289 }
290 this->lastClipStackData(devPath);
291 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000292 fMatrix = filterCanvas.getTotalMatrix();
Mike Reed918e1442017-01-23 11:39:45 -0500293 fClip = filterCanvas.getDeviceClipBounds();
Ravi Mistry99c97962016-12-21 22:41:03 +0000294 filterCanvas.restoreToCount(saveCount);
fmalita65cdb572015-03-26 07:24:48 -0700295
joshualitt10d8fc22016-02-29 11:15:06 -0800296#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500297 // draw any ops if required and issue a full reset onto GrAuditTrail
joshualitt10d8fc22016-02-29 11:15:06 -0800298 if (at) {
joshualitte43f7e62016-03-04 10:45:05 -0800299 // just in case there is global reordering, we flush the canvas before querying
300 // GrAuditTrail
joshualittb0666ad2016-03-08 10:43:41 -0800301 GrAuditTrail::AutoEnable ae(at);
Ravi Mistry99c97962016-12-21 22:41:03 +0000302 filterCanvas.flush();
joshualitte43f7e62016-03-04 10:45:05 -0800303
joshualittbdc6b2b2016-03-01 14:22:02 -0800304 // we pick three colorblind-safe colors, 75% alpha
305 static const SkColor kTotalBounds = SkColorSetARGB(0xC0, 0x6A, 0x3D, 0x9A);
Brian Salomon144a5c52016-12-20 16:48:59 -0500306 static const SkColor kCommandOpBounds = SkColorSetARGB(0xC0, 0xE3, 0x1A, 0x1C);
307 static const SkColor kOtherOpBounds = SkColorSetARGB(0xC0, 0xFF, 0x7F, 0x00);
joshualittbdc6b2b2016-03-01 14:22:02 -0800308
Ravi Mistry99c97962016-12-21 22:41:03 +0000309 // get the render target of the top device (from the original canvas) so we can ignore ops
310 // drawn offscreen
311 GrRenderTargetContext* rtc =
312 originalCanvas->internal_private_accessTopLayerRenderTargetContext();
Robert Phillips22f4a1f2016-12-20 08:57:26 -0500313 GrGpuResource::UniqueID rtID = rtc->accessRenderTarget()->uniqueID();
joshualitt1d7decf2016-03-01 07:15:52 -0800314
315 // get the bounding boxes to draw
Brian Salomon42ad83a2016-12-20 16:14:45 -0500316 SkTArray<GrAuditTrail::OpInfo> childrenBounds;
joshualitt46b301d2016-03-02 08:32:37 -0800317 if (m == -1) {
318 at->getBoundsByClientID(&childrenBounds, index);
319 } else {
Brian Salomon144a5c52016-12-20 16:48:59 -0500320 // the client wants us to draw the mth op
Brian Salomon42ad83a2016-12-20 16:14:45 -0500321 at->getBoundsByOpListID(&childrenBounds.push_back(), m);
joshualitt46b301d2016-03-02 08:32:37 -0800322 }
joshualitt10d8fc22016-02-29 11:15:06 -0800323 SkPaint paint;
324 paint.setStyle(SkPaint::kStroke_Style);
325 paint.setStrokeWidth(1);
326 for (int i = 0; i < childrenBounds.count(); i++) {
joshualitt1d7decf2016-03-01 07:15:52 -0800327 if (childrenBounds[i].fRenderTargetUniqueID != rtID) {
328 // offscreen draw, ignore for now
329 continue;
330 }
joshualittbdc6b2b2016-03-01 14:22:02 -0800331 paint.setColor(kTotalBounds);
Ravi Mistry99c97962016-12-21 22:41:03 +0000332 filterCanvas.drawRect(childrenBounds[i].fBounds, paint);
Brian Salomon42ad83a2016-12-20 16:14:45 -0500333 for (int j = 0; j < childrenBounds[i].fOps.count(); j++) {
Brian Salomon144a5c52016-12-20 16:48:59 -0500334 const GrAuditTrail::OpInfo::Op& op = childrenBounds[i].fOps[j];
335 if (op.fClientID != index) {
336 paint.setColor(kOtherOpBounds);
joshualitt10d8fc22016-02-29 11:15:06 -0800337 } else {
Brian Salomon144a5c52016-12-20 16:48:59 -0500338 paint.setColor(kCommandOpBounds);
joshualitt10d8fc22016-02-29 11:15:06 -0800339 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000340 filterCanvas.drawRect(op.fBounds, paint);
joshualitt10d8fc22016-02-29 11:15:06 -0800341 }
342 }
joshualitt10d8fc22016-02-29 11:15:06 -0800343 }
joshualitt10d8fc22016-02-29 11:15:06 -0800344#endif
Ravi Mistry99c97962016-12-21 22:41:03 +0000345 this->cleanupAuditTrail(originalCanvas);
chudy@google.com902ebe52012-06-29 14:21:22 +0000346}
347
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000348void SkDebugCanvas::deleteDrawCommandAt(int index) {
349 SkASSERT(index < fCommandVector.count());
350 delete fCommandVector[index];
351 fCommandVector.remove(index);
352}
353
chudy@google.com902ebe52012-06-29 14:21:22 +0000354SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000355 SkASSERT(index < fCommandVector.count());
356 return fCommandVector[index];
chudy@google.com902ebe52012-06-29 14:21:22 +0000357}
358
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000359void SkDebugCanvas::setDrawCommandAt(int index, SkDrawCommand* command) {
360 SkASSERT(index < fCommandVector.count());
361 delete fCommandVector[index];
362 fCommandVector[index] = command;
363}
364
fmalita8c89c522014-11-08 16:18:56 -0800365const SkTDArray<SkString*>* SkDebugCanvas::getCommandInfo(int index) const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000366 SkASSERT(index < fCommandVector.count());
367 return fCommandVector[index]->Info();
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000368}
chudy@google.com902ebe52012-06-29 14:21:22 +0000369
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000370bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000371 SkASSERT(index < fCommandVector.count());
372 return fCommandVector[index]->isVisible();
chudy@google.com902ebe52012-06-29 14:21:22 +0000373}
374
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000375const SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000376 return fCommandVector;
chudy@google.com902ebe52012-06-29 14:21:22 +0000377}
378
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000379SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() {
380 return fCommandVector;
381}
382
joshualittae47aee2016-03-10 13:29:36 -0800383GrAuditTrail* SkDebugCanvas::getAuditTrail(SkCanvas* canvas) {
384 GrAuditTrail* at = nullptr;
joshualitte43f7e62016-03-04 10:45:05 -0800385#if SK_SUPPORT_GPU
robertphillips175dd9b2016-04-28 14:32:04 -0700386 GrContext* ctx = canvas->getGrContext();
387 if (ctx) {
388 at = ctx->getAuditTrail();
joshualitte43f7e62016-03-04 10:45:05 -0800389 }
390#endif
joshualittae47aee2016-03-10 13:29:36 -0800391 return at;
392}
393
Brian Salomon144a5c52016-12-20 16:48:59 -0500394void SkDebugCanvas::drawAndCollectOps(int n, SkCanvas* canvas) {
joshualitt40836102016-03-11 11:45:53 -0800395#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800396 GrAuditTrail* at = this->getAuditTrail(canvas);
397 if (at) {
joshualittae47aee2016-03-10 13:29:36 -0800398 // loop over all of the commands and draw them, this is to collect reordering
399 // information
400 for (int i = 0; i < this->getSize() && i <= n; i++) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500401 GrAuditTrail::AutoCollectOps enable(at, i);
joshualittae47aee2016-03-10 13:29:36 -0800402 fCommandVector[i]->execute(canvas);
403 }
404
405 // in case there is some kind of global reordering
406 {
407 GrAuditTrail::AutoEnable ae(at);
408 canvas->flush();
409 }
joshualittae47aee2016-03-10 13:29:36 -0800410 }
joshualitt40836102016-03-11 11:45:53 -0800411#endif
joshualittae47aee2016-03-10 13:29:36 -0800412}
413
414void SkDebugCanvas::cleanupAuditTrail(SkCanvas* canvas) {
415 GrAuditTrail* at = this->getAuditTrail(canvas);
416 if (at) {
417#if SK_SUPPORT_GPU
418 GrAuditTrail::AutoEnable ae(at);
419 at->fullReset();
420#endif
421 }
422}
423
424Json::Value SkDebugCanvas::toJSON(UrlDataManager& urlDataManager, int n, SkCanvas* canvas) {
Brian Salomon144a5c52016-12-20 16:48:59 -0500425 this->drawAndCollectOps(n, canvas);
halcanary9d524f22016-03-29 09:03:52 -0700426
joshualitte43f7e62016-03-04 10:45:05 -0800427 // now collect json
joshualitt40836102016-03-11 11:45:53 -0800428#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800429 GrAuditTrail* at = this->getAuditTrail(canvas);
joshualitt40836102016-03-11 11:45:53 -0800430#endif
ethannicholas402cd912016-02-10 12:57:30 -0800431 Json::Value result = Json::Value(Json::objectValue);
432 result[SKDEBUGCANVAS_ATTRIBUTE_VERSION] = Json::Value(SKDEBUGCANVAS_VERSION);
433 Json::Value commands = Json::Value(Json::arrayValue);
ethannicholas0a0520a2016-02-12 12:06:53 -0800434 for (int i = 0; i < this->getSize() && i <= n; i++) {
joshualitte43f7e62016-03-04 10:45:05 -0800435 commands[i] = this->getDrawCommandAt(i)->toJSON(urlDataManager);
436#if SK_SUPPORT_GPU
437 if (at) {
438 // TODO if this is inefficient we could add a method to GrAuditTrail which takes
439 // a Json::Value and is only compiled in this file
440 Json::Value parsedFromString;
441 Json::Reader reader;
442 SkAssertResult(reader.parse(at->toJson(i).c_str(), parsedFromString));
443
444 commands[i][SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL] = parsedFromString;
445 }
446#endif
ethannicholas402cd912016-02-10 12:57:30 -0800447 }
joshualittae47aee2016-03-10 13:29:36 -0800448 this->cleanupAuditTrail(canvas);
ethannicholas402cd912016-02-10 12:57:30 -0800449 result[SKDEBUGCANVAS_ATTRIBUTE_COMMANDS] = commands;
450 return result;
451}
452
Brian Salomon144a5c52016-12-20 16:48:59 -0500453Json::Value SkDebugCanvas::toJSONOpList(int n, SkCanvas* canvas) {
454 this->drawAndCollectOps(n, canvas);
joshualittae47aee2016-03-10 13:29:36 -0800455
456 Json::Value parsedFromString;
joshualittae47aee2016-03-10 13:29:36 -0800457#if SK_SUPPORT_GPU
joshualitt40836102016-03-11 11:45:53 -0800458 GrAuditTrail* at = this->getAuditTrail(canvas);
joshualittae47aee2016-03-10 13:29:36 -0800459 if (at) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500460 GrAuditTrail::AutoManageOpList enable(at);
joshualittae47aee2016-03-10 13:29:36 -0800461 Json::Reader reader;
462 SkAssertResult(reader.parse(at->toJson().c_str(), parsedFromString));
463 }
464#endif
465 this->cleanupAuditTrail(canvas);
466 return parsedFromString;
467}
468
fmalita65cdb572015-03-26 07:24:48 -0700469void SkDebugCanvas::setOverdrawViz(bool overdrawViz) {
fmalita65cdb572015-03-26 07:24:48 -0700470 fOverdrawViz = overdrawViz;
fmalita65cdb572015-03-26 07:24:48 -0700471}
472
473void SkDebugCanvas::overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality quality) {
fmalita65cdb572015-03-26 07:24:48 -0700474 fOverrideFilterQuality = overrideTexFiltering;
475 fFilterQuality = quality;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000476}
477
Mike Reedc1f77742016-12-09 09:00:50 -0500478void SkDebugCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000479 this->addDrawCommand(new SkClipPathCommand(path, op, kSoft_ClipEdgeStyle == edgeStyle));
chudy@google.com902ebe52012-06-29 14:21:22 +0000480}
481
Mike Reedc1f77742016-12-09 09:00:50 -0500482void SkDebugCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000483 this->addDrawCommand(new SkClipRectCommand(rect, op, kSoft_ClipEdgeStyle == edgeStyle));
chudy@google.com902ebe52012-06-29 14:21:22 +0000484}
485
Mike Reedc1f77742016-12-09 09:00:50 -0500486void SkDebugCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000487 this->addDrawCommand(new SkClipRRectCommand(rrect, op, kSoft_ClipEdgeStyle == edgeStyle));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000488}
489
Mike Reedc1f77742016-12-09 09:00:50 -0500490void SkDebugCanvas::onClipRegion(const SkRegion& region, SkClipOp op) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000491 this->addDrawCommand(new SkClipRegionCommand(region, op));
chudy@google.com902ebe52012-06-29 14:21:22 +0000492}
493
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000494void SkDebugCanvas::didConcat(const SkMatrix& matrix) {
robertphillips9bafc302015-02-13 11:13:00 -0800495 this->addDrawCommand(new SkConcatCommand(matrix));
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000496 this->INHERITED::didConcat(matrix);
chudy@google.com902ebe52012-06-29 14:21:22 +0000497}
498
reed97660cc2016-06-28 18:54:19 -0700499void SkDebugCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
500 this->addDrawCommand(new SkDrawAnnotationCommand(rect, key, sk_ref_sp(value)));
501}
502
reed41af9662015-01-05 07:49:08 -0800503void SkDebugCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar left,
504 SkScalar top, const SkPaint* paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000505 this->addDrawCommand(new SkDrawBitmapCommand(bitmap, left, top, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000506}
507
reed41af9662015-01-05 07:49:08 -0800508void SkDebugCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700509 const SkPaint* paint, SrcRectConstraint constraint) {
reeda5517e22015-07-14 10:54:12 -0700510 this->addDrawCommand(new SkDrawBitmapRectCommand(bitmap, src, dst, paint,
511 (SrcRectConstraint)constraint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000512}
513
reed41af9662015-01-05 07:49:08 -0800514void SkDebugCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
515 const SkRect& dst, const SkPaint* paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000516 this->addDrawCommand(new SkDrawBitmapNineCommand(bitmap, center, dst, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000517}
518
reed41af9662015-01-05 07:49:08 -0800519void SkDebugCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
520 const SkPaint* paint) {
fmalita651c9202015-07-22 10:23:01 -0700521 this->addDrawCommand(new SkDrawImageCommand(image, left, top, paint));
reed41af9662015-01-05 07:49:08 -0800522}
523
Stan Ilievac42aeb2017-01-12 16:20:50 -0500524void SkDebugCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
525 const SkRect& dst, const SkPaint* paint) {
526 this->addDrawCommand(new SkDrawImageLatticeCommand(image, lattice, dst, paint));
527}
528
reed41af9662015-01-05 07:49:08 -0800529void SkDebugCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700530 const SkPaint* paint, SrcRectConstraint constraint) {
fmalita651c9202015-07-22 10:23:01 -0700531 this->addDrawCommand(new SkDrawImageRectCommand(image, src, dst, paint, constraint));
reed41af9662015-01-05 07:49:08 -0800532}
533
reed41af9662015-01-05 07:49:08 -0800534void SkDebugCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000535 this->addDrawCommand(new SkDrawOvalCommand(oval, paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000536}
537
bsalomonac3aa242016-08-19 11:25:19 -0700538void SkDebugCanvas::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
539 bool useCenter, const SkPaint& paint) {
540 this->addDrawCommand(new SkDrawArcCommand(oval, startAngle, sweepAngle, useCenter, paint));
541}
542
reed41af9662015-01-05 07:49:08 -0800543void SkDebugCanvas::onDrawPaint(const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000544 this->addDrawCommand(new SkDrawPaintCommand(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000545}
546
reed41af9662015-01-05 07:49:08 -0800547void SkDebugCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000548 this->addDrawCommand(new SkDrawPathCommand(path, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000549}
550
mtkleinf0f14112014-12-12 08:46:25 -0800551void SkDebugCanvas::onDrawPicture(const SkPicture* picture,
552 const SkMatrix* matrix,
robertphillipsb3f319f2014-08-13 10:46:23 -0700553 const SkPaint* paint) {
fmalita160ebb22015-04-01 20:58:37 -0700554 this->addDrawCommand(new SkBeginDrawPictureCommand(picture, matrix, paint));
ethannicholas891ad662016-02-12 07:15:45 -0800555 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
556 picture->playback(this);
fmalita160ebb22015-04-01 20:58:37 -0700557 this->addDrawCommand(new SkEndDrawPictureCommand(SkToBool(matrix) || SkToBool(paint)));
chudy@google.com902ebe52012-06-29 14:21:22 +0000558}
559
vjiaoblack95302da2016-07-21 10:25:54 -0700560void SkDebugCanvas::onDrawShadowedPicture(const SkPicture* picture,
561 const SkMatrix* matrix,
vjiaoblacke6f5d562016-08-25 06:30:23 -0700562 const SkPaint* paint,
563 const SkShadowParams& params) {
564 this->addDrawCommand(new SkBeginDrawShadowedPictureCommand(picture, matrix, paint, params));
vjiaoblack95302da2016-07-21 10:25:54 -0700565 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
566 picture->playback(this);
567 this->addDrawCommand(new SkEndDrawShadowedPictureCommand(SkToBool(matrix) || SkToBool(paint)));
568}
569
reed41af9662015-01-05 07:49:08 -0800570void SkDebugCanvas::onDrawPoints(PointMode mode, size_t count,
571 const SkPoint pts[], const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000572 this->addDrawCommand(new SkDrawPointsCommand(mode, count, pts, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000573}
574
reed@google.come0d9ce82014-04-23 04:00:17 +0000575void SkDebugCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
576 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000577 this->addDrawCommand(new SkDrawPosTextCommand(text, byteLength, pos, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000578}
579
reed@google.come0d9ce82014-04-23 04:00:17 +0000580void SkDebugCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
581 SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000582 this->addDrawCommand(
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000583 new SkDrawPosTextHCommand(text, byteLength, xpos, constY, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000584}
585
reed41af9662015-01-05 07:49:08 -0800586void SkDebugCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
chudy@google.com902ebe52012-06-29 14:21:22 +0000587 // NOTE(chudy): Messing up when renamed to DrawRect... Why?
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000588 addDrawCommand(new SkDrawRectCommand(rect, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000589}
590
reed41af9662015-01-05 07:49:08 -0800591void SkDebugCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000592 this->addDrawCommand(new SkDrawRRectCommand(rrect, paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000593}
594
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000595void SkDebugCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
596 const SkPaint& paint) {
commit-bot@chromium.org3d305202014-02-24 17:28:55 +0000597 this->addDrawCommand(new SkDrawDRRectCommand(outer, inner, paint));
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000598}
599
reed@google.come0d9ce82014-04-23 04:00:17 +0000600void SkDebugCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
601 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000602 this->addDrawCommand(new SkDrawTextCommand(text, byteLength, x, y, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000603}
604
reed@google.come0d9ce82014-04-23 04:00:17 +0000605void SkDebugCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
606 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000607 this->addDrawCommand(
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000608 new SkDrawTextOnPathCommand(text, byteLength, path, matrix, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000609}
610
reed45561a02016-07-07 12:47:17 -0700611void SkDebugCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
612 const SkRect* cull, const SkPaint& paint) {
613 this->addDrawCommand(new SkDrawTextRSXformCommand(text, byteLength, xform, cull, paint));
614}
615
fmalitab7425172014-08-26 07:56:44 -0700616void SkDebugCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
617 const SkPaint& paint) {
fmalita37283c22016-09-13 10:00:23 -0700618 this->addDrawCommand(new SkDrawTextBlobCommand(sk_ref_sp(const_cast<SkTextBlob*>(blob)),
619 x, y, paint));
fmalitab7425172014-08-26 07:56:44 -0700620}
621
robertphillips9bafc302015-02-13 11:13:00 -0800622void SkDebugCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400623 const SkPoint texCoords[4], SkBlendMode bmode,
robertphillips9bafc302015-02-13 11:13:00 -0800624 const SkPaint& paint) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400625 this->addDrawCommand(new SkDrawPatchCommand(cubics, colors, texCoords, bmode, paint));
robertphillips9bafc302015-02-13 11:13:00 -0800626}
627
reed41af9662015-01-05 07:49:08 -0800628void SkDebugCanvas::onDrawVertices(VertexMode vmode, int vertexCount, const SkPoint vertices[],
629 const SkPoint texs[], const SkColor colors[],
Mike Reedfaba3712016-11-03 14:45:31 -0400630 SkBlendMode bmode, const uint16_t indices[], int indexCount,
reed41af9662015-01-05 07:49:08 -0800631 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000632 this->addDrawCommand(new SkDrawVerticesCommand(vmode, vertexCount, vertices,
Mike Reed7d954ad2016-10-28 15:42:34 -0400633 texs, colors, bmode, indices, indexCount, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000634}
635
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000636void SkDebugCanvas::willRestore() {
637 this->addDrawCommand(new SkRestoreCommand());
638 this->INHERITED::willRestore();
chudy@google.com902ebe52012-06-29 14:21:22 +0000639}
640
Florin Malita5f6102d2014-06-30 10:13:28 -0400641void SkDebugCanvas::willSave() {
642 this->addDrawCommand(new SkSaveCommand());
643 this->INHERITED::willSave();
chudy@google.com902ebe52012-06-29 14:21:22 +0000644}
645
reed4960eee2015-12-18 07:09:18 -0800646SkCanvas::SaveLayerStrategy SkDebugCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
647 this->addDrawCommand(new SkSaveLayerCommand(rec));
648 (void)this->INHERITED::getSaveLayerStrategy(rec);
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000649 // No need for a full layer.
650 return kNoLayer_SaveLayerStrategy;
chudy@google.com902ebe52012-06-29 14:21:22 +0000651}
652
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000653void SkDebugCanvas::didSetMatrix(const SkMatrix& matrix) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000654 this->addDrawCommand(new SkSetMatrixCommand(matrix));
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000655 this->INHERITED::didSetMatrix(matrix);
chudy@google.com902ebe52012-06-29 14:21:22 +0000656}
657
vjiaoblacke5de1302016-07-13 14:05:28 -0700658void SkDebugCanvas::didTranslateZ(SkScalar z) {
vjiaoblack95302da2016-07-21 10:25:54 -0700659#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -0700660 this->addDrawCommand(new SkTranslateZCommand(z));
661 this->INHERITED::didTranslateZ(z);
vjiaoblack95302da2016-07-21 10:25:54 -0700662#endif
vjiaoblacke5de1302016-07-13 14:05:28 -0700663}
664
chudy@google.com902ebe52012-06-29 14:21:22 +0000665void SkDebugCanvas::toggleCommand(int index, bool toggle) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000666 SkASSERT(index < fCommandVector.count());
667 fCommandVector[index]->setVisible(toggle);
chudy@google.com902ebe52012-06-29 14:21:22 +0000668}
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000669
670static const char* gFillTypeStrs[] = {
671 "kWinding_FillType",
672 "kEvenOdd_FillType",
673 "kInverseWinding_FillType",
674 "kInverseEvenOdd_FillType"
675};
676
677static const char* gOpStrs[] = {
scroggo5965b732015-04-07 06:53:21 -0700678 "kDifference_PathOp",
679 "kIntersect_PathOp",
680 "kUnion_PathOp",
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000681 "kXor_PathOp",
scroggo5965b732015-04-07 06:53:21 -0700682 "kReverseDifference_PathOp",
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000683};
684
685static const char kHTML4SpaceIndent[] = "&nbsp;&nbsp;&nbsp;&nbsp;";
686
687void SkDebugCanvas::outputScalar(SkScalar num) {
688 if (num == (int) num) {
689 fClipStackData.appendf("%d", (int) num);
690 } else {
691 SkString str;
692 str.printf("%1.9g", num);
693 int width = (int) str.size();
694 const char* cStr = str.c_str();
695 while (cStr[width - 1] == '0') {
696 --width;
697 }
698 str.resize(width);
699 fClipStackData.appendf("%sf", str.c_str());
700 }
701}
702
703void SkDebugCanvas::outputPointsCommon(const SkPoint* pts, int count) {
704 for (int index = 0; index < count; ++index) {
705 this->outputScalar(pts[index].fX);
706 fClipStackData.appendf(", ");
707 this->outputScalar(pts[index].fY);
708 if (index + 1 < count) {
709 fClipStackData.appendf(", ");
710 }
711 }
712}
713
714void SkDebugCanvas::outputPoints(const SkPoint* pts, int count) {
715 this->outputPointsCommon(pts, count);
716 fClipStackData.appendf(");<br>");
717}
718
719void SkDebugCanvas::outputConicPoints(const SkPoint* pts, SkScalar weight) {
720 this->outputPointsCommon(pts, 2);
721 fClipStackData.appendf(", ");
722 this->outputScalar(weight);
723 fClipStackData.appendf(");<br>");
724}
725
726void SkDebugCanvas::addPathData(const SkPath& path, const char* pathName) {
727 SkPath::RawIter iter(path);
728 SkPath::FillType fillType = path.getFillType();
729 fClipStackData.appendf("%sSkPath %s;<br>", kHTML4SpaceIndent, pathName);
730 fClipStackData.appendf("%s%s.setFillType(SkPath::%s);<br>", kHTML4SpaceIndent, pathName,
731 gFillTypeStrs[fillType]);
732 iter.setPath(path);
733 uint8_t verb;
734 SkPoint pts[4];
735 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
736 switch (verb) {
737 case SkPath::kMove_Verb:
738 fClipStackData.appendf("%s%s.moveTo(", kHTML4SpaceIndent, pathName);
739 this->outputPoints(&pts[0], 1);
740 continue;
741 case SkPath::kLine_Verb:
742 fClipStackData.appendf("%s%s.lineTo(", kHTML4SpaceIndent, pathName);
743 this->outputPoints(&pts[1], 1);
744 break;
745 case SkPath::kQuad_Verb:
746 fClipStackData.appendf("%s%s.quadTo(", kHTML4SpaceIndent, pathName);
747 this->outputPoints(&pts[1], 2);
748 break;
749 case SkPath::kConic_Verb:
750 fClipStackData.appendf("%s%s.conicTo(", kHTML4SpaceIndent, pathName);
751 this->outputConicPoints(&pts[1], iter.conicWeight());
752 break;
753 case SkPath::kCubic_Verb:
754 fClipStackData.appendf("%s%s.cubicTo(", kHTML4SpaceIndent, pathName);
755 this->outputPoints(&pts[1], 3);
756 break;
757 case SkPath::kClose_Verb:
758 fClipStackData.appendf("%s%s.close();<br>", kHTML4SpaceIndent, pathName);
759 break;
760 default:
761 SkDEBUGFAIL("bad verb");
762 return;
763 }
764 }
765}
766
767void SkDebugCanvas::addClipStackData(const SkPath& devPath, const SkPath& operand,
Mike Reedc1f77742016-12-09 09:00:50 -0500768 SkClipOp elementOp) {
769 if (elementOp == kReplace_SkClipOp) {
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000770 if (!lastClipStackData(devPath)) {
771 fSaveDevPath = operand;
772 }
773 fCalledAddStackData = false;
774 } else {
775 fClipStackData.appendf("<br>static void test(skiatest::Reporter* reporter,"
776 " const char* filename) {<br>");
777 addPathData(fCalledAddStackData ? devPath : fSaveDevPath, "path");
778 addPathData(operand, "pathB");
779 fClipStackData.appendf("%stestPathOp(reporter, path, pathB, %s, filename);<br>",
Mike Reedebfce6d2016-12-12 10:02:12 -0500780 kHTML4SpaceIndent, gOpStrs[static_cast<int>(elementOp)]);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000781 fClipStackData.appendf("}<br>");
782 fCalledAddStackData = true;
783 }
784}
785
786bool SkDebugCanvas::lastClipStackData(const SkPath& devPath) {
787 if (fCalledAddStackData) {
788 fClipStackData.appendf("<br>");
789 addPathData(devPath, "pathOut");
790 return true;
791 }
792 return false;
793}