blob: cb4acc8b6640c450501fb3426c346c7fe744163b [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
commit-bot@chromium.org768ac852014-03-03 16:32:17 +000026 void toggleFilter(bool toggle) { fFilter = toggle; }
27
28 void setMegaVizMode(bool megaVizMode) { fMegaVizMode = megaVizMode; }
chudy@google.com902ebe52012-06-29 14:21:22 +000029
30 /**
robertphillips@google.comf4741c12013-02-06 20:13:54 +000031 * Enable or disable overdraw visualization
32 */
33 void setOverdrawViz(bool overdrawViz) { fOverdrawViz = overdrawViz; }
34
35 /**
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000036 * Enable or disable texure filtering override
37 */
skia.committer@gmail.comf84ad8f2013-10-18 07:01:59 +000038 void overrideTexFiltering(bool overrideTexFiltering, SkPaint::FilterLevel level);
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000039
40 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000041 Executes all draw calls to the canvas.
42 @param canvas The canvas being drawn to
43 */
44 void draw(SkCanvas* canvas);
45
46 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000047 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 */
chudy@google.com0b5bbb02012-07-31 19:55:32 +000051 void drawTo(SkCanvas* canvas, int index);
52
53 /**
chudy@google.coma9e937c2012-08-03 17:32:05 +000054 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 /**
chudy@google.com0b5bbb02012-07-31 19:55:32 +000068 Returns the index of the last draw command to write to the pixel at (x,y)
69 */
chudy@google.com830b8792012-08-01 15:57:52 +000070 int getCommandAtPoint(int x, int y, int index);
chudy@google.com902ebe52012-06-29 14:21:22 +000071
72 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000073 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 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000079 Returns the draw command at the given index.
80 @param index The index of the command
81 */
82 SkDrawCommand* getDrawCommandAt(int index);
83
84 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000085 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 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000092 Returns information about the command at the given index.
93 @param index The index of the command
94 */
chudy@google.com97cee972012-08-07 20:41:37 +000095 SkTDArray<SkString*>* getCommandInfo(int index);
chudy@google.com902ebe52012-06-29 14:21:22 +000096
97 /**
chudy@google.com7e4cfbf2012-07-17 15:40:51 +000098 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 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000104 Returns the vector of draw commands
105 */
reed@google.com44699382013-10-31 17:28:30 +0000106 SK_ATTR_DEPRECATED("please use getDrawCommandAt and getSize instead")
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000107 const SkTDArray<SkDrawCommand*>& getDrawCommands() const;
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000108
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000109 /**
110 Returns the vector of draw commands. Do not use this entry
111 point - it is going away!
112 */
113 SkTDArray<SkDrawCommand*>& getDrawCommands();
chudy@google.com902ebe52012-06-29 14:21:22 +0000114
115 /**
116 * Returns the string vector of draw commands
117 */
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000118 SkTArray<SkString>* getDrawCommandsAsStrings() const;
chudy@google.com902ebe52012-06-29 14:21:22 +0000119
120 /**
chudy@google.comf1414322012-07-03 20:28:14 +0000121 Returns length of draw command vector.
122 */
commit-bot@chromium.org0d4fe142013-07-15 22:47:14 +0000123 int getSize() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000124 return fCommandVector.count();
chudy@google.comf1414322012-07-03 20:28:14 +0000125 }
126
chudy@google.com902ebe52012-06-29 14:21:22 +0000127 /**
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
chudy@google.comb9ddd4e2012-07-10 14:14:50 +0000133 void setBounds(int width, int height) {
134 fWidth = width;
135 fHeight = height;
136 }
137
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000138 void setUserMatrix(SkMatrix matrix) {
139 fUserMatrix = matrix;
chudy@google.com830b8792012-08-01 15:57:52 +0000140 }
141
chudy@google.com902ebe52012-06-29 14:21:22 +0000142////////////////////////////////////////////////////////////////////////////////
143// Inherited from SkCanvas
144////////////////////////////////////////////////////////////////////////////////
145
146 virtual void clear(SkColor) SK_OVERRIDE;
147
chudy@google.com902ebe52012-06-29 14:21:22 +0000148 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
reed@google.com71121732012-09-18 15:14:33 +0000153 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000154 const SkRect& dst, const SkPaint* paint,
155 DrawBitmapRectFlags flags) SK_OVERRIDE;
chudy@google.com902ebe52012-06-29 14:21:22 +0000156
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
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000165 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
robertphillips@google.com67baba42013-01-02 20:20:31 +0000171 virtual void drawOval(const SkRect& oval, const SkPaint&) SK_OVERRIDE;
172
chudy@google.com902ebe52012-06-29 14:21:22 +0000173 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
174
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000175 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
176
chudy@google.com902ebe52012-06-29 14:21:22 +0000177 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,
skia.committer@gmail.com422188f2013-01-03 02:01:32 +0000186 const SkScalar xpos[], SkScalar constY,
robertphillips@google.com67baba42013-01-02 20:20:31 +0000187 const SkPaint&) SK_OVERRIDE;
chudy@google.com902ebe52012-06-29 14:21:22 +0000188
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000189 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
190
robertphillips@google.com67baba42013-01-02 20:20:31 +0000191 virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint) SK_OVERRIDE;
192
chudy@google.com902ebe52012-06-29 14:21:22 +0000193 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,
robertphillips@google.com67baba42013-01-02 20:20:31 +0000200 const SkPath& path, const SkMatrix* matrix,
chudy@google.com902ebe52012-06-29 14:21:22 +0000201 const SkPaint&) SK_OVERRIDE;
202
203 virtual void drawVertices(VertexMode, int vertexCount,
robertphillips@google.com67baba42013-01-02 20:20:31 +0000204 const SkPoint vertices[], const SkPoint texs[],
205 const SkColor colors[], SkXfermode*,
206 const uint16_t indices[], int indexCount,
chudy@google.com902ebe52012-06-29 14:21:22 +0000207 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
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000225 static const int kVizImageHeight = 256;
226 static const int kVizImageWidth = 256;
227
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000228 virtual bool isClipEmpty() const SK_OVERRIDE { return false; }
skia.committer@gmail.com370a8992014-03-01 03:02:09 +0000229 virtual ClipType getClipType() const SK_OVERRIDE {
230 return kRect_ClipType;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000231 }
232 virtual bool getClipBounds(SkRect* bounds) const SK_OVERRIDE {
233 if (NULL != bounds) {
skia.committer@gmail.com370a8992014-03-01 03:02:09 +0000234 bounds->setXYWH(0, 0,
235 SkIntToScalar(this->imageInfo().fWidth),
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000236 SkIntToScalar(this->imageInfo().fHeight));
237 }
238 return true;
239 }
240 virtual bool getClipDeviceBounds(SkIRect* bounds) const SK_OVERRIDE {
241 if (NULL != bounds) {
242 bounds->setLargest();
243 }
244 return true;
245 }
246
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000247protected:
248 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000249 virtual void onPushCull(const SkRect& cullRect) SK_OVERRIDE;
250 virtual void onPopCull() SK_OVERRIDE;
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000251
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000252 virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
253 virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
254 virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
255 virtual void onClipRegion(const SkRegion& region, SkRegion::Op) SK_OVERRIDE;
256
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000257 void markActiveSaveLayers(int index);
258
chudy@google.com902ebe52012-06-29 14:21:22 +0000259private:
robertphillips@google.com67baba42013-01-02 20:20:31 +0000260 SkTDArray<SkDrawCommand*> fCommandVector;
chudy@google.comb9ddd4e2012-07-10 14:14:50 +0000261 int fWidth;
commit-bot@chromium.org1735d662013-12-04 13:42:46 +0000262 int fHeight;
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000263 bool fFilter;
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000264 bool fMegaVizMode;
chudy@google.com830b8792012-08-01 15:57:52 +0000265 int fIndex;
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000266 SkMatrix fUserMatrix;
chudy@google.coma9e937c2012-08-03 17:32:05 +0000267 SkMatrix fMatrix;
268 SkIRect fClip;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000269
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000270 bool fOverdrawViz;
271 SkDrawFilter* fOverdrawFilter;
chudy@google.com902ebe52012-06-29 14:21:22 +0000272
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000273 bool fOverrideTexFiltering;
274 SkTexOverrideFilter* fTexOverrideFilter;
275
chudy@google.com902ebe52012-06-29 14:21:22 +0000276 /**
tomhudson@google.com0699e022012-11-27 16:09:42 +0000277 Number of unmatched save() calls at any point during a draw.
278 If there are any saveLayer() calls outstanding, we need to resolve
279 all of them, which in practice means resolving all save() calls,
280 to avoid corruption of our canvas.
281 */
282 int fOutstandingSaveCount;
283
284 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000285 Adds the command to the classes vector of commands.
286 @param command The draw command for execution
287 */
288 void addDrawCommand(SkDrawCommand* command);
chudy@google.com830b8792012-08-01 15:57:52 +0000289
290 /**
291 Applies any panning and zooming the user has specified before
292 drawing anything else into the canvas.
293 */
294 void applyUserTransform(SkCanvas* canvas);
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000295
296 typedef SkCanvas INHERITED;
chudy@google.com902ebe52012-06-29 14:21:22 +0000297};
298
299#endif