Login  Register

Re: JOGL and gl.glTexSubImage2D() performance issue on Linux

Posted by Umka on Nov 15, 2012; 4:00pm
URL: https://forum.jogamp.org/JOGL-and-gl-glTexSubImage2D-performance-issue-on-Linux-tp4026810p4026967.html

diff --git a/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java b/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java
index 94f451e..5cca3d0 100644
--- a/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java
+++ b/src/java/com/jogamp/common/jvm/JNILibLoaderBase.java
@@ -154,12 +154,17 @@ public class JNILibLoaderBase {
         msg.append(nativeJarName);
         final URL jarUrlRoot = JarUtil.getURLDirname( JarUtil.getJarSubURL( classJarURL ) );
         msg.append(" + ").append(jarUrlRoot);
-        final URL nativeJarURL = JarUtil.getJarFileURL(jarUrlRoot, nativeJarName);
+        URL nativeJarURL = JarUtil.getJarFileURL(jarUrlRoot, nativeJarName);
         msg.append(" -> ").append(nativeJarURL);
         if(DEBUG) {
             System.err.println(msg.toString());
         }
-        TempJarCache.addNativeLibs(classFromJavaJar, nativeJarURL);
+        try {
+            TempJarCache.addNativeLibs(classFromJavaJar, nativeJarURL);
+        } catch (Exception e) {
+            nativeJarURL = JarUtil.getJarFileURL(jarUrlRoot, nativeJarBasename + ".jar");
+            TempJarCache.addNativeLibs(classFromJavaJar, nativeJarURL);
+        }
         ok = true;
     }
     return ok;
