blob: 5844049fe8ece0e6dde09dd089d48f1ae8bd29b7 [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 <cstring>
11#include <iostream>
12#include <string>
13#include "SkDrawCommand.h"
14#include "SkObjectParser.h"
15
16// TODO(chudy): Refactor into non subclass model.
17
18SkDrawCommand::SkDrawCommand() {
19 fVisible = true;
20}
21
22SkDrawCommand::~SkDrawCommand() {
23}
24
25const char* SkDrawCommand::GetCommandString(DrawType type) {
26 switch (type) {
27 case UNUSED: SkDEBUGFAIL("DrawType UNUSED\n"); break;
28 case DRAW_CLEAR: return "Clear";
29 case CLIP_PATH: return "Clip Path";
30 case CLIP_REGION: return "Clip Region";
31 case CLIP_RECT: return "Clip Rect";
32 case CONCAT: return "Concat";
33 case DRAW_BITMAP: return "Draw Bitmap";
34 case DRAW_BITMAP_MATRIX: return "Draw Bitmap Matrix";
35 case DRAW_BITMAP_NINE: return "Draw Bitmap Nine";
36 case DRAW_BITMAP_RECT: return "Draw Bitmap Rect";
37 case DRAW_DATA: return "Draw Data";
38 case DRAW_PAINT: return "Draw Paint";
39 case DRAW_PATH: return "Draw Path";
40 case DRAW_PICTURE: return "Draw Picture";
41 case DRAW_POINTS: return "Draw Points";
42 case DRAW_POS_TEXT: return "Draw Pos Text";
43 case DRAW_POS_TEXT_H: return "Draw Pos Text H";
44 case DRAW_RECT: return "Draw Rect";
45 case DRAW_SPRITE: return "Draw Sprite";
46 case DRAW_TEXT: return "Draw Text";
47 case DRAW_TEXT_ON_PATH: return "Draw Text On Path";
48 case DRAW_VERTICES: return "Draw Vertices";
49 case RESTORE: return "Restore";
50 case ROTATE: return "Rotate";
51 case SAVE: return "Save";
52 case SAVE_LAYER: return "Save Layer";
53 case SCALE: return "Scale";
54 case SET_MATRIX: return "Set Matrix";
55 case SKEW: return "Skew";
56 case TRANSLATE: return "Translate";
57 default:
58 SkDebugf("DrawType error 0x%08x\n", type);
59 SkASSERT(0);
60 break;
61 }
62 SkDEBUGFAIL("DrawType UNUSED\n");
63 return NULL;
64}
65
66std::string SkDrawCommand::toString() {
67 std::stringstream ss;
68 ss << GetCommandString(fDrawType);
69 return ss.str();
70}
71
72Clear::Clear(SkColor color) {
73 this->fColor = color;
74 this->fDrawType = DRAW_CLEAR;
75 this->fInfo.push_back(std::string("No Parameters"));
76}
77
78void Clear::execute(SkCanvas* canvas) {
79 canvas->clear(this->fColor);
80}
81
82ClipPath::ClipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
83 this->fPath = &path;
84 this->fOp = op;
85 this->fDoAA = doAA;
86 this->fDrawType = CLIP_PATH;
87
88 this->fInfo.push_back(SkObjectParser::PathToString(path));
89 this->fInfo.push_back(SkObjectParser::RegionOpToString(op));
90 this->fInfo.push_back(SkObjectParser::BoolToString(doAA));
91}
92
93void ClipPath::execute(SkCanvas* canvas) {
94 canvas->clipPath(*this->fPath, this->fOp, this->fDoAA);
95}
96
97ClipRegion::ClipRegion(const SkRegion& region, SkRegion::Op op) {
98 this->fRegion = &region;
99 this->fOp = op;
100 this->fDrawType = CLIP_REGION;
101
102 this->fInfo.push_back(SkObjectParser::RegionToString(region));
103 this->fInfo.push_back(SkObjectParser::RegionOpToString(op));
104}
105
106void ClipRegion::execute(SkCanvas* canvas) {
107 canvas->clipRegion(*this->fRegion, this->fOp);
108}
109
110ClipRect::ClipRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
111 this->fRect = &rect;
112 this->fOp = op;
113 this->fDoAA = doAA;
114 this->fDrawType = CLIP_RECT;
115
116 this->fInfo.push_back(SkObjectParser::RectToString(rect));
117 this->fInfo.push_back(SkObjectParser::RegionOpToString(op));
118 this->fInfo.push_back(SkObjectParser::BoolToString(doAA));
119}
120
121void ClipRect::execute(SkCanvas* canvas) {
122 canvas->clipRect(*this->fRect, this->fOp, this->fDoAA);
123}
124
125Concat::Concat(const SkMatrix& matrix) {
126 this->fMatrix = &matrix;
127 this->fDrawType = CONCAT;
128
129 this->fInfo.push_back(SkObjectParser::MatrixToString(matrix));
130}
131
132void Concat::execute(SkCanvas* canvas) {
133 canvas->concat(*this->fMatrix);
134}
135
136DrawBitmap::DrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
137 const SkPaint* paint) {
138 this->fBitmap = &bitmap;
139 this->fLeft = left;
140 this->fTop = top;
141 this->fPaint = paint;
142 this->fDrawType = DRAW_BITMAP;
143
144 this->fInfo.push_back(SkObjectParser::BitmapToString(bitmap));
145 this->fInfo.push_back(SkObjectParser::ScalarToString(left, "SkScalar left: "));
146 this->fInfo.push_back(SkObjectParser::ScalarToString(top, "SkScalar top: "));
147}
148
149void DrawBitmap::execute(SkCanvas* canvas) {
150 canvas->drawBitmap(*this->fBitmap, this->fLeft, this->fTop, this->fPaint);
151}
152
153DrawBitmapMatrix::DrawBitmapMatrix(const SkBitmap& bitmap,
154 const SkMatrix& matrix, const SkPaint* paint) {
155 this->fBitmap = &bitmap;
156 this->fMatrix = &matrix;
157 this->fPaint = paint;
158 this->fDrawType = DRAW_BITMAP_MATRIX;
159
160 this->fInfo.push_back(SkObjectParser::BitmapToString(bitmap));
161 this->fInfo.push_back(SkObjectParser::MatrixToString(matrix));
162 if (paint) this->fInfo.push_back(SkObjectParser::PaintToString(*paint));
163}
164
165void DrawBitmapMatrix::execute(SkCanvas* canvas) {
166 canvas->drawBitmapMatrix(*this->fBitmap, *this->fMatrix, this->fPaint);
167}
168
169DrawBitmapNine::DrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
170 const SkRect& dst, const SkPaint* paint) {
171 this->fBitmap = &bitmap;
172 this->fCenter = &center;
173 this->fDst = &dst;
174 this->fPaint = paint;
175 this->fDrawType = DRAW_BITMAP_NINE;
176
177 this->fInfo.push_back(SkObjectParser::BitmapToString(bitmap));
178 this->fInfo.push_back(SkObjectParser::IRectToString(center));
179 this->fInfo.push_back(SkObjectParser::RectToString(dst));
180 if (paint) this->fInfo.push_back(SkObjectParser::PaintToString(*paint));
181}
182
183void DrawBitmapNine::execute(SkCanvas* canvas) {
184 canvas->drawBitmapNine(*this->fBitmap, *this->fCenter, *this->fDst, this->fPaint);
185}
186
187DrawBitmapRect::DrawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
188 const SkRect& dst, const SkPaint* paint) {
189 this->fBitmap = &bitmap;
190 this->fSrc = src;
191 this->fDst = &dst;
192 this->fPaint = paint;
193 this->fDrawType = DRAW_BITMAP_RECT;
194
195 this->fInfo.push_back(SkObjectParser::BitmapToString(bitmap));
196 if (src) this->fInfo.push_back(SkObjectParser::IRectToString(*src));
197 this->fInfo.push_back(SkObjectParser::RectToString(dst));
198 if (paint) this->fInfo.push_back(SkObjectParser::PaintToString(*paint));
199}
200
201void DrawBitmapRect::execute(SkCanvas* canvas) {
202 canvas->drawBitmapRect(*this->fBitmap, this->fSrc, *this->fDst, this->fPaint);
203}
204
205DrawData::DrawData(const void* data, size_t length) {
206 this->fData = data;
207 this->fLength = length;
208 this->fDrawType = DRAW_DATA;
209 // TODO(chudy): See if we can't display data and length.
210}
211
212void DrawData::execute(SkCanvas* canvas) {
213 canvas->drawData(this->fData, this->fLength);
214}
215
216DrawPaint::DrawPaint(const SkPaint& paint) {
217 this->fPaint = &paint;
218 this->fDrawType = DRAW_PAINT;
219
220 this->fInfo.push_back(SkObjectParser::PaintToString(paint));
221}
222
223void DrawPaint::execute(SkCanvas* canvas) {
224 canvas->drawPaint(*this->fPaint);
225}
226
227DrawPath::DrawPath(const SkPath& path, const SkPaint& paint) {
228 this->fPath = &path;
229 this->fPaint = &paint;
230 this->fDrawType = DRAW_PATH;
231
232 this->fInfo.push_back(SkObjectParser::PathToString(path));
233 this->fInfo.push_back(SkObjectParser::PaintToString(paint));
234}
235
236void DrawPath::execute(SkCanvas* canvas) {
237 canvas->drawPath(*this->fPath, *this->fPaint);
238}
239
240DrawPicture::DrawPicture(SkPicture& picture) {
241 this->fPicture = &picture;
242 this->fDrawType = DRAW_PICTURE;
243 this->fInfo.push_back(std::string("Data unavailable. To be implemented"));
244}
245
246void DrawPicture::execute(SkCanvas* canvas) {
247 canvas->drawPicture(*this->fPicture);
248}
249
250DrawPoints::DrawPoints(SkCanvas::PointMode mode, size_t count,
251 const SkPoint pts[], const SkPaint& paint) {
252 this->fMode = mode;
253 this->fCount = count;
254 this->fPts = pts;
255 this->fPaint = &paint;
256 this->fDrawType = DRAW_POINTS;
257
258 this->fInfo.push_back(SkObjectParser::PointsToString(pts, count));
chudy@google.com92b11f62012-08-01 16:10:06 +0000259 this->fInfo.push_back(SkObjectParser::ScalarToString(count, "Points: "));
260 this->fInfo.push_back(SkObjectParser::PointModeToString(mode));
chudy@google.com902ebe52012-06-29 14:21:22 +0000261}
262
263void DrawPoints::execute(SkCanvas* canvas) {
264 canvas->drawPoints(this->fMode, this->fCount, this->fPts, *this->fPaint);
265}
266
267DrawPosText::DrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
268 const SkPaint& paint) {
269 this->fText = text;
270 this->fByteLength = byteLength;
271 this->fPos = pos;
272 this->fPaint = &paint;
273 this->fDrawType = DRAW_POS_TEXT;
274
275 this->fInfo.push_back(SkObjectParser::TextToString(text, byteLength));
276 // TODO(chudy): Test that this works.
277 this->fInfo.push_back(SkObjectParser::PointsToString(pos, 1));
278 this->fInfo.push_back(SkObjectParser::PaintToString(paint));
279}
280
281void DrawPosText::execute(SkCanvas* canvas) {
282 canvas->drawPosText(this->fText, this->fByteLength, this->fPos, *this->fPaint);
283}
284
285
286DrawPosTextH::DrawPosTextH(const void* text, size_t byteLength,
287 const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
288 this->fText = text;
289 this->fByteLength = byteLength;
290 this->fXpos = xpos;
291 this->fConstY = constY;
292 this->fPaint = &paint;
293 this->fDrawType = DRAW_POS_TEXT_H;
294
295 this->fInfo.push_back(SkObjectParser::TextToString(text, byteLength));
296 this->fInfo.push_back(SkObjectParser::ScalarToString(xpos[0], "XPOS: "));
297 this->fInfo.push_back(SkObjectParser::ScalarToString(constY, "SkScalar constY: "));
298 this->fInfo.push_back(SkObjectParser::PaintToString(paint));
299}
300
301void DrawPosTextH::execute(SkCanvas* canvas) {
302 canvas->drawPosTextH(this->fText, this->fByteLength, this->fXpos, this->fConstY,
303 *this->fPaint);
304}
305
306DrawRectC::DrawRectC(const SkRect& rect, const SkPaint& paint) {
307 this->fRect = &rect;
308 this->fPaint = &paint;
309 this->fDrawType = DRAW_RECT;
310
311 this->fInfo.push_back(SkObjectParser::RectToString(rect));
312 this->fInfo.push_back(SkObjectParser::PaintToString(paint));
313}
314
315void DrawRectC::execute(SkCanvas* canvas) {
316 canvas->drawRect(*this->fRect, *this->fPaint);
317}
318
319DrawSprite::DrawSprite(const SkBitmap& bitmap, int left, int top,
320 const SkPaint* paint) {
321 this->fBitmap = &bitmap;
322 this->fLeft = left;
323 this->fTop = top;
324 this->fPaint = paint;
325 this->fDrawType = DRAW_SPRITE;
326
327 this->fInfo.push_back(SkObjectParser::BitmapToString(bitmap));
328 this->fInfo.push_back(SkObjectParser::IntToString(left, "Left: "));
329 this->fInfo.push_back(SkObjectParser::IntToString(top, "Top: "));
330}
331
332void DrawSprite::execute(SkCanvas* canvas) {
333 canvas->drawSprite(*this->fBitmap, this->fLeft, this->fTop, this->fPaint);
334}
335
336DrawTextC::DrawTextC(const void* text, size_t byteLength, SkScalar x, SkScalar y,
337 const SkPaint& paint) {
338 this->fText = text;
339 this->fByteLength = byteLength;
340 this->fX = x;
341 this->fY = y;
342 this->fPaint = &paint;
343 this->fDrawType = DRAW_TEXT;
344
345 this->fInfo.push_back(SkObjectParser::TextToString(text, byteLength));
346 this->fInfo.push_back(SkObjectParser::ScalarToString(x, "SkScalar x: "));
347 this->fInfo.push_back(SkObjectParser::ScalarToString(y, "SkScalar y: "));
348 this->fInfo.push_back(SkObjectParser::PaintToString(paint));
349}
350
351void DrawTextC::execute(SkCanvas* canvas) {
352 canvas->drawText(this->fText, this->fByteLength, this->fX, this->fY, *this->fPaint);
353}
354
355DrawTextOnPath::DrawTextOnPath(const void* text, size_t byteLength,
356 const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) {
357 this->fText = text;
358 this->fByteLength = byteLength;
359 this->fPath = &path;
360 this->fMatrix = matrix;
361 this->fPaint = &paint;
362 this->fDrawType = DRAW_TEXT_ON_PATH;
363
364 this->fInfo.push_back(SkObjectParser::TextToString(text, byteLength));
365 this->fInfo.push_back(SkObjectParser::PathToString(path));
366 if (matrix) this->fInfo.push_back(SkObjectParser::MatrixToString(*matrix));
367 this->fInfo.push_back(SkObjectParser::PaintToString(paint));
368}
369
370void DrawTextOnPath::execute(SkCanvas* canvas) {
371 canvas->drawTextOnPath(this->fText, this->fByteLength, *this->fPath,
372 this->fMatrix, *this->fPaint);
373}
374
375DrawVertices::DrawVertices(SkCanvas::VertexMode vmode, int vertexCount,
376 const SkPoint vertices[], const SkPoint texs[], const SkColor colors[],
377 SkXfermode* xfermode, const uint16_t indices[], int indexCount,
378 const SkPaint& paint) {
379 this->fVmode = vmode;
380 this->fVertexCount = vertexCount;
381 this->fTexs = texs;
382 this->fColors = colors;
383 this->fXfermode = xfermode;
384 this->fIndices = indices;
385 this->fIndexCount = indexCount;
386 this->fPaint = &paint;
387 this->fDrawType = DRAW_VERTICES;
388 // TODO(chudy)
389 this->fInfo.push_back(std::string("To be implemented"));
390}
391
392void DrawVertices::execute(SkCanvas* canvas) {
393 canvas->drawVertices(this->fVmode, this->fVertexCount, this->fVertices,
394 this->fTexs, this->fColors, this->fXfermode, this->fIndices,
395 this->fIndexCount, *this->fPaint);
396}
397
398Restore::Restore() {
399 this->fDrawType = RESTORE;
400 this->fInfo.push_back(std::string("No Parameters"));
401}
402
403void Restore::execute(SkCanvas* canvas) {
404 canvas->restore();
405}
406
407Rotate::Rotate(SkScalar degrees) {
408 this->fDegrees = degrees;
409 this->fDrawType = ROTATE;
410
411 this->fInfo.push_back(SkObjectParser::ScalarToString(degrees, "SkScalar degrees: "));
412}
413
414void Rotate::execute(SkCanvas* canvas) {
415 canvas->rotate(this->fDegrees);
416}
417
418Save::Save(SkCanvas::SaveFlags flags) {
419 this->fFlags = flags;
420 this->fDrawType = SAVE;
421
422 this->fInfo.push_back(SkObjectParser::SaveFlagsToString(flags));
423}
424
425void Save::execute(SkCanvas* canvas) {
426 canvas->save(this->fFlags);
427}
428
429SaveLayer::SaveLayer(const SkRect* bounds, const SkPaint* paint,
430 SkCanvas::SaveFlags flags) {
431 this->fBounds = bounds;
432 this->fPaint = paint;
433 this->fFlags = flags;
434 this->fDrawType = SAVE_LAYER;
435
436 if (bounds) this->fInfo.push_back(SkObjectParser::RectToString(*bounds));
437 if (paint) this->fInfo.push_back(SkObjectParser::PaintToString(*paint));
438 this->fInfo.push_back(SkObjectParser::SaveFlagsToString(flags));
439}
440
441void SaveLayer::execute(SkCanvas* canvas) {
442 canvas->saveLayer(this->fBounds, this->fPaint, this->fFlags);
443}
444
445Scale::Scale(SkScalar sx, SkScalar sy) {
446 this->fSx = sx;
447 this->fSy = sy;
448 this->fDrawType = SCALE;
449
450 this->fInfo.push_back(SkObjectParser::ScalarToString(sx, "SkScalar sx: "));
451 this->fInfo.push_back(SkObjectParser::ScalarToString(sy, "SkScalar sy: "));
452}
453
454void Scale::execute(SkCanvas* canvas) {
455 canvas->scale(this->fSx, this->fSy);
456}
457
458SetMatrix::SetMatrix(const SkMatrix& matrix) {
459 this->fMatrix = &matrix;
460 this->fDrawType = SET_MATRIX;
461
462 this->fInfo.push_back(SkObjectParser::MatrixToString(matrix));
463}
464
465void SetMatrix::execute(SkCanvas* canvas) {
466 canvas->setMatrix(*this->fMatrix);
467}
468
469Skew::Skew(SkScalar sx, SkScalar sy) {
470 this->fSx = sx;
471 this->fSy = sy;
472 this->fDrawType = SKEW;
473
474 this->fInfo.push_back(SkObjectParser::ScalarToString(sx, "SkScalar sx: "));
475 this->fInfo.push_back(SkObjectParser::ScalarToString(sy, "SkScalar sy: "));
476}
477
478void Skew::execute(SkCanvas* canvas) {
479 canvas->skew(this->fSx, this->fSy);
480}
481
482Translate::Translate(SkScalar dx, SkScalar dy) {
483 this->fDx = dx;
484 this->fDy = dy;
485 this->fDrawType = TRANSLATE;
486
487 this->fInfo.push_back(SkObjectParser::ScalarToString(dx, "SkScalar dx: "));
488 this->fInfo.push_back(SkObjectParser::ScalarToString(dy, "SkScalar dy: "));
489}
490
491void Translate::execute(SkCanvas* canvas) {
492 canvas->translate(this->fDx, this->fDy);
493}