summaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/mossbar/concepts/ShmBufferPool.java
blob: f7cd6367bed4045e2d7f9ce8c032c387e55ca264 (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
package moe.nea.mossbar.concepts;

import manifold.ext.props.rt.api.get;
import org.freedesktop.wayland.client.WlBufferEvents;
import org.freedesktop.wayland.client.WlBufferProxy;
import org.freedesktop.wayland.client.WlShmPoolEvents;
import org.freedesktop.wayland.shared.WlShmFormat;
import org.freedesktop.wayland.util.ShmPool;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayDeque;
import java.util.Queue;

public class ShmBufferPool extends Scope {
    private boolean destroyed = false;
    private Queue<Buffer> ready = new ArrayDeque<>();

    void enqueue(Buffer buffer) {
        requireNotDestroyed();
        ready.add(buffer);
    }

    public class Buffer extends Scope implements WlBufferEvents, WlShmPoolEvents {
        private final WlBufferProxy bufferProxy;
        private final ShmPool shmPool;
        @get
        final int width;
        @get
        final int height;
        @get
        final int byteWidth;
        @get
        final @NonNull WlShmFormat format;

        Buffer(Display display, int width, int height, int byteWidth, WlShmFormat format) throws IOException {
            this.width = width;
            this.height = height;
            this.byteWidth = byteWidth;
            this.format = format;
            int bufferSize = width * height * byteWidth;
            shmPool = new ShmPool(bufferSize).bindTo(this); // TODO: shouldnt this be a long upstream? maybe wayland is just 32 bit like that
            var poolProxy = display.shmProxy.createPool(this, shmPool.fd, bufferSize);
            bufferProxy = poolProxy.createBuffer(this, 0, width, height, width * byteWidth, format.value).bindTo(this);
            poolProxy.destroy();
        }

        public ByteBuffer getByteBuffer() {
            return shmPool.asByteBuffer();
        }

        @Override
        public void release(WlBufferProxy emitter) {
            enqueue(this);
        }

        public WlBufferProxy getProxy() {
            return bufferProxy;
        }
    }

    private void requireNotDestroyed() {
        if (destroyed)
            throw new IllegalStateException("pool destroyed");
    }

    public @Nullable Buffer poll() {
        requireNotDestroyed();
        return ready.poll();
    }

    @Override
    public void closeObject() {
        super.closeObject();
        destroyed = true;
    }

    public ShmBufferPool(Display display, int width, int height, int bufferCount, int byteWidth, WlShmFormat shmFormat) throws IOException {
        for (int i = 0; i < bufferCount; i++) {
            ready.add(new Buffer(display, width, height, byteWidth, shmFormat).bindTo(this));
        }
    }

    public static ShmBufferPool newARGBPool(Display display, int width, int height, int bufferCount) throws IOException {
        return new ShmBufferPool(display, width, height, bufferCount, 4, WlShmFormat.ARGB8888);
    }
}