Top Description Inners Fields Constructors Methods
org.lwjgl.opengl

public final Class GLContext

extends Object
Class Inheritance
Imports
org.lwjgl.LWJGLException, .LWJGLUtil, .MemoryUtil, .Sys, java.lang.reflect.Method, java.nio.ByteBuffer, java.security.AccessController, .PrivilegedAction, .PrivilegedExceptionAction, java.util.*

Manages GL contexts. Before any rendering is done by a LWJGL system, a call should be made to GLContext.useContext() with a context. This will ensure that GLContext has an accurate reflection of the current context's capabilities and function pointers.

This class is thread-safe in the sense that multiple threads can safely call all public methods. The class is also thread-aware in the sense that it tracks a per-thread current context (including capabilities and function pointers). That way, multiple threads can have multiple contexts current and render to them concurrently.

Author
elias_naur
Version
$Revision$ $Id$

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final Map<Object, ContextCapabilities>
capability_cache

The weak mapping from context Object instances to ContextCapabilities.

private static final ThreadLocal<ContextCapabilities>
current_capabilities

Maps threads to their current context's ContextCapabilities, if any

private static boolean
private static GLContext.CapabilitiesCacheEntry
fast_path_cache

The getCapabilities() method is a potential hot spot in any LWJGL application, since it is needed for context capability discovery (e.g. is OpenGL 2.0 supported?), and for the function pointers of gl functions.

private static int
gl_ref_count

Reference count of the native opengl implementation library

private static final ThreadLocal<GLContext.CapabilitiesCacheEntry>
thread_cache_entries

Simple lock-free cache of CapabilitesEntryCache to avoid allocating more than one cache entry per thread

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static ContextCapabilities

Returns:

The current capabilities instance.
getCapabilities
()

Get the current capabilities instance.

pack-priv static ContextCapabilities

Returns:

the capabilities instance
getCapabilities
(Object
the context object
context
)

Returns the capabilities instance associated with the specified context object.

private static ContextCapabilities
pack-priv static long

Returns:

the function pointer address
getFunctionAddress
(String[]
the function name aliases.
aliases
)

Helper method to get a pointer to a named function with aliases in the OpenGL library.

pack-priv static long
getFunctionAddress(String name)

Helper method to get a pointer to a named function in the OpenGL library.

pack-priv static long
getPlatformSpecificFunctionAddress(String function_prefix, String[] os_prefixes, String[] os_function_prefixes, String function)

Helper method to get a pointer to a named function in the OpenGL library with a name dependent on the current platform

pack-priv static int

Returns:

the context profile mask, will be 0 for any version < 3.2
getSupportedExtensions
(final Set<String>
the Set to fill with the available extension names
supported_extensions
)

Determine which extensions are available and returns the context profile mask.

private static ContextCapabilities
pack-priv static void
initNativeStubs(final Class<?> extension_class, Set<E> supported_extensions, String ext_name)

Helper method to ContextCapabilities.

public static synchronized void
loadOpenGLLibrary()

If the OpenGL reference count is 0, the library is loaded.

private static native long
private static native void
private static native void
pack-priv static native void
resetNativeStubs(Class<T> clazz)

Native method to clear native stub bindings

pack-priv static void

Returns:

The current capabilities instance.
setCapabilities
(ContextCapabilities capabilities)

Set the current capabilities instance.

public static synchronized void
unloadOpenGLLibrary()

The OpenGL library reference count is decremented, and if it reaches 0, the library is unloaded.

public static synchronized void
useContext(Object
The context object, which uniquely identifies a GL context. If context is null, the native stubs are unloaded.
context
)

Makes a GL context the current LWJGL context by loading GL function pointers.

public static synchronized void
useContext(Object
The context object, which uniquely identifies a GL context. If context is null, the native stubs are unloaded.
context
,
boolean
If the context is a forward compatible context (does not expose deprecated functionality, see XGL_ARB_create_context)
forwardCompatible
)

