blob: 6a3d92543f41d16a751b2ddae3c23645cc75aa23 [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
fmalita@google.com86681b32013-06-13 20:59:14 +000024class SK_API 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
halcanary9d524f22016-03-29 09:03:52 -070050 void setDrawGpuBatchBounds(bool drawGpuBatchBounds) {
51 fDrawGpuBatchBounds = drawGpuBatchBounds;
joshualitt10d8fc22016-02-29 11:15:06 -080052 }
53
joshualitt5d5207a2016-02-29 12:46:04 -080054 bool getDrawGpuBatchBounds() const { return fDrawGpuBatchBounds; }
55
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000056 bool getAllowSimplifyClip() const { return fAllowSimplifyClip; }
57
vjiaoblack95302da2016-07-21 10:25:54 -070058 void setPicture(SkPicture *picture) { fPicture = picture; }
robertphillips@google.comf4741c12013-02-06 20:13:54 +000059
60 /**
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000061 * Enable or disable texure filtering override
62 */
reed93a12152015-03-16 10:08:34 -070063 void overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality);
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000064
65 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000066 Executes all draw calls to the canvas.
67 @param canvas The canvas being drawn to
68 */
vjiaoblack95302da2016-07-21 10:25:54 -070069 void draw(SkCanvas *canvas);
chudy@google.com902ebe52012-06-29 14:21:22 +000070
71 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000072 Executes the draw calls up to the specified index.
73 @param canvas The canvas being drawn to
74 @param index The index of the final command being executed
joshualitt46b301d2016-03-02 08:32:37 -080075 @param m an optional Mth gpu batch to highlight, or -1
chudy@google.com902ebe52012-06-29 14:21:22 +000076 */
vjiaoblack95302da2016-07-21 10:25:54 -070077 void drawTo(SkCanvas *canvas, int index, int m = -1);
chudy@google.com0b5bbb02012-07-31 19:55:32 +000078
79 /**
chudy@google.coma9e937c2012-08-03 17:32:05 +000080 Returns the most recently calculated transformation matrix
81 */
vjiaoblack95302da2016-07-21 10:25:54 -070082 const SkMatrix &getCurrentMatrix() {
chudy@google.coma9e937c2012-08-03 17:32:05 +000083 return fMatrix;
84 }
85
86 /**
87 Returns the most recently calculated clip
88 */
vjiaoblack95302da2016-07-21 10:25:54 -070089 const SkIRect &getCurrentClip() {
chudy@google.coma9e937c2012-08-03 17:32:05 +000090 return fClip;
91 }
92
93 /**
chudy@google.com0b5bbb02012-07-31 19:55:32 +000094 Returns the index of the last draw command to write to the pixel at (x,y)
95 */
chudy@google.com830b8792012-08-01 15:57:52 +000096 int getCommandAtPoint(int x, int y, int index);
chudy@google.com902ebe52012-06-29 14:21:22 +000097
98 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000099 Removes the command at the specified index
100 @param index The index of the command to delete
101 */
102 void deleteDrawCommandAt(int index);
103
104 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000105 Returns the draw command at the given index.
106 @param index The index of the command
107 */
vjiaoblack95302da2016-07-21 10:25:54 -0700108 SkDrawCommand *getDrawCommandAt(int index);
chudy@google.com902ebe52012-06-29 14:21:22 +0000109
110 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000111 Sets the draw command for a given index.
112 @param index The index to overwrite
113 @param command The new command
114 */
vjiaoblack95302da2016-07-21 10:25:54 -0700115 void setDrawCommandAt(int index, SkDrawCommand *command);
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000116
117 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000118 Returns information about the command at the given index.
119 @param index The index of the command
120 */
vjiaoblack95302da2016-07-21 10:25:54 -0700121 const SkTDArray<SkString *> *getCommandInfo(int index) const;
chudy@google.com902ebe52012-06-29 14:21:22 +0000122
123 /**
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000124 Returns the visibility of the command at the given index.
125 @param index The index of the command
126 */
127 bool getDrawCommandVisibilityAt(int index);
128
129 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000130 Returns the vector of draw commands
131 */
reed@google.com44699382013-10-31 17:28:30 +0000132 SK_ATTR_DEPRECATED("please use getDrawCommandAt and getSize instead")
vjiaoblack95302da2016-07-21 10:25:54 -0700133 const SkTDArray<SkDrawCommand *> &getDrawCommands() const;
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000134
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000135 /**
136 Returns the vector of draw commands. Do not use this entry
137 point - it is going away!
138 */
vjiaoblack95302da2016-07-21 10:25:54 -0700139 SkTDArray<SkDrawCommand *> &getDrawCommands();
chudy@google.com902ebe52012-06-29 14:21:22 +0000140
141 /**
chudy@google.comf1414322012-07-03 20:28:14 +0000142 Returns length of draw command vector.
143 */
commit-bot@chromium.org0d4fe142013-07-15 22:47:14 +0000144 int getSize() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000145 return fCommandVector.count();
chudy@google.comf1414322012-07-03 20:28:14 +0000146 }
147
chudy@google.com902ebe52012-06-29 14:21:22 +0000148 /**
149 Toggles the visibility / execution of the draw command at index i with
150 the value of toggle.
151 */
152 void toggleCommand(int index, bool toggle);
153
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000154 void setUserMatrix(SkMatrix matrix) {
155 fUserMatrix = matrix;
chudy@google.com830b8792012-08-01 15:57:52 +0000156 }
157
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000158 SkString clipStackData() const { return fClipStackData; }
159
ethannicholas402cd912016-02-10 12:57:30 -0800160 /**
ethannicholas0a0520a2016-02-12 12:06:53 -0800161 Returns a JSON object representing up to the Nth draw, where N is less than
halcanary9d524f22016-03-29 09:03:52 -0700162 SkDebugCanvas::getSize(). The encoder may use the UrlDataManager to store binary data such
ethannicholas0a0520a2016-02-12 12:06:53 -0800163 as images, referring to them via URLs embedded in the JSON.
ethannicholas402cd912016-02-10 12:57:30 -0800164 */
vjiaoblack95302da2016-07-21 10:25:54 -0700165 Json::Value toJSON(UrlDataManager &urlDataManager, int n, SkCanvas *);
ethannicholas402cd912016-02-10 12:57:30 -0800166
vjiaoblack95302da2016-07-21 10:25:54 -0700167 Json::Value toJSONBatchList(int n, SkCanvas *);
joshualittae47aee2016-03-10 13:29:36 -0800168
chudy@google.com902ebe52012-06-29 14:21:22 +0000169////////////////////////////////////////////////////////////////////////////////
170// Inherited from SkCanvas
171////////////////////////////////////////////////////////////////////////////////
172
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000173 static const int kVizImageHeight = 256;
174 static const int kVizImageWidth = 256;
175
mtklein36352bf2015-03-25 18:17:31 -0700176 bool isClipEmpty() const override { return false; }
vjiaoblack95302da2016-07-21 10:25:54 -0700177
mtklein36352bf2015-03-25 18:17:31 -0700178 bool isClipRect() const override { return true; }
vjiaoblack95302da2016-07-21 10:25:54 -0700179
180 bool getClipBounds(SkRect *bounds) const override {
bsalomon49f085d2014-09-05 13:34:00 -0700181 if (bounds) {
skia.committer@gmail.com370a8992014-03-01 03:02:09 +0000182 bounds->setXYWH(0, 0,
reede5ea5002014-09-03 11:54:58 -0700183 SkIntToScalar(this->imageInfo().width()),
184 SkIntToScalar(this->imageInfo().height()));
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000185 }
186 return true;
187 }
vjiaoblack95302da2016-07-21 10:25:54 -0700188
189 bool getClipDeviceBounds(SkIRect *bounds) const override {
bsalomon49f085d2014-09-05 13:34:00 -0700190 if (bounds) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000191 bounds->setLargest();
192 }
193 return true;
194 }
195
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000196protected:
mtklein36352bf2015-03-25 18:17:31 -0700197 void willSave() override;
vjiaoblack95302da2016-07-21 10:25:54 -0700198
199 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &) override;
200
mtklein36352bf2015-03-25 18:17:31 -0700201 void willRestore() override;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000202
vjiaoblack95302da2016-07-21 10:25:54 -0700203 void didConcat(const SkMatrix &) override;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000204
vjiaoblack95302da2016-07-21 10:25:54 -0700205 void didSetMatrix(const SkMatrix &) override;
206
207#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblack5bfee982016-07-22 10:04:15 -0700208 void didTranslateZ(SkScalar) override;
vjiaoblack95302da2016-07-21 10:25:54 -0700209#else
210 void didTranslateZ(SkScalar);
211#endif
vjiaoblacke5de1302016-07-13 14:05:28 -0700212
reed97660cc2016-06-28 18:54:19 -0700213 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
mtklein36352bf2015-03-25 18:17:31 -0700214 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800215 void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700216 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800217 void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
mtklein36352bf2015-03-25 18:17:31 -0700218 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800219 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
mtklein36352bf2015-03-25 18:17:31 -0700220 SkScalar constY, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800221 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700222 const SkMatrix* matrix, const SkPaint&) override;
reed45561a02016-07-07 12:47:17 -0700223 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform[], const SkRect*,
224 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800225 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700226 const SkPaint& paint) override;
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000227
robertphillips9bafc302015-02-13 11:13:00 -0800228 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
mtkleinf0599002015-07-13 06:18:39 -0700229 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) override;
mtklein36352bf2015-03-25 18:17:31 -0700230 void onDrawPaint(const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800231
mtklein36352bf2015-03-25 18:17:31 -0700232 void onDrawRect(const SkRect&, const SkPaint&) override;
233 void onDrawOval(const SkRect&, const SkPaint&) override;
bsalomonac3aa242016-08-19 11:25:19 -0700234 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700235 void onDrawRRect(const SkRRect&, const SkPaint&) override;
236 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800237 void onDrawVertices(VertexMode vmode, int vertexCount,
238 const SkPoint vertices[], const SkPoint texs[],
239 const SkColor colors[], SkXfermode* xmode,
240 const uint16_t indices[], int indexCount,
mtklein36352bf2015-03-25 18:17:31 -0700241 const SkPaint&) override;
242 void onDrawPath(const SkPath&, const SkPaint&) override;
243 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800244 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
reed562fe472015-07-28 07:35:14 -0700245 SrcRectConstraint) override;
mtklein36352bf2015-03-25 18:17:31 -0700246 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) 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;
mtklein36352bf2015-03-25 18:17:31 -0700251 void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
252 void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
253 void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
254 void onClipRegion(const SkRegion& region, SkRegion::Op) 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;
joshualitt10d8fc22016-02-29 11:15:06 -0800289 bool fDrawGpuBatchBounds;
chudy@google.com902ebe52012-06-29 14:21:22 +0000290
fmalita65cdb572015-03-26 07:24:48 -0700291 SkAutoTUnref<SkNWayCanvas> fPaintFilterCanvas;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000292
chudy@google.com902ebe52012-06-29 14:21:22 +0000293 /**
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000294 The active saveLayer commands at a given point in the renderering.
295 Only used when "mega" visualization is enabled.
296 */
297 SkTDArray<SkDrawCommand*> fActiveLayers;
298
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000299 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000300 Adds the command to the classes vector of commands.
301 @param command The draw command for execution
302 */
303 void addDrawCommand(SkDrawCommand* command);
chudy@google.com830b8792012-08-01 15:57:52 +0000304
305 /**
306 Applies any panning and zooming the user has specified before
307 drawing anything else into the canvas.
308 */
309 void applyUserTransform(SkCanvas* canvas);
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000310
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000311 void resetClipStackData() { fClipStackData.reset(); fCalledAddStackData = false; }
312
313 void addClipStackData(const SkPath& devPath, const SkPath& operand, SkRegion::Op elementOp);
314 void addPathData(const SkPath& path, const char* pathName);
315 bool lastClipStackData(const SkPath& devPath);
316 void outputConicPoints(const SkPoint* pts, SkScalar weight);
317 void outputPoints(const SkPoint* pts, int count);
318 void outputPointsCommon(const SkPoint* pts, int count);
319 void outputScalar(SkScalar num);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000320
joshualittae47aee2016-03-10 13:29:36 -0800321 GrAuditTrail* getAuditTrail(SkCanvas*);
322
fmalita65cdb572015-03-26 07:24:48 -0700323 void updatePaintFilterCanvas();
joshualittae47aee2016-03-10 13:29:36 -0800324 void drawAndCollectBatches(int n, SkCanvas*);
325 void cleanupAuditTrail(SkCanvas*);
fmalita65cdb572015-03-26 07:24:48 -0700326
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000327 typedef SkCanvas INHERITED;
chudy@google.com902ebe52012-06-29 14:21:22 +0000328};
329
330#endif