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 #ifndef SKDRAWCOMMAND_H_
10 #define SKDRAWCOMMAND_H_
11 
12 #include "SkCanvas.h"
13 #include "SkString.h"
14 
15 class SK_API SkDrawCommand {
16 public:
17     enum OpType {
18         kBeginCommentGroup_OpType,
19         kBeginDrawPicture_OpType,
20         kClipPath_OpType,
21         kClipRegion_OpType,
22         kClipRect_OpType,
23         kClipRRect_OpType,
24         kComment_OpType,
25         kConcat_OpType,
26         kDrawBitmap_OpType,
27         kDrawBitmapNine_OpType,
28         kDrawBitmapRect_OpType,
29         kDrawClear_OpType,
30         kDrawDRRect_OpType,
31         kDrawOval_OpType,
32         kDrawPaint_OpType,
33         kDrawPatch_OpType,
34         kDrawPath_OpType,
35         kDrawPoints_OpType,
36         kDrawPosText_OpType,
37         kDrawPosTextH_OpType,
38         kDrawRect_OpType,
39         kDrawRRect_OpType,
40         kDrawSprite_OpType,
41         kDrawText_OpType,
42         kDrawTextBlob_OpType,
43         kDrawTextOnPath_OpType,
44         kDrawVertices_OpType,
45         kEndCommentGroup_OpType,
46         kEndDrawPicture_OpType,
47         kRestore_OpType,
48         kSave_OpType,
49         kSaveLayer_OpType,
50         kSetMatrix_OpType,
51 
52         kLast_OpType = kSetMatrix_OpType
53     };
54 
55     static const int kOpTypeCount = kLast_OpType + 1;
56 
57     SkDrawCommand(OpType opType);
58 
59     virtual ~SkDrawCommand();
60 
61     virtual SkString toString() const;
62 
toCString()63     virtual const char* toCString() const {
64         return GetCommandString(fOpType);
65     }
66 
isVisible()67     bool isVisible() const {
68         return fVisible;
69     }
70 
setVisible(bool toggle)71     void setVisible(bool toggle) {
72         fVisible = toggle;
73     }
74 
Info()75     const SkTDArray<SkString*>* Info() const { return &fInfo; }
76     virtual void execute(SkCanvas*) const = 0;
vizExecute(SkCanvas *)77     virtual void vizExecute(SkCanvas*) const {}
78 
setUserMatrix(const SkMatrix &)79     virtual void setUserMatrix(const SkMatrix&) {}
80 
81     // The next "active" system is only used by save, saveLayer, and restore.
82     // It is used to determine which saveLayers are currently active (at a
83     // given point in the rendering).
84     //      saves just return a kPushLayer action but don't track active state
85     //      restores just return a kPopLayer action
86     //      saveLayers return kPushLayer but also track the active state
87     enum Action {
88         kNone_Action,
89         kPopLayer_Action,
90         kPushLayer_Action,
91     };
action()92     virtual Action action() const { return kNone_Action; }
setActive(bool active)93     virtual void setActive(bool active) {}
active()94     virtual bool active() const { return false; }
95 
getType()96     OpType getType() const { return fOpType; }
97 
render(SkCanvas * canvas)98     virtual bool render(SkCanvas* canvas) const { return false; }
99 
100     static const char* GetCommandString(OpType type);
101 
102 protected:
103     SkTDArray<SkString*> fInfo;
104 
105 private:
106     OpType fOpType;
107     bool   fVisible;
108 };
109 
110 class SkRestoreCommand : public SkDrawCommand {
111 public:
112     SkRestoreCommand();
113     void execute(SkCanvas* canvas) const override;
action()114     Action action() const override { return kPopLayer_Action; }
115 
116 private:
117     typedef SkDrawCommand INHERITED;
118 };
119 
120 class SkClearCommand : public SkDrawCommand {
121 public:
122     SkClearCommand(SkColor color);
123     void execute(SkCanvas* canvas) const override;
124 private:
125     SkColor fColor;
126 
127     typedef SkDrawCommand INHERITED;
128 };
129 
130 class SkClipPathCommand : public SkDrawCommand {
131 public:
132     SkClipPathCommand(const SkPath& path, SkRegion::Op op, bool doAA);
133     void execute(SkCanvas* canvas) const override;
134     bool render(SkCanvas* canvas) const override;
135 private:
136     SkPath       fPath;
137     SkRegion::Op fOp;
138     bool         fDoAA;
139 
140     typedef SkDrawCommand INHERITED;
141 };
142 
143 class SkClipRegionCommand : public SkDrawCommand {
144 public:
145     SkClipRegionCommand(const SkRegion& region, SkRegion::Op op);
146     void execute(SkCanvas* canvas) const override;
147 private:
148     SkRegion     fRegion;
149     SkRegion::Op fOp;
150 
151     typedef SkDrawCommand INHERITED;
152 };
153 
154 class SkClipRectCommand : public SkDrawCommand {
155 public:
156     SkClipRectCommand(const SkRect& rect, SkRegion::Op op, bool doAA);
157     void execute(SkCanvas* canvas) const override;
158 
rect()159     const SkRect& rect() const { return fRect; }
op()160     SkRegion::Op op() const { return fOp; }
doAA()161     bool doAA() const { return fDoAA; }
162 
163 private:
164     SkRect       fRect;
165     SkRegion::Op fOp;
166     bool         fDoAA;
167 
168     typedef SkDrawCommand INHERITED;
169 };
170 
171 class SkClipRRectCommand : public SkDrawCommand {
172 public:
173     SkClipRRectCommand(const SkRRect& rrect, SkRegion::Op op, bool doAA);
174     void execute(SkCanvas* canvas) const override;
175     bool render(SkCanvas* canvas) const override;
176 
rrect()177     const SkRRect& rrect() const { return fRRect; }
op()178     SkRegion::Op op() const { return fOp; }
doAA()179     bool doAA() const { return fDoAA; }
180 
181 private:
182     SkRRect      fRRect;
183     SkRegion::Op fOp;
184     bool         fDoAA;
185 
186     typedef SkDrawCommand INHERITED;
187 };
188 
189 class SkConcatCommand : public SkDrawCommand {
190 public:
191     SkConcatCommand(const SkMatrix& matrix);
192     void execute(SkCanvas* canvas) const override;
193 private:
194     SkMatrix fMatrix;
195 
196     typedef SkDrawCommand INHERITED;
197 };
198 
199 class SkDrawBitmapCommand : public SkDrawCommand {
200 public:
201     SkDrawBitmapCommand(const SkBitmap& bitmap, SkScalar left, SkScalar top,
202                         const SkPaint* paint);
203     void execute(SkCanvas* canvas) const override;
204     bool render(SkCanvas* canvas) const override;
205 private:
206     SkBitmap fBitmap;
207     SkScalar fLeft;
208     SkScalar fTop;
209     SkPaint  fPaint;
210     SkPaint* fPaintPtr;
211 
212     typedef SkDrawCommand INHERITED;
213 };
214 
215 class SkDrawBitmapNineCommand : public SkDrawCommand {
216 public:
217     SkDrawBitmapNineCommand(const SkBitmap& bitmap, const SkIRect& center,
218                             const SkRect& dst, const SkPaint* paint);
219     void execute(SkCanvas* canvas) const override;
220     bool render(SkCanvas* canvas) const override;
221 private:
222     SkBitmap fBitmap;
223     SkIRect  fCenter;
224     SkRect   fDst;
225     SkPaint  fPaint;
226     SkPaint* fPaintPtr;
227 
228     typedef SkDrawCommand INHERITED;
229 };
230 
231 class SkDrawBitmapRectCommand : public SkDrawCommand {
232 public:
233     SkDrawBitmapRectCommand(const SkBitmap& bitmap, const SkRect* src,
234                             const SkRect& dst, const SkPaint* paint,
235                             SkCanvas::DrawBitmapRectFlags flags);
236     void execute(SkCanvas* canvas) const override;
237     bool render(SkCanvas* canvas) const override;
238 
bitmap()239     const SkBitmap& bitmap() const { return fBitmap; }
240 
241     // The non-const 'paint' method allows modification of this object's
242     // SkPaint. For this reason the ctor and setPaint method make a local copy.
243     // The 'fPaintPtr' member acts a signal that the local SkPaint is valid
244     // (since only an SkPaint* is passed into the ctor).
paint()245     const SkPaint* paint() const { return fPaintPtr; }
paint()246     SkPaint* paint() { return fPaintPtr; }
247 
setPaint(const SkPaint & paint)248     void setPaint(const SkPaint& paint) { fPaint = paint; fPaintPtr = &fPaint; }
249 
srcRect()250     const SkRect* srcRect() const { return fSrc.isEmpty() ? NULL : &fSrc; }
setSrcRect(const SkRect & src)251     void setSrcRect(const SkRect& src) { fSrc = src; }
252 
dstRect()253     const SkRect& dstRect() const { return fDst; }
setDstRect(const SkRect & dst)254     void setDstRect(const SkRect& dst) { fDst = dst; }
255 
flags()256     SkCanvas::DrawBitmapRectFlags flags() const { return fFlags; }
setFlags(SkCanvas::DrawBitmapRectFlags flags)257     void setFlags(SkCanvas::DrawBitmapRectFlags flags) { fFlags = flags; }
258 
259 private:
260     SkBitmap                      fBitmap;
261     SkRect                        fSrc;
262     SkRect                        fDst;
263     SkPaint                       fPaint;
264     SkPaint*                      fPaintPtr;
265     SkCanvas::DrawBitmapRectFlags fFlags;
266 
267     typedef SkDrawCommand INHERITED;
268 };
269 
270 class SkBeginCommentGroupCommand : public SkDrawCommand {
271 public:
272     SkBeginCommentGroupCommand(const char* description);
execute(SkCanvas * canvas)273     void execute(SkCanvas* canvas) const override {
274         canvas->beginCommentGroup(fDescription.c_str());
275     };
276 private:
277     SkString fDescription;
278 
279     typedef SkDrawCommand INHERITED;
280 };
281 
282 class SkCommentCommand : public SkDrawCommand {
283 public:
284     SkCommentCommand(const char* kywd, const char* value);
execute(SkCanvas * canvas)285     void execute(SkCanvas* canvas) const override {
286         canvas->addComment(fKywd.c_str(), fValue.c_str());
287     };
288 private:
289     SkString fKywd;
290     SkString fValue;
291 
292     typedef SkDrawCommand INHERITED;
293 };
294 
295 class SkEndCommentGroupCommand : public SkDrawCommand {
296 public:
297     SkEndCommentGroupCommand();
execute(SkCanvas * canvas)298     void execute(SkCanvas* canvas) const override {
299         canvas->endCommentGroup();
300     };
301 private:
302     typedef SkDrawCommand INHERITED;
303 };
304 
305 class SkDrawOvalCommand : public SkDrawCommand {
306 public:
307     SkDrawOvalCommand(const SkRect& oval, const SkPaint& paint);
308     void execute(SkCanvas* canvas) const override;
309     bool render(SkCanvas* canvas) const override;
310 private:
311     SkRect  fOval;
312     SkPaint fPaint;
313 
314     typedef SkDrawCommand INHERITED;
315 };
316 
317 class SkDrawPaintCommand : public SkDrawCommand {
318 public:
319     SkDrawPaintCommand(const SkPaint& paint);
320     void execute(SkCanvas* canvas) const override;
321     bool render(SkCanvas* canvas) const override;
322 private:
323     SkPaint fPaint;
324 
325     typedef SkDrawCommand INHERITED;
326 };
327 
328 class SkDrawPathCommand : public SkDrawCommand {
329 public:
330     SkDrawPathCommand(const SkPath& path, const SkPaint& paint);
331     void execute(SkCanvas* canvas) const override;
332     bool render(SkCanvas* canvas) const override;
333 
334 private:
335     SkPath   fPath;
336     SkPaint  fPaint;
337 
338     typedef SkDrawCommand INHERITED;
339 };
340 
341 class SkBeginDrawPictureCommand : public SkDrawCommand {
342 public:
343     SkBeginDrawPictureCommand(const SkPicture* picture,
344                               const SkMatrix* matrix,
345                               const SkPaint* paint);
346 
347     void execute(SkCanvas* canvas) const override;
348     bool render(SkCanvas* canvas) const override;
349 
350 private:
351     SkAutoTUnref<const SkPicture> fPicture;
352     SkTLazy<SkMatrix>             fMatrix;
353     SkTLazy<SkPaint>              fPaint;
354 
355     typedef SkDrawCommand INHERITED;
356 };
357 
358 class SkEndDrawPictureCommand : public SkDrawCommand {
359 public:
360     SkEndDrawPictureCommand(bool restore);
361 
362     void execute(SkCanvas* canvas) const override;
363 
364 private:
365     bool fRestore;
366 
367     typedef SkDrawCommand INHERITED;
368 };
369 
370 class SkDrawPointsCommand : public SkDrawCommand {
371 public:
372     SkDrawPointsCommand(SkCanvas::PointMode mode, size_t count, const SkPoint pts[],
373                         const SkPaint& paint);
~SkDrawPointsCommand()374     virtual ~SkDrawPointsCommand() { delete [] fPts; }
375     void execute(SkCanvas* canvas) const override;
376     bool render(SkCanvas* canvas) const override;
377 private:
378     SkCanvas::PointMode fMode;
379     size_t              fCount;
380     SkPoint*            fPts;
381     SkPaint             fPaint;
382 
383     typedef SkDrawCommand INHERITED;
384 };
385 
386 class SkDrawTextCommand : public SkDrawCommand {
387 public:
388     SkDrawTextCommand(const void* text, size_t byteLength, SkScalar x, SkScalar y,
389                       const SkPaint& paint);
~SkDrawTextCommand()390     virtual ~SkDrawTextCommand() { delete [] fText; }
391     void execute(SkCanvas* canvas) const override;
392 private:
393     char*    fText;
394     size_t   fByteLength;
395     SkScalar fX;
396     SkScalar fY;
397     SkPaint  fPaint;
398 
399     typedef SkDrawCommand INHERITED;
400 };
401 
402 class SkDrawPosTextCommand : public SkDrawCommand {
403 public:
404     SkDrawPosTextCommand(const void* text, size_t byteLength, const SkPoint pos[],
405                          const SkPaint& paint);
~SkDrawPosTextCommand()406     virtual ~SkDrawPosTextCommand() { delete [] fPos; delete [] fText; }
407     void execute(SkCanvas* canvas) const override;
408 private:
409     char*    fText;
410     size_t   fByteLength;
411     SkPoint* fPos;
412     SkPaint  fPaint;
413 
414     typedef SkDrawCommand INHERITED;
415 };
416 
417 class SkDrawTextOnPathCommand : public SkDrawCommand {
418 public:
419     SkDrawTextOnPathCommand(const void* text, size_t byteLength, const SkPath& path,
420                             const SkMatrix* matrix, const SkPaint& paint);
~SkDrawTextOnPathCommand()421     virtual ~SkDrawTextOnPathCommand() { delete [] fText; }
422     void execute(SkCanvas* canvas) const override;
423 private:
424     char*    fText;
425     size_t   fByteLength;
426     SkPath   fPath;
427     SkMatrix fMatrix;
428     SkPaint  fPaint;
429 
430     typedef SkDrawCommand INHERITED;
431 };
432 
433 class SkDrawPosTextHCommand : public SkDrawCommand {
434 public:
435     SkDrawPosTextHCommand(const void* text, size_t byteLength, const SkScalar xpos[],
436                           SkScalar constY, const SkPaint& paint);
~SkDrawPosTextHCommand()437     virtual ~SkDrawPosTextHCommand() { delete [] fXpos; delete [] fText; }
438     void execute(SkCanvas* canvas) const override;
439 private:
440     SkScalar* fXpos;
441     char*     fText;
442     size_t    fByteLength;
443     SkScalar  fConstY;
444     SkPaint   fPaint;
445 
446     typedef SkDrawCommand INHERITED;
447 };
448 
449 class SkDrawTextBlobCommand : public SkDrawCommand {
450 public:
451     SkDrawTextBlobCommand(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint);
452 
453     void execute(SkCanvas* canvas) const override;
454     bool render(SkCanvas* canvas) const override;
455 
456 private:
457     SkAutoTUnref<const SkTextBlob> fBlob;
458     SkScalar                       fXPos;
459     SkScalar                       fYPos;
460     SkPaint                        fPaint;
461 
462     typedef SkDrawCommand INHERITED;
463 };
464 
465 class SkDrawPatchCommand : public SkDrawCommand {
466 public:
467     SkDrawPatchCommand(const SkPoint cubics[12], const SkColor colors[4],
468                        const SkPoint texCoords[4], SkXfermode* xmode,
469                        const SkPaint& paint);
470     void execute(SkCanvas* canvas) const override;
471 
472 private:
473     SkPoint fCubics[12];
474     SkColor fColors[4];
475     SkPoint fTexCoords[4];
476     SkAutoTUnref<SkXfermode> fXfermode;
477     SkPaint fPaint;
478 
479     typedef SkDrawCommand INHERITED;
480 };
481 
482 
483 class SkDrawRectCommand : public SkDrawCommand {
484 public:
485     SkDrawRectCommand(const SkRect& rect, const SkPaint& paint);
486     void execute(SkCanvas* canvas) const override;
487 
rect()488     const SkRect& rect() const   { return fRect; }
paint()489     const SkPaint& paint() const { return fPaint; }
490 private:
491     SkRect  fRect;
492     SkPaint fPaint;
493 
494     typedef SkDrawCommand INHERITED;
495 };
496 
497 class SkDrawRRectCommand : public SkDrawCommand {
498 public:
499     SkDrawRRectCommand(const SkRRect& rrect, const SkPaint& paint);
500     void execute(SkCanvas* canvas) const override;
501     bool render(SkCanvas* canvas) const override;
502 private:
503     SkRRect fRRect;
504     SkPaint fPaint;
505 
506     typedef SkDrawCommand INHERITED;
507 };
508 
509 class SkDrawDRRectCommand : public SkDrawCommand {
510 public:
511     SkDrawDRRectCommand(const SkRRect& outer, const SkRRect& inner,
512                         const SkPaint& paint);
513     void execute(SkCanvas* canvas) const override;
514     bool render(SkCanvas* canvas) const override;
515 private:
516     SkRRect fOuter;
517     SkRRect fInner;
518     SkPaint fPaint;
519 
520     typedef SkDrawCommand INHERITED;
521 };
522 
523 class SkDrawSpriteCommand : public SkDrawCommand {
524 public:
525     SkDrawSpriteCommand(const SkBitmap& bitmap, int left, int top, const SkPaint* paint);
526     void execute(SkCanvas* canvas) const override;
527     bool render(SkCanvas* canvas) const override;
528 private:
529     SkBitmap fBitmap;
530     int      fLeft;
531     int      fTop;
532     SkPaint  fPaint;
533     SkPaint* fPaintPtr;
534 
535     typedef SkDrawCommand INHERITED;
536 };
537 
538 class SkDrawVerticesCommand : public SkDrawCommand {
539 public:
540     SkDrawVerticesCommand(SkCanvas::VertexMode vmode, int vertexCount,
541                           const SkPoint vertices[], const SkPoint texs[],
542                           const SkColor colors[], SkXfermode* xfermode,
543                           const uint16_t indices[], int indexCount,
544                           const SkPaint& paint);
545     virtual ~SkDrawVerticesCommand();
546     void execute(SkCanvas* canvas) const override;
547 private:
548     SkCanvas::VertexMode fVmode;
549     int         fVertexCount;
550     SkPoint*    fVertices;
551     SkPoint*    fTexs;
552     SkColor*    fColors;
553     SkXfermode* fXfermode;
554     uint16_t*   fIndices;
555     int         fIndexCount;
556     SkPaint     fPaint;
557 
558     typedef SkDrawCommand INHERITED;
559 };
560 
561 class SkSaveCommand : public SkDrawCommand {
562 public:
563     SkSaveCommand();
564     void execute(SkCanvas* canvas) const override;
action()565     Action action() const override { return kPushLayer_Action; }
566 private:
567     typedef SkDrawCommand INHERITED;
568 };
569 
570 class SkSaveLayerCommand : public SkDrawCommand {
571 public:
572     SkSaveLayerCommand(const SkRect* bounds, const SkPaint* paint,
573                        SkCanvas::SaveFlags flags);
574     void execute(SkCanvas* canvas) const override;
575     void vizExecute(SkCanvas* canvas) const override;
action()576     Action action() const override{ return kPushLayer_Action; }
setActive(bool active)577     void setActive(bool active) override { fActive = active; }
active()578     bool active() const override { return fActive; }
579 
paint()580     const SkPaint* paint() const { return fPaintPtr; }
581 
582 private:
583     SkRect              fBounds;
584     SkPaint             fPaint;
585     SkPaint*            fPaintPtr;
586     SkCanvas::SaveFlags fFlags;
587 
588     bool                fActive;
589 
590     typedef SkDrawCommand INHERITED;
591 };
592 
593 class SkSetMatrixCommand : public SkDrawCommand {
594 public:
595     SkSetMatrixCommand(const SkMatrix& matrix);
596     void setUserMatrix(const SkMatrix&) override;
597     void execute(SkCanvas* canvas) const override;
598 private:
599     SkMatrix fUserMatrix;
600     SkMatrix fMatrix;
601 
602     typedef SkDrawCommand INHERITED;
603 };
604 
605 #endif
606