GrMagnifierEffect.fp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. in uniform sampler2D src;
  8. layout(ctype=SkIRect) in int4 bounds;
  9. uniform float4 boundsUniform;
  10. layout(ctype=SkRect) in float4 srcRect;
  11. in uniform float xInvZoom;
  12. in uniform float yInvZoom;
  13. in uniform float xInvInset;
  14. in uniform float yInvInset;
  15. uniform half2 offset;
  16. @coordTransform(src) {
  17. SkMatrix::I()
  18. }
  19. void main() {
  20. float2 coord = sk_TransformedCoords2D[0];
  21. float2 zoom_coord = offset + coord * float2(xInvZoom, yInvZoom);
  22. float2 delta = (coord - boundsUniform.xy) * boundsUniform.zw;
  23. delta = min(delta, half2(1.0, 1.0) - delta);
  24. delta *= float2(xInvInset, yInvInset);
  25. float weight = 0.0;
  26. if (delta.s < 2.0 && delta.t < 2.0) {
  27. delta = half2(2.0, 2.0) - delta;
  28. float dist = length(delta);
  29. dist = max(2.0 - dist, 0.0);
  30. weight = min(dist * dist, 1.0);
  31. } else {
  32. float2 delta_squared = delta * delta;
  33. weight = min(min(delta_squared.x, delta_squared.y), 1.0);
  34. }
  35. sk_OutColor = texture(src, mix(coord, zoom_coord, weight));
  36. }
  37. @setData(pdman) {
  38. SkScalar invW = 1.0f / src.width();
  39. SkScalar invH = 1.0f / src.height();
  40. {
  41. SkScalar y = srcRect.y() * invH;
  42. if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
  43. y = 1.0f - (srcRect.height() / bounds.height()) - y;
  44. }
  45. pdman.set2f(offset, srcRect.x() * invW, y);
  46. }
  47. {
  48. SkScalar y = bounds.y() * invH;
  49. if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
  50. y = 1.0f - bounds.height() * invH;
  51. }
  52. pdman.set4f(boundsUniform,
  53. bounds.x() * invW,
  54. y,
  55. SkIntToScalar(src.width()) / bounds.width(),
  56. SkIntToScalar(src.height()) / bounds.height());
  57. }
  58. }
  59. @test(d) {
  60. sk_sp<GrTextureProxy> proxy = d->textureProxy(0);
  61. const int kMaxWidth = 200;
  62. const int kMaxHeight = 200;
  63. const SkScalar kMaxInset = 20.0f;
  64. uint32_t width = d->fRandom->nextULessThan(kMaxWidth);
  65. uint32_t height = d->fRandom->nextULessThan(kMaxHeight);
  66. SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset);
  67. SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight));
  68. SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
  69. auto effect = GrMagnifierEffect::Make(std::move(proxy),
  70. bounds,
  71. srcRect,
  72. srcRect.width() / bounds.width(),
  73. srcRect.height() / bounds.height(),
  74. bounds.width() / inset,
  75. bounds.height() / inset);
  76. SkASSERT(effect);
  77. return effect;
  78. }