blob: 93444ac10bce42c5013884f5fb82c087147dfeaf [file] [log] [blame]
chudy@google.com902ebe52012-06-29 14:21:22 +00001
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
chudy@google.com902ebe52012-06-29 14:21:22 +000013#include "SkCanvas.h"
14#include "SkDrawCommand.h"
15#include "SkPicture.h"
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +000016#include "SkTArray.h"
chudy@google.com97cee972012-08-07 20:41:37 +000017#include "SkString.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000018
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000019class SkTexOverrideFilter;
20
fmalita@google.com86681b32013-06-13 20:59:14 +000021class SK_API SkDebugCanvas : public SkCanvas {
chudy@google.com902ebe52012-06-29 14:21:22 +000022public:
chudy@google.com80a4a602012-07-30 18:54:07 +000023 SkDebugCanvas(int width, int height);
robertphillips@google.comf4741c12013-02-06 20:13:54 +000024 virtual ~SkDebugCanvas();
chudy@google.com902ebe52012-06-29 14:21:22 +000025
26 void toggleFilter(bool toggle);
27
28 /**
robertphillips@google.comf4741c12013-02-06 20:13:54 +000029 * Enable or disable overdraw visualization
30 */
31 void setOverdrawViz(bool overdrawViz) { fOverdrawViz = overdrawViz; }
32
33 /**
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000034 * Enable or disable texure filtering override
35 */
skia.committer@gmail.comf84ad8f2013-10-18 07:01:59 +000036 void overrideTexFiltering(bool overrideTexFiltering, SkPaint::FilterLevel level);
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000037
38 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000039 Executes all draw calls to the canvas.
40 @param canvas The canvas being drawn to
41 */
42 void draw(SkCanvas* canvas);
43
44 /**
45 Executes the draw calls in the specified range.
46 @param canvas The canvas being drawn to
47 @param i The beginning of the range
48 @param j The end of the range
49 TODO(chudy): Implement
50 */
51 void drawRange(SkCanvas* canvas, int i, int j);
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 */
chudy@google.com0b5bbb02012-07-31 19:55:32 +000058 void drawTo(SkCanvas* canvas, int index);
59
60 /**
chudy@google.coma9e937c2012-08-03 17:32:05 +000061 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 /**
chudy@google.com0b5bbb02012-07-31 19:55:32 +000075 Returns the index of the last draw command to write to the pixel at (x,y)
76 */
chudy@google.com830b8792012-08-01 15:57:52 +000077 int getCommandAtPoint(int x, int y, int index);
chudy@google.com902ebe52012-06-29 14:21:22 +000078
79 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000080 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 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000086 Returns the draw command at the given index.
87 @param index The index of the command
88 */
89 SkDrawCommand* getDrawCommandAt(int index);
90
91 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000092 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 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000099 Returns information about the command at the given index.
100 @param index The index of the command
101 */
chudy@google.com97cee972012-08-07 20:41:37 +0000102 SkTDArray<SkString*>* getCommandInfo(int index);
chudy@google.com902ebe52012-06-29 14:21:22 +0000103
104 /**
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000105 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 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000111 Returns the vector of draw commands
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000112 DEPRECATED: please use getDrawCommandAt and getSize instead
chudy@google.com902ebe52012-06-29 14:21:22 +0000113 */
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000114 const SkTDArray<SkDrawCommand*>& getDrawCommands() const;
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000115
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000116 /**
117 Returns the vector of draw commands. Do not use this entry
118 point - it is going away!
119 */
120 SkTDArray<SkDrawCommand*>& getDrawCommands();
chudy@google.com902ebe52012-06-29 14:21:22 +0000121
122 /**
123 * Returns the string vector of draw commands
124 */
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000125 SkTArray<SkString>* getDrawCommandsAsStrings() const;
chudy@google.com902ebe52012-06-29 14:21:22 +0000126
127 /**
chudy@google.comf1414322012-07-03 20:28:14 +0000128 Returns length of draw command vector.
129 */
commit-bot@chromium.org0d4fe142013-07-15 22:47:14 +0000130 int getSize() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000131 return fCommandVector.count();
chudy@google.comf1414322012-07-03 20:28:14 +0000132 }
133
chudy@google.com902ebe52012-06-29 14:21:22 +0000134 /**
135 Toggles the visibility / execution of the draw command at index i with
136 the value of toggle.
137 */
138 void toggleCommand(int index, bool toggle);
139
chudy@google.comb9ddd4e2012-07-10 14:14:50 +0000140 void setBounds(int width, int height) {
141 fWidth = width;
142 fHeight = height;
143 }
144
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000145 void setUserMatrix(SkMatrix matrix) {
146 fUserMatrix = matrix;
chudy@google.com830b8792012-08-01 15:57:52 +0000147 }
148
chudy@google.com902ebe52012-06-29 14:21:22 +0000149////////////////////////////////////////////////////////////////////////////////
150// Inherited from SkCanvas
151////////////////////////////////////////////////////////////////////////////////
152
153 virtual void clear(SkColor) SK_OVERRIDE;
154
155 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
156
157 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
158
robertphillips@google.com67baba42013-01-02 20:20:31 +0000159 virtual bool clipRRect(const SkRRect& rrect,
160 SkRegion::Op op = SkRegion::kIntersect_Op,
161 bool doAntiAlias = false) SK_OVERRIDE;
162
chudy@google.com902ebe52012-06-29 14:21:22 +0000163 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE;
164
165 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
166
167 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
168 const SkPaint*) SK_OVERRIDE;
169
reed@google.com71121732012-09-18 15:14:33 +0000170 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000171 const SkRect& dst, const SkPaint* paint,
172 DrawBitmapRectFlags flags) SK_OVERRIDE;
chudy@google.com902ebe52012-06-29 14:21:22 +0000173
174 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&,
175 const SkPaint*) SK_OVERRIDE;
176
177 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
178 const SkRect& dst, const SkPaint*) SK_OVERRIDE;
179
180 virtual void drawData(const void*, size_t) SK_OVERRIDE;
181
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000182 virtual void beginCommentGroup(const char* description) SK_OVERRIDE;
183
184 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
185
186 virtual void endCommentGroup() SK_OVERRIDE;
187
robertphillips@google.com67baba42013-01-02 20:20:31 +0000188 virtual void drawOval(const SkRect& oval, const SkPaint&) SK_OVERRIDE;
189
chudy@google.com902ebe52012-06-29 14:21:22 +0000190 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
191
chudy@google.com902ebe52012-06-29 14:21:22 +0000192 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 drawPosText(const void* text, size_t byteLength,
198 const SkPoint pos[], const SkPaint&) SK_OVERRIDE;
199
200 virtual void drawPosTextH(const void* text, size_t byteLength,
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000201 const SkScalar xpos[], SkScalar constY,
robertphillips@google.com67baba42013-01-02 20:20:31 +0000202 const SkPaint&) SK_OVERRIDE;
chudy@google.com902ebe52012-06-29 14:21:22 +0000203
robertphillips@google.com67baba42013-01-02 20:20:31 +0000204 virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint) SK_OVERRIDE;
205
chudy@google.com902ebe52012-06-29 14:21:22 +0000206 virtual void drawSprite(const SkBitmap&, int left, int top,
207 const SkPaint*) SK_OVERRIDE;
208
209 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
210 SkScalar y, const SkPaint&) SK_OVERRIDE;
211
212 virtual void drawTextOnPath(const void* text, size_t byteLength,
robertphillips@google.com67baba42013-01-02 20:20:31 +0000213 const SkPath& path, const SkMatrix* matrix,
chudy@google.com902ebe52012-06-29 14:21:22 +0000214 const SkPaint&) SK_OVERRIDE;
215
216 virtual void drawVertices(VertexMode, int vertexCount,
robertphillips@google.com67baba42013-01-02 20:20:31 +0000217 const SkPoint vertices[], const SkPoint texs[],
218 const SkColor colors[], SkXfermode*,
219 const uint16_t indices[], int indexCount,
chudy@google.com902ebe52012-06-29 14:21:22 +0000220 const SkPaint&) SK_OVERRIDE;
221
222 virtual void restore() SK_OVERRIDE;
223
224 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
225
226 virtual int save(SaveFlags) SK_OVERRIDE;
227
228 virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE;
229
230 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
231
232 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
233
234 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
235
236 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
237
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000238 static const int kVizImageHeight = 256;
239 static const int kVizImageWidth = 256;
240
bsalomon@google.comad254fe2013-10-22 13:19:12 +0000241protected:
242 virtual void onDrawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
243
244 virtual void onDrawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
245
chudy@google.com902ebe52012-06-29 14:21:22 +0000246private:
robertphillips@google.com67baba42013-01-02 20:20:31 +0000247 SkTDArray<SkDrawCommand*> fCommandVector;
chudy@google.comb9ddd4e2012-07-10 14:14:50 +0000248 int fHeight;
249 int fWidth;
chudy@google.com902ebe52012-06-29 14:21:22 +0000250 SkBitmap fBm;
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000251 bool fFilter;
chudy@google.com830b8792012-08-01 15:57:52 +0000252 int fIndex;
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000253 SkMatrix fUserMatrix;
chudy@google.coma9e937c2012-08-03 17:32:05 +0000254 SkMatrix fMatrix;
255 SkIRect fClip;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000256
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000257 bool fOverdrawViz;
258 SkDrawFilter* fOverdrawFilter;
chudy@google.com902ebe52012-06-29 14:21:22 +0000259
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000260 bool fOverrideTexFiltering;
261 SkTexOverrideFilter* fTexOverrideFilter;
262
chudy@google.com902ebe52012-06-29 14:21:22 +0000263 /**
tomhudson@google.com0699e022012-11-27 16:09:42 +0000264 Number of unmatched save() calls at any point during a draw.
265 If there are any saveLayer() calls outstanding, we need to resolve
266 all of them, which in practice means resolving all save() calls,
267 to avoid corruption of our canvas.
268 */
269 int fOutstandingSaveCount;
270
271 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000272 Adds the command to the classes vector of commands.
273 @param command The draw command for execution
274 */
275 void addDrawCommand(SkDrawCommand* command);
chudy@google.com830b8792012-08-01 15:57:52 +0000276
277 /**
278 Applies any panning and zooming the user has specified before
279 drawing anything else into the canvas.
280 */
281 void applyUserTransform(SkCanvas* canvas);
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000282
283 typedef SkCanvas INHERITED;
chudy@google.com902ebe52012-06-29 14:21:22 +0000284};
285
286#endif