From 1bfa7a8f6e6dc5999e337aac2b9ddf118cf45a2f Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Sun, 7 Aug 2011 07:53:46 +0200
Subject: GLProfile / eager init: Use relaxed query, if context was created
 while initialize shared

---
 .../javax/media/opengl/GLDrawableFactory.java      | 12 ++-
 src/jogl/classes/javax/media/opengl/GLProfile.java |  6 +-
 .../jogamp/opengl/egl/EGLDrawableFactory.java      | 93 +++++++++++++++++-----
 .../macosx/cgl/MacOSXCGLDrawableFactory.java       |  9 ++-
 .../windows/wgl/WindowsWGLDrawableFactory.java     |  8 ++
 .../opengl/x11/glx/X11GLXDrawableFactory.java      |  9 +++
 6 files changed, 106 insertions(+), 31 deletions(-)

(limited to 'src/jogl')

diff --git a/src/jogl/classes/javax/media/opengl/GLDrawableFactory.java b/src/jogl/classes/javax/media/opengl/GLDrawableFactory.java
index 5fc977581..1340b661d 100644
--- a/src/jogl/classes/javax/media/opengl/GLDrawableFactory.java
+++ b/src/jogl/classes/javax/media/opengl/GLDrawableFactory.java
@@ -260,19 +260,17 @@ public abstract class GLDrawableFactory {
   }
 
   /**
-   * Returns true if a shared context is already mapped to the <code>device</code> {@link AbstractGraphicsDevice#getConnection()},
-   * or if a new shared context could be created and mapped. Otherwise return false.<br>
-   * Creation of the shared context is tried only once.
+   * Returns true if a shared context could be created while initialization 
+   * of shared resources for <code>device</code> {@link AbstractGraphicsDevice#getConnection()}.<br>
+   * This does not imply a shared context is mapped, but was available<br>.
    *
    * @param device which {@link javax.media.nativewindow.AbstractGraphicsDevice#getConnection() connection} denotes the shared the target device, may be <code>null</code> for the platform's default device.
    */
