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