blob: 8ec4ffbbe87e5d286e2949a4a3f79e32eb0708ef [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"
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050015#include "src/sksl/dsl/priv/DSLFPs.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000016
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000017//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000018
egdaniel64c47282015-11-13 06:54:19 -080019class GrGLConvexPolyEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000020public:
robertphillips9cdb9922016-02-03 12:25:40 -080021 GrGLConvexPolyEffect() {
Brian Salomon73a850f2017-03-27 10:17:38 -040022 for (size_t i = 0; i < SK_ARRAY_COUNT(fPrevEdges); ++i) {
23 fPrevEdges[i] = SK_ScalarNaN;
24 }
robertphillips9cdb9922016-02-03 12:25:40 -080025 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000026
robertphillips9cdb9922016-02-03 12:25:40 -080027 void emitCode(EmitArgs&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000028
Brian Salomon94efbf52016-11-29 13:43:05 -050029 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000030
wangyixb1daa862015-08-18 11:29:31 -070031protected:
Brian Salomonab015ef2017-04-04 10:15:51 -040032 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000033
34private:
egdaniel018fb622015-10-28 07:26:40 -070035 GrGLSLProgramDataManager::UniformHandle fEdgeUniform;
robertphillipsbf536af2016-02-04 06:11:53 -080036 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
John Stiles7571f9e2020-09-02 22:42:33 -040037 using INHERITED = GrGLSLFragmentProcessor;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000038};
39
wangyix7c157a92015-07-22 15:08:53 -070040void GrGLConvexPolyEffect::emitCode(EmitArgs& args) {
41 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000042
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050043 using namespace SkSL::dsl;
44 StartFragmentProcessor(this, &args);
45 Var edgeArray(kUniform_Modifier, Array(kHalf3, cpe.getEdgeCount()));
46 fEdgeUniform = VarUniformHandle(edgeArray);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -050047 Var alpha(kHalf, "alpha", 1);
48 Declare(alpha);
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050049 Var edge(kHalf, "edge");
50 Declare(edge);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000051 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050052 edge = Dot(edgeArray[i], Half3(Swizzle(sk_FragCoord(), X, Y, ONE)));
joshualittb0a8a372014-09-23 09:50:21 -070053 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050054 edge = Saturate(edge);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000055 } else {
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050056 edge = Select(edge >= 0.5, 1.0, 0.0);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000057 }
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050058 alpha *= edge;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000059 }
60
joshualittb0a8a372014-09-23 09:50:21 -070061 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050062 alpha = 1.0 - alpha;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000063 }
John Stilesec9269b2020-06-15 16:05:14 -040064
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050065 Return(SampleChild(0) * alpha);
66 EndFragmentProcessor();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000067}
68
egdaniel018fb622015-10-28 07:26:40 -070069void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -040070 const GrFragmentProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -070071 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000072 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
73 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -070074 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000075 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
76 }
77}
78
Brian Salomon94efbf52016-11-29 13:43:05 -050079void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070080 GrProcessorKeyBuilder* b) {
81 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
Brian Salomon4dea72a2019-12-18 10:43:10 -050082 static_assert(kGrClipEdgeTypeCnt <= 8);
Ethan Nicholas1706f842017-11-10 11:58:19 -050083 uint32_t key = (cpe.getEdgeCount() << 3) | (int) cpe.getEdgeType();
bsalomon63e99f72014-07-21 08:03:14 -070084 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000085}
86
87//////////////////////////////////////////////////////////////////////////////
88
John Stiles72e57642020-06-24 10:42:35 -040089GrFPResult GrConvexPolyEffect::Make(std::unique_ptr<GrFragmentProcessor> inputFP,
90 GrClipEdgeType type, const SkPath& path) {
John Stilesf08a82b2020-06-16 16:34:12 -040091 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || !path.isConvex()) {
John Stiles72e57642020-06-24 10:42:35 -040092 return GrFPFailure(std::move(inputFP));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000093 }
94
Mike Reed85f51b22020-08-30 10:32:06 -040095 SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(path);
bsalomon7888de02016-03-28 15:04:45 -070096 // The only way this should fail is if the clip is effectively a infinitely thin line. In that
97 // case nothing is inside the clip. It'd be nice to detect this at a higher level and either
98 // skip the draw or omit the clip element.
Mike Reed85f51b22020-08-30 10:32:06 -040099 if (dir == SkPathFirstDirection::kUnknown) {
bsalomon7888de02016-03-28 15:04:45 -0700100 if (GrProcessorEdgeTypeIsInverseFill(type)) {
John Stiles85894302020-07-13 11:39:52 -0400101 return GrFPSuccess(
102 GrFragmentProcessor::ModulateRGBA(std::move(inputFP), SK_PMColor4fWHITE));
bsalomon7888de02016-03-28 15:04:45 -0700103 }
John Stiles7c196772020-07-13 10:00:16 -0400104 // This could use ConstColor instead of ModulateRGBA but it would trigger a debug print
Brian Salomoneb628292017-02-15 14:12:26 -0500105 // about a coverage processor not being compatible with the alpha-as-coverage optimization.
John Stiles7c196772020-07-13 10:00:16 -0400106 // We don't really care about this unlikely case so we just use ModulateRGBA to suppress
Brian Salomoneb628292017-02-15 14:12:26 -0500107 // the print.
John Stiles85894302020-07-13 11:39:52 -0400108 return GrFPSuccess(
109 GrFragmentProcessor::ModulateRGBA(std::move(inputFP), SK_PMColor4fTRANSPARENT));
bsalomon7888de02016-03-28 15:04:45 -0700110 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000111
lsalzmand15947e2016-05-31 09:46:00 -0700112 SkScalar edges[3 * kMaxEdges];
113 SkPoint pts[4];
114 SkPath::Verb verb;
115 SkPath::Iter iter(path, true);
116
117 // SkPath considers itself convex so long as there is a convex contour within it,
118 // regardless of any degenerate contours such as a string of moveTos before it.
119 // Iterate here to consume any degenerate contours and only process the points
120 // on the actual convex contour.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000121 int n = 0;
Mike Reedba7e9a62019-08-16 13:30:34 -0400122 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
lsalzmand15947e2016-05-31 09:46:00 -0700123 switch (verb) {
124 case SkPath::kMove_Verb:
125 SkASSERT(n == 0);
John Stiles30212b72020-06-11 17:55:07 -0400126 break;
lsalzmand15947e2016-05-31 09:46:00 -0700127 case SkPath::kClose_Verb:
128 break;
129 case SkPath::kLine_Verb: {
130 if (n >= kMaxEdges) {
John Stiles72e57642020-06-24 10:42:35 -0400131 return GrFPFailure(std::move(inputFP));
lsalzmand15947e2016-05-31 09:46:00 -0700132 }
Mike Reedba7e9a62019-08-16 13:30:34 -0400133 if (pts[0] != pts[1]) {
134 SkVector v = pts[1] - pts[0];
135 v.normalize();
Mike Reed3872c982020-08-29 17:46:51 -0400136 if (SkPathFirstDirection::kCCW == dir) {
Mike Reedba7e9a62019-08-16 13:30:34 -0400137 edges[3 * n] = v.fY;
138 edges[3 * n + 1] = -v.fX;
139 } else {
140 edges[3 * n] = -v.fY;
141 edges[3 * n + 1] = v.fX;
142 }
143 edges[3 * n + 2] = -(edges[3 * n] * pts[1].fX + edges[3 * n + 1] * pts[1].fY);
144 ++n;
lsalzmand15947e2016-05-31 09:46:00 -0700145 }
lsalzmand15947e2016-05-31 09:46:00 -0700146 break;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000147 }
lsalzmand15947e2016-05-31 09:46:00 -0700148 default:
John Stiles72e57642020-06-24 10:42:35 -0400149 return GrFPFailure(std::move(inputFP));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000150 }
151 }
lsalzmand15947e2016-05-31 09:46:00 -0700152
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000153 if (path.isInverseFillType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700154 type = GrInvertProcessorEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000155 }
John Stilesf08a82b2020-06-16 16:34:12 -0400156 return GrConvexPolyEffect::Make(std::move(inputFP), type, n, edges);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000157}
158
159GrConvexPolyEffect::~GrConvexPolyEffect() {}
160
Brian Salomon94efbf52016-11-29 13:43:05 -0500161void GrConvexPolyEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800162 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800163 GrGLConvexPolyEffect::GenKey(*this, caps, b);
164}
165
Brian Salomon18ab2032021-02-23 10:07:05 -0500166std::unique_ptr<GrGLSLFragmentProcessor> GrConvexPolyEffect::onMakeProgramImpl() const {
167 return std::make_unique<GrGLConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000168}
169
John Stilesec9269b2020-06-15 16:05:14 -0400170GrConvexPolyEffect::GrConvexPolyEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
171 GrClipEdgeType edgeType, int n, const SkScalar edges[])
Ethan Nicholasabff9562017-10-09 10:54:08 -0400172 : INHERITED(kGrConvexPolyEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomonf3b995b2017-02-15 10:22:23 -0500173 , fEdgeType(edgeType)
174 , fEdgeCount(n) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000175 // Factory function should have already ensured this.
176 SkASSERT(n <= kMaxEdges);
177 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
178 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
179 // and 100% covered in the non-AA case.
180 for (int i = 0; i < n; ++i) {
181 fEdges[3 * i + 2] += SK_ScalarHalf;
182 }
John Stilesec9269b2020-06-15 16:05:14 -0400183
Brian Osman54867de2020-07-10 14:22:57 -0400184 this->registerChild(std::move(inputFP));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000185}
186
Brian Salomonfcc527b2017-07-26 12:21:21 -0400187GrConvexPolyEffect::GrConvexPolyEffect(const GrConvexPolyEffect& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400188 : INHERITED(kGrConvexPolyEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomonfcc527b2017-07-26 12:21:21 -0400189 , fEdgeType(that.fEdgeType)
190 , fEdgeCount(that.fEdgeCount) {
John Stilesec9269b2020-06-15 16:05:14 -0400191 this->cloneAndRegisterAllChildProcessors(that);
Brian Salomonfcc527b2017-07-26 12:21:21 -0400192 memcpy(fEdges, that.fEdges, 3 * that.fEdgeCount * sizeof(SkScalar));
193}
194
Brian Salomonaff329b2017-08-11 09:40:37 -0400195std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::clone() const {
196 return std::unique_ptr<GrFragmentProcessor>(new GrConvexPolyEffect(*this));
Brian Salomonfcc527b2017-07-26 12:21:21 -0400197}
198
bsalomon0e08fc12014-10-15 08:19:04 -0700199bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700200 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000201 // ignore the fact that 0 == -0 and just use memcmp.
202 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
203 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
204}
205
206//////////////////////////////////////////////////////////////////////////////
207
joshualittb0a8a372014-09-23 09:50:21 -0700208GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000209
Hal Canary6f6961e2017-01-31 13:50:44 -0500210#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400211std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700212 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000213 SkScalar edges[kMaxEdges * 3];
214 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700215 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000216 }
217
John Stiles6609cb62020-07-17 14:52:12 -0400218 bool success;
219 std::unique_ptr<GrFragmentProcessor> fp = d->inputFP();
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000220 do {
John Stiles6609cb62020-07-17 14:52:12 -0400221 GrClipEdgeType edgeType =
222 static_cast<GrClipEdgeType>(d->fRandom->nextULessThan(kGrClipEdgeTypeCnt));
223 std::tie(success, fp) = GrConvexPolyEffect::Make(std::move(fp), edgeType, count, edges);
224 } while (!success);
joshualittb0a8a372014-09-23 09:50:21 -0700225 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000226}
Hal Canary6f6961e2017-01-31 13:50:44 -0500227#endif