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