1 
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 "SkDrawCommand.h"
11 #include "SkObjectParser.h"
12 #include "SkPicture.h"
13 #include "SkTextBlob.h"
14 
15 // TODO(chudy): Refactor into non subclass model.
16 
SkDrawCommand(OpType type)17 SkDrawCommand::SkDrawCommand(OpType type)
18     : fOpType(type)
19     , fVisible(true) {
20 }
21 
~SkDrawCommand()22 SkDrawCommand::~SkDrawCommand() {
23     fInfo.deleteAll();
24 }
25 
GetCommandString(OpType type)26 const char* SkDrawCommand::GetCommandString(OpType type) {
27     switch (type) {
28         case kBeginCommentGroup_OpType: return "BeginCommentGroup";
29         case kBeginDrawPicture_OpType: return "BeginDrawPicture";
30         case kClipPath_OpType: return "ClipPath";
31         case kClipRegion_OpType: return "ClipRegion";
32         case kClipRect_OpType: return "ClipRect";
33         case kClipRRect_OpType: return "ClipRRect";
34         case kComment_OpType: return "Comment";
35         case kConcat_OpType: return "Concat";
36         case kDrawBitmap_OpType: return "DrawBitmap";
37         case kDrawBitmapNine_OpType: return "DrawBitmapNine";
38         case kDrawBitmapRect_OpType: return "DrawBitmapRect";
39         case kDrawClear_OpType: return "DrawClear";
40         case kDrawDRRect_OpType: return "DrawDRRect";
41         case kDrawOval_OpType: return "DrawOval";
42         case kDrawPaint_OpType: return "DrawPaint";
43         case kDrawPatch_OpType: return "DrawPatch";
44         case kDrawPath_OpType: return "DrawPath";
45         case kDrawPoints_OpType: return "DrawPoints";
46         case kDrawPosText_OpType: return "DrawPosText";
47         case kDrawPosTextH_OpType: return "DrawPosTextH";
48         case kDrawRect_OpType: return "DrawRect";
49         case kDrawRRect_OpType: return "DrawRRect";
50         case kDrawSprite_OpType: return "DrawSprite";
51         case kDrawText_OpType: return "DrawText";
52         case kDrawTextBlob_OpType: return "DrawTextBlob";
53         case kDrawTextOnPath_OpType: return "DrawTextOnPath";
54         case kDrawVertices_OpType: return "DrawVertices";
55         case kEndCommentGroup_OpType: return "EndCommentGroup";
56         case kEndDrawPicture_OpType: return "EndDrawPicture";
57         case kRestore_OpType: return "Restore";
58         case kSave_OpType: return "Save";
59         case kSaveLayer_OpType: return "SaveLayer";
60         case kSetMatrix_OpType: return "SetMatrix";
61         default:
62             SkDebugf("OpType error 0x%08x\n", type);
63             SkASSERT(0);
64             break;
65     }
66     SkDEBUGFAIL("DrawType UNUSED\n");
67     return NULL;
68 }
69 
toString() const70 SkString SkDrawCommand::toString() const {
71     return SkString(GetCommandString(fOpType));
72 }
73 
SkClearCommand(SkColor color)74 SkClearCommand::SkClearCommand(SkColor color) : INHERITED(kDrawClear_OpType) {
75     fColor = color;
76     fInfo.push(SkObjectParser::CustomTextToString("No Parameters"));
77 }
78 
execute(SkCanvas * canvas) const79 void SkClearCommand::execute(SkCanvas* canvas) const {
80     canvas->clear(fColor);
81 }
82 
83 namespace {
84 
xlate_and_scale_to_bounds(SkCanvas * canvas,const SkRect & bounds)85 void xlate_and_scale_to_bounds(SkCanvas* canvas, const SkRect& bounds) {
86     const SkISize& size = canvas->getDeviceSize();
87 
88     static const SkScalar kInsetFrac = 0.9f; // Leave a border around object
89 
90     canvas->translate(size.fWidth/2.0f, size.fHeight/2.0f);
91     if (bounds.width() > bounds.height()) {
92         canvas->scale(SkDoubleToScalar((kInsetFrac*size.fWidth)/bounds.width()),
93                       SkDoubleToScalar((kInsetFrac*size.fHeight)/bounds.width()));
94     } else {
95         canvas->scale(SkDoubleToScalar((kInsetFrac*size.fWidth)/bounds.height()),
96                       SkDoubleToScalar((kInsetFrac*size.fHeight)/bounds.height()));
97     }
98     canvas->translate(-bounds.centerX(), -bounds.centerY());
99 }
100 
101 
render_path(SkCanvas * canvas,const SkPath & path)102 void render_path(SkCanvas* canvas, const SkPath& path) {
103     canvas->clear(0xFFFFFFFF);
104     canvas->save();
105 
106     const SkRect& bounds = path.getBounds();
107 
108     xlate_and_scale_to_bounds(canvas, bounds);
109 
110     SkPaint p;
111     p.setColor(SK_ColorBLACK);
112     p.setStyle(SkPaint::kStroke_Style);
113 
114     canvas->drawPath(path, p);
115     canvas->restore();
116 }
117 
render_bitmap(SkCanvas * canvas,const SkBitmap & input,const SkRect * srcRect=NULL)118 void render_bitmap(SkCanvas* canvas, const SkBitmap& input, const SkRect* srcRect = NULL) {
119     const SkISize& size = canvas->getDeviceSize();
120 
121     SkScalar xScale = SkIntToScalar(size.fWidth-2) / input.width();
122     SkScalar yScale = SkIntToScalar(size.fHeight-2) / input.height();
123 
124     if (input.width() > input.height()) {
125         yScale *= input.height() / (float) input.width();
126     } else {
127         xScale *= input.width() / (float) input.height();
128     }
129 
130     SkRect dst = SkRect::MakeXYWH(SK_Scalar1, SK_Scalar1,
131                                   xScale * input.width(),
132                                   yScale * input.height());
133 
134     canvas->clear(0xFFFFFFFF);
135     canvas->drawBitmapRect(input, NULL, dst);
136 
137     if (srcRect) {
138         SkRect r = SkRect::MakeLTRB(srcRect->fLeft * xScale + SK_Scalar1,
139                                     srcRect->fTop * yScale + SK_Scalar1,
140                                     srcRect->fRight * xScale + SK_Scalar1,
141                                     srcRect->fBottom * yScale + SK_Scalar1);
142         SkPaint p;
143         p.setColor(SK_ColorRED);
144         p.setStyle(SkPaint::kStroke_Style);
145 
146         canvas->drawRect(r, p);
147     }
148 }
149 
render_rrect(SkCanvas * canvas,const SkRRect & rrect)150 void render_rrect(SkCanvas* canvas, const SkRRect& rrect) {
151     canvas->clear(0xFFFFFFFF);
152     canvas->save();
153 
154     const SkRect& bounds = rrect.getBounds();
155 
156     xlate_and_scale_to_bounds(canvas, bounds);
157 
158     SkPaint p;
159     p.setColor(SK_ColorBLACK);
160     p.setStyle(SkPaint::kStroke_Style);
161 
162     canvas->drawRRect(rrect, p);
163     canvas->restore();
164 }
165 
render_drrect(SkCanvas * canvas,const SkRRect & outer,const SkRRect & inner)166 void render_drrect(SkCanvas* canvas, const SkRRect& outer, const SkRRect& inner) {
167     canvas->clear(0xFFFFFFFF);
168     canvas->save();
169 
170     const SkRect& bounds = outer.getBounds();
171 
172     xlate_and_scale_to_bounds(canvas, bounds);
173 
174     SkPaint p;
175     p.setColor(SK_ColorBLACK);
176     p.setStyle(SkPaint::kStroke_Style);
177 
178     canvas->drawDRRect(outer, inner, p);
179     canvas->restore();
180 }
181 
182 };
183 
184 
SkClipPathCommand(const SkPath & path,SkRegion::Op op,bool doAA)185 SkClipPathCommand::SkClipPathCommand(const SkPath& path, SkRegion::Op op, bool doAA)
186     : INHERITED(kClipPath_OpType) {
187     fPath = path;
188     fOp = op;
189     fDoAA = doAA;
190 
191     fInfo.push(SkObjectParser::PathToString(path));
192     fInfo.push(SkObjectParser::RegionOpToString(op));
193     fInfo.push(SkObjectParser::BoolToString(doAA));
194 }
195 
execute(SkCanvas * canvas) const196 void SkClipPathCommand::execute(SkCanvas* canvas) const {
197     canvas->clipPath(fPath, fOp, fDoAA);
198 }
199 
render(SkCanvas * canvas) const200 bool SkClipPathCommand::render(SkCanvas* canvas) const {
201     render_path(canvas, fPath);
202     return true;
203 }
204 
SkClipRegionCommand(const SkRegion & region,SkRegion::Op op)205 SkClipRegionCommand::SkClipRegionCommand(const SkRegion& region, SkRegion::Op op)
206     : INHERITED(kClipRegion_OpType) {
207     fRegion = region;
208     fOp = op;
209 
210     fInfo.push(SkObjectParser::RegionToString(region));
211     fInfo.push(SkObjectParser::RegionOpToString(op));
212 }
213 
execute(SkCanvas * canvas) const214 void SkClipRegionCommand::execute(SkCanvas* canvas) const {
215     canvas->clipRegion(fRegion, fOp);
216 }
217 
SkClipRectCommand(const SkRect & rect,SkRegion::Op op,bool doAA)218 SkClipRectCommand::SkClipRectCommand(const SkRect& rect, SkRegion::Op op, bool doAA)
219     : INHERITED(kClipRect_OpType) {
220     fRect = rect;
221     fOp = op;
222     fDoAA = doAA;
223 
224     fInfo.push(SkObjectParser::RectToString(rect));
225     fInfo.push(SkObjectParser::RegionOpToString(op));
226     fInfo.push(SkObjectParser::BoolToString(doAA));
227 }
228 
execute(SkCanvas * canvas) const229 void SkClipRectCommand::execute(SkCanvas* canvas) const {
230     canvas->clipRect(fRect, fOp, fDoAA);
231 }
232 
SkClipRRectCommand(const SkRRect & rrect,SkRegion::Op op,bool doAA)233 SkClipRRectCommand::SkClipRRectCommand(const SkRRect& rrect, SkRegion::Op op, bool doAA)
234     : INHERITED(kClipRRect_OpType) {
235     fRRect = rrect;
236     fOp = op;
237     fDoAA = doAA;
238 
239     fInfo.push(SkObjectParser::RRectToString(rrect));
240     fInfo.push(SkObjectParser::RegionOpToString(op));
241     fInfo.push(SkObjectParser::BoolToString(doAA));
242 }
243 
execute(SkCanvas * canvas) const244 void SkClipRRectCommand::execute(SkCanvas* canvas) const {
245     canvas->clipRRect(fRRect, fOp, fDoAA);
246 }
247 
render(SkCanvas * canvas) const248 bool SkClipRRectCommand::render(SkCanvas* canvas) const {
249     render_rrect(canvas, fRRect);
250     return true;
251 }
252 
SkConcatCommand(const SkMatrix & matrix)253 SkConcatCommand::SkConcatCommand(const SkMatrix& matrix)
254     : INHERITED(kConcat_OpType) {
255     fMatrix = matrix;
256 
257     fInfo.push(SkObjectParser::MatrixToString(matrix));
258 }
259 
execute(SkCanvas * canvas) const260 void SkConcatCommand::execute(SkCanvas* canvas) const {
261     canvas->concat(fMatrix);
262 }
263 
SkDrawBitmapCommand(const SkBitmap & bitmap,SkScalar left,SkScalar top,const SkPaint * paint)264 SkDrawBitmapCommand::SkDrawBitmapCommand(const SkBitmap& bitmap, SkScalar left, SkScalar top,
265                                          const SkPaint* paint)
266     : INHERITED(kDrawBitmap_OpType) {
267     fBitmap = bitmap;
268     fLeft = left;
269     fTop = top;
270     if (paint) {
271         fPaint = *paint;
272         fPaintPtr = &fPaint;
273     } else {
274         fPaintPtr = NULL;
275     }
276 
277     fInfo.push(SkObjectParser::BitmapToString(bitmap));
278     fInfo.push(SkObjectParser::ScalarToString(left, "SkScalar left: "));
279     fInfo.push(SkObjectParser::ScalarToString(top, "SkScalar top: "));
280     if (paint) {
281         fInfo.push(SkObjectParser::PaintToString(*paint));
282     }
283 }
284 
execute(SkCanvas * canvas) const285 void SkDrawBitmapCommand::execute(SkCanvas* canvas) const {
286     canvas->drawBitmap(fBitmap, fLeft, fTop, fPaintPtr);
287 }
288 
render(SkCanvas * canvas) const289 bool SkDrawBitmapCommand::render(SkCanvas* canvas) const {
290     render_bitmap(canvas, fBitmap);
291     return true;
292 }
293 
SkDrawBitmapNineCommand(const SkBitmap & bitmap,const SkIRect & center,const SkRect & dst,const SkPaint * paint)294 SkDrawBitmapNineCommand::SkDrawBitmapNineCommand(const SkBitmap& bitmap, const SkIRect& center,
295                                                  const SkRect& dst, const SkPaint* paint)
296     : INHERITED(kDrawBitmapNine_OpType) {
297     fBitmap = bitmap;
298     fCenter = center;
299     fDst = dst;
300     if (paint) {
301         fPaint = *paint;
302         fPaintPtr = &fPaint;
303     } else {
304         fPaintPtr = NULL;
305     }
306 
307     fInfo.push(SkObjectParser::BitmapToString(bitmap));
308     fInfo.push(SkObjectParser::IRectToString(center));
309     fInfo.push(SkObjectParser::RectToString(dst, "Dst: "));
310     if (paint) {
311         fInfo.push(SkObjectParser::PaintToString(*paint));
312     }
313 }
314 
execute(SkCanvas * canvas) const315 void SkDrawBitmapNineCommand::execute(SkCanvas* canvas) const {
316     canvas->drawBitmapNine(fBitmap, fCenter, fDst, fPaintPtr);
317 }
318 
render(SkCanvas * canvas) const319 bool SkDrawBitmapNineCommand::render(SkCanvas* canvas) const {
320     render_bitmap(canvas, fBitmap);
321     return true;
322 }
323 
SkDrawBitmapRectCommand(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint * paint,SkCanvas::DrawBitmapRectFlags flags)324 SkDrawBitmapRectCommand::SkDrawBitmapRectCommand(const SkBitmap& bitmap, const SkRect* src,
325                                                  const SkRect& dst, const SkPaint* paint,
326                                                  SkCanvas::DrawBitmapRectFlags flags)
327     : INHERITED(kDrawBitmapRect_OpType) {
328     fBitmap = bitmap;
329     if (src) {
330         fSrc = *src;
331     } else {
332         fSrc.setEmpty();
333     }
334     fDst = dst;
335 
336     if (paint) {
337         fPaint = *paint;
338         fPaintPtr = &fPaint;
339     } else {
340         fPaintPtr = NULL;
341     }
342     fFlags = flags;
343 
344     fInfo.push(SkObjectParser::BitmapToString(bitmap));
345     if (src) {
346         fInfo.push(SkObjectParser::RectToString(*src, "Src: "));
347     }
348     fInfo.push(SkObjectParser::RectToString(dst, "Dst: "));
349     if (paint) {
350         fInfo.push(SkObjectParser::PaintToString(*paint));
351     }
352     fInfo.push(SkObjectParser::IntToString(fFlags, "Flags: "));
353 }
354 
execute(SkCanvas * canvas) const355 void SkDrawBitmapRectCommand::execute(SkCanvas* canvas) const {
356     canvas->drawBitmapRectToRect(fBitmap, this->srcRect(), fDst, fPaintPtr, fFlags);
357 }
358 
render(SkCanvas * canvas) const359 bool SkDrawBitmapRectCommand::render(SkCanvas* canvas) const {
360     render_bitmap(canvas, fBitmap, this->srcRect());
361     return true;
362 }
363 
SkBeginCommentGroupCommand(const char * description)364 SkBeginCommentGroupCommand::SkBeginCommentGroupCommand(const char* description)
365     : INHERITED(kBeginCommentGroup_OpType)
366     , fDescription(description) {
367     SkString* temp = new SkString;
368     temp->appendf("Description: %s", description);
369     fInfo.push(temp);
370 }
371 
SkCommentCommand(const char * kywd,const char * value)372 SkCommentCommand::SkCommentCommand(const char* kywd, const char* value)
373     : INHERITED(kComment_OpType)
374     , fKywd(kywd)
375     , fValue(value) {
376     SkString* temp = new SkString;
377     temp->appendf("%s: %s", kywd, value);
378     fInfo.push(temp);
379 }
380 
SkEndCommentGroupCommand()381 SkEndCommentGroupCommand::SkEndCommentGroupCommand()
382     : INHERITED(kEndCommentGroup_OpType) {
383 }
384 
SkDrawOvalCommand(const SkRect & oval,const SkPaint & paint)385 SkDrawOvalCommand::SkDrawOvalCommand(const SkRect& oval, const SkPaint& paint)
386     : INHERITED(kDrawOval_OpType) {
387     fOval = oval;
388     fPaint = paint;
389 
390     fInfo.push(SkObjectParser::RectToString(oval));
391     fInfo.push(SkObjectParser::PaintToString(paint));
392 }
393 
execute(SkCanvas * canvas) const394 void SkDrawOvalCommand::execute(SkCanvas* canvas) const {
395     canvas->drawOval(fOval, fPaint);
396 }
397 
render(SkCanvas * canvas) const398 bool SkDrawOvalCommand::render(SkCanvas* canvas) const {
399     canvas->clear(0xFFFFFFFF);
400     canvas->save();
401 
402     xlate_and_scale_to_bounds(canvas, fOval);
403 
404     SkPaint p;
405     p.setColor(SK_ColorBLACK);
406     p.setStyle(SkPaint::kStroke_Style);
407 
408     canvas->drawOval(fOval, p);
409     canvas->restore();
410 
411     return true;
412 }
413 
SkDrawPaintCommand(const SkPaint & paint)414 SkDrawPaintCommand::SkDrawPaintCommand(const SkPaint& paint)
415     : INHERITED(kDrawPaint_OpType) {
416     fPaint = paint;
417 
418     fInfo.push(SkObjectParser::PaintToString(paint));
419 }
420 
execute(SkCanvas * canvas) const421 void SkDrawPaintCommand::execute(SkCanvas* canvas) const {
422     canvas->drawPaint(fPaint);
423 }
424 
render(SkCanvas * canvas) const425 bool SkDrawPaintCommand::render(SkCanvas* canvas) const {
426     canvas->clear(0xFFFFFFFF);
427     canvas->drawPaint(fPaint);
428     return true;
429 }
430 
SkDrawPathCommand(const SkPath & path,const SkPaint & paint)431 SkDrawPathCommand::SkDrawPathCommand(const SkPath& path, const SkPaint& paint)
432     : INHERITED(kDrawPath_OpType) {
433     fPath = path;
434     fPaint = paint;
435 
436     fInfo.push(SkObjectParser::PathToString(path));
437     fInfo.push(SkObjectParser::PaintToString(paint));
438 }
439 
execute(SkCanvas * canvas) const440 void SkDrawPathCommand::execute(SkCanvas* canvas) const {
441     canvas->drawPath(fPath, fPaint);
442 }
443 
render(SkCanvas * canvas) const444 bool SkDrawPathCommand::render(SkCanvas* canvas) const {
445     render_path(canvas, fPath);
446     return true;
447 }
448 
SkBeginDrawPictureCommand(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)449 SkBeginDrawPictureCommand::SkBeginDrawPictureCommand(const SkPicture* picture,
450                                                      const SkMatrix* matrix,
451                                                      const SkPaint* paint)
452     : INHERITED(kBeginDrawPicture_OpType)
453     , fPicture(SkRef(picture)) {
454 
455     SkString* str = new SkString;
456     str->appendf("SkPicture: L: %f T: %f R: %f B: %f",
457                  picture->cullRect().fLeft, picture->cullRect().fTop,
458                  picture->cullRect().fRight, picture->cullRect().fBottom);
459     fInfo.push(str);
460 
461     if (matrix) {
462         fMatrix.set(*matrix);
463         fInfo.push(SkObjectParser::MatrixToString(*matrix));
464     }
465 
466     if (paint) {
467         fPaint.set(*paint);
468         fInfo.push(SkObjectParser::PaintToString(*paint));
469     }
470 
471 }
472 
execute(SkCanvas * canvas) const473 void SkBeginDrawPictureCommand::execute(SkCanvas* canvas) const {
474     if (fPaint.isValid()) {
475         SkRect bounds = fPicture->cullRect();
476         if (fMatrix.isValid()) {
477             fMatrix.get()->mapRect(&bounds);
478         }
479         canvas->saveLayer(&bounds, fPaint.get());
480     }
481 
482     if (fMatrix.isValid()) {
483         if (!fPaint.isValid()) {
484             canvas->save();
485         }
486         canvas->concat(*fMatrix.get());
487     }
488 }
489 
render(SkCanvas * canvas) const490 bool SkBeginDrawPictureCommand::render(SkCanvas* canvas) const {
491     canvas->clear(0xFFFFFFFF);
492     canvas->save();
493 
494     xlate_and_scale_to_bounds(canvas, fPicture->cullRect());
495 
496     canvas->drawPicture(fPicture.get());
497 
498     canvas->restore();
499 
500     return true;
501 }
502 
SkEndDrawPictureCommand(bool restore)503 SkEndDrawPictureCommand::SkEndDrawPictureCommand(bool restore)
504     : INHERITED(kEndDrawPicture_OpType) , fRestore(restore) { }
505 
execute(SkCanvas * canvas) const506 void SkEndDrawPictureCommand::execute(SkCanvas* canvas) const {
507     if (fRestore) {
508         canvas->restore();
509     }
510 }
511 
SkDrawPointsCommand(SkCanvas::PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)512 SkDrawPointsCommand::SkDrawPointsCommand(SkCanvas::PointMode mode, size_t count,
513                                          const SkPoint pts[], const SkPaint& paint)
514     : INHERITED(kDrawPoints_OpType) {
515     fMode = mode;
516     fCount = count;
517     fPts = new SkPoint[count];
518     memcpy(fPts, pts, count * sizeof(SkPoint));
519     fPaint = paint;
520 
521     fInfo.push(SkObjectParser::PointsToString(pts, count));
522     fInfo.push(SkObjectParser::ScalarToString(SkIntToScalar((unsigned int)count),
523                                               "Points: "));
524     fInfo.push(SkObjectParser::PointModeToString(mode));
525     fInfo.push(SkObjectParser::PaintToString(paint));
526 }
527 
execute(SkCanvas * canvas) const528 void SkDrawPointsCommand::execute(SkCanvas* canvas) const {
529     canvas->drawPoints(fMode, fCount, fPts, fPaint);
530 }
531 
render(SkCanvas * canvas) const532 bool SkDrawPointsCommand::render(SkCanvas* canvas) const {
533     canvas->clear(0xFFFFFFFF);
534     canvas->save();
535 
536     SkRect bounds;
537 
538     bounds.setEmpty();
539     for (unsigned int i = 0; i < fCount; ++i) {
540         bounds.growToInclude(fPts[i].fX, fPts[i].fY);
541     }
542 
543     xlate_and_scale_to_bounds(canvas, bounds);
544 
545     SkPaint p;
546     p.setColor(SK_ColorBLACK);
547     p.setStyle(SkPaint::kStroke_Style);
548 
549     canvas->drawPoints(fMode, fCount, fPts, p);
550     canvas->restore();
551 
552     return true;
553 }
554 
SkDrawPosTextCommand(const void * text,size_t byteLength,const SkPoint pos[],const SkPaint & paint)555 SkDrawPosTextCommand::SkDrawPosTextCommand(const void* text, size_t byteLength,
556                                            const SkPoint pos[], const SkPaint& paint)
557     : INHERITED(kDrawPosText_OpType) {
558     size_t numPts = paint.countText(text, byteLength);
559 
560     fText = new char[byteLength];
561     memcpy(fText, text, byteLength);
562     fByteLength = byteLength;
563 
564     fPos = new SkPoint[numPts];
565     memcpy(fPos, pos, numPts * sizeof(SkPoint));
566 
567     fPaint = paint;
568 
569     fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding()));
570     // TODO(chudy): Test that this works.
571     fInfo.push(SkObjectParser::PointsToString(pos, 1));
572     fInfo.push(SkObjectParser::PaintToString(paint));
573 }
574 
execute(SkCanvas * canvas) const575 void SkDrawPosTextCommand::execute(SkCanvas* canvas) const {
576     canvas->drawPosText(fText, fByteLength, fPos, fPaint);
577 }
578 
579 
SkDrawPosTextHCommand(const void * text,size_t byteLength,const SkScalar xpos[],SkScalar constY,const SkPaint & paint)580 SkDrawPosTextHCommand::SkDrawPosTextHCommand(const void* text, size_t byteLength,
581                                              const SkScalar xpos[], SkScalar constY,
582                                              const SkPaint& paint)
583     : INHERITED(kDrawPosTextH_OpType) {
584     size_t numPts = paint.countText(text, byteLength);
585 
586     fText = new char[byteLength];
587     memcpy(fText, text, byteLength);
588     fByteLength = byteLength;
589 
590     fXpos = new SkScalar[numPts];
591     memcpy(fXpos, xpos, numPts * sizeof(SkScalar));
592 
593     fConstY = constY;
594     fPaint = paint;
595 
596     fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding()));
597     fInfo.push(SkObjectParser::ScalarToString(xpos[0], "XPOS: "));
598     fInfo.push(SkObjectParser::ScalarToString(constY, "SkScalar constY: "));
599     fInfo.push(SkObjectParser::PaintToString(paint));
600 }
601 
execute(SkCanvas * canvas) const602 void SkDrawPosTextHCommand::execute(SkCanvas* canvas) const {
603     canvas->drawPosTextH(fText, fByteLength, fXpos, fConstY, fPaint);
604 }
605 
SkDrawTextBlobCommand(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)606 SkDrawTextBlobCommand::SkDrawTextBlobCommand(const SkTextBlob* blob, SkScalar x, SkScalar y,
607                                              const SkPaint& paint)
608     : INHERITED(kDrawTextBlob_OpType)
609     , fBlob(blob)
610     , fXPos(x)
611     , fYPos(y)
612     , fPaint(paint) {
613 
614     blob->ref();
615 
616     // FIXME: push blob info
617     fInfo.push(SkObjectParser::ScalarToString(x, "XPOS: "));
618     fInfo.push(SkObjectParser::ScalarToString(y, "YPOS: "));
619     fInfo.push(SkObjectParser::RectToString(fBlob->bounds(), "Bounds: "));
620     fInfo.push(SkObjectParser::PaintToString(paint));
621 }
622 
execute(SkCanvas * canvas) const623 void SkDrawTextBlobCommand::execute(SkCanvas* canvas) const {
624     canvas->drawTextBlob(fBlob, fXPos, fYPos, fPaint);
625 }
626 
render(SkCanvas * canvas) const627 bool SkDrawTextBlobCommand::render(SkCanvas* canvas) const {
628     canvas->clear(SK_ColorWHITE);
629     canvas->save();
630 
631     SkRect bounds = fBlob->bounds().makeOffset(fXPos, fYPos);
632     xlate_and_scale_to_bounds(canvas, bounds);
633 
634     canvas->drawTextBlob(fBlob.get(), fXPos, fYPos, fPaint);
635 
636     canvas->restore();
637 
638     return true;
639 }
640 
SkDrawPatchCommand(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkXfermode * xfermode,const SkPaint & paint)641 SkDrawPatchCommand::SkDrawPatchCommand(const SkPoint cubics[12], const SkColor colors[4],
642                                        const SkPoint texCoords[4], SkXfermode* xfermode,
643                                        const SkPaint& paint)
644     : INHERITED(kDrawPatch_OpType) {
645     memcpy(fCubics, cubics, sizeof(fCubics));
646     memcpy(fColors, colors, sizeof(fColors));
647     memcpy(fTexCoords, texCoords, sizeof(fTexCoords));
648     fXfermode.reset(xfermode);
649     fPaint = paint;
650 
651     fInfo.push(SkObjectParser::PaintToString(paint));
652 }
653 
execute(SkCanvas * canvas) const654 void SkDrawPatchCommand::execute(SkCanvas* canvas) const {
655     canvas->drawPatch(fCubics, fColors, fTexCoords, fXfermode, fPaint);
656 }
657 
SkDrawRectCommand(const SkRect & rect,const SkPaint & paint)658 SkDrawRectCommand::SkDrawRectCommand(const SkRect& rect, const SkPaint& paint)
659     : INHERITED(kDrawRect_OpType) {
660     fRect = rect;
661     fPaint = paint;
662 
663     fInfo.push(SkObjectParser::RectToString(rect));
664     fInfo.push(SkObjectParser::PaintToString(paint));
665 }
666 
execute(SkCanvas * canvas) const667 void SkDrawRectCommand::execute(SkCanvas* canvas) const {
668     canvas->drawRect(fRect, fPaint);
669 }
670 
SkDrawRRectCommand(const SkRRect & rrect,const SkPaint & paint)671 SkDrawRRectCommand::SkDrawRRectCommand(const SkRRect& rrect, const SkPaint& paint)
672     : INHERITED(kDrawRRect_OpType) {
673     fRRect = rrect;
674     fPaint = paint;
675 
676     fInfo.push(SkObjectParser::RRectToString(rrect));
677     fInfo.push(SkObjectParser::PaintToString(paint));
678 }
679 
execute(SkCanvas * canvas) const680 void SkDrawRRectCommand::execute(SkCanvas* canvas) const {
681     canvas->drawRRect(fRRect, fPaint);
682 }
683 
render(SkCanvas * canvas) const684 bool SkDrawRRectCommand::render(SkCanvas* canvas) const {
685     render_rrect(canvas, fRRect);
686     return true;
687 }
688 
SkDrawDRRectCommand(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)689 SkDrawDRRectCommand::SkDrawDRRectCommand(const SkRRect& outer,
690                                          const SkRRect& inner,
691                                          const SkPaint& paint)
692     : INHERITED(kDrawDRRect_OpType) {
693     fOuter = outer;
694     fInner = inner;
695     fPaint = paint;
696 
697     fInfo.push(SkObjectParser::RRectToString(outer));
698     fInfo.push(SkObjectParser::RRectToString(inner));
699     fInfo.push(SkObjectParser::PaintToString(paint));
700 }
701 
execute(SkCanvas * canvas) const702 void SkDrawDRRectCommand::execute(SkCanvas* canvas) const {
703     canvas->drawDRRect(fOuter, fInner, fPaint);
704 }
705 
render(SkCanvas * canvas) const706 bool SkDrawDRRectCommand::render(SkCanvas* canvas) const {
707     render_drrect(canvas, fOuter, fInner);
708     return true;
709 }
710 
SkDrawSpriteCommand(const SkBitmap & bitmap,int left,int top,const SkPaint * paint)711 SkDrawSpriteCommand::SkDrawSpriteCommand(const SkBitmap& bitmap, int left, int top,
712                                          const SkPaint* paint)
713     : INHERITED(kDrawSprite_OpType) {
714     fBitmap = bitmap;
715     fLeft = left;
716     fTop = top;
717     if (paint) {
718         fPaint = *paint;
719         fPaintPtr = &fPaint;
720     } else {
721         fPaintPtr = NULL;
722     }
723 
724     fInfo.push(SkObjectParser::BitmapToString(bitmap));
725     fInfo.push(SkObjectParser::IntToString(left, "Left: "));
726     fInfo.push(SkObjectParser::IntToString(top, "Top: "));
727     if (paint) {
728         fInfo.push(SkObjectParser::PaintToString(*paint));
729     }
730 }
731 
execute(SkCanvas * canvas) const732 void SkDrawSpriteCommand::execute(SkCanvas* canvas) const {
733     canvas->drawSprite(fBitmap, fLeft, fTop, fPaintPtr);
734 }
735 
render(SkCanvas * canvas) const736 bool SkDrawSpriteCommand::render(SkCanvas* canvas) const {
737     render_bitmap(canvas, fBitmap);
738     return true;
739 }
740 
SkDrawTextCommand(const void * text,size_t byteLength,SkScalar x,SkScalar y,const SkPaint & paint)741 SkDrawTextCommand::SkDrawTextCommand(const void* text, size_t byteLength, SkScalar x, SkScalar y,
742                                      const SkPaint& paint)
743     : INHERITED(kDrawText_OpType) {
744     fText = new char[byteLength];
745     memcpy(fText, text, byteLength);
746     fByteLength = byteLength;
747     fX = x;
748     fY = y;
749     fPaint = paint;
750 
751     fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding()));
752     fInfo.push(SkObjectParser::ScalarToString(x, "SkScalar x: "));
753     fInfo.push(SkObjectParser::ScalarToString(y, "SkScalar y: "));
754     fInfo.push(SkObjectParser::PaintToString(paint));
755 }
756 
execute(SkCanvas * canvas) const757 void SkDrawTextCommand::execute(SkCanvas* canvas) const {
758     canvas->drawText(fText, fByteLength, fX, fY, fPaint);
759 }
760 
SkDrawTextOnPathCommand(const void * text,size_t byteLength,const SkPath & path,const SkMatrix * matrix,const SkPaint & paint)761 SkDrawTextOnPathCommand::SkDrawTextOnPathCommand(const void* text, size_t byteLength,
762                                                  const SkPath& path, const SkMatrix* matrix,
763                                                  const SkPaint& paint)
764     : INHERITED(kDrawTextOnPath_OpType) {
765     fText = new char[byteLength];
766     memcpy(fText, text, byteLength);
767     fByteLength = byteLength;
768     fPath = path;
769     if (matrix) {
770         fMatrix = *matrix;
771     } else {
772         fMatrix.setIdentity();
773     }
774     fPaint = paint;
775 
776     fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding()));
777     fInfo.push(SkObjectParser::PathToString(path));
778     if (matrix) {
779         fInfo.push(SkObjectParser::MatrixToString(*matrix));
780     }
781     fInfo.push(SkObjectParser::PaintToString(paint));
782 }
783 
execute(SkCanvas * canvas) const784 void SkDrawTextOnPathCommand::execute(SkCanvas* canvas) const {
785     canvas->drawTextOnPath(fText, fByteLength, fPath,
786                            fMatrix.isIdentity() ? NULL : &fMatrix,
787                            fPaint);
788 }
789 
SkDrawVerticesCommand(SkCanvas::VertexMode vmode,int vertexCount,const SkPoint vertices[],const SkPoint texs[],const SkColor colors[],SkXfermode * xfermode,const uint16_t indices[],int indexCount,const SkPaint & paint)790 SkDrawVerticesCommand::SkDrawVerticesCommand(SkCanvas::VertexMode vmode, int vertexCount,
791                                              const SkPoint vertices[], const SkPoint texs[],
792                                              const SkColor colors[], SkXfermode* xfermode,
793                                              const uint16_t indices[], int indexCount,
794                                              const SkPaint& paint)
795     : INHERITED(kDrawVertices_OpType) {
796     fVmode = vmode;
797 
798     fVertexCount = vertexCount;
799 
800     fVertices = new SkPoint[vertexCount];
801     memcpy(fVertices, vertices, vertexCount * sizeof(SkPoint));
802 
803     if (texs) {
804         fTexs = new SkPoint[vertexCount];
805         memcpy(fTexs, texs, vertexCount * sizeof(SkPoint));
806     } else {
807         fTexs = NULL;
808     }
809 
810     if (colors) {
811         fColors = new SkColor[vertexCount];
812         memcpy(fColors, colors, vertexCount * sizeof(SkColor));
813     } else {
814         fColors = NULL;
815     }
816 
817     fXfermode = xfermode;
818     if (fXfermode) {
819         fXfermode->ref();
820     }
821 
822     if (indexCount > 0) {
823         fIndices = new uint16_t[indexCount];
824         memcpy(fIndices, indices, indexCount * sizeof(uint16_t));
825     } else {
826         fIndices = NULL;
827     }
828 
829     fIndexCount = indexCount;
830     fPaint = paint;
831 
832     // TODO(chudy)
833     fInfo.push(SkObjectParser::CustomTextToString("To be implemented."));
834     fInfo.push(SkObjectParser::PaintToString(paint));
835 }
836 
~SkDrawVerticesCommand()837 SkDrawVerticesCommand::~SkDrawVerticesCommand() {
838     delete [] fVertices;
839     delete [] fTexs;
840     delete [] fColors;
841     SkSafeUnref(fXfermode);
842     delete [] fIndices;
843 }
844 
execute(SkCanvas * canvas) const845 void SkDrawVerticesCommand::execute(SkCanvas* canvas) const {
846     canvas->drawVertices(fVmode, fVertexCount, fVertices,
847                          fTexs, fColors, fXfermode, fIndices,
848                          fIndexCount, fPaint);
849 }
850 
SkRestoreCommand()851 SkRestoreCommand::SkRestoreCommand()
852     : INHERITED(kRestore_OpType) {
853     fInfo.push(SkObjectParser::CustomTextToString("No Parameters"));
854 }
855 
execute(SkCanvas * canvas) const856 void SkRestoreCommand::execute(SkCanvas* canvas) const {
857     canvas->restore();
858 }
859 
SkSaveCommand()860 SkSaveCommand::SkSaveCommand()
861     : INHERITED(kSave_OpType) {
862 }
863 
execute(SkCanvas * canvas) const864 void SkSaveCommand::execute(SkCanvas* canvas) const {
865     canvas->save();
866 }
867 
SkSaveLayerCommand(const SkRect * bounds,const SkPaint * paint,SkCanvas::SaveFlags flags)868 SkSaveLayerCommand::SkSaveLayerCommand(const SkRect* bounds, const SkPaint* paint,
869                                        SkCanvas::SaveFlags flags)
870     : INHERITED(kSaveLayer_OpType) {
871     if (bounds) {
872         fBounds = *bounds;
873     } else {
874         fBounds.setEmpty();
875     }
876 
877     if (paint) {
878         fPaint = *paint;
879         fPaintPtr = &fPaint;
880     } else {
881         fPaintPtr = NULL;
882     }
883     fFlags = flags;
884 
885     if (bounds) {
886         fInfo.push(SkObjectParser::RectToString(*bounds, "Bounds: "));
887     }
888     if (paint) {
889         fInfo.push(SkObjectParser::PaintToString(*paint));
890     }
891     fInfo.push(SkObjectParser::SaveFlagsToString(flags));
892 }
893 
execute(SkCanvas * canvas) const894 void SkSaveLayerCommand::execute(SkCanvas* canvas) const {
895     canvas->saveLayer(fBounds.isEmpty() ? NULL : &fBounds,
896                       fPaintPtr,
897                       fFlags);
898 }
899 
vizExecute(SkCanvas * canvas) const900 void SkSaveLayerCommand::vizExecute(SkCanvas* canvas) const {
901     canvas->save();
902 }
903 
SkSetMatrixCommand(const SkMatrix & matrix)904 SkSetMatrixCommand::SkSetMatrixCommand(const SkMatrix& matrix)
905     : INHERITED(kSetMatrix_OpType) {
906     fUserMatrix.reset();
907     fMatrix = matrix;
908 
909     fInfo.push(SkObjectParser::MatrixToString(matrix));
910 }
911 
setUserMatrix(const SkMatrix & userMatrix)912 void SkSetMatrixCommand::setUserMatrix(const SkMatrix& userMatrix) {
913     fUserMatrix = userMatrix;
914 }
915 
execute(SkCanvas * canvas) const916 void SkSetMatrixCommand::execute(SkCanvas* canvas) const {
917     SkMatrix temp = SkMatrix::Concat(fUserMatrix, fMatrix);
918     canvas->setMatrix(temp);
919 }
920 
921