Home | History | Annotate | Download | only in utils
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #ifndef SkDumpCanvas_DEFINED
      9 #define SkDumpCanvas_DEFINED
     10 
     11 #include "SkCanvas.h"
     12 
     13 #ifdef SK_DEVELOPER
     14 
     15 /** This class overrides all the draw methods on SkCanvas, and formats them
     16     as text, and then sends that to a Dumper helper object.
     17 
     18     Typical use might be to dump a display list to a log file to see what is
     19     being drawn.
     20  */
     21 class SkDumpCanvas : public SkCanvas {
     22 public:
     23     class Dumper;
     24 
     25     explicit SkDumpCanvas(Dumper* = 0);
     26     virtual ~SkDumpCanvas();
     27 
     28     enum Verb {
     29         kNULL_Verb,
     30 
     31         kSave_Verb,
     32         kRestore_Verb,
     33 
     34         kMatrix_Verb,
     35 
     36         kClip_Verb,
     37 
     38         kDrawPaint_Verb,
     39         kDrawPoints_Verb,
     40         kDrawOval_Verb,
     41         kDrawRect_Verb,
     42         kDrawRRect_Verb,
     43         kDrawDRRect_Verb,
     44         kDrawPath_Verb,
     45         kDrawBitmap_Verb,
     46         kDrawText_Verb,
     47         kDrawPicture_Verb,
     48         kDrawVertices_Verb,
     49         kDrawPatch_Verb,
     50         kDrawData_Verb,
     51 
     52         kBeginCommentGroup_Verb,
     53         kAddComment_Verb,
     54         kEndCommentGroup_Verb,
     55 
     56         kCull_Verb
     57     };
     58 
     59     /** Subclasses of this are installed on the DumpCanvas, and then called for
     60         each drawing command.
     61      */
     62     class Dumper : public SkRefCnt {
     63     public:
     64         SK_DECLARE_INST_COUNT(Dumper)
     65 
     66         virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
     67                           const SkPaint*) = 0;
     68 
     69     private:
     70         typedef SkRefCnt INHERITED;
     71     };
     72 
     73     Dumper* getDumper() const { return fDumper; }
     74     void    setDumper(Dumper*);
     75 
     76     int getNestLevel() const { return fNestLevel; }
     77 
     78     virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
     79     virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
     80                             const SkPaint& paint) SK_OVERRIDE;
     81     virtual void drawOval(const SkRect&, const SkPaint& paint) SK_OVERRIDE;
     82     virtual void drawRect(const SkRect&, const SkPaint& paint) SK_OVERRIDE;
     83     virtual void drawRRect(const SkRRect&, const SkPaint& paint) SK_OVERRIDE;
     84     virtual void drawPath(const SkPath& path, const SkPaint& paint) SK_OVERRIDE;
     85     virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
     86                             const SkPaint* paint) SK_OVERRIDE;
     87     virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
     88                                       const SkRect& dst, const SkPaint* paint,
     89                                       DrawBitmapRectFlags flags) SK_OVERRIDE;
     90     virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
     91                                   const SkPaint* paint) SK_OVERRIDE;
     92     virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
     93                             const SkPaint* paint) SK_OVERRIDE;
     94     virtual void drawVertices(VertexMode vmode, int vertexCount,
     95                               const SkPoint vertices[], const SkPoint texs[],
     96                               const SkColor colors[], SkXfermode* xmode,
     97                               const uint16_t indices[], int indexCount,
     98                               const SkPaint& paint) SK_OVERRIDE;
     99     virtual void drawData(const void*, size_t) SK_OVERRIDE;
    100     virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
    101     virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
    102     virtual void endCommentGroup() SK_OVERRIDE;
    103 
    104 protected:
    105     virtual void willSave() SK_OVERRIDE;
    106     virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
    107     virtual void willRestore() SK_OVERRIDE;
    108 
    109     virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
    110     virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
    111 
    112     virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
    113     virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
    114                             const SkPaint&) SK_OVERRIDE;
    115     virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
    116                                const SkPaint&) SK_OVERRIDE;
    117     virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
    118                                 SkScalar constY, const SkPaint&) SK_OVERRIDE;
    119     virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
    120                                   const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE;
    121     virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
    122                                 const SkPaint& paint) SK_OVERRIDE;
    123     virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
    124                              const SkPoint texCoords[4], SkXfermode* xmode,
    125                              const SkPaint& paint) SK_OVERRIDE;
    126     virtual void onPushCull(const SkRect& cullRect) SK_OVERRIDE;
    127     virtual void onPopCull() SK_OVERRIDE;
    128 
    129     virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
    130     virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
    131     virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
    132     virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
    133 
    134     virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
    135 
    136     static const char* EdgeStyleToAAString(ClipEdgeStyle edgeStyle);
    137 
    138 private:
    139     Dumper* fDumper;
    140     int     fNestLevel; // for nesting recursive elements like pictures
    141 
    142     void dump(Verb, const SkPaint*, const char format[], ...);
    143 
    144     typedef SkCanvas INHERITED;
    145 };
    146 
    147 /** Formats the draw commands, and send them to a function-pointer provided
    148     by the caller.
    149  */
    150 class SkFormatDumper : public SkDumpCanvas::Dumper {
    151 public:
    152     SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon);
    153 
    154     // override from baseclass that does the formatting, and in turn calls
    155     // the function pointer that was passed to the constructor
    156     virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
    157                       const SkPaint*) SK_OVERRIDE;
    158 
    159 private:
    160     void (*fProc)(const char*, void*);
    161     void* fRefcon;
    162 
    163     typedef SkDumpCanvas::Dumper INHERITED;
    164 };
    165 
    166 /** Subclass of Dumper that dumps the drawing command to SkDebugf
    167  */
    168 class SkDebugfDumper : public SkFormatDumper {
    169 public:
    170     SkDebugfDumper();
    171 
    172 private:
    173     typedef SkFormatDumper INHERITED;
    174 };
    175 
    176 #endif
    177 
    178 #endif
    179