SkDebugCanvas.h revision 830b8793bb1646bb76817bdc228dd8e2a92bef7d
1
2/*
3 * Copyright 2012 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
9
10#ifndef SKDEBUGCANVAS_H_
11#define SKDEBUGCANVAS_H_
12
13#include "SkCanvas.h"
14#include "SkDrawCommand.h"
15#include "SkPicture.h"
16#include <vector>
17
18class SkDebugCanvas : public SkCanvas {
19public:
20    SkDebugCanvas(int width, int height);
21    ~SkDebugCanvas();
22
23    void toggleFilter(bool toggle);
24
25    /**
26        Executes all draw calls to the canvas.
27        @param canvas  The canvas being drawn to
28     */
29    void draw(SkCanvas* canvas);
30
31    /**
32        Executes the draw calls in the specified range.
33        @param canvas  The canvas being drawn to
34        @param i  The beginning of the range
35        @param j  The end of the range
36        TODO(chudy): Implement
37     */
38    void drawRange(SkCanvas* canvas, int i, int j);
39
40    /**
41        Executes the draw calls up to the specified index.
42        @param canvas  The canvas being drawn to
43        @param index  The index of the final command being executed
44     */
45    void drawTo(SkCanvas* canvas, int index);
46
47    /**
48        Returns the index of the last draw command to write to the pixel at (x,y)
49     */
50    int getCommandAtPoint(int x, int y, int index);
51
52    /**
53        Returns the draw command at the given index.
54        @param index  The index of the command
55     */
56    SkDrawCommand* getDrawCommandAt(int index);
57
58    /**
59        Returns information about the command at the given index.
60        @param index  The index of the command
61     */
62    std::vector<std::string>* getCommandInfoAt(int index);
63
64    /**
65        Returns the visibility of the command at the given index.
66        @param index  The index of the command
67     */
68    bool getDrawCommandVisibilityAt(int index);
69
70    /**
71        Returns the vector of draw commands
72     */
73    std::vector<SkDrawCommand*> getDrawCommands();
74
75    /**
76     * Returns the string vector of draw commands
77     */
78    std::vector<std::string>* getDrawCommandsAsStrings();
79
80    /**
81        Returns length of draw command vector.
82     */
83    int getSize() {
84        return commandVector.size();
85    }
86
87    /**
88        Toggles the visibility / execution of the draw command at index i with
89        the value of toggle.
90     */
91    void toggleCommand(int index, bool toggle);
92
93    void setBounds(int width, int height) {
94        fWidth = width;
95        fHeight = height;
96    }
97
98    void setUserOffset(SkIPoint offset) {
99        fUserOffset = offset;
100    }
101
102    void setUserScale(float scale) {
103        fUserScale = scale;
104    }
105
106////////////////////////////////////////////////////////////////////////////////
107// Inherited from SkCanvas
108////////////////////////////////////////////////////////////////////////////////
109
110    virtual void clear(SkColor) SK_OVERRIDE;
111
112    virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
113
114    virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
115
116    virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
117
118    virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
119
120    virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
121                            const SkPaint*) SK_OVERRIDE;
122
123    virtual void drawBitmapRect(const SkBitmap&, const SkIRect* src,
124                                const SkRect& dst, const SkPaint*) SK_OVERRIDE;
125
126    virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
127                                  const SkPaint*) SK_OVERRIDE;
128
129    virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
130                                const SkRect& dst, const SkPaint*) SK_OVERRIDE;
131
132    virtual void drawData(const void*, size_t) SK_OVERRIDE;
133
134    virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
135
136    virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
137
138    virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
139
140    virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
141                            const SkPaint&) SK_OVERRIDE;
142
143    virtual void drawPosText(const void* text, size_t byteLength,
144                             const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
145
146    virtual void drawPosTextH(const void* text, size_t byteLength,
147                      const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE;
148
149    virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
150
151    virtual void drawSprite(const SkBitmap&, int left, int top,
152                            const SkPaint*) SK_OVERRIDE;
153
154    virtual void drawText(const void* text, size_t byteLength, SkScalar x,
155                          SkScalar y, const SkPaint&) SK_OVERRIDE;
156
157    virtual void drawTextOnPath(const void* text, size_t byteLength,
158                            const SkPath& path, const SkMatrix* matrix,
159                                const SkPaint&) SK_OVERRIDE;
160
161    virtual void drawVertices(VertexMode, int vertexCount,
162                          const SkPoint vertices[], const SkPoint texs[],
163                          const SkColor colors[], SkXfermode*,
164                          const uint16_t indices[], int indexCount,
165                              const SkPaint&) SK_OVERRIDE;
166
167    virtual void restore() SK_OVERRIDE;
168
169    virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
170
171    virtual int save(SaveFlags) SK_OVERRIDE;
172
173    virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
174
175    virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
176
177    virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
178
179    virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
180
181    virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
182
183private:
184    typedef SkCanvas INHERITED;
185    std::vector<SkDrawCommand*> commandVector;
186    std::vector<SkDrawCommand*>::const_iterator it;
187    int fHeight;
188    int fWidth;
189    SkBitmap fBm;
190    bool fFilter;
191    int fIndex;
192    SkIPoint fUserOffset;
193    float fUserScale;
194
195    /**
196        Adds the command to the classes vector of commands.
197        @param command  The draw command for execution
198     */
199    void addDrawCommand(SkDrawCommand* command);
200
201    /**
202        Applies any panning and zooming the user has specified before
203        drawing anything else into the canvas.
204     */
205    void applyUserTransform(SkCanvas* canvas);
206};
207
208#endif
209