chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 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 | #include <iostream> |
| 11 | #include "SkDebugCanvas.h" |
| 12 | #include "SkDrawCommand.h" |
| 13 | |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 14 | SkDebugCanvas::SkDebugCanvas(int width, int height) { |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 15 | // TODO(chudy): Free up memory from all draw commands in destructor. |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 16 | fWidth = width; |
| 17 | fHeight = height; |
chudy@google.com | b9ddd4e | 2012-07-10 14:14:50 +0000 | [diff] [blame] | 18 | fBm.setConfig(SkBitmap::kNo_Config, fWidth, fHeight); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 19 | this->setBitmapDevice(fBm); |
| 20 | fFilter = false; |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 21 | fIndex = 0; |
| 22 | fUserOffset.set(0,0); |
| 23 | fUserScale = 1.0; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | SkDebugCanvas::~SkDebugCanvas() {} |
| 27 | |
| 28 | void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) { |
| 29 | commandVector.push_back(command); |
| 30 | } |
| 31 | |
| 32 | void SkDebugCanvas::draw(SkCanvas* canvas) { |
| 33 | if(!commandVector.empty()) { |
| 34 | for(it = commandVector.begin(); it != commandVector.end(); ++it) { |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 35 | if ((*it)->isVisible()) { |
chudy@google.com | 0ab0339 | 2012-07-28 20:16:11 +0000 | [diff] [blame] | 36 | (*it)->execute(canvas); |
| 37 | } |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 38 | } |
| 39 | } |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 40 | fIndex = commandVector.size() - 1; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 41 | } |
| 42 | |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 43 | void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) { |
| 44 | canvas->translate(fUserOffset.fX, fUserOffset.fY); |
| 45 | if (fUserScale < 0) { |
| 46 | canvas->scale((1.0 / -fUserScale), (1.0 / -fUserScale)); |
| 47 | } else if (fUserScale > 0) { |
| 48 | canvas->scale(fUserScale, fUserScale); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) { |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 53 | SkBitmap bitmap; |
| 54 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
| 55 | bitmap.allocPixels(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 56 | |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 57 | SkCanvas canvas(bitmap); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 58 | canvas.translate(-x, -y); |
| 59 | applyUserTransform(&canvas); |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 60 | |
| 61 | int layer = 0; |
chudy@google.com | 751961d | 2012-07-31 20:07:42 +0000 | [diff] [blame] | 62 | SkColor prev = bitmap.getColor(0,0); |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 63 | for (int i = 0; i < index; i++) { |
| 64 | if (commandVector[i]->isVisible()) { |
| 65 | commandVector[i]->execute(&canvas); |
| 66 | } |
| 67 | if (prev != bitmap.getColor(0,0)) { |
| 68 | layer = i; |
| 69 | } |
| 70 | prev = bitmap.getColor(0,0); |
| 71 | } |
| 72 | return layer; |
| 73 | } |
| 74 | |
| 75 | void SkDebugCanvas::drawTo(SkCanvas* canvas, int index) { |
| 76 | int counter = 0; |
| 77 | SkASSERT(!commandVector.empty()); |
chudy@google.com | 751961d | 2012-07-31 20:07:42 +0000 | [diff] [blame] | 78 | SkASSERT(index < (int)commandVector.size()); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 79 | int i; |
| 80 | |
| 81 | // This only works assuming the canvas and device are the same ones that |
| 82 | // were previously drawn into because they need to preserve all saves |
| 83 | // and restores. |
| 84 | if (fIndex < index) { |
| 85 | i = fIndex + 1; |
| 86 | } else { |
| 87 | i = 0; |
| 88 | canvas->clear(0); |
| 89 | canvas->resetMatrix(); |
| 90 | applyUserTransform(canvas); |
| 91 | } |
| 92 | |
| 93 | for (; i <= index; i++) { |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 94 | if (i == index && fFilter) { |
| 95 | SkPaint p; |
| 96 | p.setColor(0xAAFFFFFF); |
| 97 | canvas->save(); |
| 98 | canvas->resetMatrix(); |
| 99 | SkRect mask; |
| 100 | mask.set(SkIntToScalar(0), SkIntToScalar(0), |
| 101 | SkIntToScalar(fWidth), SkIntToScalar(fHeight)); |
| 102 | canvas->clipRect(mask, SkRegion::kReplace_Op, false); |
| 103 | canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0), |
| 104 | SkIntToScalar(fWidth), SkIntToScalar(fHeight), p); |
| 105 | canvas->restore(); |
| 106 | } |
| 107 | |
| 108 | if (commandVector[i]->isVisible()) { |
| 109 | commandVector[i]->execute(canvas); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
chudy@google.com | a9e937c | 2012-08-03 17:32:05 +0000 | [diff] [blame] | 112 | fMatrix = canvas->getTotalMatrix(); |
| 113 | fClip = canvas->getTotalClip().getBounds(); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 114 | fIndex = index; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) { |
chudy@google.com | 751961d | 2012-07-31 20:07:42 +0000 | [diff] [blame] | 118 | SkASSERT(index < (int)commandVector.size()); |
chudy@google.com | 7e4cfbf | 2012-07-17 15:40:51 +0000 | [diff] [blame] | 119 | return commandVector[index]; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | std::vector<std::string>* SkDebugCanvas::getCommandInfoAt(int index) { |
chudy@google.com | 751961d | 2012-07-31 20:07:42 +0000 | [diff] [blame] | 123 | SkASSERT(index < (int)commandVector.size()); |
chudy@google.com | 7e4cfbf | 2012-07-17 15:40:51 +0000 | [diff] [blame] | 124 | return commandVector[index]->Info(); |
| 125 | } |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 126 | |
chudy@google.com | 7e4cfbf | 2012-07-17 15:40:51 +0000 | [diff] [blame] | 127 | bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) { |
chudy@google.com | 751961d | 2012-07-31 20:07:42 +0000 | [diff] [blame] | 128 | SkASSERT(index < (int)commandVector.size()); |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 129 | return commandVector[index]->isVisible(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | std::vector<SkDrawCommand*> SkDebugCanvas::getDrawCommands() { |
| 133 | return commandVector; |
| 134 | } |
| 135 | |
| 136 | // TODO(chudy): Free command string memory. |
| 137 | std::vector<std::string>* SkDebugCanvas::getDrawCommandsAsStrings() { |
| 138 | std::vector<std::string>* commandString = new std::vector<std::string>(); |
| 139 | if (!commandVector.empty()) { |
| 140 | for(it = commandVector.begin(); it != commandVector.end(); ++it) { |
| 141 | commandString->push_back((*it)->toString()); |
| 142 | } |
| 143 | } |
| 144 | return commandString; |
| 145 | } |
| 146 | |
| 147 | void SkDebugCanvas::toggleFilter(bool toggle) { |
| 148 | fFilter = toggle; |
| 149 | } |
| 150 | |
| 151 | void SkDebugCanvas::clear(SkColor color) { |
| 152 | addDrawCommand(new Clear(color)); |
| 153 | } |
| 154 | |
| 155 | bool SkDebugCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { |
| 156 | addDrawCommand(new ClipPath(path, op, doAA)); |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | bool SkDebugCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { |
| 161 | addDrawCommand(new ClipRect(rect, op, doAA)); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | bool SkDebugCanvas::clipRegion(const SkRegion& region, SkRegion::Op op) { |
| 166 | addDrawCommand(new ClipRegion(region, op)); |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | bool SkDebugCanvas::concat(const SkMatrix& matrix) { |
| 171 | addDrawCommand(new Concat(matrix)); |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | void SkDebugCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left, |
| 176 | SkScalar top, const SkPaint* paint = NULL) { |
| 177 | addDrawCommand(new DrawBitmap(bitmap, left, top, paint)); |
| 178 | } |
| 179 | |
| 180 | void SkDebugCanvas::drawBitmapRect(const SkBitmap& bitmap, |
| 181 | const SkIRect* src, const SkRect& dst, const SkPaint* paint) { |
| 182 | addDrawCommand(new DrawBitmapRect(bitmap, src, dst, paint)); |
| 183 | } |
| 184 | |
| 185 | void SkDebugCanvas::drawBitmapMatrix(const SkBitmap& bitmap, |
| 186 | const SkMatrix& matrix, const SkPaint* paint) { |
| 187 | addDrawCommand(new DrawBitmapMatrix(bitmap, matrix, paint)); |
| 188 | } |
| 189 | |
| 190 | void SkDebugCanvas::drawBitmapNine(const SkBitmap& bitmap, |
| 191 | const SkIRect& center, const SkRect& dst, const SkPaint* paint) { |
| 192 | addDrawCommand(new DrawBitmapNine(bitmap, center, dst, paint)); |
| 193 | } |
| 194 | |
| 195 | void SkDebugCanvas::drawData(const void* data, size_t length) { |
| 196 | addDrawCommand(new DrawData(data, length)); |
| 197 | } |
| 198 | |
| 199 | void SkDebugCanvas::drawPaint(const SkPaint& paint) { |
| 200 | addDrawCommand(new DrawPaint(paint)); |
| 201 | } |
| 202 | |
| 203 | void SkDebugCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
| 204 | addDrawCommand(new DrawPath(path, paint)); |
| 205 | } |
| 206 | |
| 207 | void SkDebugCanvas::drawPicture(SkPicture& picture) { |
| 208 | addDrawCommand(new DrawPicture(picture)); |
| 209 | } |
| 210 | |
| 211 | void SkDebugCanvas::drawPoints(PointMode mode, size_t count, |
| 212 | const SkPoint pts[], const SkPaint& paint) { |
| 213 | addDrawCommand(new DrawPoints(mode, count, pts, paint)); |
| 214 | } |
| 215 | |
| 216 | void SkDebugCanvas::drawPosText(const void* text, size_t byteLength, |
| 217 | const SkPoint pos[], const SkPaint& paint) { |
| 218 | addDrawCommand(new DrawPosText(text, byteLength, pos, paint)); |
| 219 | } |
| 220 | |
| 221 | void SkDebugCanvas::drawPosTextH(const void* text, size_t byteLength, |
| 222 | const SkScalar xpos[], SkScalar constY, const SkPaint& paint) { |
| 223 | addDrawCommand(new DrawPosTextH(text, byteLength, xpos, constY, paint)); |
| 224 | } |
| 225 | |
| 226 | void SkDebugCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { |
| 227 | // NOTE(chudy): Messing up when renamed to DrawRect... Why? |
| 228 | addDrawCommand(new DrawRectC(rect, paint)); |
| 229 | } |
| 230 | |
| 231 | void SkDebugCanvas::drawSprite(const SkBitmap& bitmap, int left, int top, |
| 232 | const SkPaint* paint = NULL) { |
| 233 | addDrawCommand(new DrawSprite(bitmap, left, top, paint)); |
| 234 | } |
| 235 | |
| 236 | void SkDebugCanvas::drawText(const void* text, size_t byteLength, SkScalar x, |
| 237 | SkScalar y, const SkPaint& paint) { |
| 238 | addDrawCommand(new DrawTextC(text, byteLength, x, y, paint)); |
| 239 | } |
| 240 | |
| 241 | void SkDebugCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 242 | const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) { |
| 243 | addDrawCommand(new DrawTextOnPath(text, byteLength, path, matrix, paint)); |
| 244 | } |
| 245 | |
| 246 | void SkDebugCanvas::drawVertices(VertexMode vmode, int vertexCount, |
| 247 | const SkPoint vertices[], const SkPoint texs[], const SkColor colors[], |
| 248 | SkXfermode*, const uint16_t indices[], int indexCount, |
| 249 | const SkPaint& paint) { |
| 250 | addDrawCommand(new DrawVertices(vmode, vertexCount, vertices, texs, colors, |
| 251 | NULL, indices, indexCount, paint)); |
| 252 | } |
| 253 | |
| 254 | void SkDebugCanvas::restore() { |
| 255 | addDrawCommand(new Restore()); |
| 256 | } |
| 257 | |
| 258 | bool SkDebugCanvas::rotate(SkScalar degrees) { |
| 259 | addDrawCommand(new Rotate(degrees)); |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | int SkDebugCanvas::save(SaveFlags flags) { |
| 264 | addDrawCommand(new Save(flags)); |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | int SkDebugCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 269 | SaveFlags flags) { |
| 270 | addDrawCommand(new SaveLayer(bounds, paint, flags)); |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | bool SkDebugCanvas::scale(SkScalar sx, SkScalar sy) { |
| 275 | addDrawCommand(new Scale(sx, sy)); |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | void SkDebugCanvas::setMatrix(const SkMatrix& matrix) { |
| 280 | addDrawCommand(new SetMatrix(matrix)); |
| 281 | } |
| 282 | |
| 283 | bool SkDebugCanvas::skew(SkScalar sx, SkScalar sy) { |
| 284 | addDrawCommand(new Skew(sx, sy)); |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | bool SkDebugCanvas::translate(SkScalar dx, SkScalar dy) { |
| 289 | addDrawCommand(new Translate(dx, dy)); |
| 290 | return true; |
| 291 | } |
| 292 | |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 293 | void SkDebugCanvas::toggleCommand(int index, bool toggle) { |
chudy@google.com | 751961d | 2012-07-31 20:07:42 +0000 | [diff] [blame] | 294 | SkASSERT(index < (int)commandVector.size()); |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 295 | commandVector[index]->setVisible(toggle); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 296 | } |