blob: 0f2ae514dd67dadea37903357d3012f23268e73b [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"
Brian Salomon199fb872017-02-06 09:41:10 -050019#include "SkVertices.h"
ethannicholas402cd912016-02-10 12:57:30 -080020#include "UrlDataManager.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000021
joshualittae47aee2016-03-10 13:29:36 -080022class GrAuditTrail;
fmalita65cdb572015-03-26 07:24:48 -070023class SkNWayCanvas;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000024
Mike Klein0f61faa2016-10-11 16:26:57 -040025class SkDebugCanvas : public SkCanvas {
chudy@google.com902ebe52012-06-29 14:21:22 +000026public:
chudy@google.com80a4a602012-07-30 18:54:07 +000027 SkDebugCanvas(int width, int height);
vjiaoblack95302da2016-07-21 10:25:54 -070028
robertphillips@google.comf4741c12013-02-06 20:13:54 +000029 virtual ~SkDebugCanvas();
chudy@google.com902ebe52012-06-29 14:21:22 +000030
commit-bot@chromium.org768ac852014-03-03 16:32:17 +000031 void toggleFilter(bool toggle) { fFilter = toggle; }
32
33 void setMegaVizMode(bool megaVizMode) { fMegaVizMode = megaVizMode; }
vjiaoblack95302da2016-07-21 10:25:54 -070034
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000035 bool getMegaVizMode() const { return fMegaVizMode; }
chudy@google.com902ebe52012-06-29 14:21:22 +000036
37 /**
robertphillips@google.comf4741c12013-02-06 20:13:54 +000038 * Enable or disable overdraw visualization
39 */
fmalita65cdb572015-03-26 07:24:48 -070040 void setOverdrawViz(bool overdrawViz);
vjiaoblack95302da2016-07-21 10:25:54 -070041
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000042 bool getOverdrawViz() const { return fOverdrawViz; }
43
ethannicholas0a0520a2016-02-12 12:06:53 -080044 /**
45 * Set the color of the clip visualization. An alpha of zero renders the clip invisible.
46 */
47 void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; }
vjiaoblack95302da2016-07-21 10:25:54 -070048
ethannicholas0a0520a2016-02-12 12:06:53 -080049 SkColor getClipVizColor() const { return fClipVizColor; }
50
Brian Salomon144a5c52016-12-20 16:48:59 -050051 void setDrawGpuOpBounds(bool drawGpuOpBounds) { fDrawGpuOpBounds = drawGpuOpBounds; }
joshualitt10d8fc22016-02-29 11:15:06 -080052
Brian Salomon144a5c52016-12-20 16:48:59 -050053 bool getDrawGpuOpBounds() const { return fDrawGpuOpBounds; }
joshualitt5d5207a2016-02-29 12:46:04 -080054
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000055 bool getAllowSimplifyClip() const { return fAllowSimplifyClip; }
56
vjiaoblack95302da2016-07-21 10:25:54 -070057 void setPicture(SkPicture *picture) { fPicture = picture; }
robertphillips@google.comf4741c12013-02-06 20:13:54 +000058
59 /**
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000060 * Enable or disable texure filtering override
61 */
reed93a12152015-03-16 10:08:34 -070062 void overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality);
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000063
64 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000065 Executes all draw calls to the canvas.
66 @param canvas The canvas being drawn to
67 */
vjiaoblack95302da2016-07-21 10:25:54 -070068 void draw(SkCanvas *canvas);
chudy@google.com902ebe52012-06-29 14:21:22 +000069
70 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000071 Executes the draw calls up to the specified index.
72 @param canvas The canvas being drawn to
73 @param index The index of the final command being executed
Brian Salomon144a5c52016-12-20 16:48:59 -050074 @param m an optional Mth gpu op to highlight, or -1
chudy@google.com902ebe52012-06-29 14:21:22 +000075 */
vjiaoblack95302da2016-07-21 10:25:54 -070076 void drawTo(SkCanvas *canvas, int index, int m = -1);
chudy@google.com0b5bbb02012-07-31 19:55:32 +000077
78 /**
chudy@google.coma9e937c2012-08-03 17:32:05 +000079 Returns the most recently calculated transformation matrix
80 */
vjiaoblack95302da2016-07-21 10:25:54 -070081 const SkMatrix &getCurrentMatrix() {
chudy@google.coma9e937c2012-08-03 17:32:05 +000082 return fMatrix;
83 }
84
85 /**
86 Returns the most recently calculated clip
87 */
vjiaoblack95302da2016-07-21 10:25:54 -070088 const SkIRect &getCurrentClip() {
chudy@google.coma9e937c2012-08-03 17:32:05 +000089 return fClip;
90 }
91
92 /**
chudy@google.com0b5bbb02012-07-31 19:55:32 +000093 Returns the index of the last draw command to write to the pixel at (x,y)
94 */
chudy@google.com830b8792012-08-01 15:57:52 +000095 int getCommandAtPoint(int x, int y, int index);
chudy@google.com902ebe52012-06-29 14:21:22 +000096
97 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000098 Removes the command at the specified index
99 @param index The index of the command to delete
100 */
101 void deleteDrawCommandAt(int index);
102
103 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000104 Returns the draw command at the given index.
105 @param index The index of the command
106 */
vjiaoblack95302da2016-07-21 10:25:54 -0700107 SkDrawCommand *getDrawCommandAt(int index);
chudy@google.com902ebe52012-06-29 14:21:22 +0000108
109 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000110 Sets the draw command for a given index.
111 @param index The index to overwrite
112 @param command The new command
113 */
vjiaoblack95302da2016-07-21 10:25:54 -0700114 void setDrawCommandAt(int index, SkDrawCommand *command);
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000115
116 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000117 Returns information about the command at the given index.
118 @param index The index of the command
119 */
vjiaoblack95302da2016-07-21 10:25:54 -0700120 const SkTDArray<SkString *> *getCommandInfo(int index) const;
chudy@google.com902ebe52012-06-29 14:21:22 +0000121
122 /**
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000123 Returns the visibility of the command at the given index.
124 @param index The index of the command
125 */
126 bool getDrawCommandVisibilityAt(int index);
127
128 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000129 Returns the vector of draw commands
130 */
reed@google.com44699382013-10-31 17:28:30 +0000131 SK_ATTR_DEPRECATED("please use getDrawCommandAt and getSize instead")
vjiaoblack95302da2016-07-21 10:25:54 -0700132 const SkTDArray<SkDrawCommand *> &getDrawCommands() const;
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000133
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000134 /**
135 Returns the vector of draw commands. Do not use this entry
136 point - it is going away!
137 */
vjiaoblack95302da2016-07-21 10:25:54 -0700138 SkTDArray<SkDrawCommand *> &getDrawCommands();
chudy@google.com902ebe52012-06-29 14:21:22 +0000139
140 /**
chudy@google.comf1414322012-07-03 20:28:14 +0000141 Returns length of draw command vector.
142 */
commit-bot@chromium.org0d4fe142013-07-15 22:47:14 +0000143 int getSize() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000144 return fCommandVector.count();
chudy@google.comf1414322012-07-03 20:28:14 +0000145 }
146
chudy@google.com902ebe52012-06-29 14:21:22 +0000147 /**
148 Toggles the visibility / execution of the draw command at index i with
149 the value of toggle.
150 */
151 void toggleCommand(int index, bool toggle);
152
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000153 void setUserMatrix(SkMatrix matrix) {
154 fUserMatrix = matrix;
chudy@google.com830b8792012-08-01 15:57:52 +0000155 }
156
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000157 SkString clipStackData() const { return fClipStackData; }
158
ethannicholas402cd912016-02-10 12:57:30 -0800159 /**
ethannicholas0a0520a2016-02-12 12:06:53 -0800160 Returns a JSON object representing up to the Nth draw, where N is less than
halcanary9d524f22016-03-29 09:03:52 -0700161 SkDebugCanvas::getSize(). The encoder may use the UrlDataManager to store binary data such
ethannicholas0a0520a2016-02-12 12:06:53 -0800162 as images, referring to them via URLs embedded in the JSON.
ethannicholas402cd912016-02-10 12:57:30 -0800163 */
vjiaoblack95302da2016-07-21 10:25:54 -0700164 Json::Value toJSON(UrlDataManager &urlDataManager, int n, SkCanvas *);
ethannicholas402cd912016-02-10 12:57:30 -0800165
Brian Salomon144a5c52016-12-20 16:48:59 -0500166 Json::Value toJSONOpList(int n, SkCanvas*);
joshualittae47aee2016-03-10 13:29:36 -0800167
Brian Salomon144a5c52016-12-20 16:48:59 -0500168 ////////////////////////////////////////////////////////////////////////////////
169 // Inherited from SkCanvas
170 ////////////////////////////////////////////////////////////////////////////////
chudy@google.com902ebe52012-06-29 14:21:22 +0000171
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000172 static const int kVizImageHeight = 256;
173 static const int kVizImageWidth = 256;
174
mtklein36352bf2015-03-25 18:17:31 -0700175 bool isClipEmpty() const override { return false; }
vjiaoblack95302da2016-07-21 10:25:54 -0700176
mtklein36352bf2015-03-25 18:17:31 -0700177 bool isClipRect() const override { return true; }
vjiaoblack95302da2016-07-21 10:25:54 -0700178
Mike Reed42e8c532017-01-23 14:09:13 -0500179 SkRect onGetLocalClipBounds() const override {
180 return SkRect::MakeIWH(this->imageInfo().width(), this->imageInfo().height());
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000181 }
vjiaoblack95302da2016-07-21 10:25:54 -0700182
Mike Reed42e8c532017-01-23 14:09:13 -0500183 SkIRect onGetDeviceClipBounds() const override {
184 return SkIRect::MakeWH(this->imageInfo().width(), this->imageInfo().height());
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000185 }
186
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000187protected:
mtklein36352bf2015-03-25 18:17:31 -0700188 void willSave() override;
vjiaoblack95302da2016-07-21 10:25:54 -0700189
190 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &) override;
191
mtklein36352bf2015-03-25 18:17:31 -0700192 void willRestore() override;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000193
vjiaoblack95302da2016-07-21 10:25:54 -0700194 void didConcat(const SkMatrix &) override;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000195
vjiaoblack95302da2016-07-21 10:25:54 -0700196 void didSetMatrix(const SkMatrix &) override;
197
198#ifdef SK_EXPERIMENTAL_SHADOWING
Mike Klein0f61faa2016-10-11 16:26:57 -0400199 void didTranslateZ(SkScalar) override;
vjiaoblack95302da2016-07-21 10:25:54 -0700200#else
201 void didTranslateZ(SkScalar);
202#endif
vjiaoblacke5de1302016-07-13 14:05:28 -0700203
reed97660cc2016-06-28 18:54:19 -0700204 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
mtklein36352bf2015-03-25 18:17:31 -0700205 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800206 void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700207 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800208 void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
mtklein36352bf2015-03-25 18:17:31 -0700209 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800210 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
mtklein36352bf2015-03-25 18:17:31 -0700211 SkScalar constY, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800212 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700213 const SkMatrix* matrix, const SkPaint&) override;
reed45561a02016-07-07 12:47:17 -0700214 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform[], const SkRect*,
215 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800216 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700217 const SkPaint& paint) override;
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000218
robertphillips9bafc302015-02-13 11:13:00 -0800219 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400220 const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
mtklein36352bf2015-03-25 18:17:31 -0700221 void onDrawPaint(const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800222
mtklein36352bf2015-03-25 18:17:31 -0700223 void onDrawRect(const SkRect&, const SkPaint&) override;
224 void onDrawOval(const SkRect&, const SkPaint&) override;
bsalomonac3aa242016-08-19 11:25:19 -0700225 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700226 void onDrawRRect(const SkRRect&, const SkPaint&) override;
227 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
Mike Reedfed9cfd2017-03-17 12:09:04 -0400228 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700229 void onDrawPath(const SkPath&, const SkPaint&) override;
230 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800231 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
reed562fe472015-07-28 07:35:14 -0700232 SrcRectConstraint) override;
mtklein36352bf2015-03-25 18:17:31 -0700233 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
Stan Ilievac42aeb2017-01-12 16:20:50 -0500234 void onDrawImageLattice(const SkImage* image, const Lattice& lattice,
235 const SkRect& dst, const SkPaint* paint) override;
reed41af9662015-01-05 07:49:08 -0800236 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700237 const SkPaint*, SrcRectConstraint) override;
reed41af9662015-01-05 07:49:08 -0800238 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
mtklein36352bf2015-03-25 18:17:31 -0700239 const SkPaint*) override;
Mike Reedc1f77742016-12-09 09:00:50 -0500240 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
241 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
242 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
243 void onClipRegion(const SkRegion& region, SkClipOp) override;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000244
mtklein36352bf2015-03-25 18:17:31 -0700245 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
robertphillips9b14f262014-06-04 05:40:44 -0700246
vjiaoblack95302da2016-07-21 10:25:54 -0700247#ifdef SK_EXPERIMENTAL_SHADOWING
248 void onDrawShadowedPicture(const SkPicture*,
249 const SkMatrix*,
vjiaoblacke6f5d562016-08-25 06:30:23 -0700250 const SkPaint*,
251 const SkShadowParams& params) override;
vjiaoblack95302da2016-07-21 10:25:54 -0700252#else
253 void onDrawShadowedPicture(const SkPicture*,
254 const SkMatrix*,
vjiaoblacke6f5d562016-08-25 06:30:23 -0700255 const SkPaint*,
256 const SkShadowParams& params);
vjiaoblack95302da2016-07-21 10:25:54 -0700257#endif
258
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000259 void markActiveCommands(int index);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000260
chudy@google.com902ebe52012-06-29 14:21:22 +0000261private:
robertphillips@google.com67baba42013-01-02 20:20:31 +0000262 SkTDArray<SkDrawCommand*> fCommandVector;
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000263 SkPicture* fPicture;
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000264 bool fFilter;
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000265 bool fMegaVizMode;
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
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000270 SkString fClipStackData;
271 bool fCalledAddStackData;
272 SkPath fSaveDevPath;
273
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000274 bool fOverdrawViz;
fmalita65cdb572015-03-26 07:24:48 -0700275 bool fOverrideFilterQuality;
276 SkFilterQuality fFilterQuality;
ethannicholas0a0520a2016-02-12 12:06:53 -0800277 SkColor fClipVizColor;
Brian Salomon144a5c52016-12-20 16:48:59 -0500278 bool fDrawGpuOpBounds;
chudy@google.com902ebe52012-06-29 14:21:22 +0000279
280 /**
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000281 The active saveLayer commands at a given point in the renderering.
282 Only used when "mega" visualization is enabled.
283 */
284 SkTDArray<SkDrawCommand*> fActiveLayers;
285
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000286 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000287 Adds the command to the classes vector of commands.
288 @param command The draw command for execution
289 */
290 void addDrawCommand(SkDrawCommand* command);
chudy@google.com830b8792012-08-01 15:57:52 +0000291
292 /**
293 Applies any panning and zooming the user has specified before
294 drawing anything else into the canvas.
295 */
296 void applyUserTransform(SkCanvas* canvas);
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000297
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000298 void resetClipStackData() { fClipStackData.reset(); fCalledAddStackData = false; }
299
Mike Reedc1f77742016-12-09 09:00:50 -0500300 void addClipStackData(const SkPath& devPath, const SkPath& operand, SkClipOp elementOp);
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000301 void addPathData(const SkPath& path, const char* pathName);
302 bool lastClipStackData(const SkPath& devPath);
303 void outputConicPoints(const SkPoint* pts, SkScalar weight);
304 void outputPoints(const SkPoint* pts, int count);
305 void outputPointsCommon(const SkPoint* pts, int count);
306 void outputScalar(SkScalar num);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000307
joshualittae47aee2016-03-10 13:29:36 -0800308 GrAuditTrail* getAuditTrail(SkCanvas*);
309
Brian Salomon144a5c52016-12-20 16:48:59 -0500310 void drawAndCollectOps(int n, SkCanvas*);
joshualittae47aee2016-03-10 13:29:36 -0800311 void cleanupAuditTrail(SkCanvas*);
fmalita65cdb572015-03-26 07:24:48 -0700312
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000313 typedef SkCanvas INHERITED;
chudy@google.com902ebe52012-06-29 14:21:22 +0000314};
315
316#endif