Makes a GL context the current LWJGL context by loading GL function pointers.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

capability_cacheback to summary
private static final Map<Object, ContextCapabilities> capability_cache

The weak mapping from context Object instances to ContextCapabilities. Used to avoid recreating a ContextCapabilities every time a context is made current.

current_capabilitiesback to summary
private static final ThreadLocal<ContextCapabilities> current_capabilities

Maps threads to their current context's ContextCapabilities, if any

did_auto_loadback to summary
private static boolean did_auto_load
fast_path_cacheback to summary
private static GLContext.CapabilitiesCacheEntry fast_path_cache

The getCapabilities() method is a potential hot spot in any LWJGL application, since it is needed for context capability discovery (e.g. is OpenGL 2.0 supported?), and for the function pointers of gl functions. However, the 'current_capabilities' ThreadLocal is (relatively) expensive to look up, and since most OpenGL applications use are single threaded rendering, the following two is an optimization for this case.

ThreadLocals can be thought of as a mapping between threads and values, so the idea is to use a lock-less cache of mappings between threads and the current ContextCapabilities. The cache could be any size, but in our case, we want a single sized cache for optimal performance in the single threaded case.

'fast_path_cache' is the most recent ContextCapabilities (potentially null) and its owner. By recent I mean the last thread setting the value in setCapabilities(). When getCapabilities() is called, a check to see if the current is the owner of the ContextCapabilities instance in fast_path_cache. If so, the instance is returned, if not, some thread has since taken ownership of the cache entry and the slower current_capabilities ThreadLocal is queried instead.

No locks are needed in get/setCapabilities, because even though fast_path_cache can be accessed from multiple threads at once, we are guaranteed by the JVM spec that its value is always valid. Furthermore, if the ownership test in getCapabilities() succeeds, the cache entry can only contain the correct ContextCapabilites (that is, the one from getThreadLocalCapabilites()), since no other thread can set the owner to anyone else than itself.

gl_ref_countback to summary
private static int gl_ref_count

Reference count of the native opengl implementation library

thread_cache_entriesback to summary
private static final ThreadLocal<GLContext.CapabilitiesCacheEntry> thread_cache_entries

Simple lock-free cache of CapabilitesEntryCache to avoid allocating more than one cache entry per thread

Constructor Detail

GLContextback to summary
public GLContext()

Method Detail

getCapabilitiesback to summary
public static ContextCapabilities getCapabilities()

Get the current capabilities instance. It contains the flags used to test for support of a particular extension.

Returns:ContextCapabilities

The current capabilities instance.

getCapabilitiesback to summary
pack-priv static ContextCapabilities getCapabilities(Object context)

Returns the capabilities instance associated with the specified context object.

Parameters
context:Object

the context object

Returns:ContextCapabilities

the capabilities instance

getCapabilitiesImplback to summary
private static ContextCapabilities getCapabilitiesImpl()
getFunctionAddressback to summary
pack-priv static long getFunctionAddress(String[] aliases)

Helper method to get a pointer to a named function with aliases in the OpenGL library.

Parameters
aliases:String[]

the function name aliases.

Returns:long

the function pointer address

getFunctionAddressback to summary
pack-priv static long getFunctionAddress(String name)

Helper method to get a pointer to a named function in the OpenGL library.

getPlatformSpecificFunctionAddressback to summary
pack-priv static long getPlatformSpecificFunctionAddress(String function_prefix, String[] os_prefixes, String[] os_function_prefixes, String function)

Helper method to get a pointer to a named function in the OpenGL library with a name dependent on the current platform

getSupportedExtensionsback to summary
pack-priv static int getSupportedExtensions(final Set<String> supported_extensions)

Determine which extensions are available and returns the context profile mask. Helper method to ContextCapabilities.

Parameters
supported_extensions:Set<String>

the Set to fill with the available extension names

