Home | History | Annotate | Download | only in debugger
      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 "SkTextBlob.h"
     14 #include "SkClipOpPriv.h"
     15 
     16 #if SK_SUPPORT_GPU
     17 #include "GrAuditTrail.h"
     18 #include "GrContext.h"
     19 #include "GrRenderTargetContext.h"
     20 #endif
     21 
     22 #define SKDEBUGCANVAS_VERSION                     1
     23 #define SKDEBUGCANVAS_ATTRIBUTE_VERSION           "version"
     24 #define SKDEBUGCANVAS_ATTRIBUTE_COMMANDS          "commands"
     25 #define SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL        "auditTrail"
     26 
     27 class DebugPaintFilterCanvas : public SkPaintFilterCanvas {
     28 public:
     29     DebugPaintFilterCanvas(SkCanvas* canvas,
     30                            bool overdrawViz,
     31                            bool overrideFilterQuality,
     32                            SkFilterQuality quality)
     33         : INHERITED(canvas)
     34         , fOverdrawViz(overdrawViz)
     35         , fOverrideFilterQuality(overrideFilterQuality)
     36         , fFilterQuality(quality) {}
     37 
     38 protected:
     39     bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type) const override {
     40         if (*paint) {
     41             if (fOverdrawViz) {
     42                 paint->writable()->setColor(SK_ColorRED);
     43                 paint->writable()->setAlpha(0x08);
     44                 paint->writable()->setBlendMode(SkBlendMode::kSrcOver);
     45             }
     46 
     47             if (fOverrideFilterQuality) {
     48                 paint->writable()->setFilterQuality(fFilterQuality);
     49             }
     50         }
     51         return true;
     52     }
     53 
     54     void onDrawPicture(const SkPicture* picture,
     55                        const SkMatrix* matrix,
     56                        const SkPaint* paint) override {
     57         // 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 
     61     void onDrawShadowedPicture(const SkPicture* picture,
     62                                const SkMatrix* matrix,
     63                                const SkPaint* paint,
     64                                const SkShadowParams& params) {
     65 #ifdef SK_EXPERIMENTAL_SHADOWING
     66         this->SkCanvas::onDrawShadowedPicture(picture, matrix, paint, params);
     67 #else
     68         this->SkCanvas::onDrawPicture(picture, matrix, paint);
     69 #endif
     70     }
     71 
     72 private:
     73     bool fOverdrawViz;
     74     bool fOverrideFilterQuality;
     75     SkFilterQuality fFilterQuality;
     76 
     77     typedef SkPaintFilterCanvas INHERITED;
     78 };
     79 
     80 SkDebugCanvas::SkDebugCanvas(int width, int height)
     81         : INHERITED(width, height)
     82         , fPicture(nullptr)
     83         , fFilter(false)
     84         , fMegaVizMode(false)
     85         , fOverdrawViz(false)
     86         , fOverrideFilterQuality(false)
     87         , fFilterQuality(kNone_SkFilterQuality)
     88         , fClipVizColor(SK_ColorTRANSPARENT)
     89         , fDrawGpuOpBounds(false) {
     90     fUserMatrix.reset();
     91 
     92     // SkPicturePlayback uses the base-class' quickReject calls to cull clipped
     93     // operations. This can lead to problems in the debugger which expects all
     94     // the operations in the captured skp to appear in the debug canvas. To
     95     // circumvent this we create a wide open clip here (an empty clip rect
     96     // is not sufficient).
     97     // Internally, the SkRect passed to clipRect is converted to an SkIRect and
     98     // rounded out. The following code creates a nearly maximal rect that will
     99     // not get collapsed by the coming conversions (Due to precision loss the
    100     // inset has to be surprisingly large).
    101     SkIRect largeIRect = SkIRect::MakeLargest();
    102     largeIRect.inset(1024, 1024);
    103     SkRect large = SkRect::Make(largeIRect);
    104 #ifdef SK_DEBUG
    105     SkASSERT(!large.roundOut().isEmpty());
    106 #endif
    107     // call the base class' version to avoid adding a draw command
    108     this->INHERITED::onClipRect(large, kReplace_SkClipOp, kHard_ClipEdgeStyle);
    109 }
    110 
    111 SkDebugCanvas::~SkDebugCanvas() {
    112     fCommandVector.deleteAll();
    113 }
    114 
    115 void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) {
    116     fCommandVector.push(command);
    117 }
    118 
    119 void SkDebugCanvas::draw(SkCanvas* canvas) {
    120     if (!fCommandVector.isEmpty()) {
    121         this->drawTo(canvas, fCommandVector.count() - 1);
    122     }
    123 }
    124 
    125 void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) {
    126     canvas->concat(fUserMatrix);
    127 }
    128 
    129 int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) {
    130     SkBitmap bitmap;
    131     bitmap.allocPixels(SkImageInfo::MakeN32Premul(1, 1));
    132 
    133     SkCanvas canvas(bitmap);
    134     canvas.translate(SkIntToScalar(-x), SkIntToScalar(-y));
    135     this->applyUserTransform(&canvas);
    136 
    137     int layer = 0;
    138     SkColor prev = bitmap.getColor(0,0);
    139     for (int i = 0; i < index; i++) {
    140         if (fCommandVector[i]->isVisible()) {
    141             fCommandVector[i]->setUserMatrix(fUserMatrix);
    142             fCommandVector[i]->execute(&canvas);
    143         }
    144         if (prev != bitmap.getColor(0,0)) {
    145             layer = i;
    146         }
    147         prev = bitmap.getColor(0,0);
    148     }
    149     return layer;
    150 }
    151 
    152 class SkDebugClipVisitor : public SkCanvas::ClipVisitor {
    153 public:
    154     SkDebugClipVisitor(SkCanvas* canvas) : fCanvas(canvas) {}
    155 
    156     void clipRect(const SkRect& r, SkClipOp, bool doAA) override {
    157         SkPaint p;
    158         p.setColor(SK_ColorRED);
    159         p.setStyle(SkPaint::kStroke_Style);
    160         p.setAntiAlias(doAA);
    161         fCanvas->drawRect(r, p);
    162     }
    163     void clipRRect(const SkRRect& rr, SkClipOp, bool doAA) override {
    164         SkPaint p;
    165         p.setColor(SK_ColorGREEN);
    166         p.setStyle(SkPaint::kStroke_Style);
    167         p.setAntiAlias(doAA);
    168         fCanvas->drawRRect(rr, p);
    169     }
    170     void clipPath(const SkPath& path, SkClipOp, bool doAA) override {
    171         SkPaint p;
    172         p.setColor(SK_ColorBLUE);
    173         p.setStyle(SkPaint::kStroke_Style);
    174         p.setAntiAlias(doAA);
    175         fCanvas->drawPath(path, p);
    176     }
    177 
    178 protected:
    179     SkCanvas* fCanvas;
    180 
    181 private:
    182     typedef SkCanvas::ClipVisitor INHERITED;
    183 };
    184 
    185 // set up the saveLayer commands so that the active ones
    186 // return true in their 'active' method
    187 void SkDebugCanvas::markActiveCommands(int index) {
    188     fActiveLayers.rewind();
    189 
    190     for (int i = 0; i < fCommandVector.count(); ++i) {
    191         fCommandVector[i]->setActive(false);
    192     }
    193 
    194     for (int i = 0; i < index; ++i) {
    195         SkDrawCommand::Action result = fCommandVector[i]->action();
    196         if (SkDrawCommand::kPushLayer_Action == result) {
    197             fActiveLayers.push(fCommandVector[i]);
    198         } else if (SkDrawCommand::kPopLayer_Action == result) {
    199             fActiveLayers.pop();
    200         }
    201     }
    202 
    203     for (int i = 0; i < fActiveLayers.count(); ++i) {
    204         fActiveLayers[i]->setActive(true);
    205     }
    206 
    207 }
    208 
    209 void SkDebugCanvas::drawTo(SkCanvas* originalCanvas, int index, int m) {
    210     SkASSERT(!fCommandVector.isEmpty());
    211     SkASSERT(index < fCommandVector.count());
    212 
    213     int saveCount = originalCanvas->save();
    214 
    215     SkRect windowRect = SkRect::MakeWH(SkIntToScalar(originalCanvas->getBaseLayerSize().width()),
    216                                        SkIntToScalar(originalCanvas->getBaseLayerSize().height()));
    217 
    218     bool pathOpsMode = getAllowSimplifyClip();
    219     originalCanvas->setAllowSimplifyClip(pathOpsMode);
    220     originalCanvas->clear(SK_ColorWHITE);
    221     originalCanvas->resetMatrix();
    222     if (!windowRect.isEmpty()) {
    223         originalCanvas->clipRect(windowRect, kReplace_SkClipOp);
    224     }
    225     this->applyUserTransform(originalCanvas);
    226 
    227     DebugPaintFilterCanvas filterCanvas(originalCanvas, fOverdrawViz, fOverrideFilterQuality,
    228                                         fFilterQuality);
    229 
    230     if (fMegaVizMode) {
    231         this->markActiveCommands(index);
    232     }
    233 
    234 #if SK_SUPPORT_GPU
    235     // If we have a GPU backend we can also visualize the op information
    236     GrAuditTrail* at = nullptr;
    237     if (fDrawGpuOpBounds || m != -1) {
    238         // The audit trail must be obtained from the original canvas.
    239         at = this->getAuditTrail(originalCanvas);
    240     }
    241 #endif
    242 
    243     for (int i = 0; i <= index; i++) {
    244         if (i == index && fFilter) {
    245             filterCanvas.clear(0xAAFFFFFF);
    246         }
    247 
    248 #if SK_SUPPORT_GPU
    249         // We need to flush any pending operations, or they might combine with commands below.
    250         // Previous operations were not registered with the audit trail when they were
    251         // created, so if we allow them to combine, the audit trail will fail to find them.
    252         filterCanvas.flush();
    253 
    254         GrAuditTrail::AutoCollectOps* acb = nullptr;
    255         if (at) {
    256             acb = new GrAuditTrail::AutoCollectOps(at, i);
    257         }
    258 #endif
    259 
    260         if (fCommandVector[i]->isVisible()) {
    261             if (fMegaVizMode && fCommandVector[i]->active()) {
    262                 // "active" commands execute their visualization behaviors:
    263                 //     All active saveLayers get replaced with saves so all draws go to the
    264                 //     visible canvas.
    265                 //     All active culls draw their cull box
    266                 fCommandVector[i]->vizExecute(&filterCanvas);
    267             } else {
    268                 fCommandVector[i]->setUserMatrix(fUserMatrix);
    269                 fCommandVector[i]->execute(&filterCanvas);
    270             }
    271         }
    272 #if SK_SUPPORT_GPU
    273         if (at && acb) {
    274             delete acb;
    275         }
    276 #endif
    277     }
    278 
    279     if (SkColorGetA(fClipVizColor) != 0) {
    280         filterCanvas.save();
    281         #define LARGE_COORD 1000000000
    282         filterCanvas.clipRect(
    283                 SkRect::MakeLTRB(-LARGE_COORD, -LARGE_COORD, LARGE_COORD, LARGE_COORD),
    284                 kReverseDifference_SkClipOp);
    285         SkPaint clipPaint;
    286         clipPaint.setColor(fClipVizColor);
    287         filterCanvas.drawPaint(clipPaint);
    288         filterCanvas.restore();
    289     }
    290 
    291     if (fMegaVizMode) {
    292         filterCanvas.save();
    293         // nuke the CTM
    294         filterCanvas.resetMatrix();
    295         // turn off clipping
    296         if (!windowRect.isEmpty()) {
    297             SkRect r = windowRect;
    298             r.outset(SK_Scalar1, SK_Scalar1);
    299             filterCanvas.clipRect(r, kReplace_SkClipOp);
    300         }
    301         // visualize existing clips
    302         SkDebugClipVisitor visitor(&filterCanvas);
    303 
    304         filterCanvas.replayClips(&visitor);
    305 
    306         filterCanvas.restore();
    307     }
    308     if (pathOpsMode) {
    309         this->resetClipStackData();
    310         const SkClipStack* clipStack = nullptr;//HACK filterCanvas.getClipStack();
    311         SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
    312         const SkClipStack::Element* element;
    313         SkPath devPath;
    314         while ((element = iter.next())) {
    315             SkClipStack::Element::Type type = element->getType();
    316             SkPath operand;
    317             if (type != SkClipStack::Element::kEmpty_Type) {
    318                element->asPath(&operand);
    319             }
    320             SkClipOp elementOp = element->getOp();
    321             this->addClipStackData(devPath, operand, elementOp);
    322             if (elementOp == kReplace_SkClipOp) {
    323                 devPath = operand;
    324             } else {
    325                 Op(devPath, operand, (SkPathOp) elementOp, &devPath);
    326             }
    327         }
    328         this->lastClipStackData(devPath);
    329     }
    330     fMatrix = filterCanvas.getTotalMatrix();
    331     fClip = filterCanvas.getDeviceClipBounds();
    332     filterCanvas.restoreToCount(saveCount);
    333 
    334 #if SK_SUPPORT_GPU
    335     // draw any ops if required and issue a full reset onto GrAuditTrail
    336     if (at) {
    337         // just in case there is global reordering, we flush the canvas before querying
    338         // GrAuditTrail
    339         GrAuditTrail::AutoEnable ae(at);
    340         filterCanvas.flush();
    341 
    342         // we pick three colorblind-safe colors, 75% alpha
    343         static const SkColor kTotalBounds = SkColorSetARGB(0xC0, 0x6A, 0x3D, 0x9A);
    344         static const SkColor kCommandOpBounds = SkColorSetARGB(0xC0, 0xE3, 0x1A, 0x1C);
    345         static const SkColor kOtherOpBounds = SkColorSetARGB(0xC0, 0xFF, 0x7F, 0x00);
    346 
    347         // get the render target of the top device (from the original canvas) so we can ignore ops
    348         // drawn offscreen
    349         GrRenderTargetContext* rtc =
    350                 originalCanvas->internal_private_accessTopLayerRenderTargetContext();
    351         GrGpuResource::UniqueID rtID = rtc->accessRenderTarget()->uniqueID();
    352 
    353         // get the bounding boxes to draw
    354         SkTArray<GrAuditTrail::OpInfo> childrenBounds;
    355         if (m == -1) {
    356             at->getBoundsByClientID(&childrenBounds, index);
    357         } else {
    358             // the client wants us to draw the mth op
    359             at->getBoundsByOpListID(&childrenBounds.push_back(), m);
    360         }
    361         SkPaint paint;
    362         paint.setStyle(SkPaint::kStroke_Style);
    363         paint.setStrokeWidth(1);
    364         for (int i = 0; i < childrenBounds.count(); i++) {
    365             if (childrenBounds[i].fRenderTargetUniqueID != rtID) {
    366                 // offscreen draw, ignore for now
    367                 continue;
    368             }
    369             paint.setColor(kTotalBounds);
    370             filterCanvas.drawRect(childrenBounds[i].fBounds, paint);
    371             for (int j = 0; j < childrenBounds[i].fOps.count(); j++) {
    372                 const GrAuditTrail::OpInfo::Op& op = childrenBounds[i].fOps[j];
    373                 if (op.fClientID != index) {
    374                     paint.setColor(kOtherOpBounds);
    375                 } else {
    376                     paint.setColor(kCommandOpBounds);
    377                 }
    378                 filterCanvas.drawRect(op.fBounds, paint);
    379             }
    380         }
    381     }
    382 #endif
    383     this->cleanupAuditTrail(originalCanvas);
    384 }
    385 
    386 void SkDebugCanvas::deleteDrawCommandAt(int index) {
    387     SkASSERT(index < fCommandVector.count());
    388     delete fCommandVector[index];
    389     fCommandVector.remove(index);
    390 }
    391 
    392 SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) {
    393     SkASSERT(index < fCommandVector.count());
    394     return fCommandVector[index];
    395 }
    396 
    397 void SkDebugCanvas::setDrawCommandAt(int index, SkDrawCommand* command) {
    398     SkASSERT(index < fCommandVector.count());
    399     delete fCommandVector[index];
    400     fCommandVector[index] = command;
    401 }
    402 
    403 const SkTDArray<SkString*>* SkDebugCanvas::getCommandInfo(int index) const {
    404     SkASSERT(index < fCommandVector.count());
    405     return fCommandVector[index]->Info();
    406 }
    407 
    408 bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) {
    409     SkASSERT(index < fCommandVector.count());
    410     return fCommandVector[index]->isVisible();
    411 }
    412 
    413 const SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() const {
    414     return fCommandVector;
    415 }
    416 
    417 SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() {
    418     return fCommandVector;
    419 }
    420 
    421 GrAuditTrail* SkDebugCanvas::getAuditTrail(SkCanvas* canvas) {
    422     GrAuditTrail* at = nullptr;
    423 #if SK_SUPPORT_GPU
    424     GrContext* ctx = canvas->getGrContext();
    425     if (ctx) {
    426         at = ctx->getAuditTrail();
    427     }
    428 #endif
    429     return at;
    430 }
    431 
    432 void SkDebugCanvas::drawAndCollectOps(int n, SkCanvas* canvas) {
    433 #if SK_SUPPORT_GPU
    434     GrAuditTrail* at = this->getAuditTrail(canvas);
    435     if (at) {
    436         // loop over all of the commands and draw them, this is to collect reordering
    437         // information
    438         for (int i = 0; i < this->getSize() && i <= n; i++) {
    439             GrAuditTrail::AutoCollectOps enable(at, i);
    440             fCommandVector[i]->execute(canvas);
    441         }
    442 
    443         // in case there is some kind of global reordering
    444         {
    445             GrAuditTrail::AutoEnable ae(at);
    446             canvas->flush();
    447         }
    448     }
    449 #endif
    450 }
    451 
    452 void SkDebugCanvas::cleanupAuditTrail(SkCanvas* canvas) {
    453     GrAuditTrail* at = this->getAuditTrail(canvas);
    454     if (at) {
    455 #if SK_SUPPORT_GPU
    456         GrAuditTrail::AutoEnable ae(at);
    457         at->fullReset();
    458 #endif
    459     }
    460 }
    461 
    462 Json::Value SkDebugCanvas::toJSON(UrlDataManager& urlDataManager, int n, SkCanvas* canvas) {
    463     this->drawAndCollectOps(n, canvas);
    464 
    465     // now collect json
    466 #if SK_SUPPORT_GPU
    467     GrAuditTrail* at = this->getAuditTrail(canvas);
    468 #endif
    469     Json::Value result = Json::Value(Json::objectValue);
    470     result[SKDEBUGCANVAS_ATTRIBUTE_VERSION] = Json::Value(SKDEBUGCANVAS_VERSION);
    471     Json::Value commands = Json::Value(Json::arrayValue);
    472     for (int i = 0; i < this->getSize() && i <= n; i++) {
    473         commands[i] = this->getDrawCommandAt(i)->toJSON(urlDataManager);
    474 #if SK_SUPPORT_GPU
    475         if (at) {
    476             // TODO if this is inefficient we could add a method to GrAuditTrail which takes
    477             // a Json::Value and is only compiled in this file
    478             Json::Value parsedFromString;
    479             Json::Reader reader;
    480             SkAssertResult(reader.parse(at->toJson(i).c_str(), parsedFromString));
    481 
    482             commands[i][SKDEBUGCANVAS_ATTRIBUTE_AUDITTRAIL] = parsedFromString;
    483         }
    484 #endif
    485     }
    486     this->cleanupAuditTrail(canvas);
    487     result[SKDEBUGCANVAS_ATTRIBUTE_COMMANDS] = commands;
    488     return result;
    489 }
    490 
    491 Json::Value SkDebugCanvas::toJSONOpList(int n, SkCanvas* canvas) {
    492     this->drawAndCollectOps(n, canvas);
    493 
    494     Json::Value parsedFromString;
    495 #if SK_SUPPORT_GPU
    496     GrAuditTrail* at = this->getAuditTrail(canvas);
    497     if (at) {
    498         GrAuditTrail::AutoManageOpList enable(at);
    499         Json::Reader reader;
    500         SkAssertResult(reader.parse(at->toJson().c_str(), parsedFromString));
    501     }
    502 #endif
    503     this->cleanupAuditTrail(canvas);
    504     return parsedFromString;
    505 }
    506 
    507 void SkDebugCanvas::setOverdrawViz(bool overdrawViz) {
    508     fOverdrawViz = overdrawViz;
    509 }
    510 
    511 void SkDebugCanvas::overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality quality) {
    512     fOverrideFilterQuality = overrideTexFiltering;
    513     fFilterQuality = quality;
    514 }
    515 
    516 void SkDebugCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
    517     this->addDrawCommand(new SkClipPathCommand(path, op, kSoft_ClipEdgeStyle == edgeStyle));
    518 }
    519 
    520 void SkDebugCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
    521     this->addDrawCommand(new SkClipRectCommand(rect, op, kSoft_ClipEdgeStyle == edgeStyle));
    522 }
    523 
    524 void SkDebugCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
    525     this->addDrawCommand(new SkClipRRectCommand(rrect, op, kSoft_ClipEdgeStyle == edgeStyle));
    526 }
    527 
    528 void SkDebugCanvas::onClipRegion(const SkRegion& region, SkClipOp op) {
    529     this->addDrawCommand(new SkClipRegionCommand(region, op));
    530 }
    531 
    532 void SkDebugCanvas::didConcat(const SkMatrix& matrix) {
    533     this->addDrawCommand(new SkConcatCommand(matrix));
    534     this->INHERITED::didConcat(matrix);
    535 }
    536 
    537 void SkDebugCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
    538     this->addDrawCommand(new SkDrawAnnotationCommand(rect, key, sk_ref_sp(value)));
    539 }
    540 
    541 void SkDebugCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar left,
    542                                  SkScalar top, const SkPaint* paint) {
    543     this->addDrawCommand(new SkDrawBitmapCommand(bitmap, left, top, paint));
    544 }
    545 
    546 void SkDebugCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
    547                                      const SkPaint* paint, SrcRectConstraint constraint) {
    548     this->addDrawCommand(new SkDrawBitmapRectCommand(bitmap, src, dst, paint,
    549                                                      (SrcRectConstraint)constraint));
    550 }
    551 
    552 void SkDebugCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
    553                                      const SkRect& dst, const SkPaint* paint) {
    554     this->addDrawCommand(new SkDrawBitmapNineCommand(bitmap, center, dst, paint));
    555 }
    556 
    557 void SkDebugCanvas::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
    558                                 const SkPaint* paint) {
    559     this->addDrawCommand(new SkDrawImageCommand(image, left, top, paint));
    560 }
    561 
    562 void SkDebugCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
    563                                        const SkRect& dst, const SkPaint* paint) {
    564     this->addDrawCommand(new SkDrawImageLatticeCommand(image, lattice, dst, paint));
    565 }
    566 
    567 void SkDebugCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
    568                                     const SkPaint* paint, SrcRectConstraint constraint) {
    569     this->addDrawCommand(new SkDrawImageRectCommand(image, src, dst, paint, constraint));
    570 }
    571 
    572 void SkDebugCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
    573     this->addDrawCommand(new SkDrawOvalCommand(oval, paint));
    574 }
    575 
    576 void SkDebugCanvas::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
    577                                bool useCenter, const SkPaint& paint) {
    578     this->addDrawCommand(new SkDrawArcCommand(oval, startAngle, sweepAngle, useCenter, paint));
    579 }
    580 
    581 void SkDebugCanvas::onDrawPaint(const SkPaint& paint) {
    582     this->addDrawCommand(new SkDrawPaintCommand(paint));
    583 }
    584 
    585 void SkDebugCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
    586     this->addDrawCommand(new SkDrawPathCommand(path, paint));
    587 }
    588 
    589 void SkDebugCanvas::onDrawPicture(const SkPicture* picture,
    590                                   const SkMatrix* matrix,
    591                                   const SkPaint* paint) {
    592     this->addDrawCommand(new SkBeginDrawPictureCommand(picture, matrix, paint));
    593     SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
    594     picture->playback(this);
    595     this->addDrawCommand(new SkEndDrawPictureCommand(SkToBool(matrix) || SkToBool(paint)));
    596 }
    597 
    598 void SkDebugCanvas::onDrawShadowedPicture(const SkPicture* picture,
    599                                           const SkMatrix* matrix,
    600                                           const SkPaint* paint,
    601                                           const SkShadowParams& params) {
    602     this->addDrawCommand(new SkBeginDrawShadowedPictureCommand(picture, matrix, paint, params));
    603     SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
    604     picture->playback(this);
    605     this->addDrawCommand(new SkEndDrawShadowedPictureCommand(SkToBool(matrix) || SkToBool(paint)));
    606 }
    607 
    608 void SkDebugCanvas::onDrawPoints(PointMode mode, size_t count,
    609                                  const SkPoint pts[], const SkPaint& paint) {
    610     this->addDrawCommand(new SkDrawPointsCommand(mode, count, pts, paint));
    611 }
    612 
    613 void SkDebugCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
    614                                   const SkPaint& paint) {
    615     this->addDrawCommand(new SkDrawPosTextCommand(text, byteLength, pos, paint));
    616 }
    617 
    618 void SkDebugCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
    619                                    SkScalar constY, const SkPaint& paint) {
    620     this->addDrawCommand(
    621         new SkDrawPosTextHCommand(text, byteLength, xpos, constY, paint));
    622 }
    623 
    624 void SkDebugCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
    625     // NOTE(chudy): Messing up when renamed to DrawRect... Why?
    626     addDrawCommand(new SkDrawRectCommand(rect, paint));
    627 }
    628 
    629 void SkDebugCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
    630     this->addDrawCommand(new SkDrawRRectCommand(rrect, paint));
    631 }
    632 
    633 void SkDebugCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
    634                                  const SkPaint& paint) {
    635     this->addDrawCommand(new SkDrawDRRectCommand(outer, inner, paint));
    636 }
    637 
    638 void SkDebugCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
    639                                const SkPaint& paint) {
    640     this->addDrawCommand(new SkDrawTextCommand(text, byteLength, x, y, paint));
    641 }
    642 
    643 void SkDebugCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
    644                                      const SkMatrix* matrix, const SkPaint& paint) {
    645     this->addDrawCommand(
    646         new SkDrawTextOnPathCommand(text, byteLength, path, matrix, paint));
    647 }
    648 
    649 void SkDebugCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
    650                                       const SkRect* cull, const SkPaint& paint) {
    651     this->addDrawCommand(new SkDrawTextRSXformCommand(text, byteLength, xform, cull, paint));
    652 }
    653 
    654 void SkDebugCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
    655                                    const SkPaint& paint) {
    656     this->addDrawCommand(new SkDrawTextBlobCommand(sk_ref_sp(const_cast<SkTextBlob*>(blob)),
    657                                                    x, y, paint));
    658 }
    659 
    660 void SkDebugCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
    661                                 const SkPoint texCoords[4], SkBlendMode bmode,
    662                                 const SkPaint& paint) {
    663     this->addDrawCommand(new SkDrawPatchCommand(cubics, colors, texCoords, bmode, paint));
    664 }
    665 
    666 void SkDebugCanvas::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode,
    667                                          const SkPaint& paint) {
    668     this->addDrawCommand(new SkDrawVerticesCommand(sk_ref_sp(const_cast<SkVertices*>(vertices)),
    669                                                    bmode, paint));
    670 }
    671 
    672 void SkDebugCanvas::willRestore() {
    673     this->addDrawCommand(new SkRestoreCommand());
    674     this->INHERITED::willRestore();
    675 }
    676 
    677 void SkDebugCanvas::willSave() {
    678     this->addDrawCommand(new SkSaveCommand());
    679     this->INHERITED::willSave();
    680 }
    681 
    682 SkCanvas::SaveLayerStrategy SkDebugCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
    683     this->addDrawCommand(new SkSaveLayerCommand(rec));
    684     (void)this->INHERITED::getSaveLayerStrategy(rec);
    685     // No need for a full layer.
    686     return kNoLayer_SaveLayerStrategy;
    687 }
    688 
    689 void SkDebugCanvas::didSetMatrix(const SkMatrix& matrix) {
    690     this->addDrawCommand(new SkSetMatrixCommand(matrix));
    691     this->INHERITED::didSetMatrix(matrix);
    692 }
    693 
    694 void SkDebugCanvas::didTranslateZ(SkScalar z) {
    695 #ifdef SK_EXPERIMENTAL_SHADOWING
    696     this->addDrawCommand(new SkTranslateZCommand(z));
    697     this->INHERITED::didTranslateZ(z);
    698 #endif
    699 }
    700 
    701 void SkDebugCanvas::toggleCommand(int index, bool toggle) {
    702     SkASSERT(index < fCommandVector.count());
    703     fCommandVector[index]->setVisible(toggle);
    704 }
    705 
    706 static const char* gFillTypeStrs[] = {
    707     "kWinding_FillType",
    708     "kEvenOdd_FillType",
    709     "kInverseWinding_FillType",
    710     "kInverseEvenOdd_FillType"
    711 };
    712 
    713 static const char* gOpStrs[] = {
    714     "kDifference_PathOp",
    715     "kIntersect_PathOp",
    716     "kUnion_PathOp",
    717     "kXor_PathOp",
    718     "kReverseDifference_PathOp",
    719 };
    720 
    721 static const char kHTML4SpaceIndent[] = "&nbsp;&nbsp;&nbsp;&nbsp;";
    722 
    723 void SkDebugCanvas::outputScalar(SkScalar num) {
    724     if (num == (int) num) {
    725         fClipStackData.appendf("%d", (int) num);
    726     } else {
    727         SkString str;
    728         str.printf("%1.9g", num);
    729         int width = (int) str.size();
    730         const char* cStr = str.c_str();
    731         while (cStr[width - 1] == '0') {
    732             --width;
    733         }
    734         str.resize(width);
    735         fClipStackData.appendf("%sf", str.c_str());
    736     }
    737 }
    738 
    739 void SkDebugCanvas::outputPointsCommon(const SkPoint* pts, int count) {
    740     for (int index = 0; index < count; ++index) {
    741         this->outputScalar(pts[index].fX);
    742         fClipStackData.appendf(", ");
    743         this->outputScalar(pts[index].fY);
    744         if (index + 1 < count) {
    745             fClipStackData.appendf(", ");
    746         }
    747     }
    748 }
    749 
    750 void SkDebugCanvas::outputPoints(const SkPoint* pts, int count) {
    751     this->outputPointsCommon(pts, count);
    752     fClipStackData.appendf(");<br>");
    753 }
    754 
    755 void SkDebugCanvas::outputConicPoints(const SkPoint* pts, SkScalar weight) {
    756     this->outputPointsCommon(pts, 2);
    757     fClipStackData.appendf(", ");
    758     this->outputScalar(weight);
    759     fClipStackData.appendf(");<br>");
    760 }
    761 
    762 void SkDebugCanvas::addPathData(const SkPath& path, const char* pathName) {
    763     SkPath::RawIter iter(path);
    764     SkPath::FillType fillType = path.getFillType();
    765     fClipStackData.appendf("%sSkPath %s;<br>", kHTML4SpaceIndent, pathName);
    766     fClipStackData.appendf("%s%s.setFillType(SkPath::%s);<br>", kHTML4SpaceIndent, pathName,
    767             gFillTypeStrs[fillType]);
    768     iter.setPath(path);
    769     uint8_t verb;
    770     SkPoint pts[4];
    771     while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
    772         switch (verb) {
    773             case SkPath::kMove_Verb:
    774                 fClipStackData.appendf("%s%s.moveTo(", kHTML4SpaceIndent, pathName);
    775                 this->outputPoints(&pts[0], 1);
    776                 continue;
    777             case SkPath::kLine_Verb:
    778                 fClipStackData.appendf("%s%s.lineTo(", kHTML4SpaceIndent, pathName);
    779                 this->outputPoints(&pts[1], 1);
    780                 break;
    781             case SkPath::kQuad_Verb:
    782                 fClipStackData.appendf("%s%s.quadTo(", kHTML4SpaceIndent, pathName);
    783                 this->outputPoints(&pts[1], 2);
    784                 break;
    785             case SkPath::kConic_Verb:
    786                 fClipStackData.appendf("%s%s.conicTo(", kHTML4SpaceIndent, pathName);
    787                 this->outputConicPoints(&pts[1], iter.conicWeight());
    788                 break;
    789             case SkPath::kCubic_Verb:
    790                 fClipStackData.appendf("%s%s.cubicTo(", kHTML4SpaceIndent, pathName);
    791                 this->outputPoints(&pts[1], 3);
    792                 break;
    793             case SkPath::kClose_Verb:
    794                 fClipStackData.appendf("%s%s.close();<br>", kHTML4SpaceIndent, pathName);
    795                 break;
    796             default:
    797                 SkDEBUGFAIL("bad verb");
    798                 return;
    799         }
    800     }
    801 }
    802 
    803 void SkDebugCanvas::addClipStackData(const SkPath& devPath, const SkPath& operand,
    804                                      SkClipOp elementOp) {
    805     if (elementOp == kReplace_SkClipOp) {
    806         if (!lastClipStackData(devPath)) {
    807             fSaveDevPath = operand;
    808         }
    809         fCalledAddStackData = false;
    810     } else {
    811         fClipStackData.appendf("<br>static void test(skiatest::Reporter* reporter,"
    812             " const char* filename) {<br>");
    813         addPathData(fCalledAddStackData ? devPath : fSaveDevPath, "path");
    814         addPathData(operand, "pathB");
    815         fClipStackData.appendf("%stestPathOp(reporter, path, pathB, %s, filename);<br>",
    816             kHTML4SpaceIndent, gOpStrs[static_cast<int>(elementOp)]);
    817         fClipStackData.appendf("}<br>");
    818         fCalledAddStackData = true;
    819     }
    820 }
    821 
    822 bool SkDebugCanvas::lastClipStackData(const SkPath& devPath) {
    823     if (fCalledAddStackData) {
    824         fClipStackData.appendf("<br>");
    825         addPathData(devPath, "pathOut");
    826         return true;
    827     }
    828     return false;
    829 }
    830