From e2506d7663b752f00f0a98f793ebad52e65bd1e3 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Wed, 13 Mar 2013 07:12:59 +0100 Subject: Bug 665: Allow re-association of GLContext/GLEventListener to a GLDrawable (Part 5) - GLAutoDrawableBase: - Add 'setPreserveGLStateAtDestroy(..)' to preserve the GLEventListenerState at destroy() operation, and impl. details pullGLEventListenerState()/pushGLEventListenerState(). pullGLEventListenerState() is called automatic at destroyImplInLock(), where pushGLEventListenerState() has to be called after drawable realization instead of context creation. - Note/TODO: Method will become public in GLAutoDrawable in general! - NEWT/GLWindow: - Use GLEventListenerState preservation for reparenting case w/ destruction, i.e. keep GLContext/GLEventListener alive while reparenting in recreation mode. Scenario: NewtCanvasAWT Child <-> Top on OSX w/ CALayer --- .../classes/com/jogamp/newt/opengl/GLWindow.java | 20 +++++++++------ src/newt/classes/jogamp/newt/WindowImpl.java | 30 +++++++++++++++------- 2 files changed, 33 insertions(+), 17 deletions(-) (limited to 'src/newt/classes') diff --git a/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java b/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java index ce50d95dd..33f136460 100644 --- a/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java +++ b/src/newt/classes/com/jogamp/newt/opengl/GLWindow.java @@ -420,6 +420,11 @@ public class GLWindow extends GLAutoDrawableBase implements GLAutoDrawable, Wind // Hide methods here .. protected class GLLifecycleHook implements WindowImpl.LifecycleHook { + @Override + public void setPreserveResourcesAtDestroy() { + GLWindow.this.setPreserveGLStateAtDestroy(true); + } + @Override public synchronized void destroyActionPreLock() { // nop @@ -433,7 +438,7 @@ public class GLWindow extends GLAutoDrawableBase implements GLAutoDrawable, Wind //Exception e1 = new Exception(msg); //e1.printStackTrace(); } - + destroyImplInLock(); if(Window.DEBUG_IMPLEMENTATION) { @@ -479,8 +484,11 @@ public class GLWindow extends GLAutoDrawableBase implements GLAutoDrawable, Wind } drawable = (GLDrawableImpl) factory.createGLDrawable(nw); drawable.setRealized(true); - context = (GLContextImpl) drawable.createContext(sharedContext); - context.setContextCreationFlags(additionalCtxCreationFlags); + + if( !GLWindow.this.pushGLEventListenerState() ) { + context = (GLContextImpl) drawable.createContext(sharedContext); + context.setContextCreationFlags(additionalCtxCreationFlags); + } } if(Window.DEBUG_IMPLEMENTATION) { System.err.println("GLWindow.setVisibleActionPost("+visible+", "+nativeWindowCreated+") "+WindowImpl.getThreadName()+", fin: dt "+ (System.nanoTime()-t0)/1e6 +"ms"); @@ -512,7 +520,7 @@ public class GLWindow extends GLAutoDrawableBase implements GLAutoDrawable, Wind // private GLContext sharedContext = null; - + @Override protected final RecursiveLock getLock() { return window.getLock(); @@ -570,10 +578,6 @@ public class GLWindow extends GLAutoDrawableBase implements GLAutoDrawable, Wind return factory; } - @Override - public final void setRealized(boolean realized) { - } - @Override public final void swapBuffers() throws GLException { defaultSwapBuffers(); diff --git a/src/newt/classes/jogamp/newt/WindowImpl.java b/src/newt/classes/jogamp/newt/WindowImpl.java index af0bde179..dfaa55679 100644 --- a/src/newt/classes/jogamp/newt/WindowImpl.java +++ b/src/newt/classes/jogamp/newt/WindowImpl.java @@ -222,6 +222,12 @@ public abstract class WindowImpl implements Window, NEWTEventConsumer */ void setVisibleActionPost(boolean visible, boolean nativeWindowCreated); + /** + * Notifies the receiver to preserve resources (GL, ..) + * for the next destroy*() calls (only). + */ + void setPreserveResourcesAtDestroy(); + /** * Invoked before Window destroy action, * allows releasing of resources depending on the native Window.
@@ -953,6 +959,13 @@ public abstract class WindowImpl implements Window, NEWTEventConsumer runOnEDTIfAvail(true, destroyAction); } + protected void destroy(boolean preserveResources) { + if( preserveResources && null != WindowImpl.this.lifecycleHook ) { + WindowImpl.this.lifecycleHook.setPreserveResourcesAtDestroy(); + } + destroy(); + } + /** * @param cWin child window, must not be null * @param pWin parent window, may be null @@ -1053,9 +1066,9 @@ public abstract class WindowImpl implements Window, NEWTEventConsumer if(null==newParentWindowNEWT) { throw new NativeWindowException("Reparenting with non NEWT Window type only available after it's realized: "+newParentWindow); } - // Destroy this window and use parent's Screen. + // Destroy this window and use parent's Screen, while trying to preserve resources. // It may be created properly when the parent is made visible. - destroy(); + destroy(true); setScreen( (ScreenImpl) newParentWindowNEWT.getScreen() ); operation = ReparentOperation.ACTION_NATIVE_CREATION_PENDING; } else if(newParentWindow != getParent()) { @@ -1078,9 +1091,8 @@ public abstract class WindowImpl implements Window, NEWTEventConsumer operation = ReparentOperation.ACTION_NATIVE_CREATION_PENDING; } } else if ( forceDestroyCreate || !NewtFactory.isScreenCompatible(newParentWindow, screen) ) { - // Destroy this window, may create a new compatible Screen/Display, - // and mark it for creation. - destroy(); + // Destroy this window, may create a new compatible Screen/Display, while trying to preserve resources. + destroy(true); if(null!=newParentWindowNEWT) { setScreen( (ScreenImpl) newParentWindowNEWT.getScreen() ); } else { @@ -1109,8 +1121,8 @@ public abstract class WindowImpl implements Window, NEWTEventConsumer // Already Top Window operation = ReparentOperation.ACTION_NOP; } else if( !isNativeValid() || forceDestroyCreate ) { - // Destroy this window and mark it for [pending] creation. - destroy(); + // Destroy this window and mark it for [pending] creation, while trying to preserve resources. + destroy(true); if( 0 < width && 0 < height ) { operation = ReparentOperation.ACTION_NATIVE_CREATION; } else { @@ -1213,11 +1225,11 @@ public abstract class WindowImpl implements Window, NEWTEventConsumer } if(!ok) { - // native reparent failed -> try creation + // native reparent failed -> try creation, while trying to preserve resources. if(DEBUG_IMPLEMENTATION) { System.err.println("Window.reparent: native reparenting failed ("+getThreadName()+") windowHandle "+toHexString(windowHandle)+" parentWindowHandle "+toHexString(parentWindowHandle)+" -> "+toHexString(newParentWindowHandle)+" - Trying recreation"); } - destroy(); + destroy(true); operation = ReparentOperation.ACTION_NATIVE_CREATION ; } } -- cgit v1.2.3