blob: 123b9b4aa874279ae684f4e0e04ee0744467e84c [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"
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +000012#include "SkDevice.h"
fmalita65cdb572015-03-26 07:24:48 -070013#include "SkPaintFilterCanvas.h"
robertphillipsf42fca42016-01-27 05:00:04 -080014#include "SkOverdrawMode.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"
19#include "GrRenderTarget.h"
joshualitt1d7decf2016-03-01 07:15:52 -080020#include "SkGpuDevice.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:
halcanary385fe4d2015-08-26 13:07:48 -070030 DebugPaintFilterCanvas(int width,
31 int height,
32 bool overdrawViz,
33 bool overrideFilterQuality,
fmalita65cdb572015-03-26 07:24:48 -070034 SkFilterQuality quality)
35 : INHERITED(width, height)
reedcfb6bdf2016-03-29 11:32:50 -070036 , fOverdrawXfermode(overdrawViz ? SkOverdrawMode::Make() : nullptr)
fmalita65cdb572015-03-26 07:24:48 -070037 , fOverrideFilterQuality(overrideFilterQuality)
halcanary385fe4d2015-08-26 13:07:48 -070038 , fFilterQuality(quality) {}
fmalita65cdb572015-03-26 07:24:48 -070039
40protected:
fmalita32cdc322016-01-12 07:21:11 -080041 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
42 if (*paint) {
fmalitabad23dc2016-01-11 13:58:29 -080043 if (nullptr != fOverdrawXfermode.get()) {
fmalita32cdc322016-01-12 07:21:11 -080044 paint->writable()->setAntiAlias(false);
reedcfb6bdf2016-03-29 11:32:50 -070045 paint->writable()->setXfermode(fOverdrawXfermode);
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:
reedcfb6bdf2016-03-29 11:32:50 -070063 sk_sp<SkXfermode> fOverdrawXfermode;
fmalita65cdb572015-03-26 07:24:48 -070064
65 bool fOverrideFilterQuality;
66 SkFilterQuality fFilterQuality;
67
68 typedef SkPaintFilterCanvas INHERITED;
69};
70
kkinnunen26e54002015-01-05 12:58:56 -080071SkDebugCanvas::SkDebugCanvas(int width, int height)
72 : INHERITED(width, height)
halcanary96fcdcc2015-08-27 07:41:13 -070073 , fPicture(nullptr)
commit-bot@chromium.org1735d662013-12-04 13:42:46 +000074 , fFilter(false)
commit-bot@chromium.org768ac852014-03-03 16:32:17 +000075 , fMegaVizMode(false)
robertphillips@google.comf4741c12013-02-06 20:13:54 +000076 , fOverdrawViz(false)
fmalita65cdb572015-03-26 07:24:48 -070077 , fOverrideFilterQuality(false)
ethannicholas0a0520a2016-02-12 12:06:53 -080078 , fFilterQuality(kNone_SkFilterQuality)
joshualitt10d8fc22016-02-29 11:15:06 -080079 , fClipVizColor(SK_ColorTRANSPARENT)
joshualitt5d5207a2016-02-29 12:46:04 -080080 , fDrawGpuBatchBounds(false) {
bungeman@google.come8cc6e82013-01-17 16:30:56 +000081 fUserMatrix.reset();
robertphillips@google.com8b157172013-11-07 22:20:31 +000082
83 // SkPicturePlayback uses the base-class' quickReject calls to cull clipped
84 // operations. This can lead to problems in the debugger which expects all
85 // the operations in the captured skp to appear in the debug canvas. To
86 // circumvent this we create a wide open clip here (an empty clip rect
87 // is not sufficient).
88 // Internally, the SkRect passed to clipRect is converted to an SkIRect and
89 // rounded out. The following code creates a nearly maximal rect that will
90 // not get collapsed by the coming conversions (Due to precision loss the
91 // inset has to be surprisingly large).
92 SkIRect largeIRect = SkIRect::MakeLargest();
93 largeIRect.inset(1024, 1024);
robertphillips@google.com6c1e49a2013-11-10 15:08:45 +000094 SkRect large = SkRect::Make(largeIRect);
robertphillips@google.com8b157172013-11-07 22:20:31 +000095#ifdef SK_DEBUG
reedb07a94f2014-11-19 05:03:18 -080096 SkASSERT(!large.roundOut().isEmpty());
robertphillips@google.com8b157172013-11-07 22:20:31 +000097#endif
robertphillips@google.com8f90a892014-02-28 18:19:39 +000098 // call the base class' version to avoid adding a draw command
99 this->INHERITED::onClipRect(large, SkRegion::kReplace_Op, kHard_ClipEdgeStyle);
chudy@google.com902ebe52012-06-29 14:21:22 +0000100}
101
chudy@google.com9cda6f72012-08-07 15:08:33 +0000102SkDebugCanvas::~SkDebugCanvas() {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000103 fCommandVector.deleteAll();
chudy@google.com9cda6f72012-08-07 15:08:33 +0000104}
chudy@google.com902ebe52012-06-29 14:21:22 +0000105
106void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000107 fCommandVector.push(command);
chudy@google.com902ebe52012-06-29 14:21:22 +0000108}
109
110void SkDebugCanvas::draw(SkCanvas* canvas) {
commit-bot@chromium.org1735d662013-12-04 13:42:46 +0000111 if (!fCommandVector.isEmpty()) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000112 this->drawTo(canvas, fCommandVector.count() - 1);
chudy@google.com902ebe52012-06-29 14:21:22 +0000113 }
114}
115
chudy@google.com830b8792012-08-01 15:57:52 +0000116void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) {
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000117 canvas->concat(fUserMatrix);
chudy@google.com830b8792012-08-01 15:57:52 +0000118}
119
120int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000121 SkBitmap bitmap;
reed@google.com9ebcac52014-01-24 18:53:42 +0000122 bitmap.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
chudy@google.com902ebe52012-06-29 14:21:22 +0000123
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000124 SkCanvas canvas(bitmap);
robertphillips@google.com94acc702012-09-06 18:43:21 +0000125 canvas.translate(SkIntToScalar(-x), SkIntToScalar(-y));
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700126 this->applyUserTransform(&canvas);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000127
128 int layer = 0;
chudy@google.com751961d2012-07-31 20:07:42 +0000129 SkColor prev = bitmap.getColor(0,0);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000130 for (int i = 0; i < index; i++) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000131 if (fCommandVector[i]->isVisible()) {
robertphillips70171682014-10-16 14:28:28 -0700132 fCommandVector[i]->setUserMatrix(fUserMatrix);
robertphillips@google.com67baba42013-01-02 20:20:31 +0000133 fCommandVector[i]->execute(&canvas);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000134 }
135 if (prev != bitmap.getColor(0,0)) {
136 layer = i;
137 }
138 prev = bitmap.getColor(0,0);
139 }
140 return layer;
141}
142
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000143class SkDebugClipVisitor : public SkCanvas::ClipVisitor {
144public:
145 SkDebugClipVisitor(SkCanvas* canvas) : fCanvas(canvas) {}
146
mtklein36352bf2015-03-25 18:17:31 -0700147 void clipRect(const SkRect& r, SkRegion::Op, bool doAA) override {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000148 SkPaint p;
149 p.setColor(SK_ColorRED);
150 p.setStyle(SkPaint::kStroke_Style);
151 p.setAntiAlias(doAA);
152 fCanvas->drawRect(r, p);
153 }
mtklein36352bf2015-03-25 18:17:31 -0700154 void clipRRect(const SkRRect& rr, SkRegion::Op, bool doAA) override {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000155 SkPaint p;
156 p.setColor(SK_ColorGREEN);
157 p.setStyle(SkPaint::kStroke_Style);
158 p.setAntiAlias(doAA);
159 fCanvas->drawRRect(rr, p);
160 }
mtklein36352bf2015-03-25 18:17:31 -0700161 void clipPath(const SkPath& path, SkRegion::Op, bool doAA) override {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000162 SkPaint p;
163 p.setColor(SK_ColorBLUE);
164 p.setStyle(SkPaint::kStroke_Style);
165 p.setAntiAlias(doAA);
166 fCanvas->drawPath(path, p);
167 }
168
169protected:
170 SkCanvas* fCanvas;
171
172private:
173 typedef SkCanvas::ClipVisitor INHERITED;
174};
175
176// set up the saveLayer commands so that the active ones
177// return true in their 'active' method
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000178void SkDebugCanvas::markActiveCommands(int index) {
179 fActiveLayers.rewind();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000180
181 for (int i = 0; i < fCommandVector.count(); ++i) {
182 fCommandVector[i]->setActive(false);
183 }
184
185 for (int i = 0; i < index; ++i) {
186 SkDrawCommand::Action result = fCommandVector[i]->action();
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000187 if (SkDrawCommand::kPushLayer_Action == result) {
188 fActiveLayers.push(fCommandVector[i]);
189 } else if (SkDrawCommand::kPopLayer_Action == result) {
190 fActiveLayers.pop();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000191 }
192 }
193
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000194 for (int i = 0; i < fActiveLayers.count(); ++i) {
195 fActiveLayers[i]->setActive(true);
196 }
197
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000198}
199
joshualitt46b301d2016-03-02 08:32:37 -0800200void SkDebugCanvas::drawTo(SkCanvas* canvas, int index, int m) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000201 SkASSERT(!fCommandVector.isEmpty());
202 SkASSERT(index < fCommandVector.count());
kkinnunen26a00de2015-01-13 23:09:19 -0800203
204 int saveCount = canvas->save();
205
kkinnunen26e54002015-01-05 12:58:56 -0800206 SkRect windowRect = SkRect::MakeWH(SkIntToScalar(canvas->getBaseLayerSize().width()),
207 SkIntToScalar(canvas->getBaseLayerSize().height()));
chudy@google.com830b8792012-08-01 15:57:52 +0000208
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000209 bool pathOpsMode = getAllowSimplifyClip();
210 canvas->setAllowSimplifyClip(pathOpsMode);
ethannicholascecbbe22016-02-17 11:49:43 -0800211 canvas->clear(SK_ColorWHITE);
kkinnunen26a00de2015-01-13 23:09:19 -0800212 canvas->resetMatrix();
213 if (!windowRect.isEmpty()) {
214 canvas->clipRect(windowRect, SkRegion::kReplace_Op);
commit-bot@chromium.orga27622c2013-08-05 16:31:27 +0000215 }
kkinnunen26a00de2015-01-13 23:09:19 -0800216 this->applyUserTransform(canvas);
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000217
fmalita65cdb572015-03-26 07:24:48 -0700218 if (fPaintFilterCanvas) {
219 fPaintFilterCanvas->addCanvas(canvas);
220 canvas = fPaintFilterCanvas.get();
halcanary9d524f22016-03-29 09:03:52 -0700221
chudy@google.com830b8792012-08-01 15:57:52 +0000222 }
223
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000224 if (fMegaVizMode) {
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000225 this->markActiveCommands(index);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000226 }
halcanary9d524f22016-03-29 09:03:52 -0700227
joshualitt40836102016-03-11 11:45:53 -0800228#if SK_SUPPORT_GPU
joshualitt10d8fc22016-02-29 11:15:06 -0800229 // If we have a GPU backend we can also visualize the batching information
joshualitt10d8fc22016-02-29 11:15:06 -0800230 GrAuditTrail* at = nullptr;
joshualittae47aee2016-03-10 13:29:36 -0800231 if (fDrawGpuBatchBounds || m != -1) {
232 at = this->getAuditTrail(canvas);
joshualitt10d8fc22016-02-29 11:15:06 -0800233 }
joshualitt40836102016-03-11 11:45:53 -0800234#endif
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000235
kkinnunen26a00de2015-01-13 23:09:19 -0800236 for (int i = 0; i <= index; i++) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000237 if (i == index && fFilter) {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700238 canvas->clear(0xAAFFFFFF);
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000239 }
halcanary9d524f22016-03-29 09:03:52 -0700240
joshualitt10d8fc22016-02-29 11:15:06 -0800241#if SK_SUPPORT_GPU
242 GrAuditTrail::AutoCollectBatches* acb = nullptr;
243 if (at) {
244 acb = new GrAuditTrail::AutoCollectBatches(at, i);
245 }
246#endif
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000247
robertphillips@google.com67baba42013-01-02 20:20:31 +0000248 if (fCommandVector[i]->isVisible()) {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000249 if (fMegaVizMode && fCommandVector[i]->active()) {
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000250 // "active" commands execute their visualization behaviors:
251 // All active saveLayers get replaced with saves so all draws go to the
252 // visible canvas.
253 // All active culls draw their cull box
254 fCommandVector[i]->vizExecute(canvas);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000255 } else {
robertphillips70171682014-10-16 14:28:28 -0700256 fCommandVector[i]->setUserMatrix(fUserMatrix);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000257 fCommandVector[i]->execute(canvas);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000258 }
chudy@google.com902ebe52012-06-29 14:21:22 +0000259 }
joshualitt10d8fc22016-02-29 11:15:06 -0800260#if SK_SUPPORT_GPU
261 if (at && acb) {
262 delete acb;
263 }
264#endif
chudy@google.com902ebe52012-06-29 14:21:22 +0000265 }
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000266
ethannicholas0a0520a2016-02-12 12:06:53 -0800267 if (SkColorGetA(fClipVizColor) != 0) {
268 canvas->save();
269 #define LARGE_COORD 1000000000
270 canvas->clipRect(SkRect::MakeLTRB(-LARGE_COORD, -LARGE_COORD, LARGE_COORD, LARGE_COORD),
271 SkRegion::kReverseDifference_Op);
272 SkPaint clipPaint;
273 clipPaint.setColor(fClipVizColor);
274 canvas->drawPaint(clipPaint);
275 canvas->restore();
276 }
277
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000278 if (fMegaVizMode) {
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000279 canvas->save();
280 // nuke the CTM
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700281 canvas->resetMatrix();
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000282 // turn off clipping
kkinnunen26e54002015-01-05 12:58:56 -0800283 if (!windowRect.isEmpty()) {
284 SkRect r = windowRect;
285 r.outset(SK_Scalar1, SK_Scalar1);
286 canvas->clipRect(r, SkRegion::kReplace_Op);
287 }
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000288 // visualize existing clips
289 SkDebugClipVisitor visitor(canvas);
290
291 canvas->replayClips(&visitor);
292
293 canvas->restore();
294 }
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000295 if (pathOpsMode) {
296 this->resetClipStackData();
297 const SkClipStack* clipStack = canvas->getClipStack();
298 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
299 const SkClipStack::Element* element;
300 SkPath devPath;
301 while ((element = iter.next())) {
302 SkClipStack::Element::Type type = element->getType();
303 SkPath operand;
304 if (type != SkClipStack::Element::kEmpty_Type) {
305 element->asPath(&operand);
306 }
307 SkRegion::Op elementOp = element->getOp();
308 this->addClipStackData(devPath, operand, elementOp);
309 if (elementOp == SkRegion::kReplace_Op) {
310 devPath = operand;
311 } else {
312 Op(devPath, operand, (SkPathOp) elementOp, &devPath);
313 }
314 }
315 this->lastClipStackData(devPath);
316 }
chudy@google.coma9e937c2012-08-03 17:32:05 +0000317 fMatrix = canvas->getTotalMatrix();
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000318 if (!canvas->getClipDeviceBounds(&fClip)) {
319 fClip.setEmpty();
320 }
kkinnunen26a00de2015-01-13 23:09:19 -0800321
322 canvas->restoreToCount(saveCount);
fmalita65cdb572015-03-26 07:24:48 -0700323
324 if (fPaintFilterCanvas) {
325 fPaintFilterCanvas->removeAll();
326 }
joshualitt10d8fc22016-02-29 11:15:06 -0800327
328#if SK_SUPPORT_GPU
329 // draw any batches if required and issue a full reset onto GrAuditTrail
330 if (at) {
joshualitte43f7e62016-03-04 10:45:05 -0800331 // just in case there is global reordering, we flush the canvas before querying
332 // GrAuditTrail
joshualittb0666ad2016-03-08 10:43:41 -0800333 GrAuditTrail::AutoEnable ae(at);
joshualitte43f7e62016-03-04 10:45:05 -0800334 canvas->flush();
335
joshualittbdc6b2b2016-03-01 14:22:02 -0800336 // we pick three colorblind-safe colors, 75% alpha
337 static const SkColor kTotalBounds = SkColorSetARGB(0xC0, 0x6A, 0x3D, 0x9A);
338 static const SkColor kOpBatchBounds = SkColorSetARGB(0xC0, 0xE3, 0x1A, 0x1C);
339 static const SkColor kOtherBatchBounds = SkColorSetARGB(0xC0, 0xFF, 0x7F, 0x00);
340
joshualitt1d7decf2016-03-01 07:15:52 -0800341 // get the render target of the top device so we can ignore batches drawn offscreen
342 SkBaseDevice* bd = canvas->getDevice_just_for_deprecated_compatibility_testing();
343 SkGpuDevice* gbd = reinterpret_cast<SkGpuDevice*>(bd);
344 uint32_t rtID = gbd->accessRenderTarget()->getUniqueID();
345
346 // get the bounding boxes to draw
joshualitt10d8fc22016-02-29 11:15:06 -0800347 SkTArray<GrAuditTrail::BatchInfo> childrenBounds;
joshualitt46b301d2016-03-02 08:32:37 -0800348 if (m == -1) {
349 at->getBoundsByClientID(&childrenBounds, index);
350 } else {
351 // the client wants us to draw the mth batch
352 at->getBoundsByBatchListID(&childrenBounds.push_back(), m);
353 }
joshualitt10d8fc22016-02-29 11:15:06 -0800354 SkPaint paint;
355 paint.setStyle(SkPaint::kStroke_Style);
356 paint.setStrokeWidth(1);
357 for (int i = 0; i < childrenBounds.count(); i++) {
joshualitt1d7decf2016-03-01 07:15:52 -0800358 if (childrenBounds[i].fRenderTargetUniqueID != rtID) {
359 // offscreen draw, ignore for now
360 continue;
361 }
joshualittbdc6b2b2016-03-01 14:22:02 -0800362 paint.setColor(kTotalBounds);
joshualitt10d8fc22016-02-29 11:15:06 -0800363 canvas->drawRect(childrenBounds[i].fBounds, paint);
364 for (int j = 0; j < childrenBounds[i].fBatches.count(); j++) {
365 const GrAuditTrail::BatchInfo::Batch& batch = childrenBounds[i].fBatches[j];
366 if (batch.fClientID != index) {
joshualittbdc6b2b2016-03-01 14:22:02 -0800367 paint.setColor(kOtherBatchBounds);
joshualitt10d8fc22016-02-29 11:15:06 -0800368 } else {
joshualittbdc6b2b2016-03-01 14:22:02 -0800369 paint.setColor(kOpBatchBounds);
joshualitt10d8fc22016-02-29 11:15:06 -0800370 }
371 canvas->drawRect(batch.fBounds, paint);
372 }
373 }
joshualitt10d8fc22016-02-29 11:15:06 -0800374 }
joshualitt10d8fc22016-02-29 11:15:06 -0800375#endif
joshualittae47aee2016-03-10 13:29:36 -0800376 this->cleanupAuditTrail(canvas);
chudy@google.com902ebe52012-06-29 14:21:22 +0000377}
378
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000379void SkDebugCanvas::deleteDrawCommandAt(int index) {
380 SkASSERT(index < fCommandVector.count());
381 delete fCommandVector[index];
382 fCommandVector.remove(index);
383}
384
chudy@google.com902ebe52012-06-29 14:21:22 +0000385SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000386 SkASSERT(index < fCommandVector.count());
387 return fCommandVector[index];
chudy@google.com902ebe52012-06-29 14:21:22 +0000388}
389
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000390void SkDebugCanvas::setDrawCommandAt(int index, SkDrawCommand* command) {
391 SkASSERT(index < fCommandVector.count());
392 delete fCommandVector[index];
393 fCommandVector[index] = command;
394}
395
fmalita8c89c522014-11-08 16:18:56 -0800396const SkTDArray<SkString*>* SkDebugCanvas::getCommandInfo(int index) const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000397 SkASSERT(index < fCommandVector.count());
398 return fCommandVector[index]->Info();
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000399}
chudy@google.com902ebe52012-06-29 14:21:22 +0000400
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000401bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000402 SkASSERT(index < fCommandVector.count());
403 return fCommandVector[index]->isVisible();
chudy@google.com902ebe52012-06-29 14:21:22 +0000404}
405
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000406const SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000407 return fCommandVector;
chudy@google.com902ebe52012-06-29 14:21:22 +0000408}
409
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000410SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() {
411 return fCommandVector;
412}
413
joshualittae47aee2016-03-10 13:29:36 -0800414GrAuditTrail* SkDebugCanvas::getAuditTrail(SkCanvas* canvas) {
415 GrAuditTrail* at = nullptr;
joshualitte43f7e62016-03-04 10:45:05 -0800416#if SK_SUPPORT_GPU
417 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
joshualitte43f7e62016-03-04 10:45:05 -0800418 if (rt) {
419 GrContext* ctx = rt->getContext();
joshualittae47aee2016-03-10 13:29:36 -0800420 if (ctx) {
joshualitte43f7e62016-03-04 10:45:05 -0800421 at = ctx->getAuditTrail();
joshualitte43f7e62016-03-04 10:45:05 -0800422 }
423 }
424#endif
joshualittae47aee2016-03-10 13:29:36 -0800425 return at;
426}
427
428void SkDebugCanvas::drawAndCollectBatches(int n, SkCanvas* canvas) {
joshualitt40836102016-03-11 11:45:53 -0800429#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800430 GrAuditTrail* at = this->getAuditTrail(canvas);
431 if (at) {
joshualittae47aee2016-03-10 13:29:36 -0800432 // loop over all of the commands and draw them, this is to collect reordering
433 // information
434 for (int i = 0; i < this->getSize() && i <= n; i++) {
435 GrAuditTrail::AutoCollectBatches enable(at, i);
436 fCommandVector[i]->execute(canvas);
437 }
438
439 // in case there is some kind of global reordering
440 {
441 GrAuditTrail::AutoEnable ae(at);
442 canvas->flush();
443 }
joshualittae47aee2016-03-10 13:29:36 -0800444 }
joshualitt40836102016-03-11 11:45:53 -0800445#endif
joshualittae47aee2016-03-10 13:29:36 -0800446}
447
448void SkDebugCanvas::cleanupAuditTrail(SkCanvas* canvas) {
449 GrAuditTrail* at = this->getAuditTrail(canvas);
450 if (at) {
451#if SK_SUPPORT_GPU
452 GrAuditTrail::AutoEnable ae(at);
453 at->fullReset();
454#endif
455 }
456}
457
458Json::Value SkDebugCanvas::toJSON(UrlDataManager& urlDataManager, int n, SkCanvas* canvas) {
459 this->drawAndCollectBatches(n, canvas);
halcanary9d524f22016-03-29 09:03:52 -0700460
joshualitte43f7e62016-03-04 10:45:05 -0800461 // now collect json
joshualitt40836102016-03-11 11:45:53 -0800462#if SK_SUPPORT_GPU
joshualittae47aee2016-03-10 13:29:36 -0800463 GrAuditTrail* at = this->getAuditTrail(canvas);
joshualitt40836102016-03-11 11:45:53 -0800464#endif
ethannicholas402cd912016-02-10 12:57:30 -0800465 Json::Value result = Json::Value(Json::objectValue);
466 result[SKDEBUGCANVAS_ATTRIBUTE_VERSION] = Json::Value(SKDEBUGCANVAS_VERSION);
467 Json::Value commands = Json::Value(Json::arrayValue);
ethannicholas0a0520a2016-02-12 12:06:53 -0800468 for (int i = 0; i < this->getSize() && i <= n; i++) {
joshualitte43f7e62016-03-04 10:45:05 -0800469 commands[i] = this->getDrawCommandAt(i)->toJSON(urlDataManager);
470#if SK_SUPPORT_GPU
471 if (at) {
472 // TODO if this is inefficient we could add a method to GrAuditTrail which takes
473 // a Json::Value and is only compiled in this file
474 Json::Value parsedFromString;
475 Json::Reader reader;
476 SkAssertResult(reader.parse(at->toJson(i).c_str(), parsedFromString));
477
478 commands[i][SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL] = parsedFromString;
479 }
480#endif
ethannicholas402cd912016-02-10 12:57:30 -0800481 }
joshualittae47aee2016-03-10 13:29:36 -0800482 this->cleanupAuditTrail(canvas);
ethannicholas402cd912016-02-10 12:57:30 -0800483 result[SKDEBUGCANVAS_ATTRIBUTE_COMMANDS] = commands;
484 return result;
485}
486
joshualittae47aee2016-03-10 13:29:36 -0800487Json::Value SkDebugCanvas::toJSONBatchList(int n, SkCanvas* canvas) {
488 this->drawAndCollectBatches(n, canvas);
489
490 Json::Value parsedFromString;
joshualittae47aee2016-03-10 13:29:36 -0800491#if SK_SUPPORT_GPU
joshualitt40836102016-03-11 11:45:53 -0800492 GrAuditTrail* at = this->getAuditTrail(canvas);
joshualittae47aee2016-03-10 13:29:36 -0800493 if (at) {
494 GrAuditTrail::AutoManageBatchList enable(at);
495 Json::Reader reader;
496 SkAssertResult(reader.parse(at->toJson().c_str(), parsedFromString));
497 }
498#endif
499 this->cleanupAuditTrail(canvas);
500 return parsedFromString;
501}
502
fmalita65cdb572015-03-26 07:24:48 -0700503void SkDebugCanvas::updatePaintFilterCanvas() {
504 if (!fOverdrawViz && !fOverrideFilterQuality) {
halcanary96fcdcc2015-08-27 07:41:13 -0700505 fPaintFilterCanvas.reset(nullptr);
fmalita65cdb572015-03-26 07:24:48 -0700506 return;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000507 }
508
fmalita65cdb572015-03-26 07:24:48 -0700509 const SkImageInfo info = this->imageInfo();
halcanary385fe4d2015-08-26 13:07:48 -0700510 fPaintFilterCanvas.reset(new DebugPaintFilterCanvas(info.width(), info.height(), fOverdrawViz,
511 fOverrideFilterQuality, fFilterQuality));
fmalita65cdb572015-03-26 07:24:48 -0700512}
513
514void SkDebugCanvas::setOverdrawViz(bool overdrawViz) {
515 if (fOverdrawViz == overdrawViz) {
516 return;
517 }
518
519 fOverdrawViz = overdrawViz;
520 this->updatePaintFilterCanvas();
521}
522
523void SkDebugCanvas::overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality quality) {
524 if (fOverrideFilterQuality == overrideTexFiltering && fFilterQuality == quality) {
525 return;
526 }
527
528 fOverrideFilterQuality = overrideTexFiltering;
529 fFilterQuality = quality;
530 this->updatePaintFilterCanvas();
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000531}
532
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000533void SkDebugCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
534 this->addDrawCommand(new SkClipPathCommand(path, op, kSoft_ClipEdgeStyle == edgeStyle));
chudy@google.com902ebe52012-06-29 14:21:22 +0000535}
536
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000537void SkDebugCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
538 this->addDrawCommand(new SkClipRectCommand(rect, op, kSoft_ClipEdgeStyle == edgeStyle));
chudy@google.com902ebe52012-06-29 14:21:22 +0000539}
540
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000541void SkDebugCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
542 this->addDrawCommand(new SkClipRRectCommand(rrect, op, kSoft_ClipEdgeStyle == edgeStyle));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000543}
544
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000545void SkDebugCanvas::onClipRegion(const SkRegion& region, SkRegion::Op op) {
546 this->addDrawCommand(new SkClipRegionCommand(region, op));
chudy@google.com902ebe52012-06-29 14:21:22 +0000547}
548
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000549void SkDebugCanvas::didConcat(const SkMatrix& matrix) {
robertphillips9bafc302015-02-13 11:13:00 -0800550 this->addDrawCommand(new SkConcatCommand(matrix));
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000551 this->INHERITED::didConcat(matrix);
chudy@google.com902ebe52012-06-29 14:21:22 +0000552}
553
reed41af9662015-01-05 07:49:08 -0800554void SkDebugCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar left,
555 SkScalar top, const SkPaint* paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000556 this->addDrawCommand(new SkDrawBitmapCommand(bitmap, left, top, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000557}
558
reed41af9662015-01-05 07:49:08 -0800559void SkDebugCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700560 const SkPaint* paint, SrcRectConstraint constraint) {
reeda5517e22015-07-14 10:54:12 -0700561 this->addDrawCommand(new SkDrawBitmapRectCommand(bitmap, src, dst, paint,
562 (SrcRectConstraint)constraint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000563}
564
reed41af9662015-01-05 07:49:08 -0800565void SkDebugCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
566 const SkRect& dst, const SkPaint* paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000567 this->addDrawCommand(new SkDrawBitmapNineCommand(bitmap, center, dst, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000568}
569
reed41af9662015-01-05 07:49:08 -0800570void SkDebugCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
571 const SkPaint* paint) {
fmalita651c9202015-07-22 10:23:01 -0700572 this->addDrawCommand(new SkDrawImageCommand(image, left, top, paint));
reed41af9662015-01-05 07:49:08 -0800573}
574
575void SkDebugCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700576 const SkPaint* paint, SrcRectConstraint constraint) {
fmalita651c9202015-07-22 10:23:01 -0700577 this->addDrawCommand(new SkDrawImageRectCommand(image, src, dst, paint, constraint));
reed41af9662015-01-05 07:49:08 -0800578}
579
reed41af9662015-01-05 07:49:08 -0800580void SkDebugCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000581 this->addDrawCommand(new SkDrawOvalCommand(oval, paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000582}
583
reed41af9662015-01-05 07:49:08 -0800584void SkDebugCanvas::onDrawPaint(const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000585 this->addDrawCommand(new SkDrawPaintCommand(paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000586}
587
reed41af9662015-01-05 07:49:08 -0800588void SkDebugCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000589 this->addDrawCommand(new SkDrawPathCommand(path, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000590}
591
mtkleinf0f14112014-12-12 08:46:25 -0800592void SkDebugCanvas::onDrawPicture(const SkPicture* picture,
593 const SkMatrix* matrix,
robertphillipsb3f319f2014-08-13 10:46:23 -0700594 const SkPaint* paint) {
fmalita160ebb22015-04-01 20:58:37 -0700595 this->addDrawCommand(new SkBeginDrawPictureCommand(picture, matrix, paint));
ethannicholas891ad662016-02-12 07:15:45 -0800596 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
597 picture->playback(this);
fmalita160ebb22015-04-01 20:58:37 -0700598 this->addDrawCommand(new SkEndDrawPictureCommand(SkToBool(matrix) || SkToBool(paint)));
chudy@google.com902ebe52012-06-29 14:21:22 +0000599}
600
reed41af9662015-01-05 07:49:08 -0800601void SkDebugCanvas::onDrawPoints(PointMode mode, size_t count,
602 const SkPoint pts[], const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000603 this->addDrawCommand(new SkDrawPointsCommand(mode, count, pts, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000604}
605
reed@google.come0d9ce82014-04-23 04:00:17 +0000606void SkDebugCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
607 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000608 this->addDrawCommand(new SkDrawPosTextCommand(text, byteLength, pos, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000609}
610
reed@google.come0d9ce82014-04-23 04:00:17 +0000611void SkDebugCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
612 SkScalar constY, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000613 this->addDrawCommand(
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000614 new SkDrawPosTextHCommand(text, byteLength, xpos, constY, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000615}
616
reed41af9662015-01-05 07:49:08 -0800617void SkDebugCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
chudy@google.com902ebe52012-06-29 14:21:22 +0000618 // NOTE(chudy): Messing up when renamed to DrawRect... Why?
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000619 addDrawCommand(new SkDrawRectCommand(rect, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000620}
621
reed41af9662015-01-05 07:49:08 -0800622void SkDebugCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000623 this->addDrawCommand(new SkDrawRRectCommand(rrect, paint));
robertphillips@google.com67baba42013-01-02 20:20:31 +0000624}
625
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000626void SkDebugCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
627 const SkPaint& paint) {
commit-bot@chromium.org3d305202014-02-24 17:28:55 +0000628 this->addDrawCommand(new SkDrawDRRectCommand(outer, inner, paint));
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000629}
630
reed@google.come0d9ce82014-04-23 04:00:17 +0000631void SkDebugCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
632 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000633 this->addDrawCommand(new SkDrawTextCommand(text, byteLength, x, y, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000634}
635
reed@google.come0d9ce82014-04-23 04:00:17 +0000636void SkDebugCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
637 const SkMatrix* matrix, const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000638 this->addDrawCommand(
commit-bot@chromium.org7a115912013-06-18 20:20:55 +0000639 new SkDrawTextOnPathCommand(text, byteLength, path, matrix, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000640}
641
fmalitab7425172014-08-26 07:56:44 -0700642void SkDebugCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
643 const SkPaint& paint) {
644 this->addDrawCommand(new SkDrawTextBlobCommand(blob, x, y, paint));
645}
646
robertphillips9bafc302015-02-13 11:13:00 -0800647void SkDebugCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
648 const SkPoint texCoords[4], SkXfermode* xmode,
649 const SkPaint& paint) {
650 this->addDrawCommand(new SkDrawPatchCommand(cubics, colors, texCoords, xmode, paint));
651}
652
reed41af9662015-01-05 07:49:08 -0800653void SkDebugCanvas::onDrawVertices(VertexMode vmode, int vertexCount, const SkPoint vertices[],
654 const SkPoint texs[], const SkColor colors[],
655 SkXfermode*, const uint16_t indices[], int indexCount,
656 const SkPaint& paint) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000657 this->addDrawCommand(new SkDrawVerticesCommand(vmode, vertexCount, vertices,
halcanary96fcdcc2015-08-27 07:41:13 -0700658 texs, colors, nullptr, indices, indexCount, paint));
chudy@google.com902ebe52012-06-29 14:21:22 +0000659}
660
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000661void SkDebugCanvas::willRestore() {
662 this->addDrawCommand(new SkRestoreCommand());
663 this->INHERITED::willRestore();
chudy@google.com902ebe52012-06-29 14:21:22 +0000664}
665
Florin Malita5f6102d2014-06-30 10:13:28 -0400666void SkDebugCanvas::willSave() {
667 this->addDrawCommand(new SkSaveCommand());
668 this->INHERITED::willSave();
chudy@google.com902ebe52012-06-29 14:21:22 +0000669}
670
reed4960eee2015-12-18 07:09:18 -0800671SkCanvas::SaveLayerStrategy SkDebugCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
672 this->addDrawCommand(new SkSaveLayerCommand(rec));
673 (void)this->INHERITED::getSaveLayerStrategy(rec);
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000674 // No need for a full layer.
675 return kNoLayer_SaveLayerStrategy;
chudy@google.com902ebe52012-06-29 14:21:22 +0000676}
677
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000678void SkDebugCanvas::didSetMatrix(const SkMatrix& matrix) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000679 this->addDrawCommand(new SkSetMatrixCommand(matrix));
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000680 this->INHERITED::didSetMatrix(matrix);
chudy@google.com902ebe52012-06-29 14:21:22 +0000681}
682
chudy@google.com902ebe52012-06-29 14:21:22 +0000683void SkDebugCanvas::toggleCommand(int index, bool toggle) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000684 SkASSERT(index < fCommandVector.count());
685 fCommandVector[index]->setVisible(toggle);
chudy@google.com902ebe52012-06-29 14:21:22 +0000686}
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000687
688static const char* gFillTypeStrs[] = {
689 "kWinding_FillType",
690 "kEvenOdd_FillType",
691 "kInverseWinding_FillType",
692 "kInverseEvenOdd_FillType"
693};
694
695static const char* gOpStrs[] = {
scroggo5965b732015-04-07 06:53:21 -0700696 "kDifference_PathOp",
697 "kIntersect_PathOp",
698 "kUnion_PathOp",
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000699 "kXor_PathOp",
scroggo5965b732015-04-07 06:53:21 -0700700 "kReverseDifference_PathOp",
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000701};
702
703static const char kHTML4SpaceIndent[] = "&nbsp;&nbsp;&nbsp;&nbsp;";
704
705void SkDebugCanvas::outputScalar(SkScalar num) {
706 if (num == (int) num) {
707 fClipStackData.appendf("%d", (int) num);
708 } else {
709 SkString str;
710 str.printf("%1.9g", num);
711 int width = (int) str.size();
712 const char* cStr = str.c_str();
713 while (cStr[width - 1] == '0') {
714 --width;
715 }
716 str.resize(width);
717 fClipStackData.appendf("%sf", str.c_str());
718 }
719}
720
721void SkDebugCanvas::outputPointsCommon(const SkPoint* pts, int count) {
722 for (int index = 0; index < count; ++index) {
723 this->outputScalar(pts[index].fX);
724 fClipStackData.appendf(", ");
725 this->outputScalar(pts[index].fY);
726 if (index + 1 < count) {
727 fClipStackData.appendf(", ");
728 }
729 }
730}
731
732void SkDebugCanvas::outputPoints(const SkPoint* pts, int count) {
733 this->outputPointsCommon(pts, count);
734 fClipStackData.appendf(");<br>");
735}
736
737void SkDebugCanvas::outputConicPoints(const SkPoint* pts, SkScalar weight) {
738 this->outputPointsCommon(pts, 2);
739 fClipStackData.appendf(", ");
740 this->outputScalar(weight);
741 fClipStackData.appendf(");<br>");
742}
743
744void SkDebugCanvas::addPathData(const SkPath& path, const char* pathName) {
745 SkPath::RawIter iter(path);
746 SkPath::FillType fillType = path.getFillType();
747 fClipStackData.appendf("%sSkPath %s;<br>", kHTML4SpaceIndent, pathName);
748 fClipStackData.appendf("%s%s.setFillType(SkPath::%s);<br>", kHTML4SpaceIndent, pathName,
749 gFillTypeStrs[fillType]);
750 iter.setPath(path);
751 uint8_t verb;
752 SkPoint pts[4];
753 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
754 switch (verb) {
755 case SkPath::kMove_Verb:
756 fClipStackData.appendf("%s%s.moveTo(", kHTML4SpaceIndent, pathName);
757 this->outputPoints(&pts[0], 1);
758 continue;
759 case SkPath::kLine_Verb:
760 fClipStackData.appendf("%s%s.lineTo(", kHTML4SpaceIndent, pathName);
761 this->outputPoints(&pts[1], 1);
762 break;
763 case SkPath::kQuad_Verb:
764 fClipStackData.appendf("%s%s.quadTo(", kHTML4SpaceIndent, pathName);
765 this->outputPoints(&pts[1], 2);
766 break;
767 case SkPath::kConic_Verb:
768 fClipStackData.appendf("%s%s.conicTo(", kHTML4SpaceIndent, pathName);
769 this->outputConicPoints(&pts[1], iter.conicWeight());
770 break;
771 case SkPath::kCubic_Verb:
772 fClipStackData.appendf("%s%s.cubicTo(", kHTML4SpaceIndent, pathName);
773 this->outputPoints(&pts[1], 3);
774 break;
775 case SkPath::kClose_Verb:
776 fClipStackData.appendf("%s%s.close();<br>", kHTML4SpaceIndent, pathName);
777 break;
778 default:
779 SkDEBUGFAIL("bad verb");
780 return;
781 }
782 }
783}
784
785void SkDebugCanvas::addClipStackData(const SkPath& devPath, const SkPath& operand,
786 SkRegion::Op elementOp) {
787 if (elementOp == SkRegion::kReplace_Op) {
788 if (!lastClipStackData(devPath)) {
789 fSaveDevPath = operand;
790 }
791 fCalledAddStackData = false;
792 } else {
793 fClipStackData.appendf("<br>static void test(skiatest::Reporter* reporter,"
794 " const char* filename) {<br>");
795 addPathData(fCalledAddStackData ? devPath : fSaveDevPath, "path");
796 addPathData(operand, "pathB");
797 fClipStackData.appendf("%stestPathOp(reporter, path, pathB, %s, filename);<br>",
798 kHTML4SpaceIndent, gOpStrs[elementOp]);
799 fClipStackData.appendf("}<br>");
800 fCalledAddStackData = true;
801 }
802}
803
804bool SkDebugCanvas::lastClipStackData(const SkPath& devPath) {
805 if (fCalledAddStackData) {
806 fClipStackData.appendf("<br>");
807 addPathData(devPath, "pathOut");
808 return true;
809 }
810 return false;
811}