@@ -426,7 +431,7 @@ public class JNILibLoaderBase {
         }
     } else {
       // System.err.println("sun.boot.library.path=" + Debug.getProperty("sun.boot.library.path", false));
-      final String libraryPath = NativeLibrary.findLibrary(libraryName, cl); // implicit TempJarCache usage if used/initialized
+      String libraryPath = NativeLibrary.findLibrary(libraryName, cl); // implicit TempJarCache usage if used/initialized
       if(DEBUG) {
           System.err.print("JNILibLoaderBase: loadLibraryInternal("+libraryName+"): CL: "+libraryPath);
       }
diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java
index 72a0b3d..6a551c1 100644
--- a/src/java/com/jogamp/common/os/Platform.java
+++ b/src/java/com/jogamp/common/os/Platform.java
@@ -183,11 +183,11 @@ public class Platform extends PlatformPropsImpl {
         AWT_AVAILABLE = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
             public Boolean run() {
                 // load GluegenRT native library
-                loadGlueGenRTImpl();
-                
+                loadGlueGenRTImpl(libBaseName);
+
                 // JVM bug workaround
                 JVMUtil.initSingleton(); // requires gluegen-rt, one-time init.
-                
+
                 // detect AWT availability
                 boolean awtAvailable = false;
                 {
@@ -231,7 +231,7 @@ public class Platform extends PlatformPropsImpl {
         return null != platformClassJarURL;
     }
     
-    private static final void loadGlueGenRTImpl() {
+    public static final void loadGlueGenRTImpl(String libName) {
         if(USE_TEMP_JAR_CACHE && TempJarCache.initSingleton()) {
             String nativeJarName = null;
             URL jarUrlRoot = null;
@@ -241,14 +241,19 @@ public class Platform extends PlatformPropsImpl {
                 final String nativeJarBasename = jarName.substring(0, jarName.indexOf(".jar")); // ".jar" already validated w/ JarUtil.getJarBasename(..)
                 nativeJarName = nativeJarBasename+"-natives-"+PlatformPropsImpl.os_and_arch+".jar";                    
                 jarUrlRoot = JarUtil.getURLDirname( JarUtil.getJarSubURL(platformClassJarURL) );
-                nativeJarURL = JarUtil.getJarFileURL(jarUrlRoot, nativeJarName);
-                TempJarCache.bootstrapNativeLib(Platform.class, libBaseName, nativeJarURL);
+                try {
+                    nativeJarURL = JarUtil.getJarFileURL(jarUrlRoot, nativeJarName);
+                    TempJarCache.bootstrapNativeLib(Platform.class, libName, nativeJarURL);
+                } catch (Exception ee) {
+                    nativeJarURL = JarUtil.getJarFileURL(jarUrlRoot, jarName);
+                    TempJarCache.bootstrapNativeLib(Platform.class, libName, nativeJarURL);
+                }
+                DynamicLibraryBundle.GlueJNILibLoader.loadLibrary(libName, false, Platform.class.getClassLoader());
             } catch (Exception e0) {
                 // IllegalArgumentException, IOException
                 System.err.println("Catched "+e0.getClass().getSimpleName()+": "+e0.getMessage()+", while TempJarCache.bootstrapNativeLib() of "+nativeJarURL+" ("+jarUrlRoot+" + "+nativeJarName+")");
             }
         }
-        DynamicLibraryBundle.GlueJNILibLoader.loadLibrary(libBaseName, false, Platform.class.getClassLoader());
     }
     
     /**
diff --git a/src/java/com/jogamp/common/util/JarUtil.java b/src/java/com/jogamp/common/util/JarUtil.java
index 84ec59d..ea08183 100644
--- a/src/java/com/jogamp/common/util/JarUtil.java
+++ b/src/java/com/jogamp/common/util/JarUtil.java
@@ -48,6 +48,7 @@ import java.util.jar.JarFile;
 import com.jogamp.common.os.NativeLibrary;
 
 import jogamp.common.Debug;
+import jogamp.common.os.PlatformPropsImpl;
 
 public class JarUtil {
     private static final boolean DEBUG = Debug.debug("JarUtil");
@@ -499,36 +500,43 @@ public class JarUtil {
                                    ", isClassFile " + isClassFile + ", isDir " + isDir +
                                    ", isRootEntry " + isRootEntry );
             }
-            
+
             final File destFile = new File(dest, entryName);
             if(isDir) {
                 if (DEBUG) {
                     System.err.println("JarUtil: MKDIR: " + entryName + " -> " + destFile );
                 }
-                destFile.mkdir();
+                destFile.mkdirs();
             } else {
                 final File destFolder = new File(destFile.getParent());
                 if(!destFolder.exists()) {
                     if (DEBUG) {
                         System.err.println("JarUtil: MKDIR (parent): " + entryName + " -> " + destFolder );
-                    }                    
-                    destFolder.mkdir();
+                    }
+                    destFolder.mkdirs();
                 }
-                final InputStream in = new BufferedInputStream(jarFile.getInputStream(entry));
-                final OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
+                boolean addedAsNativeLib = false;
                 int numBytes = -1;
-                try {
-                    numBytes = IOUtil.copyStream2Stream(in, out, -1);
-                } finally {
-                    in.close();
-                    out.close();
+                if (DEBUG) {
+                    System.err.println("JarUtil: hasOsAndArch(" + destFolder.getName() + "): " + PlatformPropsImpl.hasOsAndArch(destFolder.getName()));
+                    System.err.println("JarUtil: hasLocalArch(" + destFile.getParent() + "): " + PlatformPropsImpl.hasLocalArch(destFolder.getName()));
+                    System.err.println("JarUtil: localArch: " + PlatformPropsImpl.os_and_arch);
                 }
-                boolean addedAsNativeLib = false;
-                if (numBytes>0) {
-                    num++;
-                    if (isNativeLib && ( isRootEntry || !nativeLibMap.containsKey(libBaseName) ) ) {                    
-                        nativeLibMap.put(libBaseName, destFile.getAbsolutePath());
-                        addedAsNativeLib = true;
+                if ((!PlatformPropsImpl.hasOsAndArch(destFolder.getName()) || PlatformPropsImpl.hasLocalArch(destFolder.getName())) && !nativeLibMap.containsKey(libBaseName)) {
+                    final InputStream in = new BufferedInputStream(jarFile.getInputStream(entry));
+                    final OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
+                    try {
+                        numBytes = IOUtil.copyStream2Stream(in, out, -1);
+                    } finally {
+                        in.close();
+                        out.close();
+                    }
+                    if (numBytes>0) {
+                        num++;
+                        if (isNativeLib && ( isRootEntry || !nativeLibMap.containsKey(libBaseName) )) {
+                            nativeLibMap.put(libBaseName, destFile.getAbsolutePath());
+                            addedAsNativeLib = true;
+                        }
                     }
                 }
                 if (DEBUG) {
diff --git a/src/java/com/jogamp/common/util/cache/TempJarCache.java b/src/java/com/jogamp/common/util/cache/TempJarCache.java
index 6f24c68..822efe0 100644
--- a/src/java/com/jogamp/common/util/cache/TempJarCache.java
+++ b/src/java/com/jogamp/common/util/cache/TempJarCache.java
@@ -49,6 +49,7 @@ import com.jogamp.common.os.NativeLibrary;
 import com.jogamp.common.util.IOUtil;
 import com.jogamp.common.util.JarUtil;
 import com.jogamp.common.util.SecurityUtil;
+import jogamp.common.os.PlatformPropsImpl;
 
 public class TempJarCache {
     private static final boolean DEBUG = Debug.debug("TempJarCache");
@@ -209,15 +210,15 @@ public class TempJarCache {
      */
     public synchronized static final void addNativeLibs(Class<?> certClass, URL jarURL) throws IOException, SecurityException {        
         final LoadState nativeLibJarsLS = nativeLibJars.get(jarURL);
-        if( !testLoadState(nativeLibJarsLS, LoadState.LOOKED_UP) ) {
-            nativeLibJars.put(jarURL, LoadState.LOOKED_UP);
+        if( !testLoadState(nativeLibJarsLS, LoadState.LOOKED_UP) || !testLoadState(nativeLibJarsLS, LoadState.LOADED) ) {
             final JarFile jarFile = JarUtil.getJarFile(jarURL);
             if(DEBUG) {
                 System.err.println("TempJarCache: addNativeLibs: "+jarURL+": nativeJar "+jarFile.getName());
             }
+            nativeLibJars.put(jarURL, LoadState.LOOKED_UP);
             validateCertificates(certClass, jarFile);
             JarUtil.extract(tmpFileCache.getTempDir(), nativeLibMap, jarFile,
-                            true, false, false);
+                            true, false, false);
             nativeLibJars.put(jarURL, LoadState.LOADED);
         } else if( !testLoadState(nativeLibJarsLS, LoadState.LOADED) ) {
             throw new IOException("TempJarCache: addNativeLibs: "+jarURL+", previous load attempt failed");
@@ -237,12 +238,12 @@ public class TempJarCache {
      */
     public synchronized static final void addClasses(Class<?> certClass, URL jarURL) throws IOException, SecurityException {
         final LoadState classFileJarsLS = classFileJars.get(jarURL);
-        if( !testLoadState(classFileJarsLS, LoadState.LOOKED_UP) ) {
-            classFileJars.put(jarURL, LoadState.LOOKED_UP);
+        if( !testLoadState(classFileJarsLS, LoadState.LOOKED_UP) || !testLoadState(classFileJarsLS, LoadState.LOADED) ) {
             final JarFile jarFile = JarUtil.getJarFile(jarURL);
             if(DEBUG) {
                 System.err.println("TempJarCache: addClasses: "+jarURL+": nativeJar "+jarFile.getName());
             }
+            classFileJars.put(jarURL, LoadState.LOOKED_UP);
             validateCertificates(certClass, jarFile);
             JarUtil.extract(tmpFileCache.getTempDir(), null, jarFile,
                             false, true, false);
@@ -263,12 +264,12 @@ public class TempJarCache {
      */
     public synchronized static final void addResources(Class<?> certClass, URL jarURL) throws IOException, SecurityException {        
         final LoadState resourceFileJarsLS = resourceFileJars.get(jarURL);
-        if( !testLoadState(resourceFileJarsLS, LoadState.LOOKED_UP) ) {
-            resourceFileJars.put(jarURL, LoadState.LOOKED_UP);
+        if( !testLoadState(resourceFileJarsLS, LoadState.LOOKED_UP) || !testLoadState(resourceFileJarsLS, LoadState.LOADED) ) {
             final JarFile jarFile = JarUtil.getJarFile(jarURL);
             if(DEBUG) {
                 System.err.println("TempJarCache: addResources: "+jarURL+": nativeJar "+jarFile.getName());
             }
+            resourceFileJars.put(jarURL, LoadState.LOOKED_UP);
             validateCertificates(certClass, jarFile);
             JarUtil.extract(tmpFileCache.getTempDir(), null, jarFile,
                             false, false, true);
@@ -298,14 +299,18 @@ public class TempJarCache {
         final LoadState nativeLibJarsLS = nativeLibJars.get(jarURL);
         final LoadState classFileJarsLS = classFileJars.get(jarURL);
         final LoadState resourceFileJarsLS = resourceFileJars.get(jarURL);
-        if( !testLoadState(nativeLibJarsLS, LoadState.LOOKED_UP) ||
-            !testLoadState(classFileJarsLS, LoadState.LOOKED_UP) ||
-            !testLoadState(resourceFileJarsLS, LoadState.LOOKED_UP) ) {
+        if( !testLoadState(nativeLibJarsLS, LoadState.LOOKED_UP) || !testLoadState(nativeLibJarsLS, LoadState.LOADED) ||
+            !testLoadState(classFileJarsLS, LoadState.LOOKED_UP) || !testLoadState(classFileJarsLS, LoadState.LOADED) ||
+            !testLoadState(resourceFileJarsLS, LoadState.LOOKED_UP) || !testLoadState(resourceFileJarsLS, LoadState.LOADED) ) {
             
             final boolean extractNativeLibraries = !testLoadState(nativeLibJarsLS, LoadState.LOADED);
             final boolean extractClassFiles = !testLoadState(classFileJarsLS, LoadState.LOADED);
             final boolean extractOtherFiles = !testLoadState(resourceFileJarsLS, LoadState.LOOKED_UP);
             
+            final JarFile jarFile = JarUtil.getJarFile(jarURL);
+            if(DEBUG) {
+                System.err.println("TempJarCache: addAll: "+jarURL+": nativeJar "+jarFile.getName());
+            }
             // mark looked-up (those who are not loaded)
             if(extractNativeLibraries) {
                 nativeLibJars.put(jarURL, LoadState.LOOKED_UP);
@@ -317,10 +322,6 @@ public class TempJarCache {
                 resourceFileJars.put(jarURL, LoadState.LOOKED_UP);
             }
             
-            final JarFile jarFile = JarUtil.getJarFile(jarURL);
-            if(DEBUG) {
-                System.err.println("TempJarCache: addAll: "+jarURL+": nativeJar "+jarFile.getName());
-            }
             validateCertificates(certClass, jarFile);
             JarUtil.extract(tmpFileCache.getTempDir(), nativeLibMap, jarFile,
                             extractNativeLibraries, extractClassFiles, extractOtherFiles);
@@ -342,6 +343,7 @@ public class TempJarCache {
         }
     }
     
+
     public synchronized static final String findLibrary(String libName) {
         checkInitialized();
         // try with mapped library basename first
@@ -408,26 +410,29 @@ public class TempJarCache {
         boolean ok = false;
         int countEntries = 0;
         final LoadState nativeLibJarsLS = nativeLibJars.get(jarURL);
-        if( !testLoadState(nativeLibJarsLS, LoadState.LOOKED_UP) && !nativeLibMap.containsKey(libBaseName) ) {
+        if( !testLoadState(nativeLibJarsLS, LoadState.LOOKED_UP) || !nativeLibMap.containsKey(libBaseName) ) {
             if(DEBUG) {
                 System.err.println("TempJarCache: bootstrapNativeLib(certClass: "+certClass+", libBaseName "+libBaseName+", jarURL "+jarURL+")");
             }
-            nativeLibJars.put(jarURL, LoadState.LOOKED_UP);
             final JarFile jarFile = JarUtil.getJarFile(jarURL);
             if(DEBUG) {
                 System.err.println("TempJarCache: bootstrapNativeLib: nativeJar "+jarFile.getName());
             }
+            //nativeLibJars.put(jarURL, LoadState.LOOKED_UP);
             validateCertificates(certClass, jarFile);
             final Enumeration<JarEntry> entries = jarFile.entries();
             while (entries.hasMoreElements()) {
                 final JarEntry entry = (JarEntry) entries.nextElement();
                 final String entryName = entry.getName();
-    
+                final File destFile = new File(tmpFileCache.getTempDir(), entryName);
+
                 if( entryName.indexOf('/') == -1 &&
                     entryName.indexOf(File.separatorChar) == -1 &&
-                    entryName.indexOf(libBaseName) >= 0 )
+                    entryName.indexOf(libBaseName) >= 0 &&
+                    NativeLibrary.isValidNativeLibraryName(entryName, false) != null &&
+                    (!PlatformPropsImpl.hasOsAndArch(destFile.getParent()) ||
+                      PlatformPropsImpl.hasLocalArch(destFile.getParent())))
                 {
-                    final File destFile = new File(tmpFileCache.getTempDir(), entryName);
                     final InputStream in = new BufferedInputStream(jarFile.getInputStream(entry));
                     final OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
                     int numBytes = 0;
@@ -442,7 +447,7 @@ public class TempJarCache {
                     } finally { in.close(); out.close(); }
                     if (numBytes>0) {
                         nativeLibMap.put(libBaseName, destFile.getAbsolutePath());
-                        nativeLibJars.put(jarURL, LoadState.LOADED);
+                        //nativeLibJars.put(jarURL, LoadState.LOADED);
                         ok = true;
                         countEntries++;
                     }
diff --git a/src/java/jogamp/common/os/PlatformPropsImpl.java b/src/java/jogamp/common/os/PlatformPropsImpl.java
index 111523d..fc3f4b9 100644
--- a/src/java/jogamp/common/os/PlatformPropsImpl.java
+++ b/src/java/jogamp/common/os/PlatformPropsImpl.java
@@ -210,7 +210,22 @@ public abstract class PlatformPropsImpl {
      * kick off static initialization of platform property information
      */
     public static void initSingleton() { }
-    
+
+    public static final boolean hasOsAndArch(String str) {
+        String arches[] = new String[] {"freebsd-i386", "freebsd-i586", "freebsd-amd64", "hpux-hppa", "linux-amd64", "linux-ia64", "linux-i586",
+                                        "linux-armv6", "android-armv6", "macosx-universal", "solaris-sparc", "solaris-sparcv9",
+                                        "solaris-amd64", "solaris-i586", "windows-amd64", "windows-i586", "linux-i386", "sunos-x86"};
+        for (int i = 0; i < arches.length; i++) {
+                if (str.indexOf(arches[i]) != -1)
+                    return true;
+        }
+        return false;
+    }
+
+    public static final boolean hasLocalArch(String str) {
+        return (str.indexOf(PlatformPropsImpl.os_and_arch) != -1);
+    }
+
     /**
      * Returns the GlueGen common name for the given OSType and CPUType
      * as implemented in the build system in 'gluegen-cpptasks-base.xml'.<br>