blob: 93746f663ce9fe6638fbd33eef535cc13e842a95 [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"
Florin Malitad4e9ec82017-10-25 18:00:26 -04009#include "SkReadBuffer.h"
rileya@google.com589708b2012-07-26 20:04:23 +000010#include "SkSweepGradient.h"
Herb Derby7eb86982017-05-02 19:04:39 -040011#include "SkPM4fPriv.h"
12#include "SkRasterPipeline.h"
Florin Malitad4e9ec82017-10-25 18:00:26 -040013#include "SkWriteBuffer.h"
Herb Derby7eb86982017-05-02 19:04:39 -040014
Florin Malita5a9a9812017-08-01 16:38:08 -040015SkSweepGradient::SkSweepGradient(const SkPoint& center, SkScalar t0, SkScalar t1,
16 const Descriptor& desc)
17 : SkGradientShaderBase(desc, SkMatrix::MakeTrans(-center.x(), -center.y()))
18 , fCenter(center)
19 , fTBias(-t0)
20 , fTScale(1 / (t1 - t0))
rileya@google.com589708b2012-07-26 20:04:23 +000021{
Florin Malita5a9a9812017-08-01 16:38:08 -040022 SkASSERT(t0 < t1);
rileya@google.com589708b2012-07-26 20:04:23 +000023}
24
rileya@google.com589708b2012-07-26 20:04:23 +000025SkShader::GradientType SkSweepGradient::asAGradient(GradientInfo* info) const {
26 if (info) {
27 commonAsAGradient(info);
28 info->fPoint[0] = fCenter;
29 }
30 return kSweep_GradientType;
31}
32
Florin Malita545630b2017-08-02 10:42:41 -040033static std::tuple<SkScalar, SkScalar> angles_from_t_coeff(SkScalar tBias, SkScalar tScale) {
34 return std::make_tuple(-tBias * 360, (1 / tScale - tBias) * 360);
35}
36
reed60c9b582016-04-03 09:11:13 -070037sk_sp<SkFlattenable> SkSweepGradient::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070038 DescriptorScope desc;
39 if (!desc.unflatten(buffer)) {
halcanary96fcdcc2015-08-27 07:41:13 -070040 return nullptr;
reed9fa60da2014-08-21 07:59:51 -070041 }
42 const SkPoint center = buffer.readPoint();
Florin Malita5a9a9812017-08-01 16:38:08 -040043
44 SkScalar startAngle = 0,
45 endAngle = 360;
46 if (!buffer.isVersionLT(SkReadBuffer::kTileInfoInSweepGradient_Version)) {
47 const auto tBias = buffer.readScalar(),
48 tScale = buffer.readScalar();
Florin Malita545630b2017-08-02 10:42:41 -040049 std::tie(startAngle, endAngle) = angles_from_t_coeff(tBias, tScale);
Florin Malita5a9a9812017-08-01 16:38:08 -040050 }
51
brianosmane25d71c2016-09-28 11:27:28 -070052 return SkGradientShader::MakeSweep(center.x(), center.y(), desc.fColors,
53 std::move(desc.fColorSpace), desc.fPos, desc.fCount,
Florin Malita5a9a9812017-08-01 16:38:08 -040054 desc.fTileMode, startAngle, endAngle,
brianosmane25d71c2016-09-28 11:27:28 -070055 desc.fGradFlags, desc.fLocalMatrix);
reed9fa60da2014-08-21 07:59:51 -070056}
rileya@google.com589708b2012-07-26 20:04:23 +000057
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000058void SkSweepGradient::flatten(SkWriteBuffer& buffer) const {
rileya@google.com589708b2012-07-26 20:04:23 +000059 this->INHERITED::flatten(buffer);
60 buffer.writePoint(fCenter);
Florin Malita5a9a9812017-08-01 16:38:08 -040061 buffer.writeScalar(fTBias);
62 buffer.writeScalar(fTScale);
rileya@google.com589708b2012-07-26 20:04:23 +000063}
64
rileya@google.comd7cc6512012-07-27 14:00:39 +000065/////////////////////////////////////////////////////////////////////
66
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000067#if SK_SUPPORT_GPU
68
dandov9de5b512014-06-10 14:38:28 -070069#include "SkGr.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050070#include "GrShaderCaps.h"
egdanielf5294392015-10-21 07:14:17 -070071#include "gl/GrGLContext.h"
egdaniel7ea439b2015-12-03 09:20:44 -080072#include "glsl/GrGLSLFragmentShaderBuilder.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000073
rileya@google.com98e8b6d2012-07-31 20:38:06 +000074class GrSweepGradient : public GrGradientEffect {
75public:
fmenozzi55d318d2016-08-09 08:05:57 -070076 class GLSLSweepProcessor;
77
Brian Salomonaff329b2017-08-11 09:40:37 -040078 static std::unique_ptr<GrFragmentProcessor> Make(const CreateArgs& args, SkScalar tBias,
79 SkScalar tScale) {
Brian Osman5911a7c2017-10-25 12:52:31 -040080 return GrGradientEffect::AdjustFP(std::unique_ptr<GrSweepGradient>(
81 new GrSweepGradient(args, tBias, tScale)),
82 args);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000083 }
Brian Salomon6af27012017-06-09 08:21:42 -040084
mtklein36352bf2015-03-25 18:17:31 -070085 const char* name() const override { return "Sweep Gradient"; }
joshualitteb2a6762014-12-04 11:35:33 -080086
Brian Salomonaff329b2017-08-11 09:40:37 -040087 std::unique_ptr<GrFragmentProcessor> clone() const override {
88 return std::unique_ptr<GrFragmentProcessor>(new GrSweepGradient(*this));
Brian Salomonf8480b92017-07-27 15:45:59 -040089 }
90
bsalomon@google.comd4726202012-08-03 14:34:46 +000091private:
Florin Malita5a9a9812017-08-01 16:38:08 -040092 explicit GrSweepGradient(const CreateArgs& args, SkScalar tBias, SkScalar tScale)
Ethan Nicholasabff9562017-10-09 10:54:08 -040093 : INHERITED(kGrSweepGradient_ClassID, args, args.fShader->colorsAreOpaque())
Florin Malita5a9a9812017-08-01 16:38:08 -040094 , fTBias(tBias)
Ethan Nicholasabff9562017-10-09 10:54:08 -040095 , fTScale(tScale) {}
Brian Salomonf8480b92017-07-27 15:45:59 -040096
Florin Malita5a9a9812017-08-01 16:38:08 -040097 explicit GrSweepGradient(const GrSweepGradient& that)
98 : INHERITED(that)
99 , fTBias(that.fTBias)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400100 , fTScale(that.fTScale) {}
wangyix4b3050b2015-08-04 07:59:37 -0700101
fmenozzi55d318d2016-08-09 08:05:57 -0700102 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700103
Florin Malitae657dc82017-11-03 08:46:18 -0400104 bool onIsEqual(const GrFragmentProcessor& base) const override {
105 const GrSweepGradient& fp = base.cast<GrSweepGradient>();
106 return INHERITED::onIsEqual(base)
107 && fTBias == fp.fTBias
108 && fTScale == fp.fTScale;
109 }
Florin Malita5a9a9812017-08-01 16:38:08 -0400110
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400111 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000112
Florin Malita5a9a9812017-08-01 16:38:08 -0400113 SkScalar fTBias;
114 SkScalar fTScale;
115
rileya@google.com98e8b6d2012-07-31 20:38:06 +0000116 typedef GrGradientEffect INHERITED;
117};
118
119/////////////////////////////////////////////////////////////////////
120
fmenozzi55d318d2016-08-09 08:05:57 -0700121class GrSweepGradient::GLSLSweepProcessor : public GrGradientEffect::GLSLProcessor {
122public:
Florin Malita5a9a9812017-08-01 16:38:08 -0400123 GLSLSweepProcessor(const GrProcessor&)
124 : fCachedTBias(SK_FloatNaN)
125 , fCachedTScale(SK_FloatNaN) {}
fmenozzi55d318d2016-08-09 08:05:57 -0700126
Florin Malita5a9a9812017-08-01 16:38:08 -0400127 void emitCode(EmitArgs&) override;
fmenozzi55d318d2016-08-09 08:05:57 -0700128
Florin Malita5a9a9812017-08-01 16:38:08 -0400129protected:
130 void onSetData(const GrGLSLProgramDataManager& pdman,
131 const GrFragmentProcessor& processor) override {
132 INHERITED::onSetData(pdman, processor);
133 const GrSweepGradient& data = processor.cast<GrSweepGradient>();
fmenozzi55d318d2016-08-09 08:05:57 -0700134
Florin Malita5a9a9812017-08-01 16:38:08 -0400135 if (fCachedTBias != data.fTBias || fCachedTScale != data.fTScale) {
136 fCachedTBias = data.fTBias;
137 fCachedTScale = data.fTScale;
138 pdman.set2f(fTBiasScaleUni, fCachedTBias, fCachedTScale);
139 }
140 }
141
142private:
143 UniformHandle fTBiasScaleUni;
144
145 // Uploaded uniform values.
146 float fCachedTBias,
147 fCachedTScale;
148
149 typedef GrGradientEffect::GLSLProcessor INHERITED;
fmenozzi55d318d2016-08-09 08:05:57 -0700150};
151
152/////////////////////////////////////////////////////////////////////
153
154GrGLSLFragmentProcessor* GrSweepGradient::onCreateGLSLInstance() const {
155 return new GrSweepGradient::GLSLSweepProcessor(*this);
156}
157
fmenozzi55d318d2016-08-09 08:05:57 -0700158/////////////////////////////////////////////////////////////////////
159
joshualittb0a8a372014-09-23 09:50:21 -0700160GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSweepGradient);
bsalomon@google.comd4726202012-08-03 14:34:46 +0000161
Hal Canary6f6961e2017-01-31 13:50:44 -0500162#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400163std::unique_ptr<GrFragmentProcessor> GrSweepGradient::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700164 SkPoint center = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()};
bsalomon@google.comd4726202012-08-03 14:34:46 +0000165
Brian Osman3f748602016-10-03 18:29:03 -0400166 RandomGradientParams params(d->fRandom);
Brian Osmana2196532016-10-17 12:48:13 -0400167 auto shader = params.fUseColors4f ?
168 SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors4f, params.fColorSpace,
169 params.fStops, params.fColorCount) :
170 SkGradientShader::MakeSweep(center.fX, center.fY, params.fColors,
171 params.fStops, params.fColorCount);
Brian Osman9f532a32016-10-19 11:12:09 -0400172 GrTest::TestAsFPArgs asFPArgs(d);
Brian Salomonaff329b2017-08-11 09:40:37 -0400173 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
bsalomonc21b09e2015-08-28 18:46:56 -0700174 GrAlwaysAssert(fp);
joshualittb0a8a372014-09-23 09:50:21 -0700175 return fp;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000176}
Hal Canary6f6961e2017-01-31 13:50:44 -0500177#endif
bsalomon@google.comd4726202012-08-03 14:34:46 +0000178
179/////////////////////////////////////////////////////////////////////
180
fmenozzi55d318d2016-08-09 08:05:57 -0700181void GrSweepGradient::GLSLSweepProcessor::emitCode(EmitArgs& args) {
wangyix7c157a92015-07-22 15:08:53 -0700182 const GrSweepGradient& ge = args.fFp.cast<GrSweepGradient>();
Florin Malita5a9a9812017-08-01 16:38:08 -0400183 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
184 this->emitUniforms(uniformHandler, ge);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400185 fTBiasScaleUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
186 "SweepFSParams");
Florin Malita5a9a9812017-08-01 16:38:08 -0400187 const char* tBiasScaleV = uniformHandler->getUniformCStr(fTBiasScaleUni);
188
189 const SkString coords2D = args.fFragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
190
191 // On some devices they incorrectly implement atan2(y,x) as atan(y/x). In actuality it is
192 // atan2(y,x) = 2 * atan(y / (sqrt(x^2 + y^2) + x)). So to work around this we pass in
193 // (sqrt(x^2 + y^2) + x) as the second parameter to atan2 in these cases. We let the device
194 // handle the undefined behavior of the second paramenter being 0 instead of doing the
195 // divide ourselves and using atan instead.
196 const SkString atan = args.fShaderCaps->atan2ImplementedAsAtanYOverX()
197 ? SkStringPrintf("2.0 * atan(- %s.y, length(%s) - %s.x)",
198 coords2D.c_str(), coords2D.c_str(), coords2D.c_str())
199 : SkStringPrintf("atan(- %s.y, - %s.x)", coords2D.c_str(), coords2D.c_str());
200
commit-bot@chromium.org54318d32014-02-14 17:27:04 +0000201 // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi]
Florin Malita5a9a9812017-08-01 16:38:08 -0400202 const SkString t = SkStringPrintf("((%s * 0.1591549430918 + 0.5 + %s[0]) * %s[1])",
203 atan.c_str(), tBiasScaleV, tBiasScaleV);
204
egdaniel7ea439b2015-12-03 09:20:44 -0800205 this->emitColor(args.fFragBuilder,
206 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500207 args.fShaderCaps,
egdaniel4ca2e602015-11-18 08:01:26 -0800208 ge, t.c_str(),
209 args.fOutputColor,
210 args.fInputColor,
cdalton3f6f76f2016-04-11 12:18:09 -0700211 args.fTexSamplers);
rileya@google.comd7cc6512012-07-27 14:00:39 +0000212}
213
214/////////////////////////////////////////////////////////////////////
215
Brian Salomonaff329b2017-08-11 09:40:37 -0400216std::unique_ptr<GrFragmentProcessor> SkSweepGradient::asFragmentProcessor(
217 const AsFPArgs& args) const {
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000218 SkMatrix matrix;
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000219 if (!this->getLocalMatrix().invert(&matrix)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700220 return nullptr;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000221 }
brianosman839345d2016-07-22 11:04:53 -0700222 if (args.fLocalMatrix) {
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000223 SkMatrix inv;
brianosman839345d2016-07-22 11:04:53 -0700224 if (!args.fLocalMatrix->invert(&inv)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700225 return nullptr;
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000226 }
227 matrix.postConcat(inv);
228 }
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000229 matrix.postConcat(fPtsToUnit);
mtklein3f3b3d02014-12-01 11:47:08 -0800230
Brian Osman5911a7c2017-10-25 12:52:31 -0400231 return GrSweepGradient::Make(
Brian Salomonaff329b2017-08-11 09:40:37 -0400232 GrGradientEffect::CreateArgs(args.fContext, this, &matrix, fTileMode,
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400233 args.fDstColorSpaceInfo->colorSpace()),
Brian Salomonaff329b2017-08-11 09:40:37 -0400234 fTBias, fTScale);
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000235}
236
237#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000238
Matt Sarett6cc6ae752017-04-18 18:29:12 -0400239sk_sp<SkShader> SkSweepGradient::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
Florin Malita39d71de2017-10-31 11:33:49 -0400240 const AutoXformColors xformedColors(*this, xformer);
Florin Malita545630b2017-08-02 10:42:41 -0400241
242 SkScalar startAngle, endAngle;
243 std::tie(startAngle, endAngle) = angles_from_t_coeff(fTBias, fTScale);
244
Florin Malita39d71de2017-10-31 11:33:49 -0400245 return SkGradientShader::MakeSweep(fCenter.fX, fCenter.fY, xformedColors.fColors.get(),
246 fOrigPos, fColorCount, fTileMode, startAngle, endAngle,
Florin Malita545630b2017-08-02 10:42:41 -0400247 fGradFlags, &this->getLocalMatrix());
Matt Sarett6cc6ae752017-04-18 18:29:12 -0400248}
249
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000250#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000251void SkSweepGradient::toString(SkString* str) const {
252 str->append("SkSweepGradient: (");
253
254 str->append("center: (");
255 str->appendScalar(fCenter.fX);
256 str->append(", ");
257 str->appendScalar(fCenter.fY);
258 str->append(") ");
259
260 this->INHERITED::toString(str);
261
262 str->append(")");
263}
Herb Derby7eb86982017-05-02 19:04:39 -0400264
Florin Malita5a9a9812017-08-01 16:38:08 -0400265void SkSweepGradient::appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* p,
Florin Malita50b20842017-07-29 19:08:28 -0400266 SkRasterPipeline*) const {
Mike Klein5c7960b2017-05-11 10:59:22 -0400267 p->append(SkRasterPipeline::xy_to_unit_angle);
Florin Malita5a9a9812017-08-01 16:38:08 -0400268 p->append_matrix(alloc, SkMatrix::Concat(SkMatrix::MakeScale(fTScale, 1),
269 SkMatrix::MakeTrans(fTBias , 0)));
Herb Derby7eb86982017-05-02 19:04:39 -0400270}
271
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000272#endif