1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkDeferredCanvas_DEFINED
9#define SkDeferredCanvas_DEFINED
10
11#include "../private/SkTDArray.h"
12#include "SkNoDrawCanvas.h"
13
14class SK_API SkDeferredCanvas : public SkNoDrawCanvas {
15public:
16    enum EvalType {kEager, kLazy};
17    // There are two strategies for evaluating of sub-drawings (pictures and drawables).
18    // * kEager - a sub-drawing is expanded using the using the SkDeferredCanvas. This has
19    //            the advantage of optimizing the sub drawing, and is used when the underlying
20    //            SkCanvas is drawing and not recording.
21    // * kLazy  - a sub-drawing is not expanded, but passed directly to the underlying SkCanvas.
22    //            This has the advantage of not expanding the sub drawing and then immediately
23    //            re-encoding it, and is used for recording canvases.
24    SkDeferredCanvas(SkCanvas*, EvalType);
25    ~SkDeferredCanvas() override;
26
27    void reset(SkCanvas*);
28
29#ifdef SK_SUPPORT_LEGACY_DRAWFILTER
30    SkDrawFilter* setDrawFilter(SkDrawFilter*) override;
31#endif
32
33protected:
34    sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
35    SkISize getBaseLayerSize() const override;
36    SkRect onGetLocalClipBounds() const override;
37    SkIRect onGetDeviceClipBounds() const override;
38    bool isClipEmpty() const override;
39    bool isClipRect() const override;
40    bool onPeekPixels(SkPixmap*) override;
41    bool onAccessTopLayerPixels(SkPixmap*) override;
42    SkImageInfo onImageInfo() const override;
43    bool onGetProps(SkSurfaceProps*) const override;
44    void onFlush() override;
45
46    void willSave() override;
47    SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
48    void willRestore() override;
49
50    void didConcat(const SkMatrix&) override;
51    void didSetMatrix(const SkMatrix&) override;
52
53    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
54    virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
55                            const SkPaint&) override;
56    virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
57                               const SkPaint&) override;
58    virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
59                                SkScalar constY, const SkPaint&) override;
60    virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
61                                  const SkMatrix* matrix, const SkPaint&) override;
62    void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform[],
63                           const SkRect* cullRect, const SkPaint&) override;
64    virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
65                                const SkPaint& paint) override;
66    virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
67                             const SkPoint texCoords[4], SkBlendMode,
68                             const SkPaint& paint) override;
69
70    void onDrawPaint(const SkPaint&) override;
71    void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
72    void onDrawRect(const SkRect&, const SkPaint&) override;
73    void onDrawRegion(const SkRegion& region, const SkPaint& paint) override;
74    void onDrawOval(const SkRect&, const SkPaint&) override;
75    void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
76    void onDrawRRect(const SkRRect&, const SkPaint&) override;
77    void onDrawPath(const SkPath&, const SkPaint&) override;
78
79    void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
80    void onDrawBitmapLattice(const SkBitmap&, const Lattice& lattice, const SkRect& dst,
81                             const SkPaint*) override;
82    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
83                          const SkPaint*) override;
84    void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
85                          SrcRectConstraint) override;
86
87    void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
88    void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst,
89                            const SkPaint*) override;
90    void onDrawImageNine(const SkImage* image, const SkIRect& center,
91                         const SkRect& dst, const SkPaint*) override;
92    void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
93                         const SkPaint*, SrcRectConstraint) override;
94
95    void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
96    void onDrawAtlas(const SkImage* image, const SkRSXform xform[],
97                     const SkRect rects[], const SkColor colors[],
98                     int count, SkBlendMode, const SkRect* cull, const SkPaint* paint) override;
99
100    void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
101    void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
102    void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
103    void onClipRegion(const SkRegion&, SkClipOp) override;
104
105    void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
106    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
107    void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
108
109    class Iter;
110
111private:
112    enum Type {
113        kSave_Type,
114        kClipRect_Type,
115        kTrans_Type,
116        kScaleTrans_Type,
117    };
118    struct Rec {
119        Type    fType;
120        union {
121            SkRect      fBounds;
122            SkVector    fTranslate;
123            struct {
124                SkVector    fScale;
125                SkVector    fTrans; // post translate
126            } fScaleTrans;
127        } fData;
128
129        bool isConcat(SkMatrix*) const;
130        void getConcat(SkMatrix* mat) const {
131            SkDEBUGCODE(bool isconcat = ) this->isConcat(mat);
132            SkASSERT(isconcat);
133        }
134        void setConcat(const SkMatrix&);
135    };
136
137    void push_save();
138    void push_cliprect(const SkRect&);
139    bool push_concat(const SkMatrix&);
140
141    void emit(const Rec& rec);
142
143    void flush_all();
144    void flush_before_saves();
145    void flush_le(int index);
146    void flush_translate(SkScalar* x, SkScalar* y, const SkPaint&);
147    void flush_translate(SkScalar* x, SkScalar* y, const SkRect& bounds, const SkPaint* = nullptr);
148    void flush_check(SkRect* bounds, const SkPaint*, unsigned flags = 0);
149
150    void internal_flush_translate(SkScalar* x, SkScalar* y, const SkRect* boundsOrNull);
151
152    SkTDArray<Rec> fRecs;
153    SkCanvas*      fCanvas;
154    const EvalType fEvalType;
155
156    typedef SkNoDrawCanvas INHERITED;
157};
158
159
160#endif
161