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