blob: 6d94e257fc8c4fe351e7f705a929a8361609c136 [file] [log] [blame]
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +00001/*
2 * Copyright 2014 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 "src/core/SkPathPriv.h"
9#include "src/gpu/effects/GrConvexPolyEffect.h"
10#include "src/gpu/effects/generated/GrAARectEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
12#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
13#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
14#include "src/gpu/glsl/GrGLSLUniformHandler.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000015
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000016//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000017
egdaniel64c47282015-11-13 06:54:19 -080018class GrGLConvexPolyEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000019public:
robertphillips9cdb9922016-02-03 12:25:40 -080020 GrGLConvexPolyEffect() {
Brian Salomon73a850f2017-03-27 10:17:38 -040021 for (size_t i = 0; i < SK_ARRAY_COUNT(fPrevEdges); ++i) {
22 fPrevEdges[i] = SK_ScalarNaN;
23 }
robertphillips9cdb9922016-02-03 12:25:40 -080024 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000025
robertphillips9cdb9922016-02-03 12:25:40 -080026 void emitCode(EmitArgs&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000027
Brian Salomon94efbf52016-11-29 13:43:05 -050028 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000029
wangyixb1daa862015-08-18 11:29:31 -070030protected:
Brian Salomonab015ef2017-04-04 10:15:51 -040031 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000032
33private:
egdaniel018fb622015-10-28 07:26:40 -070034 GrGLSLProgramDataManager::UniformHandle fEdgeUniform;
robertphillipsbf536af2016-02-04 06:11:53 -080035 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
egdaniel64c47282015-11-13 06:54:19 -080036 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000037};
38
wangyix7c157a92015-07-22 15:08:53 -070039void GrGLConvexPolyEffect::emitCode(EmitArgs& args) {
40 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000041
42 const char *edgeArrayName;
Ethan Nicholas16464c32020-04-06 13:53:05 -040043 fEdgeUniform = args.fUniformHandler->addUniformArray(&cpe,
44 kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040045 kHalf3_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -080046 "edges",
47 cpe.getEdgeCount(),
48 &edgeArrayName);
cdalton85285412016-02-18 12:37:07 -080049 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040050 fragBuilder->codeAppend("\t\thalf alpha = 1.0;\n");
51 fragBuilder->codeAppend("\t\thalf edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000052 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
Ethan Nicholase1f55022019-02-05 17:17:40 -050053 fragBuilder->codeAppendf("\t\tedge = dot(%s[%d], half3(half(sk_FragCoord.x), "
54 "half(sk_FragCoord.y), "
55 "1));\n",
Ethan Nicholas38657112017-02-09 17:01:22 -050056 edgeArrayName, i);
joshualittb0a8a372014-09-23 09:50:21 -070057 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -040058 fragBuilder->codeAppend("\t\tedge = saturate(edge);\n");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000059 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080060 fragBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000061 }
egdaniel4ca2e602015-11-18 08:01:26 -080062 fragBuilder->codeAppend("\t\talpha *= edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000063 }
64
joshualittb0a8a372014-09-23 09:50:21 -070065 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -080066 fragBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000067 }
John Stilesec9269b2020-06-15 16:05:14 -040068
Brian Osman6b5dbb42020-07-15 15:31:05 -040069 SkString inputSample = this->invokeChild(/*childIndex=*/0, args);
John Stilesec9269b2020-06-15 16:05:14 -040070
71 fragBuilder->codeAppendf("\t%s = %s * alpha;\n", args.fOutputColor, inputSample.c_str());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000072}
73
egdaniel018fb622015-10-28 07:26:40 -070074void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -040075 const GrFragmentProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -070076 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000077 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
78 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -070079 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000080 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
81 }
82}
83
Brian Salomon94efbf52016-11-29 13:43:05 -050084void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070085 GrProcessorKeyBuilder* b) {
86 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
Brian Salomon4dea72a2019-12-18 10:43:10 -050087 static_assert(kGrClipEdgeTypeCnt <= 8);
Ethan Nicholas1706f842017-11-10 11:58:19 -050088 uint32_t key = (cpe.getEdgeCount() << 3) | (int) cpe.getEdgeType();
bsalomon63e99f72014-07-21 08:03:14 -070089 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000090}
91
92//////////////////////////////////////////////////////////////////////////////
93
John Stiles72e57642020-06-24 10:42:35 -040094GrFPResult GrConvexPolyEffect::Make(std::unique_ptr<GrFragmentProcessor> inputFP,
95 GrClipEdgeType type, const SkPath& path) {
John Stilesf08a82b2020-06-16 16:34:12 -040096 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || !path.isConvex()) {
John Stiles72e57642020-06-24 10:42:35 -040097 return GrFPFailure(std::move(inputFP));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000098 }
99
Mike Reed3872c982020-08-29 17:46:51 -0400100 SkPathFirstDirection dir;
bsalomon7888de02016-03-28 15:04:45 -0700101 // The only way this should fail is if the clip is effectively a infinitely thin line. In that
102 // case nothing is inside the clip. It'd be nice to detect this at a higher level and either
103 // skip the draw or omit the clip element.
104 if (!SkPathPriv::CheapComputeFirstDirection(path, &dir)) {
105 if (GrProcessorEdgeTypeIsInverseFill(type)) {
John Stiles85894302020-07-13 11:39:52 -0400106 return GrFPSuccess(
107 GrFragmentProcessor::ModulateRGBA(std::move(inputFP), SK_PMColor4fWHITE));
bsalomon7888de02016-03-28 15:04:45 -0700108 }
John Stiles7c196772020-07-13 10:00:16 -0400109 // This could use ConstColor instead of ModulateRGBA but it would trigger a debug print
Brian Salomoneb628292017-02-15 14:12:26 -0500110 // about a coverage processor not being compatible with the alpha-as-coverage optimization.
John Stiles7c196772020-07-13 10:00:16 -0400111 // We don't really care about this unlikely case so we just use ModulateRGBA to suppress
Brian Salomoneb628292017-02-15 14:12:26 -0500112 // the print.
John Stiles85894302020-07-13 11:39:52 -0400113 return GrFPSuccess(
114 GrFragmentProcessor::ModulateRGBA(std::move(inputFP), SK_PMColor4fTRANSPARENT));
bsalomon7888de02016-03-28 15:04:45 -0700115 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000116
lsalzmand15947e2016-05-31 09:46:00 -0700117 SkScalar edges[3 * kMaxEdges];
118 SkPoint pts[4];
119 SkPath::Verb verb;
120 SkPath::Iter iter(path, true);
121
122 // SkPath considers itself convex so long as there is a convex contour within it,
123 // regardless of any degenerate contours such as a string of moveTos before it.
124 // Iterate here to consume any degenerate contours and only process the points
125 // on the actual convex contour.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000126 int n = 0;
Mike Reedba7e9a62019-08-16 13:30:34 -0400127 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
lsalzmand15947e2016-05-31 09:46:00 -0700128 switch (verb) {
129 case SkPath::kMove_Verb:
130 SkASSERT(n == 0);
John Stiles30212b72020-06-11 17:55:07 -0400131 break;
lsalzmand15947e2016-05-31 09:46:00 -0700132 case SkPath::kClose_Verb:
133 break;
134 case SkPath::kLine_Verb: {
135 if (n >= kMaxEdges) {
John Stiles72e57642020-06-24 10:42:35 -0400136 return GrFPFailure(std::move(inputFP));
lsalzmand15947e2016-05-31 09:46:00 -0700137 }
Mike Reedba7e9a62019-08-16 13:30:34 -0400138 if (pts[0] != pts[1]) {
139 SkVector v = pts[1] - pts[0];
140 v.normalize();
Mike Reed3872c982020-08-29 17:46:51 -0400141 if (SkPathFirstDirection::kCCW == dir) {
Mike Reedba7e9a62019-08-16 13:30:34 -0400142 edges[3 * n] = v.fY;
143 edges[3 * n + 1] = -v.fX;
144 } else {
145 edges[3 * n] = -v.fY;
146 edges[3 * n + 1] = v.fX;
147 }
148 edges[3 * n + 2] = -(edges[3 * n] * pts[1].fX + edges[3 * n + 1] * pts[1].fY);
149 ++n;
lsalzmand15947e2016-05-31 09:46:00 -0700150 }
lsalzmand15947e2016-05-31 09:46:00 -0700151 break;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000152 }
lsalzmand15947e2016-05-31 09:46:00 -0700153 default:
John Stiles72e57642020-06-24 10:42:35 -0400154 return GrFPFailure(std::move(inputFP));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000155 }
156 }
lsalzmand15947e2016-05-31 09:46:00 -0700157
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000158 if (path.isInverseFillType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700159 type = GrInvertProcessorEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000160 }
John Stilesf08a82b2020-06-16 16:34:12 -0400161 return GrConvexPolyEffect::Make(std::move(inputFP), type, n, edges);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000162}
163
John Stiles72e57642020-06-24 10:42:35 -0400164GrFPResult GrConvexPolyEffect::Make(std::unique_ptr<GrFragmentProcessor> inputFP,
165 GrClipEdgeType edgeType, const SkRect& rect) {
John Stiles3b2c06c2020-06-17 12:45:57 -0400166 // TODO: Replace calls to this method with calling GrAARectEffect::Make directly
John Stiles72e57642020-06-24 10:42:35 -0400167 return GrFPSuccess(GrAARectEffect::Make(std::move(inputFP), edgeType, rect));
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000168}
169
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000170GrConvexPolyEffect::~GrConvexPolyEffect() {}
171
Brian Salomon94efbf52016-11-29 13:43:05 -0500172void GrConvexPolyEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800173 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800174 GrGLConvexPolyEffect::GenKey(*this, caps, b);
175}
176
egdaniel57d3b032015-11-13 11:57:27 -0800177GrGLSLFragmentProcessor* GrConvexPolyEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800178 return new GrGLConvexPolyEffect;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000179}
180
John Stilesec9269b2020-06-15 16:05:14 -0400181GrConvexPolyEffect::GrConvexPolyEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
182 GrClipEdgeType edgeType, int n, const SkScalar edges[])
Ethan Nicholasabff9562017-10-09 10:54:08 -0400183 : INHERITED(kGrConvexPolyEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomonf3b995b2017-02-15 10:22:23 -0500184 , fEdgeType(edgeType)
185 , fEdgeCount(n) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000186 // Factory function should have already ensured this.
187 SkASSERT(n <= kMaxEdges);
188 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
189 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
190 // and 100% covered in the non-AA case.
191 for (int i = 0; i < n; ++i) {
192 fEdges[3 * i + 2] += SK_ScalarHalf;
193 }
John Stilesec9269b2020-06-15 16:05:14 -0400194
Brian Osman54867de2020-07-10 14:22:57 -0400195 this->registerChild(std::move(inputFP));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000196}
197
Brian Salomonfcc527b2017-07-26 12:21:21 -0400198GrConvexPolyEffect::GrConvexPolyEffect(const GrConvexPolyEffect& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400199 : INHERITED(kGrConvexPolyEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomonfcc527b2017-07-26 12:21:21 -0400200 , fEdgeType(that.fEdgeType)
201 , fEdgeCount(that.fEdgeCount) {
John Stilesec9269b2020-06-15 16:05:14 -0400202 this->cloneAndRegisterAllChildProcessors(that);
Brian Salomonfcc527b2017-07-26 12:21:21 -0400203 memcpy(fEdges, that.fEdges, 3 * that.fEdgeCount * sizeof(SkScalar));
204}
205
Brian Salomonaff329b2017-08-11 09:40:37 -0400206std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::clone() const {
207 return std::unique_ptr<GrFragmentProcessor>(new GrConvexPolyEffect(*this));
Brian Salomonfcc527b2017-07-26 12:21:21 -0400208}
209
bsalomon0e08fc12014-10-15 08:19:04 -0700210bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700211 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000212 // ignore the fact that 0 == -0 and just use memcmp.
213 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
214 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
215}
216
217//////////////////////////////////////////////////////////////////////////////
218
joshualittb0a8a372014-09-23 09:50:21 -0700219GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000220
Hal Canary6f6961e2017-01-31 13:50:44 -0500221#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400222std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700223 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000224 SkScalar edges[kMaxEdges * 3];
225 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700226 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000227 }
228
John Stiles6609cb62020-07-17 14:52:12 -0400229 bool success;
230 std::unique_ptr<GrFragmentProcessor> fp = d->inputFP();
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000231 do {
John Stiles6609cb62020-07-17 14:52:12 -0400232 GrClipEdgeType edgeType =
233 static_cast<GrClipEdgeType>(d->fRandom->nextULessThan(kGrClipEdgeTypeCnt));
234 std::tie(success, fp) = GrConvexPolyEffect::Make(std::move(fp), edgeType, count, edges);
235 } while (!success);
joshualittb0a8a372014-09-23 09:50:21 -0700236 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000237}
Hal Canary6f6961e2017-01-31 13:50:44 -0500238#endif