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