Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / utils / SkPaintFilterCanvas.cpp
1 /*
2  * Copyright 2015 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 #include "include/utils/SkPaintFilterCanvas.h"
9
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPixmap.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkSurface.h" // IWYU pragma: keep
16
17 #include <optional>
18
19 class SkData;
20 class SkDrawable;
21 class SkGlyphRunList;
22 class SkImage;
23 class SkPath;
24 class SkPicture;
25 class SkRRect;
26 class SkRegion;
27 class SkSurfaceProps;
28 class SkTextBlob;
29 class SkVertices;
30 struct SkDrawShadowRec;
31
32 class SkPaintFilterCanvas::AutoPaintFilter {
33 public:
34     AutoPaintFilter(const SkPaintFilterCanvas* canvas, const SkPaint* paint)
35         : fPaint(paint ? *paint : SkPaint()) {
36         fShouldDraw = canvas->onFilter(fPaint);
37     }
38
39     AutoPaintFilter(const SkPaintFilterCanvas* canvas, const SkPaint& paint)
40         : AutoPaintFilter(canvas, &paint) { }
41
42     const SkPaint& paint() const { return fPaint; }
43
44     bool shouldDraw() const { return fShouldDraw; }
45
46 private:
47     SkPaint fPaint;
48     bool fShouldDraw;
49 };
50
51 SkPaintFilterCanvas::SkPaintFilterCanvas(SkCanvas *canvas)
52     : SkCanvasVirtualEnforcer<SkNWayCanvas>(canvas->imageInfo().width(),
53                                               canvas->imageInfo().height()) {
54
55     // Transfer matrix & clip state before adding the target canvas.
56     this->clipRect(SkRect::Make(canvas->getDeviceClipBounds()));
57     this->setMatrix(canvas->getLocalToDevice());
58
59     this->addCanvas(canvas);
60 }
61
62 void SkPaintFilterCanvas::onDrawPaint(const SkPaint& paint) {
63     AutoPaintFilter apf(this, paint);
64     if (apf.shouldDraw()) {
65         this->SkNWayCanvas::onDrawPaint(apf.paint());
66     }
67 }
68
69 void SkPaintFilterCanvas::onDrawBehind(const SkPaint& paint) {
70     AutoPaintFilter apf(this, paint);
71     if (apf.shouldDraw()) {
72         this->SkNWayCanvas::onDrawBehind(apf.paint());
73     }
74 }
75
76 void SkPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
77                                        const SkPaint& paint) {
78     AutoPaintFilter apf(this, paint);
79     if (apf.shouldDraw()) {
80         this->SkNWayCanvas::onDrawPoints(mode, count, pts, apf.paint());
81     }
82 }
83
84 void SkPaintFilterCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
85     AutoPaintFilter apf(this, paint);
86     if (apf.shouldDraw()) {
87         this->SkNWayCanvas::onDrawRect(rect, apf.paint());
88     }
89 }
90
91 void SkPaintFilterCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
92     AutoPaintFilter apf(this, paint);
93     if (apf.shouldDraw()) {
94         this->SkNWayCanvas::onDrawRRect(rrect, apf.paint());
95     }
96 }
97
98 void SkPaintFilterCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
99                                        const SkPaint& paint) {
100     AutoPaintFilter apf(this, paint);
101     if (apf.shouldDraw()) {
102         this->SkNWayCanvas::onDrawDRRect(outer, inner, apf.paint());
103     }
104 }
105
106 void SkPaintFilterCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
107     AutoPaintFilter apf(this, paint);
108     if (apf.shouldDraw()) {
109         this->SkNWayCanvas::onDrawRegion(region, apf.paint());
110     }
111 }
112
113 void SkPaintFilterCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
114     AutoPaintFilter apf(this, paint);
115     if (apf.shouldDraw()) {
116         this->SkNWayCanvas::onDrawOval(rect, apf.paint());
117     }
118 }
119
120 void SkPaintFilterCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
121                                     bool useCenter, const SkPaint& paint) {
122     AutoPaintFilter apf(this, paint);
123     if (apf.shouldDraw()) {
124         this->SkNWayCanvas::onDrawArc(rect, startAngle, sweepAngle, useCenter, apf.paint());
125     }
126 }
127
128 void SkPaintFilterCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
129     AutoPaintFilter apf(this, paint);
130     if (apf.shouldDraw()) {
131         this->SkNWayCanvas::onDrawPath(path, apf.paint());
132     }
133 }
134
135 void SkPaintFilterCanvas::onDrawImage2(const SkImage* image, SkScalar left, SkScalar top,
136                                        const SkSamplingOptions& sampling, const SkPaint* paint) {
137     AutoPaintFilter apf(this, paint);
138     if (apf.shouldDraw()) {
139         this->SkNWayCanvas::onDrawImage2(image, left, top, sampling, &apf.paint());
140     }
141 }
142
143 void SkPaintFilterCanvas::onDrawImageRect2(const SkImage* image, const SkRect& src,
144                                            const SkRect& dst, const SkSamplingOptions& sampling,
145                                            const SkPaint* paint, SrcRectConstraint constraint) {
146     AutoPaintFilter apf(this, paint);
147     if (apf.shouldDraw()) {
148         this->SkNWayCanvas::onDrawImageRect2(image, src, dst, sampling, &apf.paint(), constraint);
149     }
150 }
151
152 void SkPaintFilterCanvas::onDrawImageLattice2(const SkImage* image, const Lattice& lattice,
153                                               const SkRect& dst, SkFilterMode filter,
154                                               const SkPaint* paint) {
155     AutoPaintFilter apf(this, paint);
156     if (apf.shouldDraw()) {
157         this->SkNWayCanvas::onDrawImageLattice2(image, lattice, dst, filter, &apf.paint());
158     }
159 }
160
161 void SkPaintFilterCanvas::onDrawVerticesObject(const SkVertices* vertices,
162                                                SkBlendMode bmode, const SkPaint& paint) {
163     AutoPaintFilter apf(this, paint);
164     if (apf.shouldDraw()) {
165         this->SkNWayCanvas::onDrawVerticesObject(vertices, bmode, apf.paint());
166     }
167 }
168
169 void SkPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColor colors[],
170                                       const SkPoint texCoords[], SkBlendMode bmode,
171                                       const SkPaint& paint) {
172     AutoPaintFilter apf(this, paint);
173     if (apf.shouldDraw()) {
174         this->SkNWayCanvas::onDrawPatch(cubics, colors, texCoords, bmode, apf.paint());
175     }
176 }
177
178 void SkPaintFilterCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* m,
179                                         const SkPaint* originalPaint) {
180     AutoPaintFilter apf(this, originalPaint);
181     if (apf.shouldDraw()) {
182         const SkPaint* newPaint = &apf.paint();
183
184         // Passing a paint (-vs- passing null) makes drawPicture draw into a layer...
185         // much slower, and can produce different blending. Thus we should only do this
186         // if the filter's effect actually impacts the picture.
187         if (originalPaint == nullptr) {
188             if (   newPaint->getAlphaf()      == 1.0f
189                 && newPaint->getColorFilter() == nullptr
190                 && newPaint->getImageFilter() == nullptr
191                 && newPaint->asBlendMode()    == SkBlendMode::kSrcOver) {
192                 // restore the original nullptr
193                 newPaint = nullptr;
194             }
195         }
196         this->SkNWayCanvas::onDrawPicture(picture, m, newPaint);
197     }
198 }
199
200 void SkPaintFilterCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
201     // There is no paint to filter in this case, but we can still filter on type.
202     // Subclasses need to unroll the drawable explicity (by overriding this method) in
203     // order to actually filter nested content.
204     AutoPaintFilter apf(this, nullptr);
205     if (apf.shouldDraw()) {
206         this->SkNWayCanvas::onDrawDrawable(drawable, matrix);
207     }
208 }
209
210 void SkPaintFilterCanvas::onDrawGlyphRunList(const SkGlyphRunList& list, const SkPaint& paint) {
211     AutoPaintFilter apf(this, paint);
212     if (apf.shouldDraw()) {
213         this->SkNWayCanvas::onDrawGlyphRunList(list, apf.paint());
214     }
215 }
216
217 void SkPaintFilterCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
218                                          const SkPaint& paint) {
219     AutoPaintFilter apf(this, paint);
220     if (apf.shouldDraw()) {
221         this->SkNWayCanvas::onDrawTextBlob(blob, x, y, apf.paint());
222     }
223 }
224
225 void SkPaintFilterCanvas::onDrawAtlas2(const SkImage* image, const SkRSXform xform[],
226                                        const SkRect tex[], const SkColor colors[], int count,
227                                        SkBlendMode bmode, const SkSamplingOptions& sampling,
228                                        const SkRect* cull, const SkPaint* paint) {
229     AutoPaintFilter apf(this, paint);
230     if (apf.shouldDraw()) {
231         this->SkNWayCanvas::onDrawAtlas2(image, xform, tex, colors, count, bmode, sampling, cull,
232                                          &apf.paint());
233     }
234 }
235
236 void SkPaintFilterCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
237     this->SkNWayCanvas::onDrawAnnotation(rect, key, value);
238 }
239
240 void SkPaintFilterCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
241     this->SkNWayCanvas::onDrawShadowRec(path, rec);
242 }
243
244 void SkPaintFilterCanvas::onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4],
245                                            QuadAAFlags aa, const SkColor4f& color, SkBlendMode mode) {
246     SkPaint paint;
247     paint.setColor(color);
248     paint.setBlendMode(mode);
249     AutoPaintFilter apf(this, paint);
250     if (apf.shouldDraw()) {
251         this->SkNWayCanvas::onDrawEdgeAAQuad(rect, clip, aa, apf.paint().getColor4f(),
252                                              apf.paint().getBlendMode_or(SkBlendMode::kSrcOver));
253     }
254 }
255
256 void SkPaintFilterCanvas::onDrawEdgeAAImageSet2(const ImageSetEntry set[], int count,
257                                                 const SkPoint dstClips[],
258                                                 const SkMatrix preViewMatrices[],
259                                                 const SkSamplingOptions& sampling,
260                                                 const SkPaint* paint,
261                                                 SrcRectConstraint constraint) {
262     AutoPaintFilter apf(this, paint);
263     if (apf.shouldDraw()) {
264         this->SkNWayCanvas::onDrawEdgeAAImageSet2(
265                 set, count, dstClips, preViewMatrices, sampling, &apf.paint(), constraint);
266     }
267 }
268
269 sk_sp<SkSurface> SkPaintFilterCanvas::onNewSurface(const SkImageInfo& info,
270                                                    const SkSurfaceProps& props) {
271     return this->proxy()->makeSurface(info, &props);
272 }
273
274 bool SkPaintFilterCanvas::onPeekPixels(SkPixmap* pixmap) {
275     return this->proxy()->peekPixels(pixmap);
276 }
277
278 bool SkPaintFilterCanvas::onAccessTopLayerPixels(SkPixmap* pixmap) {
279     SkImageInfo info;
280     size_t rowBytes;
281
282     void* addr = this->proxy()->accessTopLayerPixels(&info, &rowBytes);
283     if (!addr) {
284         return false;
285     }
286
287     pixmap->reset(info, addr, rowBytes);
288     return true;
289 }
290
291 SkImageInfo SkPaintFilterCanvas::onImageInfo() const {
292     return this->proxy()->imageInfo();
293 }
294
295 bool SkPaintFilterCanvas::onGetProps(SkSurfaceProps* props) const {
296     return this->proxy()->getProps(props);
297 }