SkDebugCanvas.h revision 069a55a7fe2c3ae6f5049843b3fc1a167b51215c
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 "SkTArray.h"
17#include "SkString.h"
18
19class SkTexOverrideFilter;
20
21class SK_API SkDebugCanvas : public SkCanvas {
22public:
23    SkDebugCanvas(int width, int height);
24    virtual ~SkDebugCanvas();
25
26    void toggleFilter(bool toggle) { fFilter = toggle; }
27
28    void setMegaVizMode(bool megaVizMode) { fMegaVizMode = megaVizMode; }
29
30    /**
31     * Enable or disable overdraw visualization
32     */
33    void setOverdrawViz(bool overdrawViz) { fOverdrawViz = overdrawViz; }
34
35    /**
36     * Enable or disable texure filtering override
37     */
38    void overrideTexFiltering(bool overrideTexFiltering, SkPaint::FilterLevel level);
39
40    /**
41        Executes all draw calls to the canvas.
42        @param canvas  The canvas being drawn to
43     */
44    void draw(SkCanvas* canvas);
45
46    /**
47        Executes the draw calls up to the specified index.
48        @param canvas  The canvas being drawn to
49        @param index  The index of the final command being executed
50     */
51    void drawTo(SkCanvas* canvas, int index);
52
53    /**
54        Returns the most recently calculated transformation matrix
55     */
56    const SkMatrix& getCurrentMatrix() {
57        return fMatrix;
58    }
59
60    /**
61        Returns the most recently calculated clip
62     */
63    const SkIRect& getCurrentClip() {
64        return fClip;
65    }
66
67    /**
68        Returns the index of the last draw command to write to the pixel at (x,y)
69     */
70    int getCommandAtPoint(int x, int y, int index);
71
72    /**
73        Removes the command at the specified index
74        @param index  The index of the command to delete
75     */
76    void deleteDrawCommandAt(int index);
77
78    /**
79        Returns the draw command at the given index.
80        @param index  The index of the command
81     */
82    SkDrawCommand* getDrawCommandAt(int index);
83
84    /**
85        Sets the draw command for a given index.
86        @param index  The index to overwrite
87        @param command The new command
88     */
89    void setDrawCommandAt(int index, SkDrawCommand* command);
90
91    /**
92        Returns information about the command at the given index.
93        @param index  The index of the command
94     */
95    SkTDArray<SkString*>* getCommandInfo(int index);
96
97    /**
98        Returns the visibility of the command at the given index.
99        @param index  The index of the command
100     */
101    bool getDrawCommandVisibilityAt(int index);
102
103    /**
104        Returns the vector of draw commands
105     */
106    SK_ATTR_DEPRECATED("please use getDrawCommandAt and getSize instead")
107    const SkTDArray<SkDrawCommand*>& getDrawCommands() const;
108
109    /**
110        Returns the vector of draw commands. Do not use this entry
111        point - it is going away!
112     */
113    SkTDArray<SkDrawCommand*>& getDrawCommands();
114
115    /**
116     * Returns the string vector of draw commands
117     */
118    SkTArray<SkString>* getDrawCommandsAsStrings() const;
119
120    /**
121        Returns length of draw command vector.
122     */
123    int getSize() const {
124        return fCommandVector.count();
125    }
126
127    /**
128        Toggles the visibility / execution of the draw command at index i with
129        the value of toggle.
130     */
131    void toggleCommand(int index, bool toggle);
132
133    void setBounds(int width, int height) {
134        fWidth = width;
135        fHeight = height;
136    }
137
138    void setUserMatrix(SkMatrix matrix) {
139        fUserMatrix = matrix;
140    }
141
142////////////////////////////////////////////////////////////////////////////////
143// Inherited from SkCanvas
144////////////////////////////////////////////////////////////////////////////////
145
146    virtual void clear(SkColor) SK_OVERRIDE;
147
148    virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
149
150    virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
151                            const SkPaint*) SK_OVERRIDE;
152
153    virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
154                                      const SkRect& dst, const SkPaint* paint,
155                                      DrawBitmapRectFlags flags) SK_OVERRIDE;
156
157    virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
158                                  const SkPaint*) SK_OVERRIDE;
159
160    virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
161                                const SkRect& dst, const SkPaint*) SK_OVERRIDE;
162
163    virtual void drawData(const void*, size_t) SK_OVERRIDE;
164
165    virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
166
167    virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
168
169    virtual void endCommentGroup() SK_OVERRIDE;
170
171    virtual void drawOval(const SkRect& oval, const SkPaint&) SK_OVERRIDE;
172
173    virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
174
175    virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
176
177    virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
178
179    virtual void drawPoints(PointMode, size_t count, const SkPoint pts[],
180                            const SkPaint&) SK_OVERRIDE;
181
182    virtual void drawPosText(const void* text, size_t byteLength,
183                             const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
184
185    virtual void drawPosTextH(const void* text, size_t byteLength,
186                              const SkScalar xpos[], SkScalar constY,
187                              const SkPaint&) SK_OVERRIDE;
188
189    virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
190
191    virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint) SK_OVERRIDE;
192
193    virtual void drawSprite(const SkBitmap&, int left, int top,
194                            const SkPaint*) SK_OVERRIDE;
195
196    virtual void drawText(const void* text, size_t byteLength, SkScalar x,
197                          SkScalar y, const SkPaint&) SK_OVERRIDE;
198
199    virtual void drawTextOnPath(const void* text, size_t byteLength,
200                                const SkPath& path, const SkMatrix* matrix,
201                                const SkPaint&) SK_OVERRIDE;
202
203    virtual void drawVertices(VertexMode, int vertexCount,
204                              const SkPoint vertices[], const SkPoint texs[],
205                              const SkColor colors[], SkXfermode*,
206                              const uint16_t indices[], int indexCount,
207                              const SkPaint&) SK_OVERRIDE;
208
209    virtual void restore() SK_OVERRIDE;
210
211    virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
212
213    virtual int save(SaveFlags) SK_OVERRIDE;
214
215    virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
216
217    virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
218
219    virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
220
221    virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
222
223    virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
224
225    static const int kVizImageHeight = 256;
226    static const int kVizImageWidth = 256;
227
228    virtual bool isClipEmpty() const SK_OVERRIDE { return false; }
229    virtual bool isClipRect() const SK_OVERRIDE { return true; }
230#ifdef SK_SUPPORT_LEGACY_GETCLIPTYPE
231    virtual ClipType getClipType() const SK_OVERRIDE {
232        return kRect_ClipType;
233    }
234#endif
235    virtual bool getClipBounds(SkRect* bounds) const SK_OVERRIDE {
236        if (NULL != bounds) {
237            bounds->setXYWH(0, 0,
238                            SkIntToScalar(this->imageInfo().fWidth),
239                            SkIntToScalar(this->imageInfo().fHeight));
240        }
241        return true;
242    }
243    virtual bool getClipDeviceBounds(SkIRect* bounds) const SK_OVERRIDE {
244        if (NULL != bounds) {
245            bounds->setLargest();
246        }
247        return true;
248    }
249
250protected:
251    virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
252    virtual void onPushCull(const SkRect& cullRect) SK_OVERRIDE;
253    virtual void onPopCull() SK_OVERRIDE;
254
255    virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
256    virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
257    virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
258    virtual void onClipRegion(const SkRegion& region, SkRegion::Op) SK_OVERRIDE;
259
260    void markActiveCommands(int index);
261
262private:
263    SkTDArray<SkDrawCommand*> fCommandVector;
264    int fWidth;
265    int fHeight;
266    bool fFilter;
267    bool fMegaVizMode;
268    int fIndex;
269    SkMatrix fUserMatrix;
270    SkMatrix fMatrix;
271    SkIRect fClip;
272
273    bool fOverdrawViz;
274    SkDrawFilter* fOverdrawFilter;
275
276    bool fOverrideTexFiltering;
277    SkTexOverrideFilter* fTexOverrideFilter;
278
279    /**
280        Number of unmatched save() calls at any point during a draw.
281        If there are any saveLayer() calls outstanding, we need to resolve
282        all of them, which in practice means resolving all save() calls,
283        to avoid corruption of our canvas.
284    */
285    int fOutstandingSaveCount;
286
287    /**
288        The active saveLayer commands at a given point in the renderering.
289        Only used when "mega" visualization is enabled.
290    */
291    SkTDArray<SkDrawCommand*> fActiveLayers;
292
293    /**
294        The active cull commands at a given point in the rendering.
295        Only used when "mega" visualization is enabled.
296    */
297    SkTDArray<SkDrawCommand*> fActiveCulls;
298
299    /**
300        Adds the command to the classes vector of commands.
301        @param command  The draw command for execution
302     */
303    void addDrawCommand(SkDrawCommand* command);
304
305    /**
306        Applies any panning and zooming the user has specified before
307        drawing anything else into the canvas.
308     */
309    void applyUserTransform(SkCanvas* canvas);
310
311    typedef SkCanvas INHERITED;
312};
313
314#endif
315