blob: 0bfd85f7f9b5984d99ca1864c74464e15c6ea213 [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"
Mike Reed274218e2018-01-08 15:05:02 -050013#include "SkRectPriv.h"
fmalita37283c22016-09-13 10:00:23 -070014#include "SkTextBlob.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050015#include "SkClipOpPriv.h"
fmalita65cdb572015-03-26 07:24:48 -070016
joshualitt10d8fc22016-02-29 11:15:06 -080017#if SK_SUPPORT_GPU
18#include "GrAuditTrail.h"
19#include "GrContext.h"
Robert Phillips22f4a1f2016-12-20 08:57:26 -050020#include "GrRenderTargetContext.h"
joshualitt10d8fc22016-02-29 11:15:06 -080021#endif
22
joshualitte43f7e62016-03-04 10:45:05 -080023#define SKDEBUGCANVAS_VERSION 1
24#define SKDEBUGCANVAS_ATTRIBUTE_VERSION "version"
25#define SKDEBUGCANVAS_ATTRIBUTE_COMMANDS "commands"
26#define SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL "auditTrail"
ethannicholas402cd912016-02-10 12:57:30 -080027
fmalita65cdb572015-03-26 07:24:48 -070028class DebugPaintFilterCanvas : public SkPaintFilterCanvas {
29public:
Ben Wagnerc03e1c52016-10-17 15:20:02 -040030 DebugPaintFilterCanvas(SkCanvas* canvas,
halcanary385fe4d2015-08-26 13:07:48 -070031 bool overdrawViz,
32 bool overrideFilterQuality,
fmalita65cdb572015-03-26 07:24:48 -070033 SkFilterQuality quality)
Ben Wagnerc03e1c52016-10-17 15:20:02 -040034 : INHERITED(canvas)
35 , fOverdrawViz(overdrawViz)
fmalita65cdb572015-03-26 07:24:48 -070036 , fOverrideFilterQuality(overrideFilterQuality)
halcanary385fe4d2015-08-26 13:07:48 -070037 , fFilterQuality(quality) {}
fmalita65cdb572015-03-26 07:24:48 -070038
39protected:
fmalita32cdc322016-01-12 07:21:11 -080040 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
41 if (*paint) {
Ben Wagnerc03e1c52016-10-17 15:20:02 -040042 if (fOverdrawViz) {
43 paint->writable()->setColor(SK_ColorRED);
44 paint->writable()->setAlpha(0x08);
45 paint->writable()->setBlendMode(SkBlendMode::kSrcOver);
fmalitabad23dc2016-01-11 13:58:29 -080046 }
fmalita65cdb572015-03-26 07:24:48 -070047
fmalitabad23dc2016-01-11 13:58:29 -080048 if (fOverrideFilterQuality) {
fmalita32cdc322016-01-12 07:21:11 -080049 paint->writable()->setFilterQuality(fFilterQuality);
fmalitabad23dc2016-01-11 13:58:29 -080050 }
fmalita65cdb572015-03-26 07:24:48 -070051 }
fmalitabad23dc2016-01-11 13:58:29 -080052 return true;
fmalita65cdb572015-03-26 07:24:48 -070053 }
54
mtkleinf0599002015-07-13 06:18:39 -070055 void onDrawPicture(const SkPicture* picture,
56 const SkMatrix* matrix,
57 const SkPaint* paint) override {
fmalita65cdb572015-03-26 07:24:48 -070058 // We need to replay the picture onto this canvas in order to filter its internal paints.
59 this->SkCanvas::onDrawPicture(picture, matrix, paint);
60 }
61
62private:
Ben Wagnerc03e1c52016-10-17 15:20:02 -040063 bool fOverdrawViz;
fmalita65cdb572015-03-26 07:24:48 -070064 bool fOverrideFilterQuality;
65 SkFilterQuality fFilterQuality;
66
67 typedef SkPaintFilterCanvas INHERITED;
68};
69
kkinnunen26e54002015-01-05 12:58:56 -080070SkDebugCanvas::SkDebugCanvas(int width, int height)
71 : INHERITED(width, height)
halcanary96fcdcc2015-08-27 07:41:13 -070072 , fPicture(nullptr)
commit-bot@chromium.org1735d662013-12-04 13:42:46 +000073 , fFilter(false)
commit-bot@chromium.org768ac852014-03-03 16:32:17 +000074 , fMegaVizMode(false)
robertphillips@google.comf4741c12013-02-06 20:13:54 +000075 , fOverdrawViz(false)
fmalita65cdb572015-03-26 07:24:48 -070076 , fOverrideFilterQuality(false)
ethannicholas0a0520a2016-02-12 12:06:53 -080077 , fFilterQuality(kNone_SkFilterQuality)
joshualitt10d8fc22016-02-29 11:15:06 -080078 , fClipVizColor(SK_ColorTRANSPARENT)
Brian Salomon144a5c52016-12-20 16:48:59 -050079 , fDrawGpuOpBounds(false) {
bungeman@google.come8cc6e82013-01-17 16:30:56 +000080 fUserMatrix.reset();
robertphillips@google.com8b157172013-11-07 22:20:31 +000081
82 // SkPicturePlayback uses the base-class' quickReject calls to cull clipped
83 // operations. This can lead to problems in the debugger which expects all
84 // the operations in the captured skp to appear in the debug canvas. To
85 // circumvent this we create a wide open clip here (an empty clip rect
86 // is not sufficient).
87 // Internally, the SkRect passed to clipRect is converted to an SkIRect and
88 // rounded out. The following code creates a nearly maximal rect that will
89 // not get collapsed by the coming conversions (Due to precision loss the
90 // inset has to be surprisingly large).
Mike Reed8008df12018-01-17 12:20:04 -050091 SkIRect largeIRect = SkRectPriv::MakeILarge();
robertphillips@google.com8b157172013-11-07 22:20:31 +000092 largeIRect.inset(1024, 1024);
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000093 SkRect large = SkRect::Make(largeIRect);
robertphillips@google.com8b157172013-11-07 22:20:31 +000094#ifdef SK_DEBUG
reedb07a94f2014-11-19 05:03:18 -080095 SkASSERT(!large.roundOut().isEmpty());
robertphillips@google.com8b157172013-11-07 22:20:31 +000096#endif
robertphillips@google.com8f90a892014-02-28 18:19:39 +000097 // call the base class' version to avoid adding a draw command
Mike Reedc1f77742016-12-09 09:00:50 -050098 this->INHERITED::onClipRect(large, kReplace_SkClipOp, kHard_ClipEdgeStyle);
chudy@google.com902ebe52012-06-29 14:21:22 +000099}
100
chudy@google.com9cda6f72012-08-07 15:08:33 +0000101SkDebugCanvas::~SkDebugCanvas() {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000102 fCommandVector.deleteAll();
chudy@google.com9cda6f72012-08-07 15:08:33 +0000103}
chudy@google.com902ebe52012-06-29 14:21:22 +0000104
105void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000106 fCommandVector.push(command);
chudy@google.com902ebe52012-06-29 14:21:22 +0000107}
108
109void SkDebugCanvas::draw(SkCanvas* canvas) {
commit-bot@chromium.org1735d662013-12-04 13:42:46 +0000110 if (!fCommandVector.isEmpty()) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000111 this->drawTo(canvas, fCommandVector.count() - 1);
chudy@google.com902ebe52012-06-29 14:21:22 +0000112 }
113}
114
chudy@google.com830b8792012-08-01 15:57:52 +0000115void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) {
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000116 canvas->concat(fUserMatrix);
chudy@google.com830b8792012-08-01 15:57:52 +0000117}
118
119int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000120 SkBitmap bitmap;
reed@google.com9ebcac52014-01-24 18:53:42 +0000121 bitmap.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
chudy@google.com902ebe52012-06-29 14:21:22 +0000122
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000123 SkCanvas canvas(bitmap);
robertphillips@google.com94acc702012-09-06 18:43:21 +0000124 canvas.translate(SkIntToScalar(-x), SkIntToScalar(-y));
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700125 this->applyUserTransform(&canvas);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000126
127 int layer = 0;
chudy@google.com751961d2012-07-31 20:07:42 +0000128 SkColor prev = bitmap.getColor(0,0);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000129 for (int i = 0; i < index; i++) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000130 if (fCommandVector[i]->isVisible()) {
robertphillips70171682014-10-16 14:28:28 -0700131 fCommandVector[i]->setUserMatrix(fUserMatrix);
robertphillips@google.com67baba42013-01-02 20:20:31 +0000132 fCommandVector[i]->execute(&canvas);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000133 }
134 if (prev != bitmap.getColor(0,0)) {
135 layer = i;
136 }
137 prev = bitmap.getColor(0,0);
138 }
139 return layer;
140}
141
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000142// set up the saveLayer commands so that the active ones
143// return true in their 'active' method
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000144void SkDebugCanvas::markActiveCommands(int index) {
145 fActiveLayers.rewind();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000146
147 for (int i = 0; i < fCommandVector.count(); ++i) {
148 fCommandVector[i]->setActive(false);
149 }
150
151 for (int i = 0; i < index; ++i) {
152 SkDrawCommand::Action result = fCommandVector[i]->action();
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000153 if (SkDrawCommand::kPushLayer_Action == result) {
154 fActiveLayers.push(fCommandVector[i]);
155 } else if (SkDrawCommand::kPopLayer_Action == result) {
156 fActiveLayers.pop();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000157 }
158 }
159
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000160 for (int i = 0; i < fActiveLayers.count(); ++i) {
161 fActiveLayers[i]->setActive(true);
162 }
163
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000164}
165
Ravi Mistry99c97962016-12-21 22:41:03 +0000166void SkDebugCanvas::drawTo(SkCanvas* originalCanvas, int index, int m) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000167 SkASSERT(!fCommandVector.isEmpty());
168 SkASSERT(index < fCommandVector.count());
kkinnunen26a00de2015-01-13 23:09:19 -0800169
Ravi Mistry99c97962016-12-21 22:41:03 +0000170 int saveCount = originalCanvas->save();
kkinnunen26a00de2015-01-13 23:09:19 -0800171
Ravi Mistry99c97962016-12-21 22:41:03 +0000172 SkRect windowRect = SkRect::MakeWH(SkIntToScalar(originalCanvas->getBaseLayerSize().width()),
173 SkIntToScalar(originalCanvas->getBaseLayerSize().height()));
chudy@google.com830b8792012-08-01 15:57:52 +0000174
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000175 bool pathOpsMode = getAllowSimplifyClip();
Ravi Mistry99c97962016-12-21 22:41:03 +0000176 originalCanvas->setAllowSimplifyClip(pathOpsMode);
177 originalCanvas->clear(SK_ColorWHITE);
178 originalCanvas->resetMatrix();
kkinnunen26a00de2015-01-13 23:09:19 -0800179 if (!windowRect.isEmpty()) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000180 originalCanvas->clipRect(windowRect, kReplace_SkClipOp);
commit-bot@chromium.orga27622c2013-08-05 16:31:27 +0000181 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000182 this->applyUserTransform(originalCanvas);
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000183
Ravi Mistry99c97962016-12-21 22:41:03 +0000184 DebugPaintFilterCanvas filterCanvas(originalCanvas, fOverdrawViz, fOverrideFilterQuality,
185 fFilterQuality);
chudy@google.com830b8792012-08-01 15:57:52 +0000186
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000187 if (fMegaVizMode) {
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000188 this->markActiveCommands(index);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000189 }
halcanary9d524f22016-03-29 09:03:52 -0700190
joshualitt40836102016-03-11 11:45:53 -0800191#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500192 // If we have a GPU backend we can also visualize the op information
joshualitt10d8fc22016-02-29 11:15:06 -0800193 GrAuditTrail* at = nullptr;
Brian Salomon144a5c52016-12-20 16:48:59 -0500194 if (fDrawGpuOpBounds || m != -1) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000195 // The audit trail must be obtained from the original canvas.
196 at = this->getAuditTrail(originalCanvas);
joshualitt10d8fc22016-02-29 11:15:06 -0800197 }
joshualitt40836102016-03-11 11:45:53 -0800198#endif
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000199
kkinnunen26a00de2015-01-13 23:09:19 -0800200 for (int i = 0; i <= index; i++) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000201 if (i == index && fFilter) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000202 filterCanvas.clear(0xAAFFFFFF);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000203 }
halcanary9d524f22016-03-29 09:03:52 -0700204
joshualitt10d8fc22016-02-29 11:15:06 -0800205#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500206 // We need to flush any pending operations, or they might combine with commands below.
brianosman1c9f9222016-04-15 11:00:51 -0700207 // Previous operations were not registered with the audit trail when they were
208 // created, so if we allow them to combine, the audit trail will fail to find them.
Ravi Mistry99c97962016-12-21 22:41:03 +0000209 filterCanvas.flush();
brianosman1c9f9222016-04-15 11:00:51 -0700210
Brian Salomon42ad83a2016-12-20 16:14:45 -0500211 GrAuditTrail::AutoCollectOps* acb = nullptr;
joshualitt10d8fc22016-02-29 11:15:06 -0800212 if (at) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500213 acb = new GrAuditTrail::AutoCollectOps(at, i);
joshualitt10d8fc22016-02-29 11:15:06 -0800214 }
215#endif
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000216
robertphillips@google.com67baba42013-01-02 20:20:31 +0000217 if (fCommandVector[i]->isVisible()) {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000218 if (fMegaVizMode && fCommandVector[i]->active()) {
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000219 // "active" commands execute their visualization behaviors:
220 // All active saveLayers get replaced with saves so all draws go to the
221 // visible canvas.
222 // All active culls draw their cull box
Ravi Mistry99c97962016-12-21 22:41:03 +0000223 fCommandVector[i]->vizExecute(&filterCanvas);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000224 } else {
robertphillips70171682014-10-16 14:28:28 -0700225 fCommandVector[i]->setUserMatrix(fUserMatrix);
Ravi Mistry99c97962016-12-21 22:41:03 +0000226 fCommandVector[i]->execute(&filterCanvas);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000227 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000228 }
joshualitt10d8fc22016-02-29 11:15:06 -0800229#if SK_SUPPORT_GPU
230 if (at && acb) {
231 delete acb;
232 }
233#endif
chudy@google.com902ebe52012-06-29 14:21:22 +0000234 }
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000235
ethannicholas0a0520a2016-02-12 12:06:53 -0800236 if (SkColorGetA(fClipVizColor) != 0) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000237 filterCanvas.save();
ethannicholas0a0520a2016-02-12 12:06:53 -0800238 #define LARGE_COORD 1000000000
Ravi Mistry99c97962016-12-21 22:41:03 +0000239 filterCanvas.clipRect(
240 SkRect::MakeLTRB(-LARGE_COORD, -LARGE_COORD, LARGE_COORD, LARGE_COORD),
241 kReverseDifference_SkClipOp);
ethannicholas0a0520a2016-02-12 12:06:53 -0800242 SkPaint clipPaint;
243 clipPaint.setColor(fClipVizColor);
Ravi Mistry99c97962016-12-21 22:41:03 +0000244 filterCanvas.drawPaint(clipPaint);
245 filterCanvas.restore();
ethannicholas0a0520a2016-02-12 12:06:53 -0800246 }
247
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000248 if (pathOpsMode) {
249 this->resetClipStackData();
Mike Reeda1361362017-03-07 09:37:29 -0500250 const SkClipStack* clipStack = nullptr;//HACK filterCanvas.getClipStack();
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000251 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
252 const SkClipStack::Element* element;
253 SkPath devPath;
254 while ((element = iter.next())) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400255 SkClipStack::Element::DeviceSpaceType type = element->getDeviceSpaceType();
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000256 SkPath operand;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400257 if (type != SkClipStack::Element::DeviceSpaceType::kEmpty) {
258 element->asDeviceSpacePath(&operand);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000259 }
Mike Reedc1f77742016-12-09 09:00:50 -0500260 SkClipOp elementOp = element->getOp();
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000261 this->addClipStackData(devPath, operand, elementOp);
Mike Reedc1f77742016-12-09 09:00:50 -0500262 if (elementOp == kReplace_SkClipOp) {
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000263 devPath = operand;
264 } else {
265 Op(devPath, operand, (SkPathOp) elementOp, &devPath);
266 }
267 }
268 this->lastClipStackData(devPath);
269 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000270 fMatrix = filterCanvas.getTotalMatrix();
Mike Reed918e1442017-01-23 11:39:45 -0500271 fClip = filterCanvas.getDeviceClipBounds();
Ravi Mistry99c97962016-12-21 22:41:03 +0000272 filterCanvas.restoreToCount(saveCount);
fmalita65cdb572015-03-26 07:24:48 -0700273
joshualitt10d8fc22016-02-29 11:15:06 -0800274#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500275 // draw any ops if required and issue a full reset onto GrAuditTrail
joshualitt10d8fc22016-02-29 11:15:06 -0800276 if (at) {
joshualitte43f7e62016-03-04 10:45:05 -0800277 // just in case there is global reordering, we flush the canvas before querying
278 // GrAuditTrail
joshualittb0666ad2016-03-08 10:43:41 -0800279 GrAuditTrail::AutoEnable ae(at);
Ravi Mistry99c97962016-12-21 22:41:03 +0000280 filterCanvas.flush();
joshualitte43f7e62016-03-04 10:45:05 -0800281
joshualittbdc6b2b2016-03-01 14:22:02 -0800282 // we pick three colorblind-safe colors, 75% alpha
283 static const SkColor kTotalBounds = SkColorSetARGB(0xC0, 0x6A, 0x3D, 0x9A);
Brian Salomon144a5c52016-12-20 16:48:59 -0500284 static const SkColor kCommandOpBounds = SkColorSetARGB(0xC0, 0xE3, 0x1A, 0x1C);
285 static const SkColor kOtherOpBounds = SkColorSetARGB(0xC0, 0xFF, 0x7F, 0x00);
joshualittbdc6b2b2016-03-01 14:22:02 -0800286
Ravi Mistry99c97962016-12-21 22:41:03 +0000287 // get the render target of the top device (from the original canvas) so we can ignore ops
288 // drawn offscreen
289 GrRenderTargetContext* rtc =
290 originalCanvas->internal_private_accessTopLayerRenderTargetContext();
Robert Phillips318c4192017-05-17 09:36:38 -0400291 GrSurfaceProxy::UniqueID proxyID = rtc->asSurfaceProxy()->uniqueID();
joshualitt1d7decf2016-03-01 07:15:52 -0800292
293 // get the bounding boxes to draw
Brian Salomon42ad83a2016-12-20 16:14:45 -0500294 SkTArray<GrAuditTrail::OpInfo> childrenBounds;
joshualitt46b301d2016-03-02 08:32:37 -0800295 if (m == -1) {
296 at->getBoundsByClientID(&childrenBounds, index);
297 } else {
Brian Salomon144a5c52016-12-20 16:48:59 -0500298 // the client wants us to draw the mth op
Brian Salomon42ad83a2016-12-20 16:14:45 -0500299 at->getBoundsByOpListID(&childrenBounds.push_back(), m);
joshualitt46b301d2016-03-02 08:32:37 -0800300 }
joshualitt10d8fc22016-02-29 11:15:06 -0800301 SkPaint paint;
302 paint.setStyle(SkPaint::kStroke_Style);
303 paint.setStrokeWidth(1);
304 for (int i = 0; i < childrenBounds.count(); i++) {
Robert Phillips318c4192017-05-17 09:36:38 -0400305 if (childrenBounds[i].fProxyUniqueID != proxyID) {
joshualitt1d7decf2016-03-01 07:15:52 -0800306 // offscreen draw, ignore for now
307 continue;
308 }
joshualittbdc6b2b2016-03-01 14:22:02 -0800309 paint.setColor(kTotalBounds);
Ravi Mistry99c97962016-12-21 22:41:03 +0000310 filterCanvas.drawRect(childrenBounds[i].fBounds, paint);
Brian Salomon42ad83a2016-12-20 16:14:45 -0500311 for (int j = 0; j < childrenBounds[i].fOps.count(); j++) {
Brian Salomon144a5c52016-12-20 16:48:59 -0500312 const GrAuditTrail::OpInfo::Op& op = childrenBounds[i].fOps[j];
313 if (op.fClientID != index) {
314 paint.setColor(kOtherOpBounds);
joshualitt10d8fc22016-02-29 11:15:06 -0800315 } else {
Brian Salomon144a5c52016-12-20 16:48:59 -0500316 paint.setColor(kCommandOpBounds);
joshualitt10d8fc22016-02-29 11:15:06 -0800317 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000318 filterCanvas.drawRect(op.fBounds, paint);
joshualitt10d8fc22016-02-29 11:15:06 -0800319 }
320 }
joshualitt10d8fc22016-02-29 11:15:06 -0800321 }
joshualitt10d8fc22016-02-29 11:15:06 -0800322#endif
Ravi Mistry99c97962016-12-21 22:41:03 +0000323 this->cleanupAuditTrail(originalCanvas);
chudy@google.com902ebe52012-06-29 14:21:22 +0000324}
325
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000326void SkDebugCanvas::deleteDrawCommandAt(int index) {
327 SkASSERT(index < fCommandVector.count());
328 delete fCommandVector[index];
329 fCommandVector.remove(index);
330}
331
chudy@google.com902ebe52012-06-29 14:21:22 +0000332SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000333 SkASSERT(index < fCommandVector.count());
334 return fCommandVector[index];
chudy@google.com902ebe52012-06-29 14:21:22 +0000335}
336
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000337void SkDebugCanvas::setDrawCommandAt(int index, SkDrawCommand* command) {
338 SkASSERT(index < fCommandVector.count());
339 delete fCommandVector[index];
340 fCommandVector[index] = command;
341}
342
fmalita8c89c522014-11-08 16:18:56 -0800343const SkTDArray<SkString*>* SkDebugCanvas::getCommandInfo(int index) const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000344 SkASSERT(index < fCommandVector.count());
345 return fCommandVector[index]->Info();
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000346}
chudy@google.com902ebe52012-06-29 14:21:22 +0000347
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000348bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000349 SkASSERT(index < fCommandVector.count());
350 return fCommandVector[index]->isVisible();
chudy@google.com902ebe52012-06-29 14:21:22 +0000351}
352
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000353const SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000354 return fCommandVector;
chudy@google.com902ebe52012-06-29 14:21:22 +0000355}
356
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000357SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() {
358 return fCommandVector;
359}
360
joshualittae47aee2016-03-10 13:29:36 -0800361GrAuditTrail* SkDebugCanvas::getAuditTrail(SkCanvas* canvas) {
362 GrAuditTrail* at = nullptr;
joshualitte43f7e62016-03-04 10:45:05 -0800363#if SK_SUPPORT_GPU
robertphillips175dd9b2016-04-28 14:32:04 -0700364 GrContext* ctx = canvas->getGrContext();
365 if (ctx) {
366 at = ctx->getAuditTrail();
joshualitte43f7e62016-03-04 10:45:05 -0800367 }
368#endif
joshualittae47aee2016-03-10 13:29:36 -0800369 return at;
370}
371
Brian Salomon144a5c52016-12-20 16:48:59 -0500372void SkDebugCanvas::drawAndCollectOps(int n, SkCanvas* canvas) {
joshualitt40836102016-03-11 11:45:53 -0800373#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800374 GrAuditTrail* at = this->getAuditTrail(canvas);
375 if (at) {
joshualittae47aee2016-03-10 13:29:36 -0800376 // loop over all of the commands and draw them, this is to collect reordering
377 // information
378 for (int i = 0; i < this->getSize() && i <= n; i++) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500379 GrAuditTrail::AutoCollectOps enable(at, i);
joshualittae47aee2016-03-10 13:29:36 -0800380 fCommandVector[i]->execute(canvas);
381 }
382
383 // in case there is some kind of global reordering
384 {
385 GrAuditTrail::AutoEnable ae(at);
386 canvas->flush();
387 }
joshualittae47aee2016-03-10 13:29:36 -0800388 }
joshualitt40836102016-03-11 11:45:53 -0800389#endif
joshualittae47aee2016-03-10 13:29:36 -0800390}
391
392void SkDebugCanvas::cleanupAuditTrail(SkCanvas* canvas) {
393 GrAuditTrail* at = this->getAuditTrail(canvas);
394 if (at) {
395#if SK_SUPPORT_GPU
396 GrAuditTrail::AutoEnable ae(at);
397 at->fullReset();
398#endif
399 }
400}
401
402Json::Value SkDebugCanvas::toJSON(UrlDataManager& urlDataManager, int n, SkCanvas* canvas) {
Brian Salomon144a5c52016-12-20 16:48:59 -0500403 this->drawAndCollectOps(n, canvas);
halcanary9d524f22016-03-29 09:03:52 -0700404
joshualitte43f7e62016-03-04 10:45:05 -0800405 // now collect json
joshualitt40836102016-03-11 11:45:53 -0800406#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800407 GrAuditTrail* at = this->getAuditTrail(canvas);
joshualitt40836102016-03-11 11:45:53 -0800408#endif
ethannicholas402cd912016-02-10 12:57:30 -0800409 Json::Value result = Json::Value(Json::objectValue);
410 result[SKDEBUGCANVAS_ATTRIBUTE_VERSION] = Json::Value(SKDEBUGCANVAS_VERSION);
411 Json::Value commands = Json::Value(Json::arrayValue);
ethannicholas0a0520a2016-02-12 12:06:53 -0800412 for (int i = 0; i < this->getSize() && i <= n; i++) {
joshualitte43f7e62016-03-04 10:45:05 -0800413 commands[i] = this->getDrawCommandAt(i)->toJSON(urlDataManager);
414#if SK_SUPPORT_GPU
415 if (at) {
416 // TODO if this is inefficient we could add a method to GrAuditTrail which takes
417 // a Json::Value and is only compiled in this file
418 Json::Value parsedFromString;
419 Json::Reader reader;
420 SkAssertResult(reader.parse(at->toJson(i).c_str(), parsedFromString));
421
422 commands[i][SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL] = parsedFromString;
423 }
424#endif
ethannicholas402cd912016-02-10 12:57:30 -0800425 }
joshualittae47aee2016-03-10 13:29:36 -0800426 this->cleanupAuditTrail(canvas);
ethannicholas402cd912016-02-10 12:57:30 -0800427 result[SKDEBUGCANVAS_ATTRIBUTE_COMMANDS] = commands;
428 return result;
429}
430
Brian Salomon144a5c52016-12-20 16:48:59 -0500431Json::Value SkDebugCanvas::toJSONOpList(int n, SkCanvas* canvas) {
432 this->drawAndCollectOps(n, canvas);
joshualittae47aee2016-03-10 13:29:36 -0800433
434 Json::Value parsedFromString;
joshualittae47aee2016-03-10 13:29:36 -0800435#if SK_SUPPORT_GPU
joshualitt40836102016-03-11 11:45:53 -0800436 GrAuditTrail* at = this->getAuditTrail(canvas);
joshualittae47aee2016-03-10 13:29:36 -0800437 if (at) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500438 GrAuditTrail::AutoManageOpList enable(at);
joshualittae47aee2016-03-10 13:29:36 -0800439 Json::Reader reader;
440 SkAssertResult(reader.parse(at->toJson().c_str(), parsedFromString));
441 }
442#endif
443 this->cleanupAuditTrail(canvas);
444 return parsedFromString;
445}
446
fmalita65cdb572015-03-26 07:24:48 -0700447void SkDebugCanvas::setOverdrawViz(bool overdrawViz) {
fmalita65cdb572015-03-26 07:24:48 -0700448 fOverdrawViz = overdrawViz;
fmalita65cdb572015-03-26 07:24:48 -0700449}
450
451void SkDebugCanvas::overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality quality) {
fmalita65cdb572015-03-26 07:24:48 -0700452 fOverrideFilterQuality = overrideTexFiltering;
453 fFilterQuality = quality;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000454}
455
Mike Reedc1f77742016-12-09 09:00:50 -0500456void SkDebugCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000457 this->addDrawCommand(new SkClipPathCommand(path, op, kSoft_ClipEdgeStyle == edgeStyle));
chudy@google.com902ebe52012-06-29 14:21:22 +0000458}
459
Mike Reedc1f77742016-12-09 09:00:50 -0500460void SkDebugCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000461 this->addDrawCommand(new SkClipRectCommand(rect, op, kSoft_ClipEdgeStyle == edgeStyle));
chudy@google.com902ebe52012-06-29 14:21:22 +0000462}
463
Mike Reedc1f77742016-12-09 09:00:50 -0500464void SkDebugCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000465 this->addDrawCommand(new SkClipRRectCommand(rrect, op, kSoft_ClipEdgeStyle == edgeStyle));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000466}
467
Mike Reedc1f77742016-12-09 09:00:50 -0500468void SkDebugCanvas::onClipRegion(const SkRegion& region, SkClipOp op) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000469 this->addDrawCommand(new SkClipRegionCommand(region, op));
chudy@google.com902ebe52012-06-29 14:21:22 +0000470}
471
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000472void SkDebugCanvas::didConcat(const SkMatrix& matrix) {
robertphillips9bafc302015-02-13 11:13:00 -0800473 this->addDrawCommand(new SkConcatCommand(matrix));
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000474 this->INHERITED::didConcat(matrix);
chudy@google.com902ebe52012-06-29 14:21:22 +0000475}
476
reed97660cc2016-06-28 18:54:19 -0700477void SkDebugCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
478 this->addDrawCommand(new SkDrawAnnotationCommand(rect, key, sk_ref_sp(value)));
479}
480
reed41af9662015-01-05 07:49:08 -0800481void SkDebugCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar left,
482 SkScalar top, const SkPaint* paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000483 this->addDrawCommand(new SkDrawBitmapCommand(bitmap, left, top, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000484}
485
reed41af9662015-01-05 07:49:08 -0800486void SkDebugCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700487 const SkPaint* paint, SrcRectConstraint constraint) {
reeda5517e22015-07-14 10:54:12 -0700488 this->addDrawCommand(new SkDrawBitmapRectCommand(bitmap, src, dst, paint,
489 (SrcRectConstraint)constraint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000490}
491
reed41af9662015-01-05 07:49:08 -0800492void SkDebugCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
493 const SkRect& dst, const SkPaint* paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000494 this->addDrawCommand(new SkDrawBitmapNineCommand(bitmap, center, dst, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000495}
496
reed41af9662015-01-05 07:49:08 -0800497void SkDebugCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
498 const SkPaint* paint) {
fmalita651c9202015-07-22 10:23:01 -0700499 this->addDrawCommand(new SkDrawImageCommand(image, left, top, paint));
reed41af9662015-01-05 07:49:08 -0800500}
501
Stan Ilievac42aeb2017-01-12 16:20:50 -0500502void SkDebugCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
503 const SkRect& dst, const SkPaint* paint) {
504 this->addDrawCommand(new SkDrawImageLatticeCommand(image, lattice, dst, paint));
505}
506
reed41af9662015-01-05 07:49:08 -0800507void SkDebugCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700508 const SkPaint* paint, SrcRectConstraint constraint) {
fmalita651c9202015-07-22 10:23:01 -0700509 this->addDrawCommand(new SkDrawImageRectCommand(image, src, dst, paint, constraint));
reed41af9662015-01-05 07:49:08 -0800510}
511
reed41af9662015-01-05 07:49:08 -0800512void SkDebugCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000513 this->addDrawCommand(new SkDrawOvalCommand(oval, paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000514}
515
bsalomonac3aa242016-08-19 11:25:19 -0700516void SkDebugCanvas::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
517 bool useCenter, const SkPaint& paint) {
518 this->addDrawCommand(new SkDrawArcCommand(oval, startAngle, sweepAngle, useCenter, paint));
519}
520
reed41af9662015-01-05 07:49:08 -0800521void SkDebugCanvas::onDrawPaint(const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000522 this->addDrawCommand(new SkDrawPaintCommand(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000523}
524
reed41af9662015-01-05 07:49:08 -0800525void SkDebugCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000526 this->addDrawCommand(new SkDrawPathCommand(path, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000527}
528
mtkleinf0f14112014-12-12 08:46:25 -0800529void SkDebugCanvas::onDrawPicture(const SkPicture* picture,
530 const SkMatrix* matrix,
robertphillipsb3f319f2014-08-13 10:46:23 -0700531 const SkPaint* paint) {
fmalita160ebb22015-04-01 20:58:37 -0700532 this->addDrawCommand(new SkBeginDrawPictureCommand(picture, matrix, paint));
ethannicholas891ad662016-02-12 07:15:45 -0800533 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
534 picture->playback(this);
fmalita160ebb22015-04-01 20:58:37 -0700535 this->addDrawCommand(new SkEndDrawPictureCommand(SkToBool(matrix) || SkToBool(paint)));
chudy@google.com902ebe52012-06-29 14:21:22 +0000536}
537
reed41af9662015-01-05 07:49:08 -0800538void SkDebugCanvas::onDrawPoints(PointMode mode, size_t count,
539 const SkPoint pts[], const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000540 this->addDrawCommand(new SkDrawPointsCommand(mode, count, pts, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000541}
542
reed@google.come0d9ce82014-04-23 04:00:17 +0000543void SkDebugCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
544 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000545 this->addDrawCommand(new SkDrawPosTextCommand(text, byteLength, pos, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000546}
547
reed@google.come0d9ce82014-04-23 04:00:17 +0000548void SkDebugCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
549 SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000550 this->addDrawCommand(
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000551 new SkDrawPosTextHCommand(text, byteLength, xpos, constY, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000552}
553
reed41af9662015-01-05 07:49:08 -0800554void SkDebugCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
chudy@google.com902ebe52012-06-29 14:21:22 +0000555 // NOTE(chudy): Messing up when renamed to DrawRect... Why?
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000556 addDrawCommand(new SkDrawRectCommand(rect, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000557}
558
reed41af9662015-01-05 07:49:08 -0800559void SkDebugCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000560 this->addDrawCommand(new SkDrawRRectCommand(rrect, paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000561}
562
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000563void SkDebugCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
564 const SkPaint& paint) {
commit-bot@chromium.org3d305202014-02-24 17:28:55 +0000565 this->addDrawCommand(new SkDrawDRRectCommand(outer, inner, paint));
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000566}
567
reed@google.come0d9ce82014-04-23 04:00:17 +0000568void SkDebugCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
569 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000570 this->addDrawCommand(new SkDrawTextCommand(text, byteLength, x, y, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000571}
572
reed@google.come0d9ce82014-04-23 04:00:17 +0000573void SkDebugCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
574 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000575 this->addDrawCommand(
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000576 new SkDrawTextOnPathCommand(text, byteLength, path, matrix, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000577}
578
reed45561a02016-07-07 12:47:17 -0700579void SkDebugCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
580 const SkRect* cull, const SkPaint& paint) {
581 this->addDrawCommand(new SkDrawTextRSXformCommand(text, byteLength, xform, cull, paint));
582}
583
fmalitab7425172014-08-26 07:56:44 -0700584void SkDebugCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
585 const SkPaint& paint) {
fmalita37283c22016-09-13 10:00:23 -0700586 this->addDrawCommand(new SkDrawTextBlobCommand(sk_ref_sp(const_cast<SkTextBlob*>(blob)),
587 x, y, paint));
fmalitab7425172014-08-26 07:56:44 -0700588}
589
robertphillips9bafc302015-02-13 11:13:00 -0800590void SkDebugCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400591 const SkPoint texCoords[4], SkBlendMode bmode,
robertphillips9bafc302015-02-13 11:13:00 -0800592 const SkPaint& paint) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400593 this->addDrawCommand(new SkDrawPatchCommand(cubics, colors, texCoords, bmode, paint));
robertphillips9bafc302015-02-13 11:13:00 -0800594}
595
Mike Reedfed9cfd2017-03-17 12:09:04 -0400596void SkDebugCanvas::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode,
597 const SkPaint& paint) {
598 this->addDrawCommand(new SkDrawVerticesCommand(sk_ref_sp(const_cast<SkVertices*>(vertices)),
599 bmode, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000600}
601
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000602void SkDebugCanvas::willRestore() {
603 this->addDrawCommand(new SkRestoreCommand());
604 this->INHERITED::willRestore();
chudy@google.com902ebe52012-06-29 14:21:22 +0000605}
606
Florin Malita5f6102d2014-06-30 10:13:28 -0400607void SkDebugCanvas::willSave() {
608 this->addDrawCommand(new SkSaveCommand());
609 this->INHERITED::willSave();
chudy@google.com902ebe52012-06-29 14:21:22 +0000610}
611
reed4960eee2015-12-18 07:09:18 -0800612SkCanvas::SaveLayerStrategy SkDebugCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
613 this->addDrawCommand(new SkSaveLayerCommand(rec));
614 (void)this->INHERITED::getSaveLayerStrategy(rec);
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000615 // No need for a full layer.
616 return kNoLayer_SaveLayerStrategy;
chudy@google.com902ebe52012-06-29 14:21:22 +0000617}
618
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000619void SkDebugCanvas::didSetMatrix(const SkMatrix& matrix) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000620 this->addDrawCommand(new SkSetMatrixCommand(matrix));
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000621 this->INHERITED::didSetMatrix(matrix);
chudy@google.com902ebe52012-06-29 14:21:22 +0000622}
623
chudy@google.com902ebe52012-06-29 14:21:22 +0000624void SkDebugCanvas::toggleCommand(int index, bool toggle) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000625 SkASSERT(index < fCommandVector.count());
626 fCommandVector[index]->setVisible(toggle);
chudy@google.com902ebe52012-06-29 14:21:22 +0000627}
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000628
629static const char* gFillTypeStrs[] = {
630 "kWinding_FillType",
631 "kEvenOdd_FillType",
632 "kInverseWinding_FillType",
633 "kInverseEvenOdd_FillType"
634};
635
636static const char* gOpStrs[] = {
scroggo5965b732015-04-07 06:53:21 -0700637 "kDifference_PathOp",
638 "kIntersect_PathOp",
639 "kUnion_PathOp",
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000640 "kXor_PathOp",
scroggo5965b732015-04-07 06:53:21 -0700641 "kReverseDifference_PathOp",
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000642};
643
644static const char kHTML4SpaceIndent[] = "&nbsp;&nbsp;&nbsp;&nbsp;";
645
646void SkDebugCanvas::outputScalar(SkScalar num) {
647 if (num == (int) num) {
648 fClipStackData.appendf("%d", (int) num);
649 } else {
650 SkString str;
651 str.printf("%1.9g", num);
652 int width = (int) str.size();
653 const char* cStr = str.c_str();
654 while (cStr[width - 1] == '0') {
655 --width;
656 }
657 str.resize(width);
658 fClipStackData.appendf("%sf", str.c_str());
659 }
660}
661
662void SkDebugCanvas::outputPointsCommon(const SkPoint* pts, int count) {
663 for (int index = 0; index < count; ++index) {
664 this->outputScalar(pts[index].fX);
665 fClipStackData.appendf(", ");
666 this->outputScalar(pts[index].fY);
667 if (index + 1 < count) {
668 fClipStackData.appendf(", ");
669 }
670 }
671}
672
673void SkDebugCanvas::outputPoints(const SkPoint* pts, int count) {
674 this->outputPointsCommon(pts, count);
675 fClipStackData.appendf(");<br>");
676}
677
678void SkDebugCanvas::outputConicPoints(const SkPoint* pts, SkScalar weight) {
679 this->outputPointsCommon(pts, 2);
680 fClipStackData.appendf(", ");
681 this->outputScalar(weight);
682 fClipStackData.appendf(");<br>");
683}
684
685void SkDebugCanvas::addPathData(const SkPath& path, const char* pathName) {
686 SkPath::RawIter iter(path);
687 SkPath::FillType fillType = path.getFillType();
688 fClipStackData.appendf("%sSkPath %s;<br>", kHTML4SpaceIndent, pathName);
689 fClipStackData.appendf("%s%s.setFillType(SkPath::%s);<br>", kHTML4SpaceIndent, pathName,
690 gFillTypeStrs[fillType]);
691 iter.setPath(path);
692 uint8_t verb;
693 SkPoint pts[4];
694 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
695 switch (verb) {
696 case SkPath::kMove_Verb:
697 fClipStackData.appendf("%s%s.moveTo(", kHTML4SpaceIndent, pathName);
698 this->outputPoints(&pts[0], 1);
699 continue;
700 case SkPath::kLine_Verb:
701 fClipStackData.appendf("%s%s.lineTo(", kHTML4SpaceIndent, pathName);
702 this->outputPoints(&pts[1], 1);
703 break;
704 case SkPath::kQuad_Verb:
705 fClipStackData.appendf("%s%s.quadTo(", kHTML4SpaceIndent, pathName);
706 this->outputPoints(&pts[1], 2);
707 break;
708 case SkPath::kConic_Verb:
709 fClipStackData.appendf("%s%s.conicTo(", kHTML4SpaceIndent, pathName);
710 this->outputConicPoints(&pts[1], iter.conicWeight());
711 break;
712 case SkPath::kCubic_Verb:
713 fClipStackData.appendf("%s%s.cubicTo(", kHTML4SpaceIndent, pathName);
714 this->outputPoints(&pts[1], 3);
715 break;
716 case SkPath::kClose_Verb:
717 fClipStackData.appendf("%s%s.close();<br>", kHTML4SpaceIndent, pathName);
718 break;
719 default:
720 SkDEBUGFAIL("bad verb");
721 return;
722 }
723 }
724}
725
726void SkDebugCanvas::addClipStackData(const SkPath& devPath, const SkPath& operand,
Mike Reedc1f77742016-12-09 09:00:50 -0500727 SkClipOp elementOp) {
728 if (elementOp == kReplace_SkClipOp) {
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000729 if (!lastClipStackData(devPath)) {
730 fSaveDevPath = operand;
731 }
732 fCalledAddStackData = false;
733 } else {
734 fClipStackData.appendf("<br>static void test(skiatest::Reporter* reporter,"
735 " const char* filename) {<br>");
736 addPathData(fCalledAddStackData ? devPath : fSaveDevPath, "path");
737 addPathData(operand, "pathB");
738 fClipStackData.appendf("%stestPathOp(reporter, path, pathB, %s, filename);<br>",
Mike Reedebfce6d2016-12-12 10:02:12 -0500739 kHTML4SpaceIndent, gOpStrs[static_cast<int>(elementOp)]);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000740 fClipStackData.appendf("}<br>");
741 fCalledAddStackData = true;
742 }
743}
744
745bool SkDebugCanvas::lastClipStackData(const SkPath& devPath) {
746 if (fCalledAddStackData) {
747 fClipStackData.appendf("<br>");
748 addPathData(devPath, "pathOut");
749 return true;
750 }
751 return false;
752}