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 | |
robertphillips@google.com | f4741c1 | 2013-02-06 20:13:54 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 11 | #include "SkDebugCanvas.h" |
| 12 | #include "SkDrawCommand.h" |
robertphillips@google.com | f4741c1 | 2013-02-06 20:13:54 +0000 | [diff] [blame] | 13 | #include "SkDrawFilter.h" |
robertphillips@google.com | 6dec8fc | 2012-11-21 17:11:02 +0000 | [diff] [blame] | 14 | #include "SkDevice.h" |
robertphillips@google.com | f4741c1 | 2013-02-06 20:13:54 +0000 | [diff] [blame] | 15 | #include "SkXfermode.h" |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 16 | |
bsalomon@google.com | 50c79d8 | 2013-01-08 20:31:53 +0000 | [diff] [blame] | 17 | #ifdef SK_BUILD_FOR_WIN |
| 18 | // iostream includes xlocale which generates warning 4530 because we're compiling without |
| 19 | // exceptions |
| 20 | #pragma warning(push) |
| 21 | #pragma warning(disable : 4530) |
| 22 | #endif |
| 23 | #include <iostream> |
| 24 | #ifdef SK_BUILD_FOR_WIN |
| 25 | #pragma warning(pop) |
| 26 | #endif |
| 27 | |
reed@google.com | 6ae24e0 | 2012-09-26 13:44:13 +0000 | [diff] [blame] | 28 | static SkBitmap make_noconfig_bm(int width, int height) { |
| 29 | SkBitmap bm; |
| 30 | bm.setConfig(SkBitmap::kNo_Config, width, height); |
| 31 | return bm; |
| 32 | } |
| 33 | |
| 34 | SkDebugCanvas::SkDebugCanvas(int width, int height) |
tomhudson@google.com | 0699e02 | 2012-11-27 16:09:42 +0000 | [diff] [blame] | 35 | : INHERITED(make_noconfig_bm(width, height)) |
robertphillips@google.com | f4741c1 | 2013-02-06 20:13:54 +0000 | [diff] [blame] | 36 | , fOverdrawViz(false) |
scroggo@google.com | 06d6ac6 | 2013-02-08 21:16:19 +0000 | [diff] [blame^] | 37 | , fOverdrawFilter(NULL) |
| 38 | , fOutstandingSaveCount(0) { |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 39 | // TODO(chudy): Free up memory from all draw commands in destructor. |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 40 | fWidth = width; |
| 41 | fHeight = height; |
reed@google.com | 6ae24e0 | 2012-09-26 13:44:13 +0000 | [diff] [blame] | 42 | // do we need fBm anywhere? |
chudy@google.com | b9ddd4e | 2012-07-10 14:14:50 +0000 | [diff] [blame] | 43 | fBm.setConfig(SkBitmap::kNo_Config, fWidth, fHeight); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 44 | fFilter = false; |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 45 | fIndex = 0; |
bungeman@google.com | e8cc6e8 | 2013-01-17 16:30:56 +0000 | [diff] [blame] | 46 | fUserMatrix.reset(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 47 | } |
| 48 | |
chudy@google.com | 9cda6f7 | 2012-08-07 15:08:33 +0000 | [diff] [blame] | 49 | SkDebugCanvas::~SkDebugCanvas() { |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 50 | fCommandVector.deleteAll(); |
robertphillips@google.com | f4741c1 | 2013-02-06 20:13:54 +0000 | [diff] [blame] | 51 | SkSafeUnref(fOverdrawFilter); |
chudy@google.com | 9cda6f7 | 2012-08-07 15:08:33 +0000 | [diff] [blame] | 52 | } |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 53 | |
| 54 | void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) { |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 55 | fCommandVector.push(command); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void SkDebugCanvas::draw(SkCanvas* canvas) { |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 59 | if(!fCommandVector.isEmpty()) { |
| 60 | for (int i = 0; i < fCommandVector.count(); i++) { |
| 61 | if (fCommandVector[i]->isVisible()) { |
| 62 | fCommandVector[i]->execute(canvas); |
chudy@google.com | 0ab0339 | 2012-07-28 20:16:11 +0000 | [diff] [blame] | 63 | } |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 66 | fIndex = fCommandVector.count() - 1; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 67 | } |
| 68 | |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 69 | void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) { |
bungeman@google.com | e8cc6e8 | 2013-01-17 16:30:56 +0000 | [diff] [blame] | 70 | canvas->concat(fUserMatrix); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) { |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 74 | SkBitmap bitmap; |
| 75 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
| 76 | bitmap.allocPixels(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 77 | |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 78 | SkCanvas canvas(bitmap); |
robertphillips@google.com | 94acc70 | 2012-09-06 18:43:21 +0000 | [diff] [blame] | 79 | canvas.translate(SkIntToScalar(-x), SkIntToScalar(-y)); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 80 | applyUserTransform(&canvas); |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 81 | |
| 82 | int layer = 0; |
chudy@google.com | 751961d | 2012-07-31 20:07:42 +0000 | [diff] [blame] | 83 | SkColor prev = bitmap.getColor(0,0); |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 84 | for (int i = 0; i < index; i++) { |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 85 | if (fCommandVector[i]->isVisible()) { |
| 86 | fCommandVector[i]->execute(&canvas); |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 87 | } |
| 88 | if (prev != bitmap.getColor(0,0)) { |
| 89 | layer = i; |
| 90 | } |
| 91 | prev = bitmap.getColor(0,0); |
| 92 | } |
| 93 | return layer; |
| 94 | } |
| 95 | |
bsalomon@google.com | 383e234 | 2013-02-06 21:44:21 +0000 | [diff] [blame] | 96 | static SkPMColor OverdrawXferModeProc(SkPMColor src, SkPMColor dst) { |
robertphillips@google.com | f4741c1 | 2013-02-06 20:13:54 +0000 | [diff] [blame] | 97 | // This table encodes the color progression of the overdraw visualization |
| 98 | static const SkPMColor gTable[] = { |
| 99 | SkPackARGB32(0x00, 0x00, 0x00, 0x00), |
| 100 | SkPackARGB32(0xFF, 128, 158, 255), |
| 101 | SkPackARGB32(0xFF, 170, 185, 212), |
| 102 | SkPackARGB32(0xFF, 213, 195, 170), |
| 103 | SkPackARGB32(0xFF, 255, 192, 127), |
| 104 | SkPackARGB32(0xFF, 255, 185, 85), |
| 105 | SkPackARGB32(0xFF, 255, 165, 42), |
| 106 | SkPackARGB32(0xFF, 255, 135, 0), |
| 107 | SkPackARGB32(0xFF, 255, 95, 0), |
| 108 | SkPackARGB32(0xFF, 255, 50, 0), |
| 109 | SkPackARGB32(0xFF, 255, 0, 0) |
| 110 | }; |
| 111 | |
robertphillips@google.com | 0b256e1 | 2013-02-06 20:42:14 +0000 | [diff] [blame] | 112 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTable)-1; ++i) { |
robertphillips@google.com | f4741c1 | 2013-02-06 20:13:54 +0000 | [diff] [blame] | 113 | if (gTable[i] == dst) { |
| 114 | return gTable[i+1]; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return gTable[SK_ARRAY_COUNT(gTable)-1]; |
| 119 | } |
| 120 | |
| 121 | // The OverdrawFilter modifies every paint to use an SkProcXfermode which |
| 122 | // in turn invokes OverdrawXferModeProc |
| 123 | class OverdrawFilter : public SkDrawFilter { |
| 124 | public: |
| 125 | OverdrawFilter() { |
| 126 | fXferMode = new SkProcXfermode(OverdrawXferModeProc); |
| 127 | } |
| 128 | |
| 129 | virtual ~OverdrawFilter() { |
| 130 | delete fXferMode; |
| 131 | } |
| 132 | |
| 133 | virtual bool filter(SkPaint* p, Type) SK_OVERRIDE { |
| 134 | p->setXfermode(fXferMode); |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | protected: |
| 139 | SkXfermode* fXferMode; |
| 140 | |
| 141 | private: |
| 142 | typedef SkDrawFilter INHERITED; |
| 143 | }; |
| 144 | |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 145 | void SkDebugCanvas::drawTo(SkCanvas* canvas, int index) { |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 146 | SkASSERT(!fCommandVector.isEmpty()); |
| 147 | SkASSERT(index < fCommandVector.count()); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 148 | int i; |
| 149 | |
| 150 | // This only works assuming the canvas and device are the same ones that |
| 151 | // were previously drawn into because they need to preserve all saves |
| 152 | // and restores. |
| 153 | if (fIndex < index) { |
| 154 | i = fIndex + 1; |
| 155 | } else { |
tomhudson@google.com | 0699e02 | 2012-11-27 16:09:42 +0000 | [diff] [blame] | 156 | for (int j = 0; j < fOutstandingSaveCount; j++) { |
| 157 | canvas->restore(); |
| 158 | } |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 159 | i = 0; |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 160 | canvas->clear(SK_ColorTRANSPARENT); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 161 | canvas->resetMatrix(); |
skia.committer@gmail.com | 04ba448 | 2012-09-07 02:01:30 +0000 | [diff] [blame] | 162 | SkRect rect = SkRect::MakeWH(SkIntToScalar(fWidth), |
robertphillips@google.com | 94acc70 | 2012-09-06 18:43:21 +0000 | [diff] [blame] | 163 | SkIntToScalar(fHeight)); |
chudy@google.com | 4c7962e | 2012-08-14 19:38:31 +0000 | [diff] [blame] | 164 | canvas->clipRect(rect, SkRegion::kReplace_Op ); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 165 | applyUserTransform(canvas); |
tomhudson@google.com | 0699e02 | 2012-11-27 16:09:42 +0000 | [diff] [blame] | 166 | fOutstandingSaveCount = 0; |
robertphillips@google.com | f4741c1 | 2013-02-06 20:13:54 +0000 | [diff] [blame] | 167 | |
| 168 | // The setting of the draw filter has to go here (rather than in |
| 169 | // SkRasterWidget) due to the canvas restores this class performs. |
| 170 | // Since the draw filter is stored in the layer stack if we |
| 171 | // call setDrawFilter on anything but the root layer odd things happen |
| 172 | if (fOverdrawViz) { |
| 173 | if (NULL == fOverdrawFilter) { |
| 174 | fOverdrawFilter = new OverdrawFilter; |
| 175 | } |
| 176 | |
| 177 | if (fOverdrawFilter != canvas->getDrawFilter()) { |
| 178 | canvas->setDrawFilter(fOverdrawFilter); |
| 179 | } |
| 180 | } else { |
| 181 | canvas->setDrawFilter(NULL); |
| 182 | } |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | for (; i <= index; i++) { |
chudy@google.com | 0b5bbb0 | 2012-07-31 19:55:32 +0000 | [diff] [blame] | 186 | if (i == index && fFilter) { |
| 187 | SkPaint p; |
| 188 | p.setColor(0xAAFFFFFF); |
| 189 | canvas->save(); |
| 190 | canvas->resetMatrix(); |
| 191 | SkRect mask; |
| 192 | mask.set(SkIntToScalar(0), SkIntToScalar(0), |
| 193 | SkIntToScalar(fWidth), SkIntToScalar(fHeight)); |
| 194 | canvas->clipRect(mask, SkRegion::kReplace_Op, false); |
| 195 | canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0), |
| 196 | SkIntToScalar(fWidth), SkIntToScalar(fHeight), p); |
| 197 | canvas->restore(); |
| 198 | } |
| 199 | |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 200 | if (fCommandVector[i]->isVisible()) { |
| 201 | fCommandVector[i]->execute(canvas); |
| 202 | fCommandVector[i]->trackSaveState(&fOutstandingSaveCount); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
chudy@google.com | a9e937c | 2012-08-03 17:32:05 +0000 | [diff] [blame] | 205 | fMatrix = canvas->getTotalMatrix(); |
| 206 | fClip = canvas->getTotalClip().getBounds(); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 207 | fIndex = index; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) { |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 211 | SkASSERT(index < fCommandVector.count()); |
| 212 | return fCommandVector[index]; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 213 | } |
| 214 | |
chudy@google.com | 97cee97 | 2012-08-07 20:41:37 +0000 | [diff] [blame] | 215 | SkTDArray<SkString*>* SkDebugCanvas::getCommandInfo(int index) { |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 216 | SkASSERT(index < fCommandVector.count()); |
| 217 | return fCommandVector[index]->Info(); |
chudy@google.com | 7e4cfbf | 2012-07-17 15:40:51 +0000 | [diff] [blame] | 218 | } |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 219 | |
chudy@google.com | 7e4cfbf | 2012-07-17 15:40:51 +0000 | [diff] [blame] | 220 | bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) { |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 221 | SkASSERT(index < fCommandVector.count()); |
| 222 | return fCommandVector[index]->isVisible(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 223 | } |
| 224 | |
robertphillips@google.com | 8a1cdae | 2012-11-19 20:44:29 +0000 | [diff] [blame] | 225 | const SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() const { |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 226 | return fCommandVector; |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // TODO(chudy): Free command string memory. |
robertphillips@google.com | 8a1cdae | 2012-11-19 20:44:29 +0000 | [diff] [blame] | 230 | SkTArray<SkString>* SkDebugCanvas::getDrawCommandsAsStrings() const { |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 231 | SkTArray<SkString>* commandString = new SkTArray<SkString>(fCommandVector.count()); |
| 232 | if (!fCommandVector.isEmpty()) { |
| 233 | for (int i = 0; i < fCommandVector.count(); i ++) { |
| 234 | commandString->push_back() = fCommandVector[i]->toString(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | return commandString; |
| 238 | } |
| 239 | |
| 240 | void SkDebugCanvas::toggleFilter(bool toggle) { |
| 241 | fFilter = toggle; |
| 242 | } |
| 243 | |
| 244 | void SkDebugCanvas::clear(SkColor color) { |
| 245 | addDrawCommand(new Clear(color)); |
| 246 | } |
| 247 | |
robertphillips@google.com | 6dec8fc | 2012-11-21 17:11:02 +0000 | [diff] [blame] | 248 | static SkBitmap createBitmap(const SkPath& path) { |
| 249 | SkBitmap bitmap; |
skia.committer@gmail.com | 1c9c0d3 | 2012-11-22 02:02:41 +0000 | [diff] [blame] | 250 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 251 | SkDebugCanvas::kVizImageWidth, |
| 252 | SkDebugCanvas::kVizImageHeight); |
robertphillips@google.com | 6dec8fc | 2012-11-21 17:11:02 +0000 | [diff] [blame] | 253 | bitmap.allocPixels(); |
| 254 | bitmap.eraseColor(SK_ColorWHITE); |
| 255 | SkDevice* device = new SkDevice(bitmap); |
| 256 | |
| 257 | SkCanvas canvas(device); |
| 258 | device->unref(); |
| 259 | |
| 260 | const SkRect& bounds = path.getBounds(); |
| 261 | |
| 262 | if (bounds.width() > bounds.height()) { |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 263 | canvas.scale(SkDoubleToScalar((0.9*SkDebugCanvas::kVizImageWidth)/bounds.width()), |
| 264 | SkDoubleToScalar((0.9*SkDebugCanvas::kVizImageHeight)/bounds.width())); |
robertphillips@google.com | 6dec8fc | 2012-11-21 17:11:02 +0000 | [diff] [blame] | 265 | } else { |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 266 | canvas.scale(SkDoubleToScalar((0.9*SkDebugCanvas::kVizImageWidth)/bounds.height()), |
| 267 | SkDoubleToScalar((0.9*SkDebugCanvas::kVizImageHeight)/bounds.height())); |
robertphillips@google.com | 6dec8fc | 2012-11-21 17:11:02 +0000 | [diff] [blame] | 268 | } |
| 269 | canvas.translate(-bounds.fLeft+2, -bounds.fTop+2); |
| 270 | |
| 271 | SkPaint p; |
| 272 | p.setColor(SK_ColorBLACK); |
| 273 | p.setStyle(SkPaint::kStroke_Style); |
| 274 | |
| 275 | canvas.drawPath(path, p); |
| 276 | |
| 277 | return bitmap; |
| 278 | } |
| 279 | |
robertphillips@google.com | 53ec73d | 2012-11-26 13:09:17 +0000 | [diff] [blame] | 280 | static SkBitmap createBitmap(const SkBitmap& input, const SkRect* srcRect) { |
| 281 | SkBitmap bitmap; |
skia.committer@gmail.com | 8ccf590 | 2012-11-27 02:01:19 +0000 | [diff] [blame] | 282 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 283 | SkDebugCanvas::kVizImageWidth, |
| 284 | SkDebugCanvas::kVizImageHeight); |
robertphillips@google.com | 53ec73d | 2012-11-26 13:09:17 +0000 | [diff] [blame] | 285 | bitmap.allocPixels(); |
| 286 | bitmap.eraseColor(SK_ColorLTGRAY); |
| 287 | SkDevice* device = new SkDevice(bitmap); |
| 288 | |
| 289 | SkCanvas canvas(device); |
| 290 | device->unref(); |
| 291 | |
robertphillips@google.com | 3b0a9fe | 2013-01-31 15:56:22 +0000 | [diff] [blame] | 292 | SkScalar xScale = SkIntToScalar(SkDebugCanvas::kVizImageWidth-2) / input.width(); |
| 293 | SkScalar yScale = SkIntToScalar(SkDebugCanvas::kVizImageHeight-2) / input.height(); |
robertphillips@google.com | 53ec73d | 2012-11-26 13:09:17 +0000 | [diff] [blame] | 294 | |
| 295 | if (input.width() > input.height()) { |
| 296 | yScale *= input.height() / (float) input.width(); |
| 297 | } else { |
| 298 | xScale *= input.width() / (float) input.height(); |
| 299 | } |
| 300 | |
skia.committer@gmail.com | 8ccf590 | 2012-11-27 02:01:19 +0000 | [diff] [blame] | 301 | SkRect dst = SkRect::MakeXYWH(SK_Scalar1, SK_Scalar1, |
robertphillips@google.com | 53ec73d | 2012-11-26 13:09:17 +0000 | [diff] [blame] | 302 | xScale * input.width(), |
| 303 | yScale * input.height()); |
| 304 | |
| 305 | canvas.drawBitmapRect(input, NULL, dst); |
| 306 | |
| 307 | if (NULL != srcRect) { |
| 308 | SkRect r = SkRect::MakeLTRB(srcRect->fLeft * xScale + SK_Scalar1, |
| 309 | srcRect->fTop * yScale + SK_Scalar1, |
| 310 | srcRect->fRight * xScale + SK_Scalar1, |
| 311 | srcRect->fBottom * yScale + SK_Scalar1); |
| 312 | SkPaint p; |
| 313 | p.setColor(SK_ColorRED); |
| 314 | p.setStyle(SkPaint::kStroke_Style); |
| 315 | |
| 316 | canvas.drawRect(r, p); |
| 317 | } |
| 318 | |
| 319 | return bitmap; |
| 320 | } |
| 321 | |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 322 | bool SkDebugCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { |
rmistry@google.com | 4473765 | 2012-11-21 18:37:58 +0000 | [diff] [blame] | 323 | SkBitmap bitmap = createBitmap(path); |
| 324 | addDrawCommand(new ClipPath(path, op, doAA, bitmap)); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 325 | return true; |
| 326 | } |
| 327 | |
| 328 | bool SkDebugCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { |
| 329 | addDrawCommand(new ClipRect(rect, op, doAA)); |
| 330 | return true; |
| 331 | } |
| 332 | |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 333 | bool SkDebugCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
| 334 | addDrawCommand(new ClipRRect(rrect, op, doAA)); |
| 335 | return true; |
| 336 | } |
| 337 | |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 338 | bool SkDebugCanvas::clipRegion(const SkRegion& region, SkRegion::Op op) { |
| 339 | addDrawCommand(new ClipRegion(region, op)); |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | bool SkDebugCanvas::concat(const SkMatrix& matrix) { |
| 344 | addDrawCommand(new Concat(matrix)); |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | void SkDebugCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left, |
| 349 | SkScalar top, const SkPaint* paint = NULL) { |
robertphillips@google.com | 53ec73d | 2012-11-26 13:09:17 +0000 | [diff] [blame] | 350 | SkBitmap resizedBitmap = createBitmap(bitmap, NULL); |
| 351 | addDrawCommand(new DrawBitmap(bitmap, left, top, paint, resizedBitmap)); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 352 | } |
| 353 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 354 | void SkDebugCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, |
| 355 | const SkRect* src, const SkRect& dst, const SkPaint* paint) { |
robertphillips@google.com | 53ec73d | 2012-11-26 13:09:17 +0000 | [diff] [blame] | 356 | SkBitmap resizedBitmap = createBitmap(bitmap, src); |
| 357 | addDrawCommand(new DrawBitmapRect(bitmap, src, dst, paint, resizedBitmap)); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void SkDebugCanvas::drawBitmapMatrix(const SkBitmap& bitmap, |
| 361 | const SkMatrix& matrix, const SkPaint* paint) { |
robertphillips@google.com | 53ec73d | 2012-11-26 13:09:17 +0000 | [diff] [blame] | 362 | SkBitmap resizedBitmap = createBitmap(bitmap, NULL); |
| 363 | addDrawCommand(new DrawBitmapMatrix(bitmap, matrix, paint, resizedBitmap)); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void SkDebugCanvas::drawBitmapNine(const SkBitmap& bitmap, |
| 367 | const SkIRect& center, const SkRect& dst, const SkPaint* paint) { |
robertphillips@google.com | 53ec73d | 2012-11-26 13:09:17 +0000 | [diff] [blame] | 368 | SkBitmap resizedBitmap = createBitmap(bitmap, NULL); |
| 369 | addDrawCommand(new DrawBitmapNine(bitmap, center, dst, paint, resizedBitmap)); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void SkDebugCanvas::drawData(const void* data, size_t length) { |
| 373 | addDrawCommand(new DrawData(data, length)); |
| 374 | } |
| 375 | |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 376 | void SkDebugCanvas::drawOval(const SkRect& oval, const SkPaint& paint) { |
| 377 | addDrawCommand(new DrawOval(oval, paint)); |
| 378 | } |
| 379 | |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 380 | void SkDebugCanvas::drawPaint(const SkPaint& paint) { |
| 381 | addDrawCommand(new DrawPaint(paint)); |
| 382 | } |
| 383 | |
| 384 | void SkDebugCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
rmistry@google.com | 4473765 | 2012-11-21 18:37:58 +0000 | [diff] [blame] | 385 | SkBitmap bitmap = createBitmap(path); |
| 386 | addDrawCommand(new DrawPath(path, paint, bitmap)); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | void SkDebugCanvas::drawPicture(SkPicture& picture) { |
| 390 | addDrawCommand(new DrawPicture(picture)); |
| 391 | } |
| 392 | |
| 393 | void SkDebugCanvas::drawPoints(PointMode mode, size_t count, |
| 394 | const SkPoint pts[], const SkPaint& paint) { |
| 395 | addDrawCommand(new DrawPoints(mode, count, pts, paint)); |
| 396 | } |
| 397 | |
| 398 | void SkDebugCanvas::drawPosText(const void* text, size_t byteLength, |
| 399 | const SkPoint pos[], const SkPaint& paint) { |
| 400 | addDrawCommand(new DrawPosText(text, byteLength, pos, paint)); |
| 401 | } |
| 402 | |
| 403 | void SkDebugCanvas::drawPosTextH(const void* text, size_t byteLength, |
| 404 | const SkScalar xpos[], SkScalar constY, const SkPaint& paint) { |
| 405 | addDrawCommand(new DrawPosTextH(text, byteLength, xpos, constY, paint)); |
| 406 | } |
| 407 | |
| 408 | void SkDebugCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { |
| 409 | // NOTE(chudy): Messing up when renamed to DrawRect... Why? |
| 410 | addDrawCommand(new DrawRectC(rect, paint)); |
| 411 | } |
| 412 | |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 413 | void SkDebugCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
| 414 | addDrawCommand(new DrawRRect(rrect, paint)); |
| 415 | } |
| 416 | |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 417 | void SkDebugCanvas::drawSprite(const SkBitmap& bitmap, int left, int top, |
| 418 | const SkPaint* paint = NULL) { |
robertphillips@google.com | 53ec73d | 2012-11-26 13:09:17 +0000 | [diff] [blame] | 419 | SkBitmap resizedBitmap = createBitmap(bitmap, NULL); |
| 420 | addDrawCommand(new DrawSprite(bitmap, left, top, paint, resizedBitmap)); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | void SkDebugCanvas::drawText(const void* text, size_t byteLength, SkScalar x, |
| 424 | SkScalar y, const SkPaint& paint) { |
| 425 | addDrawCommand(new DrawTextC(text, byteLength, x, y, paint)); |
| 426 | } |
| 427 | |
| 428 | void SkDebugCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 429 | const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) { |
| 430 | addDrawCommand(new DrawTextOnPath(text, byteLength, path, matrix, paint)); |
| 431 | } |
| 432 | |
| 433 | void SkDebugCanvas::drawVertices(VertexMode vmode, int vertexCount, |
| 434 | const SkPoint vertices[], const SkPoint texs[], const SkColor colors[], |
| 435 | SkXfermode*, const uint16_t indices[], int indexCount, |
| 436 | const SkPaint& paint) { |
| 437 | addDrawCommand(new DrawVertices(vmode, vertexCount, vertices, texs, colors, |
| 438 | NULL, indices, indexCount, paint)); |
| 439 | } |
| 440 | |
| 441 | void SkDebugCanvas::restore() { |
| 442 | addDrawCommand(new Restore()); |
| 443 | } |
| 444 | |
| 445 | bool SkDebugCanvas::rotate(SkScalar degrees) { |
| 446 | addDrawCommand(new Rotate(degrees)); |
| 447 | return true; |
| 448 | } |
| 449 | |
| 450 | int SkDebugCanvas::save(SaveFlags flags) { |
| 451 | addDrawCommand(new Save(flags)); |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | int SkDebugCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 456 | SaveFlags flags) { |
| 457 | addDrawCommand(new SaveLayer(bounds, paint, flags)); |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | bool SkDebugCanvas::scale(SkScalar sx, SkScalar sy) { |
| 462 | addDrawCommand(new Scale(sx, sy)); |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | void SkDebugCanvas::setMatrix(const SkMatrix& matrix) { |
| 467 | addDrawCommand(new SetMatrix(matrix)); |
| 468 | } |
| 469 | |
| 470 | bool SkDebugCanvas::skew(SkScalar sx, SkScalar sy) { |
| 471 | addDrawCommand(new Skew(sx, sy)); |
| 472 | return true; |
| 473 | } |
| 474 | |
| 475 | bool SkDebugCanvas::translate(SkScalar dx, SkScalar dy) { |
| 476 | addDrawCommand(new Translate(dx, dy)); |
| 477 | return true; |
| 478 | } |
| 479 | |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 480 | void SkDebugCanvas::toggleCommand(int index, bool toggle) { |
robertphillips@google.com | 67baba4 | 2013-01-02 20:20:31 +0000 | [diff] [blame] | 481 | SkASSERT(index < fCommandVector.count()); |
| 482 | fCommandVector[index]->setVisible(toggle); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 483 | } |