Returns:int

the context profile mask, will be 0 for any version < 3.2

getThreadLocalCapabilitiesback to summary
private static ContextCapabilities getThreadLocalCapabilities()
initNativeStubsback to summary
pack-priv static void initNativeStubs(final Class<?> extension_class, Set<E> supported_extensions, String ext_name)

Helper method to ContextCapabilities. It will try to initialize the native stubs, and remove the given extension name from the extension set if the initialization fails.

loadOpenGLLibraryback to summary
public static synchronized void loadOpenGLLibrary() throws LWJGLException

If the OpenGL reference count is 0, the library is loaded. The reference count is then incremented.

ngetFunctionAddressback to summary
private static native long ngetFunctionAddress(long name)
nLoadOpenGLLibraryback to summary
private static native void nLoadOpenGLLibrary() throws LWJGLException
nUnloadOpenGLLibraryback to summary
private static native void nUnloadOpenGLLibrary()
resetNativeStubsback to summary
pack-priv static native void resetNativeStubs(Class<T> clazz)

Native method to clear native stub bindings

setCapabilitiesback to summary
pack-priv static void setCapabilities(ContextCapabilities capabilities)

Set the current capabilities instance. It contains the flags used to test for support of a particular extension.

Returns:void

The current capabilities instance.

unloadOpenGLLibraryback to summary
public static synchronized void unloadOpenGLLibrary()

The OpenGL library reference count is decremented, and if it reaches 0, the library is unloaded.

useContextback to summary
public static synchronized void useContext(Object context) throws LWJGLException

Makes a GL context the current LWJGL context by loading GL function pointers. The context must be current before a call to this method! Instead it simply ensures that the current context is reflected accurately by GLContext's extension caps and function pointers. Use useContext(null) when no context is active.

If the context is the same as last time, then this is a no-op.

If the context has not been encountered before it will be fully initialized from scratch. Otherwise a cached set of caps and function pointers will be used.

The reference to the context is held in a weak reference; therefore if no strong reference exists to the GL context it will automatically be forgotten by the VM at an indeterminate point in the future, freeing up a little RAM.

Parameters
context:Object

The context object, which uniquely identifies a GL context. If context is null, the native stubs are unloaded.

Exceptions
LWJGLException:
if context non-null, and the gl library can't be loaded or the basic GL11 functions can't be loaded
useContextback to summary
public static synchronized void useContext(Object context, boolean forwardCompatible) throws LWJGLException

Makes a GL context the current LWJGL context by loading GL function pointers. The context must be current before a call to this method! Instead it simply ensures that the current context is reflected accurately by GLContext's extension caps and function pointers. Use useContext(null) when no context is active.

If the context is the same as last time, then this is a no-op.

If the context has not been encountered before it will be fully initialized from scratch. Otherwise a cached set of caps and function pointers will be used.

The reference to the context is held in a weak reference; therefore if no strong reference exists to the GL context it will automatically be forgotten by the VM at an indeterminate point in the future, freeing up a little RAM.

If forwardCompatible is true, function pointers of deprecated GL11-GL21 functionality will not be loaded. Calling a deprecated function using the specified context will result in an IllegalStateException.

Parameters
context:Object

The context object, which uniquely identifies a GL context. If context is null, the native stubs are unloaded.

forwardCompatible:boolean

If the context is a forward compatible context (does not expose deprecated functionality, see XGL_ARB_create_context)

Exceptions
LWJGLException:
if context non-null, and the gl library can't be loaded or the basic GL11 functions can't be loaded
org.lwjgl.opengl back to summary

private final Class GLContext.CapabilitiesCacheEntry

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv ContextCapabilities
pack-priv Thread

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

capabilitiesback to summary
pack-priv ContextCapabilities capabilities
ownerback to summary
pack-priv Thread owner

Constructor Detail

CapabilitiesCacheEntryback to summary
private CapabilitiesCacheEntry()