diff options
author | Erik De Rijcke <Erik.De.Rijcke@prodatamobility.com> | 2015-03-16 17:10:54 +0100 |
---|---|---|
committer | Erik De Rijcke <Erik.De.Rijcke@prodatamobility.com> | 2015-03-16 17:10:54 +0100 |
commit | 5660ca52403c66084db4bead135b38b0d3b31726 (patch) | |
tree | 1d6dcae35e99bb580979934bb35e865436eb8f26 | |
parent | 87c19d97fcc613ba0f002eaca40fc86dc3b862e4 (diff) |
provide better buffer management
-rw-r--r-- | examples/src/main/java/examples/Buffer.java | 41 | ||||
-rw-r--r-- | examples/src/main/java/examples/BufferPool.java | 25 | ||||
-rw-r--r-- | examples/src/main/java/examples/BufferPoolFactory.java | 47 |
3 files changed, 113 insertions, 0 deletions
diff --git a/examples/src/main/java/examples/Buffer.java b/examples/src/main/java/examples/Buffer.java new file mode 100644 index 0000000..7480589 --- /dev/null +++ b/examples/src/main/java/examples/Buffer.java @@ -0,0 +1,41 @@ +package examples; + +import org.freedesktop.wayland.client.WlBufferEvents; +import org.freedesktop.wayland.client.WlBufferProxy; +import org.freedesktop.wayland.client.WlShmPoolProxy; + +import java.nio.ByteBuffer; + + +public class Buffer implements WlBufferEvents { + + private final WlShmPoolProxy wlShmPoolProxy; + private final ByteBuffer byteBuffer; + private final int width; + private final int height; + + public Buffer(final WlShmPoolProxy wlShmPoolProxy, final ByteBuffer byteBuffer, final int width, final int height) { + this.wlShmPoolProxy = wlShmPoolProxy; + this.byteBuffer = byteBuffer; + this.width = width; + this.height = height; + } + + @Override + public void release(final WlBufferProxy emitter) { + final BufferPool bufferPool = (BufferPool) this.wlShmPoolProxy.getImplementation(); + bufferPool.queueBuffer(emitter); + } + + public ByteBuffer getByteBuffer() { + return byteBuffer; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } +} diff --git a/examples/src/main/java/examples/BufferPool.java b/examples/src/main/java/examples/BufferPool.java new file mode 100644 index 0000000..9d9f523 --- /dev/null +++ b/examples/src/main/java/examples/BufferPool.java @@ -0,0 +1,25 @@ +package examples; + + +import org.freedesktop.wayland.client.WlBufferProxy; +import org.freedesktop.wayland.client.WlShmPoolEvents; + +import java.util.LinkedList; + +public class BufferPool implements WlShmPoolEvents{ + + private LinkedList<WlBufferProxy> bufferQueue = new LinkedList<WlBufferProxy>(); + + + public BufferPool() { + + } + + public void queueBuffer(WlBufferProxy buffer){ + this.bufferQueue.add(buffer); + } + + public WlBufferProxy popBuffer(){ + return bufferQueue.pop(); + } +} diff --git a/examples/src/main/java/examples/BufferPoolFactory.java b/examples/src/main/java/examples/BufferPoolFactory.java new file mode 100644 index 0000000..cd2e199 --- /dev/null +++ b/examples/src/main/java/examples/BufferPoolFactory.java @@ -0,0 +1,47 @@ +package examples; + +import org.freedesktop.wayland.client.WlBufferProxy; +import org.freedesktop.wayland.client.WlShmPoolProxy; +import org.freedesktop.wayland.shared.WlShmFormat; + +import java.io.IOException; +import java.nio.ByteBuffer; + +public class BufferPoolFactory { + + private final Display display; + + public BufferPoolFactory(Display display) { + this.display = display; + } + + public WlShmPoolProxy create(int width, int height, int size) throws IOException { + final int bufferSize = width * height * 4; + final ShmPool shmPool = new ShmPool(bufferSize *size); + final BufferPool bufferPool = new BufferPool(); + final WlShmPoolProxy + wlShmPoolProxy = + this.display.getShmProxy() + .createPool(bufferPool, + shmPool.getFileDescriptor(), + bufferSize * size); + for(int i = 0; i < size; i++){ + final int offset = i * bufferSize; + final ByteBuffer poolByteBuffer = shmPool.asByteBuffer(); + poolByteBuffer.position(offset); + final ByteBuffer byteBuffer = poolByteBuffer.slice(); + + final WlBufferProxy buffer = wlShmPoolProxy.createBuffer(new Buffer(wlShmPoolProxy, + byteBuffer, + width, + height), + offset, + width, + height, + width * 4, + WlShmFormat.XRGB8888.getValue()); + bufferPool.queueBuffer(buffer); + } + return wlShmPoolProxy; + } +} |