blob: 04448b45a173696ce92b97b1d0c5f5b51718c465 [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
8#include "SkTwoPointConicalGradient.h"
commit-bot@chromium.orgaa64fbf2014-04-03 14:59:19 +00009
Florin Malitaa66ef2d2017-06-28 10:02:40 -040010#include "SkRasterPipeline.h"
11#include "../../jumper/SkJumper.h"
12
Florin Malita9c2212f2017-07-29 18:23:10 -040013sk_sp<SkShader> SkTwoPointConicalGradient::Create(const SkPoint& c0, SkScalar r0,
14 const SkPoint& c1, SkScalar r1,
Florin Malita5f379a82017-10-18 16:22:35 -040015 const Descriptor& desc) {
Florin Malita9c2212f2017-07-29 18:23:10 -040016 SkMatrix gradientMatrix;
17 Type gradientType;
18
19 if (SkScalarNearlyZero((c0 - c1).length())) {
20 // Concentric case: we can pretend we're radial (with a tiny twist).
Florin Malita5f379a82017-10-18 16:22:35 -040021 const SkScalar scale = 1.0f / SkTMax(r0, r1);
Florin Malita9c2212f2017-07-29 18:23:10 -040022 gradientMatrix = SkMatrix::MakeTrans(-c1.x(), -c1.y());
Florin Malita5f379a82017-10-18 16:22:35 -040023 gradientMatrix.postScale(scale, scale);
Florin Malita9c2212f2017-07-29 18:23:10 -040024
25 gradientType = Type::kRadial;
26 } else {
27 const SkPoint centers[2] = { c0 , c1 };
28 const SkPoint unitvec[2] = { {0, 0}, {1, 0} };
29
30 if (!gradientMatrix.setPolyToPoly(centers, unitvec, 2)) {
31 // Degenerate case.
32 return nullptr;
33 }
34
35 // General two-point case.
36 gradientType = Type::kTwoPoint;
37 }
38
Florin Malita5f379a82017-10-18 16:22:35 -040039 return sk_sp<SkShader>(new SkTwoPointConicalGradient(c0, r0, c1, r1, desc,
Florin Malita9c2212f2017-07-29 18:23:10 -040040 gradientType, gradientMatrix));
41}
42
rileya@google.com589708b2012-07-26 20:04:23 +000043SkTwoPointConicalGradient::SkTwoPointConicalGradient(
reed@google.com3d3a8602013-05-24 14:58:44 +000044 const SkPoint& start, SkScalar startRadius,
45 const SkPoint& end, SkScalar endRadius,
Florin Malita5f379a82017-10-18 16:22:35 -040046 const Descriptor& desc, Type type, const SkMatrix& gradientMatrix)
Florin Malita9c2212f2017-07-29 18:23:10 -040047 : SkGradientShaderBase(desc, gradientMatrix)
reedaddf2ed2014-08-11 08:28:24 -070048 , fCenter1(start)
49 , fCenter2(end)
50 , fRadius1(startRadius)
51 , fRadius2(endRadius)
Florin Malita9c2212f2017-07-29 18:23:10 -040052 , fType(type)
reedaddf2ed2014-08-11 08:28:24 -070053{
rileya@google.com589708b2012-07-26 20:04:23 +000054 // this is degenerate, and should be caught by our caller
55 SkASSERT(fCenter1 != fCenter2 || fRadius1 != fRadius2);
rileya@google.com589708b2012-07-26 20:04:23 +000056}
57
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +000058bool SkTwoPointConicalGradient::isOpaque() const {
robertphillips@google.comcb6d97c2013-07-09 13:50:09 +000059 // Because areas outside the cone are left untouched, we cannot treat the
60 // shader as opaque even if the gradient itself is opaque.
61 // TODO(junov): Compute whether the cone fills the plane crbug.com/222380
62 return false;
commit-bot@chromium.org3fbab822013-03-20 00:49:57 +000063}
64
commit-bot@chromium.org44d83c12014-04-21 13:10:25 +000065// Returns the original non-sorted version of the gradient
Florin Malita5f379a82017-10-18 16:22:35 -040066SkShader::GradientType SkTwoPointConicalGradient::asAGradient(GradientInfo* info) const {
rileya@google.com589708b2012-07-26 20:04:23 +000067 if (info) {
Florin Malita5f379a82017-10-18 16:22:35 -040068 commonAsAGradient(info);
rileya@google.com589708b2012-07-26 20:04:23 +000069 info->fPoint[0] = fCenter1;
70 info->fPoint[1] = fCenter2;
71 info->fRadius[0] = fRadius1;
72 info->fRadius[1] = fRadius2;
73 }
74 return kConical_GradientType;
75}
76
reed60c9b582016-04-03 09:11:13 -070077sk_sp<SkFlattenable> SkTwoPointConicalGradient::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070078 DescriptorScope desc;
79 if (!desc.unflatten(buffer)) {
halcanary96fcdcc2015-08-27 07:41:13 -070080 return nullptr;
reed9fa60da2014-08-21 07:59:51 -070081 }
82 SkPoint c1 = buffer.readPoint();
83 SkPoint c2 = buffer.readPoint();
84 SkScalar r1 = buffer.readScalar();
85 SkScalar r2 = buffer.readScalar();
86
Florin Malita5f379a82017-10-18 16:22:35 -040087 if (buffer.isVersionLT(SkReadBuffer::k2PtConicalNoFlip_Version) && buffer.readBool()) {
88 // legacy flipped gradient
reed9fa60da2014-08-21 07:59:51 -070089 SkTSwap(c1, c2);
90 SkTSwap(r1, r2);
91
brianosmane25d71c2016-09-28 11:27:28 -070092 SkColor4f* colors = desc.mutableColors();
reed9fa60da2014-08-21 07:59:51 -070093 SkScalar* pos = desc.mutablePos();
94 const int last = desc.fCount - 1;
95 const int half = desc.fCount >> 1;
96 for (int i = 0; i < half; ++i) {
97 SkTSwap(colors[i], colors[last - i]);
98 if (pos) {
99 SkScalar tmp = pos[i];
100 pos[i] = SK_Scalar1 - pos[last - i];
101 pos[last - i] = SK_Scalar1 - tmp;
102 }
103 }
104 if (pos) {
105 if (desc.fCount & 1) {
106 pos[half] = SK_Scalar1 - pos[half];
107 }
108 }
109 }
110
brianosmane25d71c2016-09-28 11:27:28 -0700111 return SkGradientShader::MakeTwoPointConical(c1, r1, c2, r2, desc.fColors,
112 std::move(desc.fColorSpace), desc.fPos,
reed8a21c9f2016-03-08 18:50:00 -0800113 desc.fCount, desc.fTileMode, desc.fGradFlags,
reed60c9b582016-04-03 09:11:13 -0700114 desc.fLocalMatrix);
reed9fa60da2014-08-21 07:59:51 -0700115}
116
117void SkTwoPointConicalGradient::flatten(SkWriteBuffer& buffer) const {
rileya@google.com589708b2012-07-26 20:04:23 +0000118 this->INHERITED::flatten(buffer);
119 buffer.writePoint(fCenter1);
120 buffer.writePoint(fCenter2);
121 buffer.writeScalar(fRadius1);
122 buffer.writeScalar(fRadius2);
123}
124
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000125#if SK_SUPPORT_GPU
126
dandov9de5b512014-06-10 14:38:28 -0700127#include "SkGr.h"
brianosman9557c272016-09-15 06:59:15 -0700128#include "SkTwoPointConicalGradient_gpu.h"
dandov9de5b512014-06-10 14:38:28 -0700129
Brian Salomonaff329b2017-08-11 09:40:37 -0400130std::unique_ptr<GrFragmentProcessor> SkTwoPointConicalGradient::asFragmentProcessor(
brianosman1638c0d2016-07-25 05:12:53 -0700131 const AsFPArgs& args) const {
brianosman839345d2016-07-22 11:04:53 -0700132 SkASSERT(args.fContext);
brianosmanb9c51372016-09-15 11:09:45 -0700133 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(fColorSpace.get(),
134 args.fDstColorSpace);
Brian Salomonaff329b2017-08-11 09:40:37 -0400135 auto inner = Gr2PtConicalGradientEffect::Make(GrGradientEffect::CreateArgs(
136 args.fContext, this, args.fLocalMatrix, fTileMode, std::move(colorSpaceXform),
137 SkToBool(args.fDstColorSpace)));
Brian Salomon6af27012017-06-09 08:21:42 -0400138 if (!inner) {
139 return nullptr;
140 }
bungeman06ca8ec2016-06-09 08:01:03 -0700141 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000142}
143
twiz@google.coma5e65ec2012-08-02 15:15:16 +0000144#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000145
Matt Sarett6cc6ae752017-04-18 18:29:12 -0400146sk_sp<SkShader> SkTwoPointConicalGradient::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
Florin Malita5f379a82017-10-18 16:22:35 -0400147 SkSTArray<8, SkColor> xformedColors(fColorCount);
148 xformer->apply(xformedColors.begin(), fOrigColors, fColorCount);
149 return SkGradientShader::MakeTwoPointConical(fCenter1, fRadius1, fCenter2, fRadius2,
150 xformedColors.begin(), fOrigPos, fColorCount,
151 fTileMode, fGradFlags, &this->getLocalMatrix());
Matt Sarett6cc6ae752017-04-18 18:29:12 -0400152}
153
154
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000155#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000156void SkTwoPointConicalGradient::toString(SkString* str) const {
157 str->append("SkTwoPointConicalGradient: (");
158
159 str->append("center1: (");
160 str->appendScalar(fCenter1.fX);
161 str->append(", ");
162 str->appendScalar(fCenter1.fY);
163 str->append(") radius1: ");
164 str->appendScalar(fRadius1);
165 str->append(" ");
166
167 str->append("center2: (");
168 str->appendScalar(fCenter2.fX);
169 str->append(", ");
170 str->appendScalar(fCenter2.fY);
171 str->append(") radius2: ");
172 str->appendScalar(fRadius2);
173 str->append(" ");
174
175 this->INHERITED::toString(str);
176
177 str->append(")");
178}
179#endif
Florin Malita0bb04112017-06-27 14:35:50 -0400180
Florin Malita50b20842017-07-29 19:08:28 -0400181void SkTwoPointConicalGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p,
182 SkRasterPipeline* postPipeline) const {
Florin Malitaa66ef2d2017-06-28 10:02:40 -0400183 const auto dRadius = fRadius2 - fRadius1;
Florin Malitaa66ef2d2017-06-28 10:02:40 -0400184
Florin Malita9c2212f2017-07-29 18:23:10 -0400185 if (fType == Type::kRadial) {
Florin Malita0bb04112017-06-27 14:35:50 -0400186 p->append(SkRasterPipeline::xy_to_radius);
187
188 // Tiny twist: radial computes a t for [0, r2], but we want a t for [r1, r2].
Florin Malita5f379a82017-10-18 16:22:35 -0400189 auto scale = SkTMax(fRadius1, fRadius2) / dRadius;
Florin Malitaa66ef2d2017-06-28 10:02:40 -0400190 auto bias = -fRadius1 / dRadius;
Florin Malita0bb04112017-06-27 14:35:50 -0400191
Mike Reed6b59bf42017-07-03 21:26:44 -0400192 p->append_matrix(alloc, SkMatrix::Concat(SkMatrix::MakeTrans(bias, 0),
193 SkMatrix::MakeScale(scale, 1)));
Florin Malita50b20842017-07-29 19:08:28 -0400194 return;
Florin Malita0bb04112017-06-27 14:35:50 -0400195 }
196
Florin Malita9c2212f2017-07-29 18:23:10 -0400197 const auto dCenter = (fCenter1 - fCenter2).length();
Florin Malitaa66ef2d2017-06-28 10:02:40 -0400198
199 // Since we've squashed the centers into a unit vector, we must also scale
200 // all the coefficient variables by (1 / dCenter).
201 const auto coeffA = 1 - dRadius * dRadius / (dCenter * dCenter);
Florin Malitaa66ef2d2017-06-28 10:02:40 -0400202 auto* ctx = alloc->make<SkJumper_2PtConicalCtx>();
203 ctx->fCoeffA = coeffA;
204 ctx->fInvCoeffA = 1 / coeffA;
205 ctx->fR0 = fRadius1 / dCenter;
206 ctx->fDR = dRadius / dCenter;
207
Florin Malita9026fe12017-06-29 11:03:45 -0400208 // Is the solver guaranteed to not produce degenerates?
209 bool isWellBehaved = true;
210
Florin Malita2e409002017-06-28 14:46:54 -0400211 if (SkScalarNearlyZero(coeffA)) {
212 // The focal point is on the edge of the end circle.
213 p->append(SkRasterPipeline::xy_to_2pt_conical_linear, ctx);
Florin Malita9026fe12017-06-29 11:03:45 -0400214 isWellBehaved = false;
Florin Malita2e409002017-06-28 14:46:54 -0400215 } else {
Florin Malita5f379a82017-10-18 16:22:35 -0400216 isWellBehaved = SkScalarAbs(dRadius) >= dCenter;
217 bool isFlipped = isWellBehaved && dRadius < 0;
Florin Malita9026fe12017-06-29 11:03:45 -0400218
Florin Malita5f379a82017-10-18 16:22:35 -0400219 // We want the larger root, per spec:
220 // "For all values of ω where r(ω) > 0, starting with the value of ω nearest
221 // to positive infinity and ending with the value of ω nearest to negative
222 // infinity, draw the circumference of the circle with radius r(ω) at position
223 // (x(ω), y(ω)), with the color at ω, but only painting on the parts of the
224 // bitmap that have not yet been painted on by earlier circles in this step for
225 // this rendering of the gradient."
226 // (https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createradialgradient)
227 //
228 // ... except when the gradient is flipped.
229 p->append(isFlipped ? SkRasterPipeline::xy_to_2pt_conical_quadratic_min
230 : SkRasterPipeline::xy_to_2pt_conical_quadratic_max, ctx);
Florin Malita9026fe12017-06-29 11:03:45 -0400231 }
232
233 if (!isWellBehaved) {
234 p->append(SkRasterPipeline::mask_2pt_conical_degenerates, ctx);
235 postPipeline->append(SkRasterPipeline::apply_vector_mask, &ctx->fMask);
Florin Malita2e409002017-06-28 14:46:54 -0400236 }
Florin Malita0bb04112017-06-27 14:35:50 -0400237}