blob: d9392bcc3334cc9fdac3a17c24674511b3de84a9 [file] [log] [blame]
chudy@google.com902ebe52012-06-29 14:21:22 +00001
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"
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +000013#include "SkDevice.h"
14#include "SkImageWidget.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000015
reed@google.com6ae24e02012-09-26 13:44:13 +000016static SkBitmap make_noconfig_bm(int width, int height) {
17 SkBitmap bm;
18 bm.setConfig(SkBitmap::kNo_Config, width, height);
19 return bm;
20}
21
22SkDebugCanvas::SkDebugCanvas(int width, int height)
23 : INHERITED(make_noconfig_bm(width, height)) {
chudy@google.com902ebe52012-06-29 14:21:22 +000024 // TODO(chudy): Free up memory from all draw commands in destructor.
chudy@google.com80a4a602012-07-30 18:54:07 +000025 fWidth = width;
26 fHeight = height;
reed@google.com6ae24e02012-09-26 13:44:13 +000027 // do we need fBm anywhere?
chudy@google.comb9ddd4e2012-07-10 14:14:50 +000028 fBm.setConfig(SkBitmap::kNo_Config, fWidth, fHeight);
chudy@google.com902ebe52012-06-29 14:21:22 +000029 fFilter = false;
chudy@google.com830b8792012-08-01 15:57:52 +000030 fIndex = 0;
31 fUserOffset.set(0,0);
32 fUserScale = 1.0;
chudy@google.com902ebe52012-06-29 14:21:22 +000033}
34
chudy@google.com9cda6f72012-08-07 15:08:33 +000035SkDebugCanvas::~SkDebugCanvas() {
chudy@google.com97cee972012-08-07 20:41:37 +000036 commandVector.deleteAll();
chudy@google.com9cda6f72012-08-07 15:08:33 +000037}
chudy@google.com902ebe52012-06-29 14:21:22 +000038
39void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) {
chudy@google.com97cee972012-08-07 20:41:37 +000040 commandVector.push(command);
chudy@google.com902ebe52012-06-29 14:21:22 +000041}
42
43void SkDebugCanvas::draw(SkCanvas* canvas) {
chudy@google.com97cee972012-08-07 20:41:37 +000044 if(!commandVector.isEmpty()) {
45 for (int i = 0; i < commandVector.count(); i++) {
46 if (commandVector[i]->isVisible()) {
47 commandVector[i]->execute(canvas);
chudy@google.com0ab03392012-07-28 20:16:11 +000048 }
chudy@google.com902ebe52012-06-29 14:21:22 +000049 }
50 }
chudy@google.com97cee972012-08-07 20:41:37 +000051 fIndex = commandVector.count() - 1;
chudy@google.com902ebe52012-06-29 14:21:22 +000052}
53
chudy@google.com830b8792012-08-01 15:57:52 +000054void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) {
skia.committer@gmail.com04ba4482012-09-07 02:01:30 +000055 canvas->translate(SkIntToScalar(fUserOffset.fX),
robertphillips@google.com94acc702012-09-06 18:43:21 +000056 SkIntToScalar(fUserOffset.fY));
chudy@google.com830b8792012-08-01 15:57:52 +000057 if (fUserScale < 0) {
robertphillips@google.com94acc702012-09-06 18:43:21 +000058 canvas->scale((1.0f / -fUserScale), (1.0f / -fUserScale));
chudy@google.com830b8792012-08-01 15:57:52 +000059 } else if (fUserScale > 0) {
60 canvas->scale(fUserScale, fUserScale);
61 }
62}
63
64int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +000065 SkBitmap bitmap;
66 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
67 bitmap.allocPixels();
chudy@google.com902ebe52012-06-29 14:21:22 +000068
chudy@google.com0b5bbb02012-07-31 19:55:32 +000069 SkCanvas canvas(bitmap);
robertphillips@google.com94acc702012-09-06 18:43:21 +000070 canvas.translate(SkIntToScalar(-x), SkIntToScalar(-y));
chudy@google.com830b8792012-08-01 15:57:52 +000071 applyUserTransform(&canvas);
chudy@google.com0b5bbb02012-07-31 19:55:32 +000072
73 int layer = 0;
chudy@google.com751961d2012-07-31 20:07:42 +000074 SkColor prev = bitmap.getColor(0,0);
chudy@google.com0b5bbb02012-07-31 19:55:32 +000075 for (int i = 0; i < index; i++) {
76 if (commandVector[i]->isVisible()) {
77 commandVector[i]->execute(&canvas);
78 }
79 if (prev != bitmap.getColor(0,0)) {
80 layer = i;
81 }
82 prev = bitmap.getColor(0,0);
83 }
84 return layer;
85}
86
87void SkDebugCanvas::drawTo(SkCanvas* canvas, int index) {
88 int counter = 0;
chudy@google.com97cee972012-08-07 20:41:37 +000089 SkASSERT(!commandVector.isEmpty());
90 SkASSERT(index < commandVector.count());
chudy@google.com830b8792012-08-01 15:57:52 +000091 int i;
92
93 // This only works assuming the canvas and device are the same ones that
94 // were previously drawn into because they need to preserve all saves
95 // and restores.
96 if (fIndex < index) {
97 i = fIndex + 1;
98 } else {
99 i = 0;
100 canvas->clear(0);
101 canvas->resetMatrix();
skia.committer@gmail.com04ba4482012-09-07 02:01:30 +0000102 SkRect rect = SkRect::MakeWH(SkIntToScalar(fWidth),
robertphillips@google.com94acc702012-09-06 18:43:21 +0000103 SkIntToScalar(fHeight));
chudy@google.com4c7962e2012-08-14 19:38:31 +0000104 canvas->clipRect(rect, SkRegion::kReplace_Op );
chudy@google.com830b8792012-08-01 15:57:52 +0000105 applyUserTransform(canvas);
106 }
107
108 for (; i <= index; i++) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000109 if (i == index && fFilter) {
110 SkPaint p;
111 p.setColor(0xAAFFFFFF);
112 canvas->save();
113 canvas->resetMatrix();
114 SkRect mask;
115 mask.set(SkIntToScalar(0), SkIntToScalar(0),
116 SkIntToScalar(fWidth), SkIntToScalar(fHeight));
117 canvas->clipRect(mask, SkRegion::kReplace_Op, false);
118 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0),
119 SkIntToScalar(fWidth), SkIntToScalar(fHeight), p);
120 canvas->restore();
121 }
122
123 if (commandVector[i]->isVisible()) {
124 commandVector[i]->execute(canvas);
chudy@google.com902ebe52012-06-29 14:21:22 +0000125 }
126 }
chudy@google.coma9e937c2012-08-03 17:32:05 +0000127 fMatrix = canvas->getTotalMatrix();
128 fClip = canvas->getTotalClip().getBounds();
chudy@google.com830b8792012-08-01 15:57:52 +0000129 fIndex = index;
chudy@google.com902ebe52012-06-29 14:21:22 +0000130}
131
132SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) {
chudy@google.com97cee972012-08-07 20:41:37 +0000133 SkASSERT(index < commandVector.count());
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000134 return commandVector[index];
chudy@google.com902ebe52012-06-29 14:21:22 +0000135}
136
chudy@google.com97cee972012-08-07 20:41:37 +0000137SkTDArray<SkString*>* SkDebugCanvas::getCommandInfo(int index) {
138 SkASSERT(index < commandVector.count());
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000139 return commandVector[index]->Info();
140}
chudy@google.com902ebe52012-06-29 14:21:22 +0000141
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000142bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) {
chudy@google.com97cee972012-08-07 20:41:37 +0000143 SkASSERT(index < commandVector.count());
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000144 return commandVector[index]->isVisible();
chudy@google.com902ebe52012-06-29 14:21:22 +0000145}
146
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000147const SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() const {
chudy@google.com902ebe52012-06-29 14:21:22 +0000148 return commandVector;
149}
150
151// TODO(chudy): Free command string memory.
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000152SkTArray<SkString>* SkDebugCanvas::getDrawCommandsAsStrings() const {
153 SkTArray<SkString>* commandString = new SkTArray<SkString>(commandVector.count());
chudy@google.com97cee972012-08-07 20:41:37 +0000154 if (!commandVector.isEmpty()) {
155 for (int i = 0; i < commandVector.count(); i ++) {
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000156 commandString->push_back() = commandVector[i]->toString();
chudy@google.com902ebe52012-06-29 14:21:22 +0000157 }
158 }
159 return commandString;
160}
161
162void SkDebugCanvas::toggleFilter(bool toggle) {
163 fFilter = toggle;
164}
165
166void SkDebugCanvas::clear(SkColor color) {
167 addDrawCommand(new Clear(color));
168}
169
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +0000170static SkBitmap createBitmap(const SkPath& path) {
171 SkBitmap bitmap;
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +0000172 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
173 SkImageWidget::kImageWidgetWidth,
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +0000174 SkImageWidget::kImageWidgetHeight);
175 bitmap.allocPixels();
176 bitmap.eraseColor(SK_ColorWHITE);
177 SkDevice* device = new SkDevice(bitmap);
178
179 SkCanvas canvas(device);
180 device->unref();
181
182 const SkRect& bounds = path.getBounds();
183
184 if (bounds.width() > bounds.height()) {
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +0000185 canvas.scale(SkDoubleToScalar((0.9*SkImageWidget::kImageWidgetWidth)/bounds.width()),
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +0000186 SkDoubleToScalar((0.9*SkImageWidget::kImageWidgetHeight)/bounds.width()));
187 } else {
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +0000188 canvas.scale(SkDoubleToScalar((0.9*SkImageWidget::kImageWidgetWidth)/bounds.height()),
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +0000189 SkDoubleToScalar((0.9*SkImageWidget::kImageWidgetHeight)/bounds.height()));
190 }
191 canvas.translate(-bounds.fLeft+2, -bounds.fTop+2);
192
193 SkPaint p;
194 p.setColor(SK_ColorBLACK);
195 p.setStyle(SkPaint::kStroke_Style);
196
197 canvas.drawPath(path, p);
198
199 return bitmap;
200}
201
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000202static SkBitmap createBitmap(const SkBitmap& input, const SkRect* srcRect) {
203 SkBitmap bitmap;
204 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
205 SkImageWidget::kImageWidgetWidth,
206 SkImageWidget::kImageWidgetHeight);
207 bitmap.allocPixels();
208 bitmap.eraseColor(SK_ColorLTGRAY);
209 SkDevice* device = new SkDevice(bitmap);
210
211 SkCanvas canvas(device);
212 device->unref();
213
214 SkScalar xScale = (SkImageWidget::kImageWidgetWidth-2.0) / input.width();
215 SkScalar yScale = (SkImageWidget::kImageWidgetHeight-2.0) / input.height();
216
217 if (input.width() > input.height()) {
218 yScale *= input.height() / (float) input.width();
219 } else {
220 xScale *= input.width() / (float) input.height();
221 }
222
223 SkRect dst = SkRect::MakeXYWH(SK_Scalar1, SK_Scalar1,
224 xScale * input.width(),
225 yScale * input.height());
226
227 canvas.drawBitmapRect(input, NULL, dst);
228
229 if (NULL != srcRect) {
230 SkRect r = SkRect::MakeLTRB(srcRect->fLeft * xScale + SK_Scalar1,
231 srcRect->fTop * yScale + SK_Scalar1,
232 srcRect->fRight * xScale + SK_Scalar1,
233 srcRect->fBottom * yScale + SK_Scalar1);
234 SkPaint p;
235 p.setColor(SK_ColorRED);
236 p.setStyle(SkPaint::kStroke_Style);
237
238 canvas.drawRect(r, p);
239 }
240
241 return bitmap;
242}
243
chudy@google.com902ebe52012-06-29 14:21:22 +0000244bool SkDebugCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
rmistry@google.com44737652012-11-21 18:37:58 +0000245 SkBitmap bitmap = createBitmap(path);
246 addDrawCommand(new ClipPath(path, op, doAA, bitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000247 return true;
248}
249
250bool SkDebugCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
251 addDrawCommand(new ClipRect(rect, op, doAA));
252 return true;
253}
254
255bool SkDebugCanvas::clipRegion(const SkRegion& region, SkRegion::Op op) {
256 addDrawCommand(new ClipRegion(region, op));
257 return true;
258}
259
260bool SkDebugCanvas::concat(const SkMatrix& matrix) {
261 addDrawCommand(new Concat(matrix));
262 return true;
263}
264
265void SkDebugCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
266 SkScalar top, const SkPaint* paint = NULL) {
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000267 SkBitmap resizedBitmap = createBitmap(bitmap, NULL);
268 addDrawCommand(new DrawBitmap(bitmap, left, top, paint, resizedBitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000269}
270
reed@google.com71121732012-09-18 15:14:33 +0000271void SkDebugCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
272 const SkRect* src, const SkRect& dst, const SkPaint* paint) {
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000273 SkBitmap resizedBitmap = createBitmap(bitmap, src);
274 addDrawCommand(new DrawBitmapRect(bitmap, src, dst, paint, resizedBitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000275}
276
277void SkDebugCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
278 const SkMatrix& matrix, const SkPaint* paint) {
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000279 SkBitmap resizedBitmap = createBitmap(bitmap, NULL);
280 addDrawCommand(new DrawBitmapMatrix(bitmap, matrix, paint, resizedBitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000281}
282
283void SkDebugCanvas::drawBitmapNine(const SkBitmap& bitmap,
284 const SkIRect& center, const SkRect& dst, const SkPaint* paint) {
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000285 SkBitmap resizedBitmap = createBitmap(bitmap, NULL);
286 addDrawCommand(new DrawBitmapNine(bitmap, center, dst, paint, resizedBitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000287}
288
289void SkDebugCanvas::drawData(const void* data, size_t length) {
290 addDrawCommand(new DrawData(data, length));
291}
292
293void SkDebugCanvas::drawPaint(const SkPaint& paint) {
294 addDrawCommand(new DrawPaint(paint));
295}
296
297void SkDebugCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
rmistry@google.com44737652012-11-21 18:37:58 +0000298 SkBitmap bitmap = createBitmap(path);
299 addDrawCommand(new DrawPath(path, paint, bitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000300}
301
302void SkDebugCanvas::drawPicture(SkPicture& picture) {
303 addDrawCommand(new DrawPicture(picture));
304}
305
306void SkDebugCanvas::drawPoints(PointMode mode, size_t count,
307 const SkPoint pts[], const SkPaint& paint) {
308 addDrawCommand(new DrawPoints(mode, count, pts, paint));
309}
310
311void SkDebugCanvas::drawPosText(const void* text, size_t byteLength,
312 const SkPoint pos[], const SkPaint& paint) {
313 addDrawCommand(new DrawPosText(text, byteLength, pos, paint));
314}
315
316void SkDebugCanvas::drawPosTextH(const void* text, size_t byteLength,
317 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
318 addDrawCommand(new DrawPosTextH(text, byteLength, xpos, constY, paint));
319}
320
321void SkDebugCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
322 // NOTE(chudy): Messing up when renamed to DrawRect... Why?
323 addDrawCommand(new DrawRectC(rect, paint));
324}
325
326void SkDebugCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
327 const SkPaint* paint = NULL) {
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000328 SkBitmap resizedBitmap = createBitmap(bitmap, NULL);
329 addDrawCommand(new DrawSprite(bitmap, left, top, paint, resizedBitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000330}
331
332void SkDebugCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
333 SkScalar y, const SkPaint& paint) {
334 addDrawCommand(new DrawTextC(text, byteLength, x, y, paint));
335}
336
337void SkDebugCanvas::drawTextOnPath(const void* text, size_t byteLength,
338 const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) {
339 addDrawCommand(new DrawTextOnPath(text, byteLength, path, matrix, paint));
340}
341
342void SkDebugCanvas::drawVertices(VertexMode vmode, int vertexCount,
343 const SkPoint vertices[], const SkPoint texs[], const SkColor colors[],
344 SkXfermode*, const uint16_t indices[], int indexCount,
345 const SkPaint& paint) {
346 addDrawCommand(new DrawVertices(vmode, vertexCount, vertices, texs, colors,
347 NULL, indices, indexCount, paint));
348}
349
350void SkDebugCanvas::restore() {
351 addDrawCommand(new Restore());
352}
353
354bool SkDebugCanvas::rotate(SkScalar degrees) {
355 addDrawCommand(new Rotate(degrees));
356 return true;
357}
358
359int SkDebugCanvas::save(SaveFlags flags) {
360 addDrawCommand(new Save(flags));
361 return true;
362}
363
364int SkDebugCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
365 SaveFlags flags) {
366 addDrawCommand(new SaveLayer(bounds, paint, flags));
367 return true;
368}
369
370bool SkDebugCanvas::scale(SkScalar sx, SkScalar sy) {
371 addDrawCommand(new Scale(sx, sy));
372 return true;
373}
374
375void SkDebugCanvas::setMatrix(const SkMatrix& matrix) {
376 addDrawCommand(new SetMatrix(matrix));
377}
378
379bool SkDebugCanvas::skew(SkScalar sx, SkScalar sy) {
380 addDrawCommand(new Skew(sx, sy));
381 return true;
382}
383
384bool SkDebugCanvas::translate(SkScalar dx, SkScalar dy) {
385 addDrawCommand(new Translate(dx, dy));
386 return true;
387}
388
chudy@google.com902ebe52012-06-29 14:21:22 +0000389void SkDebugCanvas::toggleCommand(int index, bool toggle) {
chudy@google.com97cee972012-08-07 20:41:37 +0000390 SkASSERT(index < commandVector.count());
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000391 commandVector[index]->setVisible(toggle);
chudy@google.com902ebe52012-06-29 14:21:22 +0000392}