-  public final boolean getIsSharedContextAvailable(AbstractGraphicsDevice device) {
-      return null != getOrCreateSharedContext(device);
-  }
+  public abstract boolean getWasSharedContextCreated(AbstractGraphicsDevice device);
 
   /**
    * Returns the shared context mapped to the <code>device</code> {@link AbstractGraphicsDevice#getConnection()},
-   * either a preexisting or newly created, or <code>null</code> if creation failed or not supported.<br>
+   * either a pre-existing or newly created, or <code>null</code> if creation failed or not supported.<br>
    * Creation of the shared context is tried only once.
    *
    * @param device which {@link javax.media.nativewindow.AbstractGraphicsDevice#getConnection() connection} denotes the shared the target device, may be <code>null</code> for the platform's default device.
diff --git a/src/jogl/classes/javax/media/opengl/GLProfile.java b/src/jogl/classes/javax/media/opengl/GLProfile.java
index 5882046ec..0f502ce32 100644
--- a/src/jogl/classes/javax/media/opengl/GLProfile.java
+++ b/src/jogl/classes/javax/media/opengl/GLProfile.java
@@ -1279,7 +1279,7 @@ public class GLProfile {
         final boolean addedAnyProfile = addedDesktopProfile || addedEGLProfile ;
 
         if(DEBUG) {
-            System.err.println("GLProfile.init addedAnyProfile(e/d) "+addedAnyProfile+" ("+addedDesktopProfile+"/"+addedEGLProfile+")");
+            System.err.println("GLProfile.init addedAnyProfile(d/e) "+addedAnyProfile+" ("+addedDesktopProfile+"/"+addedEGLProfile+")");
             System.err.println("GLProfile.init isAWTAvailable       "+isAWTAvailable);
             System.err.println("GLProfile.init hasDesktopGLFactory  "+hasDesktopGLFactory);
             System.err.println("GLProfile.init hasGL234Impl         "+hasGL234Impl);
@@ -1333,7 +1333,7 @@ public class GLProfile {
 
             // Triggers eager initialization of share context in GLDrawableFactory for the device,
             // hence querying all available GLProfiles
-            boolean desktopSharedCtxAvail = desktopFactory.getIsSharedContextAvailable(device);
+            boolean desktopSharedCtxAvail = desktopFactory.getWasSharedContextCreated(device);
             if (DEBUG) {
                 System.err.println("GLProfile.initProfilesForDevice: "+device+": desktop Shared Ctx "+desktopSharedCtxAvail);
             }
@@ -1352,7 +1352,7 @@ public class GLProfile {
 
             // Triggers eager initialization of share context in GLDrawableFactory for the device,
             // hence querying all available GLProfiles
-            boolean eglSharedCtxAvail = eglFactory.getIsSharedContextAvailable(device);
+            boolean eglSharedCtxAvail = eglFactory.getWasSharedContextCreated(device);
             if (DEBUG) {
                 System.err.println("GLProfile.initProfilesForDevice: "+device+": egl Shared Ctx "+eglSharedCtxAvail);
             }
diff --git a/src/jogl/classes/jogamp/opengl/egl/EGLDrawableFactory.java b/src/jogl/classes/jogamp/opengl/egl/EGLDrawableFactory.java
index 45d286da3..509ef1f99 100644
--- a/src/jogl/classes/jogamp/opengl/egl/EGLDrawableFactory.java
+++ b/src/jogl/classes/jogamp/opengl/egl/EGLDrawableFactory.java
@@ -42,7 +42,9 @@ import javax.media.opengl.*;
 
 import com.jogamp.common.JogampRuntimeException;
 import com.jogamp.common.util.*;
+
 import jogamp.opengl.*;
+import jogamp.opengl.x11.glx.X11GLXContext;
 import jogamp.nativewindow.WrappedSurface;
 
 import java.util.HashMap;
@@ -102,15 +104,27 @@ public class EGLDrawableFactory extends GLDrawableFactoryImpl {
 
     static class SharedResource {
       private EGLGraphicsDevice device;
-      //private EGLDrawable drawable;
-      //private EGLContext context;
-
-      SharedResource(EGLGraphicsDevice dev /*, EGLDrawable draw, EGLContext ctx */) {
+      // private EGLDrawable drawable;
+      // private EGLContext contextES1;
+      // private EGLContext contextES2;
+      private boolean wasES1ContextCreated;
+      private boolean wasES2ContextCreated;
+
+      SharedResource(EGLGraphicsDevice dev, boolean wasContextES1Created, boolean wasContextES2Created 
+                     /*EGLDrawable draw, EGLContext ctxES1, EGLContext ctxES2 */) {
           device = dev;
           // drawable = draw;
-          // context = ctx;
+          // contextES1 = ctxES1;
+          // contextES2 = ctxES2;
+          this.wasES1ContextCreated = wasContextES1Created;
+          this.wasES2ContextCreated = wasContextES2Created;
       }
-      EGLGraphicsDevice getDevice() { return device; }
+      final EGLGraphicsDevice getDevice() { return device; }
+      // final EGLDrawable getDrawable() { return drawable; }
+      // final EGLContext getContextES1() { return contextES1; }
+      // final EGLContext getContextES2() { return contextES2; }
+      final boolean wasES1ContextAvailable() { return wasES1ContextCreated; }
+      final boolean wasES2ContextAvailable() { return wasES2ContextCreated; }
     }
     HashMap/*<connection, SharedResource>*/ sharedMap = new HashMap();
     EGLGraphicsDevice defaultDevice;
@@ -123,40 +137,81 @@ public class EGLDrawableFactory extends GLDrawableFactoryImpl {
       return true; // via mappings (X11/WGL/.. -> EGL) we shall be able to handle all types.
     }
 
