• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "SkCanvasPriv.h"
9 #include "SkClipStack.h"
10 #include "SkDebugCanvas.h"
11 #include "SkDrawCommand.h"
12 #include "SkPaintFilterCanvas.h"
13 #include "SkRectPriv.h"
14 #include "SkTextBlob.h"
15 #include "SkClipOpPriv.h"
16 
17 #if SK_SUPPORT_GPU
18 #include "GrAuditTrail.h"
19 #include "GrContext.h"
20 #include "GrRenderTargetContext.h"
21 #endif
22 
23 #define SKDEBUGCANVAS_VERSION                     1
24 #define SKDEBUGCANVAS_ATTRIBUTE_VERSION           "version"
25 #define SKDEBUGCANVAS_ATTRIBUTE_COMMANDS          "commands"
26 #define SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL        "auditTrail"
27 
28 class DebugPaintFilterCanvas : public SkPaintFilterCanvas {
29 public:
DebugPaintFilterCanvas(SkCanvas * canvas,bool overdrawViz,bool overrideFilterQuality,SkFilterQuality quality)30     DebugPaintFilterCanvas(SkCanvas* canvas,
31                            bool overdrawViz,
32                            bool overrideFilterQuality,
33                            SkFilterQuality quality)
34         : INHERITED(canvas)
35         , fOverdrawViz(overdrawViz)
36         , fOverrideFilterQuality(overrideFilterQuality)
37         , fFilterQuality(quality) {}
38 
39 protected:
onFilter(SkTCopyOnFirstWrite<SkPaint> * paint,Type) const40     bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
41         if (*paint) {
42             if (fOverdrawViz) {
43                 paint->writable()->setColor(SK_ColorRED);
44                 paint->writable()->setAlpha(0x08);
45                 paint->writable()->setBlendMode(SkBlendMode::kSrcOver);
46             }
47 
48             if (fOverrideFilterQuality) {
49                 paint->writable()->setFilterQuality(fFilterQuality);
50             }
51         }
52         return true;
53     }
54 
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)55     void onDrawPicture(const SkPicture* picture,
56                        const SkMatrix* matrix,
57                        const SkPaint* paint) override {
58         // 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 
62 private:
63     bool fOverdrawViz;
64     bool fOverrideFilterQuality;
65     SkFilterQuality fFilterQuality;
66 
67     typedef SkPaintFilterCanvas INHERITED;
68 };
69 
SkDebugCanvas(int width,int height)70 SkDebugCanvas::SkDebugCanvas(int width, int height)
71         : INHERITED(width, height)
72         , fPicture(nullptr)
73         , fFilter(false)
74         , fMegaVizMode(false)
75         , fOverdrawViz(false)
76         , fOverrideFilterQuality(false)
77         , fFilterQuality(kNone_SkFilterQuality)
78         , fClipVizColor(SK_ColorTRANSPARENT)
79         , fDrawGpuOpBounds(false) {
80     fUserMatrix.reset();
81 
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).
91     SkIRect largeIRect = SkRectPriv::MakeILarge();
92     largeIRect.inset(1024, 1024);
93     SkRect large = SkRect::Make(largeIRect);
94 #ifdef SK_DEBUG
95     SkASSERT(!large.roundOut().isEmpty());
96 #endif
97     // call the base class' version to avoid adding a draw command
98     this->INHERITED::onClipRect(large, kReplace_SkClipOp, kHard_ClipEdgeStyle);
99 }
100 
~SkDebugCanvas()101 SkDebugCanvas::~SkDebugCanvas() {
102     fCommandVector.deleteAll();
103 }
104 
addDrawCommand(SkDrawCommand * command)105 void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) {
106     fCommandVector.push(command);
107 }
108 
draw(SkCanvas * canvas)109 void SkDebugCanvas::draw(SkCanvas* canvas) {
110     if (!fCommandVector.isEmpty()) {
111         this->drawTo(canvas, fCommandVector.count() - 1);
112     }
113 }
114 
applyUserTransform(SkCanvas * canvas)115 void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) {
116     canvas->concat(fUserMatrix);
117 }
118 
getCommandAtPoint(int x,int y,int index)119 int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) {
120     SkBitmap bitmap;
121     bitmap.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
122 
123     SkCanvas canvas(bitmap);
124     canvas.translate(SkIntToScalar(-x), SkIntToScalar(-y));
125     this->applyUserTransform(&canvas);
126 
127     int layer = 0;
128     SkColor prev = bitmap.getColor(0,0);
129     for (int i = 0; i < index; i++) {
130         if (fCommandVector[i]->isVisible()) {
131             fCommandVector[i]->setUserMatrix(fUserMatrix);
132             fCommandVector[i]->execute(&canvas);
133         }
134         if (prev != bitmap.getColor(0,0)) {
135             layer = i;
136         }
137         prev = bitmap.getColor(0,0);
138     }
139     return layer;
140 }
141 
142 // set up the saveLayer commands so that the active ones
143 // return true in their 'active' method
markActiveCommands(int index)144 void SkDebugCanvas::markActiveCommands(int index) {
145     fActiveLayers.rewind();
146 
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();
153         if (SkDrawCommand::kPushLayer_Action == result) {
154             fActiveLayers.push(fCommandVector[i]);
155         } else if (SkDrawCommand::kPopLayer_Action == result) {
156             fActiveLayers.pop();
157         }
158     }
159 
160     for (int i = 0; i < fActiveLayers.count(); ++i) {
161         fActiveLayers[i]->setActive(true);
162     }
163 
164 }
165 
drawTo(SkCanvas * originalCanvas,int index,int m)166 void SkDebugCanvas::drawTo(SkCanvas* originalCanvas, int index, int m) {
167     SkASSERT(!fCommandVector.isEmpty());
168     SkASSERT(index < fCommandVector.count());
169 
170     int saveCount = originalCanvas->save();
171 
172     SkRect windowRect = SkRect::MakeWH(SkIntToScalar(originalCanvas->getBaseLayerSize().width()),
173                                        SkIntToScalar(originalCanvas->getBaseLayerSize().height()));
174 
175     bool pathOpsMode = getAllowSimplifyClip();
176     originalCanvas->setAllowSimplifyClip(pathOpsMode);
177     originalCanvas->clear(SK_ColorWHITE);
178     originalCanvas->resetMatrix();
179     if (!windowRect.isEmpty()) {
180         originalCanvas->clipRect(windowRect, kReplace_SkClipOp);
181     }
182     this->applyUserTransform(originalCanvas);
183 
184     DebugPaintFilterCanvas filterCanvas(originalCanvas, fOverdrawViz, fOverrideFilterQuality,
185                                         fFilterQuality);
186 
187     if (fMegaVizMode) {
188         this->markActiveCommands(index);
189     }
190 
191 #if SK_SUPPORT_GPU
192     // If we have a GPU backend we can also visualize the op information
193     GrAuditTrail* at = nullptr;
194     if (fDrawGpuOpBounds || m != -1) {
195         // The audit trail must be obtained from the original canvas.
196         at = this->getAuditTrail(originalCanvas);
197     }
198 #endif
199 
200     for (int i = 0; i <= index; i++) {
201         if (i == index && fFilter) {
202             filterCanvas.clear(0xAAFFFFFF);
203         }
204 
205 #if SK_SUPPORT_GPU
206         // We need to flush any pending operations, or they might combine with commands below.
207         // 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.
209         filterCanvas.flush();
210 
211         GrAuditTrail::AutoCollectOps* acb = nullptr;
212         if (at) {
213             acb = new GrAuditTrail::AutoCollectOps(at, i);
214         }
215 #endif
216 
217         if (fCommandVector[i]->isVisible()) {
218             if (fMegaVizMode && fCommandVector[i]->active()) {
219                 // "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
223                 fCommandVector[i]->vizExecute(&filterCanvas);
224             } else {
225                 fCommandVector[i]->setUserMatrix(fUserMatrix);
226                 fCommandVector[i]->execute(&filterCanvas);
227             }
228         }
229 #if SK_SUPPORT_GPU
230         if (at && acb) {
231             delete acb;
232         }
233 #endif
234     }
235 
236     if (SkColorGetA(fClipVizColor) != 0) {
237         filterCanvas.save();
238         #define LARGE_COORD 1000000000
239         filterCanvas.clipRect(
240                 SkRect::MakeLTRB(-LARGE_COORD, -LARGE_COORD, LARGE_COORD, LARGE_COORD),
241                 kReverseDifference_SkClipOp);
242         SkPaint clipPaint;
243         clipPaint.setColor(fClipVizColor);
244         filterCanvas.drawPaint(clipPaint);
245         filterCanvas.restore();
246     }
247 
248     if (pathOpsMode) {
249         this->resetClipStackData();
250         const SkClipStack* clipStack = nullptr;//HACK filterCanvas.getClipStack();
251         SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
252         const SkClipStack::Element* element;
253         SkPath devPath;
254         while ((element = iter.next())) {
255             SkClipStack::Element::DeviceSpaceType type = element->getDeviceSpaceType();
256             SkPath operand;
257             if (type != SkClipStack::Element::DeviceSpaceType::kEmpty) {
258                 element->asDeviceSpacePath(&operand);
259             }
260             SkClipOp elementOp = element->getOp();
261             this->addClipStackData(devPath, operand, elementOp);
262             if (elementOp == kReplace_SkClipOp) {
263                 devPath = operand;
264             } else {
265                 Op(devPath, operand, (SkPathOp) elementOp, &devPath);
266             }
267         }
268         this->lastClipStackData(devPath);
269     }
270     fMatrix = filterCanvas.getTotalMatrix();
271     fClip = filterCanvas.getDeviceClipBounds();
272     filterCanvas.restoreToCount(saveCount);
273 
274 #if SK_SUPPORT_GPU
275     // draw any ops if required and issue a full reset onto GrAuditTrail
276     if (at) {
277         // just in case there is global reordering, we flush the canvas before querying
278         // GrAuditTrail
279         GrAuditTrail::AutoEnable ae(at);
280         filterCanvas.flush();
281 
282         // we pick three colorblind-safe colors, 75% alpha
283         static const SkColor kTotalBounds = SkColorSetARGB(0xC0, 0x6A, 0x3D, 0x9A);
284         static const SkColor kCommandOpBounds = SkColorSetARGB(0xC0, 0xE3, 0x1A, 0x1C);
285         static const SkColor kOtherOpBounds = SkColorSetARGB(0xC0, 0xFF, 0x7F, 0x00);
286 
287         // 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();
291         GrSurfaceProxy::UniqueID proxyID = rtc->asSurfaceProxy()->uniqueID();
292 
293         // get the bounding boxes to draw
294         SkTArray<GrAuditTrail::OpInfo> childrenBounds;
295         if (m == -1) {
296             at->getBoundsByClientID(&childrenBounds, index);
297         } else {
298             // the client wants us to draw the mth op
299             at->getBoundsByOpListID(&childrenBounds.push_back(), m);
300         }
301         SkPaint paint;
302         paint.setStyle(SkPaint::kStroke_Style);
303         paint.setStrokeWidth(1);
304         for (int i = 0; i < childrenBounds.count(); i++) {
305             if (childrenBounds[i].fProxyUniqueID != proxyID) {
306                 // offscreen draw, ignore for now
307                 continue;
308             }
309             paint.setColor(kTotalBounds);
310             filterCanvas.drawRect(childrenBounds[i].fBounds, paint);
311             for (int j = 0; j < childrenBounds[i].fOps.count(); j++) {
312                 const GrAuditTrail::OpInfo::Op& op = childrenBounds[i].fOps[j];
313                 if (op.fClientID != index) {
314                     paint.setColor(kOtherOpBounds);
315                 } else {
316                     paint.setColor(kCommandOpBounds);
317                 }
318                 filterCanvas.drawRect(op.fBounds, paint);
319             }
320         }
321     }
322 #endif
323     this->cleanupAuditTrail(originalCanvas);
324 }
325 
deleteDrawCommandAt(int index)326 void SkDebugCanvas::deleteDrawCommandAt(int index) {
327     SkASSERT(index < fCommandVector.count());
328     delete fCommandVector[index];
329     fCommandVector.remove(index);
330 }
331 
getDrawCommandAt(int index)332 SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) {
333     SkASSERT(index < fCommandVector.count());
334     return fCommandVector[index];
335 }
336 
setDrawCommandAt(int index,SkDrawCommand * command)337 void SkDebugCanvas::setDrawCommandAt(int index, SkDrawCommand* command) {
338     SkASSERT(index < fCommandVector.count());
339     delete fCommandVector[index];
340     fCommandVector[index] = command;
341 }
342 
getCommandInfo(int index) const343 const SkTDArray<SkString*>* SkDebugCanvas::getCommandInfo(int index) const {
344     SkASSERT(index < fCommandVector.count());
345     return fCommandVector[index]->Info();
346 }
347 
getDrawCommandVisibilityAt(int index)348 bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) {
349     SkASSERT(index < fCommandVector.count());
350     return fCommandVector[index]->isVisible();
351 }
352 
getDrawCommands() const353 const SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() const {
354     return fCommandVector;
355 }
356 
getDrawCommands()357 SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() {
358     return fCommandVector;
359 }
360 
getAuditTrail(SkCanvas * canvas)361 GrAuditTrail* SkDebugCanvas::getAuditTrail(SkCanvas* canvas) {
362     GrAuditTrail* at = nullptr;
363 #if SK_SUPPORT_GPU
364     GrContext* ctx = canvas->getGrContext();
365     if (ctx) {
366         at = ctx->getAuditTrail();
367     }
368 #endif
369     return at;
370 }
371 
drawAndCollectOps(int n,SkCanvas * canvas)372 void SkDebugCanvas::drawAndCollectOps(int n, SkCanvas* canvas) {
373 #if SK_SUPPORT_GPU
374     GrAuditTrail* at = this->getAuditTrail(canvas);
375     if (at) {
376         // 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++) {
379             GrAuditTrail::AutoCollectOps enable(at, i);
380             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         }
388     }
389 #endif
390 }
391 
cleanupAuditTrail(SkCanvas * canvas)392 void 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 
toJSON(UrlDataManager & urlDataManager,int n,SkCanvas * canvas)402 Json::Value SkDebugCanvas::toJSON(UrlDataManager& urlDataManager, int n, SkCanvas* canvas) {
403     this->drawAndCollectOps(n, canvas);
404 
405     // now collect json
406 #if SK_SUPPORT_GPU
407     GrAuditTrail* at = this->getAuditTrail(canvas);
408 #endif
409     Json::Value result = Json::Value(Json::objectValue);
410     result[SKDEBUGCANVAS_ATTRIBUTE_VERSION] = Json::Value(SKDEBUGCANVAS_VERSION);
411     Json::Value commands = Json::Value(Json::arrayValue);
412     for (int i = 0; i < this->getSize() && i <= n; i++) {
413         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
425     }
426     this->cleanupAuditTrail(canvas);
427     result[SKDEBUGCANVAS_ATTRIBUTE_COMMANDS] = commands;
428     return result;
429 }
430 
toJSONOpList(int n,SkCanvas * canvas)431 Json::Value SkDebugCanvas::toJSONOpList(int n, SkCanvas* canvas) {
432     this->drawAndCollectOps(n, canvas);
433 
434     Json::Value parsedFromString;
435 #if SK_SUPPORT_GPU
436     GrAuditTrail* at = this->getAuditTrail(canvas);
437     if (at) {
438         GrAuditTrail::AutoManageOpList enable(at);
439         Json::Reader reader;
440         SkAssertResult(reader.parse(at->toJson().c_str(), parsedFromString));
441     }
442 #endif
443     this->cleanupAuditTrail(canvas);
444     return parsedFromString;
445 }
446 
setOverdrawViz(bool overdrawViz)447 void SkDebugCanvas::setOverdrawViz(bool overdrawViz) {
448     fOverdrawViz = overdrawViz;
449 }
450 
overrideTexFiltering(bool overrideTexFiltering,SkFilterQuality quality)451 void SkDebugCanvas::overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality quality) {
452     fOverrideFilterQuality = overrideTexFiltering;
453     fFilterQuality = quality;
454 }
455 
onClipPath(const SkPath & path,SkClipOp op,ClipEdgeStyle edgeStyle)456 void SkDebugCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
457     this->addDrawCommand(new SkClipPathCommand(path, op, kSoft_ClipEdgeStyle == edgeStyle));
458 }
459 
onClipRect(const SkRect & rect,SkClipOp op,ClipEdgeStyle edgeStyle)460 void SkDebugCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
461     this->addDrawCommand(new SkClipRectCommand(rect, op, kSoft_ClipEdgeStyle == edgeStyle));
462 }
463 
onClipRRect(const SkRRect & rrect,SkClipOp op,ClipEdgeStyle edgeStyle)464 void SkDebugCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
465     this->addDrawCommand(new SkClipRRectCommand(rrect, op, kSoft_ClipEdgeStyle == edgeStyle));
466 }
467 
onClipRegion(const SkRegion & region,SkClipOp op)468 void SkDebugCanvas::onClipRegion(const SkRegion& region, SkClipOp op) {
469     this->addDrawCommand(new SkClipRegionCommand(region, op));
470 }
471 
didConcat(const SkMatrix & matrix)472 void SkDebugCanvas::didConcat(const SkMatrix& matrix) {
473     this->addDrawCommand(new SkConcatCommand(matrix));
474     this->INHERITED::didConcat(matrix);
475 }
476 
onDrawAnnotation(const SkRect & rect,const char key[],SkData * value)477 void SkDebugCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
478     this->addDrawCommand(new SkDrawAnnotationCommand(rect, key, sk_ref_sp(value)));
479 }
480 
onDrawBitmap(const SkBitmap & bitmap,SkScalar left,SkScalar top,const SkPaint * paint)481 void SkDebugCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar left,
482                                  SkScalar top, const SkPaint* paint) {
483     this->addDrawCommand(new SkDrawBitmapCommand(bitmap, left, top, paint));
484 }
485 
onDrawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)486 void SkDebugCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
487                                      const SkPaint* paint, SrcRectConstraint constraint) {
488     this->addDrawCommand(new SkDrawBitmapRectCommand(bitmap, src, dst, paint,
489                                                      (SrcRectConstraint)constraint));
490 }
491 
onDrawBitmapNine(const SkBitmap & bitmap,const SkIRect & center,const SkRect & dst,const SkPaint * paint)492 void SkDebugCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
493                                      const SkRect& dst, const SkPaint* paint) {
494     this->addDrawCommand(new SkDrawBitmapNineCommand(bitmap, center, dst, paint));
495 }
496 
onDrawImage(const SkImage * image,SkScalar left,SkScalar top,const SkPaint * paint)497 void SkDebugCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
498                                 const SkPaint* paint) {
499     this->addDrawCommand(new SkDrawImageCommand(image, left, top, paint));
500 }
501 
onDrawImageLattice(const SkImage * image,const Lattice & lattice,const SkRect & dst,const SkPaint * paint)502 void 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 
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint constraint)507 void SkDebugCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
508                                     const SkPaint* paint, SrcRectConstraint constraint) {
509     this->addDrawCommand(new SkDrawImageRectCommand(image, src, dst, paint, constraint));
510 }
511 
onDrawOval(const SkRect & oval,const SkPaint & paint)512 void SkDebugCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
513     this->addDrawCommand(new SkDrawOvalCommand(oval, paint));
514 }
515 
onDrawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)516 void 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 
onDrawPaint(const SkPaint & paint)521 void SkDebugCanvas::onDrawPaint(const SkPaint& paint) {
522     this->addDrawCommand(new SkDrawPaintCommand(paint));
523 }
524 
onDrawPath(const SkPath & path,const SkPaint & paint)525 void SkDebugCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
526     this->addDrawCommand(new SkDrawPathCommand(path, paint));
527 }
528 
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)529 void SkDebugCanvas::onDrawPicture(const SkPicture* picture,
530                                   const SkMatrix* matrix,
531                                   const SkPaint* paint) {
532     this->addDrawCommand(new SkBeginDrawPictureCommand(picture, matrix, paint));
533     SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
534     picture->playback(this);
535     this->addDrawCommand(new SkEndDrawPictureCommand(SkToBool(matrix) || SkToBool(paint)));
536 }
537 
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)538 void SkDebugCanvas::onDrawPoints(PointMode mode, size_t count,
539                                  const SkPoint pts[], const SkPaint& paint) {
540     this->addDrawCommand(new SkDrawPointsCommand(mode, count, pts, paint));
541 }
542 
onDrawPosText(const void * text,size_t byteLength,const SkPoint pos[],const SkPaint & paint)543 void SkDebugCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
544                                   const SkPaint& paint) {
545     this->addDrawCommand(new SkDrawPosTextCommand(text, byteLength, pos, paint));
546 }
547 
onDrawPosTextH(const void * text,size_t byteLength,const SkScalar xpos[],SkScalar constY,const SkPaint & paint)548 void SkDebugCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
549                                    SkScalar constY, const SkPaint& paint) {
550     this->addDrawCommand(
551         new SkDrawPosTextHCommand(text, byteLength, xpos, constY, paint));
552 }
553 
onDrawRect(const SkRect & rect,const SkPaint & paint)554 void SkDebugCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
555     // NOTE(chudy): Messing up when renamed to DrawRect... Why?
556     addDrawCommand(new SkDrawRectCommand(rect, paint));
557 }
558 
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)559 void SkDebugCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
560     this->addDrawCommand(new SkDrawRRectCommand(rrect, paint));
561 }
562 
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)563 void SkDebugCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
564                                  const SkPaint& paint) {
565     this->addDrawCommand(new SkDrawDRRectCommand(outer, inner, paint));
566 }
567 
onDrawText(const void * text,size_t byteLength,SkScalar x,SkScalar y,const SkPaint & paint)568 void SkDebugCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
569                                const SkPaint& paint) {
570     this->addDrawCommand(new SkDrawTextCommand(text, byteLength, x, y, paint));
571 }
572 
onDrawTextOnPath(const void * text,size_t byteLength,const SkPath & path,const SkMatrix * matrix,const SkPaint & paint)573 void SkDebugCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
574                                      const SkMatrix* matrix, const SkPaint& paint) {
575     this->addDrawCommand(
576         new SkDrawTextOnPathCommand(text, byteLength, path, matrix, paint));
577 }
578 
onDrawTextRSXform(const void * text,size_t byteLength,const SkRSXform xform[],const SkRect * cull,const SkPaint & paint)579 void 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 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)584 void SkDebugCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
585                                    const SkPaint& paint) {
586     this->addDrawCommand(new SkDrawTextBlobCommand(sk_ref_sp(const_cast<SkTextBlob*>(blob)),
587                                                    x, y, paint));
588 }
589 
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode bmode,const SkPaint & paint)590 void SkDebugCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
591                                 const SkPoint texCoords[4], SkBlendMode bmode,
592                                 const SkPaint& paint) {
593     this->addDrawCommand(new SkDrawPatchCommand(cubics, colors, texCoords, bmode, paint));
594 }
595 
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode bmode,const SkPaint & paint)596 void 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));
600 }
601 
willRestore()602 void SkDebugCanvas::willRestore() {
603     this->addDrawCommand(new SkRestoreCommand());
604     this->INHERITED::willRestore();
605 }
606 
willSave()607 void SkDebugCanvas::willSave() {
608     this->addDrawCommand(new SkSaveCommand());
609     this->INHERITED::willSave();
610 }
611 
getSaveLayerStrategy(const SaveLayerRec & rec)612 SkCanvas::SaveLayerStrategy SkDebugCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
613     this->addDrawCommand(new SkSaveLayerCommand(rec));
614     (void)this->INHERITED::getSaveLayerStrategy(rec);
615     // No need for a full layer.
616     return kNoLayer_SaveLayerStrategy;
617 }
618 
didSetMatrix(const SkMatrix & matrix)619 void SkDebugCanvas::didSetMatrix(const SkMatrix& matrix) {
620     this->addDrawCommand(new SkSetMatrixCommand(matrix));
621     this->INHERITED::didSetMatrix(matrix);
622 }
623 
toggleCommand(int index,bool toggle)624 void SkDebugCanvas::toggleCommand(int index, bool toggle) {
625     SkASSERT(index < fCommandVector.count());
626     fCommandVector[index]->setVisible(toggle);
627 }
628 
629 static const char* gFillTypeStrs[] = {
630     "kWinding_FillType",
631     "kEvenOdd_FillType",
632     "kInverseWinding_FillType",
633     "kInverseEvenOdd_FillType"
634 };
635 
636 static const char* gOpStrs[] = {
637     "kDifference_PathOp",
638     "kIntersect_PathOp",
639     "kUnion_PathOp",
640     "kXor_PathOp",
641     "kReverseDifference_PathOp",
642 };
643 
644 static const char kHTML4SpaceIndent[] = "&nbsp;&nbsp;&nbsp;&nbsp;";
645 
outputScalar(SkScalar num)646 void 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 
outputPointsCommon(const SkPoint * pts,int count)662 void 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 
outputPoints(const SkPoint * pts,int count)673 void SkDebugCanvas::outputPoints(const SkPoint* pts, int count) {
674     this->outputPointsCommon(pts, count);
675     fClipStackData.appendf(");<br>");
676 }
677 
outputConicPoints(const SkPoint * pts,SkScalar weight)678 void 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 
addPathData(const SkPath & path,const char * pathName)685 void 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 
addClipStackData(const SkPath & devPath,const SkPath & operand,SkClipOp elementOp)726 void SkDebugCanvas::addClipStackData(const SkPath& devPath, const SkPath& operand,
727                                      SkClipOp elementOp) {
728     if (elementOp == kReplace_SkClipOp) {
729         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>",
739             kHTML4SpaceIndent, gOpStrs[static_cast<int>(elementOp)]);
740         fClipStackData.appendf("}<br>");
741         fCalledAddStackData = true;
742     }
743 }
744 
lastClipStackData(const SkPath & devPath)745 bool SkDebugCanvas::lastClipStackData(const SkPath& devPath) {
746     if (fCalledAddStackData) {
747         fClipStackData.appendf("<br>");
748         addPathData(devPath, "pathOut");
749         return true;
750     }
751     return false;
752 }
753