1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <iostream>
#include "SkDebugCanvas.h"
#include "SkDrawCommand.h"
SkDebugCanvas::SkDebugCanvas(int width, int height) {
// TODO(chudy): Free up memory from all draw commands in destructor.
fWidth = width;
fHeight = height;
fBm.setConfig(SkBitmap::kNo_Config, fWidth, fHeight);
this->setBitmapDevice(fBm);
fFilter = false;
fIndex = 0;
fUserOffset.set(0,0);
fUserScale = 1.0;
}
SkDebugCanvas::~SkDebugCanvas() {
for (int i = 0; i < commandVector.size(); i++) {
delete(commandVector[i]);
}
commandVector.clear();
}
void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) {
commandVector.push_back(command);
}
void SkDebugCanvas::draw(SkCanvas* canvas) {
if(!commandVector.empty()) {
for(it = commandVector.begin(); it != commandVector.end(); ++it) {
if ((*it)->isVisible()) {
(*it)->execute(canvas);
}
}
}
fIndex = commandVector.size() - 1;
}
void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) {
canvas->translate(fUserOffset.fX, fUserOffset.fY);
if (fUserScale < 0) {
canvas->scale((1.0 / -fUserScale), (1.0 / -fUserScale));
} else if (fUserScale > 0) {
canvas->scale(fUserScale, fUserScale);
}
}
int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) {
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
bitmap.allocPixels();
SkCanvas canvas(bitmap);
canvas.translate(-x, -y);
applyUserTransform(&canvas);
int layer = 0;
SkColor prev = bitmap.getColor(0,0);
for (int i = 0; i < index; i++) {
if (commandVector[i]->isVisible()) {
commandVector[i]->execute(&canvas);
}
if (prev != bitmap.getColor(0,0)) {
layer = i;
}
prev = bitmap.getColor(0,0);
}
return layer;
}
void SkDebugCanvas::drawTo(SkCanvas* canvas, int index) {
int counter = 0;
SkASSERT(!commandVector.empty());
SkASSERT(index < (int)commandVector.size());
int i;
// This only works assuming the canvas and device are the same ones that
// were previously drawn into because they need to preserve all saves
// and restores.
if (fIndex < index) {
i = fIndex + 1;
} else {
i = 0;
canvas->clear(0);
canvas->resetMatrix();
applyUserTransform(canvas);
}
for (; i <= index; i++) {
if (i == index && fFilter) {
SkPaint p;
p.setColor(0xAAFFFFFF);
canvas->save();
canvas->resetMatrix();
SkRect mask;
mask.set(SkIntToScalar(0), SkIntToScalar(0),
SkIntToScalar(fWidth), SkIntToScalar(fHeight));
canvas->clipRect(mask, SkRegion::kReplace_Op, false);
canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0),
SkIntToScalar(fWidth), SkIntToScalar(fHeight), p);
canvas->restore();
}
if (commandVector[i]->isVisible()) {
commandVector[i]->execute(canvas);
}
}
fMatrix = canvas->getTotalMatrix();
fClip = canvas->getTotalClip().getBounds();
fIndex = index;
}
SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) {
SkASSERT(index < (int)commandVector.size());
return commandVector[index];
}
std::vector<std::string>* SkDebugCanvas::getCommandInfo(int index) {
SkASSERT(index < (int)commandVector.size());
return commandVector[index]->Info();
}
bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) {
SkASSERT(index < (int)commandVector.size());
return commandVector[index]->isVisible();
}
std::vector<SkDrawCommand*> SkDebugCanvas::getDrawCommands() {
return commandVector;
}
// TODO(chudy): Free command string memory.
std::vector<std::string>* SkDebugCanvas::getDrawCommandsAsStrings() {
std::vector<std::string>* commandString = new std::vector<std::string>();
if (!commandVector.empty()) {
for(it = commandVector.begin(); it != commandVector.end(); ++it) {
commandString->push_back((*it)->toString());
}
}
return commandString;
}
void SkDebugCanvas::toggleFilter(bool toggle) {
fFilter = toggle;
}
void SkDebugCanvas::clear(SkColor color) {
addDrawCommand(new Clear(color));
}
bool SkDebugCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
addDrawCommand(new ClipPath(path, op, doAA));
return true;
}
bool SkDebugCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
addDrawCommand(new ClipRect(rect, op, doAA));
return true;
}
bool SkDebugCanvas::clipRegion(const SkRegion& region, SkRegion::Op op) {
addDrawCommand(new ClipRegion(region, op));
return true;
}
bool SkDebugCanvas::concat(const SkMatrix& matrix) {
addDrawCommand(new Concat(matrix));
return true;
}
void SkDebugCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
SkScalar top, const SkPaint* paint = NULL) {
addDrawCommand(new DrawBitmap(bitmap, left, top, paint));
}
void SkDebugCanvas::drawBitmapRect(const SkBitmap& bitmap,
const SkIRect* src, const SkRect& dst, const SkPaint* paint) {
addDrawCommand(new DrawBitmapRect(bitmap, src, dst, paint));
}
void SkDebugCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
const SkMatrix& matrix, const SkPaint* paint) {
addDrawCommand(new DrawBitmapMatrix(bitmap, matrix, paint));
}
void SkDebugCanvas::drawBitmapNine(const SkBitmap& bitmap,
const SkIRect& center, const SkRect& dst, const SkPaint* paint) {
addDrawCommand(new DrawBitmapNine(bitmap, center, dst, paint));
}
void SkDebugCanvas::drawData(const void* data, size_t length) {
addDrawCommand(new DrawData(data, length));
}
void SkDebugCanvas::drawPaint(const SkPaint& paint) {
addDrawCommand(new DrawPaint(paint));
}
void SkDebugCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
addDrawCommand(new DrawPath(path, paint));
}
void SkDebugCanvas::drawPicture(SkPicture& picture) {
addDrawCommand(new DrawPicture(picture));
}
void SkDebugCanvas::drawPoints(PointMode mode, size_t count,
const SkPoint pts[], const SkPaint& paint) {
addDrawCommand(new DrawPoints(mode, count, pts, paint));
}
void SkDebugCanvas::drawPosText(const void* text, size_t byteLength,
const SkPoint pos[], const SkPaint& paint) {
addDrawCommand(new DrawPosText(text, byteLength, pos, paint));
}
void SkDebugCanvas::drawPosTextH(const void* text, size_t byteLength,
const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
addDrawCommand(new DrawPosTextH(text, byteLength, xpos, constY, paint));
}
void SkDebugCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
// NOTE(chudy): Messing up when renamed to DrawRect... Why?
addDrawCommand(new DrawRectC(rect, paint));
}
void SkDebugCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
const SkPaint* paint = NULL) {
addDrawCommand(new DrawSprite(bitmap, left, top, paint));
}
void SkDebugCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
SkScalar y, const SkPaint& paint) {
addDrawCommand(new DrawTextC(text, byteLength, x, y, paint));
}
void SkDebugCanvas::drawTextOnPath(const void* text, size_t byteLength,
const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) {
addDrawCommand(new DrawTextOnPath(text, byteLength, path, matrix, paint));
}
void SkDebugCanvas::drawVertices(VertexMode vmode, int vertexCount,
const SkPoint vertices[], const SkPoint texs[], const SkColor colors[],
SkXfermode*, const uint16_t indices[], int indexCount,
const SkPaint& paint) {
addDrawCommand(new DrawVertices(vmode, vertexCount, vertices, texs, colors,
NULL, indices, indexCount, paint));
}
void SkDebugCanvas::restore() {
addDrawCommand(new Restore());
}
bool SkDebugCanvas::rotate(SkScalar degrees) {
addDrawCommand(new Rotate(degrees));
return true;
}
int SkDebugCanvas::save(SaveFlags flags) {
addDrawCommand(new Save(flags));
return true;
}
int SkDebugCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
SaveFlags flags) {
addDrawCommand(new SaveLayer(bounds, paint, flags));
return true;
}
bool SkDebugCanvas::scale(SkScalar sx, SkScalar sy) {
addDrawCommand(new Scale(sx, sy));
return true;
}
void SkDebugCanvas::setMatrix(const SkMatrix& matrix) {
addDrawCommand(new SetMatrix(matrix));
}
bool SkDebugCanvas::skew(SkScalar sx, SkScalar sy) {
addDrawCommand(new Skew(sx, sy));
return true;
}
bool SkDebugCanvas::translate(SkScalar dx, SkScalar dy) {
addDrawCommand(new Translate(dx, dy));
return true;
}
void SkDebugCanvas::toggleCommand(int index, bool toggle) {
SkASSERT(index < (int)commandVector.size());
commandVector[index]->setVisible(toggle);
}
|