aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/effects/GrMagnifierEffect.fp
blob: 53245bb6becb523000c321120544c2499ff6832f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

in uniform sampler2D src;
layout(ctype=SkIRect) in int4 bounds;
uniform float4 boundsUniform;
layout(ctype=SkRect) in float4 srcRect;
in uniform float xInvZoom;
in uniform float yInvZoom;
in uniform float xInvInset;
in uniform float yInvInset;

uniform half2 offset;

@coordTransform(src) {
    SkMatrix::I()
}

void main() {
    float2 coord = sk_TransformedCoords2D[0];
    float2 zoom_coord = offset + coord * half2(xInvZoom, yInvZoom);
    float2 delta = (coord - boundsUniform.xy) * boundsUniform.zw;
    delta = min(delta, half2(1.0, 1.0) - delta);
    delta *= half2(xInvInset, yInvInset);

    half weight = 0.0;
    if (delta.s < 2.0 && delta.t < 2.0) {
        delta = half2(2.0, 2.0) - delta;
        half dist = length(delta);
        dist = max(2.0 - dist, 0.0);
        weight = min(dist * dist, 1.0);
    } else {
        float2 delta_squared = delta * delta;
        weight = min(min(delta_squared.x, delta_squared.y), 1.0);
    }

    sk_OutColor = texture(src, mix(coord, zoom_coord, weight));
}

@setData(pdman) {
    SkScalar invW = 1.0f / src.width();
    SkScalar invH = 1.0f / src.height();

    {
        SkScalar y = srcRect.y() * invH;
        if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
            y = 1.0f - (srcRect.height() / bounds.height()) - y;
        }

        pdman.set2f(offset, srcRect.x() * invW, y);
    }

    {
        SkScalar y = bounds.y() * invH;
        if (srcProxy.origin() != kTopLeft_GrSurfaceOrigin) {
            y = 1.0f - bounds.height() * invH;
        }

        pdman.set4f(boundsUniform,
                    bounds.x() * invW,
                    y,
                    SkIntToScalar(src.width()) / bounds.width(),
                    SkIntToScalar(src.height()) / bounds.height());
    }
}

@test(d) {
    sk_sp<GrTextureProxy> proxy = d->textureProxy(0);
    const int kMaxWidth = 200;
    const int kMaxHeight = 200;
    const SkScalar kMaxInset = 20.0f;
    uint32_t width = d->fRandom->nextULessThan(kMaxWidth);
    uint32_t height = d->fRandom->nextULessThan(kMaxHeight);
    SkScalar inset = d->fRandom->nextRangeScalar(1.0f, kMaxInset);

    SkIRect bounds = SkIRect::MakeWH(SkIntToScalar(kMaxWidth), SkIntToScalar(kMaxHeight));
    SkRect srcRect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));

    auto effect = GrMagnifierEffect::Make(std::move(proxy),
                                          bounds,
                                          srcRect,
                                          srcRect.width() / bounds.width(),
                                          srcRect.height() / bounds.height(),
                                          bounds.width() / inset,
                                          bounds.height() / inset);
    SkASSERT(effect);
    return effect;
}