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