68574caa73bf52a8d1f482c55cea7fca4f5e5d63
[platform/upstream/libSkiaSharp.git] / src / utils / SkDeferredCanvas.h
1
2 /*
3  * Copyright 2016 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 SkDeferredCanvas_DEFINED
10 #define SkDeferredCanvas_DEFINED
11
12 #include "../private/SkTDArray.h"
13 #include "SkCanvas.h"
14
15 class SK_API SkDeferredCanvas : public SkCanvas {
16 public:
17     SkDeferredCanvas(SkCanvas* = nullptr);
18     ~SkDeferredCanvas() override;
19
20     void reset(SkCanvas*);
21
22 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
23     SkDrawFilter* setDrawFilter(SkDrawFilter*) override;
24 #endif
25
26 protected:
27     sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
28     SkISize getBaseLayerSize() const override;
29     bool getClipBounds(SkRect* bounds) const override;
30     bool getClipDeviceBounds(SkIRect* bounds) const override;
31     bool isClipEmpty() const override;
32     bool isClipRect() const override;
33     bool onPeekPixels(SkPixmap*) override;
34     bool onAccessTopLayerPixels(SkPixmap*) override;
35     SkImageInfo onImageInfo() const override;
36     bool onGetProps(SkSurfaceProps*) const override;
37     void onFlush() override;
38
39     void willSave() override;
40     SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
41     void willRestore() override;
42
43     void didConcat(const SkMatrix&) override;
44     void didSetMatrix(const SkMatrix&) override;
45
46     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
47     virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
48                             const SkPaint&) override;
49     virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
50                                const SkPaint&) override;
51     virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
52                                 SkScalar constY, const SkPaint&) override;
53     virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
54                                   const SkMatrix* matrix, const SkPaint&) override;
55     void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform[],
56                            const SkRect* cullRect, const SkPaint&) override;
57     virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
58                                 const SkPaint& paint) override;
59     virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
60                              const SkPoint texCoords[4], SkXfermode* xmode,
61                              const SkPaint& paint) override;
62
63     void onDrawPaint(const SkPaint&) override;
64     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
65     void onDrawRect(const SkRect&, const SkPaint&) override;
66     void onDrawRegion(const SkRegion& region, const SkPaint& paint) override;
67     void onDrawOval(const SkRect&, const SkPaint&) override;
68     void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
69     void onDrawRRect(const SkRRect&, const SkPaint&) override;
70     void onDrawPath(const SkPath&, const SkPaint&) override;
71
72     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
73     void onDrawBitmapLattice(const SkBitmap&, const Lattice& lattice, const SkRect& dst,
74                              const SkPaint*) override;
75     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
76                           const SkPaint*) override;
77     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
78                           SrcRectConstraint) override;
79
80     void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
81     void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst,
82                             const SkPaint*) override;
83     void onDrawImageNine(const SkImage* image, const SkIRect& center,
84                          const SkRect& dst, const SkPaint*) override;
85     void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
86                          const SkPaint*, SrcRectConstraint) override;
87
88     void onDrawVertices(VertexMode vmode, int vertexCount,
89                               const SkPoint vertices[], const SkPoint texs[],
90                               const SkColor colors[], SkXfermode* xmode,
91                               const uint16_t indices[], int indexCount,
92                               const SkPaint&) override;
93     void onDrawAtlas(const SkImage* image, const SkRSXform xform[],
94                      const SkRect rects[], const SkColor colors[],
95                      int count, SkXfermode::Mode mode,
96                      const SkRect* cull, const SkPaint* paint) override;
97
98     void onClipRect(const SkRect&, ClipOp, ClipEdgeStyle) override;
99     void onClipRRect(const SkRRect&, ClipOp, ClipEdgeStyle) override;
100     void onClipPath(const SkPath&, ClipOp, ClipEdgeStyle) override;
101     void onClipRegion(const SkRegion&, ClipOp) override;
102
103     void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
104     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
105     void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
106
107     class Iter;
108
109 private:
110     SkCanvas* fCanvas{nullptr};
111
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     SkTDArray<Rec>  fRecs;
137
138     void push_save();
139     void push_cliprect(const SkRect&);
140     bool push_concat(const SkMatrix&);
141
142     void emit(const Rec& rec);
143
144     void flush_all();
145     void flush_before_saves();
146     void flush_le(int index);
147     void flush_translate(SkScalar* x, SkScalar* y, const SkPaint&);
148     void flush_translate(SkScalar* x, SkScalar* y, const SkRect& bounds, const SkPaint* = nullptr);
149     void flush_check(SkRect* bounds, const SkPaint*, unsigned flags = 0);
150
151     void internal_flush_translate(SkScalar* x, SkScalar* y, const SkRect* boundsOrNull);
152
153     typedef SkCanvas INHERITED;
154 };
155
156
157 #endif