blob: 27b58da3044f18cf65f4f75fc07614ee2f072bf0 [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
chudy@google.com902ebe52012-06-29 14:21:22 +000010#include "SkDebugCanvas.h"
11#include "SkDrawCommand.h"
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +000012#include "SkDevice.h"
chudy@google.com902ebe52012-06-29 14:21:22 +000013
bsalomon@google.com50c79d82013-01-08 20:31:53 +000014#ifdef SK_BUILD_FOR_WIN
15 // iostream includes xlocale which generates warning 4530 because we're compiling without
16 // exceptions
17 #pragma warning(push)
18 #pragma warning(disable : 4530)
19#endif
20#include <iostream>
21#ifdef SK_BUILD_FOR_WIN
22 #pragma warning(pop)
23#endif
24
reed@google.com6ae24e02012-09-26 13:44:13 +000025static SkBitmap make_noconfig_bm(int width, int height) {
26 SkBitmap bm;
27 bm.setConfig(SkBitmap::kNo_Config, width, height);
28 return bm;
29}
30
31SkDebugCanvas::SkDebugCanvas(int width, int height)
tomhudson@google.com0699e022012-11-27 16:09:42 +000032 : INHERITED(make_noconfig_bm(width, height))
33 , fOutstandingSaveCount(0) {
chudy@google.com902ebe52012-06-29 14:21:22 +000034 // TODO(chudy): Free up memory from all draw commands in destructor.
chudy@google.com80a4a602012-07-30 18:54:07 +000035 fWidth = width;
36 fHeight = height;
reed@google.com6ae24e02012-09-26 13:44:13 +000037 // do we need fBm anywhere?
chudy@google.comb9ddd4e2012-07-10 14:14:50 +000038 fBm.setConfig(SkBitmap::kNo_Config, fWidth, fHeight);
chudy@google.com902ebe52012-06-29 14:21:22 +000039 fFilter = false;
chudy@google.com830b8792012-08-01 15:57:52 +000040 fIndex = 0;
bungeman@google.come8cc6e82013-01-17 16:30:56 +000041 fUserMatrix.reset();
chudy@google.com902ebe52012-06-29 14:21:22 +000042}
43
chudy@google.com9cda6f72012-08-07 15:08:33 +000044SkDebugCanvas::~SkDebugCanvas() {
robertphillips@google.com67baba42013-01-02 20:20:31 +000045 fCommandVector.deleteAll();
chudy@google.com9cda6f72012-08-07 15:08:33 +000046}
chudy@google.com902ebe52012-06-29 14:21:22 +000047
48void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) {
robertphillips@google.com67baba42013-01-02 20:20:31 +000049 fCommandVector.push(command);
chudy@google.com902ebe52012-06-29 14:21:22 +000050}
51
52void SkDebugCanvas::draw(SkCanvas* canvas) {
robertphillips@google.com67baba42013-01-02 20:20:31 +000053 if(!fCommandVector.isEmpty()) {
54 for (int i = 0; i < fCommandVector.count(); i++) {
55 if (fCommandVector[i]->isVisible()) {
56 fCommandVector[i]->execute(canvas);
chudy@google.com0ab03392012-07-28 20:16:11 +000057 }
chudy@google.com902ebe52012-06-29 14:21:22 +000058 }
59 }
robertphillips@google.com67baba42013-01-02 20:20:31 +000060 fIndex = fCommandVector.count() - 1;
chudy@google.com902ebe52012-06-29 14:21:22 +000061}
62
chudy@google.com830b8792012-08-01 15:57:52 +000063void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) {
bungeman@google.come8cc6e82013-01-17 16:30:56 +000064 canvas->concat(fUserMatrix);
chudy@google.com830b8792012-08-01 15:57:52 +000065}
66
67int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +000068 SkBitmap bitmap;
69 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
70 bitmap.allocPixels();
chudy@google.com902ebe52012-06-29 14:21:22 +000071
chudy@google.com0b5bbb02012-07-31 19:55:32 +000072 SkCanvas canvas(bitmap);
robertphillips@google.com94acc702012-09-06 18:43:21 +000073 canvas.translate(SkIntToScalar(-x), SkIntToScalar(-y));
chudy@google.com830b8792012-08-01 15:57:52 +000074 applyUserTransform(&canvas);
chudy@google.com0b5bbb02012-07-31 19:55:32 +000075
76 int layer = 0;
chudy@google.com751961d2012-07-31 20:07:42 +000077 SkColor prev = bitmap.getColor(0,0);
chudy@google.com0b5bbb02012-07-31 19:55:32 +000078 for (int i = 0; i < index; i++) {
robertphillips@google.com67baba42013-01-02 20:20:31 +000079 if (fCommandVector[i]->isVisible()) {
80 fCommandVector[i]->execute(&canvas);
chudy@google.com0b5bbb02012-07-31 19:55:32 +000081 }
82 if (prev != bitmap.getColor(0,0)) {
83 layer = i;
84 }
85 prev = bitmap.getColor(0,0);
86 }
87 return layer;
88}
89
90void SkDebugCanvas::drawTo(SkCanvas* canvas, int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +000091 SkASSERT(!fCommandVector.isEmpty());
92 SkASSERT(index < fCommandVector.count());
chudy@google.com830b8792012-08-01 15:57:52 +000093 int i;
94
95 // This only works assuming the canvas and device are the same ones that
96 // were previously drawn into because they need to preserve all saves
97 // and restores.
98 if (fIndex < index) {
99 i = fIndex + 1;
100 } else {
tomhudson@google.com0699e022012-11-27 16:09:42 +0000101 for (int j = 0; j < fOutstandingSaveCount; j++) {
102 canvas->restore();
103 }
chudy@google.com830b8792012-08-01 15:57:52 +0000104 i = 0;
junov@google.comdbfac8a2012-12-06 21:47:40 +0000105 canvas->clear(SK_ColorTRANSPARENT);
chudy@google.com830b8792012-08-01 15:57:52 +0000106 canvas->resetMatrix();
skia.committer@gmail.com04ba4482012-09-07 02:01:30 +0000107 SkRect rect = SkRect::MakeWH(SkIntToScalar(fWidth),
robertphillips@google.com94acc702012-09-06 18:43:21 +0000108 SkIntToScalar(fHeight));
chudy@google.com4c7962e2012-08-14 19:38:31 +0000109 canvas->clipRect(rect, SkRegion::kReplace_Op );
chudy@google.com830b8792012-08-01 15:57:52 +0000110 applyUserTransform(canvas);
tomhudson@google.com0699e022012-11-27 16:09:42 +0000111 fOutstandingSaveCount = 0;
chudy@google.com830b8792012-08-01 15:57:52 +0000112 }
113
114 for (; i <= index; i++) {
chudy@google.com0b5bbb02012-07-31 19:55:32 +0000115 if (i == index && fFilter) {
116 SkPaint p;
117 p.setColor(0xAAFFFFFF);
118 canvas->save();
119 canvas->resetMatrix();
120 SkRect mask;
121 mask.set(SkIntToScalar(0), SkIntToScalar(0),
122 SkIntToScalar(fWidth), SkIntToScalar(fHeight));
123 canvas->clipRect(mask, SkRegion::kReplace_Op, false);
124 canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0),
125 SkIntToScalar(fWidth), SkIntToScalar(fHeight), p);
126 canvas->restore();
127 }
128
robertphillips@google.com67baba42013-01-02 20:20:31 +0000129 if (fCommandVector[i]->isVisible()) {
130 fCommandVector[i]->execute(canvas);
131 fCommandVector[i]->trackSaveState(&fOutstandingSaveCount);
chudy@google.com902ebe52012-06-29 14:21:22 +0000132 }
133 }
chudy@google.coma9e937c2012-08-03 17:32:05 +0000134 fMatrix = canvas->getTotalMatrix();
135 fClip = canvas->getTotalClip().getBounds();
chudy@google.com830b8792012-08-01 15:57:52 +0000136 fIndex = index;
chudy@google.com902ebe52012-06-29 14:21:22 +0000137}
138
139SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000140 SkASSERT(index < fCommandVector.count());
141 return fCommandVector[index];
chudy@google.com902ebe52012-06-29 14:21:22 +0000142}
143
chudy@google.com97cee972012-08-07 20:41:37 +0000144SkTDArray<SkString*>* SkDebugCanvas::getCommandInfo(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000145 SkASSERT(index < fCommandVector.count());
146 return fCommandVector[index]->Info();
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000147}
chudy@google.com902ebe52012-06-29 14:21:22 +0000148
chudy@google.com7e4cfbf2012-07-17 15:40:51 +0000149bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000150 SkASSERT(index < fCommandVector.count());
151 return fCommandVector[index]->isVisible();
chudy@google.com902ebe52012-06-29 14:21:22 +0000152}
153
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000154const SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000155 return fCommandVector;
chudy@google.com902ebe52012-06-29 14:21:22 +0000156}
157
158// TODO(chudy): Free command string memory.
robertphillips@google.com8a1cdae2012-11-19 20:44:29 +0000159SkTArray<SkString>* SkDebugCanvas::getDrawCommandsAsStrings() const {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000160 SkTArray<SkString>* commandString = new SkTArray<SkString>(fCommandVector.count());
161 if (!fCommandVector.isEmpty()) {
162 for (int i = 0; i < fCommandVector.count(); i ++) {
163 commandString->push_back() = fCommandVector[i]->toString();
chudy@google.com902ebe52012-06-29 14:21:22 +0000164 }
165 }
166 return commandString;
167}
168
169void SkDebugCanvas::toggleFilter(bool toggle) {
170 fFilter = toggle;
171}
172
173void SkDebugCanvas::clear(SkColor color) {
174 addDrawCommand(new Clear(color));
175}
176
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +0000177static SkBitmap createBitmap(const SkPath& path) {
178 SkBitmap bitmap;
skia.committer@gmail.com1c9c0d32012-11-22 02:02:41 +0000179 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000180 SkDebugCanvas::kVizImageWidth,
181 SkDebugCanvas::kVizImageHeight);
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +0000182 bitmap.allocPixels();
183 bitmap.eraseColor(SK_ColorWHITE);
184 SkDevice* device = new SkDevice(bitmap);
185
186 SkCanvas canvas(device);
187 device->unref();
188
189 const SkRect& bounds = path.getBounds();
190
191 if (bounds.width() > bounds.height()) {
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000192 canvas.scale(SkDoubleToScalar((0.9*SkDebugCanvas::kVizImageWidth)/bounds.width()),
193 SkDoubleToScalar((0.9*SkDebugCanvas::kVizImageHeight)/bounds.width()));
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +0000194 } else {
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000195 canvas.scale(SkDoubleToScalar((0.9*SkDebugCanvas::kVizImageWidth)/bounds.height()),
196 SkDoubleToScalar((0.9*SkDebugCanvas::kVizImageHeight)/bounds.height()));
robertphillips@google.com6dec8fc2012-11-21 17:11:02 +0000197 }
198 canvas.translate(-bounds.fLeft+2, -bounds.fTop+2);
199
200 SkPaint p;
201 p.setColor(SK_ColorBLACK);
202 p.setStyle(SkPaint::kStroke_Style);
203
204 canvas.drawPath(path, p);
205
206 return bitmap;
207}
208
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000209static SkBitmap createBitmap(const SkBitmap& input, const SkRect* srcRect) {
210 SkBitmap bitmap;
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +0000211 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000212 SkDebugCanvas::kVizImageWidth,
213 SkDebugCanvas::kVizImageHeight);
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000214 bitmap.allocPixels();
215 bitmap.eraseColor(SK_ColorLTGRAY);
216 SkDevice* device = new SkDevice(bitmap);
217
218 SkCanvas canvas(device);
219 device->unref();
220
robertphillips@google.com3b0a9fe2013-01-31 15:56:22 +0000221 SkScalar xScale = SkIntToScalar(SkDebugCanvas::kVizImageWidth-2) / input.width();
222 SkScalar yScale = SkIntToScalar(SkDebugCanvas::kVizImageHeight-2) / input.height();
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000223
224 if (input.width() > input.height()) {
225 yScale *= input.height() / (float) input.width();
226 } else {
227 xScale *= input.width() / (float) input.height();
228 }
229
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +0000230 SkRect dst = SkRect::MakeXYWH(SK_Scalar1, SK_Scalar1,
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000231 xScale * input.width(),
232 yScale * input.height());
233
234 canvas.drawBitmapRect(input, NULL, dst);
235
236 if (NULL != srcRect) {
237 SkRect r = SkRect::MakeLTRB(srcRect->fLeft * xScale + SK_Scalar1,
238 srcRect->fTop * yScale + SK_Scalar1,
239 srcRect->fRight * xScale + SK_Scalar1,
240 srcRect->fBottom * yScale + SK_Scalar1);
241 SkPaint p;
242 p.setColor(SK_ColorRED);
243 p.setStyle(SkPaint::kStroke_Style);
244
245 canvas.drawRect(r, p);
246 }
247
248 return bitmap;
249}
250
chudy@google.com902ebe52012-06-29 14:21:22 +0000251bool SkDebugCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
rmistry@google.com44737652012-11-21 18:37:58 +0000252 SkBitmap bitmap = createBitmap(path);
253 addDrawCommand(new ClipPath(path, op, doAA, bitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000254 return true;
255}
256
257bool SkDebugCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
258 addDrawCommand(new ClipRect(rect, op, doAA));
259 return true;
260}
261
robertphillips@google.com67baba42013-01-02 20:20:31 +0000262bool SkDebugCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
263 addDrawCommand(new ClipRRect(rrect, op, doAA));
264 return true;
265}
266
chudy@google.com902ebe52012-06-29 14:21:22 +0000267bool SkDebugCanvas::clipRegion(const SkRegion& region, SkRegion::Op op) {
268 addDrawCommand(new ClipRegion(region, op));
269 return true;
270}
271
272bool SkDebugCanvas::concat(const SkMatrix& matrix) {
273 addDrawCommand(new Concat(matrix));
274 return true;
275}
276
277void SkDebugCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
278 SkScalar top, const SkPaint* paint = NULL) {
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000279 SkBitmap resizedBitmap = createBitmap(bitmap, NULL);
280 addDrawCommand(new DrawBitmap(bitmap, left, top, paint, resizedBitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000281}
282
reed@google.com71121732012-09-18 15:14:33 +0000283void SkDebugCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
284 const SkRect* src, const SkRect& dst, const SkPaint* paint) {
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000285 SkBitmap resizedBitmap = createBitmap(bitmap, src);
286 addDrawCommand(new DrawBitmapRect(bitmap, src, dst, paint, resizedBitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000287}
288
289void SkDebugCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
290 const SkMatrix& matrix, const SkPaint* paint) {
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000291 SkBitmap resizedBitmap = createBitmap(bitmap, NULL);
292 addDrawCommand(new DrawBitmapMatrix(bitmap, matrix, paint, resizedBitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000293}
294
295void SkDebugCanvas::drawBitmapNine(const SkBitmap& bitmap,
296 const SkIRect& center, const SkRect& dst, const SkPaint* paint) {
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000297 SkBitmap resizedBitmap = createBitmap(bitmap, NULL);
298 addDrawCommand(new DrawBitmapNine(bitmap, center, dst, paint, resizedBitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000299}
300
301void SkDebugCanvas::drawData(const void* data, size_t length) {
302 addDrawCommand(new DrawData(data, length));
303}
304
robertphillips@google.com67baba42013-01-02 20:20:31 +0000305void SkDebugCanvas::drawOval(const SkRect& oval, const SkPaint& paint) {
306 addDrawCommand(new DrawOval(oval, paint));
307}
308
chudy@google.com902ebe52012-06-29 14:21:22 +0000309void SkDebugCanvas::drawPaint(const SkPaint& paint) {
310 addDrawCommand(new DrawPaint(paint));
311}
312
313void SkDebugCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
rmistry@google.com44737652012-11-21 18:37:58 +0000314 SkBitmap bitmap = createBitmap(path);
315 addDrawCommand(new DrawPath(path, paint, bitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000316}
317
318void SkDebugCanvas::drawPicture(SkPicture& picture) {
319 addDrawCommand(new DrawPicture(picture));
320}
321
322void SkDebugCanvas::drawPoints(PointMode mode, size_t count,
323 const SkPoint pts[], const SkPaint& paint) {
324 addDrawCommand(new DrawPoints(mode, count, pts, paint));
325}
326
327void SkDebugCanvas::drawPosText(const void* text, size_t byteLength,
328 const SkPoint pos[], const SkPaint& paint) {
329 addDrawCommand(new DrawPosText(text, byteLength, pos, paint));
330}
331
332void SkDebugCanvas::drawPosTextH(const void* text, size_t byteLength,
333 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
334 addDrawCommand(new DrawPosTextH(text, byteLength, xpos, constY, paint));
335}
336
337void SkDebugCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
338 // NOTE(chudy): Messing up when renamed to DrawRect... Why?
339 addDrawCommand(new DrawRectC(rect, paint));
340}
341
robertphillips@google.com67baba42013-01-02 20:20:31 +0000342void SkDebugCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
343 addDrawCommand(new DrawRRect(rrect, paint));
344}
345
chudy@google.com902ebe52012-06-29 14:21:22 +0000346void SkDebugCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
347 const SkPaint* paint = NULL) {
robertphillips@google.com53ec73d2012-11-26 13:09:17 +0000348 SkBitmap resizedBitmap = createBitmap(bitmap, NULL);
349 addDrawCommand(new DrawSprite(bitmap, left, top, paint, resizedBitmap));
chudy@google.com902ebe52012-06-29 14:21:22 +0000350}
351
352void SkDebugCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
353 SkScalar y, const SkPaint& paint) {
354 addDrawCommand(new DrawTextC(text, byteLength, x, y, paint));
355}
356
357void SkDebugCanvas::drawTextOnPath(const void* text, size_t byteLength,
358 const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) {
359 addDrawCommand(new DrawTextOnPath(text, byteLength, path, matrix, paint));
360}
361
362void SkDebugCanvas::drawVertices(VertexMode vmode, int vertexCount,
363 const SkPoint vertices[], const SkPoint texs[], const SkColor colors[],
364 SkXfermode*, const uint16_t indices[], int indexCount,
365 const SkPaint& paint) {
366 addDrawCommand(new DrawVertices(vmode, vertexCount, vertices, texs, colors,
367 NULL, indices, indexCount, paint));
368}
369
370void SkDebugCanvas::restore() {
371 addDrawCommand(new Restore());
372}
373
374bool SkDebugCanvas::rotate(SkScalar degrees) {
375 addDrawCommand(new Rotate(degrees));
376 return true;
377}
378
379int SkDebugCanvas::save(SaveFlags flags) {
380 addDrawCommand(new Save(flags));
381 return true;
382}
383
384int SkDebugCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
385 SaveFlags flags) {
386 addDrawCommand(new SaveLayer(bounds, paint, flags));
387 return true;
388}
389
390bool SkDebugCanvas::scale(SkScalar sx, SkScalar sy) {
391 addDrawCommand(new Scale(sx, sy));
392 return true;
393}
394
395void SkDebugCanvas::setMatrix(const SkMatrix& matrix) {
396 addDrawCommand(new SetMatrix(matrix));
397}
398
399bool SkDebugCanvas::skew(SkScalar sx, SkScalar sy) {
400 addDrawCommand(new Skew(sx, sy));
401 return true;
402}
403
404bool SkDebugCanvas::translate(SkScalar dx, SkScalar dy) {
405 addDrawCommand(new Translate(dx, dy));
406 return true;
407}
408
chudy@google.com902ebe52012-06-29 14:21:22 +0000409void SkDebugCanvas::toggleCommand(int index, bool toggle) {
robertphillips@google.com67baba42013-01-02 20:20:31 +0000410 SkASSERT(index < fCommandVector.count());
411 fCommandVector[index]->setVisible(toggle);
chudy@google.com902ebe52012-06-29 14:21:22 +0000412}