3 * Copyright 2012 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #ifndef SKDRAWCOMMAND_H_
10 #define SKDRAWCOMMAND_H_
15 class SK_API SkDrawCommand {
18 kBeginCommentGroup_OpType,
26 kDrawBitmapNine_OpType,
27 kDrawBitmapRect_OpType,
43 kDrawTextOnPath_OpType,
45 kEndCommentGroup_OpType,
51 kLast_OpType = kSetMatrix_OpType
54 static const int kOpTypeCount = kLast_OpType + 1;
56 SkDrawCommand(OpType opType);
58 virtual ~SkDrawCommand();
60 virtual SkString toString() const;
62 void setOffset(size_t offset) { fOffset = offset; }
63 size_t offset() const { return fOffset; }
65 virtual const char* toCString() const {
66 return GetCommandString(fOpType);
69 bool isVisible() const {
73 void setVisible(bool toggle) {
77 const SkTDArray<SkString*>* Info() const { return &fInfo; }
78 virtual void execute(SkCanvas*) const = 0;
79 virtual void vizExecute(SkCanvas*) const {}
81 virtual void setUserMatrix(const SkMatrix&) {}
83 // The next "active" system is only used by save, saveLayer, and restore.
84 // It is used to determine which saveLayers are currently active (at a
85 // given point in the rendering).
86 // saves just return a kPushLayer action but don't track active state
87 // restores just return a kPopLayer action
88 // saveLayers return kPushLayer but also track the active state
94 virtual Action action() const { return kNone_Action; }
95 virtual void setActive(bool active) {}
96 virtual bool active() const { return false; }
98 OpType getType() const { return fOpType; }
100 virtual bool render(SkCanvas* canvas) const { return false; }
102 static const char* GetCommandString(OpType type);
105 SkTDArray<SkString*> fInfo;
113 class SkRestoreCommand : public SkDrawCommand {
116 void execute(SkCanvas* canvas) const SK_OVERRIDE;
117 Action action() const SK_OVERRIDE { return kPopLayer_Action; }
120 typedef SkDrawCommand INHERITED;
123 class SkClearCommand : public SkDrawCommand {
125 SkClearCommand(SkColor color);
126 void execute(SkCanvas* canvas) const SK_OVERRIDE;
130 typedef SkDrawCommand INHERITED;
133 class SkClipPathCommand : public SkDrawCommand {
135 SkClipPathCommand(const SkPath& path, SkRegion::Op op, bool doAA);
136 void execute(SkCanvas* canvas) const SK_OVERRIDE;
137 bool render(SkCanvas* canvas) const SK_OVERRIDE;
143 typedef SkDrawCommand INHERITED;
146 class SkClipRegionCommand : public SkDrawCommand {
148 SkClipRegionCommand(const SkRegion& region, SkRegion::Op op);
149 void execute(SkCanvas* canvas) const SK_OVERRIDE;
154 typedef SkDrawCommand INHERITED;
157 class SkClipRectCommand : public SkDrawCommand {
159 SkClipRectCommand(const SkRect& rect, SkRegion::Op op, bool doAA);
160 void execute(SkCanvas* canvas) const SK_OVERRIDE;
162 const SkRect& rect() const { return fRect; }
163 SkRegion::Op op() const { return fOp; }
164 bool doAA() const { return fDoAA; }
171 typedef SkDrawCommand INHERITED;
174 class SkClipRRectCommand : public SkDrawCommand {
176 SkClipRRectCommand(const SkRRect& rrect, SkRegion::Op op, bool doAA);
177 void execute(SkCanvas* canvas) const SK_OVERRIDE;
178 bool render(SkCanvas* canvas) const SK_OVERRIDE;
180 const SkRRect& rrect() const { return fRRect; }
181 SkRegion::Op op() const { return fOp; }
182 bool doAA() const { return fDoAA; }
189 typedef SkDrawCommand INHERITED;
192 class SkConcatCommand : public SkDrawCommand {
194 SkConcatCommand(const SkMatrix& matrix);
195 void execute(SkCanvas* canvas) const SK_OVERRIDE;
199 typedef SkDrawCommand INHERITED;
202 class SkDrawBitmapCommand : public SkDrawCommand {
204 SkDrawBitmapCommand(const SkBitmap& bitmap, SkScalar left, SkScalar top,
205 const SkPaint* paint);
206 void execute(SkCanvas* canvas) const SK_OVERRIDE;
207 bool render(SkCanvas* canvas) const SK_OVERRIDE;
215 typedef SkDrawCommand INHERITED;
218 class SkDrawBitmapNineCommand : public SkDrawCommand {
220 SkDrawBitmapNineCommand(const SkBitmap& bitmap, const SkIRect& center,
221 const SkRect& dst, const SkPaint* paint);
222 void execute(SkCanvas* canvas) const SK_OVERRIDE;
223 bool render(SkCanvas* canvas) const SK_OVERRIDE;
231 typedef SkDrawCommand INHERITED;
234 class SkDrawBitmapRectCommand : public SkDrawCommand {
236 SkDrawBitmapRectCommand(const SkBitmap& bitmap, const SkRect* src,
237 const SkRect& dst, const SkPaint* paint,
238 SkCanvas::DrawBitmapRectFlags flags);
239 void execute(SkCanvas* canvas) const SK_OVERRIDE;
240 bool render(SkCanvas* canvas) const SK_OVERRIDE;
242 const SkBitmap& bitmap() const { return fBitmap; }
244 // The non-const 'paint' method allows modification of this object's
245 // SkPaint. For this reason the ctor and setPaint method make a local copy.
246 // The 'fPaintPtr' member acts a signal that the local SkPaint is valid
247 // (since only an SkPaint* is passed into the ctor).
248 const SkPaint* paint() const { return fPaintPtr; }
249 SkPaint* paint() { return fPaintPtr; }
251 void setPaint(const SkPaint& paint) { fPaint = paint; fPaintPtr = &fPaint; }
253 const SkRect* srcRect() const { return fSrc.isEmpty() ? NULL : &fSrc; }
254 void setSrcRect(const SkRect& src) { fSrc = src; }
256 const SkRect& dstRect() const { return fDst; }
257 void setDstRect(const SkRect& dst) { fDst = dst; }
259 SkCanvas::DrawBitmapRectFlags flags() const { return fFlags; }
260 void setFlags(SkCanvas::DrawBitmapRectFlags flags) { fFlags = flags; }
268 SkCanvas::DrawBitmapRectFlags fFlags;
270 typedef SkDrawCommand INHERITED;
273 class SkBeginCommentGroupCommand : public SkDrawCommand {
275 SkBeginCommentGroupCommand(const char* description);
276 void execute(SkCanvas* canvas) const SK_OVERRIDE {
277 canvas->beginCommentGroup(fDescription.c_str());
280 SkString fDescription;
282 typedef SkDrawCommand INHERITED;
285 class SkCommentCommand : public SkDrawCommand {
287 SkCommentCommand(const char* kywd, const char* value);
288 void execute(SkCanvas* canvas) const SK_OVERRIDE {
289 canvas->addComment(fKywd.c_str(), fValue.c_str());
295 typedef SkDrawCommand INHERITED;
298 class SkEndCommentGroupCommand : public SkDrawCommand {
300 SkEndCommentGroupCommand();
301 void execute(SkCanvas* canvas) const SK_OVERRIDE {
302 canvas->endCommentGroup();
305 typedef SkDrawCommand INHERITED;
308 class SkDrawOvalCommand : public SkDrawCommand {
310 SkDrawOvalCommand(const SkRect& oval, const SkPaint& paint);
311 void execute(SkCanvas* canvas) const SK_OVERRIDE;
312 bool render(SkCanvas* canvas) const SK_OVERRIDE;
317 typedef SkDrawCommand INHERITED;
320 class SkDrawPaintCommand : public SkDrawCommand {
322 SkDrawPaintCommand(const SkPaint& paint);
323 void execute(SkCanvas* canvas) const SK_OVERRIDE;
324 bool render(SkCanvas* canvas) const SK_OVERRIDE;
328 typedef SkDrawCommand INHERITED;
331 class SkDrawPathCommand : public SkDrawCommand {
333 SkDrawPathCommand(const SkPath& path, const SkPaint& paint);
334 void execute(SkCanvas* canvas) const SK_OVERRIDE;
335 bool render(SkCanvas* canvas) const SK_OVERRIDE;
341 typedef SkDrawCommand INHERITED;
344 class SkDrawPictureCommand : public SkDrawCommand {
346 SkDrawPictureCommand(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint);
347 void execute(SkCanvas* canvas) const SK_OVERRIDE;
348 bool render(SkCanvas* canvas) const SK_OVERRIDE;
351 SkAutoTUnref<const SkPicture> fPicture;
353 SkMatrix* fMatrixPtr;
357 typedef SkDrawCommand INHERITED;
360 class SkDrawPointsCommand : public SkDrawCommand {
362 SkDrawPointsCommand(SkCanvas::PointMode mode, size_t count, const SkPoint pts[],
363 const SkPaint& paint);
364 virtual ~SkDrawPointsCommand() { delete [] fPts; }
365 void execute(SkCanvas* canvas) const SK_OVERRIDE;
366 bool render(SkCanvas* canvas) const SK_OVERRIDE;
368 SkCanvas::PointMode fMode;
373 typedef SkDrawCommand INHERITED;
376 class SkDrawTextCommand : public SkDrawCommand {
378 SkDrawTextCommand(const void* text, size_t byteLength, SkScalar x, SkScalar y,
379 const SkPaint& paint);
380 virtual ~SkDrawTextCommand() { delete [] fText; }
381 void execute(SkCanvas* canvas) const SK_OVERRIDE;
389 typedef SkDrawCommand INHERITED;
392 class SkDrawPosTextCommand : public SkDrawCommand {
394 SkDrawPosTextCommand(const void* text, size_t byteLength, const SkPoint pos[],
395 const SkPaint& paint);
396 virtual ~SkDrawPosTextCommand() { delete [] fPos; delete [] fText; }
397 void execute(SkCanvas* canvas) const SK_OVERRIDE;
404 typedef SkDrawCommand INHERITED;
407 class SkDrawTextOnPathCommand : public SkDrawCommand {
409 SkDrawTextOnPathCommand(const void* text, size_t byteLength, const SkPath& path,
410 const SkMatrix* matrix, const SkPaint& paint);
411 virtual ~SkDrawTextOnPathCommand() { delete [] fText; }
412 void execute(SkCanvas* canvas) const SK_OVERRIDE;
420 typedef SkDrawCommand INHERITED;
423 class SkDrawPosTextHCommand : public SkDrawCommand {
425 SkDrawPosTextHCommand(const void* text, size_t byteLength, const SkScalar xpos[],
426 SkScalar constY, const SkPaint& paint);
427 virtual ~SkDrawPosTextHCommand() { delete [] fXpos; delete [] fText; }
428 void execute(SkCanvas* canvas) const SK_OVERRIDE;
436 typedef SkDrawCommand INHERITED;
439 class SkDrawTextBlobCommand : public SkDrawCommand {
441 SkDrawTextBlobCommand(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint);
443 void execute(SkCanvas* canvas) const SK_OVERRIDE;
444 bool render(SkCanvas* canvas) const SK_OVERRIDE;
447 SkAutoTUnref<const SkTextBlob> fBlob;
452 typedef SkDrawCommand INHERITED;
455 class SkDrawPatchCommand : public SkDrawCommand {
457 SkDrawPatchCommand(const SkPoint cubics[12], const SkColor colors[4],
458 const SkPoint texCoords[4], SkXfermode* xmode,
459 const SkPaint& paint);
460 void execute(SkCanvas* canvas) const SK_OVERRIDE;
465 SkPoint fTexCoords[4];
466 SkAutoTUnref<SkXfermode> fXfermode;
469 typedef SkDrawCommand INHERITED;
473 class SkDrawRectCommand : public SkDrawCommand {
475 SkDrawRectCommand(const SkRect& rect, const SkPaint& paint);
476 void execute(SkCanvas* canvas) const SK_OVERRIDE;
478 const SkRect& rect() const { return fRect; }
479 const SkPaint& paint() const { return fPaint; }
484 typedef SkDrawCommand INHERITED;
487 class SkDrawRRectCommand : public SkDrawCommand {
489 SkDrawRRectCommand(const SkRRect& rrect, const SkPaint& paint);
490 void execute(SkCanvas* canvas) const SK_OVERRIDE;
491 bool render(SkCanvas* canvas) const SK_OVERRIDE;
496 typedef SkDrawCommand INHERITED;
499 class SkDrawDRRectCommand : public SkDrawCommand {
501 SkDrawDRRectCommand(const SkRRect& outer, const SkRRect& inner,
502 const SkPaint& paint);
503 void execute(SkCanvas* canvas) const SK_OVERRIDE;
504 bool render(SkCanvas* canvas) const SK_OVERRIDE;
510 typedef SkDrawCommand INHERITED;
513 class SkDrawSpriteCommand : public SkDrawCommand {
515 SkDrawSpriteCommand(const SkBitmap& bitmap, int left, int top, const SkPaint* paint);
516 void execute(SkCanvas* canvas) const SK_OVERRIDE;
517 bool render(SkCanvas* canvas) const SK_OVERRIDE;
525 typedef SkDrawCommand INHERITED;
528 class SkDrawVerticesCommand : public SkDrawCommand {
530 SkDrawVerticesCommand(SkCanvas::VertexMode vmode, int vertexCount,
531 const SkPoint vertices[], const SkPoint texs[],
532 const SkColor colors[], SkXfermode* xfermode,
533 const uint16_t indices[], int indexCount,
534 const SkPaint& paint);
535 virtual ~SkDrawVerticesCommand();
536 void execute(SkCanvas* canvas) const SK_OVERRIDE;
538 SkCanvas::VertexMode fVmode;
543 SkXfermode* fXfermode;
548 typedef SkDrawCommand INHERITED;
551 class SkSaveCommand : public SkDrawCommand {
554 void execute(SkCanvas* canvas) const SK_OVERRIDE;
555 Action action() const SK_OVERRIDE { return kPushLayer_Action; }
557 typedef SkDrawCommand INHERITED;
560 class SkSaveLayerCommand : public SkDrawCommand {
562 SkSaveLayerCommand(const SkRect* bounds, const SkPaint* paint,
563 SkCanvas::SaveFlags flags);
564 void execute(SkCanvas* canvas) const SK_OVERRIDE;
565 void vizExecute(SkCanvas* canvas) const SK_OVERRIDE;
566 Action action() const SK_OVERRIDE{ return kPushLayer_Action; }
567 void setActive(bool active) SK_OVERRIDE { fActive = active; }
568 bool active() const SK_OVERRIDE { return fActive; }
570 const SkPaint* paint() const { return fPaintPtr; }
576 SkCanvas::SaveFlags fFlags;
580 typedef SkDrawCommand INHERITED;
583 class SkSetMatrixCommand : public SkDrawCommand {
585 SkSetMatrixCommand(const SkMatrix& matrix);
586 void setUserMatrix(const SkMatrix&) SK_OVERRIDE;
587 void execute(SkCanvas* canvas) const SK_OVERRIDE;
589 SkMatrix fUserMatrix;
592 typedef SkDrawCommand INHERITED;