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