GrAARectEffect.fp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2018 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. layout(key) in GrClipEdgeType edgeType;
  8. layout(ctype=SkRect) in float4 rect;
  9. layout(ctype=SkRect) float4 prevRect = float4(-1);
  10. uniform float4 rectUniform;
  11. @optimizationFlags { kCompatibleWithCoverageAsAlpha_OptimizationFlag }
  12. void main() {
  13. half alpha;
  14. @switch (edgeType) {
  15. case GrClipEdgeType::kFillBW: // fall through
  16. case GrClipEdgeType::kInverseFillBW:
  17. // non-AA
  18. alpha = all(greaterThan(float4(sk_FragCoord.xy, rectUniform.zw),
  19. float4(rectUniform.xy, sk_FragCoord.xy))) ? 1 : 0;
  20. break;
  21. default:
  22. // The amount of coverage removed in x and y by the edges is computed as a pair of
  23. // negative numbers, xSub and ySub.
  24. half xSub, ySub;
  25. xSub = min(half(sk_FragCoord.x - rectUniform.x), 0.0);
  26. xSub += min(half(rectUniform.z - sk_FragCoord.x), 0.0);
  27. ySub = min(half(sk_FragCoord.y - rectUniform.y), 0.0);
  28. ySub += min(half(rectUniform.w - sk_FragCoord.y), 0.0);
  29. // Now compute coverage in x and y and multiply them to get the fraction of the pixel
  30. // covered.
  31. alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));
  32. }
  33. @if (edgeType == GrClipEdgeType::kInverseFillBW || edgeType == GrClipEdgeType::kInverseFillAA) {
  34. alpha = 1.0 - alpha;
  35. }
  36. sk_OutColor = sk_InColor * alpha;
  37. }
  38. @setData(pdman) {
  39. const SkRect& newRect = GrProcessorEdgeTypeIsAA(edgeType) ?
  40. rect.makeInset(.5f, .5f) : rect;
  41. if (newRect != prevRect) {
  42. pdman.set4f(rectUniform, newRect.fLeft, newRect.fTop, newRect.fRight, newRect.fBottom);
  43. prevRect = newRect;
  44. }
  45. }
  46. @test(d) {
  47. SkRect rect = SkRect::MakeLTRB(d->fRandom->nextSScalar1(),
  48. d->fRandom->nextSScalar1(),
  49. d->fRandom->nextSScalar1(),
  50. d->fRandom->nextSScalar1());
  51. std::unique_ptr<GrFragmentProcessor> fp;
  52. do {
  53. GrClipEdgeType edgeType = static_cast<GrClipEdgeType>(
  54. d->fRandom->nextULessThan(kGrClipEdgeTypeCnt));
  55. fp = GrAARectEffect::Make(edgeType, rect);
  56. } while (nullptr == fp);
  57. return fp;
  58. }