blob: 9ae2950a35307fcab066d1082f60b7baf1858a6f [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"
Brian Osman78a76482018-05-18 16:59:13 -040013#include "SkCanvasVirtualEnforcer.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000014#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"
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;
Brian Osman255735e2018-04-06 14:51:42 -040024class SkPicture;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000025
Brian Osman78a76482018-05-18 16:59:13 -040026class SkDebugCanvas : public SkCanvasVirtualEnforcer<SkCanvas> {
chudy@google.com902ebe52012-06-29 14:21:22 +000027public:
chudy@google.com80a4a602012-07-30 18:54:07 +000028 SkDebugCanvas(int width, int height);
vjiaoblack95302da2016-07-21 10:25:54 -070029
Nathaniel Nifongcfdf87e2019-03-13 13:31:13 -040030 SkDebugCanvas(SkIRect bounds);
31
Brian Salomond3b65972017-03-22 12:05:03 -040032 ~SkDebugCanvas() override;
chudy@google.com902ebe52012-06-29 14:21:22 +000033
chudy@google.com902ebe52012-06-29 14:21:22 +000034 /**
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);
vjiaoblack95302da2016-07-21 10:25:54 -070038
commit-bot@chromium.org57f74e02014-03-25 23:31:33 +000039 bool getOverdrawViz() const { return fOverdrawViz; }
40
ethannicholas0a0520a2016-02-12 12:06:53 -080041 /**
42 * Set the color of the clip visualization. An alpha of zero renders the clip invisible.
43 */
44 void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; }
vjiaoblack95302da2016-07-21 10:25:54 -070045
Brian Salomon144a5c52016-12-20 16:48:59 -050046 void setDrawGpuOpBounds(bool drawGpuOpBounds) { fDrawGpuOpBounds = drawGpuOpBounds; }
joshualitt10d8fc22016-02-29 11:15:06 -080047
Brian Salomon144a5c52016-12-20 16:48:59 -050048 bool getDrawGpuOpBounds() const { return fDrawGpuOpBounds; }
joshualitt5d5207a2016-02-29 12:46:04 -080049
robertphillips@google.com32bbcf82013-10-17 17:56:10 +000050 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000051 Executes all draw calls to the canvas.
52 @param canvas The canvas being drawn to
53 */
vjiaoblack95302da2016-07-21 10:25:54 -070054 void draw(SkCanvas *canvas);
chudy@google.com902ebe52012-06-29 14:21:22 +000055
56 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000057 Executes the draw calls up to the specified index.
58 @param canvas The canvas being drawn to
59 @param index The index of the final command being executed
Brian Salomon144a5c52016-12-20 16:48:59 -050060 @param m an optional Mth gpu op to highlight, or -1
chudy@google.com902ebe52012-06-29 14:21:22 +000061 */
vjiaoblack95302da2016-07-21 10:25:54 -070062 void drawTo(SkCanvas *canvas, int index, int m = -1);
chudy@google.com0b5bbb02012-07-31 19:55:32 +000063
64 /**
chudy@google.coma9e937c2012-08-03 17:32:05 +000065 Returns the most recently calculated transformation matrix
66 */
vjiaoblack95302da2016-07-21 10:25:54 -070067 const SkMatrix &getCurrentMatrix() {
chudy@google.coma9e937c2012-08-03 17:32:05 +000068 return fMatrix;
69 }
70
71 /**
72 Returns the most recently calculated clip
73 */
vjiaoblack95302da2016-07-21 10:25:54 -070074 const SkIRect &getCurrentClip() {
chudy@google.coma9e937c2012-08-03 17:32:05 +000075 return fClip;
76 }
77
78 /**
robertphillips@google.com50c84da2013-04-01 18:18:49 +000079 Removes the command at the specified index
80 @param index The index of the command to delete
81 */
82 void deleteDrawCommandAt(int index);
83
84 /**
chudy@google.com902ebe52012-06-29 14:21:22 +000085 Returns the draw command at the given index.
86 @param index The index of the command
87 */
vjiaoblack95302da2016-07-21 10:25:54 -070088 SkDrawCommand *getDrawCommandAt(int index);
chudy@google.com902ebe52012-06-29 14:21:22 +000089
90 /**
chudy@google.comf1414322012-07-03 20:28:14 +000091 Returns length of draw command vector.
92 */
commit-bot@chromium.org0d4fe142013-07-15 22:47:14 +000093 int getSize() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +000094 return fCommandVector.count();
chudy@google.comf1414322012-07-03 20:28:14 +000095 }
96
chudy@google.com902ebe52012-06-29 14:21:22 +000097 /**
98 Toggles the visibility / execution of the draw command at index i with
99 the value of toggle.
100 */
101 void toggleCommand(int index, bool toggle);
102
ethannicholas402cd912016-02-10 12:57:30 -0800103 /**
ethannicholas0a0520a2016-02-12 12:06:53 -0800104 Returns a JSON object representing up to the Nth draw, where N is less than
halcanary9d524f22016-03-29 09:03:52 -0700105 SkDebugCanvas::getSize(). The encoder may use the UrlDataManager to store binary data such
ethannicholas0a0520a2016-02-12 12:06:53 -0800106 as images, referring to them via URLs embedded in the JSON.
ethannicholas402cd912016-02-10 12:57:30 -0800107 */
Brian Osmand8a90f92019-01-28 13:41:19 -0500108 void toJSON(SkJSONWriter& writer, UrlDataManager &urlDataManager, int n, SkCanvas *);
ethannicholas402cd912016-02-10 12:57:30 -0800109
Brian Osmand8a90f92019-01-28 13:41:19 -0500110 void toJSONOpList(SkJSONWriter& writer, int n, SkCanvas*);
joshualittae47aee2016-03-10 13:29:36 -0800111
Robert Phillipsdeaf5682017-09-06 13:07:21 -0400112 void detachCommands(SkTDArray<SkDrawCommand*>* dst) {
113 fCommandVector.swap(*dst);
114 }
115
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000116protected:
mtklein36352bf2015-03-25 18:17:31 -0700117 void willSave() override;
vjiaoblack95302da2016-07-21 10:25:54 -0700118 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &) override;
Mike Reed148b7fd2018-12-18 17:38:18 -0500119 bool onDoSaveBehind(const SkRect*) override;
mtklein36352bf2015-03-25 18:17:31 -0700120 void willRestore() override;
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000121
vjiaoblack95302da2016-07-21 10:25:54 -0700122 void didConcat(const SkMatrix &) override;
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000123
vjiaoblack95302da2016-07-21 10:25:54 -0700124 void didSetMatrix(const SkMatrix &) override;
125
reed97660cc2016-06-28 18:54:19 -0700126 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
mtklein36352bf2015-03-25 18:17:31 -0700127 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800128 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700129 const SkPaint& paint) override;
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000130
robertphillips9bafc302015-02-13 11:13:00 -0800131 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400132 const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
mtklein36352bf2015-03-25 18:17:31 -0700133 void onDrawPaint(const SkPaint&) override;
robertphillips9bafc302015-02-13 11:13:00 -0800134
mtklein36352bf2015-03-25 18:17:31 -0700135 void onDrawRect(const SkRect&, const SkPaint&) override;
Brian Salomon138a06d2019-03-15 14:33:31 +0000136 void onDrawEdgeAARect(const SkRect&, SkCanvas::QuadAAFlags, SkColor, SkBlendMode) override;
mtklein36352bf2015-03-25 18:17:31 -0700137 void onDrawOval(const SkRect&, const SkPaint&) override;
bsalomonac3aa242016-08-19 11:25:19 -0700138 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700139 void onDrawRRect(const SkRRect&, const SkPaint&) override;
140 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400141 void onDrawVerticesObject(const SkVertices*, const SkVertices::Bone bones[], int boneCount,
142 SkBlendMode, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700143 void onDrawPath(const SkPath&, const SkPaint&) override;
Brian Osmanc25e2692018-03-12 10:57:28 -0400144 void onDrawRegion(const SkRegion&, const SkPaint&) override;
mtklein36352bf2015-03-25 18:17:31 -0700145 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
Brian Osman78a76482018-05-18 16:59:13 -0400146 void onDrawBitmapLattice(const SkBitmap&, const Lattice&, const SkRect&,
147 const SkPaint*) override;
reed41af9662015-01-05 07:49:08 -0800148 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
reed562fe472015-07-28 07:35:14 -0700149 SrcRectConstraint) override;
mtklein36352bf2015-03-25 18:17:31 -0700150 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
Stan Ilievac42aeb2017-01-12 16:20:50 -0500151 void onDrawImageLattice(const SkImage* image, const Lattice& lattice,
152 const SkRect& dst, const SkPaint* paint) override;
reed41af9662015-01-05 07:49:08 -0800153 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700154 const SkPaint*, SrcRectConstraint) override;
Brian Salomon138a06d2019-03-15 14:33:31 +0000155 void onDrawImageSet(const ImageSetEntry[], int count, SkFilterQuality, SkBlendMode) override;
reed41af9662015-01-05 07:49:08 -0800156 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
mtklein36352bf2015-03-25 18:17:31 -0700157 const SkPaint*) override;
Brian Osmanc25e2692018-03-12 10:57:28 -0400158 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
159 const SkPaint*) override;
Brian Osman616f1cb2018-05-29 11:23:35 -0400160 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
161 int, SkBlendMode, const SkRect*, const SkPaint*) override;
Mike Reedc1f77742016-12-09 09:00:50 -0500162 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
163 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
164 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
165 void onClipRegion(const SkRegion& region, SkClipOp) override;
Derek Sollenberger6aab2ab2018-04-12 12:45:58 -0400166 void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000167
Brian Osmanc7611082018-05-29 14:55:50 -0400168 void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
mtklein36352bf2015-03-25 18:17:31 -0700169 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
robertphillips9b14f262014-06-04 05:40:44 -0700170
chudy@google.com902ebe52012-06-29 14:21:22 +0000171private:
robertphillips@google.com67baba42013-01-02 20:20:31 +0000172 SkTDArray<SkDrawCommand*> fCommandVector;
chudy@google.coma9e937c2012-08-03 17:32:05 +0000173 SkMatrix fMatrix;
174 SkIRect fClip;
robertphillips@google.com32bbcf82013-10-17 17:56:10 +0000175
robertphillips@google.comf4741c12013-02-06 20:13:54 +0000176 bool fOverdrawViz;
ethannicholas0a0520a2016-02-12 12:06:53 -0800177 SkColor fClipVizColor;
Brian Salomon144a5c52016-12-20 16:48:59 -0500178 bool fDrawGpuOpBounds;
chudy@google.com902ebe52012-06-29 14:21:22 +0000179
180 /**
Robert Phillipsdeaf5682017-09-06 13:07:21 -0400181 Adds the command to the class' vector of commands.
chudy@google.com902ebe52012-06-29 14:21:22 +0000182 @param command The draw command for execution
183 */
184 void addDrawCommand(SkDrawCommand* command);
chudy@google.com830b8792012-08-01 15:57:52 +0000185
joshualittae47aee2016-03-10 13:29:36 -0800186 GrAuditTrail* getAuditTrail(SkCanvas*);
187
Brian Salomon144a5c52016-12-20 16:48:59 -0500188 void drawAndCollectOps(int n, SkCanvas*);
joshualittae47aee2016-03-10 13:29:36 -0800189 void cleanupAuditTrail(SkCanvas*);
fmalita65cdb572015-03-26 07:24:48 -0700190
Brian Osmanc7611082018-05-29 14:55:50 -0400191 typedef SkCanvasVirtualEnforcer<SkCanvas> INHERITED;
chudy@google.com902ebe52012-06-29 14:21:22 +0000192};
193
194#endif