summaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/mossbar/concepts/ShmBufferPool.java
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-07-02 23:36:30 +0200
committerLinnea Gräf <nea@nea.moe>2025-07-02 23:36:30 +0200
commit1eae252f41eab7612864fb479263dc93310d8930 (patch)
treeb10172d42668cb8b5199565c0e2eee4ac3e9c5c3 /src/main/java/moe/nea/mossbar/concepts/ShmBufferPool.java
downloadmossbar-1eae252f41eab7612864fb479263dc93310d8930.tar.gz
mossbar-1eae252f41eab7612864fb479263dc93310d8930.tar.bz2
mossbar-1eae252f41eab7612864fb479263dc93310d8930.zip
init
Diffstat (limited to 'src/main/java/moe/nea/mossbar/concepts/ShmBufferPool.java')
-rw-r--r--src/main/java/moe/nea/mossbar/concepts/ShmBufferPool.java89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/mossbar/concepts/ShmBufferPool.java b/src/main/java/moe/nea/mossbar/concepts/ShmBufferPool.java
new file mode 100644
index 0000000..f7cd636
--- /dev/null
+++ b/src/main/java/moe/nea/mossbar/concepts/ShmBufferPool.java
@@ -0,0 +1,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);
+ }
+}