blob: 811d719b9d59ed59cb6f6a5be9ee1d4c65e9a5c5 [file] [log] [blame]
rileya@google.com589708b2012-07-26 20:04:23 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/SkFloatingPoint.h"
9#include "src/core/SkRasterPipeline.h"
10#include "src/core/SkReadBuffer.h"
11#include "src/core/SkWriteBuffer.h"
12#include "src/shaders/gradients/SkTwoPointConicalGradient.h"
Florin Malitaa66ef2d2017-06-28 10:02:40 -040013
Ben Wagnerf08d1d02018-06-18 15:11:00 -040014#include <utility>
15
Yuqian Lid208a882018-01-04 10:08:42 -050016// Please see https://skia.org/dev/design/conical for how our shader works.
17
Mike Reeda2f14de2018-05-09 14:01:00 -040018bool SkTwoPointConicalGradient::FocalData::set(SkScalar r0, SkScalar r1, SkMatrix* matrix) {
Yuqian Lid208a882018-01-04 10:08:42 -050019 fIsSwapped = false;
Mike Reeda2f14de2018-05-09 14:01:00 -040020 fFocalX = sk_ieee_float_divide(r0, (r0 - r1));
Yuqian Lid208a882018-01-04 10:08:42 -050021 if (SkScalarNearlyZero(fFocalX - 1)) {
22 // swap r0, r1
Mike Reeda2f14de2018-05-09 14:01:00 -040023 matrix->postTranslate(-1, 0);
24 matrix->postScale(-1, 1);
Yuqian Lid208a882018-01-04 10:08:42 -050025 std::swap(r0, r1);
26 fFocalX = 0; // because r0 is now 0
27 fIsSwapped = true;
28 }
29
30 // Map {focal point, (1, 0)} to {(0, 0), (1, 0)}
31 const SkPoint from[2] = { {fFocalX, 0}, {1, 0} };
32 const SkPoint to[2] = { {0, 0}, {1, 0} };
33 SkMatrix focalMatrix;
34 if (!focalMatrix.setPolyToPoly(from, to, 2)) {
Mike Reeda2f14de2018-05-09 14:01:00 -040035 return false;
Yuqian Lid208a882018-01-04 10:08:42 -050036 }
Mike Reeda2f14de2018-05-09 14:01:00 -040037 matrix->postConcat(focalMatrix);
Yuqian Lid208a882018-01-04 10:08:42 -050038 fR1 = r1 / SkScalarAbs(1 - fFocalX); // focalMatrix has a scale of 1/(1-f)
39
40 // The following transformations are just to accelerate the shader computation by saving
41 // some arithmatic operations.
42 if (this->isFocalOnCircle()) {
Mike Reeda2f14de2018-05-09 14:01:00 -040043 matrix->postScale(0.5, 0.5);
Yuqian Lid208a882018-01-04 10:08:42 -050044 } else {
Mike Reeda2f14de2018-05-09 14:01:00 -040045 matrix->postScale(fR1 / (fR1 * fR1 - 1), 1 / sqrt(SkScalarAbs(fR1 * fR1 - 1)));
Yuqian Lid208a882018-01-04 10:08:42 -050046 }
Mike Reeda2f14de2018-05-09 14:01:00 -040047 matrix->postScale(SkScalarAbs(1 - fFocalX), SkScalarAbs(1 - fFocalX)); // scale |1 - f|
48 return true;
Yuqian Lid208a882018-01-04 10:08:42 -050049}
50
Florin Malita9c2212f2017-07-29 18:23:10 -040051sk_sp<SkShader> SkTwoPointConicalGradient::Create(const SkPoint& c0, SkScalar r0,
52 const SkPoint& c1, SkScalar r1,
Florin Malita5f379a82017-10-18 16:22:35 -040053 const Descriptor& desc) {
Florin Malita9c2212f2017-07-29 18:23:10 -040054 SkMatrix gradientMatrix;
55 Type gradientType;
56
57 if (SkScalarNearlyZero((c0 - c1).length())) {
Brian Osman788b9162020-02-07 10:36:46 -050058 if (SkScalarNearlyZero(std::max(r0, r1)) || SkScalarNearlyEqual(r0, r1)) {
Mike Klein024072a2018-11-11 00:26:30 +000059 // Degenerate case; avoid dividing by zero. Should have been caught by caller but
60 // just in case, recheck here.
61 return nullptr;
Yuqian Li45a6d712018-05-14 09:39:24 -070062 }
Florin Malita9c2212f2017-07-29 18:23:10 -040063 // Concentric case: we can pretend we're radial (with a tiny twist).
Brian Osman788b9162020-02-07 10:36:46 -050064 const SkScalar scale = sk_ieee_float_divide(1, std::max(r0, r1));
Florin Malita9c2212f2017-07-29 18:23:10 -040065 gradientMatrix = SkMatrix::MakeTrans(-c1.x(), -c1.y());
Florin Malita5f379a82017-10-18 16:22:35 -040066 gradientMatrix.postScale(scale, scale);
Florin Malita9c2212f2017-07-29 18:23:10 -040067
68 gradientType = Type::kRadial;
69 } else {
70 const SkPoint centers[2] = { c0 , c1 };
71 const SkPoint unitvec[2] = { {0, 0}, {1, 0} };
72
73 if (!gradientMatrix.setPolyToPoly(centers, unitvec, 2)) {
74 // Degenerate case.
75 return nullptr;
76 }
77
Yuqian Lid208a882018-01-04 10:08:42 -050078 gradientType = SkScalarNearlyZero(r1 - r0) ? Type::kStrip : Type::kFocal;
Florin Malita9c2212f2017-07-29 18:23:10 -040079 }
80
Yuqian Lid208a882018-01-04 10:08:42 -050081 FocalData focalData;
82 if (gradientType == Type::kFocal) {
83 const auto dCenter = (c0 - c1).length();
Mike Reeda2f14de2018-05-09 14:01:00 -040084 if (!focalData.set(r0 / dCenter, r1 / dCenter, &gradientMatrix)) {
85 return nullptr;
86 }
Yuqian Lid208a882018-01-04 10:08:42 -050087 }
Florin Malita5f379a82017-10-18 16:22:35 -040088 return sk_sp<SkShader>(new SkTwoPointConicalGradient(c0, r0, c1, r1, desc,
Yuqian Lid208a882018-01-04 10:08:42 -050089 gradientType, gradientMatrix, focalData));
Florin Malita9c2212f2017-07-29 18:23:10 -040090}
91
rileya@google.com589708b2012-07-26 20:04:23 +000092SkTwoPointConicalGradient::SkTwoPointConicalGradient(
reed@google.com3d3a8602013-05-24 14:58:44 +000093 const SkPoint& start, SkScalar startRadius,
94 const SkPoint& end, SkScalar endRadius,
Yuqian Lid208a882018-01-04 10:08:42 -050095 const Descriptor& desc, Type type, const SkMatrix& gradientMatrix, const FocalData& data)
Florin Malita9c2212f2017-07-29 18:23:10 -040096 : SkGradientShaderBase(desc, gradientMatrix)
reedaddf2ed2014-08-11 08:28:24 -070097 , fCenter1(start)
98 , fCenter2(end)
99 , fRadius1(startRadius)
100 , fRadius2(endRadius)
Florin Malita9c2212f2017-07-29 18:23:10 -0400101 , fType(type)
reedaddf2ed2014-08-11 08:28:24 -0700102{
rileya@google.com589708b2012-07-26 20:04:23 +0000103 // this is degenerate, and should be caught by our caller
104 SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2);
Yuqian Lid208a882018-01-04 10:08:42 -0500105 if (type == Type::kFocal) {
106 fFocalData = data;
107 }
rileya@google.com589708b2012-07-26 20:04:23 +0000108}
109
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000110bool SkTwoPointConicalGradient::isOpaque() const {
robertphillips@google.comcb6d97c2013-07-09 13:50:09 +0000111 // Because areas outside the cone are left untouched, we cannot treat the
112 // shader as opaque even if the gradient itself is opaque.
113 // TODO(junov): Compute whether the cone fills the plane crbug.com/222380
114 return false;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000115}
116
commit-bot@chromium.org44d83c12014-04-21 13:10:25 +0000117// Returns the original non-sorted version of the gradient
Florin Malita5f379a82017-10-18 16:22:35 -0400118SkShader::GradientType SkTwoPointConicalGradient::asAGradient(GradientInfo* info) const {
rileya@google.com589708b2012-07-26 20:04:23 +0000119 if (info) {
Florin Malita5f379a82017-10-18 16:22:35 -0400120 commonAsAGradient(info);
rileya@google.com589708b2012-07-26 20:04:23 +0000121 info->fPoint[0] = fCenter1;
122 info->fPoint[1] = fCenter2;
123 info->fRadius[0] = fRadius1;
124 info->fRadius[1] = fRadius2;
125 }
126 return kConical_GradientType;
127}
128
reed60c9b582016-04-03 09:11:13 -0700129sk_sp<SkFlattenable> SkTwoPointConicalGradient::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700130 DescriptorScope desc;
131 if (!desc.unflatten(buffer)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700132 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700133 }
134 SkPoint c1 = buffer.readPoint();
135 SkPoint c2 = buffer.readPoint();
136 SkScalar r1 = buffer.readScalar();
137 SkScalar r2 = buffer.readScalar();
138
Mike Reed72a0c0c2019-07-30 15:03:22 -0400139 if (buffer.isVersionLT(SkPicturePriv::k2PtConicalNoFlip_Version) && buffer.readBool()) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400140 using std::swap;
Florin Malita5f379a82017-10-18 16:22:35 -0400141 // legacy flipped gradient
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400142 swap(c1, c2);
143 swap(r1, r2);
reed9fa60da2014-08-21 07:59:51 -0700144
brianosmane25d71c2016-09-28 11:27:28 -0700145 SkColor4f* colors = desc.mutableColors();
reed9fa60da2014-08-21 07:59:51 -0700146 SkScalar* pos = desc.mutablePos();
147 const int last = desc.fCount - 1;
148 const int half = desc.fCount >> 1;
149 for (int i = 0; i < half; ++i) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400150 swap(colors[i], colors[last - i]);
reed9fa60da2014-08-21 07:59:51 -0700151 if (pos) {
152 SkScalar tmp = pos[i];
153 pos[i] = SK_Scalar1 - pos[last - i];
154 pos[last - i] = SK_Scalar1 - tmp;
155 }
156 }
157 if (pos) {
158 if (desc.fCount & 1) {
159 pos[half] = SK_Scalar1 - pos[half];
160 }
161 }
162 }
Kevin Lubickdaebae92018-05-17 11:29:10 -0400163 if (!buffer.isValid()) {
164 return nullptr;
165 }
brianosmane25d71c2016-09-28 11:27:28 -0700166 return SkGradientShader::MakeTwoPointConical(c1, r1, c2, r2, desc.fColors,
167 std::move(desc.fColorSpace), desc.fPos,
reed8a21c9f2016-03-08 18:50:00 -0800168 desc.fCount, desc.fTileMode, desc.fGradFlags,
reed60c9b582016-04-03 09:11:13 -0700169 desc.fLocalMatrix);
reed9fa60da2014-08-21 07:59:51 -0700170}
171
172void SkTwoPointConicalGradient::flatten(SkWriteBuffer& buffer) const {
rileya@google.com589708b2012-07-26 20:04:23 +0000173 this->INHERITED::flatten(buffer);
174 buffer.writePoint(fCenter1);
175 buffer.writePoint(fCenter2);
176 buffer.writeScalar(fRadius1);
177 buffer.writeScalar(fRadius2);
178}
179
Florin Malita50b20842017-07-29 19:08:28 -0400180void SkTwoPointConicalGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p,
181 SkRasterPipeline* postPipeline) const {
Florin Malitaa66ef2d2017-06-28 10:02:40 -0400182 const auto dRadius = fRadius2 - fRadius1;
Florin Malitaa66ef2d2017-06-28 10:02:40 -0400183
Florin Malita9c2212f2017-07-29 18:23:10 -0400184 if (fType == Type::kRadial) {
Florin Malita0bb04112017-06-27 14:35:50 -0400185 p->append(SkRasterPipeline::xy_to_radius);
186
187 // Tiny twist: radial computes a t for [0, r2], but we want a t for [r1, r2].
Brian Osman788b9162020-02-07 10:36:46 -0500188 auto scale = std::max(fRadius1, fRadius2) / dRadius;
Florin Malitaa66ef2d2017-06-28 10:02:40 -0400189 auto bias = -fRadius1 / dRadius;
Florin Malita0bb04112017-06-27 14:35:50 -0400190
Mike Reed6b59bf42017-07-03 21:26:44 -0400191 p->append_matrix(alloc, SkMatrix::Concat(SkMatrix::MakeTrans(bias, 0),
192 SkMatrix::MakeScale(scale, 1)));
Florin Malita50b20842017-07-29 19:08:28 -0400193 return;
Florin Malita0bb04112017-06-27 14:35:50 -0400194 }
195
Yuqian Lid208a882018-01-04 10:08:42 -0500196 if (fType == Type::kStrip) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400197 auto* ctx = alloc->make<SkRasterPipeline_2PtConicalCtx>();
Yuqian Lid208a882018-01-04 10:08:42 -0500198 SkScalar scaledR0 = fRadius1 / this->getCenterX1();
199 ctx->fP0 = scaledR0 * scaledR0;
200 p->append(SkRasterPipeline::xy_to_2pt_conical_strip, ctx);
201 p->append(SkRasterPipeline::mask_2pt_conical_nan, ctx);
202 postPipeline->append(SkRasterPipeline::apply_vector_mask, &ctx->fMask);
203 return;
204 }
205
Mike Kleinb11ab572018-10-24 06:42:14 -0400206 auto* ctx = alloc->make<SkRasterPipeline_2PtConicalCtx>();
Yuqian Lid208a882018-01-04 10:08:42 -0500207 ctx->fP0 = 1/fFocalData.fR1;
208 ctx->fP1 = fFocalData.fFocalX;
209
210 if (fFocalData.isFocalOnCircle()) {
211 p->append(SkRasterPipeline::xy_to_2pt_conical_focal_on_circle);
212 } else if (fFocalData.isWellBehaved()) {
213 p->append(SkRasterPipeline::xy_to_2pt_conical_well_behaved, ctx);
214 } else if (fFocalData.isSwapped() || 1 - fFocalData.fFocalX < 0) {
215 p->append(SkRasterPipeline::xy_to_2pt_conical_smaller, ctx);
216 } else {
217 p->append(SkRasterPipeline::xy_to_2pt_conical_greater, ctx);
218 }
219
220 if (!fFocalData.isWellBehaved()) {
221 p->append(SkRasterPipeline::mask_2pt_conical_degenerates, ctx);
222 }
223 if (1 - fFocalData.fFocalX < 0) {
224 p->append(SkRasterPipeline::negate_x);
225 }
226 if (!fFocalData.isNativelyFocal()) {
227 p->append(SkRasterPipeline::alter_2pt_conical_compensate_focal, ctx);
228 }
229 if (fFocalData.isSwapped()) {
230 p->append(SkRasterPipeline::alter_2pt_conical_unswap);
231 }
232 if (!fFocalData.isWellBehaved()) {
233 postPipeline->append(SkRasterPipeline::apply_vector_mask, &ctx->fMask);
234 }
Florin Malita0bb04112017-06-27 14:35:50 -0400235}
Michael Ludwigafebe162018-09-12 15:24:34 -0400236
Mike Kleince9e0602020-01-29 09:47:44 -0600237skvm::F32 SkTwoPointConicalGradient::transformT(skvm::Builder* p, skvm::Uniforms* uniforms,
238 skvm::F32 x, skvm::F32 y, skvm::I32* mask) const {
Mike Kleincaf5ee42020-01-28 16:11:34 -0600239 // See https://skia.org/dev/design/conical, and onAppendStages() above.
Mike Kleince9e0602020-01-29 09:47:44 -0600240 // There's a lot going on here, and I'm not really sure what's independent
241 // or disjoint, what can be reordered, simplified, etc. Tweak carefully.
Mike Kleincaf5ee42020-01-28 16:11:34 -0600242
243 if (fType == Type::kRadial) {
Mike Kleincaf5ee42020-01-28 16:11:34 -0600244 float denom = 1.0f / (fRadius2 - fRadius1),
Brian Osman788b9162020-02-07 10:36:46 -0500245 scale = std::max(fRadius1, fRadius2) * denom,
Mike Kleincaf5ee42020-01-28 16:11:34 -0600246 bias = -fRadius1 * denom;
Mike Reedb6e7ef12020-04-03 13:34:26 -0400247 return norm(x,y) * p->uniformF(uniforms->pushF(scale))
248 + p->uniformF(uniforms->pushF(bias ));
Mike Kleincaf5ee42020-01-28 16:11:34 -0600249 }
250
251 if (fType == Type::kStrip) {
252 float r = fRadius1 / this->getCenterX1();
Mike Reedb6e7ef12020-04-03 13:34:26 -0400253 skvm::F32 t = x + sqrt(p->splat(r*r) - y*y);
Mike Kleince9e0602020-01-29 09:47:44 -0600254
Mike Reedb6e7ef12020-04-03 13:34:26 -0400255 *mask = (t == t); // t != NaN
Mike Kleince9e0602020-01-29 09:47:44 -0600256 return t;
Mike Kleincaf5ee42020-01-28 16:11:34 -0600257 }
258
Mike Kleince9e0602020-01-29 09:47:44 -0600259 const skvm::F32 invR1 = p->uniformF(uniforms->pushF(1 / fFocalData.fR1));
260
261 skvm::F32 t;
262 if (fFocalData.isFocalOnCircle()) {
Mike Reedb6e7ef12020-04-03 13:34:26 -0400263 t = (y/x) * y + x; // (x^2 + y^2) / x ~~> x + y^2/x ~~> y/x * y + x
Mike Kleince9e0602020-01-29 09:47:44 -0600264 } else if (fFocalData.isWellBehaved()) {
Mike Reedb6e7ef12020-04-03 13:34:26 -0400265 t = norm(x,y) - x*invR1;
Mike Kleince9e0602020-01-29 09:47:44 -0600266 } else {
Mike Reedb6e7ef12020-04-03 13:34:26 -0400267 skvm::F32 k = sqrt(x*x - y*y);
Mike Kleince9e0602020-01-29 09:47:44 -0600268 if (fFocalData.isSwapped() || 1 - fFocalData.fFocalX < 0) {
Mike Kleind0b568b2020-03-30 08:11:15 -0500269 k = -k;
Mike Kleince9e0602020-01-29 09:47:44 -0600270 }
Mike Reedb6e7ef12020-04-03 13:34:26 -0400271 t = k - x*invR1;
Mike Kleince9e0602020-01-29 09:47:44 -0600272 }
273
274 if (!fFocalData.isWellBehaved()) {
275 // TODO: not sure why we consider t == 0 degenerate
Mike Reedb6e7ef12020-04-03 13:34:26 -0400276 *mask = (t > 0.0f); // and implicitly, t != NaN
Mike Kleince9e0602020-01-29 09:47:44 -0600277 }
278
279 const skvm::F32 focalX = p->uniformF(uniforms->pushF(fFocalData.fFocalX));
Mike Kleind0b568b2020-03-30 08:11:15 -0500280 if (1 - fFocalData.fFocalX < 0) { t = -t; }
Mike Reedb6e7ef12020-04-03 13:34:26 -0400281 if (!fFocalData.isNativelyFocal()) { t += focalX; }
282 if ( fFocalData.isSwapped()) { t = 1.0f - t; }
Mike Kleince9e0602020-01-29 09:47:44 -0600283 return t;
Mike Kleincaf5ee42020-01-28 16:11:34 -0600284}
285
Michael Ludwigafebe162018-09-12 15:24:34 -0400286/////////////////////////////////////////////////////////////////////
287
288#if SK_SUPPORT_GPU
289
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500290#include "src/gpu/gradients/GrGradientShader.h"
Michael Ludwigafebe162018-09-12 15:24:34 -0400291
292std::unique_ptr<GrFragmentProcessor> SkTwoPointConicalGradient::asFragmentProcessor(
293 const GrFPArgs& args) const {
294 return GrGradientShader::MakeConical(*this, args);
295}
296
297#endif