Skip to content

Commit

Permalink
Bug 894 - GLJPanel: Expose 'initializeBackend(boolean offthread)' all…
Browse files Browse the repository at this point in the history
…owing user to trigger backend initialization eagerly and offthread (optional, !WINDOWS)

TestPerf001GLJPanelInit02AWT compares all variations: no-gl, glcanvas, gljpanel and gljpanel-initMT (offthread)
  • Loading branch information
sgothel committed Nov 6, 2013
1 parent de8a370 commit 10fee84
Show file tree
Hide file tree
Showing 7 changed files with 436 additions and 73 deletions.
1 change: 1 addition & 0 deletions make/scripts/tests-win.bat
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ REM scripts\java-win.bat com.jogamp.opengl.test.junit.jogl.tile.TestTiledPrintin
REM scripts\java-win.bat com.jogamp.opengl.test.junit.jogl.perf.TestPerf001RawInit00NEWT %*
REM scripts\java-win.bat com.jogamp.opengl.test.junit.jogl.perf.TestPerf001GLJPanelInit01AWT %*
scripts\java-win.bat com.jogamp.opengl.test.junit.jogl.perf.TestPerf001GLJPanelInit02AWT %*
REM scripts\java-win.bat com.jogamp.opengl.test.junit.jogl.perf.TestPerf001GLWindowInit03NEWT %*

