From 26f965bbe7b40968158901c3f4ef2f54e821ac70 Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Fri, 23 Jan 2015 23:34:39 +0100
Subject: GLContextImpl.makeCurrentImpl(): Remove redundant test whether
 context is already current

- GLContextImpl.makeCurrentImpl(): Remove redundant test whether context is already current
  GLContext.makeCurrent() already verifies whether native makeCurrent can be skipped
  by comparing against the thread-local current GLContext instance.

- Add X11GLXContext.glXReleaseContext(..) to simplify release call
---
 src/jogl/classes/jogamp/opengl/egl/EGLContext.java | 18 ++++---
 .../jogamp/opengl/macosx/cgl/MacOSXCGLContext.java |  2 +-
 .../opengl/windows/wgl/WindowsWGLContext.java      | 15 +++---
 .../jogamp/opengl/x11/glx/X11GLXContext.java       | 56 ++++++++++++++--------
 4 files changed, 52 insertions(+), 39 deletions(-)

(limited to 'src/jogl/classes/jogamp/opengl')

diff --git a/src/jogl/classes/jogamp/opengl/egl/EGLContext.java b/src/jogl/classes/jogamp/opengl/egl/EGLContext.java
index fe01fbc8c..7b4b55b34 100644
--- a/src/jogl/classes/jogamp/opengl/egl/EGLContext.java
+++ b/src/jogl/classes/jogamp/opengl/egl/EGLContext.java
@@ -115,16 +115,14 @@ public class EGLContext extends GLContextImpl {
 
     @Override
     protected void makeCurrentImpl() throws GLException {
-        if (EGL.eglGetCurrentContext() != contextHandle) {
-            final long dpy = drawable.getNativeSurface().getDisplayHandle();
-            if (!EGL.eglMakeCurrent(dpy, drawable.getHandle(), drawableRead.getHandle(), contextHandle)) {
-                throw new GLException("Error making context " + toHexString(contextHandle) +
-                                      " current on Thread " + getThreadName() +
-                                      " with display " + toHexString(dpy) +
-                                      ", drawableWrite " + toHexString(drawable.getHandle()) +
-                                      ", drawableRead "+ toHexString(drawableRead.getHandle()) +
-                                      " - Error code " + toHexString(EGL.eglGetError()) + ", " + this);
-            }
+        final long dpy = drawable.getNativeSurface().getDisplayHandle();
+        if ( !EGL.eglMakeCurrent(dpy, drawable.getHandle(), drawableRead.getHandle(), contextHandle) ) {
+            throw new GLException("Error making context " + toHexString(contextHandle) +
+                    " current on Thread " + getThreadName() +
+                    " with display " + toHexString(dpy) +
+                    ", drawableWrite " + toHexString(drawable.getHandle()) +
+                    ", drawableRead "+ toHexString(drawableRead.getHandle()) +
+                    " - Error code " + toHexString(EGL.eglGetError()) + ", " + this);
         }
     }
 
diff --git a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java
index 3ff4b6529..6c09bd8a3 100644
--- a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java
+++ b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLContext.java
@@ -310,7 +310,7 @@ public class MacOSXCGLContext extends GLContextImpl
     if (getOpenGLMode() != ((MacOSXCGLDrawable)drawable).getOpenGLMode()) {
       setOpenGLMode(((MacOSXCGLDrawable)drawable).getOpenGLMode());
     } */
-    if (!impl.makeCurrent(contextHandle)) {
+    if ( !impl.makeCurrent(contextHandle) ) {
       throw new GLException("Error making Context current: "+this);
     }
     drawableUpdatedNotify();
diff --git a/src/jogl/classes/jogamp/opengl/windows/wgl/WindowsWGLContext.java b/src/jogl/classes/jogamp/opengl/windows/wgl/WindowsWGLContext.java
index c9d78b116..131187a4d 100644
--- a/src/jogl/classes/jogamp/opengl/windows/wgl/WindowsWGLContext.java
+++ b/src/jogl/classes/jogamp/opengl/windows/wgl/WindowsWGLContext.java
@@ -160,20 +160,19 @@ public class WindowsWGLContext extends GLContextImpl {
     return ok;
   }
 
-  private final boolean wglReleaseContext(final long ctx) {
+  private final boolean wglReleaseContext() {
     boolean ok = false;
     if(wglGLReadDrawableAvailable) {
         // needs initilized WGL ProcAddress table
-        ok = getWGLExt().wglMakeContextCurrent(0, 0, ctx);
+        ok = getWGLExt().wglMakeContextCurrent(0, 0, 0);
     } else {
-        ok = WGL.wglMakeCurrent(0, ctx);
+        ok = WGL.wglMakeCurrent(0, 0);
     }
     if( !ok ) {
         final int werr = GDI.GetLastError();
         final boolean ok2 = werr == GDI.ERROR_SUCCESS;
         if(DEBUG) {
-            final Throwable t = new Throwable ("Info: wglReleaseContext NOK: ctx " + GLContext.toHexString(ctx) +
-                                               ", werr " + werr + " -> ok "+ok2);
+            final Throwable t = new Throwable ("Info: wglReleaseContext NOK: werr " + werr + " -> ok "+ok2);
             t.printStackTrace();
         }
         // Some GPU's falsely fails with a zero error code (success),
@@ -417,20 +416,18 @@ public class WindowsWGLContext extends GLContextImpl {
 
   @Override
   protected void  makeCurrentImpl() throws GLException {
-    if (WGL.wglGetCurrentContext() != contextHandle) {
-      if (!wglMakeContextCurrent(drawable.getHandle(), drawableRead.getHandle(), contextHandle)) {
+      if ( !wglMakeContextCurrent(drawable.getHandle(), drawableRead.getHandle(), contextHandle) ) {
         throw new GLException("Error making context " + toHexString(contextHandle) +
                               " current on Thread " + getThreadName() +
                               ", drawableWrite " + toHexString(drawable.getHandle()) +
                               ", drawableRead "+ toHexString(drawableRead.getHandle()) +
                               ", werr: " + GDI.GetLastError() + ", " + this);
       }
-    }
   }
 
   @Override
   protected void releaseImpl() throws GLException {
-    if (!wglReleaseContext(0)) {
+    if ( !wglReleaseContext() ) {
         throw new GLException("Error freeing OpenGL context, werr: " + GDI.GetLastError());
     }
   }
diff --git a/src/jogl/classes/jogamp/opengl/x11/glx/X11GLXContext.java b/src/jogl/classes/jogamp/opengl/x11/glx/X11GLXContext.java
index 5d0f154cb..9da0075a0 100644
--- a/src/jogl/classes/jogamp/opengl/x11/glx/X11GLXContext.java
+++ b/src/jogl/classes/jogamp/opengl/x11/glx/X11GLXContext.java
@@ -187,11 +187,32 @@ public class X11GLXContext extends GLContextImpl {
     return res;
   }
 
+  private final boolean glXReleaseContext(final long dpy) {
+    boolean res = false;
+
+    try {
+        if ( isGLXVersionGreaterEqualOneThree() ) {
+            // System.err.println(getThreadName() +": X11GLXContext.releaseCurrent: obj " + toHexString(hashCode()) + " / ctx "+toHexString(contextHandle)+": ctx "+toHexString(ctx)+" - switch");
+            res = GLX.glXMakeContextCurrent(dpy, 0, 0, 0);
+        } else {
+            // System.err.println(getThreadName() +": X11GLXContext.releaseCurrent: obj " + toHexString(hashCode()) + " / ctx "+toHexString(contextHandle)+": ctx "+toHexString(ctx)+" - switch");
+            res = GLX.glXMakeCurrent(dpy, 0, 0);
+        }
+    } catch (final RuntimeException re) {
+        if( DEBUG_TRACE_SWITCH ) {
+          System.err.println(getThreadName()+": Warning: X11GLXContext.glXReleaseContext failed: "+re+", with "+
+            "dpy "+toHexString(dpy));
+          re.printStackTrace();
+        }
+    }
+    return res;
+  }
+
   @Override
   protected void destroyContextARBImpl(final long ctx) {
     final long display = drawable.getNativeSurface().getDisplayHandle();
 
-    glXMakeContextCurrent(display, 0, 0, 0);
+    glXReleaseContext(display);
     GLX.glXDestroyContext(display, ctx);
   }
   private static final int ctx_arb_attribs_idx_major = 0;
@@ -269,7 +290,7 @@ public class X11GLXContext extends GLContextImpl {
               System.err.println(getThreadName()+": X11GLXContext.createContextARBImpl couldn't make current "+getGLVersion(major, minor, ctp, "@creation"));
             }
             // release & destroy
-            glXMakeContextCurrent(display, 0, 0, 0);
+            glXReleaseContext(display);
             GLX.glXDestroyContext(display, ctx);
             ctx = 0;
         } else if (DEBUG) {
@@ -314,7 +335,7 @@ public class X11GLXContext extends GLContextImpl {
           throw new GLException(getThreadName()+": Error making temp context(0) current: display "+toHexString(display)+", context "+toHexString(contextHandle)+", drawable "+drawable);
         }
         if( !setGLFunctionAvailability(true, 0, 0, CTX_PROFILE_COMPAT, false /* strictMatch */, null == sharedContext /* withinGLVersionsMapping */) ) { // use GL_VERSION
-            glXMakeContextCurrent(display, 0, 0, 0); // release temp context
+            glXReleaseContext(display); // release temp context
             GLX.glXDestroyContext(display, contextHandle);
             contextHandle = 0;
             throw new InternalError("setGLFunctionAvailability !strictMatch failed.1");
@@ -349,11 +370,11 @@ public class X11GLXContext extends GLContextImpl {
             throw new GLException(getThreadName()+": Error making temp context(1) current: display "+toHexString(display)+", context "+toHexString(temp_ctx)+", drawable "+drawable);
         }
         if( !setGLFunctionAvailability(true, 0, 0, CTX_PROFILE_COMPAT, false /* strictMatch */, null == sharedContext /* withinGLVersionsMapping */) ) { // use GL_VERSION
-            glXMakeContextCurrent(display, 0, 0, 0); // release temp context
+            glXReleaseContext(display); // release temp context
             GLX.glXDestroyContext(display, temp_ctx);
             throw new InternalError("setGLFunctionAvailability !strictMatch failed.2");
         }
-        glXMakeContextCurrent(display, 0, 0, 0); // release temp context
+        glXReleaseContext(display); // release temp context
         if( !createContextARBTried ) {
             // is*Available calls are valid since setGLFunctionAvailability(..) was called
             final boolean isProcCreateContextAttribsARBAvailable = isFunctionAvailable("glXCreateContextAttribsARB");
@@ -381,7 +402,7 @@ public class X11GLXContext extends GLContextImpl {
 
     if( 0 != contextHandle ) {
         if( 0 != temp_ctx ) {
-            glXMakeContextCurrent(display, 0, 0, 0);
+            glXReleaseContext(display);
             GLX.glXDestroyContext(display, temp_ctx);
             if ( !glXMakeContextCurrent(display, drawable.getHandle(), drawableRead.getHandle(), contextHandle) ) {
                 throw new GLException(getThreadName()+": Cannot make previous verified context current");
@@ -391,7 +412,7 @@ public class X11GLXContext extends GLContextImpl {
         if( glp.isGL3() && createContextARBTried ) {
             // We shall not allow context creation >= GL3 w/ non ARB methods if ARB is used,
             // otherwise context of similar profile but different creation method may not be share-able.
-            glXMakeContextCurrent(display, 0, 0, 0);
+            glXReleaseContext(display);
             GLX.glXDestroyContext(display, temp_ctx);
             throw new GLException(getThreadName()+": X11GLXContext.createContextImpl ARB n/a but required, profile > GL2 requested (OpenGL >= 3.1). Requested: "+glp+", current: "+getGLVersion());
         }
@@ -403,7 +424,7 @@ public class X11GLXContext extends GLContextImpl {
         // continue with temp context
         contextHandle = temp_ctx;
         if ( !glXMakeContextCurrent(display, drawable.getHandle(), drawableRead.getHandle(), contextHandle) ) {
-            glXMakeContextCurrent(display, 0, 0, 0);
+            glXReleaseContext(display);
             GLX.glXDestroyContext(display, temp_ctx);
             throw new GLException(getThreadName()+": Error making context(1) current: display "+toHexString(display)+", context "+toHexString(contextHandle)+", drawable "+drawable);
         }
@@ -422,23 +443,20 @@ public class X11GLXContext extends GLContextImpl {
   @Override
   protected void makeCurrentImpl() throws GLException {
     final long dpy = drawable.getNativeSurface().getDisplayHandle();
-
-    if (GLX.glXGetCurrentContext() != contextHandle) {
-        if (!glXMakeContextCurrent(dpy, drawable.getHandle(), drawableRead.getHandle(), contextHandle)) {
-            throw new GLException("Error making context " + toHexString(contextHandle) +
-                                  " current on Thread " + getThreadName() +
-                                  " with display " + toHexString(dpy) +
-                                  ", drawableWrite " + toHexString(drawable.getHandle()) +
-                                  ", drawableRead "+ toHexString(drawableRead.getHandle()) +
-                                  " - " + this);
-        }
+    if ( !glXMakeContextCurrent(dpy, drawable.getHandle(), drawableRead.getHandle(), contextHandle) ) {
+        throw new GLException("Error making context " + toHexString(contextHandle) +
+                " current on Thread " + getThreadName() +
+                " with display " + toHexString(dpy) +
+                ", drawableWrite " + toHexString(drawable.getHandle()) +
+                ", drawableRead "+ toHexString(drawableRead.getHandle()) +
+                " - " + this);
     }
   }
 
   @Override
   protected void releaseImpl() throws GLException {
     final long display = drawable.getNativeSurface().getDisplayHandle();
-    if (!glXMakeContextCurrent(display, 0, 0, 0)) {
+    if ( !glXReleaseContext(display) ) {
         throw new GLException(getThreadName()+": Error freeing OpenGL context");
     }
   }
-- 
cgit v1.2.3