From e6225fce71daa90a2a2b631550ba048c2a84ff25 Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Wed, 27 Oct 2010 16:39:20 +0200
Subject: WindowImpl/GLWindow LifecycleHook:     - 'destroyAction' ->
 'destroyActionPreLock' 'destroyActionInLock',       to be able to stop
 animation before locking.

GLDrawableHelper.invokeGL() dispose case (initAction == null):
    - pause animator if animating before makeCurrent (locking)

GLCanvas/GLJPanel dispose: recreate case
    - resume animator if was animating
---
 .../com/jogamp/opengl/impl/GLDrawableHelper.java   | 38 +++++++++++++++++++---
 .../classes/javax/media/opengl/awt/GLCanvas.java   | 23 ++++++++++---
 .../classes/javax/media/opengl/awt/GLJPanel.java   | 11 +++++++
 3 files changed, 63 insertions(+), 9 deletions(-)

(limited to 'src/jogl')

diff --git a/src/jogl/classes/com/jogamp/opengl/impl/GLDrawableHelper.java b/src/jogl/classes/com/jogamp/opengl/impl/GLDrawableHelper.java
index c8cfd9a01..e8df40b13 100644
--- a/src/jogl/classes/com/jogamp/opengl/impl/GLDrawableHelper.java
+++ b/src/jogl/classes/com/jogamp/opengl/impl/GLDrawableHelper.java
@@ -131,6 +131,11 @@ public class GLDrawableHelper {
     }
   }
 
