blob: d4fded7c1a8dae3bc5beb9b5c83cd11c08b448e6 [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; }
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000029 bool getMegaVizMode() const { return fMegaVizMode; }
chudy@google.com902ebe52012-06-29 14:21:22 +000030
31 /**
robertphillips@google.comf4741c12013-02-06 20:13:54 +000032 * Enable or disable overdraw visualization
33 */
34 void setOverdrawViz(bool overdrawViz) { fOverdrawViz = overdrawViz; }
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000035 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; }
robertphillips@google.comf4741c12013-02-06 20:13:54 +000041
42 /**
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000043 * Enable or disable texure filtering override
44 */
skia.committer@gmail.comf84ad8f2013-10-18 07:01:59 +000045 void overrideTexFiltering(bool overrideTexFiltering, SkPaint::FilterLevel level);
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000046
47 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000048 Executes all draw calls to the canvas.
49 @param canvas The canvas being drawn to
50 */
51 void draw(SkCanvas* canvas);
52
53 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000054 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
112 */
reed@google.com44699382013-10-31 17:28:30 +0000113 SK_ATTR_DEPRECATED("please use getDrawCommandAt and getSize instead")
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 /**
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000128 * Returns an array containing an offset (in the SkPicture) for each command
129 */
130 SkTDArray<size_t>* getDrawCommandOffsets() const;
131
132 /**
chudy@google.comf1414322012-07-03 20:28:14 +0000133 Returns length of draw command vector.
134 */
commit-bot@chromium.org0d4fe142013-07-15 22:47:14 +0000135 int getSize() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000136 return fCommandVector.count();
chudy@google.comf1414322012-07-03 20:28:14 +0000137 }
138
chudy@google.com902ebe52012-06-29 14:21:22 +0000139 /**
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
chudy@google.comb9ddd4e2012-07-10 14:14:50 +0000145 void setBounds(int width, int height) {
146 fWidth = width;
147 fHeight = height;
148 }
149
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000150 void setUserMatrix(SkMatrix matrix) {
151 fUserMatrix = matrix;
chudy@google.com830b8792012-08-01 15:57:52 +0000152 }
153
chudy@google.com902ebe52012-06-29 14:21:22 +0000154////////////////////////////////////////////////////////////////////////////////
155// Inherited from SkCanvas
156////////////////////////////////////////////////////////////////////////////////
157
158 virtual void clear(SkColor) SK_OVERRIDE;
159
chudy@google.com902ebe52012-06-29 14:21:22 +0000160 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top,
161 const SkPaint*) SK_OVERRIDE;
162
reed@google.com71121732012-09-18 15:14:33 +0000163 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000164 const SkRect& dst, const SkPaint* paint,
165 DrawBitmapRectFlags flags) SK_OVERRIDE;
chudy@google.com902ebe52012-06-29 14:21:22 +0000166
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
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000175 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
robertphillips@google.com67baba42013-01-02 20:20:31 +0000181 virtual void drawOval(const SkRect& oval, const SkPaint&) SK_OVERRIDE;
182
chudy@google.com902ebe52012-06-29 14:21:22 +0000183 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
184
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000185 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE;
186
chudy@google.com902ebe52012-06-29 14:21:22 +0000187 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
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000192 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE;
193
robertphillips@google.com67baba42013-01-02 20:20:31 +0000194 virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint) SK_OVERRIDE;
195
chudy@google.com902ebe52012-06-29 14:21:22 +0000196 virtual void drawSprite(const SkBitmap&, int left, int top,
197 const SkPaint*) SK_OVERRIDE;
198
chudy@google.com902ebe52012-06-29 14:21:22 +0000199 virtual void drawVertices(VertexMode, int vertexCount,
robertphillips@google.com67baba42013-01-02 20:20:31 +0000200 const SkPoint vertices[], const SkPoint texs[],
201 const SkColor colors[], SkXfermode*,
202 const uint16_t indices[], int indexCount,
chudy@google.com902ebe52012-06-29 14:21:22 +0000203 const SkPaint&) SK_OVERRIDE;
204
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000205 static const int kVizImageHeight = 256;
206 static const int kVizImageWidth = 256;
207
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000208 virtual bool isClipEmpty() const SK_OVERRIDE { return false; }
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000209 virtual bool isClipRect() const SK_OVERRIDE { return true; }
210#ifdef SK_SUPPORT_LEGACY_GETCLIPTYPE
skia.committer@gmail.com370a8992014-03-01 03:02:09 +0000211 virtual ClipType getClipType() const SK_OVERRIDE {
212 return kRect_ClipType;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000213 }
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +0000214#endif
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000215 virtual bool getClipBounds(SkRect* bounds) const SK_OVERRIDE {
216 if (NULL != bounds) {
skia.committer@gmail.com370a8992014-03-01 03:02:09 +0000217 bounds->setXYWH(0, 0,
218 SkIntToScalar(this->imageInfo().fWidth),
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000219 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
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000230protected:
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000231 virtual void willSave(SaveFlags) SK_OVERRIDE;
232 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
233 virtual void willRestore() SK_OVERRIDE;
234
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000235 virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
236 virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
237
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000238 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
reed@google.come0d9ce82014-04-23 04:00:17 +0000239 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;
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000247 virtual void onPushCull(const SkRect& cullRect) SK_OVERRIDE;
248 virtual void onPopCull() SK_OVERRIDE;
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000249
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000250 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
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000255 void markActiveCommands(int index);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000256
chudy@google.com902ebe52012-06-29 14:21:22 +0000257private:
robertphillips@google.com67baba42013-01-02 20:20:31 +0000258 SkTDArray<SkDrawCommand*> fCommandVector;
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000259 SkPicture* fPicture;
chudy@google.comb9ddd4e2012-07-10 14:14:50 +0000260 int fWidth;
commit-bot@chromium.org1735d662013-12-04 13:42:46 +0000261 int fHeight;
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000262 bool fFilter;
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000263 bool fMegaVizMode;
chudy@google.com830b8792012-08-01 15:57:52 +0000264 int fIndex;
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000265 SkMatrix fUserMatrix;
chudy@google.coma9e937c2012-08-03 17:32:05 +0000266 SkMatrix fMatrix;
267 SkIRect fClip;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000268
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000269 bool fOverdrawViz;
270 SkDrawFilter* fOverdrawFilter;
chudy@google.com902ebe52012-06-29 14:21:22 +0000271
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000272 bool fOverrideTexFiltering;
273 SkTexOverrideFilter* fTexOverrideFilter;
274
chudy@google.com902ebe52012-06-29 14:21:22 +0000275 /**
tomhudson@google.com0699e022012-11-27 16:09:42 +0000276 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
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000283 /**
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000284 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
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000289 /**
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000290 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
tomhudson@google.com0699e022012-11-27 16:09:42 +0000295 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000296 Adds the command to the classes vector of commands.
297 @param command The draw command for execution
298 */
299 void addDrawCommand(SkDrawCommand* command);
chudy@google.com830b8792012-08-01 15:57:52 +0000300
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);
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000306
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000307 size_t getOpID() const {
308 if (NULL != fPicture) {
309 return fPicture->EXPERIMENTAL_curOpID();
310 }
311 return 0;
312 }
313
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000314 typedef SkCanvas INHERITED;
chudy@google.com902ebe52012-06-29 14:21:22 +0000315};
316
317#endif