DebugCanvas.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  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. #ifndef SKDEBUGCANVAS_H_
  8. #define SKDEBUGCANVAS_H_
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkCanvasVirtualEnforcer.h"
  11. #include "include/core/SkPath.h"
  12. #include "include/core/SkString.h"
  13. #include "include/core/SkVertices.h"
  14. #include "include/pathops/SkPathOps.h"
  15. #include "include/private/SkTArray.h"
  16. #include "tools/UrlDataManager.h"
  17. #include "tools/debugger/DrawCommand.h"
  18. class GrAuditTrail;
  19. class SkNWayCanvas;
  20. class SkPicture;
  21. class DebugCanvas : public SkCanvasVirtualEnforcer<SkCanvas> {
  22. public:
  23. DebugCanvas(int width, int height);
  24. DebugCanvas(SkIRect bounds);
  25. ~DebugCanvas() override;
  26. /**
  27. * Enable or disable overdraw visualization
  28. */
  29. void setOverdrawViz(bool overdrawViz);
  30. bool getOverdrawViz() const { return fOverdrawViz; }
  31. /**
  32. * Set the color of the clip visualization. An alpha of zero renders the clip invisible.
  33. */
  34. void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; }
  35. void setDrawGpuOpBounds(bool drawGpuOpBounds) { fDrawGpuOpBounds = drawGpuOpBounds; }
  36. bool getDrawGpuOpBounds() const { return fDrawGpuOpBounds; }
  37. /**
  38. Executes all draw calls to the canvas.
  39. @param canvas The canvas being drawn to
  40. */
  41. void draw(SkCanvas* canvas);
  42. /**
  43. Executes the draw calls up to the specified index.
  44. @param canvas The canvas being drawn to
  45. @param index The index of the final command being executed
  46. @param m an optional Mth gpu op to highlight, or -1
  47. */
  48. void drawTo(SkCanvas* canvas, int index, int m = -1);
  49. /**
  50. Returns the most recently calculated transformation matrix
  51. */
  52. const SkMatrix& getCurrentMatrix() { return fMatrix; }
  53. /**
  54. Returns the most recently calculated clip
  55. */
  56. const SkIRect& getCurrentClip() { return fClip; }
  57. /**
  58. Removes the command at the specified index
  59. @param index The index of the command to delete
  60. */
  61. void deleteDrawCommandAt(int index);
  62. /**
  63. Returns the draw command at the given index.
  64. @param index The index of the command
  65. */
  66. DrawCommand* getDrawCommandAt(int index);
  67. /**
  68. Returns length of draw command vector.
  69. */
  70. int getSize() const { return fCommandVector.count(); }
  71. /**
  72. Toggles the visibility / execution of the draw command at index i with
  73. the value of toggle.
  74. */
  75. void toggleCommand(int index, bool toggle);
  76. /**
  77. Returns a JSON object representing up to the Nth draw, where N is less than
  78. DebugCanvas::getSize(). The encoder may use the UrlDataManager to store binary data such
  79. as images, referring to them via URLs embedded in the JSON.
  80. */
  81. void toJSON(SkJSONWriter& writer, UrlDataManager& urlDataManager, int n, SkCanvas*);
  82. void toJSONOpList(SkJSONWriter& writer, int n, SkCanvas*);
  83. void detachCommands(SkTDArray<DrawCommand*>* dst) { fCommandVector.swap(*dst); }
  84. protected:
  85. void willSave() override;
  86. SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
  87. bool onDoSaveBehind(const SkRect*) override;
  88. void willRestore() override;
  89. void didConcat(const SkMatrix&) override;
  90. void didSetMatrix(const SkMatrix&) override;
  91. void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
  92. void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
  93. void onDrawTextBlob(const SkTextBlob* blob,
  94. SkScalar x,
  95. SkScalar y,
  96. const SkPaint& paint) override;
  97. void onDrawPatch(const SkPoint cubics[12],
  98. const SkColor colors[4],
  99. const SkPoint texCoords[4],
  100. SkBlendMode,
  101. const SkPaint& paint) override;
  102. void onDrawPaint(const SkPaint&) override;
  103. void onDrawBehind(const SkPaint&) override;
  104. void onDrawRect(const SkRect&, const SkPaint&) override;
  105. void onDrawOval(const SkRect&, const SkPaint&) override;
  106. void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
  107. void onDrawRRect(const SkRRect&, const SkPaint&) override;
  108. void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
  109. void onDrawVerticesObject(const SkVertices*,
  110. const SkVertices::Bone bones[],
  111. int boneCount,
  112. SkBlendMode,
  113. const SkPaint&) override;
  114. void onDrawPath(const SkPath&, const SkPaint&) override;
  115. void onDrawRegion(const SkRegion&, const SkPaint&) override;
  116. void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
  117. void onDrawBitmapLattice(const SkBitmap&,
  118. const Lattice&,
  119. const SkRect&,
  120. const SkPaint*) override;
  121. void onDrawBitmapRect(const SkBitmap&,
  122. const SkRect* src,
  123. const SkRect& dst,
  124. const SkPaint*,
  125. SrcRectConstraint) override;
  126. void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
  127. void onDrawImageLattice(const SkImage* image,
  128. const Lattice& lattice,
  129. const SkRect& dst,
  130. const SkPaint* paint) override;
  131. void onDrawImageRect(const SkImage*,
  132. const SkRect* src,
  133. const SkRect& dst,
  134. const SkPaint*,
  135. SrcRectConstraint) override;
  136. void onDrawBitmapNine(const SkBitmap&,
  137. const SkIRect& center,
  138. const SkRect& dst,
  139. const SkPaint*) override;
  140. void onDrawImageNine(const SkImage*,
  141. const SkIRect& center,
  142. const SkRect& dst,
  143. const SkPaint*) override;
  144. void onDrawAtlas(const SkImage*,
  145. const SkRSXform[],
  146. const SkRect[],
  147. const SkColor[],
  148. int,
  149. SkBlendMode,
  150. const SkRect*,
  151. const SkPaint*) override;
  152. void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
  153. void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
  154. void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
  155. void onClipRegion(const SkRegion& region, SkClipOp) override;
  156. void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
  157. void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
  158. void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
  159. void onDrawEdgeAAQuad(const SkRect&,
  160. const SkPoint[4],
  161. QuadAAFlags,
  162. SkColor,
  163. SkBlendMode) override;
  164. void onDrawEdgeAAImageSet(const ImageSetEntry[],
  165. int count,
  166. const SkPoint[],
  167. const SkMatrix[],
  168. const SkPaint*,
  169. SrcRectConstraint) override;
  170. private:
  171. SkTDArray<DrawCommand*> fCommandVector;
  172. SkMatrix fMatrix;
  173. SkIRect fClip;
  174. bool fOverdrawViz;
  175. SkColor fClipVizColor;
  176. bool fDrawGpuOpBounds;
  177. /**
  178. Adds the command to the class' vector of commands.
  179. @param command The draw command for execution
  180. */
  181. void addDrawCommand(DrawCommand* command);
  182. GrAuditTrail* getAuditTrail(SkCanvas*);
  183. void drawAndCollectOps(int n, SkCanvas*);
  184. void cleanupAuditTrail(SkCanvas*);
  185. typedef SkCanvasVirtualEnforcer<SkCanvas> INHERITED;
  186. };
  187. #endif