blob: 96b8b0c6bb9ec639fdcfde680b51bdaaaf6bdc1c [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);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -040045 Var edgeArray(kUniform_Modifier, Array(kHalf3_Type, cpe.getEdgeCount()), "edgeArray");
46 DeclareGlobal(edgeArray);
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050047 fEdgeUniform = VarUniformHandle(edgeArray);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040048 Var alpha(kHalf_Type, "alpha", 1);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -050049 Declare(alpha);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040050 Var edge(kHalf_Type, "edge");
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050051 Declare(edge);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000052 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050053 edge = Dot(edgeArray[i], Half3(Swizzle(sk_FragCoord(), X, Y, ONE)));
joshualittb0a8a372014-09-23 09:50:21 -070054 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050055 edge = Saturate(edge);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000056 } else {
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050057 edge = Select(edge >= 0.5, 1.0, 0.0);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000058 }
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050059 alpha *= edge;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000060 }
61
joshualittb0a8a372014-09-23 09:50:21 -070062 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050063 alpha = 1.0 - alpha;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000064 }
John Stilesec9269b2020-06-15 16:05:14 -040065
Ethan Nicholas9f8e9ec2021-02-17 14:47:52 -050066 Return(SampleChild(0) * alpha);
67 EndFragmentProcessor();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000068}
69
egdaniel018fb622015-10-28 07:26:40 -070070void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -040071 const GrFragmentProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -070072 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000073 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
74 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -070075 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000076 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
77 }
78}
79
Brian Salomon94efbf52016-11-29 13:43:05 -050080void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070081 GrProcessorKeyBuilder* b) {
82 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
Brian Salomon4dea72a2019-12-18 10:43:10 -050083 static_assert(kGrClipEdgeTypeCnt <= 8);
Ethan Nicholas1706f842017-11-10 11:58:19 -050084 uint32_t key = (cpe.getEdgeCount() << 3) | (int) cpe.getEdgeType();
bsalomon63e99f72014-07-21 08:03:14 -070085 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000086}
87
88//////////////////////////////////////////////////////////////////////////////
89
John Stiles72e57642020-06-24 10:42:35 -040090GrFPResult GrConvexPolyEffect::Make(std::unique_ptr<GrFragmentProcessor> inputFP,
91 GrClipEdgeType type, const SkPath& path) {
John Stilesf08a82b2020-06-16 16:34:12 -040092 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || !path.isConvex()) {
John Stiles72e57642020-06-24 10:42:35 -040093 return GrFPFailure(std::move(inputFP));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000094 }
95
Mike Reed85f51b22020-08-30 10:32:06 -040096 SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(path);
bsalomon7888de02016-03-28 15:04:45 -070097 // The only way this should fail is if the clip is effectively a infinitely thin line. In that
98 // case nothing is inside the clip. It'd be nice to detect this at a higher level and either
99 // skip the draw or omit the clip element.
Mike Reed85f51b22020-08-30 10:32:06 -0400100 if (dir == SkPathFirstDirection::kUnknown) {
bsalomon7888de02016-03-28 15:04:45 -0700101 if (GrProcessorEdgeTypeIsInverseFill(type)) {
John Stiles85894302020-07-13 11:39:52 -0400102 return GrFPSuccess(
103 GrFragmentProcessor::ModulateRGBA(std::move(inputFP), SK_PMColor4fWHITE));
bsalomon7888de02016-03-28 15:04:45 -0700104 }
John Stiles7c196772020-07-13 10:00:16 -0400105 // This could use ConstColor instead of ModulateRGBA but it would trigger a debug print
Brian Salomoneb628292017-02-15 14:12:26 -0500106 // about a coverage processor not being compatible with the alpha-as-coverage optimization.
John Stiles7c196772020-07-13 10:00:16 -0400107 // We don't really care about this unlikely case so we just use ModulateRGBA to suppress
Brian Salomoneb628292017-02-15 14:12:26 -0500108 // the print.
John Stiles85894302020-07-13 11:39:52 -0400109 return GrFPSuccess(
110 GrFragmentProcessor::ModulateRGBA(std::move(inputFP), SK_PMColor4fTRANSPARENT));
bsalomon7888de02016-03-28 15:04:45 -0700111 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000112
lsalzmand15947e2016-05-31 09:46:00 -0700113 SkScalar edges[3 * kMaxEdges];
114 SkPoint pts[4];
115 SkPath::Verb verb;
116 SkPath::Iter iter(path, true);
117
118 // SkPath considers itself convex so long as there is a convex contour within it,
119 // regardless of any degenerate contours such as a string of moveTos before it.
120 // Iterate here to consume any degenerate contours and only process the points
121 // on the actual convex contour.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000122 int n = 0;
Mike Reedba7e9a62019-08-16 13:30:34 -0400123 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
lsalzmand15947e2016-05-31 09:46:00 -0700124 switch (verb) {
125 case SkPath::kMove_Verb:
lsalzmand15947e2016-05-31 09:46:00 -0700126 case SkPath::kClose_Verb:
127 break;
128 case SkPath::kLine_Verb: {
129 if (n >= kMaxEdges) {
John Stiles72e57642020-06-24 10:42:35 -0400130 return GrFPFailure(std::move(inputFP));
lsalzmand15947e2016-05-31 09:46:00 -0700131 }
Mike Reedba7e9a62019-08-16 13:30:34 -0400132 if (pts[0] != pts[1]) {
133 SkVector v = pts[1] - pts[0];
134 v.normalize();
Mike Reed3872c982020-08-29 17:46:51 -0400135 if (SkPathFirstDirection::kCCW == dir) {
Mike Reedba7e9a62019-08-16 13:30:34 -0400136 edges[3 * n] = v.fY;
137 edges[3 * n + 1] = -v.fX;
138 } else {
139 edges[3 * n] = -v.fY;
140 edges[3 * n + 1] = v.fX;
141 }
142 edges[3 * n + 2] = -(edges[3 * n] * pts[1].fX + edges[3 * n + 1] * pts[1].fY);
143 ++n;
lsalzmand15947e2016-05-31 09:46:00 -0700144 }
lsalzmand15947e2016-05-31 09:46:00 -0700145 break;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000146 }
lsalzmand15947e2016-05-31 09:46:00 -0700147 default:
Brian Salomon6b5b7e72021-05-17 10:37:04 -0400148 // Non-linear segment so not a polygon.
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,
Brian Osman8c281fb2021-04-29 15:16:11 -0400171 GrClipEdgeType edgeType,
172 int n,
173 const SkScalar edges[])
174 : INHERITED(kGrConvexPolyEffect_ClassID,
175 ProcessorOptimizationFlags(inputFP.get()) &
176 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomonf3b995b2017-02-15 10:22:23 -0500177 , fEdgeType(edgeType)
178 , fEdgeCount(n) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000179 // Factory function should have already ensured this.
180 SkASSERT(n <= kMaxEdges);
181 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
182 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
183 // and 100% covered in the non-AA case.
184 for (int i = 0; i < n; ++i) {
185 fEdges[3 * i + 2] += SK_ScalarHalf;
186 }
John Stilesec9269b2020-06-15 16:05:14 -0400187
Brian Osman54867de2020-07-10 14:22:57 -0400188 this->registerChild(std::move(inputFP));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000189}
190
Brian Salomonfcc527b2017-07-26 12:21:21 -0400191GrConvexPolyEffect::GrConvexPolyEffect(const GrConvexPolyEffect& that)
Brian Osman8c281fb2021-04-29 15:16:11 -0400192 : INHERITED(kGrConvexPolyEffect_ClassID, that.optimizationFlags())
Brian Salomonfcc527b2017-07-26 12:21:21 -0400193 , fEdgeType(that.fEdgeType)
194 , fEdgeCount(that.fEdgeCount) {
John Stilesec9269b2020-06-15 16:05:14 -0400195 this->cloneAndRegisterAllChildProcessors(that);
Brian Salomonfcc527b2017-07-26 12:21:21 -0400196 memcpy(fEdges, that.fEdges, 3 * that.fEdgeCount * sizeof(SkScalar));
197}
198
Brian Salomonaff329b2017-08-11 09:40:37 -0400199std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::clone() const {
200 return std::unique_ptr<GrFragmentProcessor>(new GrConvexPolyEffect(*this));
Brian Salomonfcc527b2017-07-26 12:21:21 -0400201}
202
bsalomon0e08fc12014-10-15 08:19:04 -0700203bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700204 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000205 // ignore the fact that 0 == -0 and just use memcmp.
206 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
207 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
208}
209
210//////////////////////////////////////////////////////////////////////////////
211
joshualittb0a8a372014-09-23 09:50:21 -0700212GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000213
Hal Canary6f6961e2017-01-31 13:50:44 -0500214#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400215std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700216 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000217 SkScalar edges[kMaxEdges * 3];
218 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700219 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000220 }
221
John Stiles6609cb62020-07-17 14:52:12 -0400222 bool success;
223 std::unique_ptr<GrFragmentProcessor> fp = d->inputFP();
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000224 do {
John Stiles6609cb62020-07-17 14:52:12 -0400225 GrClipEdgeType edgeType =
226 static_cast<GrClipEdgeType>(d->fRandom->nextULessThan(kGrClipEdgeTypeCnt));
227 std::tie(success, fp) = GrConvexPolyEffect::Make(std::move(fp), edgeType, count, edges);
228 } while (!success);
joshualittb0a8a372014-09-23 09:50:21 -0700229 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000230}
Hal Canary6f6961e2017-01-31 13:50:44 -0500231#endif