diff options
author | Erik De Rijcke <Erik.De.Rijcke@prodatamobility.com> | 2015-03-20 11:15:11 +0100 |
---|---|---|
committer | Erik De Rijcke <Erik.De.Rijcke@prodatamobility.com> | 2015-03-20 11:15:11 +0100 |
commit | a16213fc725e729fc40769dc77232a06c0eff55f (patch) | |
tree | 3f1f9b257ec5b28d2186a6416053d97ce53318db | |
parent | 52f6ff65da2b2cdc76d52e95206368946ad518c3 (diff) |
close fd when buffer is destroyed
-rw-r--r-- | examples/src/main/java/examples/Buffer.java | 17 | ||||
-rw-r--r-- | examples/src/main/java/examples/BufferPoolFactory.java | 3 | ||||
-rw-r--r-- | examples/src/main/java/examples/ShmPool.java | 44 | ||||
-rw-r--r-- | examples/src/main/java/examples/SimpleShm.java | 7 | ||||
-rw-r--r-- | examples/src/main/java/examples/Window.java | 8 |
5 files changed, 37 insertions, 42 deletions
diff --git a/examples/src/main/java/examples/Buffer.java b/examples/src/main/java/examples/Buffer.java index f50b749..7328a68 100644 --- a/examples/src/main/java/examples/Buffer.java +++ b/examples/src/main/java/examples/Buffer.java @@ -3,19 +3,23 @@ package examples; import org.freedesktop.wayland.client.WlBufferEvents; import org.freedesktop.wayland.client.WlBufferProxy; +import java.io.IOException; import java.nio.ByteBuffer; public class Buffer implements WlBufferEvents { private final BufferPool bufferPool; - private final ByteBuffer byteBuffer; + private final ShmPool shmPool; private final int width; private final int height; - public Buffer(final BufferPool bufferPool, final ByteBuffer byteBuffer, final int width, final int height) { + public Buffer(final BufferPool bufferPool, + final ShmPool shmPool, + final int width, + final int height) { this.bufferPool = bufferPool; - this.byteBuffer = byteBuffer; + this.shmPool = shmPool; this.width = width; this.height = height; } @@ -24,13 +28,18 @@ public class Buffer implements WlBufferEvents { public void release(final WlBufferProxy emitter) { if(bufferPool.isDestroyed()){ emitter.destroy(); + try { + shmPool.close(); + } catch (IOException e) { + e.printStackTrace(); + } }else { bufferPool.queueBuffer(emitter); } } public ByteBuffer getByteBuffer() { - return byteBuffer; + return shmPool.asByteBuffer(); } public int getWidth() { diff --git a/examples/src/main/java/examples/BufferPoolFactory.java b/examples/src/main/java/examples/BufferPoolFactory.java index 0b00775..ed06691 100644 --- a/examples/src/main/java/examples/BufferPoolFactory.java +++ b/examples/src/main/java/examples/BufferPoolFactory.java @@ -28,9 +28,8 @@ public class BufferPoolFactory { .createPool(bufferPool, shmPool.getFileDescriptor(), bufferSize); - final ByteBuffer byteBuffer = shmPool.asByteBuffer(); final WlBufferProxy buffer = wlShmPoolProxy.createBuffer(new Buffer(bufferPool, - byteBuffer, + shmPool, width, height), 0, diff --git a/examples/src/main/java/examples/ShmPool.java b/examples/src/main/java/examples/ShmPool.java index 068cfdd..aafcb9f 100644 --- a/examples/src/main/java/examples/ShmPool.java +++ b/examples/src/main/java/examples/ShmPool.java @@ -38,13 +38,13 @@ public final class ShmPool implements Closeable { this.fd = createTmpFileNative(); this.size = size; try { - truncateNative(this.fd, - this.size); - this.buffer = map(this.fd, - this.size); + truncateNative(getFd(), + getSize()); + this.buffer = map(getFd(), + getSize()); } catch (final IOException e) { - closeNative(this.fd); + closeNative(getFd()); throw e; } } @@ -65,39 +65,25 @@ public final class ShmPool implements Closeable { return this.size; } - public void resize(final int size, - final boolean truncate) throws IOException { - if (this.buffer == null) { - throw new IllegalStateException("ShmPool is closed"); - } - - unmapNative(this.buffer); - - this.size = size; - if (truncate) { - truncateNative(this.fd, - size); - } - - this.buffer = map(this.fd, - size); - } - - public void resize(final int size) throws IOException { - resize(size, - true); - } - @Override public void close() throws IOException { if (this.buffer != null) { - unmapNative(this.buffer); + unmapNative(asByteBuffer()); + closeNative(getFd()); this.fd = -1; this.size = 0; this.buffer = null; } } + public int getSize() { + return size; + } + + public int getFd() { + return fd; + } + @Override public void finalize() throws Throwable { close(); diff --git a/examples/src/main/java/examples/SimpleShm.java b/examples/src/main/java/examples/SimpleShm.java index ccb5089..17d65e3 100644 --- a/examples/src/main/java/examples/SimpleShm.java +++ b/examples/src/main/java/examples/SimpleShm.java @@ -19,6 +19,13 @@ public class SimpleShm { public static void main(final String[] args) throws IOException { + Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { + @Override + public void uncaughtException(final Thread thread, final Throwable throwable) { + System.err.println(throwable.getCause().getMessage()); + } + }); + final Display display = new Display(); final Window window = new Window(display, 250, diff --git a/examples/src/main/java/examples/Window.java b/examples/src/main/java/examples/Window.java index ec20fb8..47dd9e8 100644 --- a/examples/src/main/java/examples/Window.java +++ b/examples/src/main/java/examples/Window.java @@ -226,11 +226,6 @@ public class Window implements WlShellSurfaceEvents, int ir; int or; final IntBuffer image = buffer.getByteBuffer().asIntBuffer(); - image.clear(); - for (int i = 0; i < buffer.getWidth() * buffer.getHeight(); ++i) { - image.put(0xffffffff); - } - image.clear(); /* squared radii thresholds */ or = (halfw < halfh ? halfw : halfh) - 8; @@ -238,11 +233,11 @@ public class Window implements WlShellSurfaceEvents, or = or * or; ir = ir * ir; + image.clear(); image.position(0); for (int y = 0; y < buffer.getHeight(); y++) { final int y2 = (y - halfh) * (y - halfh); - image.position(image.position()); for (int x = 0; x < buffer.getWidth(); x++) { int v; @@ -263,7 +258,6 @@ public class Window implements WlShellSurfaceEvents, image.put(v); } - image.position(image.position()); } } |