blob: 4a81e26dd9227c4a973da430cc887e21914a4cb4 [file] [log] [blame]
chudy@google.com902ebe52012-06-29 14:21:22 +00001/*
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
chudy@google.com902ebe52012-06-29 14:21:22 +000012#include "SkCanvas.h"
13#include "SkDrawCommand.h"
bungemand3ebb482015-08-05 13:57:49 -070014#include "SkPath.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"
reedfb8c1fc2015-08-04 18:44:56 -070017#include "SkString.h"
bungemand3ebb482015-08-05 13:57:49 -070018#include "SkTArray.h"
ethannicholas402cd912016-02-10 12:57:30 -080019#include "UrlDataManager.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000020
joshualittae47aee2016-03-10 13:29:36 -080021class GrAuditTrail;
fmalita65cdb572015-03-26 07:24:48 -070022class SkNWayCanvas;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000023
Mike Klein0f61faa2016-10-11 16:26:57 -040024class SkDebugCanvas : public SkCanvas {
chudy@google.com902ebe52012-06-29 14:21:22 +000025public:
chudy@google.com80a4a602012-07-30 18:54:07 +000026 SkDebugCanvas(int width, int height);
vjiaoblack95302da2016-07-21 10:25:54 -070027
robertphillips@google.comf4741c12013-02-06 20:13:54 +000028 virtual ~SkDebugCanvas();
chudy@google.com902ebe52012-06-29 14:21:22 +000029
commit-bot@chromium.org768ac852014-03-03 16:32:17 +000030 void toggleFilter(bool toggle) { fFilter = toggle; }
31
32 void setMegaVizMode(bool megaVizMode) { fMegaVizMode = megaVizMode; }
vjiaoblack95302da2016-07-21 10:25:54 -070033
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000034 bool getMegaVizMode() const { return fMegaVizMode; }
chudy@google.com902ebe52012-06-29 14:21:22 +000035
36 /**
robertphillips@google.comf4741c12013-02-06 20:13:54 +000037 * Enable or disable overdraw visualization
38 */
fmalita65cdb572015-03-26 07:24:48 -070039 void setOverdrawViz(bool overdrawViz);
vjiaoblack95302da2016-07-21 10:25:54 -070040
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000041 bool getOverdrawViz() const { return fOverdrawViz; }
42
ethannicholas0a0520a2016-02-12 12:06:53 -080043 /**
44 * Set the color of the clip visualization. An alpha of zero renders the clip invisible.
45 */
46 void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; }
vjiaoblack95302da2016-07-21 10:25:54 -070047
ethannicholas0a0520a2016-02-12 12:06:53 -080048 SkColor getClipVizColor() const { return fClipVizColor; }
49
Brian Salomon144a5c52016-12-20 16:48:59 -050050 void setDrawGpuOpBounds(bool drawGpuOpBounds) { fDrawGpuOpBounds = drawGpuOpBounds; }
joshualitt10d8fc22016-02-29 11:15:06 -080051
Brian Salomon144a5c52016-12-20 16:48:59 -050052 bool getDrawGpuOpBounds() const { return fDrawGpuOpBounds; }
joshualitt5d5207a2016-02-29 12:46:04 -080053
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000054 bool getAllowSimplifyClip() const { return fAllowSimplifyClip; }
55
vjiaoblack95302da2016-07-21 10:25:54 -070056 void setPicture(SkPicture *picture) { fPicture = picture; }
robertphillips@google.comf4741c12013-02-06 20:13:54 +000057
58 /**
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000059 * Enable or disable texure filtering override
60 */
reed93a12152015-03-16 10:08:34 -070061 void overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality);
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000062
63 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000064 Executes all draw calls to the canvas.
65 @param canvas The canvas being drawn to
66 */
vjiaoblack95302da2016-07-21 10:25:54 -070067 void draw(SkCanvas *canvas);
chudy@google.com902ebe52012-06-29 14:21:22 +000068
69 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000070 Executes the draw calls up to the specified index.
71 @param canvas The canvas being drawn to
72 @param index The index of the final command being executed
Brian Salomon144a5c52016-12-20 16:48:59 -050073 @param m an optional Mth gpu op to highlight, or -1
chudy@google.com902ebe52012-06-29 14:21:22 +000074 */
vjiaoblack95302da2016-07-21 10:25:54 -070075 void drawTo(SkCanvas *canvas, int index, int m = -1);
chudy@google.com0b5bbb02012-07-31 19:55:32 +000076
77 /**
chudy@google.coma9e937c2012-08-03 17:32:05 +000078 Returns the most recently calculated transformation matrix
79 */
vjiaoblack95302da2016-07-21 10:25:54 -070080 const SkMatrix &getCurrentMatrix() {
chudy@google.coma9e937c2012-08-03 17:32:05 +000081 return fMatrix;
82 }
83
84 /**
85 Returns the most recently calculated clip
86 */
vjiaoblack95302da2016-07-21 10:25:54 -070087 const SkIRect &getCurrentClip() {
chudy@google.coma9e937c2012-08-03 17:32:05 +000088 return fClip;
89 }
90
91 /**
chudy@google.com0b5bbb02012-07-31 19:55:32 +000092 Returns the index of the last draw command to write to the pixel at (x,y)
93 */
chudy@google.com830b8792012-08-01 15:57:52 +000094 int getCommandAtPoint(int x, int y, int index);
chudy@google.com902ebe52012-06-29 14:21:22 +000095
96 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000097 Removes the command at the specified index
98 @param index The index of the command to delete
99 */
100 void deleteDrawCommandAt(int index);
101
102 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000103 Returns the draw command at the given index.
104 @param index The index of the command
105 */
vjiaoblack95302da2016-07-21 10:25:54 -0700106 SkDrawCommand *getDrawCommandAt(int index);
chudy@google.com902ebe52012-06-29 14:21:22 +0000107
108 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000109 Sets the draw command for a given index.
110 @param index The index to overwrite
111 @param command The new command
112 */
vjiaoblack95302da2016-07-21 10:25:54 -0700113 void setDrawCommandAt(int index, SkDrawCommand *command);
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000114
115 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000116 Returns information about the command at the given index.
117 @param index The index of the command
118 */
vjiaoblack95302da2016-07-21 10:25:54 -0700119 const SkTDArray<SkString *> *getCommandInfo(int index) const;
chudy@google.com902ebe52012-06-29 14:21:22 +0000120
121 /**
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000122 Returns the visibility of the command at the given index.
123 @param index The index of the command
124 */
125 bool getDrawCommandVisibilityAt(int index);
126
127 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000128 Returns the vector of draw commands
129 */
reed@google.com44699382013-10-31 17:28:30 +0000130 SK_ATTR_DEPRECATED("please use getDrawCommandAt and getSize instead")
vjiaoblack95302da2016-07-21 10:25:54 -0700131 const SkTDArray<SkDrawCommand *> &getDrawCommands() const;
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000132
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000133 /**
134 Returns the vector of draw commands. Do not use this entry
135 point - it is going away!
136 */
vjiaoblack95302da2016-07-21 10:25:54 -0700137 SkTDArray<SkDrawCommand *> &getDrawCommands();
chudy@google.com902ebe52012-06-29 14:21:22 +0000138
139 /**
chudy@google.comf1414322012-07-03 20:28:14 +0000140 Returns length of draw command vector.
141 */
commit-bot@chromium.org0d4fe142013-07-15 22:47:14 +0000142 int getSize() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000143 return fCommandVector.count();
chudy@google.comf1414322012-07-03 20:28:14 +0000144 }
145
chudy@google.com902ebe52012-06-29 14:21:22 +0000146 /**
147 Toggles the visibility / execution of the draw command at index i with
148 the value of toggle.
149 */
150 void toggleCommand(int index, bool toggle);
151
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000152 void setUserMatrix(SkMatrix matrix) {
153 fUserMatrix = matrix;
chudy@google.com830b8792012-08-01 15:57:52 +0000154 }
155
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000156 SkString clipStackData() const { return fClipStackData; }
157
ethannicholas402cd912016-02-10 12:57:30 -0800158 /**
ethannicholas0a0520a2016-02-12 12:06:53 -0800159 Returns a JSON object representing up to the Nth draw, where N is less than
halcanary9d524f22016-03-29 09:03:52 -0700160 SkDebugCanvas::getSize(). The encoder may use the UrlDataManager to store binary data such
ethannicholas0a0520a2016-02-12 12:06:53 -0800161 as images, referring to them via URLs embedded in the JSON.
ethannicholas402cd912016-02-10 12:57:30 -0800162 */
vjiaoblack95302da2016-07-21 10:25:54 -0700163 Json::Value toJSON(UrlDataManager &urlDataManager, int n, SkCanvas *);
ethannicholas402cd912016-02-10 12:57:30 -0800164
Brian Salomon144a5c52016-12-20 16:48:59 -0500165 Json::Value toJSONOpList(int n, SkCanvas*);
joshualittae47aee2016-03-10 13:29:36 -0800166
Brian Salomon144a5c52016-12-20 16:48:59 -0500167 ////////////////////////////////////////////////////////////////////////////////
168 // Inherited from SkCanvas
169 ////////////////////////////////////////////////////////////////////////////////
chudy@google.com902ebe52012-06-29 14:21:22 +0000170
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000171 static const int kVizImageHeight = 256;
172 static const int kVizImageWidth = 256;
173
mtklein36352bf2015-03-25 18:17:31 -0700174 bool isClipEmpty() const override { return false; }
vjiaoblack95302da2016-07-21 10:25:54 -0700175
mtklein36352bf2015-03-25 18:17:31 -0700176 bool isClipRect() const override { return true; }
vjiaoblack95302da2016-07-21 10:25:54 -0700177
178 bool getClipBounds(SkRect *bounds) const override {
bsalomon49f085d2014-09-05 13:34:00 -0700179 if (bounds) {
skia.committer@gmail.com370a8992014-03-01 03:02:09 +0000180 bounds->setXYWH(0, 0,
reede5ea5002014-09-03 11:54:58 -0700181 SkIntToScalar(this->imageInfo().width()),
182 SkIntToScalar(this->imageInfo().height()));
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000183 }
184 return true;
185 }
vjiaoblack95302da2016-07-21 10:25:54 -0700186
187 bool getClipDeviceBounds(SkIRect *bounds) const override {
bsalomon49f085d2014-09-05 13:34:00 -0700188 if (bounds) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000189 bounds->setLargest();
190 }
191 return true;
192 }
193
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000194protected:
mtklein36352bf2015-03-25 18:17:31 -0700195 void willSave() override;
vjiaoblack95302da2016-07-21 10:25:54 -0700196
197 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &) override;
198
mtklein36352bf2015-03-25 18:17:31 -0700199 void willRestore() override;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000200
vjiaoblack95302da2016-07-21 10:25:54 -0700201 void didConcat(const SkMatrix &) override;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000202
vjiaoblack95302da2016-07-21 10:25:54 -0700203 void didSetMatrix(const SkMatrix &) override;
204
205#ifdef SK_EXPERIMENTAL_SHADOWING
Mike Klein0f61faa2016-10-11 16:26:57 -0400206 void didTranslateZ(SkScalar) override;
vjiaoblack95302da2016-07-21 10:25:54 -0700207#else
208 void didTranslateZ(SkScalar);
209#endif
vjiaoblacke5de1302016-07-13 14:05:28 -0700210
reed97660cc2016-06-28 18:54:19 -0700211 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
mtklein36352bf2015-03-25 18:17:31 -0700212 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800213 void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700214 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800215 void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
mtklein36352bf2015-03-25 18:17:31 -0700216 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800217 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
mtklein36352bf2015-03-25 18:17:31 -0700218 SkScalar constY, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800219 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700220 const SkMatrix* matrix, const SkPaint&) override;
reed45561a02016-07-07 12:47:17 -0700221 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform[], const SkRect*,
222 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800223 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700224 const SkPaint& paint) override;
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000225
robertphillips9bafc302015-02-13 11:13:00 -0800226 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400227 const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
mtklein36352bf2015-03-25 18:17:31 -0700228 void onDrawPaint(const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800229
mtklein36352bf2015-03-25 18:17:31 -0700230 void onDrawRect(const SkRect&, const SkPaint&) override;
231 void onDrawOval(const SkRect&, const SkPaint&) override;
bsalomonac3aa242016-08-19 11:25:19 -0700232 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700233 void onDrawRRect(const SkRRect&, const SkPaint&) override;
234 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800235 void onDrawVertices(VertexMode vmode, int vertexCount,
236 const SkPoint vertices[], const SkPoint texs[],
Mike Reedfaba3712016-11-03 14:45:31 -0400237 const SkColor colors[], SkBlendMode,
robertphillips9bafc302015-02-13 11:13:00 -0800238 const uint16_t indices[], int indexCount,
mtklein36352bf2015-03-25 18:17:31 -0700239 const SkPaint&) override;
240 void onDrawPath(const SkPath&, const SkPaint&) override;
241 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800242 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
reed562fe472015-07-28 07:35:14 -0700243 SrcRectConstraint) override;
mtklein36352bf2015-03-25 18:17:31 -0700244 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
Stan Ilievac42aeb2017-01-12 16:20:50 -0500245 void onDrawImageLattice(const SkImage* image, const Lattice& lattice,
246 const SkRect& dst, const SkPaint* paint) override;
reed41af9662015-01-05 07:49:08 -0800247 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700248 const SkPaint*, SrcRectConstraint) override;
reed41af9662015-01-05 07:49:08 -0800249 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
mtklein36352bf2015-03-25 18:17:31 -0700250 const SkPaint*) override;
Mike Reedc1f77742016-12-09 09:00:50 -0500251 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
252 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
253 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
254 void onClipRegion(const SkRegion& region, SkClipOp) override;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000255
mtklein36352bf2015-03-25 18:17:31 -0700256 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
robertphillips9b14f262014-06-04 05:40:44 -0700257
vjiaoblack95302da2016-07-21 10:25:54 -0700258#ifdef SK_EXPERIMENTAL_SHADOWING
259 void onDrawShadowedPicture(const SkPicture*,
260 const SkMatrix*,
vjiaoblacke6f5d562016-08-25 06:30:23 -0700261 const SkPaint*,
262 const SkShadowParams& params) override;
vjiaoblack95302da2016-07-21 10:25:54 -0700263#else
264 void onDrawShadowedPicture(const SkPicture*,
265 const SkMatrix*,
vjiaoblacke6f5d562016-08-25 06:30:23 -0700266 const SkPaint*,
267 const SkShadowParams& params);
vjiaoblack95302da2016-07-21 10:25:54 -0700268#endif
269
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000270 void markActiveCommands(int index);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000271
chudy@google.com902ebe52012-06-29 14:21:22 +0000272private:
robertphillips@google.com67baba42013-01-02 20:20:31 +0000273 SkTDArray<SkDrawCommand*> fCommandVector;
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000274 SkPicture* fPicture;
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000275 bool fFilter;
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000276 bool fMegaVizMode;
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000277 SkMatrix fUserMatrix;
chudy@google.coma9e937c2012-08-03 17:32:05 +0000278 SkMatrix fMatrix;
279 SkIRect fClip;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000280
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000281 SkString fClipStackData;
282 bool fCalledAddStackData;
283 SkPath fSaveDevPath;
284
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000285 bool fOverdrawViz;
fmalita65cdb572015-03-26 07:24:48 -0700286 bool fOverrideFilterQuality;
287 SkFilterQuality fFilterQuality;
ethannicholas0a0520a2016-02-12 12:06:53 -0800288 SkColor fClipVizColor;
Brian Salomon144a5c52016-12-20 16:48:59 -0500289 bool fDrawGpuOpBounds;
chudy@google.com902ebe52012-06-29 14:21:22 +0000290
291 /**
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000292 The active saveLayer commands at a given point in the renderering.
293 Only used when "mega" visualization is enabled.
294 */
295 SkTDArray<SkDrawCommand*> fActiveLayers;
296
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000297 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000298 Adds the command to the classes vector of commands.
299 @param command The draw command for execution
300 */
301 void addDrawCommand(SkDrawCommand* command);
chudy@google.com830b8792012-08-01 15:57:52 +0000302
303 /**
304 Applies any panning and zooming the user has specified before
305 drawing anything else into the canvas.
306 */
307 void applyUserTransform(SkCanvas* canvas);
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000308
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000309 void resetClipStackData() { fClipStackData.reset(); fCalledAddStackData = false; }
310
Mike Reedc1f77742016-12-09 09:00:50 -0500311 void addClipStackData(const SkPath& devPath, const SkPath& operand, SkClipOp elementOp);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000312 void addPathData(const SkPath& path, const char* pathName);
313 bool lastClipStackData(const SkPath& devPath);
314 void outputConicPoints(const SkPoint* pts, SkScalar weight);
315 void outputPoints(const SkPoint* pts, int count);
316 void outputPointsCommon(const SkPoint* pts, int count);
317 void outputScalar(SkScalar num);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000318
joshualittae47aee2016-03-10 13:29:36 -0800319 GrAuditTrail* getAuditTrail(SkCanvas*);
320
Brian Salomon144a5c52016-12-20 16:48:59 -0500321 void drawAndCollectOps(int n, SkCanvas*);
joshualittae47aee2016-03-10 13:29:36 -0800322 void cleanupAuditTrail(SkCanvas*);
fmalita65cdb572015-03-26 07:24:48 -0700323
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000324 typedef SkCanvas INHERITED;
chudy@google.com902ebe52012-06-29 14:21:22 +0000325};
326
327#endif