blob: 27517bb080e0387e6763fb801290f9a535a3ec77 [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
Matt Sarett6cc6ae752017-04-18 18:29:12 -04008#include "SkColorSpaceXformer.h"
rileya@google.com589708b2012-07-26 20:04:23 +00009#include "SkSweepGradient.h"
10
Herb Derby0d63e4e2017-04-13 12:15:50 -040011#include <algorithm>
12
mtkleincc695fe2014-12-10 10:29:19 -080013static SkMatrix translate(SkScalar dx, SkScalar dy) {
14 SkMatrix matrix;
15 matrix.setTranslate(dx, dy);
16 return matrix;
17}
18
reedaddf2ed2014-08-11 08:28:24 -070019SkSweepGradient::SkSweepGradient(SkScalar cx, SkScalar cy, const Descriptor& desc)
mtkleincc695fe2014-12-10 10:29:19 -080020 : SkGradientShaderBase(desc, translate(-cx, -cy))
reed@google.com3d3a8602013-05-24 14:58:44 +000021 , fCenter(SkPoint::Make(cx, cy))
rileya@google.com589708b2012-07-26 20:04:23 +000022{
reed@google.com437d6eb2013-05-23 19:03:05 +000023 // overwrite the tilemode to a canonical value (since sweep ignores it)
24 fTileMode = SkShader::kClamp_TileMode;
rileya@google.com589708b2012-07-26 20:04:23 +000025}
26
rileya@google.com589708b2012-07-26 20:04:23 +000027SkShader::GradientType SkSweepGradient::asAGradient(GradientInfo* info) const {
28 if (info) {
29 commonAsAGradient(info);
30 info->fPoint[0] = fCenter;
31 }
32 return kSweep_GradientType;
33}
34
reed60c9b582016-04-03 09:11:13 -070035sk_sp<SkFlattenable> SkSweepGradient::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070036 DescriptorScope desc;
37 if (!desc.unflatten(buffer)) {
halcanary96fcdcc2015-08-27 07:41:13 -070038 return nullptr;
reed9fa60da2014-08-21 07:59:51 -070039 }
40 const SkPoint center = buffer.readPoint();
brianosmane25d71c2016-09-28 11:27:28 -070041 return SkGradientShader::MakeSweep(center.x(), center.y(), desc.fColors,
42 std::move(desc.fColorSpace), desc.fPos, desc.fCount,
43 desc.fGradFlags, desc.fLocalMatrix);
reed9fa60da2014-08-21 07:59:51 -070044}
rileya@google.com589708b2012-07-26 20:04:23 +000045
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000046void SkSweepGradient::flatten(SkWriteBuffer& buffer) const {
rileya@google.com589708b2012-07-26 20:04:23 +000047 this->INHERITED::flatten(buffer);
48 buffer.writePoint(fCenter);
49}
50
Herb Derby83e939b2017-02-07 14:25:11 -050051SkShader::Context* SkSweepGradient::onMakeContext(
52 const ContextRec& rec, SkArenaAlloc* alloc) const
53{
54 return CheckedMakeContext<SweepGradientContext>(alloc, *this, rec);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000055}
56
57SkSweepGradient::SweepGradientContext::SweepGradientContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +000058 const SkSweepGradient& shader, const ContextRec& rec)
59 : INHERITED(shader, rec) {}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000060
rileya@google.com589708b2012-07-26 20:04:23 +000061// returns angle in a circle [0..2PI) -> [0..255]
Herb Derby0d63e4e2017-04-13 12:15:50 -040062#ifdef SK_LEGACY_SWEEP_GRADIENT
rileya@google.com589708b2012-07-26 20:04:23 +000063static unsigned SkATan2_255(float y, float x) {
64 // static const float g255Over2PI = 255 / (2 * SK_ScalarPI);
65 static const float g255Over2PI = 40.584510488433314f;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000066
rileya@google.com589708b2012-07-26 20:04:23 +000067 float result = sk_float_atan2(y, x);
robertphillips1d265ca2015-12-07 09:54:02 -080068 if (!SkScalarIsFinite(result)) {
69 return 0;
70 }
rileya@google.com589708b2012-07-26 20:04:23 +000071 if (result < 0) {
72 result += 2 * SK_ScalarPI;
73 }
74 SkASSERT(result >= 0);
75 // since our value is always >= 0, we can cast to int, which is faster than
76 // calling floorf()
77 int ir = (int)(result * g255Over2PI);
78 SkASSERT(ir >= 0 && ir <= 255);
79 return ir;
80}
Herb Derby0d63e4e2017-04-13 12:15:50 -040081#else
82static unsigned SkATan2_255(float y, float x) {
83 if (y == 0 && x == 0) return 0;
84 float yabs = sk_float_abs(y),
85 xabs = sk_float_abs(x);
86 float little, big;
87 std::tie(little, big) = std::minmax(yabs, xabs);
88 float a = little/big;
89 float s = a * a;
90 float r = a*(40.57589784014689f
91 + s*(-13.222755844396332f + s*(6.314046289038564f - s*1.7989502668982151f)));
92 r = xabs < yabs ? 255/4.0f - r : r;
93 r = x < 0.0f ? 255/2.0f - r : r;
94 r = y < 0.0f ? 255 - r : r;
95
96 int ir = (int)r;
97 SkASSERT(ir >= 0 && ir <= 255);
98 return ir;
99}
100#endif
rileya@google.com589708b2012-07-26 20:04:23 +0000101
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000102void SkSweepGradient::SweepGradientContext::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC,
103 int count) {
rileya@google.com589708b2012-07-26 20:04:23 +0000104 SkMatrix::MapXYProc proc = fDstToIndexProc;
105 const SkMatrix& matrix = fDstToIndex;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000106 const SkPMColor* SK_RESTRICT cache = fCache->getCache32();
reed@google.com60040292013-02-04 18:21:23 +0000107 int toggle = init_dither_toggle(x, y);
rileya@google.com589708b2012-07-26 20:04:23 +0000108 SkPoint srcPt;
109
110 if (fDstToIndexClass != kPerspective_MatrixClass) {
111 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
112 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
113 SkScalar dx, fx = srcPt.fX;
114 SkScalar dy, fy = srcPt.fY;
115
116 if (fDstToIndexClass == kFixedStepInX_MatrixClass) {
benjaminwagner8e175562016-02-16 10:09:40 -0800117 const auto step = matrix.fixedStepInX(SkIntToScalar(y) + SK_ScalarHalf);
118 dx = step.fX;
119 dy = step.fY;
rileya@google.com589708b2012-07-26 20:04:23 +0000120 } else {
121 SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
122 dx = matrix.getScaleX();
123 dy = matrix.getSkewY();
124 }
125
126 for (; count > 0; --count) {
reed@google.com60040292013-02-04 18:21:23 +0000127 *dstC++ = cache[toggle + SkATan2_255(fy, fx)];
rileya@google.com589708b2012-07-26 20:04:23 +0000128 fx += dx;
129 fy += dy;
reed@google.com60040292013-02-04 18:21:23 +0000130 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000131 }
132 } else { // perspective case
133 for (int stop = x + count; x < stop; x++) {
134 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf,
135 SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
reed@google.com60040292013-02-04 18:21:23 +0000136 *dstC++ = cache[toggle + SkATan2_255(srcPt.fY, srcPt.fX)];
reed@google.com60040292013-02-04 18:21:23 +0000137 toggle = next_dither_toggle(toggle);
rileya@google.com589708b2012-07-26 20:04:23 +0000138 }
139 }
140}
141
rileya@google.comd7cc6512012-07-27 14:00:39 +0000142/////////////////////////////////////////////////////////////////////
143
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000144#if SK_SUPPORT_GPU
145
dandov9de5b512014-06-10 14:38:28 -0700146#include "SkGr.h"
Brian Salomon94efbf52016-11-29 13:43:05 -0500147#include "GrShaderCaps.h"
egdanielf5294392015-10-21 07:14:17 -0700148#include "gl/GrGLContext.h"
egdaniel7ea439b2015-12-03 09:20:44 -0800149#include "glsl/GrGLSLFragmentShaderBuilder.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000150
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000151class GrSweepGradient : public GrGradientEffect {
152public:
fmenozzi55d318d2016-08-09 08:05:57 -0700153 class GLSLSweepProcessor;
154
brianosman9557c272016-09-15 06:59:15 -0700155 static sk_sp<GrFragmentProcessor> Make(const CreateArgs& args) {
156 return sk_sp<GrFragmentProcessor>(new GrSweepGradient(args));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000157 }
Brian Salomond3b65972017-03-22 12:05:03 -0400158 ~GrSweepGradient() override {}
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000159
mtklein36352bf2015-03-25 18:17:31 -0700160 const char* name() const override { return "Sweep Gradient"; }
joshualitteb2a6762014-12-04 11:35:33 -0800161
bsalomon@google.comd4726202012-08-03 14:34:46 +0000162private:
Brian Salomon587e08f2017-01-27 10:59:27 -0500163 GrSweepGradient(const CreateArgs& args) : INHERITED(args, args.fShader->colorsAreOpaque()) {
joshualitteb2a6762014-12-04 11:35:33 -0800164 this->initClassID<GrSweepGradient>();
165 }
wangyix4b3050b2015-08-04 07:59:37 -0700166
fmenozzi55d318d2016-08-09 08:05:57 -0700167 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700168
Brian Salomon94efbf52016-11-29 13:43:05 -0500169 virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps,
fmenozzi55d318d2016-08-09 08:05:57 -0700170 GrProcessorKeyBuilder* b) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700171
joshualittb0a8a372014-09-23 09:50:21 -0700172 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000173
174 typedef GrGradientEffect INHERITED;
175};
176
177/////////////////////////////////////////////////////////////////////
178
fmenozzi55d318d2016-08-09 08:05:57 -0700179class GrSweepGradient::GLSLSweepProcessor : public GrGradientEffect::GLSLProcessor {
180public:
181 GLSLSweepProcessor(const GrProcessor&) {}
Brian Salomond3b65972017-03-22 12:05:03 -0400182 ~GLSLSweepProcessor() override {}
fmenozzi55d318d2016-08-09 08:05:57 -0700183
184 virtual void emitCode(EmitArgs&) override;
185
Brian Salomon94efbf52016-11-29 13:43:05 -0500186 static void GenKey(const GrProcessor& processor, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
fmenozzi55d318d2016-08-09 08:05:57 -0700187 b->add32(GenBaseGradientKey(processor));
188 }
189
190private:
191 typedef GrGradientEffect::GLSLProcessor INHERITED;
192
193};
194
195/////////////////////////////////////////////////////////////////////
196
197GrGLSLFragmentProcessor* GrSweepGradient::onCreateGLSLInstance() const {
198 return new GrSweepGradient::GLSLSweepProcessor(*this);
199}
200
Brian Salomon94efbf52016-11-29 13:43:05 -0500201void GrSweepGradient::onGetGLSLProcessorKey(const GrShaderCaps& caps,
fmenozzi55d318d2016-08-09 08:05:57 -0700202 GrProcessorKeyBuilder* b) const {
203 GrSweepGradient::GLSLSweepProcessor::GenKey(*this, caps, b);
204}
205
206
207/////////////////////////////////////////////////////////////////////
208
joshualittb0a8a372014-09-23 09:50:21 -0700209GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSweepGradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000210
Hal Canary6f6961e2017-01-31 13:50:44 -0500211#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700212sk_sp<GrFragmentProcessor> GrSweepGradient::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700213 SkPoint center = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()};
bsalomon@google.comd4726202012-08-03 14:34:46 +0000214
Brian Osman3f748602016-10-03 18:29:03 -0400215 RandomGradientParams params(d->fRandom);
Brian Osmana2196532016-10-17 12:48:13 -0400216 auto shader = params.fUseColors4f ?
217 SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors4f, params.fColorSpace,
218 params.fStops, params.fColorCount) :
219 SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors,
220 params.fStops, params.fColorCount);
Brian Osman9f532a32016-10-19 11:12:09 -0400221 GrTest::TestAsFPArgs asFPArgs(d);
222 sk_sp<GrFragmentProcessor> fp = shader->asFragmentProcessor(asFPArgs.args());
bsalomonc21b09e2015-08-28 18:46:56 -0700223 GrAlwaysAssert(fp);
joshualittb0a8a372014-09-23 09:50:21 -0700224 return fp;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000225}
Hal Canary6f6961e2017-01-31 13:50:44 -0500226#endif
bsalomon@google.comd4726202012-08-03 14:34:46 +0000227
228/////////////////////////////////////////////////////////////////////
229
fmenozzi55d318d2016-08-09 08:05:57 -0700230void GrSweepGradient::GLSLSweepProcessor::emitCode(EmitArgs& args) {
wangyix7c157a92015-07-22 15:08:53 -0700231 const GrSweepGradient& ge = args.fFp.cast<GrSweepGradient>();
egdaniel7ea439b2015-12-03 09:20:44 -0800232 this->emitUniforms(args.fUniformHandler, ge);
bsalomon1a1aa932016-09-12 09:30:36 -0700233 SkString coords2D = args.fFragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000234 SkString t;
commit-bot@chromium.org54318d32014-02-14 17:27:04 +0000235 // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi]
Greg Daniel80a08dd2017-01-20 10:45:49 -0500236 if (args.fShaderCaps->atan2ImplementedAsAtanYOverX()) {
237 // On some devices they incorrectly implement atan2(y,x) as atan(y/x). In actuality it is
238 // atan2(y,x) = 2 * atan(y / (sqrt(x^2 + y^2) + x)). So to work around this we pass in
239 // (sqrt(x^2 + y^2) + x) as the second parameter to atan2 in these cases. We let the device
240 // handle the undefined behavior of the second paramenter being 0 instead of doing the
241 // divide ourselves and using atan instead.
242 t.printf("(2.0 * atan(- %s.y, length(%s) - %s.x) * 0.1591549430918 + 0.5)",
243 coords2D.c_str(), coords2D.c_str(), coords2D.c_str());
244 } else {
245 t.printf("(atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5)",
246 coords2D.c_str(), coords2D.c_str());
247 }
egdaniel7ea439b2015-12-03 09:20:44 -0800248 this->emitColor(args.fFragBuilder,
249 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500250 args.fShaderCaps,
egdaniel4ca2e602015-11-18 08:01:26 -0800251 ge, t.c_str(),
252 args.fOutputColor,
253 args.fInputColor,
cdalton3f6f76f2016-04-11 12:18:09 -0700254 args.fTexSamplers);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000255}
256
257/////////////////////////////////////////////////////////////////////
258
brianosman839345d2016-07-22 11:04:53 -0700259sk_sp<GrFragmentProcessor> SkSweepGradient::asFragmentProcessor(const AsFPArgs& args) const {
mtklein3f3b3d02014-12-01 11:47:08 -0800260
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000261 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000262 if (!this->getLocalMatrix().invert(&matrix)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700263 return nullptr;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000264 }
brianosman839345d2016-07-22 11:04:53 -0700265 if (args.fLocalMatrix) {
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000266 SkMatrix inv;
brianosman839345d2016-07-22 11:04:53 -0700267 if (!args.fLocalMatrix->invert(&inv)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700268 return nullptr;
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000269 }
270 matrix.postConcat(inv);
271 }
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000272 matrix.postConcat(fPtsToUnit);
mtklein3f3b3d02014-12-01 11:47:08 -0800273
brianosmanb9c51372016-09-15 11:09:45 -0700274 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(fColorSpace.get(),
275 args.fDstColorSpace);
brianosman9557c272016-09-15 06:59:15 -0700276 sk_sp<GrFragmentProcessor> inner(GrSweepGradient::Make(
brianosmanb9c51372016-09-15 11:09:45 -0700277 GrGradientEffect::CreateArgs(args.fContext, this, &matrix, SkShader::kClamp_TileMode,
278 std::move(colorSpaceXform), SkToBool(args.fDstColorSpace))));
bungeman06ca8ec2016-06-09 08:01:03 -0700279 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000280}
281
282#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000283
Matt Sarett6cc6ae752017-04-18 18:29:12 -0400284sk_sp<SkShader> SkSweepGradient::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
285 SkSTArray<8, SkColor> xformedColors(fColorCount);
286 xformer->apply(xformedColors.begin(), fOrigColors, fColorCount);
287 return SkGradientShader::MakeSweep(fCenter.fX, fCenter.fY, xformedColors.begin(), fOrigPos,
288 fColorCount, fGradFlags, &this->getLocalMatrix());
289}
290
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000291#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000292void SkSweepGradient::toString(SkString* str) const {
293 str->append("SkSweepGradient: (");
294
295 str->append("center: (");
296 str->appendScalar(fCenter.fX);
297 str->append(", ");
298 str->appendScalar(fCenter.fY);
299 str->append(") ");
300
301 this->INHERITED::toString(str);
302
303 str->append(")");
304}
305#endif