|
@@ -34,6 +34,7 @@ package com.jme3.system.awt;
|
|
import com.jme3.post.SceneProcessor;
|
|
import com.jme3.post.SceneProcessor;
|
|
import com.jme3.renderer.RenderManager;
|
|
import com.jme3.renderer.RenderManager;
|
|
import com.jme3.renderer.ViewPort;
|
|
import com.jme3.renderer.ViewPort;
|
|
|
|
+import com.jme3.renderer.opengl.GLRenderer;
|
|
import com.jme3.renderer.queue.RenderQueue;
|
|
import com.jme3.renderer.queue.RenderQueue;
|
|
import com.jme3.texture.FrameBuffer;
|
|
import com.jme3.texture.FrameBuffer;
|
|
import com.jme3.texture.Image.Format;
|
|
import com.jme3.texture.Image.Format;
|
|
@@ -47,20 +48,19 @@ import java.awt.image.AffineTransformOp;
|
|
import java.awt.image.BufferStrategy;
|
|
import java.awt.image.BufferStrategy;
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.ByteBuffer;
|
|
-import java.nio.IntBuffer;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
|
|
+import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
|
+import java.util.concurrent.Future;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
-public class AwtPanel extends Canvas implements SceneProcessor {
|
|
|
|
|
|
+public class AwtPanel extends Canvas implements JmePanel, SceneProcessor {
|
|
|
|
|
|
private boolean attachAsMain = false;
|
|
private boolean attachAsMain = false;
|
|
|
|
|
|
private BufferedImage img;
|
|
private BufferedImage img;
|
|
- private FrameBuffer fb;
|
|
|
|
- private boolean srgb = false;
|
|
|
|
- private ByteBuffer byteBuf;
|
|
|
|
- private IntBuffer intBuf;
|
|
|
|
|
|
+// private FrameBuffer fb;
|
|
private RenderManager rm;
|
|
private RenderManager rm;
|
|
private PaintMode paintMode;
|
|
private PaintMode paintMode;
|
|
private ArrayList<ViewPort> viewPorts = new ArrayList<ViewPort>();
|
|
private ArrayList<ViewPort> viewPorts = new ArrayList<ViewPort>();
|
|
@@ -75,35 +75,47 @@ public class AwtPanel extends Canvas implements SceneProcessor {
|
|
// Reshape vars
|
|
// Reshape vars
|
|
private int newWidth = 1;
|
|
private int newWidth = 1;
|
|
private int newHeight = 1;
|
|
private int newHeight = 1;
|
|
- private AtomicBoolean reshapeNeeded = new AtomicBoolean(false);
|
|
|
|
|
|
+ private AtomicBoolean reshapeNeeded = new AtomicBoolean(true);
|
|
private final Object lock = new Object();
|
|
private final Object lock = new Object();
|
|
|
|
|
|
- public AwtPanel(PaintMode paintMode){
|
|
|
|
- this(paintMode, false);
|
|
|
|
- }
|
|
|
|
|
|
+ // Buffer pool and pending buffers
|
|
|
|
+ private int NUM_FRAMES = 3;
|
|
|
|
+ private final ArrayBlockingQueue<Future<ByteBuffer>> pendingFrames = new ArrayBlockingQueue<Future<ByteBuffer>>(NUM_FRAMES);
|
|
|
|
+ private final ArrayBlockingQueue<ByteBuffer> bufferPool = new ArrayBlockingQueue<ByteBuffer>(NUM_FRAMES);
|
|
|
|
+ private final ArrayList<FrameBuffer> fbs = new ArrayList<FrameBuffer>(NUM_FRAMES);
|
|
|
|
+ private int frameIndex = 0;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private final ComponentAdapter resizeListener = new ComponentAdapter() {
|
|
|
|
+ @Override
|
|
|
|
+ public void componentResized(ComponentEvent e) {
|
|
|
|
+ onResize(e);
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
|
|
public AwtPanel(PaintMode paintMode, boolean srgb){
|
|
public AwtPanel(PaintMode paintMode, boolean srgb){
|
|
this.paintMode = paintMode;
|
|
this.paintMode = paintMode;
|
|
- this.srgb = srgb;
|
|
|
|
|
|
+
|
|
|
|
+ invalidatePendingFrames();
|
|
|
|
+
|
|
if (paintMode == PaintMode.Accelerated){
|
|
if (paintMode == PaintMode.Accelerated){
|
|
setIgnoreRepaint(true);
|
|
setIgnoreRepaint(true);
|
|
}
|
|
}
|
|
|
|
|
|
- addComponentListener(new ComponentAdapter(){
|
|
|
|
- @Override
|
|
|
|
- public void componentResized(ComponentEvent e) {
|
|
|
|
- synchronized (lock){
|
|
|
|
- int newWidth2 = Math.max(getWidth(), 1);
|
|
|
|
- int newHeight2 = Math.max(getHeight(), 1);
|
|
|
|
- if (newWidth != newWidth2 || newHeight != newHeight2){
|
|
|
|
- newWidth = newWidth2;
|
|
|
|
- newHeight = newHeight2;
|
|
|
|
- reshapeNeeded.set(true);
|
|
|
|
- System.out.println("EDT: componentResized " + newWidth + ", " + newHeight);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ addComponentListener(resizeListener);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void onResize(ComponentEvent e) {
|
|
|
|
+ synchronized (lock) {
|
|
|
|
+ int newWidth2 = Math.max(getWidth(), 1);
|
|
|
|
+ int newHeight2 = Math.max(getHeight(), 1);
|
|
|
|
+ if (newWidth != newWidth2 || newHeight != newHeight2) {
|
|
|
|
+ newWidth = newWidth2;
|
|
|
|
+ newHeight = newHeight2;
|
|
|
|
+ reshapeNeeded.set(true);
|
|
|
|
+ System.out.println("EDT: componentResized " + newWidth + ", " + newHeight);
|
|
}
|
|
}
|
|
- });
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -128,13 +140,13 @@ public class AwtPanel extends Canvas implements SceneProcessor {
|
|
super.removeNotify();
|
|
super.removeNotify();
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public void paint(Graphics g){
|
|
|
|
- Graphics2D g2d = (Graphics2D) g;
|
|
|
|
- synchronized (lock){
|
|
|
|
- g2d.drawImage(img, transformOp, 0, 0);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+// @Override
|
|
|
|
+// public void paint(Graphics g){
|
|
|
|
+// Graphics2D g2d = (Graphics2D) g;
|
|
|
|
+// synchronized (lock){
|
|
|
|
+// g2d.drawImage(img, transformOp, 0, 0);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
|
|
public boolean checkVisibilityState(){
|
|
public boolean checkVisibilityState(){
|
|
if (!hasNativePeer.get()){
|
|
if (!hasNativePeer.get()){
|
|
@@ -157,24 +169,85 @@ public class AwtPanel extends Canvas implements SceneProcessor {
|
|
return currentShowing;
|
|
return currentShowing;
|
|
}
|
|
}
|
|
|
|
|
|
- public void repaintInThread(){
|
|
|
|
- // Convert screenshot.
|
|
|
|
- byteBuf.clear();
|
|
|
|
- rm.getRenderer().readFrameBuffer(fb, byteBuf);
|
|
|
|
|
|
+// public void repaintInThread(){
|
|
|
|
+// // Convert screenshot.
|
|
|
|
+// byteBuf.clear();
|
|
|
|
+// rm.getRenderer().readFrameBuffer(fb, byteBuf);
|
|
|
|
+//
|
|
|
|
+// synchronized (lock){
|
|
|
|
+// // All operations on img must be synchronized
|
|
|
|
+// // as it is accessed from EDT.
|
|
|
|
+// Screenshots.convertScreenShot2(intBuf, img);
|
|
|
|
+// repaint();
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ public ByteBuffer acquireNextFrame() {
|
|
|
|
+ if (pendingFrames.isEmpty()) {
|
|
|
|
+ System.out.println("!!! No pending frames, returning null.");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
|
|
- synchronized (lock){
|
|
|
|
- // All operations on img must be synchronized
|
|
|
|
- // as it is accessed from EDT.
|
|
|
|
- Screenshots.convertScreenShot2(intBuf, img);
|
|
|
|
- repaint();
|
|
|
|
|
|
+ try {
|
|
|
|
+ ByteBuffer nextFrame = null;
|
|
|
|
+
|
|
|
|
+// while (!pendingFrames.isEmpty() && pendingFrames.peek().isDone()) {
|
|
|
|
+// nextFrame = pendingFrames.take().get();
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// if (nextFrame != null) {
|
|
|
|
+// return nextFrame;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// if (pendingFrames.remainingCapacity() == 0) {
|
|
|
|
+ // Force it to finish ..
|
|
|
|
+ return pendingFrames.take().get();
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ // Some frames are pending, none are finished though.
|
|
|
|
+// return null;
|
|
|
|
+ } catch (InterruptedException ex) {
|
|
|
|
+ throw new RuntimeException(ex);
|
|
|
|
+ } catch (ExecutionException ex) {
|
|
|
|
+ throw new RuntimeException(ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public void drawFrameInThread(){
|
|
|
|
- // Convert screenshot.
|
|
|
|
- byteBuf.clear();
|
|
|
|
- rm.getRenderer().readFrameBuffer(fb, byteBuf);
|
|
|
|
- Screenshots.convertScreenShot2(intBuf, img);
|
|
|
|
|
|
+ public void readNextFrame() {
|
|
|
|
+ if (bufferPool.isEmpty()) {
|
|
|
|
+ System.out.println("??? Too many pending frames!");
|
|
|
|
+ return; // need to draw more frames ..
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ int size = fbs.get(frameIndex).getWidth() * fbs.get(frameIndex).getHeight() * 4;
|
|
|
|
+ ByteBuffer byteBuf = bufferPool.take();
|
|
|
|
+ byteBuf = BufferUtils.ensureLargeEnough(byteBuf, size);
|
|
|
|
+ byteBuf.clear();
|
|
|
|
+
|
|
|
|
+ GLRenderer renderer = (GLRenderer) rm.getRenderer();
|
|
|
|
+ Future<ByteBuffer> future = renderer.readFrameBufferLater(fbs.get(frameIndex), byteBuf);
|
|
|
|
+ if (!pendingFrames.offer(future)) {
|
|
|
|
+ throw new AssertionError();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ frameIndex ++;
|
|
|
|
+ if (frameIndex >= NUM_FRAMES) {
|
|
|
|
+ frameIndex = 0;
|
|
|
|
+ }
|
|
|
|
+ } catch (InterruptedException ex) {
|
|
|
|
+ throw new RuntimeException(ex);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void drawFrameInThread(ByteBuffer byteBuf){
|
|
|
|
+ // Convert the frame into the image so it can be rendered.
|
|
|
|
+ Screenshots.convertScreenShot2(byteBuf.asIntBuffer(), img);
|
|
|
|
+
|
|
|
|
+ // return the frame back to its rightful owner.
|
|
|
|
+ if (!bufferPool.offer(byteBuf)) {
|
|
|
|
+ throw new AssertionError();
|
|
|
|
+ }
|
|
|
|
|
|
synchronized (lock){
|
|
synchronized (lock){
|
|
// All operations on strategy should be synchronized (?)
|
|
// All operations on strategy should be synchronized (?)
|
|
@@ -235,44 +308,65 @@ public class AwtPanel extends Canvas implements SceneProcessor {
|
|
if (this.rm == null){
|
|
if (this.rm == null){
|
|
// First time called in OGL thread
|
|
// First time called in OGL thread
|
|
this.rm = rm;
|
|
this.rm = rm;
|
|
- reshapeInThread(1, 1);
|
|
|
|
|
|
+// reshapeInThread(1, 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void updateAccelerated() {
|
|
|
|
+ readNextFrame();
|
|
|
|
+ ByteBuffer byteBuf = acquireNextFrame();
|
|
|
|
+ if (byteBuf != null) {
|
|
|
|
+ drawFrameInThread(byteBuf);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void invalidatePendingFrames() {
|
|
|
|
+ // NOTE: all pending read requests are invalid!
|
|
|
|
+ for (Future<ByteBuffer> pendingRequest : pendingFrames) {
|
|
|
|
+ pendingRequest.cancel(true);
|
|
|
|
+ }
|
|
|
|
+ pendingFrames.clear();
|
|
|
|
+ bufferPool.clear();
|
|
|
|
+
|
|
|
|
+ // Populate buffer pool.
|
|
|
|
+ int cap = bufferPool.remainingCapacity();
|
|
|
|
+ for (int i = 0; i < cap; i++) {
|
|
|
|
+ bufferPool.add(BufferUtils.createByteBuffer(1 * 1 * 4));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private void reshapeInThread(int width, int height) {
|
|
private void reshapeInThread(int width, int height) {
|
|
- byteBuf = BufferUtils.ensureLargeEnough(byteBuf, width * height * 4);
|
|
|
|
- intBuf = byteBuf.asIntBuffer();
|
|
|
|
-
|
|
|
|
- if (fb != null) {
|
|
|
|
|
|
+ invalidatePendingFrames();
|
|
|
|
+
|
|
|
|
+ for (FrameBuffer fb : fbs) {
|
|
fb.dispose();
|
|
fb.dispose();
|
|
- fb = null;
|
|
|
|
}
|
|
}
|
|
|
|
+ fbs.clear();
|
|
|
|
|
|
- fb = new FrameBuffer(width, height, 1);
|
|
|
|
- fb.setDepthBuffer(Format.Depth);
|
|
|
|
- fb.setColorBuffer(Format.RGB8);
|
|
|
|
- fb.setSrgb(srgb);
|
|
|
|
-
|
|
|
|
- if (attachAsMain){
|
|
|
|
- rm.getRenderer().setMainFrameBufferOverride(fb);
|
|
|
|
|
|
+ for (int i = 0; i < NUM_FRAMES; i++) {
|
|
|
|
+ FrameBuffer fb = new FrameBuffer(width, height, 1);
|
|
|
|
+ fb.setDepthBuffer(Format.Depth);
|
|
|
|
+ fb.setColorBuffer(Format.RGBA8);
|
|
|
|
+ fbs.add(fb);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+// if (attachAsMain){
|
|
|
|
+// rm.getRenderer().setMainFrameBufferOverride(fb);
|
|
|
|
+// }
|
|
|
|
+
|
|
synchronized (lock){
|
|
synchronized (lock){
|
|
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
|
|
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
|
|
}
|
|
}
|
|
|
|
|
|
-// synchronized (lock){
|
|
|
|
-// img = (BufferedImage) getGraphicsConfiguration().createCompatibleImage(width, height);
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
|
|
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
|
|
tx.translate(0, -img.getHeight());
|
|
tx.translate(0, -img.getHeight());
|
|
transformOp = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
|
|
transformOp = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
|
|
|
|
|
|
for (ViewPort vp : viewPorts){
|
|
for (ViewPort vp : viewPorts){
|
|
- if (!attachAsMain){
|
|
|
|
- vp.setOutputFrameBuffer(fb);
|
|
|
|
- }
|
|
|
|
|
|
+// if (!attachAsMain){
|
|
|
|
+// vp.setOutputFrameBuffer(fb);
|
|
|
|
+// }
|
|
vp.getCamera().resize(width, height, true);
|
|
vp.getCamera().resize(width, height, true);
|
|
|
|
|
|
// NOTE: Hack alert. This is done ONLY for custom framebuffers.
|
|
// NOTE: Hack alert. This is done ONLY for custom framebuffers.
|
|
@@ -283,8 +377,9 @@ public class AwtPanel extends Canvas implements SceneProcessor {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
public boolean isInitialized() {
|
|
public boolean isInitialized() {
|
|
- return fb != null;
|
|
|
|
|
|
+ return rm != null;
|
|
}
|
|
}
|
|
|
|
|
|
public void preFrame(float tpf) {
|
|
public void preFrame(float tpf) {
|
|
@@ -298,8 +393,21 @@ public class AwtPanel extends Canvas implements SceneProcessor {
|
|
// For "PaintMode.OnDemand" only.
|
|
// For "PaintMode.OnDemand" only.
|
|
repaintRequest.set(true);
|
|
repaintRequest.set(true);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Component getComponent() {
|
|
|
|
+ return this;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onFrameBegin() {
|
|
|
|
+ if (attachAsMain && rm != null){
|
|
|
|
+ rm.getRenderer().setMainFrameBufferOverride(fbs.get(frameIndex));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- void onFrameEnd() {
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public void onFrameEnd() {
|
|
if (reshapeNeeded.getAndSet(false)) {
|
|
if (reshapeNeeded.getAndSet(false)) {
|
|
reshapeInThread(newWidth, newHeight);
|
|
reshapeInThread(newWidth, newHeight);
|
|
} else {
|
|
} else {
|
|
@@ -309,26 +417,24 @@ public class AwtPanel extends Canvas implements SceneProcessor {
|
|
|
|
|
|
switch (paintMode) {
|
|
switch (paintMode) {
|
|
case Accelerated:
|
|
case Accelerated:
|
|
- drawFrameInThread();
|
|
|
|
- break;
|
|
|
|
- case Repaint:
|
|
|
|
- repaintInThread();
|
|
|
|
- break;
|
|
|
|
- case OnRequest:
|
|
|
|
- if (repaintRequest.getAndSet(false)) {
|
|
|
|
- repaintInThread();
|
|
|
|
- }
|
|
|
|
|
|
+ updateAccelerated();
|
|
break;
|
|
break;
|
|
|
|
+// case Repaint:
|
|
|
|
+// repaintInThread();
|
|
|
|
+// break;
|
|
|
|
+// case OnRequest:
|
|
|
|
+// if (repaintRequest.getAndSet(false)) {
|
|
|
|
+// repaintInThread();
|
|
|
|
+// }
|
|
|
|
+// break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void postFrame(FrameBuffer out) {
|
|
public void postFrame(FrameBuffer out) {
|
|
- if (!attachAsMain && out != fb){
|
|
|
|
- throw new IllegalStateException("Why did you change the output framebuffer?");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // onFrameEnd();
|
|
|
|
|
|
+// if (!attachAsMain && out != fb){
|
|
|
|
+// throw new IllegalStateException("Why did you change the output framebuffer?");
|
|
|
|
+// }
|
|
}
|
|
}
|
|
|
|
|
|
public void reshape(ViewPort vp, int w, int h) {
|
|
public void reshape(ViewPort vp, int w, int h) {
|