blob: 3d9983b5046749106356e98ee760c85a4328b1d4 [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"
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000015#include "SkPathOps.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000016#include "SkPicture.h"
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +000017#include "SkTArray.h"
chudy@google.com97cee972012-08-07 20:41:37 +000018#include "SkString.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000019
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000020class SkTexOverrideFilter;
21
fmalita@google.com86681b32013-06-13 20:59:14 +000022class SK_API SkDebugCanvas : public SkCanvas {
chudy@google.com902ebe52012-06-29 14:21:22 +000023public:
chudy@google.com80a4a602012-07-30 18:54:07 +000024 SkDebugCanvas(int width, int height);
robertphillips@google.comf4741c12013-02-06 20:13:54 +000025 virtual ~SkDebugCanvas();
chudy@google.com902ebe52012-06-29 14:21:22 +000026
commit-bot@chromium.org768ac852014-03-03 16:32:17 +000027 void toggleFilter(bool toggle) { fFilter = toggle; }
28
29 void setMegaVizMode(bool megaVizMode) { fMegaVizMode = megaVizMode; }
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000030 bool getMegaVizMode() const { return fMegaVizMode; }
chudy@google.com902ebe52012-06-29 14:21:22 +000031
32 /**
robertphillips@google.comf4741c12013-02-06 20:13:54 +000033 * Enable or disable overdraw visualization
34 */
35 void setOverdrawViz(bool overdrawViz) { fOverdrawViz = overdrawViz; }
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000036 bool getOverdrawViz() const { return fOverdrawViz; }
37
38 void setOutstandingSaveCount(int saveCount) { fOutstandingSaveCount = saveCount; }
39 int getOutstandingSaveCount() const { return fOutstandingSaveCount; }
40
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000041 bool getAllowSimplifyClip() const { return fAllowSimplifyClip; }
42
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000043 void setPicture(SkPicture* picture) { fPicture = picture; }
robertphillips@google.comf4741c12013-02-06 20:13:54 +000044
45 /**
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000046 * Enable or disable texure filtering override
47 */
skia.committer@gmail.comf84ad8f2013-10-18 07:01:59 +000048 void overrideTexFiltering(bool overrideTexFiltering, SkPaint::FilterLevel level);
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000049
50 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000051 Executes all draw calls to the canvas.
52 @param canvas The canvas being drawn to
53 */
54 void draw(SkCanvas* canvas);
55
56 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000057 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 */
chudy@google.com0b5bbb02012-07-31 19:55:32 +000061 void drawTo(SkCanvas* canvas, int index);
62
63 /**
chudy@google.coma9e937c2012-08-03 17:32:05 +000064 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 /**
chudy@google.com0b5bbb02012-07-31 19:55:32 +000078 Returns the index of the last draw command to write to the pixel at (x,y)
79 */
chudy@google.com830b8792012-08-01 15:57:52 +000080 int getCommandAtPoint(int x, int y, int index);
chudy@google.com902ebe52012-06-29 14:21:22 +000081
82 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000083 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 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000089 Returns the draw command at the given index.
90 @param index The index of the command
91 */
92 SkDrawCommand* getDrawCommandAt(int index);
93
94 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000095 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 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000102 Returns information about the command at the given index.
103 @param index The index of the command
104 */
fmalita8c89c522014-11-08 16:18:56 -0800105 const SkTDArray<SkString*>* getCommandInfo(int index) const;
chudy@google.com902ebe52012-06-29 14:21:22 +0000106
107 /**
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000108 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 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000114 Returns the vector of draw commands
115 */
reed@google.com44699382013-10-31 17:28:30 +0000116 SK_ATTR_DEPRECATED("please use getDrawCommandAt and getSize instead")
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000117 const SkTDArray<SkDrawCommand*>& getDrawCommands() const;
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000118
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000119 /**
120 Returns the vector of draw commands. Do not use this entry
121 point - it is going away!
122 */
123 SkTDArray<SkDrawCommand*>& getDrawCommands();
chudy@google.com902ebe52012-06-29 14:21:22 +0000124
125 /**
chudy@google.comf1414322012-07-03 20:28:14 +0000126 Returns length of draw command vector.
127 */
commit-bot@chromium.org0d4fe142013-07-15 22:47:14 +0000128 int getSize() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000129 return fCommandVector.count();
chudy@google.comf1414322012-07-03 20:28:14 +0000130 }
131
chudy@google.com902ebe52012-06-29 14:21:22 +0000132 /**
133 Toggles the visibility / execution of the draw command at index i with
134 the value of toggle.
135 */
136 void toggleCommand(int index, bool toggle);
137
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000138 void setUserMatrix(SkMatrix matrix) {
139 fUserMatrix = matrix;
robertphillips70171682014-10-16 14:28:28 -0700140 fDrawNeedsReset = true;
chudy@google.com830b8792012-08-01 15:57:52 +0000141 }
142
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000143 SkString clipStackData() const { return fClipStackData; }
144
chudy@google.com902ebe52012-06-29 14:21:22 +0000145////////////////////////////////////////////////////////////////////////////////
146// Inherited from SkCanvas
147////////////////////////////////////////////////////////////////////////////////
148
mtklein72c9faa2015-01-09 10:06:39 -0800149 void beginCommentGroup(const char* description) SK_OVERRIDE;
150 void addComment(const char* kywd, const char* value) SK_OVERRIDE;
151 void endCommentGroup() SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000152
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000153 static const int kVizImageHeight = 256;
154 static const int kVizImageWidth = 256;
155
mtklein72c9faa2015-01-09 10:06:39 -0800156 bool isClipEmpty() const SK_OVERRIDE { return false; }
157 bool isClipRect() const SK_OVERRIDE { return true; }
158 bool getClipBounds(SkRect* bounds) const SK_OVERRIDE {
bsalomon49f085d2014-09-05 13:34:00 -0700159 if (bounds) {
skia.committer@gmail.com370a8992014-03-01 03:02:09 +0000160 bounds->setXYWH(0, 0,
reede5ea5002014-09-03 11:54:58 -0700161 SkIntToScalar(this->imageInfo().width()),
162 SkIntToScalar(this->imageInfo().height()));
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000163 }
164 return true;
165 }
mtklein72c9faa2015-01-09 10:06:39 -0800166 bool getClipDeviceBounds(SkIRect* bounds) const SK_OVERRIDE {
bsalomon49f085d2014-09-05 13:34:00 -0700167 if (bounds) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000168 bounds->setLargest();
169 }
170 return true;
171 }
172
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000173protected:
mtklein72c9faa2015-01-09 10:06:39 -0800174 void willSave() SK_OVERRIDE;
175 SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
176 void willRestore() SK_OVERRIDE;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000177
mtklein72c9faa2015-01-09 10:06:39 -0800178 void didConcat(const SkMatrix&) SK_OVERRIDE;
179 void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000180
mtklein72c9faa2015-01-09 10:06:39 -0800181 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
reed@google.come0d9ce82014-04-23 04:00:17 +0000182 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
183 const SkPaint&) SK_OVERRIDE;
184 virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
185 const SkPaint&) SK_OVERRIDE;
186 virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
187 SkScalar constY, const SkPaint&) SK_OVERRIDE;
188 virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
189 const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE;
fmalitab7425172014-08-26 07:56:44 -0700190 virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
191 const SkPaint& paint) SK_OVERRIDE;
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000192
reed41af9662015-01-05 07:49:08 -0800193 void onDrawPaint(const SkPaint&) SK_OVERRIDE;
194 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) SK_OVERRIDE;
195 void onDrawRect(const SkRect&, const SkPaint&) SK_OVERRIDE;
196 void onDrawOval(const SkRect&, const SkPaint&) SK_OVERRIDE;
197 void onDrawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE;
198 void onDrawPath(const SkPath&, const SkPaint&) SK_OVERRIDE;
199 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) SK_OVERRIDE;
200 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
201 DrawBitmapRectFlags flags) SK_OVERRIDE;
202 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) SK_OVERRIDE;
203 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
204 const SkPaint*) SK_OVERRIDE;
205 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
206 const SkPaint*) SK_OVERRIDE;
207 void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) SK_OVERRIDE;
208 void onDrawVertices(VertexMode vmode, int vertexCount,
209 const SkPoint vertices[], const SkPoint texs[],
210 const SkColor colors[], SkXfermode* xmode,
211 const uint16_t indices[], int indexCount,
212 const SkPaint&) SK_OVERRIDE;
213
mtklein72c9faa2015-01-09 10:06:39 -0800214 void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
215 void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
216 void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
217 void onClipRegion(const SkRegion& region, SkRegion::Op) SK_OVERRIDE;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000218
mtklein72c9faa2015-01-09 10:06:39 -0800219 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) SK_OVERRIDE;
robertphillips9b14f262014-06-04 05:40:44 -0700220
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000221 void markActiveCommands(int index);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000222
chudy@google.com902ebe52012-06-29 14:21:22 +0000223private:
robertphillips@google.com67baba42013-01-02 20:20:31 +0000224 SkTDArray<SkDrawCommand*> fCommandVector;
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000225 SkPicture* fPicture;
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000226 bool fFilter;
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000227 bool fMegaVizMode;
chudy@google.com830b8792012-08-01 15:57:52 +0000228 int fIndex;
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000229 SkMatrix fUserMatrix;
robertphillips70171682014-10-16 14:28:28 -0700230 bool fDrawNeedsReset; // fUserMatrix has changed so the incremental draw won't work
chudy@google.coma9e937c2012-08-03 17:32:05 +0000231 SkMatrix fMatrix;
232 SkIRect fClip;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000233
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000234 SkString fClipStackData;
235 bool fCalledAddStackData;
236 SkPath fSaveDevPath;
237
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000238 bool fOverdrawViz;
239 SkDrawFilter* fOverdrawFilter;
chudy@google.com902ebe52012-06-29 14:21:22 +0000240
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000241 bool fOverrideTexFiltering;
242 SkTexOverrideFilter* fTexOverrideFilter;
243
chudy@google.com902ebe52012-06-29 14:21:22 +0000244 /**
tomhudson@google.com0699e022012-11-27 16:09:42 +0000245 Number of unmatched save() calls at any point during a draw.
246 If there are any saveLayer() calls outstanding, we need to resolve
247 all of them, which in practice means resolving all save() calls,
248 to avoid corruption of our canvas.
249 */
250 int fOutstandingSaveCount;
251
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000252 /**
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000253 The active saveLayer commands at a given point in the renderering.
254 Only used when "mega" visualization is enabled.
255 */
256 SkTDArray<SkDrawCommand*> fActiveLayers;
257
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000258 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000259 Adds the command to the classes vector of commands.
260 @param command The draw command for execution
261 */
262 void addDrawCommand(SkDrawCommand* command);
chudy@google.com830b8792012-08-01 15:57:52 +0000263
264 /**
265 Applies any panning and zooming the user has specified before
266 drawing anything else into the canvas.
267 */
268 void applyUserTransform(SkCanvas* canvas);
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000269
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000270 size_t getOpID() const {
robertphillipsce4dd3d2014-07-07 13:46:35 -0700271#if 0
bsalomon49f085d2014-09-05 13:34:00 -0700272 if (fPicture) {
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000273 return fPicture->EXPERIMENTAL_curOpID();
274 }
robertphillipsce4dd3d2014-07-07 13:46:35 -0700275#endif
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000276 return 0;
277 }
278
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000279 void resetClipStackData() { fClipStackData.reset(); fCalledAddStackData = false; }
280
281 void addClipStackData(const SkPath& devPath, const SkPath& operand, SkRegion::Op elementOp);
282 void addPathData(const SkPath& path, const char* pathName);
283 bool lastClipStackData(const SkPath& devPath);
284 void outputConicPoints(const SkPoint* pts, SkScalar weight);
285 void outputPoints(const SkPoint* pts, int count);
286 void outputPointsCommon(const SkPoint* pts, int count);
287 void outputScalar(SkScalar num);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000288
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000289 typedef SkCanvas INHERITED;
chudy@google.com902ebe52012-06-29 14:21:22 +0000290};
291
292#endif