|
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 |
|
13 // TODO(chudy): Refactor into non subclass model. |
|
14 |
|
15 SkDrawCommand::SkDrawCommand(DrawType type) |
|
16 : fDrawType(type) |
|
17 , fVisible(true) { |
|
18 } |
|
19 |
|
20 SkDrawCommand::SkDrawCommand() { |
|
21 fVisible = true; |
|
22 } |
|
23 |
|
24 SkDrawCommand::~SkDrawCommand() { |
|
25 fInfo.deleteAll(); |
|
26 } |
|
27 |
|
28 const char* SkDrawCommand::GetCommandString(DrawType type) { |
|
29 switch (type) { |
|
30 case UNUSED: SkDEBUGFAIL("DrawType UNUSED\n"); break; |
|
31 case DRAW_CLEAR: return "Clear"; |
|
32 case CLIP_PATH: return "Clip Path"; |
|
33 case CLIP_REGION: return "Clip Region"; |
|
34 case CLIP_RECT: return "Clip Rect"; |
|
35 case CLIP_RRECT: return "Clip RRect"; |
|
36 case CONCAT: return "Concat"; |
|
37 case DRAW_BITMAP: return "Draw Bitmap"; |
|
38 case DRAW_BITMAP_MATRIX: return "Draw Bitmap Matrix"; |
|
39 case DRAW_BITMAP_NINE: return "Draw Bitmap Nine"; |
|
40 case DRAW_BITMAP_RECT_TO_RECT: return "Draw Bitmap Rect"; |
|
41 case DRAW_DATA: return "Draw Data"; |
|
42 case DRAW_OVAL: return "Draw Oval"; |
|
43 case DRAW_PAINT: return "Draw Paint"; |
|
44 case DRAW_PATH: return "Draw Path"; |
|
45 case DRAW_PICTURE: return "Draw Picture"; |
|
46 case DRAW_POINTS: return "Draw Points"; |
|
47 case DRAW_POS_TEXT: return "Draw Pos Text"; |
|
48 case DRAW_POS_TEXT_H: return "Draw Pos Text H"; |
|
49 case DRAW_RECT: return "Draw Rect"; |
|
50 case DRAW_RRECT: return "Draw RRect"; |
|
51 case DRAW_SPRITE: return "Draw Sprite"; |
|
52 case DRAW_TEXT: return "Draw Text"; |
|
53 case DRAW_TEXT_ON_PATH: return "Draw Text On Path"; |
|
54 case DRAW_VERTICES: return "Draw Vertices"; |
|
55 case RESTORE: return "Restore"; |
|
56 case ROTATE: return "Rotate"; |
|
57 case SAVE: return "Save"; |
|
58 case SAVE_LAYER: return "Save Layer"; |
|
59 case SCALE: return "Scale"; |
|
60 case SET_MATRIX: return "Set Matrix"; |
|
61 case SKEW: return "Skew"; |
|
62 case TRANSLATE: return "Translate"; |
|
63 case NOOP: return "NoOp"; |
|
64 case BEGIN_COMMENT_GROUP: return "BeginCommentGroup"; |
|
65 case COMMENT: return "Comment"; |
|
66 case END_COMMENT_GROUP: return "EndCommentGroup"; |
|
67 case DRAW_DRRECT: return "Draw DRRect"; |
|
68 case PUSH_CULL: return "PushCull"; |
|
69 case POP_CULL: return "PopCull"; |
|
70 default: |
|
71 SkDebugf("DrawType error 0x%08x\n", type); |
|
72 SkASSERT(0); |
|
73 break; |
|
74 } |
|
75 SkDEBUGFAIL("DrawType UNUSED\n"); |
|
76 return NULL; |
|
77 } |
|
78 |
|
79 SkString SkDrawCommand::toString() { |
|
80 return SkString(GetCommandString(fDrawType)); |
|
81 } |
|
82 |
|
83 SkClearCommand::SkClearCommand(SkColor color) { |
|
84 fColor = color; |
|
85 fDrawType = DRAW_CLEAR; |
|
86 fInfo.push(SkObjectParser::CustomTextToString("No Parameters")); |
|
87 } |
|
88 |
|
89 void SkClearCommand::execute(SkCanvas* canvas) { |
|
90 canvas->clear(fColor); |
|
91 } |
|
92 |
|
93 namespace { |
|
94 |
|
95 void xlate_and_scale_to_bounds(SkCanvas* canvas, const SkRect& bounds) { |
|
96 const SkISize& size = canvas->getDeviceSize(); |
|
97 |
|
98 static const SkScalar kInsetFrac = 0.9f; // Leave a border around object |
|
99 |
|
100 canvas->translate(size.fWidth/2.0f, size.fHeight/2.0f); |
|
101 if (bounds.width() > bounds.height()) { |
|
102 canvas->scale(SkDoubleToScalar((kInsetFrac*size.fWidth)/bounds.width()), |
|
103 SkDoubleToScalar((kInsetFrac*size.fHeight)/bounds.width())); |
|
104 } else { |
|
105 canvas->scale(SkDoubleToScalar((kInsetFrac*size.fWidth)/bounds.height()), |
|
106 SkDoubleToScalar((kInsetFrac*size.fHeight)/bounds.height())); |
|
107 } |
|
108 canvas->translate(-bounds.centerX(), -bounds.centerY()); |
|
109 } |
|
110 |
|
111 |
|
112 void render_path(SkCanvas* canvas, const SkPath& path) { |
|
113 canvas->clear(0xFFFFFFFF); |
|
114 canvas->save(); |
|
115 |
|
116 const SkRect& bounds = path.getBounds(); |
|
117 |
|
118 xlate_and_scale_to_bounds(canvas, bounds); |
|
119 |
|
120 SkPaint p; |
|
121 p.setColor(SK_ColorBLACK); |
|
122 p.setStyle(SkPaint::kStroke_Style); |
|
123 |
|
124 canvas->drawPath(path, p); |
|
125 canvas->restore(); |
|
126 } |
|
127 |
|
128 void render_bitmap(SkCanvas* canvas, const SkBitmap& input, const SkRect* srcRect = NULL) { |
|
129 const SkISize& size = canvas->getDeviceSize(); |
|
130 |
|
131 SkScalar xScale = SkIntToScalar(size.fWidth-2) / input.width(); |
|
132 SkScalar yScale = SkIntToScalar(size.fHeight-2) / input.height(); |
|
133 |
|
134 if (input.width() > input.height()) { |
|
135 yScale *= input.height() / (float) input.width(); |
|
136 } else { |
|
137 xScale *= input.width() / (float) input.height(); |
|
138 } |
|
139 |
|
140 SkRect dst = SkRect::MakeXYWH(SK_Scalar1, SK_Scalar1, |
|
141 xScale * input.width(), |
|
142 yScale * input.height()); |
|
143 |
|
144 canvas->clear(0xFFFFFFFF); |
|
145 canvas->drawBitmapRect(input, NULL, dst); |
|
146 |
|
147 if (NULL != srcRect) { |
|
148 SkRect r = SkRect::MakeLTRB(srcRect->fLeft * xScale + SK_Scalar1, |
|
149 srcRect->fTop * yScale + SK_Scalar1, |
|
150 srcRect->fRight * xScale + SK_Scalar1, |
|
151 srcRect->fBottom * yScale + SK_Scalar1); |
|
152 SkPaint p; |
|
153 p.setColor(SK_ColorRED); |
|
154 p.setStyle(SkPaint::kStroke_Style); |
|
155 |
|
156 canvas->drawRect(r, p); |
|
157 } |
|
158 } |
|
159 |
|
160 void render_rrect(SkCanvas* canvas, const SkRRect& rrect) { |
|
161 canvas->clear(0xFFFFFFFF); |
|
162 canvas->save(); |
|
163 |
|
164 const SkRect& bounds = rrect.getBounds(); |
|
165 |
|
166 xlate_and_scale_to_bounds(canvas, bounds); |
|
167 |
|
168 SkPaint p; |
|
169 p.setColor(SK_ColorBLACK); |
|
170 p.setStyle(SkPaint::kStroke_Style); |
|
171 |
|
172 canvas->drawRRect(rrect, p); |
|
173 canvas->restore(); |
|
174 } |
|
175 |
|
176 void render_drrect(SkCanvas* canvas, const SkRRect& outer, const SkRRect& inner) { |
|
177 canvas->clear(0xFFFFFFFF); |
|
178 canvas->save(); |
|
179 |
|
180 const SkRect& bounds = outer.getBounds(); |
|
181 |
|
182 xlate_and_scale_to_bounds(canvas, bounds); |
|
183 |
|
184 SkPaint p; |
|
185 p.setColor(SK_ColorBLACK); |
|
186 p.setStyle(SkPaint::kStroke_Style); |
|
187 |
|
188 canvas->drawDRRect(outer, inner, p); |
|
189 canvas->restore(); |
|
190 } |
|
191 |
|
192 }; |
|
193 |
|
194 |
|
195 SkClipPathCommand::SkClipPathCommand(const SkPath& path, SkRegion::Op op, bool doAA) { |
|
196 fPath = path; |
|
197 fOp = op; |
|
198 fDoAA = doAA; |
|
199 fDrawType = CLIP_PATH; |
|
200 |
|
201 fInfo.push(SkObjectParser::PathToString(path)); |
|
202 fInfo.push(SkObjectParser::RegionOpToString(op)); |
|
203 fInfo.push(SkObjectParser::BoolToString(doAA)); |
|
204 } |
|
205 |
|
206 void SkClipPathCommand::execute(SkCanvas* canvas) { |
|
207 canvas->clipPath(fPath, fOp, fDoAA); |
|
208 } |
|
209 |
|
210 bool SkClipPathCommand::render(SkCanvas* canvas) const { |
|
211 render_path(canvas, fPath); |
|
212 return true; |
|
213 } |
|
214 |
|
215 SkClipRegionCommand::SkClipRegionCommand(const SkRegion& region, SkRegion::Op op) { |
|
216 fRegion = region; |
|
217 fOp = op; |
|
218 fDrawType = CLIP_REGION; |
|
219 |
|
220 fInfo.push(SkObjectParser::RegionToString(region)); |
|
221 fInfo.push(SkObjectParser::RegionOpToString(op)); |
|
222 } |
|
223 |
|
224 void SkClipRegionCommand::execute(SkCanvas* canvas) { |
|
225 canvas->clipRegion(fRegion, fOp); |
|
226 } |
|
227 |
|
228 SkClipRectCommand::SkClipRectCommand(const SkRect& rect, SkRegion::Op op, bool doAA) { |
|
229 fRect = rect; |
|
230 fOp = op; |
|
231 fDoAA = doAA; |
|
232 fDrawType = CLIP_RECT; |
|
233 |
|
234 fInfo.push(SkObjectParser::RectToString(rect)); |
|
235 fInfo.push(SkObjectParser::RegionOpToString(op)); |
|
236 fInfo.push(SkObjectParser::BoolToString(doAA)); |
|
237 } |
|
238 |
|
239 void SkClipRectCommand::execute(SkCanvas* canvas) { |
|
240 canvas->clipRect(fRect, fOp, fDoAA); |
|
241 } |
|
242 |
|
243 SkClipRRectCommand::SkClipRRectCommand(const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
|
244 fRRect = rrect; |
|
245 fOp = op; |
|
246 fDoAA = doAA; |
|
247 fDrawType = CLIP_RRECT; |
|
248 |
|
249 fInfo.push(SkObjectParser::RRectToString(rrect)); |
|
250 fInfo.push(SkObjectParser::RegionOpToString(op)); |
|
251 fInfo.push(SkObjectParser::BoolToString(doAA)); |
|
252 } |
|
253 |
|
254 void SkClipRRectCommand::execute(SkCanvas* canvas) { |
|
255 canvas->clipRRect(fRRect, fOp, fDoAA); |
|
256 } |
|
257 |
|
258 bool SkClipRRectCommand::render(SkCanvas* canvas) const { |
|
259 render_rrect(canvas, fRRect); |
|
260 return true; |
|
261 } |
|
262 |
|
263 SkConcatCommand::SkConcatCommand(const SkMatrix& matrix) { |
|
264 fMatrix = matrix; |
|
265 fDrawType = CONCAT; |
|
266 |
|
267 fInfo.push(SkObjectParser::MatrixToString(matrix)); |
|
268 } |
|
269 |
|
270 void SkConcatCommand::execute(SkCanvas* canvas) { |
|
271 canvas->concat(fMatrix); |
|
272 } |
|
273 |
|
274 SkDrawBitmapCommand::SkDrawBitmapCommand(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
|
275 const SkPaint* paint) { |
|
276 fBitmap = bitmap; |
|
277 fLeft = left; |
|
278 fTop = top; |
|
279 if (NULL != paint) { |
|
280 fPaint = *paint; |
|
281 fPaintPtr = &fPaint; |
|
282 } else { |
|
283 fPaintPtr = NULL; |
|
284 } |
|
285 fDrawType = DRAW_BITMAP; |
|
286 |
|
287 fInfo.push(SkObjectParser::BitmapToString(bitmap)); |
|
288 fInfo.push(SkObjectParser::ScalarToString(left, "SkScalar left: ")); |
|
289 fInfo.push(SkObjectParser::ScalarToString(top, "SkScalar top: ")); |
|
290 if (NULL != paint) { |
|
291 fInfo.push(SkObjectParser::PaintToString(*paint)); |
|
292 } |
|
293 } |
|
294 |
|
295 void SkDrawBitmapCommand::execute(SkCanvas* canvas) { |
|
296 canvas->drawBitmap(fBitmap, fLeft, fTop, fPaintPtr); |
|
297 } |
|
298 |
|
299 bool SkDrawBitmapCommand::render(SkCanvas* canvas) const { |
|
300 render_bitmap(canvas, fBitmap); |
|
301 return true; |
|
302 } |
|
303 |
|
304 SkDrawBitmapMatrixCommand::SkDrawBitmapMatrixCommand(const SkBitmap& bitmap, |
|
305 const SkMatrix& matrix, |
|
306 const SkPaint* paint) { |
|
307 fBitmap = bitmap; |
|
308 fMatrix = matrix; |
|
309 if (NULL != paint) { |
|
310 fPaint = *paint; |
|
311 fPaintPtr = &fPaint; |
|
312 } else { |
|
313 fPaintPtr = NULL; |
|
314 } |
|
315 fDrawType = DRAW_BITMAP_MATRIX; |
|
316 |
|
317 fInfo.push(SkObjectParser::BitmapToString(bitmap)); |
|
318 fInfo.push(SkObjectParser::MatrixToString(matrix)); |
|
319 if (NULL != paint) { |
|
320 fInfo.push(SkObjectParser::PaintToString(*paint)); |
|
321 } |
|
322 } |
|
323 |
|
324 void SkDrawBitmapMatrixCommand::execute(SkCanvas* canvas) { |
|
325 canvas->drawBitmapMatrix(fBitmap, fMatrix, fPaintPtr); |
|
326 } |
|
327 |
|
328 bool SkDrawBitmapMatrixCommand::render(SkCanvas* canvas) const { |
|
329 render_bitmap(canvas, fBitmap); |
|
330 return true; |
|
331 } |
|
332 |
|
333 SkDrawBitmapNineCommand::SkDrawBitmapNineCommand(const SkBitmap& bitmap, const SkIRect& center, |
|
334 const SkRect& dst, const SkPaint* paint) { |
|
335 fBitmap = bitmap; |
|
336 fCenter = center; |
|
337 fDst = dst; |
|
338 if (NULL != paint) { |
|
339 fPaint = *paint; |
|
340 fPaintPtr = &fPaint; |
|
341 } else { |
|
342 fPaintPtr = NULL; |
|
343 } |
|
344 fDrawType = DRAW_BITMAP_NINE; |
|
345 |
|
346 fInfo.push(SkObjectParser::BitmapToString(bitmap)); |
|
347 fInfo.push(SkObjectParser::IRectToString(center)); |
|
348 fInfo.push(SkObjectParser::RectToString(dst, "Dst: ")); |
|
349 if (NULL != paint) { |
|
350 fInfo.push(SkObjectParser::PaintToString(*paint)); |
|
351 } |
|
352 } |
|
353 |
|
354 void SkDrawBitmapNineCommand::execute(SkCanvas* canvas) { |
|
355 canvas->drawBitmapNine(fBitmap, fCenter, fDst, fPaintPtr); |
|
356 } |
|
357 |
|
358 bool SkDrawBitmapNineCommand::render(SkCanvas* canvas) const { |
|
359 render_bitmap(canvas, fBitmap); |
|
360 return true; |
|
361 } |
|
362 |
|
363 SkDrawBitmapRectCommand::SkDrawBitmapRectCommand(const SkBitmap& bitmap, const SkRect* src, |
|
364 const SkRect& dst, const SkPaint* paint, |
|
365 SkCanvas::DrawBitmapRectFlags flags) { |
|
366 fBitmap = bitmap; |
|
367 if (NULL != src) { |
|
368 fSrc = *src; |
|
369 } else { |
|
370 fSrc.setEmpty(); |
|
371 } |
|
372 fDst = dst; |
|
373 |
|
374 if (NULL != paint) { |
|
375 fPaint = *paint; |
|
376 fPaintPtr = &fPaint; |
|
377 } else { |
|
378 fPaintPtr = NULL; |
|
379 } |
|
380 fFlags = flags; |
|
381 |
|
382 fDrawType = DRAW_BITMAP_RECT_TO_RECT; |
|
383 |
|
384 fInfo.push(SkObjectParser::BitmapToString(bitmap)); |
|
385 if (NULL != src) { |
|
386 fInfo.push(SkObjectParser::RectToString(*src, "Src: ")); |
|
387 } |
|
388 fInfo.push(SkObjectParser::RectToString(dst, "Dst: ")); |
|
389 if (NULL != paint) { |
|
390 fInfo.push(SkObjectParser::PaintToString(*paint)); |
|
391 } |
|
392 fInfo.push(SkObjectParser::IntToString(fFlags, "Flags: ")); |
|
393 } |
|
394 |
|
395 void SkDrawBitmapRectCommand::execute(SkCanvas* canvas) { |
|
396 canvas->drawBitmapRectToRect(fBitmap, this->srcRect(), fDst, fPaintPtr, fFlags); |
|
397 } |
|
398 |
|
399 bool SkDrawBitmapRectCommand::render(SkCanvas* canvas) const { |
|
400 render_bitmap(canvas, fBitmap, this->srcRect()); |
|
401 return true; |
|
402 } |
|
403 |
|
404 SkDrawDataCommand::SkDrawDataCommand(const void* data, size_t length) { |
|
405 fData = new char[length]; |
|
406 memcpy(fData, data, length); |
|
407 fLength = length; |
|
408 fDrawType = DRAW_DATA; |
|
409 |
|
410 // TODO: add display of actual data? |
|
411 SkString* str = new SkString; |
|
412 str->appendf("length: %d", (int) length); |
|
413 fInfo.push(str); |
|
414 } |
|
415 |
|
416 void SkDrawDataCommand::execute(SkCanvas* canvas) { |
|
417 canvas->drawData(fData, fLength); |
|
418 } |
|
419 |
|
420 SkBeginCommentGroupCommand::SkBeginCommentGroupCommand(const char* description) |
|
421 : INHERITED(BEGIN_COMMENT_GROUP) |
|
422 , fDescription(description) { |
|
423 SkString* temp = new SkString; |
|
424 temp->appendf("Description: %s", description); |
|
425 fInfo.push(temp); |
|
426 } |
|
427 |
|
428 SkCommentCommand::SkCommentCommand(const char* kywd, const char* value) |
|
429 : INHERITED(COMMENT) |
|
430 , fKywd(kywd) |
|
431 , fValue(value) { |
|
432 SkString* temp = new SkString; |
|
433 temp->appendf("%s: %s", kywd, value); |
|
434 fInfo.push(temp); |
|
435 } |
|
436 |
|
437 SkEndCommentGroupCommand::SkEndCommentGroupCommand() : INHERITED(END_COMMENT_GROUP) { |
|
438 } |
|
439 |
|
440 SkDrawOvalCommand::SkDrawOvalCommand(const SkRect& oval, const SkPaint& paint) { |
|
441 fOval = oval; |
|
442 fPaint = paint; |
|
443 fDrawType = DRAW_OVAL; |
|
444 |
|
445 fInfo.push(SkObjectParser::RectToString(oval)); |
|
446 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
447 } |
|
448 |
|
449 void SkDrawOvalCommand::execute(SkCanvas* canvas) { |
|
450 canvas->drawOval(fOval, fPaint); |
|
451 } |
|
452 |
|
453 bool SkDrawOvalCommand::render(SkCanvas* canvas) const { |
|
454 canvas->clear(0xFFFFFFFF); |
|
455 canvas->save(); |
|
456 |
|
457 xlate_and_scale_to_bounds(canvas, fOval); |
|
458 |
|
459 SkPaint p; |
|
460 p.setColor(SK_ColorBLACK); |
|
461 p.setStyle(SkPaint::kStroke_Style); |
|
462 |
|
463 canvas->drawOval(fOval, p); |
|
464 canvas->restore(); |
|
465 |
|
466 return true; |
|
467 } |
|
468 |
|
469 SkDrawPaintCommand::SkDrawPaintCommand(const SkPaint& paint) { |
|
470 fPaint = paint; |
|
471 fDrawType = DRAW_PAINT; |
|
472 |
|
473 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
474 } |
|
475 |
|
476 void SkDrawPaintCommand::execute(SkCanvas* canvas) { |
|
477 canvas->drawPaint(fPaint); |
|
478 } |
|
479 |
|
480 bool SkDrawPaintCommand::render(SkCanvas* canvas) const { |
|
481 canvas->clear(0xFFFFFFFF); |
|
482 canvas->drawPaint(fPaint); |
|
483 return true; |
|
484 } |
|
485 |
|
486 SkDrawPathCommand::SkDrawPathCommand(const SkPath& path, const SkPaint& paint) { |
|
487 fPath = path; |
|
488 fPaint = paint; |
|
489 fDrawType = DRAW_PATH; |
|
490 |
|
491 fInfo.push(SkObjectParser::PathToString(path)); |
|
492 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
493 } |
|
494 |
|
495 void SkDrawPathCommand::execute(SkCanvas* canvas) { |
|
496 canvas->drawPath(fPath, fPaint); |
|
497 } |
|
498 |
|
499 bool SkDrawPathCommand::render(SkCanvas* canvas) const { |
|
500 render_path(canvas, fPath); |
|
501 return true; |
|
502 } |
|
503 |
|
504 SkDrawPictureCommand::SkDrawPictureCommand(SkPicture& picture) : |
|
505 fPicture(picture) { |
|
506 fDrawType = DRAW_PICTURE; |
|
507 SkString* temp = new SkString; |
|
508 temp->appendf("SkPicture: W: %d H: %d", picture.width(), picture.height()); |
|
509 fInfo.push(temp); |
|
510 } |
|
511 |
|
512 void SkDrawPictureCommand::execute(SkCanvas* canvas) { |
|
513 canvas->drawPicture(fPicture); |
|
514 } |
|
515 |
|
516 bool SkDrawPictureCommand::render(SkCanvas* canvas) const { |
|
517 canvas->clear(0xFFFFFFFF); |
|
518 canvas->save(); |
|
519 |
|
520 SkRect bounds = SkRect::MakeWH(SkIntToScalar(fPicture.width()), |
|
521 SkIntToScalar(fPicture.height())); |
|
522 xlate_and_scale_to_bounds(canvas, bounds); |
|
523 |
|
524 canvas->drawPicture(const_cast<SkPicture&>(fPicture)); |
|
525 |
|
526 canvas->restore(); |
|
527 |
|
528 return true; |
|
529 } |
|
530 |
|
531 SkDrawPointsCommand::SkDrawPointsCommand(SkCanvas::PointMode mode, size_t count, |
|
532 const SkPoint pts[], const SkPaint& paint) { |
|
533 fMode = mode; |
|
534 fCount = count; |
|
535 fPts = new SkPoint[count]; |
|
536 memcpy(fPts, pts, count * sizeof(SkPoint)); |
|
537 fPaint = paint; |
|
538 fDrawType = DRAW_POINTS; |
|
539 |
|
540 fInfo.push(SkObjectParser::PointsToString(pts, count)); |
|
541 fInfo.push(SkObjectParser::ScalarToString(SkIntToScalar((unsigned int)count), |
|
542 "Points: ")); |
|
543 fInfo.push(SkObjectParser::PointModeToString(mode)); |
|
544 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
545 } |
|
546 |
|
547 void SkDrawPointsCommand::execute(SkCanvas* canvas) { |
|
548 canvas->drawPoints(fMode, fCount, fPts, fPaint); |
|
549 } |
|
550 |
|
551 bool SkDrawPointsCommand::render(SkCanvas* canvas) const { |
|
552 canvas->clear(0xFFFFFFFF); |
|
553 canvas->save(); |
|
554 |
|
555 SkRect bounds; |
|
556 |
|
557 bounds.setEmpty(); |
|
558 for (unsigned int i = 0; i < fCount; ++i) { |
|
559 bounds.growToInclude(fPts[i].fX, fPts[i].fY); |
|
560 } |
|
561 |
|
562 xlate_and_scale_to_bounds(canvas, bounds); |
|
563 |
|
564 SkPaint p; |
|
565 p.setColor(SK_ColorBLACK); |
|
566 p.setStyle(SkPaint::kStroke_Style); |
|
567 |
|
568 canvas->drawPoints(fMode, fCount, fPts, p); |
|
569 canvas->restore(); |
|
570 |
|
571 return true; |
|
572 } |
|
573 |
|
574 SkDrawPosTextCommand::SkDrawPosTextCommand(const void* text, size_t byteLength, |
|
575 const SkPoint pos[], const SkPaint& paint) { |
|
576 size_t numPts = paint.countText(text, byteLength); |
|
577 |
|
578 fText = new char[byteLength]; |
|
579 memcpy(fText, text, byteLength); |
|
580 fByteLength = byteLength; |
|
581 |
|
582 fPos = new SkPoint[numPts]; |
|
583 memcpy(fPos, pos, numPts * sizeof(SkPoint)); |
|
584 |
|
585 fPaint = paint; |
|
586 fDrawType = DRAW_POS_TEXT; |
|
587 |
|
588 fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding())); |
|
589 // TODO(chudy): Test that this works. |
|
590 fInfo.push(SkObjectParser::PointsToString(pos, 1)); |
|
591 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
592 } |
|
593 |
|
594 void SkDrawPosTextCommand::execute(SkCanvas* canvas) { |
|
595 canvas->drawPosText(fText, fByteLength, fPos, fPaint); |
|
596 } |
|
597 |
|
598 |
|
599 SkDrawPosTextHCommand::SkDrawPosTextHCommand(const void* text, size_t byteLength, |
|
600 const SkScalar xpos[], SkScalar constY, |
|
601 const SkPaint& paint) { |
|
602 size_t numPts = paint.countText(text, byteLength); |
|
603 |
|
604 fText = new char[byteLength]; |
|
605 memcpy(fText, text, byteLength); |
|
606 fByteLength = byteLength; |
|
607 |
|
608 fXpos = new SkScalar[numPts]; |
|
609 memcpy(fXpos, xpos, numPts * sizeof(SkScalar)); |
|
610 |
|
611 fConstY = constY; |
|
612 fPaint = paint; |
|
613 fDrawType = DRAW_POS_TEXT_H; |
|
614 |
|
615 fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding())); |
|
616 fInfo.push(SkObjectParser::ScalarToString(xpos[0], "XPOS: ")); |
|
617 fInfo.push(SkObjectParser::ScalarToString(constY, "SkScalar constY: ")); |
|
618 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
619 } |
|
620 |
|
621 void SkDrawPosTextHCommand::execute(SkCanvas* canvas) { |
|
622 canvas->drawPosTextH(fText, fByteLength, fXpos, fConstY, fPaint); |
|
623 } |
|
624 |
|
625 SkDrawRectCommand::SkDrawRectCommand(const SkRect& rect, const SkPaint& paint) { |
|
626 fRect = rect; |
|
627 fPaint = paint; |
|
628 fDrawType = DRAW_RECT; |
|
629 |
|
630 fInfo.push(SkObjectParser::RectToString(rect)); |
|
631 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
632 } |
|
633 |
|
634 void SkDrawRectCommand::execute(SkCanvas* canvas) { |
|
635 canvas->drawRect(fRect, fPaint); |
|
636 } |
|
637 |
|
638 SkDrawRRectCommand::SkDrawRRectCommand(const SkRRect& rrect, const SkPaint& paint) { |
|
639 fRRect = rrect; |
|
640 fPaint = paint; |
|
641 fDrawType = DRAW_RRECT; |
|
642 |
|
643 fInfo.push(SkObjectParser::RRectToString(rrect)); |
|
644 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
645 } |
|
646 |
|
647 void SkDrawRRectCommand::execute(SkCanvas* canvas) { |
|
648 canvas->drawRRect(fRRect, fPaint); |
|
649 } |
|
650 |
|
651 bool SkDrawRRectCommand::render(SkCanvas* canvas) const { |
|
652 render_rrect(canvas, fRRect); |
|
653 return true; |
|
654 } |
|
655 |
|
656 SkDrawDRRectCommand::SkDrawDRRectCommand(const SkRRect& outer, |
|
657 const SkRRect& inner, |
|
658 const SkPaint& paint) { |
|
659 fOuter = outer; |
|
660 fInner = inner; |
|
661 fPaint = paint; |
|
662 fDrawType = DRAW_DRRECT; |
|
663 |
|
664 fInfo.push(SkObjectParser::RRectToString(outer)); |
|
665 fInfo.push(SkObjectParser::RRectToString(inner)); |
|
666 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
667 } |
|
668 |
|
669 void SkDrawDRRectCommand::execute(SkCanvas* canvas) { |
|
670 canvas->drawDRRect(fOuter, fInner, fPaint); |
|
671 } |
|
672 |
|
673 bool SkDrawDRRectCommand::render(SkCanvas* canvas) const { |
|
674 render_drrect(canvas, fOuter, fInner); |
|
675 return true; |
|
676 } |
|
677 |
|
678 SkDrawSpriteCommand::SkDrawSpriteCommand(const SkBitmap& bitmap, int left, int top, |
|
679 const SkPaint* paint) { |
|
680 fBitmap = bitmap; |
|
681 fLeft = left; |
|
682 fTop = top; |
|
683 if (NULL != paint) { |
|
684 fPaint = *paint; |
|
685 fPaintPtr = &fPaint; |
|
686 } else { |
|
687 fPaintPtr = NULL; |
|
688 } |
|
689 fDrawType = DRAW_SPRITE; |
|
690 |
|
691 fInfo.push(SkObjectParser::BitmapToString(bitmap)); |
|
692 fInfo.push(SkObjectParser::IntToString(left, "Left: ")); |
|
693 fInfo.push(SkObjectParser::IntToString(top, "Top: ")); |
|
694 if (NULL != paint) { |
|
695 fInfo.push(SkObjectParser::PaintToString(*paint)); |
|
696 } |
|
697 } |
|
698 |
|
699 void SkDrawSpriteCommand::execute(SkCanvas* canvas) { |
|
700 canvas->drawSprite(fBitmap, fLeft, fTop, fPaintPtr); |
|
701 } |
|
702 |
|
703 bool SkDrawSpriteCommand::render(SkCanvas* canvas) const { |
|
704 render_bitmap(canvas, fBitmap); |
|
705 return true; |
|
706 } |
|
707 |
|
708 SkDrawTextCommand::SkDrawTextCommand(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
|
709 const SkPaint& paint) { |
|
710 fText = new char[byteLength]; |
|
711 memcpy(fText, text, byteLength); |
|
712 fByteLength = byteLength; |
|
713 fX = x; |
|
714 fY = y; |
|
715 fPaint = paint; |
|
716 fDrawType = DRAW_TEXT; |
|
717 |
|
718 fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding())); |
|
719 fInfo.push(SkObjectParser::ScalarToString(x, "SkScalar x: ")); |
|
720 fInfo.push(SkObjectParser::ScalarToString(y, "SkScalar y: ")); |
|
721 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
722 } |
|
723 |
|
724 void SkDrawTextCommand::execute(SkCanvas* canvas) { |
|
725 canvas->drawText(fText, fByteLength, fX, fY, fPaint); |
|
726 } |
|
727 |
|
728 SkDrawTextOnPathCommand::SkDrawTextOnPathCommand(const void* text, size_t byteLength, |
|
729 const SkPath& path, const SkMatrix* matrix, |
|
730 const SkPaint& paint) { |
|
731 fText = new char[byteLength]; |
|
732 memcpy(fText, text, byteLength); |
|
733 fByteLength = byteLength; |
|
734 fPath = path; |
|
735 if (NULL != matrix) { |
|
736 fMatrix = *matrix; |
|
737 } else { |
|
738 fMatrix.setIdentity(); |
|
739 } |
|
740 fPaint = paint; |
|
741 fDrawType = DRAW_TEXT_ON_PATH; |
|
742 |
|
743 fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding())); |
|
744 fInfo.push(SkObjectParser::PathToString(path)); |
|
745 if (NULL != matrix) { |
|
746 fInfo.push(SkObjectParser::MatrixToString(*matrix)); |
|
747 } |
|
748 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
749 } |
|
750 |
|
751 void SkDrawTextOnPathCommand::execute(SkCanvas* canvas) { |
|
752 canvas->drawTextOnPath(fText, fByteLength, fPath, |
|
753 fMatrix.isIdentity() ? NULL : &fMatrix, |
|
754 fPaint); |
|
755 } |
|
756 |
|
757 SkDrawVerticesCommand::SkDrawVerticesCommand(SkCanvas::VertexMode vmode, int vertexCount, |
|
758 const SkPoint vertices[], const SkPoint texs[], |
|
759 const SkColor colors[], SkXfermode* xfermode, |
|
760 const uint16_t indices[], int indexCount, |
|
761 const SkPaint& paint) { |
|
762 fVmode = vmode; |
|
763 |
|
764 fVertexCount = vertexCount; |
|
765 |
|
766 fVertices = new SkPoint[vertexCount]; |
|
767 memcpy(fVertices, vertices, vertexCount * sizeof(SkPoint)); |
|
768 |
|
769 if (NULL != texs) { |
|
770 fTexs = new SkPoint[vertexCount]; |
|
771 memcpy(fTexs, texs, vertexCount * sizeof(SkPoint)); |
|
772 } else { |
|
773 fTexs = NULL; |
|
774 } |
|
775 |
|
776 if (NULL != colors) { |
|
777 fColors = new SkColor[vertexCount]; |
|
778 memcpy(fColors, colors, vertexCount * sizeof(SkColor)); |
|
779 } else { |
|
780 fColors = NULL; |
|
781 } |
|
782 |
|
783 fXfermode = xfermode; |
|
784 if (NULL != fXfermode) { |
|
785 fXfermode->ref(); |
|
786 } |
|
787 |
|
788 if (indexCount > 0) { |
|
789 fIndices = new uint16_t[indexCount]; |
|
790 memcpy(fIndices, indices, indexCount * sizeof(uint16_t)); |
|
791 } else { |
|
792 fIndices = NULL; |
|
793 } |
|
794 |
|
795 fIndexCount = indexCount; |
|
796 fPaint = paint; |
|
797 fDrawType = DRAW_VERTICES; |
|
798 |
|
799 // TODO(chudy) |
|
800 fInfo.push(SkObjectParser::CustomTextToString("To be implemented.")); |
|
801 fInfo.push(SkObjectParser::PaintToString(paint)); |
|
802 } |
|
803 |
|
804 SkDrawVerticesCommand::~SkDrawVerticesCommand() { |
|
805 delete [] fVertices; |
|
806 delete [] fTexs; |
|
807 delete [] fColors; |
|
808 SkSafeUnref(fXfermode); |
|
809 delete [] fIndices; |
|
810 } |
|
811 |
|
812 void SkDrawVerticesCommand::execute(SkCanvas* canvas) { |
|
813 canvas->drawVertices(fVmode, fVertexCount, fVertices, |
|
814 fTexs, fColors, fXfermode, fIndices, |
|
815 fIndexCount, fPaint); |
|
816 } |
|
817 |
|
818 SkRestoreCommand::SkRestoreCommand() { |
|
819 fDrawType = RESTORE; |
|
820 fInfo.push(SkObjectParser::CustomTextToString("No Parameters")); |
|
821 } |
|
822 |
|
823 void SkRestoreCommand::execute(SkCanvas* canvas) { |
|
824 canvas->restore(); |
|
825 } |
|
826 |
|
827 void SkRestoreCommand::trackSaveState(int* state) { |
|
828 (*state)--; |
|
829 } |
|
830 |
|
831 SkRotateCommand::SkRotateCommand(SkScalar degrees) { |
|
832 fDegrees = degrees; |
|
833 fDrawType = ROTATE; |
|
834 |
|
835 fInfo.push(SkObjectParser::ScalarToString(degrees, "SkScalar degrees: ")); |
|
836 } |
|
837 |
|
838 void SkRotateCommand::execute(SkCanvas* canvas) { |
|
839 canvas->rotate(fDegrees); |
|
840 } |
|
841 |
|
842 SkSaveCommand::SkSaveCommand(SkCanvas::SaveFlags flags) { |
|
843 fFlags = flags; |
|
844 fDrawType = SAVE; |
|
845 fInfo.push(SkObjectParser::SaveFlagsToString(flags)); |
|
846 } |
|
847 |
|
848 void SkSaveCommand::execute(SkCanvas* canvas) { |
|
849 canvas->save(fFlags); |
|
850 } |
|
851 |
|
852 void SkSaveCommand::trackSaveState(int* state) { |
|
853 (*state)++; |
|
854 } |
|
855 |
|
856 SkSaveLayerCommand::SkSaveLayerCommand(const SkRect* bounds, const SkPaint* paint, |
|
857 SkCanvas::SaveFlags flags) { |
|
858 if (NULL != bounds) { |
|
859 fBounds = *bounds; |
|
860 } else { |
|
861 fBounds.setEmpty(); |
|
862 } |
|
863 |
|
864 if (NULL != paint) { |
|
865 fPaint = *paint; |
|
866 fPaintPtr = &fPaint; |
|
867 } else { |
|
868 fPaintPtr = NULL; |
|
869 } |
|
870 fFlags = flags; |
|
871 fDrawType = SAVE_LAYER; |
|
872 |
|
873 if (NULL != bounds) { |
|
874 fInfo.push(SkObjectParser::RectToString(*bounds, "Bounds: ")); |
|
875 } |
|
876 if (NULL != paint) { |
|
877 fInfo.push(SkObjectParser::PaintToString(*paint)); |
|
878 } |
|
879 fInfo.push(SkObjectParser::SaveFlagsToString(flags)); |
|
880 } |
|
881 |
|
882 void SkSaveLayerCommand::execute(SkCanvas* canvas) { |
|
883 canvas->saveLayer(fBounds.isEmpty() ? NULL : &fBounds, |
|
884 fPaintPtr, |
|
885 fFlags); |
|
886 } |
|
887 |
|
888 void SkSaveLayerCommand::vizExecute(SkCanvas* canvas) { |
|
889 canvas->save(); |
|
890 } |
|
891 |
|
892 void SkSaveLayerCommand::trackSaveState(int* state) { |
|
893 (*state)++; |
|
894 } |
|
895 |
|
896 SkScaleCommand::SkScaleCommand(SkScalar sx, SkScalar sy) { |
|
897 fSx = sx; |
|
898 fSy = sy; |
|
899 fDrawType = SCALE; |
|
900 |
|
901 fInfo.push(SkObjectParser::ScalarToString(sx, "SkScalar sx: ")); |
|
902 fInfo.push(SkObjectParser::ScalarToString(sy, "SkScalar sy: ")); |
|
903 } |
|
904 |
|
905 void SkScaleCommand::execute(SkCanvas* canvas) { |
|
906 canvas->scale(fSx, fSy); |
|
907 } |
|
908 |
|
909 SkSetMatrixCommand::SkSetMatrixCommand(const SkMatrix& matrix) { |
|
910 fMatrix = matrix; |
|
911 fDrawType = SET_MATRIX; |
|
912 |
|
913 fInfo.push(SkObjectParser::MatrixToString(matrix)); |
|
914 } |
|
915 |
|
916 void SkSetMatrixCommand::execute(SkCanvas* canvas) { |
|
917 canvas->setMatrix(fMatrix); |
|
918 } |
|
919 |
|
920 SkSkewCommand::SkSkewCommand(SkScalar sx, SkScalar sy) { |
|
921 fSx = sx; |
|
922 fSy = sy; |
|
923 fDrawType = SKEW; |
|
924 |
|
925 fInfo.push(SkObjectParser::ScalarToString(sx, "SkScalar sx: ")); |
|
926 fInfo.push(SkObjectParser::ScalarToString(sy, "SkScalar sy: ")); |
|
927 } |
|
928 |
|
929 void SkSkewCommand::execute(SkCanvas* canvas) { |
|
930 canvas->skew(fSx, fSy); |
|
931 } |
|
932 |
|
933 SkTranslateCommand::SkTranslateCommand(SkScalar dx, SkScalar dy) { |
|
934 fDx = dx; |
|
935 fDy = dy; |
|
936 fDrawType = TRANSLATE; |
|
937 |
|
938 fInfo.push(SkObjectParser::ScalarToString(dx, "SkScalar dx: ")); |
|
939 fInfo.push(SkObjectParser::ScalarToString(dy, "SkScalar dy: ")); |
|
940 } |
|
941 |
|
942 void SkTranslateCommand::execute(SkCanvas* canvas) { |
|
943 canvas->translate(fDx, fDy); |
|
944 } |
|
945 |
|
946 SkPushCullCommand::SkPushCullCommand(const SkRect& cullRect) |
|
947 : fCullRect(cullRect) { |
|
948 fDrawType = PUSH_CULL; |
|
949 fInfo.push(SkObjectParser::RectToString(cullRect)); |
|
950 } |
|
951 |
|
952 void SkPushCullCommand::execute(SkCanvas* canvas) { |
|
953 canvas->pushCull(fCullRect); |
|
954 } |
|
955 |
|
956 void SkPushCullCommand::vizExecute(SkCanvas* canvas) { |
|
957 canvas->pushCull(fCullRect); |
|
958 |
|
959 SkPaint p; |
|
960 p.setColor(SK_ColorCYAN); |
|
961 p.setStyle(SkPaint::kStroke_Style); |
|
962 canvas->drawRect(fCullRect, p); |
|
963 } |
|
964 |
|
965 SkPopCullCommand::SkPopCullCommand() { |
|
966 fDrawType = POP_CULL; |
|
967 } |
|
968 |
|
969 void SkPopCullCommand::execute(SkCanvas* canvas) { |
|
970 canvas->popCull(); |
|
971 } |