Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / shaders / gradients / SkTwoPointConicalGradient.cpp
1 /*
2  * Copyright 2012 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 "src/shaders/gradients/SkTwoPointConicalGradient.h"
9
10 #include "include/private/SkFloatingPoint.h"
11 #include "src/core/SkRasterPipeline.h"
12 #include "src/core/SkReadBuffer.h"
13 #include "src/core/SkWriteBuffer.h"
14
15 #include <utility>
16
17 #ifdef SK_ENABLE_SKSL
18 #include "src/core/SkKeyHelpers.h"
19 #endif
20
21 // Please see https://skia.org/dev/design/conical for how our shader works.
22
23 bool SkTwoPointConicalGradient::FocalData::set(SkScalar r0, SkScalar r1, SkMatrix* matrix) {
24     fIsSwapped = false;
25     fFocalX = sk_ieee_float_divide(r0, (r0 - r1));
26     if (SkScalarNearlyZero(fFocalX - 1)) {
27         // swap r0, r1
28         matrix->postTranslate(-1, 0);
29         matrix->postScale(-1, 1);
30         std::swap(r0, r1);
31         fFocalX = 0; // because r0 is now 0
32         fIsSwapped = true;
33     }
34
35     // Map {focal point, (1, 0)} to {(0, 0), (1, 0)}
36     const SkPoint from[2]   = { {fFocalX, 0}, {1, 0} };
37     const SkPoint to[2]     = { {0, 0}, {1, 0} };
38     SkMatrix focalMatrix;
39     if (!focalMatrix.setPolyToPoly(from, to, 2)) {
40         return false;
41     }
42     matrix->postConcat(focalMatrix);
43     fR1 = r1 / SkScalarAbs(1 - fFocalX); // focalMatrix has a scale of 1/(1-f)
44
45     // The following transformations are just to accelerate the shader computation by saving
46     // some arithmatic operations.
47     if (this->isFocalOnCircle()) {
48         matrix->postScale(0.5, 0.5);
49     } else {
50         matrix->postScale(fR1 / (fR1 * fR1 - 1), 1 / sqrt(SkScalarAbs(fR1 * fR1 - 1)));
51     }
52     matrix->postScale(SkScalarAbs(1 - fFocalX), SkScalarAbs(1 - fFocalX)); // scale |1 - f|
53     return true;
54 }
55
56 sk_sp<SkShader> SkTwoPointConicalGradient::Create(const SkPoint& c0, SkScalar r0,
57                                                   const SkPoint& c1, SkScalar r1,
58                                                   const Descriptor& desc) {
59     SkMatrix gradientMatrix;
60     Type     gradientType;
61
62     if (SkScalarNearlyZero((c0 - c1).length())) {
63         if (SkScalarNearlyZero(std::max(r0, r1)) || SkScalarNearlyEqual(r0, r1)) {
64             // Degenerate case; avoid dividing by zero. Should have been caught by caller but
65             // just in case, recheck here.
66             return nullptr;
67         }
68         // Concentric case: we can pretend we're radial (with a tiny twist).
69         const SkScalar scale = sk_ieee_float_divide(1, std::max(r0, r1));
70         gradientMatrix = SkMatrix::Translate(-c1.x(), -c1.y());
71         gradientMatrix.postScale(scale, scale);
72
73         gradientType = Type::kRadial;
74     } else {
75         const SkPoint centers[2] = { c0    , c1     };
76         const SkPoint unitvec[2] = { {0, 0}, {1, 0} };
77
78         if (!gradientMatrix.setPolyToPoly(centers, unitvec, 2)) {
79             // Degenerate case.
80             return nullptr;
81         }
82
83         gradientType = SkScalarNearlyZero(r1 - r0) ? Type::kStrip : Type::kFocal;
84     }
85
86     FocalData focalData;
87     if (gradientType == Type::kFocal) {
88         const auto dCenter = (c0 - c1).length();
89         if (!focalData.set(r0 / dCenter, r1 / dCenter, &gradientMatrix)) {
90             return nullptr;
91         }
92     }
93     return sk_sp<SkShader>(new SkTwoPointConicalGradient(c0, r0, c1, r1, desc,
94                                                          gradientType, gradientMatrix, focalData));
95 }
96
97 SkTwoPointConicalGradient::SkTwoPointConicalGradient(
98         const SkPoint& start, SkScalar startRadius,
99         const SkPoint& end, SkScalar endRadius,
100         const Descriptor& desc, Type type, const SkMatrix& gradientMatrix, const FocalData& data)
101     : SkGradientShaderBase(desc, gradientMatrix)
102     , fCenter1(start)
103     , fCenter2(end)
104     , fRadius1(startRadius)
105     , fRadius2(endRadius)
106     , fType(type)
107 {
108     // this is degenerate, and should be caught by our caller
109     SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2);
110     if (type == Type::kFocal) {
111         fFocalData = data;
112     }
113 }
114
115 bool SkTwoPointConicalGradient::isOpaque() const {
116     // Because areas outside the cone are left untouched, we cannot treat the
117     // shader as opaque even if the gradient itself is opaque.
118     // TODO(junov): Compute whether the cone fills the plane crbug.com/222380
119     return false;
120 }
121
122 // Returns the original non-sorted version of the gradient
123 SkShader::GradientType SkTwoPointConicalGradient::asAGradient(GradientInfo* info) const {
124     if (info) {
125         commonAsAGradient(info);
126         info->fPoint[0] = fCenter1;
127         info->fPoint[1] = fCenter2;
128         info->fRadius[0] = fRadius1;
129         info->fRadius[1] = fRadius2;
130     }
131     return kConical_GradientType;
132 }
133
134 sk_sp<SkFlattenable> SkTwoPointConicalGradient::CreateProc(SkReadBuffer& buffer) {
135     DescriptorScope desc;
136     if (!desc.unflatten(buffer)) {
137         return nullptr;
138     }
139     SkPoint c1 = buffer.readPoint();
140     SkPoint c2 = buffer.readPoint();
141     SkScalar r1 = buffer.readScalar();
142     SkScalar r2 = buffer.readScalar();
143
144     if (!buffer.isValid()) {
145         return nullptr;
146     }
147     return SkGradientShader::MakeTwoPointConical(c1, r1, c2, r2, desc.fColors,
148                                                  std::move(desc.fColorSpace), desc.fPos,
149                                                  desc.fCount, desc.fTileMode, desc.fGradFlags,
150                                                  desc.fLocalMatrix);
151 }
152
153 void SkTwoPointConicalGradient::flatten(SkWriteBuffer& buffer) const {
154     this->INHERITED::flatten(buffer);
155     buffer.writePoint(fCenter1);
156     buffer.writePoint(fCenter2);
157     buffer.writeScalar(fRadius1);
158     buffer.writeScalar(fRadius2);
159 }
160
161 void SkTwoPointConicalGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p,
162                                                      SkRasterPipeline* postPipeline) const {
163     const auto dRadius = fRadius2 - fRadius1;
164
165     if (fType == Type::kRadial) {
166         p->append(SkRasterPipeline::xy_to_radius);
167
168         // Tiny twist: radial computes a t for [0, r2], but we want a t for [r1, r2].
169         auto scale =  std::max(fRadius1, fRadius2) / dRadius;
170         auto bias  = -fRadius1 / dRadius;
171
172         p->append_matrix(alloc, SkMatrix::Translate(bias, 0) * SkMatrix::Scale(scale, 1));
173         return;
174     }
175
176     if (fType == Type::kStrip) {
177         auto* ctx = alloc->make<SkRasterPipeline_2PtConicalCtx>();
178         SkScalar scaledR0 = fRadius1 / this->getCenterX1();
179         ctx->fP0 = scaledR0 * scaledR0;
180         p->append(SkRasterPipeline::xy_to_2pt_conical_strip, ctx);
181         p->append(SkRasterPipeline::mask_2pt_conical_nan, ctx);
182         postPipeline->append(SkRasterPipeline::apply_vector_mask, &ctx->fMask);
183         return;
184     }
185
186     auto* ctx = alloc->make<SkRasterPipeline_2PtConicalCtx>();
187     ctx->fP0 = 1/fFocalData.fR1;
188     ctx->fP1 = fFocalData.fFocalX;
189
190     if (fFocalData.isFocalOnCircle()) {
191         p->append(SkRasterPipeline::xy_to_2pt_conical_focal_on_circle);
192     } else if (fFocalData.isWellBehaved()) {
193         p->append(SkRasterPipeline::xy_to_2pt_conical_well_behaved, ctx);
194     } else if (fFocalData.isSwapped() || 1 - fFocalData.fFocalX < 0) {
195         p->append(SkRasterPipeline::xy_to_2pt_conical_smaller, ctx);
196     } else {
197         p->append(SkRasterPipeline::xy_to_2pt_conical_greater, ctx);
198     }
199
200     if (!fFocalData.isWellBehaved()) {
201         p->append(SkRasterPipeline::mask_2pt_conical_degenerates, ctx);
202     }
203     if (1 - fFocalData.fFocalX < 0) {
204         p->append(SkRasterPipeline::negate_x);
205     }
206     if (!fFocalData.isNativelyFocal()) {
207         p->append(SkRasterPipeline::alter_2pt_conical_compensate_focal, ctx);
208     }
209     if (fFocalData.isSwapped()) {
210         p->append(SkRasterPipeline::alter_2pt_conical_unswap);
211     }
212     if (!fFocalData.isWellBehaved()) {
213         postPipeline->append(SkRasterPipeline::apply_vector_mask, &ctx->fMask);
214     }
215 }
216
217 skvm::F32 SkTwoPointConicalGradient::transformT(skvm::Builder* p, skvm::Uniforms* uniforms,
218                                                 skvm::Coord coord, skvm::I32* mask) const {
219     auto mag = [](skvm::F32 x, skvm::F32 y) { return sqrt(x*x + y*y); };
220
221     // See https://skia.org/dev/design/conical, and onAppendStages() above.
222     // There's a lot going on here, and I'm not really sure what's independent
223     // or disjoint, what can be reordered, simplified, etc.  Tweak carefully.
224
225     const skvm::F32 x = coord.x,
226                     y = coord.y;
227     if (fType == Type::kRadial) {
228         float denom = 1.0f / (fRadius2 - fRadius1),
229               scale = std::max(fRadius1, fRadius2) * denom,
230                bias =                  -fRadius1 * denom;
231         return mag(x,y) * p->uniformF(uniforms->pushF(scale))
232                         + p->uniformF(uniforms->pushF(bias ));
233     }
234
235     if (fType == Type::kStrip) {
236         float r = fRadius1 / this->getCenterX1();
237         skvm::F32 t = x + sqrt(p->uniformF(uniforms->pushF(r*r)) - y*y);
238
239         *mask = (t == t);   // t != NaN
240         return t;
241     }
242
243     const skvm::F32 invR1 = p->uniformF(uniforms->pushF(1 / fFocalData.fR1));
244
245     skvm::F32 t;
246     if (fFocalData.isFocalOnCircle()) {
247         t = (y/x) * y + x;       // (x^2 + y^2) / x  ~~>  x + y^2/x  ~~>  y/x * y + x
248     } else if (fFocalData.isWellBehaved()) {
249         t = mag(x,y) - x*invR1;
250     } else {
251         skvm::F32 k = sqrt(x*x - y*y);
252         if (fFocalData.isSwapped() || 1 - fFocalData.fFocalX < 0) {
253             k = -k;
254         }
255         t = k - x*invR1;
256     }
257
258     if (!fFocalData.isWellBehaved()) {
259         // TODO: not sure why we consider t == 0 degenerate
260         *mask = (t > 0.0f);  // and implicitly, t != NaN
261     }
262
263     const skvm::F32 focalX = p->uniformF(uniforms->pushF(fFocalData.fFocalX));
264     if (1 - fFocalData.fFocalX < 0)    { t = -t; }
265     if (!fFocalData.isNativelyFocal()) { t += focalX; }
266     if ( fFocalData.isSwapped())       { t = 1.0f - t; }
267     return t;
268 }
269
270 /////////////////////////////////////////////////////////////////////
271
272 #if SK_SUPPORT_GPU
273
274 #include "src/gpu/ganesh/gradients/GrGradientShader.h"
275
276 std::unique_ptr<GrFragmentProcessor> SkTwoPointConicalGradient::asFragmentProcessor(
277         const GrFPArgs& args) const {
278     return GrGradientShader::MakeConical(*this, args);
279 }
280
281 #endif
282
283 #ifdef SK_ENABLE_SKSL
284 void SkTwoPointConicalGradient::addToKey(const SkKeyContext& keyContext,
285                                          SkPaintParamsKeyBuilder* builder,
286                                          SkPipelineDataGatherer* gatherer) const {
287     GradientShaderBlocks::GradientData data(kConical_GradientType,
288                                             SkM44(this->getLocalMatrix()),
289                                             fCenter1, fCenter2,
290                                             fRadius1, fRadius2,
291                                             0.0f, 0.0f,
292                                             fTileMode,
293                                             fColorCount,
294                                             fOrigColors4f,
295                                             fOrigPos);
296
297     GradientShaderBlocks::AddToKey(keyContext, builder, gatherer, data);
298 }
299 #endif