From 0da7eeff106c1a2bf9e730c504a09e38360f141e Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 13 Feb 2012 23:17:56 +0100 Subject: Enhance ExtensionAvailabilityCache ; Expose extension count in GLContext (and clean up) - GLContext - Expose isFunctionAvailable(), isExtensionAvailable(), getPlatformExtensionCount(), getGLExtensionCount() - sort methods a bit ExtensionAvailabilityCache: - Favor StringBuilder instead of StringBuffer (faster) - Resuse set's - Hold dedicated counts of extensions, platform and GL --- .../jogamp/opengl/ExtensionAvailabilityCache.java | 107 +++++++++++++-------- 1 file changed, 66 insertions(+), 41 deletions(-) (limited to 'src/jogl/classes/jogamp/opengl/ExtensionAvailabilityCache.java') diff --git a/src/jogl/classes/jogamp/opengl/ExtensionAvailabilityCache.java b/src/jogl/classes/jogamp/opengl/ExtensionAvailabilityCache.java index 41366c9a8..55dd0fe11 100644 --- a/src/jogl/classes/jogamp/opengl/ExtensionAvailabilityCache.java +++ b/src/jogl/classes/jogamp/opengl/ExtensionAvailabilityCache.java @@ -51,7 +51,6 @@ import java.util.*; */ final class ExtensionAvailabilityCache { protected static final boolean DEBUG = GLContextImpl.DEBUG; - private static final boolean DEBUG_AVAILABILITY = Debug.isPropertyDefined("jogl.debug.ExtensionAvailabilityCache", true); ExtensionAvailabilityCache(GLContextImpl context) { @@ -67,8 +66,12 @@ final class ExtensionAvailabilityCache { if(DEBUG) { System.out.println("ExtensionAvailabilityCache: Flush availability OpenGL "+context.getGLVersion()); } - availableExtensionCache.clear(); initialized = false; + glExtensions = null; + glExtensionCount = 0; + glXExtensions = null; + glXExtensionCount = 0; + availableExtensionCache.clear(); } /** @@ -83,8 +86,9 @@ final class ExtensionAvailabilityCache { return initialized && !availableExtensionCache.isEmpty() ; } - final int getSize() { - return initialized ? availableExtensionCache.size() : 0 ; + final int getTotalExtensionCount() { + initAvailableExtensions(); + return availableExtensionCache.size(); } final boolean isExtensionAvailable(String glExtensionName) { @@ -92,11 +96,21 @@ final class ExtensionAvailabilityCache { return availableExtensionCache.contains(mapGLExtensionName(glExtensionName)); } + final int getPlatformExtensionCount() { + initAvailableExtensions(); + return glXExtensionCount; + } + final String getPlatformExtensionsString() { initAvailableExtensions(); return glXExtensions; } + final int getGLExtensionCount() { + initAvailableExtensions(); + return glExtensionCount; + } + final String getGLExtensionsString() { initAvailableExtensions(); if(DEBUG) { @@ -133,62 +147,71 @@ final class ExtensionAvailabilityCache { ", use "+ ( useGetStringi ? "glGetStringi" : "glGetString" ) ); } - StringBuffer sb = new StringBuffer(); + HashSet glExtensionSet = new HashSet(gl.isGLES() ? 50 : 320); // far less gl extension expected on mobile if(useGetStringi) { GL2GL3 gl2gl3 = gl.getGL2GL3(); - int[] numExtensions = { 0 } ; - gl2gl3.glGetIntegerv(GL2GL3.GL_NUM_EXTENSIONS, numExtensions, 0); - for (int i = 0; i < numExtensions[0]; i++) { - sb.append(gl2gl3.glGetStringi(GL.GL_EXTENSIONS, i)); - if(i < numExtensions[0]) { - sb.append(" "); - } + final int count; + { + int[] val = { 0 } ; + gl2gl3.glGetIntegerv(GL2GL3.GL_NUM_EXTENSIONS, val, 0); + count = val[0]; } - if (DEBUG) { - System.err.println(getThreadName() + ":ExtensionAvailabilityCache: GL_EXTENSIONS: "+numExtensions[0]); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < count; i++) { + if(i > 0) { + sb.append(" "); + } + final String ext = gl2gl3.glGetStringi(GL.GL_EXTENSIONS, i); + glExtensionSet.add(ext); + sb.append(ext); } - if(0==numExtensions[0]) { + if(0==count || sb.length()==0) { // fall back .. useGetStringi=false; + } else { + glExtensions = sb.toString(); } } if(!useGetStringi) { - sb.append(gl.glGetString(GL.GL_EXTENSIONS)); + glExtensions = gl.glGetString(GL.GL_EXTENSIONS); + if(null != glExtensions) { + StringTokenizer tok = new StringTokenizer(glExtensions); + while (tok.hasMoreTokens()) { + glExtensionSet.add(tok.nextToken().trim()); + } + } + } + glExtensionCount = glExtensionSet.size(); + if (DEBUG) { + System.err.println(getThreadName() + ":ExtensionAvailabilityCache: GL_EXTENSIONS: "+glExtensionCount); } - glExtensions = sb.toString(); // Platform Extensions + HashSet glXExtensionSet = new HashSet(50); { - // unify platform extension .. might have duplicates - HashSet platformSet = new HashSet(50); + // unify platform extension .. might have duplicates StringTokenizer tok = new StringTokenizer(context.getPlatformExtensionsStringImpl().toString()); while (tok.hasMoreTokens()) { - platformSet.add(tok.nextToken().trim()); + glXExtensionSet.add(tok.nextToken().trim()); } - final StringBuffer sb2 = new StringBuffer(); - for(Iterator iter = platformSet.iterator(); iter.hasNext(); ) { - sb2.append(iter.next()).append(" "); + final StringBuilder sb = new StringBuilder(); + for(Iterator iter = glXExtensionSet.iterator(); iter.hasNext(); ) { + sb.append(iter.next()); + if(iter.hasNext()) { + sb.append(" "); + } } - glXExtensions = sb2.toString(); - } - sb.append(" "); - sb.append(glXExtensions); - - String allAvailableExtensions = sb.toString(); - if (DEBUG_AVAILABILITY) { - System.err.println(getThreadName() + ":ExtensionAvailabilityCache: GL vendor: " + gl.glGetString(GL.GL_VENDOR)); - } - StringTokenizer tok = new StringTokenizer(allAvailableExtensions); - while (tok.hasMoreTokens()) { - String availableExt = tok.nextToken().trim(); - availableExt = availableExt.intern(); - availableExtensionCache.add(availableExt); - if (DEBUG_AVAILABILITY) { - System.err.println(getThreadName() + ":ExtensionAvailabilityCache: Available: " + availableExt); - } + glXExtensions = sb.toString(); + glXExtensionCount = glXExtensionSet.size(); } + + availableExtensionCache.addAll(glExtensionSet); + availableExtensionCache.addAll(glXExtensionSet); + if (DEBUG) { - System.err.println(getThreadName() + ":ExtensionAvailabilityCache: ALL EXTENSIONS: "+availableExtensionCache.size()); + System.err.println(getThreadName() + ":ExtensionAvailabilityCache: GLX_EXTENSIONS: "+glXExtensionCount); + System.err.println(getThreadName() + ":ExtensionAvailabilityCache: GL vendor: " + gl.glGetString(GL.GL_VENDOR)); + System.err.println(getThreadName() + ":ExtensionAvailabilityCache: ALL EXTENSIONS: "+availableExtensionCache.size()); } if(!context.isGLES()) { @@ -227,7 +250,9 @@ final class ExtensionAvailabilityCache { private boolean initialized = false; private String glExtensions = null; + private int glExtensionCount = 0; private String glXExtensions = null; + private int glXExtensionCount = 0; private HashSet availableExtensionCache = new HashSet(50); private GLContextImpl context; -- cgit v1.2.3