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