1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8
9#ifndef SKDEBUGCANVAS_H_
10#define SKDEBUGCANVAS_H_
11
12#include "SkCanvas.h"
13#include "SkDrawCommand.h"
14#include "SkPath.h"
15#include "SkPathOps.h"
16#include "SkPicture.h"
17#include "SkString.h"
18#include "SkTArray.h"
19#include "SkVertices.h"
20#include "UrlDataManager.h"
21
22class GrAuditTrail;
23class SkNWayCanvas;
24
25class SkDebugCanvas : public SkCanvas {
26public:
27    SkDebugCanvas(int width, int height);
28
29    ~SkDebugCanvas() override;
30
31    void toggleFilter(bool toggle) { fFilter = toggle; }
32
33    void setMegaVizMode(bool megaVizMode) { fMegaVizMode = megaVizMode; }
34
35    bool getMegaVizMode() const { return fMegaVizMode; }
36
37    /**
38     * Enable or disable overdraw visualization
39     */
40    void setOverdrawViz(bool overdrawViz);
41
42    bool getOverdrawViz() const { return fOverdrawViz; }
43
44    /**
45     * Set the color of the clip visualization. An alpha of zero renders the clip invisible.
46     */
47    void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; }
48
49    SkColor getClipVizColor() const { return fClipVizColor; }
50
51    void setDrawGpuOpBounds(bool drawGpuOpBounds) { fDrawGpuOpBounds = drawGpuOpBounds; }
52
53    bool getDrawGpuOpBounds() const { return fDrawGpuOpBounds; }
54
55    bool getAllowSimplifyClip() const { return fAllowSimplifyClip; }
56
57    void setPicture(SkPicture *picture) { fPicture = picture; }
58
59    /**
60     * Enable or disable texure filtering override
61     */
62    void overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality);
63
64    /**
65        Executes all draw calls to the canvas.
66        @param canvas  The canvas being drawn to
67     */
68    void draw(SkCanvas *canvas);
69
70    /**
71        Executes the draw calls up to the specified index.
72        @param canvas  The canvas being drawn to
73        @param index  The index of the final command being executed
74        @param m an optional Mth gpu op to highlight, or -1
75     */
76    void drawTo(SkCanvas *canvas, int index, int m = -1);
77
78    /**
79        Returns the most recently calculated transformation matrix
80     */
81    const SkMatrix &getCurrentMatrix() {
82        return fMatrix;
83    }
84
85    /**
86        Returns the most recently calculated clip
87     */
88    const SkIRect &getCurrentClip() {
89        return fClip;
90    }
91
92    /**
93        Returns the index of the last draw command to write to the pixel at (x,y)
94     */
95    int getCommandAtPoint(int x, int y, int index);
96
97    /**
98        Removes the command at the specified index
99        @param index  The index of the command to delete
100     */
101    void deleteDrawCommandAt(int index);
102
103    /**
104        Returns the draw command at the given index.
105        @param index  The index of the command
106     */
107    SkDrawCommand *getDrawCommandAt(int index);
108
109    /**
110        Sets the draw command for a given index.
111        @param index  The index to overwrite
112        @param command The new command
113     */
114    void setDrawCommandAt(int index, SkDrawCommand *command);
115
116    /**
117        Returns information about the command at the given index.
118        @param index  The index of the command
119     */
120    const SkTDArray<SkString *> *getCommandInfo(int index) const;
121
122    /**
123        Returns the visibility of the command at the given index.
124        @param index  The index of the command
125     */
126    bool getDrawCommandVisibilityAt(int index);
127
128    /**
129        Returns the vector of draw commands
130     */
131    SK_ATTR_DEPRECATED("please use getDrawCommandAt and getSize instead")
132    const SkTDArray<SkDrawCommand *> &getDrawCommands() const;
133
134    /**
135        Returns the vector of draw commands. Do not use this entry
136        point - it is going away!
137     */
138    SkTDArray<SkDrawCommand *> &getDrawCommands();
139
140    /**
141        Returns length of draw command vector.
142     */
143    int getSize() const {
144        return fCommandVector.count();
145    }
146
147    /**
148        Toggles the visibility / execution of the draw command at index i with
149        the value of toggle.
150     */
151    void toggleCommand(int index, bool toggle);
152
153    void setUserMatrix(SkMatrix matrix) {
154        fUserMatrix = matrix;
155    }
156
157    SkString clipStackData() const { return fClipStackData; }
158
159    /**
160        Returns a JSON object representing up to the Nth draw, where N is less than
161        SkDebugCanvas::getSize(). The encoder may use the UrlDataManager to store binary data such
162        as images, referring to them via URLs embedded in the JSON.
163     */
164    Json::Value toJSON(UrlDataManager &urlDataManager, int n, SkCanvas *);
165
166    Json::Value toJSONOpList(int n, SkCanvas*);
167
168    void detachCommands(SkTDArray<SkDrawCommand*>* dst) {
169        fCommandVector.swap(*dst);
170    }
171
172protected:
173    void willSave() override;
174
175    SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &) override;
176
177    void willRestore() override;
178
179    void didConcat(const SkMatrix &) override;
180
181    void didSetMatrix(const SkMatrix &) override;
182
183    void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
184    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
185    void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
186                    const SkPaint&) override;
187    void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
188                       const SkPaint&) override;
189    void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
190                        SkScalar constY, const SkPaint&) override;
191    void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
192                          const SkMatrix* matrix, const SkPaint&) override;
193    void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform[], const SkRect*,
194                           const SkPaint&) override;
195    void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
196                        const SkPaint& paint) override;
197
198    void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
199                     const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
200    void onDrawPaint(const SkPaint&) override;
201
202    void onDrawRect(const SkRect&, const SkPaint&) override;
203    void onDrawOval(const SkRect&, const SkPaint&) override;
204    void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
205    void onDrawRRect(const SkRRect&, const SkPaint&) override;
206    void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
207    void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
208    void onDrawPath(const SkPath&, const SkPaint&) override;
209    void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
210    void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
211                          SrcRectConstraint) override;
212    void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
213    void onDrawImageLattice(const SkImage* image, const Lattice& lattice,
214                            const SkRect& dst, const SkPaint* paint) override;
215    void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
216                         const SkPaint*, SrcRectConstraint) override;
217    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
218                          const SkPaint*) override;
219    void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
220    void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
221    void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
222    void onClipRegion(const SkRegion& region, SkClipOp) override;
223
224    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
225
226    void markActiveCommands(int index);
227
228private:
229    SkTDArray<SkDrawCommand*> fCommandVector;
230    SkPicture* fPicture;
231    bool fFilter;
232    bool fMegaVizMode;
233    SkMatrix fUserMatrix;
234    SkMatrix fMatrix;
235    SkIRect fClip;
236
237    SkString fClipStackData;
238    bool fCalledAddStackData;
239    SkPath fSaveDevPath;
240
241    bool fOverdrawViz;
242    bool fOverrideFilterQuality;
243    SkFilterQuality fFilterQuality;
244    SkColor fClipVizColor;
245    bool fDrawGpuOpBounds;
246
247    /**
248        The active saveLayer commands at a given point in the renderering.
249        Only used when "mega" visualization is enabled.
250    */
251    SkTDArray<SkDrawCommand*> fActiveLayers;
252
253    /**
254        Adds the command to the class' vector of commands.
255        @param command  The draw command for execution
256     */
257    void addDrawCommand(SkDrawCommand* command);
258
259    /**
260        Applies any panning and zooming the user has specified before
261        drawing anything else into the canvas.
262     */
263    void applyUserTransform(SkCanvas* canvas);
264
265    void resetClipStackData() { fClipStackData.reset(); fCalledAddStackData = false; }
266
267    void addClipStackData(const SkPath& devPath, const SkPath& operand, SkClipOp elementOp);
268    void addPathData(const SkPath& path, const char* pathName);
269    bool lastClipStackData(const SkPath& devPath);
270    void outputConicPoints(const SkPoint* pts, SkScalar weight);
271    void outputPoints(const SkPoint* pts, int count);
272    void outputPointsCommon(const SkPoint* pts, int count);
273    void outputScalar(SkScalar num);
274
275    GrAuditTrail* getAuditTrail(SkCanvas*);
276
277    void drawAndCollectOps(int n, SkCanvas*);
278    void cleanupAuditTrail(SkCanvas*);
279
280    typedef SkCanvas INHERITED;
281};
282
283#endif
284