blob: 1b4acae008e0efb1224941ca156d06e902160dba [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
11#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
12#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
13#include "src/gpu/glsl/GrGLSLUniformHandler.h"
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050014#include "src/sksl/dsl/priv/DSLFPs.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
Brian Salomon3176e862021-08-09 11:23:04 -040018class GrGLConvexPolyEffect : public GrFragmentProcessor::ProgramImpl {
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];
Brian Salomon3176e862021-08-09 11:23:04 -040036 using INHERITED = ProgramImpl;
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
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050042 using namespace SkSL::dsl;
43 StartFragmentProcessor(this, &args);
Ethan Nicholasa2d22b22021-07-15 10:35:54 -040044 GlobalVar edgeArray(kUniform_Modifier, Array(kHalf3_Type, cpe.getEdgeCount()), "edgeArray");
45 Declare(edgeArray);
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050046 fEdgeUniform = VarUniformHandle(edgeArray);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040047 Var alpha(kHalf_Type, "alpha", 1);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -050048 Declare(alpha);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040049 Var edge(kHalf_Type, "edge");
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050050 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)));
Brian Osman50f0dad2021-07-08 13:47:25 -040053 if (GrClipEdgeTypeIsAA(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
Brian Osman50f0dad2021-07-08 13:47:25 -040061 if (GrClipEdgeTypeIsInverseFill(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) {
Brian Osman50f0dad2021-07-08 13:47:25 -0400100 if (GrClipEdgeTypeIsInverseFill(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:
lsalzmand15947e2016-05-31 09:46:00 -0700125 case SkPath::kClose_Verb:
126 break;
127 case SkPath::kLine_Verb: {
128 if (n >= kMaxEdges) {
John Stiles72e57642020-06-24 10:42:35 -0400129 return GrFPFailure(std::move(inputFP));
lsalzmand15947e2016-05-31 09:46:00 -0700130 }
Mike Reedba7e9a62019-08-16 13:30:34 -0400131 if (pts[0] != pts[1]) {
132 SkVector v = pts[1] - pts[0];
133 v.normalize();
Mike Reed3872c982020-08-29 17:46:51 -0400134 if (SkPathFirstDirection::kCCW == dir) {
Mike Reedba7e9a62019-08-16 13:30:34 -0400135 edges[3 * n] = v.fY;
136 edges[3 * n + 1] = -v.fX;
137 } else {
138 edges[3 * n] = -v.fY;
139 edges[3 * n + 1] = v.fX;
140 }
141 edges[3 * n + 2] = -(edges[3 * n] * pts[1].fX + edges[3 * n + 1] * pts[1].fY);
142 ++n;
lsalzmand15947e2016-05-31 09:46:00 -0700143 }
lsalzmand15947e2016-05-31 09:46:00 -0700144 break;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000145 }
lsalzmand15947e2016-05-31 09:46:00 -0700146 default:
Brian Salomon6b5b7e72021-05-17 10:37:04 -0400147 // Non-linear segment so not a polygon.
John Stiles72e57642020-06-24 10:42:35 -0400148 return GrFPFailure(std::move(inputFP));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000149 }
150 }
lsalzmand15947e2016-05-31 09:46:00 -0700151
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000152 if (path.isInverseFillType()) {
Brian Osman50f0dad2021-07-08 13:47:25 -0400153 type = GrInvertClipEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000154 }
John Stilesf08a82b2020-06-16 16:34:12 -0400155 return GrConvexPolyEffect::Make(std::move(inputFP), type, n, edges);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000156}
157
158GrConvexPolyEffect::~GrConvexPolyEffect() {}
159
Brian Salomon13b28732021-08-06 15:33:58 -0400160void GrConvexPolyEffect::onAddToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800161 GrGLConvexPolyEffect::GenKey(*this, caps, b);
162}
163
Brian Salomon3176e862021-08-09 11:23:04 -0400164std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrConvexPolyEffect::onMakeProgramImpl() const {
Brian Salomon18ab2032021-02-23 10:07:05 -0500165 return std::make_unique<GrGLConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000166}
167
John Stilesec9269b2020-06-15 16:05:14 -0400168GrConvexPolyEffect::GrConvexPolyEffect(std::unique_ptr<GrFragmentProcessor> inputFP,
Brian Osman8c281fb2021-04-29 15:16:11 -0400169 GrClipEdgeType edgeType,
170 int n,
171 const SkScalar edges[])
172 : INHERITED(kGrConvexPolyEffect_ClassID,
173 ProcessorOptimizationFlags(inputFP.get()) &
174 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomonf3b995b2017-02-15 10:22:23 -0500175 , fEdgeType(edgeType)
176 , fEdgeCount(n) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000177 // Factory function should have already ensured this.
178 SkASSERT(n <= kMaxEdges);
179 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
180 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
181 // and 100% covered in the non-AA case.
182 for (int i = 0; i < n; ++i) {
183 fEdges[3 * i + 2] += SK_ScalarHalf;
184 }
John Stilesec9269b2020-06-15 16:05:14 -0400185
Brian Osman54867de2020-07-10 14:22:57 -0400186 this->registerChild(std::move(inputFP));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000187}
188
Brian Salomonfcc527b2017-07-26 12:21:21 -0400189GrConvexPolyEffect::GrConvexPolyEffect(const GrConvexPolyEffect& that)
John Stiles307f8f52021-08-09 15:36:59 -0400190 : INHERITED(that)
Brian Salomonfcc527b2017-07-26 12:21:21 -0400191 , fEdgeType(that.fEdgeType)
192 , fEdgeCount(that.fEdgeCount) {
Brian Salomonfcc527b2017-07-26 12:21:21 -0400193 memcpy(fEdges, that.fEdges, 3 * that.fEdgeCount * sizeof(SkScalar));
194}
195
Brian Salomonaff329b2017-08-11 09:40:37 -0400196std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::clone() const {
197 return std::unique_ptr<GrFragmentProcessor>(new GrConvexPolyEffect(*this));
Brian Salomonfcc527b2017-07-26 12:21:21 -0400198}
199
bsalomon0e08fc12014-10-15 08:19:04 -0700200bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700201 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000202 // ignore the fact that 0 == -0 and just use memcmp.
203 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
204 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
205}
206
207//////////////////////////////////////////////////////////////////////////////
208
joshualittb0a8a372014-09-23 09:50:21 -0700209GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000210
Hal Canary6f6961e2017-01-31 13:50:44 -0500211#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400212std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700213 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000214 SkScalar edges[kMaxEdges * 3];
215 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700216 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000217 }
218
John Stiles6609cb62020-07-17 14:52:12 -0400219 bool success;
220 std::unique_ptr<GrFragmentProcessor> fp = d->inputFP();
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000221 do {
John Stiles6609cb62020-07-17 14:52:12 -0400222 GrClipEdgeType edgeType =
223 static_cast<GrClipEdgeType>(d->fRandom->nextULessThan(kGrClipEdgeTypeCnt));
224 std::tie(success, fp) = GrConvexPolyEffect::Make(std::move(fp), edgeType, count, edges);
225 } while (!success);
joshualittb0a8a372014-09-23 09:50:21 -0700226 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000227}
Hal Canary6f6961e2017-01-31 13:50:44 -0500228#endif