blob: 625427a179f19ecf057b6122cb4f98107853c0d3 [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
8#include "GrConvexPolyEffect.h"
reed026beb52015-06-10 14:23:15 -07009#include "SkPathPriv.h"
Ethan Nicholaseace9352018-10-15 20:09:54 +000010#include "effects/GrAARectEffect.h"
bsalomon7888de02016-03-28 15:04:45 -070011#include "effects/GrConstColorProcessor.h"
egdaniel64c47282015-11-13 06:54:19 -080012#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080013#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070014#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080015#include "glsl/GrGLSLUniformHandler.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];
egdaniel64c47282015-11-13 06:54:19 -080037 typedef GrGLSLFragmentProcessor INHERITED;
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
43 const char *edgeArrayName;
cdalton5e58cee2016-02-11 12:49:47 -080044 fEdgeUniform = args.fUniformHandler->addUniformArray(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 }
Ethan Nicholas2983f402017-05-08 09:36:08 -040068 fragBuilder->codeAppendf("\t%s = %s * alpha;\n", args.fOutputColor, args.fInputColor);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000069}
70
egdaniel018fb622015-10-28 07:26:40 -070071void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -040072 const GrFragmentProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -070073 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000074 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
75 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -070076 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000077 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
78 }
79}
80
Brian Salomon94efbf52016-11-29 13:43:05 -050081void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070082 GrProcessorKeyBuilder* b) {
83 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
Ethan Nicholas1706f842017-11-10 11:58:19 -050084 GR_STATIC_ASSERT(kGrClipEdgeTypeCnt <= 8);
85 uint32_t key = (cpe.getEdgeCount() << 3) | (int) cpe.getEdgeType();
bsalomon63e99f72014-07-21 08:03:14 -070086 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000087}
88
89//////////////////////////////////////////////////////////////////////////////
90
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050091std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::Make(GrClipEdgeType type,
Ethan Nicholaseace9352018-10-15 20:09:54 +000092 const SkPath& path) {
Ethan Nicholas1706f842017-11-10 11:58:19 -050093 if (GrClipEdgeType::kHairlineAA == type) {
halcanary96fcdcc2015-08-27 07:41:13 -070094 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000095 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000096 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000097 !path.isConvex()) {
halcanary96fcdcc2015-08-27 07:41:13 -070098 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000099 }
100
reed026beb52015-06-10 14:23:15 -0700101 SkPathPriv::FirstDirection dir;
bsalomon7888de02016-03-28 15:04:45 -0700102 // The only way this should fail is if the clip is effectively a infinitely thin line. In that
103 // case nothing is inside the clip. It'd be nice to detect this at a higher level and either
104 // skip the draw or omit the clip element.
105 if (!SkPathPriv::CheapComputeFirstDirection(path, &dir)) {
106 if (GrProcessorEdgeTypeIsInverseFill(type)) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400107 return GrConstColorProcessor::Make(SK_PMColor4fWHITE,
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500108 GrConstColorProcessor::InputMode::kModulateRGBA);
bsalomon7888de02016-03-28 15:04:45 -0700109 }
Brian Salomoneb628292017-02-15 14:12:26 -0500110 // This could use kIgnore instead of kModulateRGBA but it would trigger a debug print
111 // about a coverage processor not being compatible with the alpha-as-coverage optimization.
112 // We don't really care about this unlikely case so we just use kModulateRGBA to suppress
113 // the print.
Brian Osmanf28e55d2018-10-03 16:35:54 -0400114 return GrConstColorProcessor::Make(SK_PMColor4fTRANSPARENT,
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500115 GrConstColorProcessor::InputMode::kModulateRGBA);
bsalomon7888de02016-03-28 15:04:45 -0700116 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000117
lsalzmand15947e2016-05-31 09:46:00 -0700118 SkScalar edges[3 * kMaxEdges];
119 SkPoint pts[4];
120 SkPath::Verb verb;
121 SkPath::Iter iter(path, true);
122
123 // SkPath considers itself convex so long as there is a convex contour within it,
124 // regardless of any degenerate contours such as a string of moveTos before it.
125 // Iterate here to consume any degenerate contours and only process the points
126 // on the actual convex contour.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000127 int n = 0;
lsalzmand15947e2016-05-31 09:46:00 -0700128 while ((verb = iter.next(pts, true, true)) != SkPath::kDone_Verb) {
129 switch (verb) {
130 case SkPath::kMove_Verb:
131 SkASSERT(n == 0);
132 case SkPath::kClose_Verb:
133 break;
134 case SkPath::kLine_Verb: {
135 if (n >= kMaxEdges) {
136 return nullptr;
137 }
138 SkVector v = pts[1] - pts[0];
139 v.normalize();
140 if (SkPathPriv::kCCW_FirstDirection == dir) {
141 edges[3 * n] = v.fY;
142 edges[3 * n + 1] = -v.fX;
143 } else {
144 edges[3 * n] = -v.fY;
145 edges[3 * n + 1] = v.fX;
146 }
Brian Salomon9a767722017-03-13 17:57:28 -0400147 edges[3 * n + 2] = -(edges[3 * n] * pts[1].fX + edges[3 * n + 1] * pts[1].fY);
lsalzmand15947e2016-05-31 09:46:00 -0700148 ++n;
149 break;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000150 }
lsalzmand15947e2016-05-31 09:46:00 -0700151 default:
152 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000153 }
154 }
lsalzmand15947e2016-05-31 09:46:00 -0700155
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000156 if (path.isInverseFillType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700157 type = GrInvertProcessorEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000158 }
bungeman06ca8ec2016-06-09 08:01:03 -0700159 return Make(type, n, edges);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000160}
161
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500162std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::Make(GrClipEdgeType edgeType,
Ethan Nicholaseace9352018-10-15 20:09:54 +0000163 const SkRect& rect) {
Ethan Nicholas1706f842017-11-10 11:58:19 -0500164 if (GrClipEdgeType::kHairlineAA == edgeType){
halcanary96fcdcc2015-08-27 07:41:13 -0700165 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000166 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000167 return GrAARectEffect::Make(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
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500181GrConvexPolyEffect::GrConvexPolyEffect(GrClipEdgeType edgeType, int n, const SkScalar edges[])
Ethan Nicholasabff9562017-10-09 10:54:08 -0400182 : INHERITED(kGrConvexPolyEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomonf3b995b2017-02-15 10:22:23 -0500183 , fEdgeType(edgeType)
184 , fEdgeCount(n) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000185 // Factory function should have already ensured this.
186 SkASSERT(n <= kMaxEdges);
187 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
188 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
189 // and 100% covered in the non-AA case.
190 for (int i = 0; i < n; ++i) {
191 fEdges[3 * i + 2] += SK_ScalarHalf;
192 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000193}
194
Brian Salomonfcc527b2017-07-26 12:21:21 -0400195GrConvexPolyEffect::GrConvexPolyEffect(const GrConvexPolyEffect& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400196 : INHERITED(kGrConvexPolyEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomonfcc527b2017-07-26 12:21:21 -0400197 , fEdgeType(that.fEdgeType)
198 , fEdgeCount(that.fEdgeCount) {
Brian Salomonfcc527b2017-07-26 12:21:21 -0400199 memcpy(fEdges, that.fEdges, 3 * that.fEdgeCount * sizeof(SkScalar));
200}
201
Brian Salomonaff329b2017-08-11 09:40:37 -0400202std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::clone() const {
203 return std::unique_ptr<GrFragmentProcessor>(new GrConvexPolyEffect(*this));
Brian Salomonfcc527b2017-07-26 12:21:21 -0400204}
205
bsalomon0e08fc12014-10-15 08:19:04 -0700206bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700207 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000208 // ignore the fact that 0 == -0 and just use memcmp.
209 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
210 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
211}
212
213//////////////////////////////////////////////////////////////////////////////
214
joshualittb0a8a372014-09-23 09:50:21 -0700215GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000216
Hal Canary6f6961e2017-01-31 13:50:44 -0500217#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400218std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700219 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000220 SkScalar edges[kMaxEdges * 3];
221 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700222 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000223 }
224
Brian Salomonaff329b2017-08-11 09:40:37 -0400225 std::unique_ptr<GrFragmentProcessor> fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000226 do {
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500227 GrClipEdgeType edgeType = static_cast<GrClipEdgeType>(
Ethan Nicholas1706f842017-11-10 11:58:19 -0500228 d->fRandom->nextULessThan(kGrClipEdgeTypeCnt));
bungeman06ca8ec2016-06-09 08:01:03 -0700229 fp = GrConvexPolyEffect::Make(edgeType, count, edges);
halcanary96fcdcc2015-08-27 07:41:13 -0700230 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -0700231 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000232}
Hal Canary6f6961e2017-01-31 13:50:44 -0500233#endif