blob: 2a3dcbbf497c81938522a8cb203b7ce5b5f94c99 [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"
reedfb8c1fc2015-08-04 18:44:56 -070016#include "SkString.h"
bungemand3ebb482015-08-05 13:57:49 -070017#include "SkTArray.h"
Brian Salomon199fb872017-02-06 09:41:10 -050018#include "SkVertices.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;
Brian Osman255735e2018-04-06 14:51:42 -040023class SkPicture;
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
Brian Salomond3b65972017-03-22 12:05:03 -040029 ~SkDebugCanvas() override;
chudy@google.com902ebe52012-06-29 14:21:22 +000030
chudy@google.com902ebe52012-06-29 14:21:22 +000031 /**
robertphillips@google.comf4741c12013-02-06 20:13:54 +000032 * Enable or disable overdraw visualization
33 */
fmalita65cdb572015-03-26 07:24:48 -070034 void setOverdrawViz(bool overdrawViz);
vjiaoblack95302da2016-07-21 10:25:54 -070035
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000036 bool getOverdrawViz() const { return fOverdrawViz; }
37
ethannicholas0a0520a2016-02-12 12:06:53 -080038 /**
39 * Set the color of the clip visualization. An alpha of zero renders the clip invisible.
40 */
41 void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; }
vjiaoblack95302da2016-07-21 10:25:54 -070042
Brian Salomon144a5c52016-12-20 16:48:59 -050043 void setDrawGpuOpBounds(bool drawGpuOpBounds) { fDrawGpuOpBounds = drawGpuOpBounds; }
joshualitt10d8fc22016-02-29 11:15:06 -080044
Brian Salomon144a5c52016-12-20 16:48:59 -050045 bool getDrawGpuOpBounds() const { return fDrawGpuOpBounds; }
joshualitt5d5207a2016-02-29 12:46:04 -080046
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000047 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000048 Executes all draw calls to the canvas.
49 @param canvas The canvas being drawn to
50 */
vjiaoblack95302da2016-07-21 10:25:54 -070051 void draw(SkCanvas *canvas);
chudy@google.com902ebe52012-06-29 14:21:22 +000052
53 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000054 Executes the draw calls up to the specified index.
55 @param canvas The canvas being drawn to
56 @param index The index of the final command being executed
Brian Salomon144a5c52016-12-20 16:48:59 -050057 @param m an optional Mth gpu op to highlight, or -1
chudy@google.com902ebe52012-06-29 14:21:22 +000058 */
vjiaoblack95302da2016-07-21 10:25:54 -070059 void drawTo(SkCanvas *canvas, int index, int m = -1);
chudy@google.com0b5bbb02012-07-31 19:55:32 +000060
61 /**
chudy@google.coma9e937c2012-08-03 17:32:05 +000062 Returns the most recently calculated transformation matrix
63 */
vjiaoblack95302da2016-07-21 10:25:54 -070064 const SkMatrix &getCurrentMatrix() {
chudy@google.coma9e937c2012-08-03 17:32:05 +000065 return fMatrix;
66 }
67
68 /**
69 Returns the most recently calculated clip
70 */
vjiaoblack95302da2016-07-21 10:25:54 -070071 const SkIRect &getCurrentClip() {
chudy@google.coma9e937c2012-08-03 17:32:05 +000072 return fClip;
73 }
74
75 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000076 Removes the command at the specified index
77 @param index The index of the command to delete
78 */
79 void deleteDrawCommandAt(int index);
80
81 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000082 Returns the draw command at the given index.
83 @param index The index of the command
84 */
vjiaoblack95302da2016-07-21 10:25:54 -070085 SkDrawCommand *getDrawCommandAt(int index);
chudy@google.com902ebe52012-06-29 14:21:22 +000086
87 /**
chudy@google.comf1414322012-07-03 20:28:14 +000088 Returns length of draw command vector.
89 */
commit-bot@chromium.org0d4fe142013-07-15 22:47:14 +000090 int getSize() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +000091 return fCommandVector.count();
chudy@google.comf1414322012-07-03 20:28:14 +000092 }
93
chudy@google.com902ebe52012-06-29 14:21:22 +000094 /**
95 Toggles the visibility / execution of the draw command at index i with
96 the value of toggle.
97 */
98 void toggleCommand(int index, bool toggle);
99
ethannicholas402cd912016-02-10 12:57:30 -0800100 /**
ethannicholas0a0520a2016-02-12 12:06:53 -0800101 Returns a JSON object representing up to the Nth draw, where N is less than
halcanary9d524f22016-03-29 09:03:52 -0700102 SkDebugCanvas::getSize(). The encoder may use the UrlDataManager to store binary data such
ethannicholas0a0520a2016-02-12 12:06:53 -0800103 as images, referring to them via URLs embedded in the JSON.
ethannicholas402cd912016-02-10 12:57:30 -0800104 */
vjiaoblack95302da2016-07-21 10:25:54 -0700105 Json::Value toJSON(UrlDataManager &urlDataManager, int n, SkCanvas *);
ethannicholas402cd912016-02-10 12:57:30 -0800106
Brian Salomon144a5c52016-12-20 16:48:59 -0500107 Json::Value toJSONOpList(int n, SkCanvas*);
joshualittae47aee2016-03-10 13:29:36 -0800108
Robert Phillipsdeaf5682017-09-06 13:07:21 -0400109 void detachCommands(SkTDArray<SkDrawCommand*>* dst) {
110 fCommandVector.swap(*dst);
111 }
112
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000113protected:
mtklein36352bf2015-03-25 18:17:31 -0700114 void willSave() override;
vjiaoblack95302da2016-07-21 10:25:54 -0700115
116 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &) override;
117
mtklein36352bf2015-03-25 18:17:31 -0700118 void willRestore() override;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000119
vjiaoblack95302da2016-07-21 10:25:54 -0700120 void didConcat(const SkMatrix &) override;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000121
vjiaoblack95302da2016-07-21 10:25:54 -0700122 void didSetMatrix(const SkMatrix &) override;
123
reed97660cc2016-06-28 18:54:19 -0700124 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
mtklein36352bf2015-03-25 18:17:31 -0700125 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800126 void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700127 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800128 void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
mtklein36352bf2015-03-25 18:17:31 -0700129 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800130 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
mtklein36352bf2015-03-25 18:17:31 -0700131 SkScalar constY, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800132 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
mtklein36352bf2015-03-25 18:17:31 -0700133 const SkMatrix* matrix, const SkPaint&) override;
reed45561a02016-07-07 12:47:17 -0700134 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform[], const SkRect*,
135 const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800136 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700137 const SkPaint& paint) override;
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000138
robertphillips9bafc302015-02-13 11:13:00 -0800139 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400140 const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
mtklein36352bf2015-03-25 18:17:31 -0700141 void onDrawPaint(const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800142
mtklein36352bf2015-03-25 18:17:31 -0700143 void onDrawRect(const SkRect&, const SkPaint&) override;
144 void onDrawOval(const SkRect&, const SkPaint&) override;
bsalomonac3aa242016-08-19 11:25:19 -0700145 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700146 void onDrawRRect(const SkRRect&, const SkPaint&) override;
147 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
Mike Reedfed9cfd2017-03-17 12:09:04 -0400148 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700149 void onDrawPath(const SkPath&, const SkPaint&) override;
Brian Osmanc25e2692018-03-12 10:57:28 -0400150 void onDrawRegion(const SkRegion&, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700151 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800152 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
reed562fe472015-07-28 07:35:14 -0700153 SrcRectConstraint) override;
mtklein36352bf2015-03-25 18:17:31 -0700154 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
Stan Ilievac42aeb2017-01-12 16:20:50 -0500155 void onDrawImageLattice(const SkImage* image, const Lattice& lattice,
156 const SkRect& dst, const SkPaint* paint) override;
reed41af9662015-01-05 07:49:08 -0800157 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700158 const SkPaint*, SrcRectConstraint) override;
reed41af9662015-01-05 07:49:08 -0800159 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
mtklein36352bf2015-03-25 18:17:31 -0700160 const SkPaint*) override;
Brian Osmanc25e2692018-03-12 10:57:28 -0400161 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
162 const SkPaint*) override;
Mike Reedc1f77742016-12-09 09:00:50 -0500163 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
164 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
165 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
166 void onClipRegion(const SkRegion& region, SkClipOp) override;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000167
mtklein36352bf2015-03-25 18:17:31 -0700168 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
robertphillips9b14f262014-06-04 05:40:44 -0700169
chudy@google.com902ebe52012-06-29 14:21:22 +0000170private:
robertphillips@google.com67baba42013-01-02 20:20:31 +0000171 SkTDArray<SkDrawCommand*> fCommandVector;
chudy@google.coma9e937c2012-08-03 17:32:05 +0000172 SkMatrix fMatrix;
173 SkIRect fClip;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000174
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000175 bool fOverdrawViz;
ethannicholas0a0520a2016-02-12 12:06:53 -0800176 SkColor fClipVizColor;
Brian Salomon144a5c52016-12-20 16:48:59 -0500177 bool fDrawGpuOpBounds;
chudy@google.com902ebe52012-06-29 14:21:22 +0000178
179 /**
Robert Phillipsdeaf5682017-09-06 13:07:21 -0400180 Adds the command to the class' vector of commands.
chudy@google.com902ebe52012-06-29 14:21:22 +0000181 @param command The draw command for execution
182 */
183 void addDrawCommand(SkDrawCommand* command);
chudy@google.com830b8792012-08-01 15:57:52 +0000184
joshualittae47aee2016-03-10 13:29:36 -0800185 GrAuditTrail* getAuditTrail(SkCanvas*);
186
Brian Salomon144a5c52016-12-20 16:48:59 -0500187 void drawAndCollectOps(int n, SkCanvas*);
joshualittae47aee2016-03-10 13:29:36 -0800188 void cleanupAuditTrail(SkCanvas*);
fmalita65cdb572015-03-26 07:24:48 -0700189
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000190 typedef SkCanvas INHERITED;
chudy@google.com902ebe52012-06-29 14:21:22 +0000191};
192
193#endif