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