+  /**
+   * Issues {@link javax.media.opengl.GLEventListener#dispose(javax.media.opengl.GLAutoDrawable)}
+   * to all listeners.
+   * @param drawable
+   */
   public final void dispose(GLAutoDrawable drawable) {
     synchronized(listenersLock) {
         listenersIter = true;
@@ -283,12 +288,24 @@ public class GLDrawableHelper {
   }
 
   private static final ThreadLocal perThreadInitAction = new ThreadLocal();
+
   /** Principal helper method which runs a Runnable with the context
       made current. This could have been made part of GLContext, but a
-      desired goal is to be able to implement the GLCanvas in terms of
+      desired goal is to be able to implement GLAutoDrawable's in terms of
       the GLContext's public APIs, and putting it into a separate
       class helps ensure that we don't inadvertently use private
-      methods of the GLContext or its implementing classes. */
+      methods of the GLContext or its implementing classes.<br>
+   * <br>
+   * Remark: In case this method is called to dispose the GLDrawable/GLAutoDrawable,
+   * <code>initAction</code> shall be <code>null</code> to mark this cause.<br>
+   * In this case, the locally delegated {@link javax.media.opengl.GLAnimatorControl} via {@link #setAnimator(javax.media.opengl.GLAnimatorControl) setAnimator(animatorControl)}
+   * is paused first, if {@link javax.media.opengl.GLAnimatorControl#isAnimating()}.
+   *
+   * @param drawable
+   * @param context
+   * @param runnable
+   * @param initAction
+   */
   public final void invokeGL(GLDrawable drawable,
                              GLContext context,
                              Runnable  runnable,
@@ -300,6 +317,20 @@ public class GLDrawableHelper {
         }
         return;
     }
+
+    if(null==initAction) {
+        // disposal case
+
+        if(!context.isCreated()) {
+            throw new GLException("Dispose case (no init action given): Native context must be created: "+context);
+        }
+
+        GLAnimatorControl animCtrl =  getAnimator();
+        if(null!=animCtrl && animCtrl.isAnimating()) {
+            animCtrl.pause();
+        }
+    }
+
     // Support for recursive makeCurrent() calls as well as calling
     // other drawables' display() methods from within another one's
     GLContext lastContext    = GLContext.getCurrent();
@@ -308,9 +339,6 @@ public class GLDrawableHelper {
       lastContext.release();
     }
   
-    if(!context.isCreated() && null == initAction) {
-        throw new GLException("Context has to be created, but no initAction is given: "+context);
-    }
     int res = 0;
     try {
       res = context.makeCurrent();
diff --git a/src/jogl/classes/javax/media/opengl/awt/GLCanvas.java b/src/jogl/classes/javax/media/opengl/awt/GLCanvas.java
index 4ac21204f..c165a4833 100644
--- a/src/jogl/classes/javax/media/opengl/awt/GLCanvas.java
+++ b/src/jogl/classes/javax/media/opengl/awt/GLCanvas.java
@@ -315,6 +315,12 @@ public class GLCanvas extends Canvas implements AWTGLAutoDrawable {
     }
 
     if(null!=context) {
+        boolean animatorWasAnimating = false;
+        GLAnimatorControl animator =  getAnimator();
+        if(null!=animator) {
+            animatorWasAnimating = animator.isAnimating();
+        }
+
         disposeRegenerate=regenerate;
 
         if (Threading.isSingleThreaded() &&
@@ -336,6 +342,10 @@ public class GLCanvas extends Canvas implements AWTGLAutoDrawable {
         } else if(context.isCreated()) {
           drawableHelper.invokeGL(drawable, context, disposeAction, null);
         }
+
+        if(regenerate && animatorWasAnimating && animator.isPaused()) {
+          animator.resume();
+        }
     }
 
     if(DEBUG) {
@@ -420,9 +430,15 @@ public class GLCanvas extends Canvas implements AWTGLAutoDrawable {
             if(null==awtConfig) {
               throw new GLException("Error: AWTGraphicsConfiguration is null");
             }
-            drawable = GLDrawableFactory.getFactory(glProfile).createGLDrawable(NativeWindowFactory.getNativeWindow(this, awtConfig));
-            context = (GLContextImpl) drawable.createContext(shareWith);
-            context.setSynchronized(true);
+            // awtConfig.getScreen().getDevice().lock();
+            try {
+                drawable = GLDrawableFactory.getFactory(glProfile).createGLDrawable(NativeWindowFactory.getNativeWindow(this, awtConfig));
+                context = (GLContextImpl) drawable.createContext(shareWith);
+                context.setSynchronized(true);
+                drawable.setRealized(true);
+            } finally {
+                // awtConfig.getScreen().getDevice().unlock();
+            }
         } finally {
             NativeWindowFactory.getDefaultToolkitLock().unlock();
         }
@@ -430,7 +446,6 @@ public class GLCanvas extends Canvas implements AWTGLAutoDrawable {
         if(DEBUG) {
             System.err.println("Created Drawable: "+drawable);
         }
-        drawable.setRealized(true);
     }
   }
 
diff --git a/src/jogl/classes/javax/media/opengl/awt/GLJPanel.java b/src/jogl/classes/javax/media/opengl/awt/GLJPanel.java
index 192695955..10aeefaf5 100644
--- a/src/jogl/classes/javax/media/opengl/awt/GLJPanel.java
+++ b/src/jogl/classes/javax/media/opengl/awt/GLJPanel.java
@@ -213,7 +213,14 @@ public class GLJPanel extends JPanel implements AWTGLAutoDrawable {
         Exception ex1 = new Exception("Info: dispose("+regenerate+") - start");
         ex1.printStackTrace();
     }
+
     if (backend != null) {
+      boolean animatorWasAnimating = false;
+      GLAnimatorControl animator =  getAnimator();
+      if(null!=animator) {
+        animatorWasAnimating = animator.isAnimating();
+      }
+
       disposeRegenerate=regenerate;
       disposeContext=backend.getContext();
       disposeDrawable=backend.getDrawable();
@@ -242,6 +249,10 @@ public class GLJPanel extends JPanel implements AWTGLAutoDrawable {
       if(null==disposeContext) {
         isInitialized = false;
       }
+
+      if(regenerate && animatorWasAnimating && animator.isPaused()) {
+          animator.resume();
+      }
     }
 
     if(DEBUG) {
-- 
cgit v1.2.3