aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/invalid/lena/scrcpy/TouchGeometry.java
blob: 5ee80d4fe954310452efe37a30f3c89dc84a1c4d (plain) (blame)
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
package invalid.lena.scrcpy;

// Publishes a target size and its matching on-screen viewport as one
// immutable value. A target resize invalidates the old viewport until the UI
// lays out the new generation.
final class TouchGeometry {

    static final class Snapshot {
        final int targetW, targetH;
        final int x, y, w, h;

        Snapshot(int targetW, int targetH, int x, int y, int w, int h) {
            this.targetW = targetW;
            this.targetH = targetH;
            this.x = x;
            this.y = y;
            this.w = w;
            this.h = h;
        }
    }

    private volatile Snapshot snapshot;
    private long version;
    private int targetW;
    private int targetH;

    synchronized void setTargetSize(long nextVersion, int w, int h) {
        if (nextVersion <= version) throw new IllegalArgumentException("stale target version");
        if (w <= 0 || h <= 0) throw new IllegalArgumentException("invalid target size");
        version = nextVersion;
        targetW = w;
        targetH = h;
        snapshot = null;
    }

    synchronized void setViewport(long expectedVersion, int x, int y, int w, int h) {
        if (expectedVersion != version) return;
        snapshot = version > 0 && targetW > 0 && targetH > 0 && w > 0 && h > 0
                ? new Snapshot(targetW, targetH, x, y, w, h)
                : null;
    }

    Snapshot snapshot() {
        return snapshot;
    }
}