2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/shaders/gradients/SkTwoPointConicalGradient.h"
10 #include "include/private/SkFloatingPoint.h"
11 #include "src/core/SkRasterPipeline.h"
12 #include "src/core/SkReadBuffer.h"
13 #include "src/core/SkWriteBuffer.h"
18 #include "src/core/SkKeyHelpers.h"
21 // Please see https://skia.org/dev/design/conical for how our shader works.
23 bool SkTwoPointConicalGradient::FocalData::set(SkScalar r0, SkScalar r1, SkMatrix* matrix) {
25 fFocalX = sk_ieee_float_divide(r0, (r0 - r1));
26 if (SkScalarNearlyZero(fFocalX - 1)) {
28 matrix->postTranslate(-1, 0);
29 matrix->postScale(-1, 1);
31 fFocalX = 0; // because r0 is now 0
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} };
39 if (!focalMatrix.setPolyToPoly(from, to, 2)) {
42 matrix->postConcat(focalMatrix);
43 fR1 = r1 / SkScalarAbs(1 - fFocalX); // focalMatrix has a scale of 1/(1-f)
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);
50 matrix->postScale(fR1 / (fR1 * fR1 - 1), 1 / sqrt(SkScalarAbs(fR1 * fR1 - 1)));
52 matrix->postScale(SkScalarAbs(1 - fFocalX), SkScalarAbs(1 - fFocalX)); // scale |1 - f|
56 sk_sp<SkShader> SkTwoPointConicalGradient::Create(const SkPoint& c0, SkScalar r0,
57 const SkPoint& c1, SkScalar r1,
58 const Descriptor& desc) {
59 SkMatrix gradientMatrix;
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.
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);
73 gradientType = Type::kRadial;
75 const SkPoint centers[2] = { c0 , c1 };
76 const SkPoint unitvec[2] = { {0, 0}, {1, 0} };
78 if (!gradientMatrix.setPolyToPoly(centers, unitvec, 2)) {
83 gradientType = SkScalarNearlyZero(r1 - r0) ? Type::kStrip : Type::kFocal;
87 if (gradientType == Type::kFocal) {
88 const auto dCenter = (c0 - c1).length();
89 if (!focalData.set(r0 / dCenter, r1 / dCenter, &gradientMatrix)) {
93 return sk_sp<SkShader>(new SkTwoPointConicalGradient(c0, r0, c1, r1, desc,
94 gradientType, gradientMatrix, focalData));
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)
104 , fRadius1(startRadius)
105 , fRadius2(endRadius)
108 // this is degenerate, and should be caught by our caller
109 SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2);
110 if (type == Type::kFocal) {
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
122 // Returns the original non-sorted version of the gradient
123 SkShader::GradientType SkTwoPointConicalGradient::asAGradient(GradientInfo* info) const {
125 commonAsAGradient(info);
126 info->fPoint[0] = fCenter1;
127 info->fPoint[1] = fCenter2;
128 info->fRadius[0] = fRadius1;
129 info->fRadius[1] = fRadius2;
131 return kConical_GradientType;
134 sk_sp<SkFlattenable> SkTwoPointConicalGradient::CreateProc(SkReadBuffer& buffer) {
135 DescriptorScope desc;
136 if (!desc.unflatten(buffer)) {
139 SkPoint c1 = buffer.readPoint();
140 SkPoint c2 = buffer.readPoint();
141 SkScalar r1 = buffer.readScalar();
142 SkScalar r2 = buffer.readScalar();
144 if (!buffer.isValid()) {
147 return SkGradientShader::MakeTwoPointConical(c1, r1, c2, r2, desc.fColors,
148 std::move(desc.fColorSpace), desc.fPos,
149 desc.fCount, desc.fTileMode, desc.fGradFlags,
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);
161 void SkTwoPointConicalGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p,
162 SkRasterPipeline* postPipeline) const {
163 const auto dRadius = fRadius2 - fRadius1;
165 if (fType == Type::kRadial) {
166 p->append(SkRasterPipeline::xy_to_radius);
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;
172 p->append_matrix(alloc, SkMatrix::Translate(bias, 0) * SkMatrix::Scale(scale, 1));
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);
186 auto* ctx = alloc->make<SkRasterPipeline_2PtConicalCtx>();
187 ctx->fP0 = 1/fFocalData.fR1;
188 ctx->fP1 = fFocalData.fFocalX;
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);
197 p->append(SkRasterPipeline::xy_to_2pt_conical_greater, ctx);
200 if (!fFocalData.isWellBehaved()) {
201 p->append(SkRasterPipeline::mask_2pt_conical_degenerates, ctx);
203 if (1 - fFocalData.fFocalX < 0) {
204 p->append(SkRasterPipeline::negate_x);
206 if (!fFocalData.isNativelyFocal()) {
207 p->append(SkRasterPipeline::alter_2pt_conical_compensate_focal, ctx);
209 if (fFocalData.isSwapped()) {
210 p->append(SkRasterPipeline::alter_2pt_conical_unswap);
212 if (!fFocalData.isWellBehaved()) {
213 postPipeline->append(SkRasterPipeline::apply_vector_mask, &ctx->fMask);
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); };
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.
225 const skvm::F32 x = coord.x,
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 ));
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);
239 *mask = (t == t); // t != NaN
243 const skvm::F32 invR1 = p->uniformF(uniforms->pushF(1 / fFocalData.fR1));
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;
251 skvm::F32 k = sqrt(x*x - y*y);
252 if (fFocalData.isSwapped() || 1 - fFocalData.fFocalX < 0) {
258 if (!fFocalData.isWellBehaved()) {
259 // TODO: not sure why we consider t == 0 degenerate
260 *mask = (t > 0.0f); // and implicitly, t != NaN
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; }
270 /////////////////////////////////////////////////////////////////////
274 #include "src/gpu/ganesh/gradients/GrGradientShader.h"
276 std::unique_ptr<GrFragmentProcessor> SkTwoPointConicalGradient::asFragmentProcessor(
277 const GrFPArgs& args) const {
278 return GrGradientShader::MakeConical(*this, args);
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()),
297 GradientShaderBlocks::AddToKey(keyContext, builder, gatherer, data);