commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 1 | /* |
| 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" |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 9 | #include "SkPathPriv.h" |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 10 | #include "effects/GrConstColorProcessor.h" |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 11 | #include "effects/GrSkSLFP.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 14 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLUniformHandler.h" |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 16 | |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 17 | GR_FP_SRC_STRING SKSL_AARECT_SRC = R"( |
| 18 | layout(key) in GrClipEdgeType edgeType; |
| 19 | layout(ctype=SkRect) in float4 rect; |
| 20 | uniform float4 rectUniform; |
| 21 | |
| 22 | void main(int x, int y, inout half4 color) { |
| 23 | half alpha; |
| 24 | @switch (edgeType) { |
| 25 | case GrClipEdgeType::kFillBW: // fall through |
| 26 | case GrClipEdgeType::kInverseFillBW: |
| 27 | // non-AA |
| 28 | alpha = all(greaterThan(float4(x, y, rectUniform.zw), |
| 29 | float4(rectUniform.xy, x, y))) ? 1 : 0; |
| 30 | break; |
| 31 | default: |
| 32 | // The amount of coverage removed in x and y by the edges is computed as a pair of |
| 33 | // negative numbers, xSub and ySub. |
| 34 | half xSub, ySub; |
| 35 | xSub = min(x - rectUniform.x, 0.0); |
| 36 | xSub += min(rectUniform.z - x, 0.0); |
| 37 | ySub = min(y - rectUniform.y, 0.0); |
| 38 | ySub += min(rectUniform.w - y, 0.0); |
| 39 | // Now compute coverage in x and y and multiply them to get the fraction of the pixel |
| 40 | // covered. |
| 41 | alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0)); |
| 42 | } |
| 43 | |
| 44 | @if (edgeType == GrClipEdgeType::kInverseFillBW || edgeType == GrClipEdgeType::kInverseFillAA) { |
| 45 | alpha = 1.0 - alpha; |
| 46 | } |
| 47 | color *= alpha; |
| 48 | } |
| 49 | )"; |
| 50 | |
| 51 | struct AARectInputs { |
| 52 | GrClipEdgeType fEdgeType; |
| 53 | SkRect fRect; |
| 54 | }; |
| 55 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 56 | ////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 57 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 58 | class GrGLConvexPolyEffect : public GrGLSLFragmentProcessor { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 59 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 60 | GrGLConvexPolyEffect() { |
Brian Salomon | 73a850f | 2017-03-27 10:17:38 -0400 | [diff] [blame] | 61 | for (size_t i = 0; i < SK_ARRAY_COUNT(fPrevEdges); ++i) { |
| 62 | fPrevEdges[i] = SK_ScalarNaN; |
| 63 | } |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 64 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 65 | |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 66 | void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 67 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 68 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 69 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 70 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 71 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 72 | |
| 73 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 74 | GrGLSLProgramDataManager::UniformHandle fEdgeUniform; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 75 | SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 76 | typedef GrGLSLFragmentProcessor INHERITED; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 79 | void GrGLConvexPolyEffect::emitCode(EmitArgs& args) { |
| 80 | const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 81 | |
| 82 | const char *edgeArrayName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 83 | fEdgeUniform = args.fUniformHandler->addUniformArray(kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 84 | kHalf3_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 85 | "edges", |
| 86 | cpe.getEdgeCount(), |
| 87 | &edgeArrayName); |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 88 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 89 | fragBuilder->codeAppend("\t\thalf alpha = 1.0;\n"); |
| 90 | fragBuilder->codeAppend("\t\thalf edge;\n"); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 91 | for (int i = 0; i < cpe.getEdgeCount(); ++i) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 92 | fragBuilder->codeAppendf("\t\tedge = dot(%s[%d], half3(sk_FragCoord.x, sk_FragCoord.y, " |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 93 | "1));\n", |
| 94 | edgeArrayName, i); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 95 | if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) { |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 96 | fragBuilder->codeAppend("\t\tedge = saturate(edge);\n"); |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 97 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 98 | fragBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n"); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 99 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 100 | fragBuilder->codeAppend("\t\talpha *= edge;\n"); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 101 | } |
| 102 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 103 | if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 104 | fragBuilder->codeAppend("\talpha = 1.0 - alpha;\n"); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 105 | } |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 106 | fragBuilder->codeAppendf("\t%s = %s * alpha;\n", args.fOutputColor, args.fInputColor); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 107 | } |
| 108 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 109 | void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 110 | const GrFragmentProcessor& effect) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 111 | const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 112 | size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar); |
| 113 | if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 114 | pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges()); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 115 | memcpy(fPrevEdges, cpe.getEdges(), byteSize); |
| 116 | } |
| 117 | } |
| 118 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 119 | void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 120 | GrProcessorKeyBuilder* b) { |
| 121 | const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>(); |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 122 | GR_STATIC_ASSERT(kGrClipEdgeTypeCnt <= 8); |
| 123 | uint32_t key = (cpe.getEdgeCount() << 3) | (int) cpe.getEdgeType(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 124 | b->add32(key); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | ////////////////////////////////////////////////////////////////////////////// |
| 128 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 129 | std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::Make(GrClipEdgeType type, |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 130 | const SkPath& path, |
| 131 | GrContext* context) { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 132 | if (GrClipEdgeType::kHairlineAA == type) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 133 | return nullptr; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 134 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 135 | if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 136 | !path.isConvex()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | return nullptr; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 138 | } |
| 139 | |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 140 | SkPathPriv::FirstDirection dir; |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 141 | // The only way this should fail is if the clip is effectively a infinitely thin line. In that |
| 142 | // case nothing is inside the clip. It'd be nice to detect this at a higher level and either |
| 143 | // skip the draw or omit the clip element. |
| 144 | if (!SkPathPriv::CheapComputeFirstDirection(path, &dir)) { |
| 145 | if (GrProcessorEdgeTypeIsInverseFill(type)) { |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 146 | return GrConstColorProcessor::Make(SK_PMColor4fWHITE, |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 147 | GrConstColorProcessor::InputMode::kModulateRGBA); |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 148 | } |
Brian Salomon | eb62829 | 2017-02-15 14:12:26 -0500 | [diff] [blame] | 149 | // This could use kIgnore instead of kModulateRGBA but it would trigger a debug print |
| 150 | // about a coverage processor not being compatible with the alpha-as-coverage optimization. |
| 151 | // We don't really care about this unlikely case so we just use kModulateRGBA to suppress |
| 152 | // the print. |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 153 | return GrConstColorProcessor::Make(SK_PMColor4fTRANSPARENT, |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 154 | GrConstColorProcessor::InputMode::kModulateRGBA); |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 155 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 156 | |
lsalzman | d15947e | 2016-05-31 09:46:00 -0700 | [diff] [blame] | 157 | SkScalar edges[3 * kMaxEdges]; |
| 158 | SkPoint pts[4]; |
| 159 | SkPath::Verb verb; |
| 160 | SkPath::Iter iter(path, true); |
| 161 | |
| 162 | // SkPath considers itself convex so long as there is a convex contour within it, |
| 163 | // regardless of any degenerate contours such as a string of moveTos before it. |
| 164 | // Iterate here to consume any degenerate contours and only process the points |
| 165 | // on the actual convex contour. |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 166 | int n = 0; |
lsalzman | d15947e | 2016-05-31 09:46:00 -0700 | [diff] [blame] | 167 | while ((verb = iter.next(pts, true, true)) != SkPath::kDone_Verb) { |
| 168 | switch (verb) { |
| 169 | case SkPath::kMove_Verb: |
| 170 | SkASSERT(n == 0); |
| 171 | case SkPath::kClose_Verb: |
| 172 | break; |
| 173 | case SkPath::kLine_Verb: { |
| 174 | if (n >= kMaxEdges) { |
| 175 | return nullptr; |
| 176 | } |
| 177 | SkVector v = pts[1] - pts[0]; |
| 178 | v.normalize(); |
| 179 | if (SkPathPriv::kCCW_FirstDirection == dir) { |
| 180 | edges[3 * n] = v.fY; |
| 181 | edges[3 * n + 1] = -v.fX; |
| 182 | } else { |
| 183 | edges[3 * n] = -v.fY; |
| 184 | edges[3 * n + 1] = v.fX; |
| 185 | } |
Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 186 | edges[3 * n + 2] = -(edges[3 * n] * pts[1].fX + edges[3 * n + 1] * pts[1].fY); |
lsalzman | d15947e | 2016-05-31 09:46:00 -0700 | [diff] [blame] | 187 | ++n; |
| 188 | break; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 189 | } |
lsalzman | d15947e | 2016-05-31 09:46:00 -0700 | [diff] [blame] | 190 | default: |
| 191 | return nullptr; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
lsalzman | d15947e | 2016-05-31 09:46:00 -0700 | [diff] [blame] | 194 | |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 195 | if (path.isInverseFillType()) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 196 | type = GrInvertProcessorEdgeType(type); |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 197 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 198 | return Make(type, n, edges); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 201 | static void aa_rect_set_data_hook(const GrGLSLProgramDataManager& pdman, |
| 202 | const GrGLSLSkSLFP& glslProc, |
| 203 | const AARectInputs& inputs, |
| 204 | SkRect* prevRect) { |
| 205 | if (inputs.fRect != *prevRect) { |
| 206 | const SkRect& newRect = GrProcessorEdgeTypeIsAA(inputs.fEdgeType) ? |
| 207 | inputs.fRect.makeInset(.5f, .5f) : inputs.fRect; |
| 208 | pdman.set4f(glslProc.uniformHandle(0), newRect.fLeft, newRect.fTop, newRect.fRight, |
| 209 | newRect.fBottom); |
| 210 | *prevRect = inputs.fRect; |
| 211 | } |
| 212 | } |
| 213 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 214 | std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::Make(GrClipEdgeType edgeType, |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 215 | const SkRect& rect, |
| 216 | GrContext* context) { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 217 | if (GrClipEdgeType::kHairlineAA == edgeType){ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 218 | return nullptr; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 219 | } |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 220 | static int aaRectIndex = GrSkSLFP::NewIndex(); |
| 221 | AARectInputs inputs; |
| 222 | inputs.fEdgeType = edgeType; |
| 223 | inputs.fRect = rect; |
| 224 | std::unique_ptr<GrSkSLFP> result = GrSkSLFP::Make(context, aaRectIndex, "AARect", |
| 225 | SKSL_AARECT_SRC, inputs, |
| 226 | SkRect::MakeXYWH(-1, -1, -1, -1), |
| 227 | aa_rect_set_data_hook); |
| 228 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 229 | } |
| 230 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 231 | GrConvexPolyEffect::~GrConvexPolyEffect() {} |
| 232 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 233 | void GrConvexPolyEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 234 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 235 | GrGLConvexPolyEffect::GenKey(*this, caps, b); |
| 236 | } |
| 237 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 238 | GrGLSLFragmentProcessor* GrConvexPolyEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 239 | return new GrGLConvexPolyEffect; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 242 | GrConvexPolyEffect::GrConvexPolyEffect(GrClipEdgeType edgeType, int n, const SkScalar edges[]) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 243 | : INHERITED(kGrConvexPolyEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 244 | , fEdgeType(edgeType) |
| 245 | , fEdgeCount(n) { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 246 | // Factory function should have already ensured this. |
| 247 | SkASSERT(n <= kMaxEdges); |
| 248 | memcpy(fEdges, edges, 3 * n * sizeof(SkScalar)); |
| 249 | // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case |
| 250 | // and 100% covered in the non-AA case. |
| 251 | for (int i = 0; i < n; ++i) { |
| 252 | fEdges[3 * i + 2] += SK_ScalarHalf; |
| 253 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Brian Salomon | fcc527b | 2017-07-26 12:21:21 -0400 | [diff] [blame] | 256 | GrConvexPolyEffect::GrConvexPolyEffect(const GrConvexPolyEffect& that) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 257 | : INHERITED(kGrConvexPolyEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Brian Salomon | fcc527b | 2017-07-26 12:21:21 -0400 | [diff] [blame] | 258 | , fEdgeType(that.fEdgeType) |
| 259 | , fEdgeCount(that.fEdgeCount) { |
Brian Salomon | fcc527b | 2017-07-26 12:21:21 -0400 | [diff] [blame] | 260 | memcpy(fEdges, that.fEdges, 3 * that.fEdgeCount * sizeof(SkScalar)); |
| 261 | } |
| 262 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 263 | std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::clone() const { |
| 264 | return std::unique_ptr<GrFragmentProcessor>(new GrConvexPolyEffect(*this)); |
Brian Salomon | fcc527b | 2017-07-26 12:21:21 -0400 | [diff] [blame] | 265 | } |
| 266 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 267 | bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 268 | const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 269 | // ignore the fact that 0 == -0 and just use memcmp. |
| 270 | return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount && |
| 271 | 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar))); |
| 272 | } |
| 273 | |
| 274 | ////////////////////////////////////////////////////////////////////////////// |
| 275 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 276 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 277 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 278 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 279 | std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 280 | int count = d->fRandom->nextULessThan(kMaxEdges) + 1; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 281 | SkScalar edges[kMaxEdges * 3]; |
| 282 | for (int i = 0; i < 3 * count; ++i) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 283 | edges[i] = d->fRandom->nextSScalar1(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 286 | std::unique_ptr<GrFragmentProcessor> fp; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 287 | do { |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 288 | GrClipEdgeType edgeType = static_cast<GrClipEdgeType>( |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 289 | d->fRandom->nextULessThan(kGrClipEdgeTypeCnt)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 290 | fp = GrConvexPolyEffect::Make(edgeType, count, edges); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 291 | } while (nullptr == fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 292 | return fp; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 293 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 294 | #endif |