blob: 1fe4bc3db80eb3b53dca8c2074c50397c6cf1a2c [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
61private:
Ben Wagnerc03e1c52016-10-17 15:20:02 -040062 bool fOverdrawViz;
fmalita65cdb572015-03-26 07:24:48 -070063 bool fOverrideFilterQuality;
64 SkFilterQuality fFilterQuality;
65
66 typedef SkPaintFilterCanvas INHERITED;
67};
68
kkinnunen26e54002015-01-05 12:58:56 -080069SkDebugCanvas::SkDebugCanvas(int width, int height)
70 : INHERITED(width, height)
halcanary96fcdcc2015-08-27 07:41:13 -070071 , fPicture(nullptr)
commit-bot@chromium.org1735d662013-12-04 13:42:46 +000072 , fFilter(false)
commit-bot@chromium.org768ac852014-03-03 16:32:17 +000073 , fMegaVizMode(false)
robertphillips@google.comf4741c12013-02-06 20:13:54 +000074 , fOverdrawViz(false)
fmalita65cdb572015-03-26 07:24:48 -070075 , fOverrideFilterQuality(false)
ethannicholas0a0520a2016-02-12 12:06:53 -080076 , fFilterQuality(kNone_SkFilterQuality)
joshualitt10d8fc22016-02-29 11:15:06 -080077 , fClipVizColor(SK_ColorTRANSPARENT)
Brian Salomon144a5c52016-12-20 16:48:59 -050078 , fDrawGpuOpBounds(false) {
bungeman@google.come8cc6e82013-01-17 16:30:56 +000079 fUserMatrix.reset();
robertphillips@google.com8b157172013-11-07 22:20:31 +000080
81 // SkPicturePlayback uses the base-class' quickReject calls to cull clipped
82 // operations. This can lead to problems in the debugger which expects all
83 // the operations in the captured skp to appear in the debug canvas. To
84 // circumvent this we create a wide open clip here (an empty clip rect
85 // is not sufficient).
86 // Internally, the SkRect passed to clipRect is converted to an SkIRect and
87 // rounded out. The following code creates a nearly maximal rect that will
88 // not get collapsed by the coming conversions (Due to precision loss the
89 // inset has to be surprisingly large).
90 SkIRect largeIRect = SkIRect::MakeLargest();
91 largeIRect.inset(1024, 1024);
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000092 SkRect large = SkRect::Make(largeIRect);
robertphillips@google.com8b157172013-11-07 22:20:31 +000093#ifdef SK_DEBUG
reedb07a94f2014-11-19 05:03:18 -080094 SkASSERT(!large.roundOut().isEmpty());
robertphillips@google.com8b157172013-11-07 22:20:31 +000095#endif
robertphillips@google.com8f90a892014-02-28 18:19:39 +000096 // call the base class' version to avoid adding a draw command
Mike Reedc1f77742016-12-09 09:00:50 -050097 this->INHERITED::onClipRect(large, kReplace_SkClipOp, kHard_ClipEdgeStyle);
chudy@google.com902ebe52012-06-29 14:21:22 +000098}
99
chudy@google.com9cda6f72012-08-07 15:08:33 +0000100SkDebugCanvas::~SkDebugCanvas() {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000101 fCommandVector.deleteAll();
chudy@google.com9cda6f72012-08-07 15:08:33 +0000102}
chudy@google.com902ebe52012-06-29 14:21:22 +0000103
104void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000105 fCommandVector.push(command);
chudy@google.com902ebe52012-06-29 14:21:22 +0000106}
107
108void SkDebugCanvas::draw(SkCanvas* canvas) {
commit-bot@chromium.org1735d662013-12-04 13:42:46 +0000109 if (!fCommandVector.isEmpty()) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000110 this->drawTo(canvas, fCommandVector.count() - 1);
chudy@google.com902ebe52012-06-29 14:21:22 +0000111 }
112}
113
chudy@google.com830b8792012-08-01 15:57:52 +0000114void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) {
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000115 canvas->concat(fUserMatrix);
chudy@google.com830b8792012-08-01 15:57:52 +0000116}
117
118int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000119 SkBitmap bitmap;
reed@google.com9ebcac52014-01-24 18:53:42 +0000120 bitmap.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
chudy@google.com902ebe52012-06-29 14:21:22 +0000121
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000122 SkCanvas canvas(bitmap);
robertphillips@google.com94acc702012-09-06 18:43:21 +0000123 canvas.translate(SkIntToScalar(-x), SkIntToScalar(-y));
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700124 this->applyUserTransform(&canvas);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000125
126 int layer = 0;
chudy@google.com751961d2012-07-31 20:07:42 +0000127 SkColor prev = bitmap.getColor(0,0);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000128 for (int i = 0; i < index; i++) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000129 if (fCommandVector[i]->isVisible()) {
robertphillips70171682014-10-16 14:28:28 -0700130 fCommandVector[i]->setUserMatrix(fUserMatrix);
robertphillips@google.com67baba42013-01-02 20:20:31 +0000131 fCommandVector[i]->execute(&canvas);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000132 }
133 if (prev != bitmap.getColor(0,0)) {
134 layer = i;
135 }
136 prev = bitmap.getColor(0,0);
137 }
138 return layer;
139}
140
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000141// set up the saveLayer commands so that the active ones
142// return true in their 'active' method
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000143void SkDebugCanvas::markActiveCommands(int index) {
144 fActiveLayers.rewind();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000145
146 for (int i = 0; i < fCommandVector.count(); ++i) {
147 fCommandVector[i]->setActive(false);
148 }
149
150 for (int i = 0; i < index; ++i) {
151 SkDrawCommand::Action result = fCommandVector[i]->action();
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000152 if (SkDrawCommand::kPushLayer_Action == result) {
153 fActiveLayers.push(fCommandVector[i]);
154 } else if (SkDrawCommand::kPopLayer_Action == result) {
155 fActiveLayers.pop();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000156 }
157 }
158
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000159 for (int i = 0; i < fActiveLayers.count(); ++i) {
160 fActiveLayers[i]->setActive(true);
161 }
162
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000163}
164
Ravi Mistry99c97962016-12-21 22:41:03 +0000165void SkDebugCanvas::drawTo(SkCanvas* originalCanvas, int index, int m) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000166 SkASSERT(!fCommandVector.isEmpty());
167 SkASSERT(index < fCommandVector.count());
kkinnunen26a00de2015-01-13 23:09:19 -0800168
Ravi Mistry99c97962016-12-21 22:41:03 +0000169 int saveCount = originalCanvas->save();
kkinnunen26a00de2015-01-13 23:09:19 -0800170
Ravi Mistry99c97962016-12-21 22:41:03 +0000171 SkRect windowRect = SkRect::MakeWH(SkIntToScalar(originalCanvas->getBaseLayerSize().width()),
172 SkIntToScalar(originalCanvas->getBaseLayerSize().height()));
chudy@google.com830b8792012-08-01 15:57:52 +0000173
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000174 bool pathOpsMode = getAllowSimplifyClip();
Ravi Mistry99c97962016-12-21 22:41:03 +0000175 originalCanvas->setAllowSimplifyClip(pathOpsMode);
176 originalCanvas->clear(SK_ColorWHITE);
177 originalCanvas->resetMatrix();
kkinnunen26a00de2015-01-13 23:09:19 -0800178 if (!windowRect.isEmpty()) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000179 originalCanvas->clipRect(windowRect, kReplace_SkClipOp);
commit-bot@chromium.orga27622c2013-08-05 16:31:27 +0000180 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000181 this->applyUserTransform(originalCanvas);
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000182
Ravi Mistry99c97962016-12-21 22:41:03 +0000183 DebugPaintFilterCanvas filterCanvas(originalCanvas, fOverdrawViz, fOverrideFilterQuality,
184 fFilterQuality);
chudy@google.com830b8792012-08-01 15:57:52 +0000185
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000186 if (fMegaVizMode) {
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000187 this->markActiveCommands(index);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000188 }
halcanary9d524f22016-03-29 09:03:52 -0700189
joshualitt40836102016-03-11 11:45:53 -0800190#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500191 // If we have a GPU backend we can also visualize the op information
joshualitt10d8fc22016-02-29 11:15:06 -0800192 GrAuditTrail* at = nullptr;
Brian Salomon144a5c52016-12-20 16:48:59 -0500193 if (fDrawGpuOpBounds || m != -1) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000194 // The audit trail must be obtained from the original canvas.
195 at = this->getAuditTrail(originalCanvas);
joshualitt10d8fc22016-02-29 11:15:06 -0800196 }
joshualitt40836102016-03-11 11:45:53 -0800197#endif
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000198
kkinnunen26a00de2015-01-13 23:09:19 -0800199 for (int i = 0; i <= index; i++) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000200 if (i == index && fFilter) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000201 filterCanvas.clear(0xAAFFFFFF);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000202 }
halcanary9d524f22016-03-29 09:03:52 -0700203
joshualitt10d8fc22016-02-29 11:15:06 -0800204#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500205 // We need to flush any pending operations, or they might combine with commands below.
brianosman1c9f9222016-04-15 11:00:51 -0700206 // Previous operations were not registered with the audit trail when they were
207 // created, so if we allow them to combine, the audit trail will fail to find them.
Ravi Mistry99c97962016-12-21 22:41:03 +0000208 filterCanvas.flush();
brianosman1c9f9222016-04-15 11:00:51 -0700209
Brian Salomon42ad83a2016-12-20 16:14:45 -0500210 GrAuditTrail::AutoCollectOps* acb = nullptr;
joshualitt10d8fc22016-02-29 11:15:06 -0800211 if (at) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500212 acb = new GrAuditTrail::AutoCollectOps(at, i);
joshualitt10d8fc22016-02-29 11:15:06 -0800213 }
214#endif
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000215
robertphillips@google.com67baba42013-01-02 20:20:31 +0000216 if (fCommandVector[i]->isVisible()) {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000217 if (fMegaVizMode && fCommandVector[i]->active()) {
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000218 // "active" commands execute their visualization behaviors:
219 // All active saveLayers get replaced with saves so all draws go to the
220 // visible canvas.
221 // All active culls draw their cull box
Ravi Mistry99c97962016-12-21 22:41:03 +0000222 fCommandVector[i]->vizExecute(&filterCanvas);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000223 } else {
robertphillips70171682014-10-16 14:28:28 -0700224 fCommandVector[i]->setUserMatrix(fUserMatrix);
Ravi Mistry99c97962016-12-21 22:41:03 +0000225 fCommandVector[i]->execute(&filterCanvas);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000226 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000227 }
joshualitt10d8fc22016-02-29 11:15:06 -0800228#if SK_SUPPORT_GPU
229 if (at && acb) {
230 delete acb;
231 }
232#endif
chudy@google.com902ebe52012-06-29 14:21:22 +0000233 }
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000234
ethannicholas0a0520a2016-02-12 12:06:53 -0800235 if (SkColorGetA(fClipVizColor) != 0) {
Ravi Mistry99c97962016-12-21 22:41:03 +0000236 filterCanvas.save();
ethannicholas0a0520a2016-02-12 12:06:53 -0800237 #define LARGE_COORD 1000000000
Ravi Mistry99c97962016-12-21 22:41:03 +0000238 filterCanvas.clipRect(
239 SkRect::MakeLTRB(-LARGE_COORD, -LARGE_COORD, LARGE_COORD, LARGE_COORD),
240 kReverseDifference_SkClipOp);
ethannicholas0a0520a2016-02-12 12:06:53 -0800241 SkPaint clipPaint;
242 clipPaint.setColor(fClipVizColor);
Ravi Mistry99c97962016-12-21 22:41:03 +0000243 filterCanvas.drawPaint(clipPaint);
244 filterCanvas.restore();
ethannicholas0a0520a2016-02-12 12:06:53 -0800245 }
246
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000247 if (pathOpsMode) {
248 this->resetClipStackData();
Mike Reeda1361362017-03-07 09:37:29 -0500249 const SkClipStack* clipStack = nullptr;//HACK filterCanvas.getClipStack();
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000250 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
251 const SkClipStack::Element* element;
252 SkPath devPath;
253 while ((element = iter.next())) {
Brian Salomonf3b46e52017-08-30 11:37:57 -0400254 SkClipStack::Element::DeviceSpaceType type = element->getDeviceSpaceType();
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000255 SkPath operand;
Brian Salomonf3b46e52017-08-30 11:37:57 -0400256 if (type != SkClipStack::Element::DeviceSpaceType::kEmpty) {
257 element->asDeviceSpacePath(&operand);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000258 }
Mike Reedc1f77742016-12-09 09:00:50 -0500259 SkClipOp elementOp = element->getOp();
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000260 this->addClipStackData(devPath, operand, elementOp);
Mike Reedc1f77742016-12-09 09:00:50 -0500261 if (elementOp == kReplace_SkClipOp) {
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000262 devPath = operand;
263 } else {
264 Op(devPath, operand, (SkPathOp) elementOp, &devPath);
265 }
266 }
267 this->lastClipStackData(devPath);
268 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000269 fMatrix = filterCanvas.getTotalMatrix();
Mike Reed918e1442017-01-23 11:39:45 -0500270 fClip = filterCanvas.getDeviceClipBounds();
Ravi Mistry99c97962016-12-21 22:41:03 +0000271 filterCanvas.restoreToCount(saveCount);
fmalita65cdb572015-03-26 07:24:48 -0700272
joshualitt10d8fc22016-02-29 11:15:06 -0800273#if SK_SUPPORT_GPU
Brian Salomon144a5c52016-12-20 16:48:59 -0500274 // draw any ops if required and issue a full reset onto GrAuditTrail
joshualitt10d8fc22016-02-29 11:15:06 -0800275 if (at) {
joshualitte43f7e62016-03-04 10:45:05 -0800276 // just in case there is global reordering, we flush the canvas before querying
277 // GrAuditTrail
joshualittb0666ad2016-03-08 10:43:41 -0800278 GrAuditTrail::AutoEnable ae(at);
Ravi Mistry99c97962016-12-21 22:41:03 +0000279 filterCanvas.flush();
joshualitte43f7e62016-03-04 10:45:05 -0800280
joshualittbdc6b2b2016-03-01 14:22:02 -0800281 // we pick three colorblind-safe colors, 75% alpha
282 static const SkColor kTotalBounds = SkColorSetARGB(0xC0, 0x6A, 0x3D, 0x9A);
Brian Salomon144a5c52016-12-20 16:48:59 -0500283 static const SkColor kCommandOpBounds = SkColorSetARGB(0xC0, 0xE3, 0x1A, 0x1C);
284 static const SkColor kOtherOpBounds = SkColorSetARGB(0xC0, 0xFF, 0x7F, 0x00);
joshualittbdc6b2b2016-03-01 14:22:02 -0800285
Ravi Mistry99c97962016-12-21 22:41:03 +0000286 // get the render target of the top device (from the original canvas) so we can ignore ops
287 // drawn offscreen
288 GrRenderTargetContext* rtc =
289 originalCanvas->internal_private_accessTopLayerRenderTargetContext();
Robert Phillips318c4192017-05-17 09:36:38 -0400290 GrSurfaceProxy::UniqueID proxyID = rtc->asSurfaceProxy()->uniqueID();
joshualitt1d7decf2016-03-01 07:15:52 -0800291
292 // get the bounding boxes to draw
Brian Salomon42ad83a2016-12-20 16:14:45 -0500293 SkTArray<GrAuditTrail::OpInfo> childrenBounds;
joshualitt46b301d2016-03-02 08:32:37 -0800294 if (m == -1) {
295 at->getBoundsByClientID(&childrenBounds, index);
296 } else {
Brian Salomon144a5c52016-12-20 16:48:59 -0500297 // the client wants us to draw the mth op
Brian Salomon42ad83a2016-12-20 16:14:45 -0500298 at->getBoundsByOpListID(&childrenBounds.push_back(), m);
joshualitt46b301d2016-03-02 08:32:37 -0800299 }
joshualitt10d8fc22016-02-29 11:15:06 -0800300 SkPaint paint;
301 paint.setStyle(SkPaint::kStroke_Style);
302 paint.setStrokeWidth(1);
303 for (int i = 0; i < childrenBounds.count(); i++) {
Robert Phillips318c4192017-05-17 09:36:38 -0400304 if (childrenBounds[i].fProxyUniqueID != proxyID) {
joshualitt1d7decf2016-03-01 07:15:52 -0800305 // offscreen draw, ignore for now
306 continue;
307 }
joshualittbdc6b2b2016-03-01 14:22:02 -0800308 paint.setColor(kTotalBounds);
Ravi Mistry99c97962016-12-21 22:41:03 +0000309 filterCanvas.drawRect(childrenBounds[i].fBounds, paint);
Brian Salomon42ad83a2016-12-20 16:14:45 -0500310 for (int j = 0; j < childrenBounds[i].fOps.count(); j++) {
Brian Salomon144a5c52016-12-20 16:48:59 -0500311 const GrAuditTrail::OpInfo::Op& op = childrenBounds[i].fOps[j];
312 if (op.fClientID != index) {
313 paint.setColor(kOtherOpBounds);
joshualitt10d8fc22016-02-29 11:15:06 -0800314 } else {
Brian Salomon144a5c52016-12-20 16:48:59 -0500315 paint.setColor(kCommandOpBounds);
joshualitt10d8fc22016-02-29 11:15:06 -0800316 }
Ravi Mistry99c97962016-12-21 22:41:03 +0000317 filterCanvas.drawRect(op.fBounds, paint);
joshualitt10d8fc22016-02-29 11:15:06 -0800318 }
319 }
joshualitt10d8fc22016-02-29 11:15:06 -0800320 }
joshualitt10d8fc22016-02-29 11:15:06 -0800321#endif
Ravi Mistry99c97962016-12-21 22:41:03 +0000322 this->cleanupAuditTrail(originalCanvas);
chudy@google.com902ebe52012-06-29 14:21:22 +0000323}
324
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000325void SkDebugCanvas::deleteDrawCommandAt(int index) {
326 SkASSERT(index < fCommandVector.count());
327 delete fCommandVector[index];
328 fCommandVector.remove(index);
329}
330
chudy@google.com902ebe52012-06-29 14:21:22 +0000331SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000332 SkASSERT(index < fCommandVector.count());
333 return fCommandVector[index];
chudy@google.com902ebe52012-06-29 14:21:22 +0000334}
335
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000336void SkDebugCanvas::setDrawCommandAt(int index, SkDrawCommand* command) {
337 SkASSERT(index < fCommandVector.count());
338 delete fCommandVector[index];
339 fCommandVector[index] = command;
340}
341
fmalita8c89c522014-11-08 16:18:56 -0800342const SkTDArray<SkString*>* SkDebugCanvas::getCommandInfo(int index) const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000343 SkASSERT(index < fCommandVector.count());
344 return fCommandVector[index]->Info();
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000345}
chudy@google.com902ebe52012-06-29 14:21:22 +0000346
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000347bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000348 SkASSERT(index < fCommandVector.count());
349 return fCommandVector[index]->isVisible();
chudy@google.com902ebe52012-06-29 14:21:22 +0000350}
351
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000352const SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000353 return fCommandVector;
chudy@google.com902ebe52012-06-29 14:21:22 +0000354}
355
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000356SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() {
357 return fCommandVector;
358}
359
joshualittae47aee2016-03-10 13:29:36 -0800360GrAuditTrail* SkDebugCanvas::getAuditTrail(SkCanvas* canvas) {
361 GrAuditTrail* at = nullptr;
joshualitte43f7e62016-03-04 10:45:05 -0800362#if SK_SUPPORT_GPU
robertphillips175dd9b2016-04-28 14:32:04 -0700363 GrContext* ctx = canvas->getGrContext();
364 if (ctx) {
365 at = ctx->getAuditTrail();
joshualitte43f7e62016-03-04 10:45:05 -0800366 }
367#endif
joshualittae47aee2016-03-10 13:29:36 -0800368 return at;
369}
370
Brian Salomon144a5c52016-12-20 16:48:59 -0500371void SkDebugCanvas::drawAndCollectOps(int n, SkCanvas* canvas) {
joshualitt40836102016-03-11 11:45:53 -0800372#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800373 GrAuditTrail* at = this->getAuditTrail(canvas);
374 if (at) {
joshualittae47aee2016-03-10 13:29:36 -0800375 // loop over all of the commands and draw them, this is to collect reordering
376 // information
377 for (int i = 0; i < this->getSize() && i <= n; i++) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500378 GrAuditTrail::AutoCollectOps enable(at, i);
joshualittae47aee2016-03-10 13:29:36 -0800379 fCommandVector[i]->execute(canvas);
380 }
381
382 // in case there is some kind of global reordering
383 {
384 GrAuditTrail::AutoEnable ae(at);
385 canvas->flush();
386 }
joshualittae47aee2016-03-10 13:29:36 -0800387 }
joshualitt40836102016-03-11 11:45:53 -0800388#endif
joshualittae47aee2016-03-10 13:29:36 -0800389}
390
391void SkDebugCanvas::cleanupAuditTrail(SkCanvas* canvas) {
392 GrAuditTrail* at = this->getAuditTrail(canvas);
393 if (at) {
394#if SK_SUPPORT_GPU
395 GrAuditTrail::AutoEnable ae(at);
396 at->fullReset();
397#endif
398 }
399}
400
401Json::Value SkDebugCanvas::toJSON(UrlDataManager& urlDataManager, int n, SkCanvas* canvas) {
Brian Salomon144a5c52016-12-20 16:48:59 -0500402 this->drawAndCollectOps(n, canvas);
halcanary9d524f22016-03-29 09:03:52 -0700403
joshualitte43f7e62016-03-04 10:45:05 -0800404 // now collect json
joshualitt40836102016-03-11 11:45:53 -0800405#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800406 GrAuditTrail* at = this->getAuditTrail(canvas);
joshualitt40836102016-03-11 11:45:53 -0800407#endif
ethannicholas402cd912016-02-10 12:57:30 -0800408 Json::Value result = Json::Value(Json::objectValue);
409 result[SKDEBUGCANVAS_ATTRIBUTE_VERSION] = Json::Value(SKDEBUGCANVAS_VERSION);
410 Json::Value commands = Json::Value(Json::arrayValue);
ethannicholas0a0520a2016-02-12 12:06:53 -0800411 for (int i = 0; i < this->getSize() && i <= n; i++) {
joshualitte43f7e62016-03-04 10:45:05 -0800412 commands[i] = this->getDrawCommandAt(i)->toJSON(urlDataManager);
413#if SK_SUPPORT_GPU
414 if (at) {
415 // TODO if this is inefficient we could add a method to GrAuditTrail which takes
416 // a Json::Value and is only compiled in this file
417 Json::Value parsedFromString;
418 Json::Reader reader;
419 SkAssertResult(reader.parse(at->toJson(i).c_str(), parsedFromString));
420
421 commands[i][SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL] = parsedFromString;
422 }
423#endif
ethannicholas402cd912016-02-10 12:57:30 -0800424 }
joshualittae47aee2016-03-10 13:29:36 -0800425 this->cleanupAuditTrail(canvas);
ethannicholas402cd912016-02-10 12:57:30 -0800426 result[SKDEBUGCANVAS_ATTRIBUTE_COMMANDS] = commands;
427 return result;
428}
429
Brian Salomon144a5c52016-12-20 16:48:59 -0500430Json::Value SkDebugCanvas::toJSONOpList(int n, SkCanvas* canvas) {
431 this->drawAndCollectOps(n, canvas);
joshualittae47aee2016-03-10 13:29:36 -0800432
433 Json::Value parsedFromString;
joshualittae47aee2016-03-10 13:29:36 -0800434#if SK_SUPPORT_GPU
joshualitt40836102016-03-11 11:45:53 -0800435 GrAuditTrail* at = this->getAuditTrail(canvas);
joshualittae47aee2016-03-10 13:29:36 -0800436 if (at) {
Brian Salomon42ad83a2016-12-20 16:14:45 -0500437 GrAuditTrail::AutoManageOpList enable(at);
joshualittae47aee2016-03-10 13:29:36 -0800438 Json::Reader reader;
439 SkAssertResult(reader.parse(at->toJson().c_str(), parsedFromString));
440 }
441#endif
442 this->cleanupAuditTrail(canvas);
443 return parsedFromString;
444}
445
fmalita65cdb572015-03-26 07:24:48 -0700446void SkDebugCanvas::setOverdrawViz(bool overdrawViz) {
fmalita65cdb572015-03-26 07:24:48 -0700447 fOverdrawViz = overdrawViz;
fmalita65cdb572015-03-26 07:24:48 -0700448}
449
450void SkDebugCanvas::overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality quality) {
fmalita65cdb572015-03-26 07:24:48 -0700451 fOverrideFilterQuality = overrideTexFiltering;
452 fFilterQuality = quality;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000453}
454
Mike Reedc1f77742016-12-09 09:00:50 -0500455void SkDebugCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000456 this->addDrawCommand(new SkClipPathCommand(path, op, kSoft_ClipEdgeStyle == edgeStyle));
chudy@google.com902ebe52012-06-29 14:21:22 +0000457}
458
Mike Reedc1f77742016-12-09 09:00:50 -0500459void SkDebugCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000460 this->addDrawCommand(new SkClipRectCommand(rect, op, kSoft_ClipEdgeStyle == edgeStyle));
chudy@google.com902ebe52012-06-29 14:21:22 +0000461}
462
Mike Reedc1f77742016-12-09 09:00:50 -0500463void SkDebugCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000464 this->addDrawCommand(new SkClipRRectCommand(rrect, op, kSoft_ClipEdgeStyle == edgeStyle));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000465}
466
Mike Reedc1f77742016-12-09 09:00:50 -0500467void SkDebugCanvas::onClipRegion(const SkRegion& region, SkClipOp op) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000468 this->addDrawCommand(new SkClipRegionCommand(region, op));
chudy@google.com902ebe52012-06-29 14:21:22 +0000469}
470
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000471void SkDebugCanvas::didConcat(const SkMatrix& matrix) {
robertphillips9bafc302015-02-13 11:13:00 -0800472 this->addDrawCommand(new SkConcatCommand(matrix));
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000473 this->INHERITED::didConcat(matrix);
chudy@google.com902ebe52012-06-29 14:21:22 +0000474}
475
reed97660cc2016-06-28 18:54:19 -0700476void SkDebugCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
477 this->addDrawCommand(new SkDrawAnnotationCommand(rect, key, sk_ref_sp(value)));
478}
479
reed41af9662015-01-05 07:49:08 -0800480void SkDebugCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar left,
481 SkScalar top, const SkPaint* paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000482 this->addDrawCommand(new SkDrawBitmapCommand(bitmap, left, top, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000483}
484
reed41af9662015-01-05 07:49:08 -0800485void SkDebugCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700486 const SkPaint* paint, SrcRectConstraint constraint) {
reeda5517e22015-07-14 10:54:12 -0700487 this->addDrawCommand(new SkDrawBitmapRectCommand(bitmap, src, dst, paint,
488 (SrcRectConstraint)constraint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000489}
490
reed41af9662015-01-05 07:49:08 -0800491void SkDebugCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
492 const SkRect& dst, const SkPaint* paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000493 this->addDrawCommand(new SkDrawBitmapNineCommand(bitmap, center, dst, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000494}
495
reed41af9662015-01-05 07:49:08 -0800496void SkDebugCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
497 const SkPaint* paint) {
fmalita651c9202015-07-22 10:23:01 -0700498 this->addDrawCommand(new SkDrawImageCommand(image, left, top, paint));
reed41af9662015-01-05 07:49:08 -0800499}
500
Stan Ilievac42aeb2017-01-12 16:20:50 -0500501void SkDebugCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
502 const SkRect& dst, const SkPaint* paint) {
503 this->addDrawCommand(new SkDrawImageLatticeCommand(image, lattice, dst, paint));
504}
505
reed41af9662015-01-05 07:49:08 -0800506void SkDebugCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700507 const SkPaint* paint, SrcRectConstraint constraint) {
fmalita651c9202015-07-22 10:23:01 -0700508 this->addDrawCommand(new SkDrawImageRectCommand(image, src, dst, paint, constraint));
reed41af9662015-01-05 07:49:08 -0800509}
510
reed41af9662015-01-05 07:49:08 -0800511void SkDebugCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000512 this->addDrawCommand(new SkDrawOvalCommand(oval, paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000513}
514
bsalomonac3aa242016-08-19 11:25:19 -0700515void SkDebugCanvas::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
516 bool useCenter, const SkPaint& paint) {
517 this->addDrawCommand(new SkDrawArcCommand(oval, startAngle, sweepAngle, useCenter, paint));
518}
519
reed41af9662015-01-05 07:49:08 -0800520void SkDebugCanvas::onDrawPaint(const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000521 this->addDrawCommand(new SkDrawPaintCommand(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000522}
523
reed41af9662015-01-05 07:49:08 -0800524void SkDebugCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000525 this->addDrawCommand(new SkDrawPathCommand(path, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000526}
527
mtkleinf0f14112014-12-12 08:46:25 -0800528void SkDebugCanvas::onDrawPicture(const SkPicture* picture,
529 const SkMatrix* matrix,
robertphillipsb3f319f2014-08-13 10:46:23 -0700530 const SkPaint* paint) {
fmalita160ebb22015-04-01 20:58:37 -0700531 this->addDrawCommand(new SkBeginDrawPictureCommand(picture, matrix, paint));
ethannicholas891ad662016-02-12 07:15:45 -0800532 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
533 picture->playback(this);
fmalita160ebb22015-04-01 20:58:37 -0700534 this->addDrawCommand(new SkEndDrawPictureCommand(SkToBool(matrix) || SkToBool(paint)));
chudy@google.com902ebe52012-06-29 14:21:22 +0000535}
536
reed41af9662015-01-05 07:49:08 -0800537void SkDebugCanvas::onDrawPoints(PointMode mode, size_t count,
538 const SkPoint pts[], const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000539 this->addDrawCommand(new SkDrawPointsCommand(mode, count, pts, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000540}
541
reed@google.come0d9ce82014-04-23 04:00:17 +0000542void SkDebugCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
543 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000544 this->addDrawCommand(new SkDrawPosTextCommand(text, byteLength, pos, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000545}
546
reed@google.come0d9ce82014-04-23 04:00:17 +0000547void SkDebugCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
548 SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000549 this->addDrawCommand(
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000550 new SkDrawPosTextHCommand(text, byteLength, xpos, constY, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000551}
552
reed41af9662015-01-05 07:49:08 -0800553void SkDebugCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
chudy@google.com902ebe52012-06-29 14:21:22 +0000554 // NOTE(chudy): Messing up when renamed to DrawRect... Why?
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000555 addDrawCommand(new SkDrawRectCommand(rect, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000556}
557
reed41af9662015-01-05 07:49:08 -0800558void SkDebugCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000559 this->addDrawCommand(new SkDrawRRectCommand(rrect, paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000560}
561
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000562void SkDebugCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
563 const SkPaint& paint) {
commit-bot@chromium.org3d305202014-02-24 17:28:55 +0000564 this->addDrawCommand(new SkDrawDRRectCommand(outer, inner, paint));
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000565}
566
reed@google.come0d9ce82014-04-23 04:00:17 +0000567void SkDebugCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
568 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000569 this->addDrawCommand(new SkDrawTextCommand(text, byteLength, x, y, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000570}
571
reed@google.come0d9ce82014-04-23 04:00:17 +0000572void SkDebugCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
573 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000574 this->addDrawCommand(
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000575 new SkDrawTextOnPathCommand(text, byteLength, path, matrix, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000576}
577
reed45561a02016-07-07 12:47:17 -0700578void SkDebugCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
579 const SkRect* cull, const SkPaint& paint) {
580 this->addDrawCommand(new SkDrawTextRSXformCommand(text, byteLength, xform, cull, paint));
581}
582
fmalitab7425172014-08-26 07:56:44 -0700583void SkDebugCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
584 const SkPaint& paint) {
fmalita37283c22016-09-13 10:00:23 -0700585 this->addDrawCommand(new SkDrawTextBlobCommand(sk_ref_sp(const_cast<SkTextBlob*>(blob)),
586 x, y, paint));
fmalitab7425172014-08-26 07:56:44 -0700587}
588
robertphillips9bafc302015-02-13 11:13:00 -0800589void SkDebugCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400590 const SkPoint texCoords[4], SkBlendMode bmode,
robertphillips9bafc302015-02-13 11:13:00 -0800591 const SkPaint& paint) {
Mike Reed7d954ad2016-10-28 15:42:34 -0400592 this->addDrawCommand(new SkDrawPatchCommand(cubics, colors, texCoords, bmode, paint));
robertphillips9bafc302015-02-13 11:13:00 -0800593}
594
Mike Reedfed9cfd2017-03-17 12:09:04 -0400595void SkDebugCanvas::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode,
596 const SkPaint& paint) {
597 this->addDrawCommand(new SkDrawVerticesCommand(sk_ref_sp(const_cast<SkVertices*>(vertices)),
598 bmode, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000599}
600
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000601void SkDebugCanvas::willRestore() {
602 this->addDrawCommand(new SkRestoreCommand());
603 this->INHERITED::willRestore();
chudy@google.com902ebe52012-06-29 14:21:22 +0000604}
605
Florin Malita5f6102d2014-06-30 10:13:28 -0400606void SkDebugCanvas::willSave() {
607 this->addDrawCommand(new SkSaveCommand());
608 this->INHERITED::willSave();
chudy@google.com902ebe52012-06-29 14:21:22 +0000609}
610
reed4960eee2015-12-18 07:09:18 -0800611SkCanvas::SaveLayerStrategy SkDebugCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
612 this->addDrawCommand(new SkSaveLayerCommand(rec));
613 (void)this->INHERITED::getSaveLayerStrategy(rec);
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000614 // No need for a full layer.
615 return kNoLayer_SaveLayerStrategy;
chudy@google.com902ebe52012-06-29 14:21:22 +0000616}
617
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000618void SkDebugCanvas::didSetMatrix(const SkMatrix& matrix) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000619 this->addDrawCommand(new SkSetMatrixCommand(matrix));
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000620 this->INHERITED::didSetMatrix(matrix);
chudy@google.com902ebe52012-06-29 14:21:22 +0000621}
622
chudy@google.com902ebe52012-06-29 14:21:22 +0000623void SkDebugCanvas::toggleCommand(int index, bool toggle) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000624 SkASSERT(index < fCommandVector.count());
625 fCommandVector[index]->setVisible(toggle);
chudy@google.com902ebe52012-06-29 14:21:22 +0000626}
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000627
628static const char* gFillTypeStrs[] = {
629 "kWinding_FillType",
630 "kEvenOdd_FillType",
631 "kInverseWinding_FillType",
632 "kInverseEvenOdd_FillType"
633};
634
635static const char* gOpStrs[] = {
scroggo5965b732015-04-07 06:53:21 -0700636 "kDifference_PathOp",
637 "kIntersect_PathOp",
638 "kUnion_PathOp",
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000639 "kXor_PathOp",
scroggo5965b732015-04-07 06:53:21 -0700640 "kReverseDifference_PathOp",
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000641};
642
643static const char kHTML4SpaceIndent[] = "&nbsp;&nbsp;&nbsp;&nbsp;";
644
645void SkDebugCanvas::outputScalar(SkScalar num) {
646 if (num == (int) num) {
647 fClipStackData.appendf("%d", (int) num);
648 } else {
649 SkString str;
650 str.printf("%1.9g", num);
651 int width = (int) str.size();
652 const char* cStr = str.c_str();
653 while (cStr[width - 1] == '0') {
654 --width;
655 }
656 str.resize(width);
657 fClipStackData.appendf("%sf", str.c_str());
658 }
659}
660
661void SkDebugCanvas::outputPointsCommon(const SkPoint* pts, int count) {
662 for (int index = 0; index < count; ++index) {
663 this->outputScalar(pts[index].fX);
664 fClipStackData.appendf(", ");
665 this->outputScalar(pts[index].fY);
666 if (index + 1 < count) {
667 fClipStackData.appendf(", ");
668 }
669 }
670}
671
672void SkDebugCanvas::outputPoints(const SkPoint* pts, int count) {
673 this->outputPointsCommon(pts, count);
674 fClipStackData.appendf(");<br>");
675}
676
677void SkDebugCanvas::outputConicPoints(const SkPoint* pts, SkScalar weight) {
678 this->outputPointsCommon(pts, 2);
679 fClipStackData.appendf(", ");
680 this->outputScalar(weight);
681 fClipStackData.appendf(");<br>");
682}
683
684void SkDebugCanvas::addPathData(const SkPath& path, const char* pathName) {
685 SkPath::RawIter iter(path);
686 SkPath::FillType fillType = path.getFillType();
687 fClipStackData.appendf("%sSkPath %s;<br>", kHTML4SpaceIndent, pathName);
688 fClipStackData.appendf("%s%s.setFillType(SkPath::%s);<br>", kHTML4SpaceIndent, pathName,
689 gFillTypeStrs[fillType]);
690 iter.setPath(path);
691 uint8_t verb;
692 SkPoint pts[4];
693 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
694 switch (verb) {
695 case SkPath::kMove_Verb:
696 fClipStackData.appendf("%s%s.moveTo(", kHTML4SpaceIndent, pathName);
697 this->outputPoints(&pts[0], 1);
698 continue;
699 case SkPath::kLine_Verb:
700 fClipStackData.appendf("%s%s.lineTo(", kHTML4SpaceIndent, pathName);
701 this->outputPoints(&pts[1], 1);
702 break;
703 case SkPath::kQuad_Verb:
704 fClipStackData.appendf("%s%s.quadTo(", kHTML4SpaceIndent, pathName);
705 this->outputPoints(&pts[1], 2);
706 break;
707 case SkPath::kConic_Verb:
708 fClipStackData.appendf("%s%s.conicTo(", kHTML4SpaceIndent, pathName);
709 this->outputConicPoints(&pts[1], iter.conicWeight());
710 break;
711 case SkPath::kCubic_Verb:
712 fClipStackData.appendf("%s%s.cubicTo(", kHTML4SpaceIndent, pathName);
713 this->outputPoints(&pts[1], 3);
714 break;
715 case SkPath::kClose_Verb:
716 fClipStackData.appendf("%s%s.close();<br>", kHTML4SpaceIndent, pathName);
717 break;
718 default:
719 SkDEBUGFAIL("bad verb");
720 return;
721 }
722 }
723}
724
725void SkDebugCanvas::addClipStackData(const SkPath& devPath, const SkPath& operand,
Mike Reedc1f77742016-12-09 09:00:50 -0500726 SkClipOp elementOp) {
727 if (elementOp == kReplace_SkClipOp) {
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000728 if (!lastClipStackData(devPath)) {
729 fSaveDevPath = operand;
730 }
731 fCalledAddStackData = false;
732 } else {
733 fClipStackData.appendf("<br>static void test(skiatest::Reporter* reporter,"
734 " const char* filename) {<br>");
735 addPathData(fCalledAddStackData ? devPath : fSaveDevPath, "path");
736 addPathData(operand, "pathB");
737 fClipStackData.appendf("%stestPathOp(reporter, path, pathB, %s, filename);<br>",
Mike Reedebfce6d2016-12-12 10:02:12 -0500738 kHTML4SpaceIndent, gOpStrs[static_cast<int>(elementOp)]);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000739 fClipStackData.appendf("}<br>");
740 fCalledAddStackData = true;
741 }
742}
743
744bool SkDebugCanvas::lastClipStackData(const SkPath& devPath) {
745 if (fCalledAddStackData) {
746 fClipStackData.appendf("<br>");
747 addPathData(devPath, "pathOut");
748 return true;
749 }
750 return false;
751}