From 7c23d482b7b59eebbd479ba691740154b64eda48 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Sun, 6 Apr 2014 10:07:54 -0400 Subject: [PATCH] some cleaner logic to allow for #2424, which should also work when video buffers start accumulating in the cache on slower machines. --- core/src/processing/opengl/Texture.java | 24 ++++++++++--------- .../video/src/processing/video/Capture.java | 10 ++++---- .../video/src/processing/video/Movie.java | 8 +++---- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/core/src/processing/opengl/Texture.java b/core/src/processing/opengl/Texture.java index dc90ab6e2..f74da6a6e 100644 --- a/core/src/processing/opengl/Texture.java +++ b/core/src/processing/opengl/Texture.java @@ -882,17 +882,15 @@ public class Texture implements PConstants { } public void getBufferPixels(int[] pixels) { - boolean addToUsed = false; + // We get the buffer either from the used buffers or the cache, giving + // priority to the used buffers. Why? Because the used buffer was already + // transferred to the texture, so the pixels should be in sync with the + // texture. BufferData data = null; if (usedBuffers != null && 0 < usedBuffers.size()) { - // the last used buffer is the one currently stored in the opengl - // texture data = usedBuffers.getLast(); } else if (bufferCache != null && 0 < bufferCache.size()) { - // The first buffer in the cache will be uploaded to the opengl texture - // the next time it is rendered - data = bufferCache.remove(0); - addToUsed = true; + data = bufferCache.getLast(); } if (data != null) { if ((data.w != width) || (data.h != height)) { @@ -903,10 +901,14 @@ public class Texture implements PConstants { data.rgbBuf.get(pixels); convertToARGB(pixels); - if (addToUsed) { - if (usedBuffers == null) { - usedBuffers = new LinkedList(); - } + // In order to avoid a cached buffer to overwrite the texture when the + // renderer draws the texture, and hence put the pixels put of sync, we + // simply empty the cache. + if (usedBuffers == null) { + usedBuffers = new LinkedList(); + } + while (0 < bufferCache.size()) { + data = bufferCache.remove(0); usedBuffers.add(data); } } diff --git a/java/libraries/video/src/processing/video/Capture.java b/java/libraries/video/src/processing/video/Capture.java index f5bced6ed..8f233b9c5 100644 --- a/java/libraries/video/src/processing/video/Capture.java +++ b/java/libraries/video/src/processing/video/Capture.java @@ -439,17 +439,15 @@ public class Capture extends PImage implements PConstants { Video.convertToARGB(pixels, width, height); } else if (sinkGetMethod != null) { try { + // sinkGetMethod will copy the latest buffer to the pixels array, + // and the pixels will be copied to the texture when the OpenGL + // renderer needs to draw it. sinkGetMethod.invoke(bufferSink, new Object[] { pixels }); } catch (Exception e) { e.printStackTrace(); } } - - // super.loadPixels() sets loaded to true, but in the useBufferSink mode, - // the contents of the pixels array is overwritten by the buffers coming - // from gstreamer, so we don't want PGraphicsOpenGL replacing the OpenGL - // texture with the pixels. - setLoaded(false); + outdatedPixels = false; } } diff --git a/java/libraries/video/src/processing/video/Movie.java b/java/libraries/video/src/processing/video/Movie.java index 2deb70738..9707d3c61 100644 --- a/java/libraries/video/src/processing/video/Movie.java +++ b/java/libraries/video/src/processing/video/Movie.java @@ -563,17 +563,15 @@ public class Movie extends PImage implements PConstants { Video.convertToARGB(pixels, width, height); } else if (sinkGetMethod != null) { try { + // sinkGetMethod will copy the latest buffer to the pixels array, + // and the pixels will be copied to the texture when the OpenGL + // renderer needs to draw it. sinkGetMethod.invoke(bufferSink, new Object[] { pixels }); } catch (Exception e) { e.printStackTrace(); } } - // super.loadPixels() sets loaded to true, but in the useBufferSink mode, - // the contents of the pixels array is overwritten by the buffers coming - // from gstreamer, so we don't want PGraphicsOpenGL replacing the OpenGL - // texture with the pixels. - setLoaded(false); outdatedPixels = false; } }