blob: d06637d42b26ba1ae95e5154de71cf8fdd9be227 [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"
bungemand3ebb482015-08-05 13:57:49 -070015#include "SkPath.h"
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000016#include "SkPathOps.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000017#include "SkPicture.h"
reedfb8c1fc2015-08-04 18:44:56 -070018#include "SkString.h"
bungemand3ebb482015-08-05 13:57:49 -070019#include "SkTArray.h"
ethannicholas402cd912016-02-10 12:57:30 -080020#include "UrlDataManager.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000021
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);
robertphillips@google.comf4741c12013-02-06 20:13:54 +000027 virtual ~SkDebugCanvas();
chudy@google.com902ebe52012-06-29 14:21:22 +000028
commit-bot@chromium.org768ac852014-03-03 16:32:17 +000029 void toggleFilter(bool toggle) { fFilter = toggle; }
30
31 void setMegaVizMode(bool megaVizMode) { fMegaVizMode = megaVizMode; }
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000032 bool getMegaVizMode() const { return fMegaVizMode; }
chudy@google.com902ebe52012-06-29 14:21:22 +000033
34 /**
robertphillips@google.comf4741c12013-02-06 20:13:54 +000035 * Enable or disable overdraw visualization
36 */
fmalita65cdb572015-03-26 07:24:48 -070037 void setOverdrawViz(bool overdrawViz);
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000038 bool getOverdrawViz() const { return fOverdrawViz; }
39
ethannicholas0a0520a2016-02-12 12:06:53 -080040 /**
41 * Set the color of the clip visualization. An alpha of zero renders the clip invisible.
42 */
43 void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; }
44 SkColor getClipVizColor() const { return fClipVizColor; }
45
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +000046 bool getAllowSimplifyClip() const { return fAllowSimplifyClip; }
47
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000048 void setPicture(SkPicture* picture) { fPicture = picture; }
robertphillips@google.comf4741c12013-02-06 20:13:54 +000049
50 /**
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000051 * Enable or disable texure filtering override
52 */
reed93a12152015-03-16 10:08:34 -070053 void overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality);
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000054
55 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000056 Executes all draw calls to the canvas.
57 @param canvas The canvas being drawn to
58 */
59 void draw(SkCanvas* canvas);
60
61 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000062 Executes the draw calls up to the specified index.
63 @param canvas The canvas being drawn to
64 @param index The index of the final command being executed
65 */
chudy@google.com0b5bbb02012-07-31 19:55:32 +000066 void drawTo(SkCanvas* canvas, int index);
67
68 /**
chudy@google.coma9e937c2012-08-03 17:32:05 +000069 Returns the most recently calculated transformation matrix
70 */
71 const SkMatrix& getCurrentMatrix() {
72 return fMatrix;
73 }
74
75 /**
76 Returns the most recently calculated clip
77 */
78 const SkIRect& getCurrentClip() {
79 return fClip;
80 }
81
82 /**
chudy@google.com0b5bbb02012-07-31 19:55:32 +000083 Returns the index of the last draw command to write to the pixel at (x,y)
84 */
chudy@google.com830b8792012-08-01 15:57:52 +000085 int getCommandAtPoint(int x, int y, int index);
chudy@google.com902ebe52012-06-29 14:21:22 +000086
87 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000088 Removes the command at the specified index
89 @param index The index of the command to delete
90 */
91 void deleteDrawCommandAt(int index);
92
93 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000094 Returns the draw command at the given index.
95 @param index The index of the command
96 */
97 SkDrawCommand* getDrawCommandAt(int index);
98
99 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +0000100 Sets the draw command for a given index.
101 @param index The index to overwrite
102 @param command The new command
103 */
104 void setDrawCommandAt(int index, SkDrawCommand* command);
105
106 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000107 Returns information about the command at the given index.
108 @param index The index of the command
109 */
fmalita8c89c522014-11-08 16:18:56 -0800110 const SkTDArray<SkString*>* getCommandInfo(int index) const;
chudy@google.com902ebe52012-06-29 14:21:22 +0000111
112 /**
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000113 Returns the visibility of the command at the given index.
114 @param index The index of the command
115 */
116 bool getDrawCommandVisibilityAt(int index);
117
118 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000119 Returns the vector of draw commands
120 */
reed@google.com44699382013-10-31 17:28:30 +0000121 SK_ATTR_DEPRECATED("please use getDrawCommandAt and getSize instead")
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000122 const SkTDArray<SkDrawCommand*>& getDrawCommands() const;
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000123
robertphillips@google.comfebc0ec2013-03-11 22:53:11 +0000124 /**
125 Returns the vector of draw commands. Do not use this entry
126 point - it is going away!
127 */
128 SkTDArray<SkDrawCommand*>& getDrawCommands();
chudy@google.com902ebe52012-06-29 14:21:22 +0000129
130 /**
chudy@google.comf1414322012-07-03 20:28:14 +0000131 Returns length of draw command vector.
132 */
commit-bot@chromium.org0d4fe142013-07-15 22:47:14 +0000133 int getSize() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000134 return fCommandVector.count();
chudy@google.comf1414322012-07-03 20:28:14 +0000135 }
136
chudy@google.com902ebe52012-06-29 14:21:22 +0000137 /**
138 Toggles the visibility / execution of the draw command at index i with
139 the value of toggle.
140 */
141 void toggleCommand(int index, bool toggle);
142
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000143 void setUserMatrix(SkMatrix matrix) {
144 fUserMatrix = matrix;
chudy@google.com830b8792012-08-01 15:57:52 +0000145 }
146
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000147 SkString clipStackData() const { return fClipStackData; }
148
ethannicholas402cd912016-02-10 12:57:30 -0800149 /**
ethannicholas0a0520a2016-02-12 12:06:53 -0800150 Returns a JSON object representing up to the Nth draw, where N is less than
151 SkDebugCanvas::getSize(). The encoder may use the UrlDataManager to store binary data such
152 as images, referring to them via URLs embedded in the JSON.
ethannicholas402cd912016-02-10 12:57:30 -0800153 */
joshualitt6b3cf732016-02-17 11:20:26 -0800154 Json::Value toJSON(UrlDataManager& urlDataManager, int n, SkCanvas*);
ethannicholas402cd912016-02-10 12:57:30 -0800155
chudy@google.com902ebe52012-06-29 14:21:22 +0000156////////////////////////////////////////////////////////////////////////////////
157// Inherited from SkCanvas
158////////////////////////////////////////////////////////////////////////////////
159
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000160 static const int kVizImageHeight = 256;
161 static const int kVizImageWidth = 256;
162
mtklein36352bf2015-03-25 18:17:31 -0700163 bool isClipEmpty() const override { return false; }
164 bool isClipRect() const override { return true; }
165 bool getClipBounds(SkRect* bounds) const override {
bsalomon49f085d2014-09-05 13:34:00 -0700166 if (bounds) {
skia.committer@gmail.com370a8992014-03-01 03:02:09 +0000167 bounds->setXYWH(0, 0,
reede5ea5002014-09-03 11:54:58 -0700168 SkIntToScalar(this->imageInfo().width()),
169 SkIntToScalar(this->imageInfo().height()));
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000170 }
171 return true;
172 }
mtklein36352bf2015-03-25 18:17:31 -0700173 bool getClipDeviceBounds(SkIRect* bounds) const override {
bsalomon49f085d2014-09-05 13:34:00 -0700174 if (bounds) {
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000175 bounds->setLargest();
176 }
177 return true;
178 }
179
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000180protected:
mtklein36352bf2015-03-25 18:17:31 -0700181 void willSave() override;
reed4960eee2015-12-18 07:09:18 -0800182 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
mtklein36352bf2015-03-25 18:17:31 -0700183 void willRestore() override;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000184
mtklein36352bf2015-03-25 18:17:31 -0700185 void didConcat(const SkMatrix&) override;
186 void didSetMatrix(const SkMatrix&) override;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000187
mtklein36352bf2015-03-25 18:17:31 -0700188 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800189 void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700190 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800191 void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
mtklein36352bf2015-03-25 18:17:31 -0700192 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800193 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
mtklein36352bf2015-03-25 18:17:31 -0700194 SkScalar constY, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800195 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700196 const SkMatrix* matrix, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800197 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700198 const SkPaint& paint) override;
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000199
robertphillips9bafc302015-02-13 11:13:00 -0800200 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
mtkleinf0599002015-07-13 06:18:39 -0700201 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) override;
mtklein36352bf2015-03-25 18:17:31 -0700202 void onDrawPaint(const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800203
mtklein36352bf2015-03-25 18:17:31 -0700204 void onDrawRect(const SkRect&, const SkPaint&) override;
205 void onDrawOval(const SkRect&, const SkPaint&) override;
206 void onDrawRRect(const SkRRect&, const SkPaint&) override;
207 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800208 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,
mtklein36352bf2015-03-25 18:17:31 -0700212 const SkPaint&) override;
213 void onDrawPath(const SkPath&, const SkPaint&) override;
214 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800215 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
reed562fe472015-07-28 07:35:14 -0700216 SrcRectConstraint) override;
mtklein36352bf2015-03-25 18:17:31 -0700217 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800218 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700219 const SkPaint*, SrcRectConstraint) override;
reed41af9662015-01-05 07:49:08 -0800220 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
mtklein36352bf2015-03-25 18:17:31 -0700221 const SkPaint*) override;
mtklein36352bf2015-03-25 18:17:31 -0700222 void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
223 void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
224 void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
225 void onClipRegion(const SkRegion& region, SkRegion::Op) override;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000226
mtklein36352bf2015-03-25 18:17:31 -0700227 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
robertphillips9b14f262014-06-04 05:40:44 -0700228
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000229 void markActiveCommands(int index);
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000230
chudy@google.com902ebe52012-06-29 14:21:22 +0000231private:
robertphillips@google.com67baba42013-01-02 20:20:31 +0000232 SkTDArray<SkDrawCommand*> fCommandVector;
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +0000233 SkPicture* fPicture;
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000234 bool fFilter;
commit-bot@chromium.org768ac852014-03-03 16:32:17 +0000235 bool fMegaVizMode;
bungeman@google.come8cc6e82013-01-17 16:30:56 +0000236 SkMatrix fUserMatrix;
chudy@google.coma9e937c2012-08-03 17:32:05 +0000237 SkMatrix fMatrix;
238 SkIRect fClip;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000239
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000240 SkString fClipStackData;
241 bool fCalledAddStackData;
242 SkPath fSaveDevPath;
243
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000244 bool fOverdrawViz;
fmalita65cdb572015-03-26 07:24:48 -0700245 bool fOverrideFilterQuality;
246 SkFilterQuality fFilterQuality;
ethannicholas0a0520a2016-02-12 12:06:53 -0800247 SkColor fClipVizColor;
chudy@google.com902ebe52012-06-29 14:21:22 +0000248
fmalita65cdb572015-03-26 07:24:48 -0700249 SkAutoTUnref<SkNWayCanvas> fPaintFilterCanvas;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000250
chudy@google.com902ebe52012-06-29 14:21:22 +0000251 /**
commit-bot@chromium.org1643b2c2014-03-03 23:25:41 +0000252 The active saveLayer commands at a given point in the renderering.
253 Only used when "mega" visualization is enabled.
254 */
255 SkTDArray<SkDrawCommand*> fActiveLayers;
256
skia.committer@gmail.comade9a342014-03-04 03:02:32 +0000257 /**
chudy@google.com902ebe52012-06-29 14:21:22 +0000258 Adds the command to the classes vector of commands.
259 @param command The draw command for execution
260 */
261 void addDrawCommand(SkDrawCommand* command);
chudy@google.com830b8792012-08-01 15:57:52 +0000262
263 /**
264 Applies any panning and zooming the user has specified before
265 drawing anything else into the canvas.
266 */
267 void applyUserTransform(SkCanvas* canvas);
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000268
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +0000269 void resetClipStackData() { fClipStackData.reset(); fCalledAddStackData = false; }
270
271 void addClipStackData(const SkPath& devPath, const SkPath& operand, SkRegion::Op elementOp);
272 void addPathData(const SkPath& path, const char* pathName);
273 bool lastClipStackData(const SkPath& devPath);
274 void outputConicPoints(const SkPoint* pts, SkScalar weight);
275 void outputPoints(const SkPoint* pts, int count);
276 void outputPointsCommon(const SkPoint* pts, int count);
277 void outputScalar(SkScalar num);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000278
fmalita65cdb572015-03-26 07:24:48 -0700279 void updatePaintFilterCanvas();
280
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000281 typedef SkCanvas INHERITED;
chudy@google.com902ebe52012-06-29 14:21:22 +0000282};
283
284#endif