blob: 92cd494a8923759f1e0bb0753f868523643092ad [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())) {
Mike Klein024072a2018-11-11 00:26:30 +000058 if (SkScalarNearlyZero(SkTMax(r0, r1)) || SkScalarNearlyEqual(r0, r1)) {
59 // 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).
Mike Reed0b510fb2018-05-03 12:50:38 -040064 const SkScalar scale = sk_ieee_float_divide(1, SkTMax(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
Florin Malita5f379a82017-10-18 16:22:35 -0400139 if (buffer.isVersionLT(SkReadBuffer::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].
Florin Malita5f379a82017-10-18 16:22:35 -0400188 auto scale = SkTMax(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
237/////////////////////////////////////////////////////////////////////
238
239#if SK_SUPPORT_GPU
240
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500241#include "src/gpu/gradients/GrGradientShader.h"
Michael Ludwigafebe162018-09-12 15:24:34 -0400242
243std::unique_ptr<GrFragmentProcessor> SkTwoPointConicalGradient::asFragmentProcessor(
244 const GrFPArgs& args) const {
245 return GrGradientShader::MakeConical(*this, args);
246}
247
248#endif