blob: 48877da34c2fd6de9f9cbf56511bc87b925735f7 [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 Reed0b510fb2018-05-03 12:50:38 -04008#include "SkFloatingPoint.h"
Florin Malitaa66ef2d2017-06-28 10:02:40 -04009#include "SkRasterPipeline.h"
Florin Malitad4e9ec82017-10-25 18:00:26 -040010#include "SkReadBuffer.h"
Mike Reed0b510fb2018-05-03 12:50:38 -040011#include "SkTwoPointConicalGradient.h"
Florin Malitad4e9ec82017-10-25 18:00:26 -040012#include "SkWriteBuffer.h"
Florin Malitaa66ef2d2017-06-28 10:02:40 -040013#include "../../jumper/SkJumper.h"
14
Yuqian Lid208a882018-01-04 10:08:42 -050015// Please see https://skia.org/dev/design/conical for how our shader works.
16
Mike Reeda2f14de2018-05-09 14:01:00 -040017bool SkTwoPointConicalGradient::FocalData::set(SkScalar r0, SkScalar r1, SkMatrix* matrix) {
Yuqian Lid208a882018-01-04 10:08:42 -050018 fIsSwapped = false;
Mike Reeda2f14de2018-05-09 14:01:00 -040019 fFocalX = sk_ieee_float_divide(r0, (r0 - r1));
Yuqian Lid208a882018-01-04 10:08:42 -050020 if (SkScalarNearlyZero(fFocalX - 1)) {
21 // swap r0, r1
Mike Reeda2f14de2018-05-09 14:01:00 -040022 matrix->postTranslate(-1, 0);
23 matrix->postScale(-1, 1);
Yuqian Lid208a882018-01-04 10:08:42 -050024 std::swap(r0, r1);
25 fFocalX = 0; // because r0 is now 0
26 fIsSwapped = true;
27 }
28
29 // Map {focal point, (1, 0)} to {(0, 0), (1, 0)}
30 const SkPoint from[2] = { {fFocalX, 0}, {1, 0} };
31 const SkPoint to[2] = { {0, 0}, {1, 0} };
32 SkMatrix focalMatrix;
33 if (!focalMatrix.setPolyToPoly(from, to, 2)) {
Mike Reeda2f14de2018-05-09 14:01:00 -040034 return false;
Yuqian Lid208a882018-01-04 10:08:42 -050035 }
Mike Reeda2f14de2018-05-09 14:01:00 -040036 matrix->postConcat(focalMatrix);
Yuqian Lid208a882018-01-04 10:08:42 -050037 fR1 = r1 / SkScalarAbs(1 - fFocalX); // focalMatrix has a scale of 1/(1-f)
38
39 // The following transformations are just to accelerate the shader computation by saving
40 // some arithmatic operations.
41 if (this->isFocalOnCircle()) {
Mike Reeda2f14de2018-05-09 14:01:00 -040042 matrix->postScale(0.5, 0.5);
Yuqian Lid208a882018-01-04 10:08:42 -050043 } else {
Mike Reeda2f14de2018-05-09 14:01:00 -040044 matrix->postScale(fR1 / (fR1 * fR1 - 1), 1 / sqrt(SkScalarAbs(fR1 * fR1 - 1)));
Yuqian Lid208a882018-01-04 10:08:42 -050045 }
Mike Reeda2f14de2018-05-09 14:01:00 -040046 matrix->postScale(SkScalarAbs(1 - fFocalX), SkScalarAbs(1 - fFocalX)); // scale |1 - f|
47 return true;
Yuqian Lid208a882018-01-04 10:08:42 -050048}
49
Florin Malita9c2212f2017-07-29 18:23:10 -040050sk_sp<SkShader> SkTwoPointConicalGradient::Create(const SkPoint& c0, SkScalar r0,
51 const SkPoint& c1, SkScalar r1,
Florin Malita5f379a82017-10-18 16:22:35 -040052 const Descriptor& desc) {
Florin Malita9c2212f2017-07-29 18:23:10 -040053 SkMatrix gradientMatrix;
54 Type gradientType;
55
56 if (SkScalarNearlyZero((c0 - c1).length())) {
57 // Concentric case: we can pretend we're radial (with a tiny twist).
Mike Reed0b510fb2018-05-03 12:50:38 -040058 const SkScalar scale = sk_ieee_float_divide(1, SkTMax(r0, r1));
Florin Malita9c2212f2017-07-29 18:23:10 -040059 gradientMatrix = SkMatrix::MakeTrans(-c1.x(), -c1.y());
Florin Malita5f379a82017-10-18 16:22:35 -040060 gradientMatrix.postScale(scale, scale);
Florin Malita9c2212f2017-07-29 18:23:10 -040061
62 gradientType = Type::kRadial;
63 } else {
64 const SkPoint centers[2] = { c0 , c1 };
65 const SkPoint unitvec[2] = { {0, 0}, {1, 0} };
66
67 if (!gradientMatrix.setPolyToPoly(centers, unitvec, 2)) {
68 // Degenerate case.
69 return nullptr;
70 }
71
Yuqian Lid208a882018-01-04 10:08:42 -050072 gradientType = SkScalarNearlyZero(r1 - r0) ? Type::kStrip : Type::kFocal;
Florin Malita9c2212f2017-07-29 18:23:10 -040073 }
74
Yuqian Lid208a882018-01-04 10:08:42 -050075 FocalData focalData;
76 if (gradientType == Type::kFocal) {
77 const auto dCenter = (c0 - c1).length();
Mike Reeda2f14de2018-05-09 14:01:00 -040078 if (!focalData.set(r0 / dCenter, r1 / dCenter, &gradientMatrix)) {
79 return nullptr;
80 }
Yuqian Lid208a882018-01-04 10:08:42 -050081 }
Florin Malita5f379a82017-10-18 16:22:35 -040082 return sk_sp<SkShader>(new SkTwoPointConicalGradient(c0, r0, c1, r1, desc,
Yuqian Lid208a882018-01-04 10:08:42 -050083 gradientType, gradientMatrix, focalData));
Florin Malita9c2212f2017-07-29 18:23:10 -040084}
85
rileya@google.com589708b2012-07-26 20:04:23 +000086SkTwoPointConicalGradient::SkTwoPointConicalGradient(
reed@google.com3d3a8602013-05-24 14:58:44 +000087 const SkPoint& start, SkScalar startRadius,
88 const SkPoint& end, SkScalar endRadius,
Yuqian Lid208a882018-01-04 10:08:42 -050089 const Descriptor& desc, Type type, const SkMatrix& gradientMatrix, const FocalData& data)
Florin Malita9c2212f2017-07-29 18:23:10 -040090 : SkGradientShaderBase(desc, gradientMatrix)
reedaddf2ed2014-08-11 08:28:24 -070091 , fCenter1(start)
92 , fCenter2(end)
93 , fRadius1(startRadius)
94 , fRadius2(endRadius)
Florin Malita9c2212f2017-07-29 18:23:10 -040095 , fType(type)
reedaddf2ed2014-08-11 08:28:24 -070096{
rileya@google.com589708b2012-07-26 20:04:23 +000097 // this is degenerate, and should be caught by our caller
98 SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2);
Yuqian Lid208a882018-01-04 10:08:42 -050099 if (type == Type::kFocal) {
100 fFocalData = data;
101 }
rileya@google.com589708b2012-07-26 20:04:23 +0000102}
103
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000104bool SkTwoPointConicalGradient::isOpaque() const {
robertphillips@google.comcb6d97c2013-07-09 13:50:09 +0000105 // Because areas outside the cone are left untouched, we cannot treat the
106 // shader as opaque even if the gradient itself is opaque.
107 // TODO(junov): Compute whether the cone fills the plane crbug.com/222380
108 return false;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +0000109}
110
commit-bot@chromium.org44d83c12014-04-21 13:10:25 +0000111// Returns the original non-sorted version of the gradient
Florin Malita5f379a82017-10-18 16:22:35 -0400112SkShader::GradientType SkTwoPointConicalGradient::asAGradient(GradientInfo* info) const {
rileya@google.com589708b2012-07-26 20:04:23 +0000113 if (info) {
Florin Malita5f379a82017-10-18 16:22:35 -0400114 commonAsAGradient(info);
rileya@google.com589708b2012-07-26 20:04:23 +0000115 info->fPoint[0] = fCenter1;
116 info->fPoint[1] = fCenter2;
117 info->fRadius[0] = fRadius1;
118 info->fRadius[1] = fRadius2;
119 }
120 return kConical_GradientType;
121}
122
reed60c9b582016-04-03 09:11:13 -0700123sk_sp<SkFlattenable> SkTwoPointConicalGradient::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700124 DescriptorScope desc;
125 if (!desc.unflatten(buffer)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700126 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700127 }
128 SkPoint c1 = buffer.readPoint();
129 SkPoint c2 = buffer.readPoint();
130 SkScalar r1 = buffer.readScalar();
131 SkScalar r2 = buffer.readScalar();
132
Florin Malita5f379a82017-10-18 16:22:35 -0400133 if (buffer.isVersionLT(SkReadBuffer::k2PtConicalNoFlip_Version) && buffer.readBool()) {
134 // legacy flipped gradient
reed9fa60da2014-08-21 07:59:51 -0700135 SkTSwap(c1, c2);
136 SkTSwap(r1, r2);
137
brianosmane25d71c2016-09-28 11:27:28 -0700138 SkColor4f* colors = desc.mutableColors();
reed9fa60da2014-08-21 07:59:51 -0700139 SkScalar* pos = desc.mutablePos();
140 const int last = desc.fCount - 1;
141 const int half = desc.fCount >> 1;
142 for (int i = 0; i < half; ++i) {
143 SkTSwap(colors[i], colors[last - i]);
144 if (pos) {
145 SkScalar tmp = pos[i];
146 pos[i] = SK_Scalar1 - pos[last - i];
147 pos[last - i] = SK_Scalar1 - tmp;
148 }
149 }
150 if (pos) {
151 if (desc.fCount & 1) {
152 pos[half] = SK_Scalar1 - pos[half];
153 }
154 }
155 }
156
brianosmane25d71c2016-09-28 11:27:28 -0700157 return SkGradientShader::MakeTwoPointConical(c1, r1, c2, r2, desc.fColors,
158 std::move(desc.fColorSpace), desc.fPos,
reed8a21c9f2016-03-08 18:50:00 -0800159 desc.fCount, desc.fTileMode, desc.fGradFlags,
reed60c9b582016-04-03 09:11:13 -0700160 desc.fLocalMatrix);
reed9fa60da2014-08-21 07:59:51 -0700161}
162
163void SkTwoPointConicalGradient::flatten(SkWriteBuffer& buffer) const {
rileya@google.com589708b2012-07-26 20:04:23 +0000164 this->INHERITED::flatten(buffer);
165 buffer.writePoint(fCenter1);
166 buffer.writePoint(fCenter2);
167 buffer.writeScalar(fRadius1);
168 buffer.writeScalar(fRadius2);
169}
170
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000171#if SK_SUPPORT_GPU
172
dandov9de5b512014-06-10 14:38:28 -0700173#include "SkGr.h"
brianosman9557c272016-09-15 06:59:15 -0700174#include "SkTwoPointConicalGradient_gpu.h"
dandov9de5b512014-06-10 14:38:28 -0700175
Brian Salomonaff329b2017-08-11 09:40:37 -0400176std::unique_ptr<GrFragmentProcessor> SkTwoPointConicalGradient::asFragmentProcessor(
Mike Reede3429e62018-01-19 11:43:34 -0500177 const GrFPArgs& args) const {
Florin Malitac6c5ead2018-04-11 15:33:40 -0400178 SkMatrix matrix;
179 if (!this->totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
180 return nullptr;
181 }
182
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400183 return Gr2PtConicalGradientEffect::Make(
Florin Malitac6c5ead2018-04-11 15:33:40 -0400184 GrGradientEffect::CreateArgs(args.fContext, this, &matrix, fTileMode,
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400185 args.fDstColorSpaceInfo->colorSpace()));
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000186}
187
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000188#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000189
Matt Sarett6cc6ae752017-04-18 18:29:12 -0400190sk_sp<SkShader> SkTwoPointConicalGradient::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
Florin Malita39d71de2017-10-31 11:33:49 -0400191 const AutoXformColors xformedColors(*this, xformer);
Florin Malita5f379a82017-10-18 16:22:35 -0400192 return SkGradientShader::MakeTwoPointConical(fCenter1, fRadius1, fCenter2, fRadius2,
Florin Malita39d71de2017-10-31 11:33:49 -0400193 xformedColors.fColors.get(), fOrigPos, fColorCount,
Florin Malita5f379a82017-10-18 16:22:35 -0400194 fTileMode, fGradFlags, &this->getLocalMatrix());
Matt Sarett6cc6ae752017-04-18 18:29:12 -0400195}
196
197
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000198void SkTwoPointConicalGradient::toString(SkString* str) const {
199 str->append("SkTwoPointConicalGradient: (");
200
201 str->append("center1: (");
202 str->appendScalar(fCenter1.fX);
203 str->append(", ");
204 str->appendScalar(fCenter1.fY);
205 str->append(") radius1: ");
206 str->appendScalar(fRadius1);
207 str->append(" ");
208
209 str->append("center2: (");
210 str->appendScalar(fCenter2.fX);
211 str->append(", ");
212 str->appendScalar(fCenter2.fY);
213 str->append(") radius2: ");
214 str->appendScalar(fRadius2);
215 str->append(" ");
216
217 this->INHERITED::toString(str);
218
219 str->append(")");
220}
Florin Malita0bb04112017-06-27 14:35:50 -0400221
Florin Malita50b20842017-07-29 19:08:28 -0400222void SkTwoPointConicalGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p,
223 SkRasterPipeline* postPipeline) const {
Florin Malitaa66ef2d2017-06-28 10:02:40 -0400224 const auto dRadius = fRadius2 - fRadius1;
Florin Malitaa66ef2d2017-06-28 10:02:40 -0400225
Florin Malita9c2212f2017-07-29 18:23:10 -0400226 if (fType == Type::kRadial) {
Florin Malita0bb04112017-06-27 14:35:50 -0400227 p->append(SkRasterPipeline::xy_to_radius);
228
229 // Tiny twist: radial computes a t for [0, r2], but we want a t for [r1, r2].
Florin Malita5f379a82017-10-18 16:22:35 -0400230 auto scale = SkTMax(fRadius1, fRadius2) / dRadius;
Florin Malitaa66ef2d2017-06-28 10:02:40 -0400231 auto bias = -fRadius1 / dRadius;
Florin Malita0bb04112017-06-27 14:35:50 -0400232
Mike Reed6b59bf42017-07-03 21:26:44 -0400233 p->append_matrix(alloc, SkMatrix::Concat(SkMatrix::MakeTrans(bias, 0),
234 SkMatrix::MakeScale(scale, 1)));
Florin Malita50b20842017-07-29 19:08:28 -0400235 return;
Florin Malita0bb04112017-06-27 14:35:50 -0400236 }
237
Yuqian Lid208a882018-01-04 10:08:42 -0500238 if (fType == Type::kStrip) {
239 auto* ctx = alloc->make<SkJumper_2PtConicalCtx>();
240 SkScalar scaledR0 = fRadius1 / this->getCenterX1();
241 ctx->fP0 = scaledR0 * scaledR0;
242 p->append(SkRasterPipeline::xy_to_2pt_conical_strip, ctx);
243 p->append(SkRasterPipeline::mask_2pt_conical_nan, ctx);
244 postPipeline->append(SkRasterPipeline::apply_vector_mask, &ctx->fMask);
245 return;
246 }
247
248 auto* ctx = alloc->make<SkJumper_2PtConicalCtx>();
249 ctx->fP0 = 1/fFocalData.fR1;
250 ctx->fP1 = fFocalData.fFocalX;
251
252 if (fFocalData.isFocalOnCircle()) {
253 p->append(SkRasterPipeline::xy_to_2pt_conical_focal_on_circle);
254 } else if (fFocalData.isWellBehaved()) {
255 p->append(SkRasterPipeline::xy_to_2pt_conical_well_behaved, ctx);
256 } else if (fFocalData.isSwapped() || 1 - fFocalData.fFocalX < 0) {
257 p->append(SkRasterPipeline::xy_to_2pt_conical_smaller, ctx);
258 } else {
259 p->append(SkRasterPipeline::xy_to_2pt_conical_greater, ctx);
260 }
261
262 if (!fFocalData.isWellBehaved()) {
263 p->append(SkRasterPipeline::mask_2pt_conical_degenerates, ctx);
264 }
265 if (1 - fFocalData.fFocalX < 0) {
266 p->append(SkRasterPipeline::negate_x);
267 }
268 if (!fFocalData.isNativelyFocal()) {
269 p->append(SkRasterPipeline::alter_2pt_conical_compensate_focal, ctx);
270 }
271 if (fFocalData.isSwapped()) {
272 p->append(SkRasterPipeline::alter_2pt_conical_unswap);
273 }
274 if (!fFocalData.isWellBehaved()) {
275 postPipeline->append(SkRasterPipeline::apply_vector_mask, &ctx->fMask);
276 }
Florin Malita0bb04112017-06-27 14:35:50 -0400277}