Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright 2011 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 #ifndef SkDumpCanvas_DEFINED
      9 #define SkDumpCanvas_DEFINED
     10 
     11 #include "SkCanvas.h"
     12 #include "SkVertices.h"
     13 
     14 /** This class overrides all the draw methods on SkCanvas, and formats them
     15     as text, and then sends that to a Dumper helper object.
     16 
     17     Typical use might be to dump a display list to a log file to see what is
     18     being drawn.
     19  */
     20 class SkDumpCanvas : public SkCanvas {
     21 public:
     22     class Dumper;
     23 
     24     explicit SkDumpCanvas(Dumper* = nullptr);
     25     ~SkDumpCanvas() override;
     26 
     27     enum Verb {
     28         kNULL_Verb,
     29 
     30         kSave_Verb,
     31         kRestore_Verb,
     32 
     33         kMatrix_Verb,
     34 
     35         kClip_Verb,
     36 
     37         kDrawPaint_Verb,
     38         kDrawPoints_Verb,
     39         kDrawOval_Verb,
     40         kDrawArc_Verb,
     41         kDrawRect_Verb,
     42         kDrawRRect_Verb,
     43         kDrawDRRect_Verb,
     44         kDrawPath_Verb,
     45         kDrawBitmap_Verb,
     46         kDrawText_Verb,
     47         kDrawPicture_Verb,
     48         kDrawDrawable_Verb,
     49         kDrawVertices_Verb,
     50         kDrawPatch_Verb,
     51         kDrawData_Verb, // obsolete
     52         kDrawAnnotation_Verb,
     53 
     54         kCull_Verb
     55     };
     56 
     57     /** Subclasses of this are installed on the DumpCanvas, and then called for
     58         each drawing command.
     59      */
     60     class Dumper : public SkRefCnt {
     61     public:
     62 
     63 
     64         virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
     65                           const SkPaint*) = 0;
     66 
     67     private:
     68         typedef SkRefCnt INHERITED;
     69     };
     70 
     71     Dumper* getDumper() const { return fDumper; }
     72     void    setDumper(Dumper*);
     73 
     74     int getNestLevel() const { return fNestLevel; }
     75 
     76 protected:
     77     void willSave() override;
     78     SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
     79     void willRestore() override;
     80 
     81     void didConcat(const SkMatrix&) override;
     82     void didSetMatrix(const SkMatrix&) override;
     83 
     84     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
     85     virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
     86                             const SkPaint&) override;
     87     virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
     88                                const SkPaint&) override;
     89     virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
     90                                 SkScalar constY, const SkPaint&) override;
     91     virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
     92                                   const SkMatrix* matrix, const SkPaint&) override;
     93     void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
     94                            const SkRect* cull, const SkPaint& paint) override;
     95     virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
     96                                 const SkPaint& paint) override;
     97     virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
     98                              const SkPoint texCoords[4], SkBlendMode,
     99                              const SkPaint& paint) override;
    100 
    101     void onDrawPaint(const SkPaint&) override;
    102     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
    103     void onDrawRect(const SkRect&, const SkPaint&) override;
    104     void onDrawOval(const SkRect&, const SkPaint&) override;
    105     void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
    106     void onDrawRRect(const SkRRect&, const SkPaint&) override;
    107     void onDrawPath(const SkPath&, const SkPaint&) override;
    108     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
    109     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
    110                           SrcRectConstraint) override;
    111     void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
    112     void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
    113                          const SkPaint*, SrcRectConstraint) override;
    114     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
    115                           const SkPaint*) override;
    116     void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
    117 
    118     void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
    119     void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
    120     void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
    121     void onClipRegion(const SkRegion&, SkClipOp) override;
    122 
    123     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
    124     void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
    125     void onDrawAnnotation(const SkRect&, const char key[], SkData* value) override;
    126 
    127     static const char* EdgeStyleToAAString(ClipEdgeStyle edgeStyle);
    128 
    129 private:
    130     Dumper* fDumper;
    131     int     fNestLevel; // for nesting recursive elements like pictures
    132 
    133     void dump(Verb, const SkPaint*, const char format[], ...);
    134 
    135     typedef SkCanvas INHERITED;
    136 };
    137 
    138 /** Formats the draw commands, and send them to a function-pointer provided
    139     by the caller.
    140  */
    141 class SkFormatDumper : public SkDumpCanvas::Dumper {
    142 public:
    143     SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon);
    144 
    145     // override from baseclass that does the formatting, and in turn calls
    146     // the function pointer that was passed to the constructor
    147     virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
    148                       const SkPaint*) override;
    149 
    150 private:
    151     void (*fProc)(const char*, void*);
    152     void* fRefcon;
    153 
    154     typedef SkDumpCanvas::Dumper INHERITED;
    155 };
    156 
    157 /** Subclass of Dumper that dumps the drawing command to SkDebugf
    158  */
    159 class SkDebugfDumper : public SkFormatDumper {
    160 public:
    161     SkDebugfDumper();
    162 
    163 private:
    164     typedef SkFormatDumper INHERITED;
    165 };
    166 
    167 #endif
    168