SkDebugCanvas.h revision f84ad8f7fc0194389a8099da2c5e8fff9f092890
1 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 13#include "SkCanvas.h" 14#include "SkDrawCommand.h" 15#include "SkPicture.h" 16#include "SkTArray.h" 17#include "SkString.h" 18 19class SkTexOverrideFilter; 20 21class SK_API SkDebugCanvas : public SkCanvas { 22public: 23 SkDebugCanvas(int width, int height); 24 virtual ~SkDebugCanvas(); 25 26 void toggleFilter(bool toggle); 27 28 /** 29 * Enable or disable overdraw visualization 30 */ 31 void setOverdrawViz(bool overdrawViz) { fOverdrawViz = overdrawViz; } 32 33 /** 34 * Enable or disable texure filtering override 35 */ 36 void overrideTexFiltering(bool overrideTexFiltering, SkPaint::FilterLevel level); 37 38 /** 39 Executes all draw calls to the canvas. 40 @param canvas The canvas being drawn to 41 */ 42 void draw(SkCanvas* canvas); 43 44 /** 45 Executes the draw calls in the specified range. 46 @param canvas The canvas being drawn to 47 @param i The beginning of the range 48 @param j The end of the range 49 TODO(chudy): Implement 50 */ 51 void drawRange(SkCanvas* canvas, int i, int j); 52 53 /** 54 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 57 */ 58 void drawTo(SkCanvas* canvas, int index); 59 60 /** 61 Returns the most recently calculated transformation matrix 62 */ 63 const SkMatrix& getCurrentMatrix() { 64 return fMatrix; 65 } 66 67 /** 68 Returns the most recently calculated clip 69 */ 70 const SkIRect& getCurrentClip() { 71 return fClip; 72 } 73 74 /** 75 Returns the index of the last draw command to write to the pixel at (x,y) 76 */ 77 int getCommandAtPoint(int x, int y, int index); 78 79 /** 80 Removes the command at the specified index 81 @param index The index of the command to delete 82 */ 83 void deleteDrawCommandAt(int index); 84 85 /** 86 Returns the draw command at the given index. 87 @param index The index of the command 88 */ 89 SkDrawCommand* getDrawCommandAt(int index); 90 91 /** 92 Sets the draw command for a given index. 93 @param index The index to overwrite 94 @param command The new command 95 */ 96 void setDrawCommandAt(int index, SkDrawCommand* command); 97 98 /** 99 Returns information about the command at the given index. 100 @param index The index of the command 101 */ 102 SkTDArray<SkString*>* getCommandInfo(int index); 103 104 /** 105 Returns the visibility of the command at the given index. 106 @param index The index of the command 107 */ 108 bool getDrawCommandVisibilityAt(int index); 109 110 /** 111 Returns the vector of draw commands 112 DEPRECATED: please use getDrawCommandAt and getSize instead 113 */ 114 const SkTDArray<SkDrawCommand*>& getDrawCommands() const; 115 116 /** 117 Returns the vector of draw commands. Do not use this entry 118 point - it is going away! 119 */ 120 SkTDArray<SkDrawCommand*>& getDrawCommands(); 121 122 /** 123 * Returns the string vector of draw commands 124 */ 125 SkTArray<SkString>* getDrawCommandsAsStrings() const; 126 127 /** 128 Returns length of draw command vector. 129 */ 130 int getSize() const { 131 return fCommandVector.count(); 132 } 133 134 /** 135 Toggles the visibility / execution of the draw command at index i with 136 the value of toggle. 137 */ 138 void toggleCommand(int index, bool toggle); 139 140 void setBounds(int width, int height) { 141 fWidth = width; 142 fHeight = height; 143 } 144 145 void setUserMatrix(SkMatrix matrix) { 146 fUserMatrix = matrix; 147 } 148 149//////////////////////////////////////////////////////////////////////////////// 150// Inherited from SkCanvas 151//////////////////////////////////////////////////////////////////////////////// 152 153 virtual void clear(SkColor) SK_OVERRIDE; 154 155 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE; 156 157 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE; 158 159 virtual bool clipRRect(const SkRRect& rrect, 160 SkRegion::Op op = SkRegion::kIntersect_Op, 161 bool doAntiAlias = false) SK_OVERRIDE; 162 163 virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE; 164 165 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE; 166 167 virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, 168 const SkPaint*) SK_OVERRIDE; 169 170 virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src, 171 const SkRect& dst, const SkPaint* paint, 172 DrawBitmapRectFlags flags) SK_OVERRIDE; 173 174 virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&, 175 const SkPaint*) SK_OVERRIDE; 176 177 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, 178 const SkRect& dst, const SkPaint*) SK_OVERRIDE; 179 180 virtual void drawData(const void*, size_t) SK_OVERRIDE; 181 182 virtual void beginCommentGroup(const char* description) SK_OVERRIDE; 183 184 virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE; 185 186 virtual void endCommentGroup() SK_OVERRIDE; 187 188 virtual void drawOval(const SkRect& oval, const SkPaint&) SK_OVERRIDE; 189 190 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; 191 192 virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE; 193 194 virtual void drawPicture(SkPicture& picture) SK_OVERRIDE; 195 196 virtual void drawPoints(PointMode, size_t count, const SkPoint pts[], 197 const SkPaint&) SK_OVERRIDE; 198 199 virtual void drawPosText(const void* text, size_t byteLength, 200 const SkPoint pos[], const SkPaint&) SK_OVERRIDE; 201 202 virtual void drawPosTextH(const void* text, size_t byteLength, 203 const SkScalar xpos[], SkScalar constY, 204 const SkPaint&) SK_OVERRIDE; 205 206 virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE; 207 208 virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint) SK_OVERRIDE; 209 210 virtual void drawSprite(const SkBitmap&, int left, int top, 211 const SkPaint*) SK_OVERRIDE; 212 213 virtual void drawText(const void* text, size_t byteLength, SkScalar x, 214 SkScalar y, const SkPaint&) SK_OVERRIDE; 215 216 virtual void drawTextOnPath(const void* text, size_t byteLength, 217 const SkPath& path, const SkMatrix* matrix, 218 const SkPaint&) SK_OVERRIDE; 219 220 virtual void drawVertices(VertexMode, int vertexCount, 221 const SkPoint vertices[], const SkPoint texs[], 222 const SkColor colors[], SkXfermode*, 223 const uint16_t indices[], int indexCount, 224 const SkPaint&) SK_OVERRIDE; 225 226 virtual void restore() SK_OVERRIDE; 227 228 virtual bool rotate(SkScalar degrees) SK_OVERRIDE; 229 230 virtual int save(SaveFlags) SK_OVERRIDE; 231 232 virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE; 233 234 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE; 235 236 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE; 237 238 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE; 239 240 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE; 241 242 static const int kVizImageHeight = 256; 243 static const int kVizImageWidth = 256; 244 245private: 246 SkTDArray<SkDrawCommand*> fCommandVector; 247 int fHeight; 248 int fWidth; 249 SkBitmap fBm; 250 bool fFilter; 251 int fIndex; 252 SkMatrix fUserMatrix; 253 SkMatrix fMatrix; 254 SkIRect fClip; 255 256 bool fOverdrawViz; 257 SkDrawFilter* fOverdrawFilter; 258 259 bool fOverrideTexFiltering; 260 SkTexOverrideFilter* fTexOverrideFilter; 261 262 /** 263 Number of unmatched save() calls at any point during a draw. 264 If there are any saveLayer() calls outstanding, we need to resolve 265 all of them, which in practice means resolving all save() calls, 266 to avoid corruption of our canvas. 267 */ 268 int fOutstandingSaveCount; 269 270 /** 271 Adds the command to the classes vector of commands. 272 @param command The draw command for execution 273 */ 274 void addDrawCommand(SkDrawCommand* command); 275 276 /** 277 Applies any panning and zooming the user has specified before 278 drawing anything else into the canvas. 279 */ 280 void applyUserTransform(SkCanvas* canvas); 281 282 typedef SkCanvas INHERITED; 283}; 284 285#endif 286