-    private SharedResource getOrCreateShared(AbstractGraphicsDevice device) {
-        String connection = device.getConnection();
+    private boolean isEGLContextAvailable(EGLGraphicsDevice sharedDevice, String profile) {
+        boolean madeCurrent = false;
+        final GLCapabilities caps = new GLCapabilities(GLProfile.get(profile));
+        caps.setRedBits(5); caps.setGreenBits(5); caps.setBlueBits(5); caps.setAlphaBits(0);
+        caps.setDoubleBuffered(false);
+        caps.setOnscreen(false);
+        caps.setPBuffer(true);
+        final EGLDrawable drawable = (EGLDrawable) createGLDrawable( createOffscreenSurfaceImpl(sharedDevice, caps, caps, null, 64, 64) );        
+        if(null!=drawable) {
+            final EGLContext context = (EGLContext) drawable.createContext(null);
+            if (null != context) {
+                context.setSynchronized(true);
+                try {
+                    context.makeCurrent(); // could cause exception
+                    madeCurrent = context.isCurrent();
+                } catch (GLException gle) {
+                    if (DEBUG) {
+                        System.err.println("EGLDrawableFactory.createShared: INFO: makeCurrent failed");
+                        gle.printStackTrace();
+                    }                    
+                } finally {
+                    context.destroy();
+                }
+            }
+            drawable.destroy();
+        }
+        return madeCurrent;
+    }
+    
+    private SharedResource getOrCreateShared(AbstractGraphicsDevice adevice) {
+        String connection = adevice.getConnection();
         SharedResource sr;
         synchronized(sharedMap) {
             sr = (SharedResource) sharedMap.get(connection);
         }
-        if(null==sr) {
+        if(null==sr) {   
             long eglDisplay = EGL.eglGetDisplay(EGL.EGL_DEFAULT_DISPLAY);
             if (eglDisplay == EGL.EGL_NO_DISPLAY) {
                 throw new GLException("Failed to created EGL default display: error 0x"+Integer.toHexString(EGL.eglGetError()));
             } else if(DEBUG) {
-                System.err.println("eglDisplay(EGL_DEFAULT_DISPLAY): 0x"+Long.toHexString(eglDisplay));
+                System.err.println("EGLDrawableFactory.createShared: eglDisplay(EGL_DEFAULT_DISPLAY): 0x"+Long.toHexString(eglDisplay));
             }
             if (!EGL.eglInitialize(eglDisplay, null, null)) {
                 throw new GLException("eglInitialize failed"+", error 0x"+Integer.toHexString(EGL.eglGetError()));
             }
-            EGLGraphicsDevice sharedDevice = new EGLGraphicsDevice(eglDisplay, connection, device.getUnitID());
-            sr = new SharedResource(sharedDevice);
+            final EGLGraphicsDevice sharedDevice = new EGLGraphicsDevice(eglDisplay, connection, adevice.getUnitID());            
+            // final boolean madeCurrentES1 = isEGLContextAvailable(sharedDevice, GLProfile.GLES1);
+            // final boolean madeCurrentES2 = isEGLContextAvailable(sharedDevice, GLProfile.GLES2);
+            final boolean madeCurrentES1 = true; // FIXME
+            final boolean madeCurrentES2 = true; // FIXME
+            sr = new SharedResource(sharedDevice, madeCurrentES1, madeCurrentES2);
             synchronized(sharedMap) {
                 sharedMap.put(connection, sr);
             }
             if (DEBUG) {
-              System.err.println("!!! SharedDevice: "+sharedDevice);
-            }
+                System.err.println("EGLDrawableFactory.createShared: device:  " + sharedDevice);
+                System.err.println("EGLDrawableFactory.createShared: context ES1: " + madeCurrentES1);
+                System.err.println("EGLDrawableFactory.createShared: context ES2: " + madeCurrentES2);
+            }                        
         }
         return sr;
     }
 
-
+    public final boolean getWasSharedContextCreated(AbstractGraphicsDevice device) {
+        SharedResource sr = getOrCreateShared(device);
+        if(null!=sr) {
+            return sr.wasES1ContextAvailable() || sr.wasES2ContextAvailable();
+        }
+        return false;        
+    }
+    
     protected final GLContext getOrCreateSharedContextImpl(AbstractGraphicsDevice device) {
-        // FIXME: not implemented .. needs a dummy EGL surface - NEEDED ?
-        return null;
+        return null; // n/a for EGL .. since we don't keep the resources
     }
-
+    
     protected AbstractGraphicsDevice getOrCreateSharedDeviceImpl(AbstractGraphicsDevice device) {
         SharedResource sr = getOrCreateShared(device);
         if(null!=sr) {
@@ -205,7 +260,7 @@ public class EGLDrawableFactory extends GLDrawableFactoryImpl {
         AbstractGraphicsConfiguration config = target.getGraphicsConfiguration().getNativeGraphicsConfiguration();
         GLCapabilitiesImmutable caps = (GLCapabilitiesImmutable) config.getChosenCapabilities();
         if(!caps.isPBuffer()) {
-            throw new GLException("Not yet implemented");
+            throw new GLException("Non pbuffer not yet implemented");
         }
         // PBuffer GLDrawable Creation
         return new EGLPbufferDrawable(this, target);
diff --git a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawableFactory.java b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawableFactory.java
index 6ce793490..3a0f28352 100644
--- a/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawableFactory.java
+++ b/src/jogl/classes/jogamp/opengl/macosx/cgl/MacOSXCGLDrawableFactory.java
@@ -116,9 +116,14 @@ public class MacOSXCGLDrawableFactory extends GLDrawableFactoryImpl {
       return false;
   }
 
+  public final boolean getWasSharedContextCreated(AbstractGraphicsDevice device) {
+      // FIXME: not implemented .. needs a dummy OSX surface
+      return false;
+  }
+  
   protected final GLContext getOrCreateSharedContextImpl(AbstractGraphicsDevice device) {
-        // FIXME: not implemented .. needs a dummy OSX surface
-        return null;
+      // FIXME: not implemented .. needs a dummy OSX surface
+      return null;
   }
 
   protected AbstractGraphicsDevice getOrCreateSharedDeviceImpl(AbstractGraphicsDevice device) {
diff --git a/src/jogl/classes/jogamp/opengl/windows/wgl/WindowsWGLDrawableFactory.java b/src/jogl/classes/jogamp/opengl/windows/wgl/WindowsWGLDrawableFactory.java
index 13bb24237..f8ba8d277 100644
--- a/src/jogl/classes/jogamp/opengl/windows/wgl/WindowsWGLDrawableFactory.java
+++ b/src/jogl/classes/jogamp/opengl/windows/wgl/WindowsWGLDrawableFactory.java
@@ -373,6 +373,14 @@ public class WindowsWGLDrawableFactory extends GLDrawableFactoryImpl {
   final static String WGL_ARB_make_current_read = "WGL_ARB_make_current_read";
   final static String wglMakeContextCurrent = "wglMakeContextCurrent";
 
+  public final boolean getWasSharedContextCreated(AbstractGraphicsDevice device) {
+    SharedResourceRunner.Resource sr = sharedResourceRunner.getOrCreateShared(device);
+    if(null!=sr) {
+      return null != sr.getContext();
+    }
+    return false;
+  }
+  
   protected final GLContext getOrCreateSharedContextImpl(AbstractGraphicsDevice device) {
     SharedResourceRunner.Resource sr = sharedResourceRunner.getOrCreateShared(device);
     if(null!=sr) {
diff --git a/src/jogl/classes/jogamp/opengl/x11/glx/X11GLXDrawableFactory.java b/src/jogl/classes/jogamp/opengl/x11/glx/X11GLXDrawableFactory.java
index 14f2a28f1..1caf1e24b 100644
--- a/src/jogl/classes/jogamp/opengl/x11/glx/X11GLXDrawableFactory.java
+++ b/src/jogl/classes/jogamp/opengl/x11/glx/X11GLXDrawableFactory.java
@@ -47,6 +47,7 @@ import javax.media.nativewindow.x11.*;
 import javax.media.opengl.*;
 
 import jogamp.opengl.*;
+
 import com.jogamp.common.JogampRuntimeException;
 import com.jogamp.common.util.*;
 import jogamp.nativewindow.WrappedSurface;
@@ -260,6 +261,14 @@ public class X11GLXDrawableFactory extends GLDrawableFactoryImpl {
       return false;
   }
 
+  public final boolean getWasSharedContextCreated(AbstractGraphicsDevice device) {
+    SharedResourceRunner.Resource sr = sharedResourceRunner.getOrCreateShared(device);
+    if(null!=sr) {
+      return null != sr.getContext();
+    }
+    return false;
+  }
+  
   protected final GLContext getOrCreateSharedContextImpl(AbstractGraphicsDevice device) {
     SharedResourceRunner.Resource sr = sharedResourceRunner.getOrCreateShared(device);
     if(null!=sr) {
-- 
cgit v1.2.3