GrCircleEffect.fp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2017 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. in float2 center;
  9. in float radius;
  10. float2 prevCenter;
  11. float prevRadius = -1;
  12. // The circle uniform is (center.x, center.y, radius + 0.5, 1 / (radius + 0.5)) for regular
  13. // fills and (..., radius - 0.5, 1 / (radius - 0.5)) for inverse fills.
  14. uniform float4 circle;
  15. @make {
  16. static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType edgeType, SkPoint center,
  17. float radius) {
  18. // A radius below half causes the implicit insetting done by this processor to become
  19. // inverted. We could handle this case by making the processor code more complicated.
  20. if (radius < .5f && GrProcessorEdgeTypeIsInverseFill(edgeType)) {
  21. return nullptr;
  22. }
  23. return std::unique_ptr<GrFragmentProcessor>(new GrCircleEffect(edgeType, center, radius));
  24. }
  25. }
  26. @optimizationFlags { kCompatibleWithCoverageAsAlpha_OptimizationFlag }
  27. @setData(pdman) {
  28. if (radius != prevRadius || center != prevCenter) {
  29. SkScalar effectiveRadius = radius;
  30. if (GrProcessorEdgeTypeIsInverseFill((GrClipEdgeType) edgeType)) {
  31. effectiveRadius -= 0.5f;
  32. // When the radius is 0.5 effectiveRadius is 0 which causes an inf * 0 in the shader.
  33. effectiveRadius = SkTMax(0.001f, effectiveRadius);
  34. } else {
  35. effectiveRadius += 0.5f;
  36. }
  37. pdman.set4f(circle, center.fX, center.fY, effectiveRadius,
  38. SkScalarInvert(effectiveRadius));
  39. prevCenter = center;
  40. prevRadius = radius;
  41. }
  42. }
  43. void main() {
  44. // TODO: Right now the distance to circle calculation is performed in a space normalized to the
  45. // radius and then denormalized. This is to mitigate overflow on devices that don't have full
  46. // float.
  47. half d;
  48. @if (edgeType == GrClipEdgeType::kInverseFillBW ||
  49. edgeType == GrClipEdgeType::kInverseFillAA) {
  50. d = half((length((circle.xy - sk_FragCoord.xy) * circle.w) - 1.0) * circle.z);
  51. } else {
  52. d = half((1.0 - length((circle.xy - sk_FragCoord.xy) * circle.w)) * circle.z);
  53. }
  54. @if (edgeType == GrClipEdgeType::kFillAA ||
  55. edgeType == GrClipEdgeType::kInverseFillAA ||
  56. edgeType == GrClipEdgeType::kHairlineAA) {
  57. sk_OutColor = sk_InColor * saturate(d);
  58. } else {
  59. sk_OutColor = d > 0.5 ? sk_InColor : half4(0);
  60. }
  61. }
  62. @test(testData) {
  63. SkPoint center;
  64. center.fX = testData->fRandom->nextRangeScalar(0.f, 1000.f);
  65. center.fY = testData->fRandom->nextRangeScalar(0.f, 1000.f);
  66. SkScalar radius = testData->fRandom->nextRangeF(1.f, 1000.f);
  67. GrClipEdgeType et;
  68. do {
  69. et = (GrClipEdgeType) testData->fRandom->nextULessThan(kGrClipEdgeTypeCnt);
  70. } while (GrClipEdgeType::kHairlineAA == et);
  71. return GrCircleEffect::Make(et, center, radius);
  72. }