REM scripts\java-win.bat com.jogamp.opengl.test.junit.jogl.demos.gl2.newt.TestGearsNewtAWTWrapper %*
REM scripts\java-win.bat com.jogamp.opengl.test.junit.jogl.demos.gl2.newt.TestGearsNEWT -time 30000
Expand Down
4 changes: 2 additions & 2 deletions make/scripts/tests-x64-dbg.bat
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ REM set D_ARGS="-Djogamp.debug=all"
REM set D_ARGS="-Djogl.debug.GLContext" "-Djogl.debug.FBObject"
REM set D_ARGS="-Djogl.debug.GLDrawable" "-Djogl.debug.EGLDrawableFactory.DontQuery"
REM set D_ARGS="-Djogl.debug.GLDrawable" "-Djogl.debug.EGLDrawableFactory.QueryNativeTK"
REM set D_ARGS="-Djogl.debug=all" "-Dnewt.debug=all" "-Dnativewindow.debug=all"
set D_ARGS="-Djogl.debug=all" "-Dnewt.debug=all" "-Dnativewindow.debug=all"
REM set D_ARGS="-Djogl.debug.GLDrawable" "-Djogl.debug.GLContext" "-Djogl.debug.GLCanvas"
REM set D_ARGS="-Djogl.debug=all" "-Dnewt.debug=all" "-Dnativewindow.debug=all" "-Djogamp.debug=all" "-Djogl.debug.EGLDrawableFactory.DontQuery"
REM set D_ARGS="-Dnativewindow.debug.GraphicsConfiguration -Djogl.debug.CapabilitiesChooser -Djogl.debug.GLProfile"
Expand Down Expand Up @@ -53,7 +53,7 @@ REM set D_ARGS="-Djogl.debug.GLCanvas" "-Djogl.debug.GLJPanel" "-Djogl.debug.Til
REM set D_ARGS="-Djogl.debug.GLCanvas" "-Djogl.debug.GLJPanel" "-Djogl.debug.TileRenderer"
REM set D_ARGS="-Dnewt.debug.Window"
REM set D_ARGS="-Dnewt.debug.Window.KeyEvent"
set D_ARGS="-Dnewt.debug.Window.MouseEvent"
REM set D_ARGS="-Dnewt.debug.Window.MouseEvent"
REM set D_ARGS="-Dnewt.debug.Window.MouseEvent" "-Dnewt.debug.Window.KeyEvent"
REM set D_ARGS="-Dnewt.debug.Window" "-Dnewt.debug.Display"
REM set D_ARGS="-Djogl.debug.GLDebugMessageHandler" "-Djogl.debug.DebugGL" "-Djogl.debug.TraceGL"
Expand Down
2 changes: 2 additions & 0 deletions make/scripts/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function jrun() {
#D_ARGS="-Djogamp.common.utils.locks.Lock.timeout=3000 -Djogamp.debug.Lock -Dnativewindow.debug.ToolkitLock.TraceLock"
#D_ARGS="-Djogamp.common.utils.locks.Lock.timeout=600000 -Djogamp.debug.Lock -Djogamp.debug.Lock.TraceLock -Dnativewindow.debug.ToolkitLock.TraceLock"
#D_ARGS="-Djogamp.common.utils.locks.Lock.timeout=600000 -Djogamp.debug.Lock -Dnativewindow.debug.X11Util"
#D_ARGS="-Djogamp.common.utils.locks.Lock.timeout=600000 -Dnativewindow.debug.X11Util"
#D_ARGS="-Dnewt.debug.EDT -Djogamp.common.utils.locks.Lock.timeout=600000 -Djogl.debug.Animator -Dnewt.debug.Display -Dnewt.debug.Screen"
#D_ARGS="-Dnewt.debug.Window -Djogamp.common.utils.locks.Lock.timeout=600000"
#D_ARGS="-Dnewt.debug=all -Djogamp.common.utils.locks.Lock.timeout=600000"
Expand Down Expand Up @@ -337,6 +338,7 @@ function testawtswt() {
#testnoawt com.jogamp.opengl.test.junit.jogl.perf.TestPerf001RawInit00NEWT $*
#testawt com.jogamp.opengl.test.junit.jogl.perf.TestPerf001GLJPanelInit01AWT $*
testawt com.jogamp.opengl.test.junit.jogl.perf.TestPerf001GLJPanelInit02AWT $*
#testnoawt com.jogamp.opengl.test.junit.jogl.perf.TestPerf001GLWindowInit03NEWT $*

#
# tile rendring / printing w/ & w/o AWT
Expand Down
118 changes: 88 additions & 30 deletions src/jogl/classes/javax/media/opengl/awt/GLJPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

import javax.media.nativewindow.AbstractGraphicsDevice;
import javax.media.nativewindow.NativeSurface;
import javax.media.nativewindow.NativeWindowFactory;
import javax.media.nativewindow.WindowClosingProtocol;
import javax.media.opengl.DefaultGLCapabilitiesChooser;
import javax.media.opengl.GL;
Expand Down Expand Up @@ -338,6 +339,51 @@ public GLJPanel(GLCapabilitiesImmutable userCapsRequest, GLCapabilitiesChooser c
this.setFocusable(true); // allow keyboard input!
}

/**
* Attempts to initialize the backend, if not initialized yet.
* <p>
* If backend is already initialized method returns <code>true</code>.
* </p>
* <p>
* If <code>offthread</code> is <code>true</code>, initialization will kicked off
* on a <i>short lived</i> arbitrary thread and method returns immediately.<br/>
* If platform supports such <i>arbitrary thread</i> initialization method returns
* <code>true</code>, otherwise <code>false</code>.
* </p>
* <p>
* Due to threading restrictions, <i>arbitrary thread</i> initialization is <i>not supported</i> on:
* <ul>
* <li>{@link NativeWindowFactory.TYPE_WINDOWS}</li>
* </ul>
* </p>
* <p>
* If <code>offthread</code> is <code>false</code>, initialization be performed
* on the current thread and method returns after initialization.<br/>
* Method returns <code>true</code> if initialization was successful, otherwise <code>false</code>.
* <p>
* @param offthread
*/
public final boolean initializeBackend(boolean offthread) {
if( offthread ) {
if( NativeWindowFactory.TYPE_WINDOWS == NativeWindowFactory.getNativeWindowType(true) ) {
return false;
}
new Thread(getThreadName()+"-GLJPanel_Init") {
public void run() {
if( !isInitialized ) {
initializeBackendImpl();
}
} }.start();
return true;
} else {
if( !isInitialized ) {
return initializeBackendImpl();
} else {
return true;
}
}
}

@Override
public final void setSharedContext(GLContext sharedContext) throws IllegalStateException {
helper.setSharedContext(this.getContext(), sharedContext);
Expand Down Expand Up @@ -457,8 +503,8 @@ protected void paintComponent(final Graphics g) {
return;
}

if (backend == null || !isInitialized) {
createAndInitializeBackend();
if( !isInitialized ) {
initializeBackendImpl();
}

if (!isInitialized || printActive) {
Expand Down Expand Up @@ -552,8 +598,8 @@ public void setupPrint(double scaleMatX, double scaleMatY, int numSamples, int t
private final Runnable setupPrintOnEDT = new Runnable() {
@Override
public void run() {
if (backend == null || !isInitialized) {
createAndInitializeBackend();
if( !isInitialized ) {
initializeBackendImpl();
}
if (!isInitialized) {
if(DEBUG) {
Expand Down Expand Up @@ -1020,35 +1066,47 @@ public final void setTextureUnit(int v) {
// Internals only below this point
//

private void createAndInitializeBackend() {
if ( 0 >= panelWidth || 0 >= panelHeight ) {
// See whether we have a non-zero size yet and can go ahead with
// initialization
if (0 >= reshapeWidth || 0 >= reshapeHeight ) {
return;
}
private final Object initSync = new Object();
private boolean initializeBackendImpl() {
if( !isInitialized ) {
synchronized(initSync) {
if( !isInitialized ) {
if ( 0 >= panelWidth || 0 >= panelHeight ) {
// See whether we have a non-zero size yet and can go ahead with
// initialization
if (0 >= reshapeWidth || 0 >= reshapeHeight ) {
return false;
}

if (DEBUG) {
System.err.println(getThreadName()+": GLJPanel.createAndInitializeBackend: " +panelWidth+"x"+panelHeight + " -> " + reshapeWidth+"x"+reshapeHeight);
}
// Pull down reshapeWidth and reshapeHeight into panelWidth and
// panelHeight eagerly in order to complete initialization, and
// force a reshape later
panelWidth = reshapeWidth;
panelHeight = reshapeHeight;
}
if (DEBUG) {
System.err.println(getThreadName()+": GLJPanel.createAndInitializeBackend: " +panelWidth+"x"+panelHeight + " -> " + reshapeWidth+"x"+reshapeHeight);
}
// Pull down reshapeWidth and reshapeHeight into panelWidth and
// panelHeight eagerly in order to complete initialization, and
// force a reshape later
panelWidth = reshapeWidth;
panelHeight = reshapeHeight;
}

if ( null == backend ) {
if ( oglPipelineUsable() ) {
backend = new J2DOGLBackend();
} else {
backend = new OffscreenBackend(glProfile, customPixelBufferProvider);
}
isInitialized = false;
}
if ( null == backend ) {
if ( oglPipelineUsable() ) {
backend = new J2DOGLBackend();
} else {
backend = new OffscreenBackend(glProfile, customPixelBufferProvider);
}
isInitialized = false;
}

if (!isInitialized) {
backend.initialize();
if (!isInitialized) {
backend.initialize();
}
return isInitialized;
} else {
return true;
}
}
} else {
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import javax.media.opengl.GLAnimatorControl;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLCapabilitiesImmutable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
Expand Down Expand Up @@ -65,8 +67,8 @@ public static void initClass() {
GLProfile.initSingleton();
}

public void test(final boolean useGears, final int width, final int height, final int rows, final int columns,
final boolean useGLJPanel, final boolean useAnim) {
public void test(final GLCapabilitiesImmutable caps, final boolean useGears, final int width, final int height, final int rows,
final int columns, final boolean useGLJPanel, final boolean useAnim) {
final GLAnimatorControl animator = useAnim ? new Animator() : null;

final JFrame frame;
Expand All @@ -90,7 +92,7 @@ public void test(final boolean useGears, final int width, final int height, fina
public void run() {
t[0] = Platform.currentTimeMillis();
for(int i=0; i<panelCount; i++) {
final GLAutoDrawable glad = useGLJPanel ? createGLJPanel(useGears, animator, eSize) : createGLCanvas(useGears, animator, eSize);
final GLAutoDrawable glad = useGLJPanel ? createGLJPanel(caps, useGears, animator, eSize) : createGLCanvas(caps, useGears, animator, eSize);
glad.addGLEventListener(new GLEventListener() {
@Override
public void init(GLAutoDrawable drawable) {
Expand Down Expand Up @@ -158,8 +160,8 @@ public void run() {
System.err.println("Total: "+(t[4]-t[0]));
}

private GLAutoDrawable createGLCanvas(boolean useGears, GLAnimatorControl anim, Dimension size) {
GLCanvas canvas = new GLCanvas();
private GLAutoDrawable createGLCanvas(GLCapabilitiesImmutable caps, boolean useGears, GLAnimatorControl anim, Dimension size) {
GLCanvas canvas = new GLCanvas(caps);
canvas.setSize(size);
canvas.setPreferredSize(size);
if( useGears ) {
Expand All @@ -170,8 +172,8 @@ private GLAutoDrawable createGLCanvas(boolean useGears, GLAnimatorControl anim,
}
return canvas;
}
private GLAutoDrawable createGLJPanel(boolean useGears, GLAnimatorControl anim, Dimension size) {
GLJPanel canvas = new GLJPanel();
private GLAutoDrawable createGLJPanel(GLCapabilitiesImmutable caps, boolean useGears, GLAnimatorControl anim, Dimension size) {
GLJPanel canvas = new GLJPanel(caps);
canvas.setSize(size);
canvas.setPreferredSize(size);
if( useGears ) {
Expand All @@ -184,13 +186,20 @@ private GLAutoDrawable createGLJPanel(boolean useGears, GLAnimatorControl anim,
}

@Test
public void test01NopGLJPanel() throws InterruptedException, InvocationTargetException {
test(false /*useGears*/, width, height, rows, cols, true /* useGLJPanel */, false /*useAnim*/);
public void test01NopGLJPanelDef() throws InterruptedException, InvocationTargetException {
test(new GLCapabilities(null), false /*useGears*/, width, height, rows, cols, true /* useGLJPanel */, false /*useAnim*/);
}

@Test
public void test02NopGLCanvas() throws InterruptedException, InvocationTargetException {
test(false /*useGears*/, width, height, rows, cols, false /* useGLJPanel */, false /*useAnim*/);
public void test02NopGLJPanelBitmap() throws InterruptedException, InvocationTargetException {
GLCapabilities caps = new GLCapabilities(null);
caps.setBitmap(true);
test(caps, false /*useGears*/, width, height, rows, cols, true /* useGLJPanel */, false /*useAnim*/);
}

@Test
public void test11NopGLCanvasDef() throws InterruptedException, InvocationTargetException {
test(new GLCapabilities(null), false /*useGears*/, width, height, rows, cols, false /* useGLJPanel */, false /*useAnim*/);
}

static long duration = 0; // ms
Expand Down Expand Up @@ -235,7 +244,7 @@ public static void main(String[] args) {
if( manual ) {
GLProfile.initSingleton();
TestPerf001GLJPanelInit01AWT demo = new TestPerf001GLJPanelInit01AWT();
demo.test(useGears, width, height, rows, cols, useGLJPanel, false /*useAnim*/);
demo.test(null, useGears, width, height, rows, cols, useGLJPanel, false /*useAnim*/);
} else {
org.junit.runner.JUnitCore.main(TestPerf001GLJPanelInit01AWT.class.getName());
}
Expand Down
Loading

0 comments on commit 10fee84

Please sign in to comment.