Top Description Inners Fields Constructors Methods
jogamp.opengl

public Class GLDrawableHelper

extends Object
Class Inheritance
Imports
java.lang.ref.WeakReference, java.util.ArrayList, .List, .HashSet, com.jogamp.nativewindow.NativeSurface, .NativeWindowException, .ProxySurface, .UpstreamSurfaceHook, com.jogamp.opengl.GL, .GLAnimatorControl, .GLAutoDrawable, .GLContext, .GLDrawable, .GLDrawableFactory, .GLEventListener, .GLException, .GLFBODrawable, .GLRunnable, com.jogamp.common.ExceptionUtils, com.jogamp.common.util.InterruptedRuntimeException, .PropertyAccess

Encapsulates the implementation of most of the GLAutoDrawable's methods to be able to share it between GLAutoDrawable implementations like GLAutoDrawableBase, GLCanvas and GLJPanel.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface

Field Summary

Modifier and TypeField and Description
private GLAnimatorControl
private boolean
protected static final boolean
private static final boolean
private volatile int
exclusiveContextSwitch

-1 release, 0 nop, 1 claim

private volatile Thread
private volatile int
private ArrayList<GLRunnableTask>
private final Object
private final ArrayList<GLEventListener>
private final Object
private final HashSet<GLEventListener>
private static final int
private static Runnable
private static final boolean
PERF_STATS

true if property jogl.debug.GLDrawable.PerfStats is defined.

private static final ThreadLocal<WeakReference<Runnable>>
private GLAutoDrawable
private GLContext

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final void
public final void
addGLEventListener(int index, final GLEventListener listener)

public final boolean
public final void
display(final GLAutoDrawable drawable)

private final void
displayImpl(final GLAutoDrawable drawable)

public final int

Returns:

the disposal count
disposeAllGLEventListener
(final GLAutoDrawable autoDrawable, final boolean remove)

Disposes all added initialized GLEventListeners via GLEventListener#dispose(GLAutoDrawable).

public final void
disposeAllGLEventListener(final GLAutoDrawable autoDrawable, final GLDrawable drawable, final GLContext context, final boolean remove)

Principal helper method which runs disposeAllGLEventListener(GLAutoDrawable, boolean) with the context made current.

public final void
disposeGL(final GLAutoDrawable autoDrawable, final GLContext context, final boolean
destroy context in the end while holding the lock
destroyContext
)

Principal helper method which runs disposeAllGLEventListener(autoDrawable, false) with the context made current.

public final GLEventListener

Returns:

the disposed and/or removed listener, otherwise null if neither action is performed
disposeGLEventListener
(final GLAutoDrawable autoDrawable, final GLEventListener listener, final boolean
if true, the listener gets removed
remove
)

Disposes the given GLEventListener via GLEventListener#dispose(GLAutoDrawable) if it has been initialized and added to this queue.

public final GLEventListener
disposeGLEventListener(final GLAutoDrawable autoDrawable, final GLDrawable drawable, final GLContext context, final GLEventListener listener, final boolean remove)

Principal helper method which runs disposeGLEventListener(GLAutoDrawable, GLEventListener, boolean) with the context made current.

public final void
enqueue(final GLRunnable glRunnable)

private final boolean
public final void
public static final void
forceNativeRelease(final GLContext ctx)

Since GLContext's GLContext#makeCurrent() and GLContext#release() is recursive, a call to GLContext#release() may not natively release the context.

public final GLAnimatorControl
public final boolean
private final String
public final Thread
public final GLEventListener
getGLEventListener(int index)

public final int
public final boolean
private static final Runnable
protected static String
private final void
init(final GLEventListener l, final GLAutoDrawable drawable, final boolean sendReshape)

public final void
init(final GLAutoDrawable drawable, final boolean
set to true if the subsequent display call won't reshape, otherwise false to avoid double reshape.
sendReshape
)

The default init action to be called once after ctx is being created @ 1st makeCurrent().

public final boolean

Returns:

true if the GLRunnable has been processed or queued, otherwise false.
invoke
(final GLAutoDrawable
the GLAutoDrawable to be used
drawable
,
boolean
if true block until execution of glRunnable is finished, otherwise return immediatly w/o waiting
wait
,
final GLRunnable
the GLRunnable to execute within display()
glRunnable
)

If wait is true the call blocks until the glRunnable has been executed.

public final boolean
invoke(final GLAutoDrawable drawable, boolean wait, final List<GLRunnable> newGLRunnables)

public final void
invokeGL(final GLDrawable drawable, final GLContext context, final Runnable runnable, final Runnable initAction)

Principal helper method which runs a Runnable with the context made current.

private final void
invokeGLImpl(final GLDrawable drawable, final GLContext context, final Runnable runnable, final Runnable initAction)

private final void
invokeGLImplStats(final GLDrawable drawable, final GLContext context, final Runnable runnable, final Runnable initAction)

public final boolean
public final boolean
public final boolean
public final boolean
public static final boolean
public static final boolean
public boolean

Returns:

true if initialization is pending due to a set shared GLAutoDrawable or GLContext which is not ready yet. Otherwise false.
isSharedGLContextPending
(final GLContext[]
returns the shared GLContext, based on set shared GLAutoDrawable or GLContext. Maybe null if none is set.
shared
)

public static final GLDrawableImpl

Returns:

the new drawable
recreateGLDrawable
(GLDrawableImpl drawable, final GLContext
maybe null
context
)

If the drawable is not realized, OP is a NOP.

public final GLEventListener

Returns:

the removed listener, or null if listener was not added
removeGLEventListener
(final GLEventListener listener)

Note that no GLEventListener#dispose(GLAutoDrawable) call is being issued due to the lack of a current context.

public final GLEventListener
public final void
public final void
reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)

public static final GLDrawableImpl

Returns:

the new drawable in case of an pbuffer/pixmap drawable, otherwise the passed drawable is being returned.
resizeOffscreenDrawable
(GLDrawableImpl drawable, final GLContext context, int
the new width, it's minimum is capped to 1
newWidth
,
int
the new height, it's minimum is capped to 1
newHeight
)

Performs resize operation on the given drawable, assuming it is offscreen.

public final void
runForAllGLEventListener(final GLAutoDrawable drawable, final GLDrawableHelper.GLEventListenerAction action)

private static GLEventListenerAction displayAction = new GLEventListenerAction() { public void run(final GLAutoDrawable drawable, final GLEventListener listener) { listener.display(drawable); } };

public final void
runOutsideOfExclusiveContextThread(final GLContext context, final Runnable runnable)

Runs given runnable outside of a probable claimed exclusive thread, i.e. releases the exclusive thread, executes the runnable and reclaims it.

public final void
public final void
setAutoSwapBufferMode(final boolean enable)

public final Thread

Returns:

previous exclusive context thread
setExclusiveContextThread
(final Thread
the exclusive thread to claim the context, or null for default operation.
t
,
final GLContext context)

Dedicates this instance's GLContext to the given thread.

public final void
setGLEventListenerInitState(final GLEventListener listener, final boolean initialized)

private static final void
setLastInitAction(final Runnable initAction)

public final void
setSharedAutoDrawable(final GLAutoDrawable thisAutoDrawable, final GLAutoDrawable sharedAutoDrawable)

public final void
setSharedContext(final GLContext thisContext, final GLContext sharedContext)

private final void
setViewportAndClear(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)

Bug 1206: Security: Clear exposed framebuffer after creation and before visibility - Clear framebuffer after setting viewport - Since we only attempt to help against leaking un-initialized framebuffer content not against user-app faults, we do not clear a 2nd-buffer (double-buffering).

public static final void
switchContext(final GLDrawable
the drawable which context is changed
drawable
,
final GLContext
the old context, maybe null.
oldCtx
,
final boolean
if true, destroy the oldCtx
destroyOldCtx
,
final GLContext
the new context, maybe null for dis-association.
newCtx
,
final int
additional creation flags if newCtx is not null and not been created yet, see GLContext#setContextCreationFlags(int)
newCtxCreationFlags
)

Switch GLContext / GLDrawable association.

public final String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

animatorCtrlback to summary
private GLAnimatorControl animatorCtrl
autoSwapBufferModeback to summary
private boolean autoSwapBufferMode
DEBUGback to summary
protected static final boolean DEBUG
DEBUG_SETCLEARback to summary
private static final boolean DEBUG_SETCLEAR
exclusiveContextSwitchback to summary
private volatile int exclusiveContextSwitch

-1 release, 0 nop, 1 claim

exclusiveContextThreadback to summary
private volatile Thread exclusiveContextThread
glRunnableCountback to summary
private volatile int glRunnableCount
glRunnablesback to summary
private ArrayList<GLRunnableTask> glRunnables
glRunnablesLockback to summary
private final Object glRunnablesLock
listenersback to summary
private final ArrayList<GLEventListener> listeners
listenersLockback to summary
private final Object listenersLock
listenersToBeInitback to summary
private final HashSet<GLEventListener> listenersToBeInit
MAX_RELEASE_ITERback to summary
private static final int MAX_RELEASE_ITER

Limit release calls of forceNativeRelease(GLContext) to .

nopback to summary
private static Runnable nop
PERF_STATSback to summary
private static final boolean PERF_STATS

true if property jogl.debug.GLDrawable.PerfStats is defined.

perThreadInitActionback to summary
private static final ThreadLocal<WeakReference<Runnable>> perThreadInitAction
sharedAutoDrawableback to summary
private GLAutoDrawable sharedAutoDrawable
sharedContextback to summary
private GLContext sharedContext

Constructor Detail

GLDrawableHelperback to summary
public GLDrawableHelper()

Method Detail

addGLEventListenerback to summary
public final void addGLEventListener(final GLEventListener listener)
addGLEventListenerback to summary
public final void addGLEventListener(int index, final GLEventListener listener)
areAllGLEventListenerInitializedback to summary
public final boolean areAllGLEventListenerInitialized()
displayback to summary
public final void display(final GLAutoDrawable drawable)
displayImplback to summary
private final void displayImpl(final GLAutoDrawable drawable)
disposeAllGLEventListenerback to summary
public final int disposeAllGLEventListener(final GLAutoDrawable autoDrawable, final boolean remove) throws GLException

Disposes all added initialized GLEventListeners via GLEventListener#dispose(GLAutoDrawable).

If remove is true, the GLEventListeners are removed from this drawable queue before disposal, otherwise maked uninitialized.

Please consider using disposeAllGLEventListener(GLAutoDrawable, GLContext, boolean) or disposeGL(GLAutoDrawable, GLContext, boolean) for correctness, i.e. encapsulating all calls w/ makeCurrent etc.

Returns:int

the disposal count

Exceptions
GLException:
caused by GLEventListener#dispose(GLAutoDrawable)
disposeAllGLEventListenerback to summary
public final void disposeAllGLEventListener(final GLAutoDrawable autoDrawable, final GLDrawable drawable, final GLContext context, final boolean remove)

Principal helper method which runs disposeAllGLEventListener(GLAutoDrawable, boolean) with the context made current.

If an GLAnimatorControl is being attached and the current thread is different than the animator's thread, it is paused during the operation.

disposeGLback to summary
public final void disposeGL(final GLAutoDrawable autoDrawable, final GLContext context, final boolean destroyContext) throws GLException

Principal helper method which runs disposeAllGLEventListener(autoDrawable, false) with the context made current.

If destroyContext is true the context is destroyed in the end while holding the lock.

If destroyContext is false the context is natively released, i.e. released as often as locked before.

Parameters
destroyContext:boolean

destroy context in the end while holding the lock

Exceptions
GLException:
caused by GLEventListener#dispose(GLAutoDrawable) or context closing
disposeGLEventListenerback to summary
public final GLEventListener disposeGLEventListener(final GLAutoDrawable autoDrawable, final GLEventListener listener, final boolean remove)

Disposes the given GLEventListener via GLEventListener#dispose(GLAutoDrawable) if it has been initialized and added to this queue.

If remove is true, the GLEventListener is removed from this drawable queue before disposal, otherwise marked uninitialized.

Please consider using disposeGLEventListener(GLAutoDrawable, GLDrawable, GLContext, GLEventListener) for correctness, i.e. encapsulating all calls w/ makeCurrent etc.

Parameters
remove:boolean

if true, the listener gets removed

Returns:GLEventListener

the disposed and/or removed listener, otherwise null if neither action is performed

disposeGLEventListenerback to summary
public final GLEventListener disposeGLEventListener(final GLAutoDrawable autoDrawable, final GLDrawable drawable, final GLContext context, final GLEventListener listener, final boolean remove)

Principal helper method which runs disposeGLEventListener(GLAutoDrawable, GLEventListener, boolean) with the context made current.

If an GLAnimatorControl is being attached and the current thread is different than the animator's thread, it is paused during the operation.

enqueueback to summary
public final void enqueue(final GLRunnable glRunnable)
execGLRunnablesback to summary
private final boolean execGLRunnables(final GLAutoDrawable drawable)
flushGLRunnablesback to summary
public final void flushGLRunnables()
forceNativeReleaseback to summary
public static final void forceNativeRelease(final GLContext ctx)

Since GLContext's GLContext#makeCurrent() and GLContext#release() is recursive, a call to GLContext#release() may not natively release the context.

This methods continues calling GLContext#release() until the context has been natively released.

getAnimatorback to summary
public final GLAnimatorControl getAnimator()
getAutoSwapBufferModeback to summary
public final boolean getAutoSwapBufferMode()
getExclusiveContextSwitchStringback to summary
private final String getExclusiveContextSwitchString()
getExclusiveContextThreadback to summary
public final Thread getExclusiveContextThread()
See Also
setExclusiveContextThread(Thread, GLContext)
getGLEventListenerback to summary
public final GLEventListener getGLEventListener(int index) throws IndexOutOfBoundsException
getGLEventListenerCountback to summary
public final int getGLEventListenerCount()
getGLEventListenerInitStateback to summary
public final boolean getGLEventListenerInitState(final GLEventListener listener)
getLastInitActionback to summary
private static final Runnable getLastInitAction()
getThreadNameback to summary
protected static String getThreadName()
initback to summary
private final void init(final GLEventListener l, final GLAutoDrawable drawable, final boolean sendReshape)
initback to summary
public final void init(final GLAutoDrawable drawable, final boolean sendReshape)

The default init action to be called once after ctx is being created @ 1st makeCurrent().

Parameters
sendReshape:boolean

set to true if the subsequent display call won't reshape, otherwise false to avoid double reshape.

invokeback to summary
public final boolean invoke(final GLAutoDrawable drawable, boolean wait, final GLRunnable glRunnable) throws IllegalStateException

If wait is true the call blocks until the glRunnable has been executed.

If wait is true and GLDrawable#isRealized() returns false or GLAutoDrawable#getContext() returns null, the call is ignored and returns false.
This helps avoiding deadlocking the caller.

0 == deferredHere && 0 == isGLThread -> display() will issue on GL thread, blocking!

        deferredHere wait isGLThread   lockedByThisThread  Note
 OK     0            x    1            x
 OK     0            x    0            0
 ERROR  0            x    0            1                   Will be deferred on GL thread by display() (blocking),
                                                           but locked by this thread -> ERROR

        1            0    x            x                   All good, due to no wait, non blocking

        1            1    1            0
        1            1    0            0
 SWITCH 1            1    1            1                   Run immediately, don't defer since locked by this thread, but isGLThread
 ERROR  1            1    0            1                   Locked by this thread, but _not_ isGLThread -> ERROR

Parameters
drawable:GLAutoDrawable

the GLAutoDrawable to be used

wait:boolean

if true block until execution of glRunnable is finished, otherwise return immediatly w/o waiting

glRunnable:GLRunnable

the GLRunnable to execute within display()

Returns:boolean

true if the GLRunnable has been processed or queued, otherwise false.

Exceptions
IllegalStateException:
in case the drawable is locked by this thread, no animator is running on another thread and wait is true.
invokeback to summary
public final boolean invoke(final GLAutoDrawable drawable, boolean wait, final List<GLRunnable> newGLRunnables) throws IllegalStateException
See Also
invoke(GLAutoDrawable, boolean, GLRunnable)
invokeGLback to summary
public final void invokeGL(final GLDrawable drawable, final GLContext context, final Runnable runnable, final Runnable initAction)

Principal helper method which runs a Runnable with the context made current. This could have been made part of GLContext, but a desired goal is to be able to implement GLAutoDrawable's in terms of the GLContext's public APIs, and putting it into a separate class helps ensure that we don't inadvertently use private methods of the GLContext or its implementing classes.

Note

Locking of the surface is implicit done by GLContext#makeCurrent(), where unlocking is performed by the latter GLContext#release().

invokeGLImplback to summary
private final void invokeGLImpl(final GLDrawable drawable, final GLContext context, final Runnable runnable, final Runnable initAction)
invokeGLImplStatsback to summary
private final void invokeGLImplStats(final GLDrawable drawable, final GLContext context, final Runnable runnable, final Runnable initAction)
isAnimatorAnimatingback to summary
public final boolean isAnimatorAnimating()
isAnimatorAnimatingOnOtherThreadback to summary
public final boolean isAnimatorAnimatingOnOtherThread()
isAnimatorStartedback to summary
public final boolean isAnimatorStarted()
isAnimatorStartedOnOtherThreadback to summary
public final boolean isAnimatorStartedOnOtherThread()
isLockedByOtherThreadback to summary
public static final boolean isLockedByOtherThread(final GLAutoDrawable d)
isLockedByThisThreadback to summary
public static final boolean isLockedByThisThread(final GLAutoDrawable d)
isSharedGLContextPendingback to summary
public boolean isSharedGLContextPending(final GLContext[] shared)
Parameters
shared:GLContext[]

returns the shared GLContext, based on set shared GLAutoDrawable or GLContext. Maybe null if none is set.

Returns:boolean

true if initialization is pending due to a set shared GLAutoDrawable or GLContext which is not ready yet. Otherwise false.

recreateGLDrawableback to summary
public static final GLDrawableImpl recreateGLDrawable(GLDrawableImpl drawable, final GLContext context)

If the drawable is not realized, OP is a NOP.

  • release context if current
  • destroy old drawable
  • create new drawable
  • attach new drawable to context
  • make context current, if it was current

Locking is performed via GLContext#makeCurrent() on the passed context.

Parameters
context:GLContext

maybe null

Returns:GLDrawableImpl

the new drawable

removeGLEventListenerback to summary
public final GLEventListener removeGLEventListener(final GLEventListener listener)

Note that no GLEventListener#dispose(GLAutoDrawable) call is being issued due to the lack of a current context. Consider calling disposeGLEventListener(GLAutoDrawable, GLDrawable, GLContext, GLEventListener).

Returns:GLEventListener

the removed listener, or null if listener was not added

removeGLEventListenerback to summary
public final GLEventListener removeGLEventListener(int index) throws IndexOutOfBoundsException
resetback to summary
public final void reset()
reshapeback to summary
public final void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
resizeOffscreenDrawableback to summary
public static final GLDrawableImpl resizeOffscreenDrawable(GLDrawableImpl drawable, final GLContext context, int newWidth, int newHeight) throws NativeWindowException, GLException

Performs resize operation on the given drawable, assuming it is offscreen.

The GLDrawableImpl's NativeSurface is being locked during operation. In case the holder is an auto drawable or similar, it's lock shall be claimed by the caller.

May recreate the drawable via recreateGLDrawable(GLDrawableImpl, GLContext) in case of a a pbuffer- or pixmap-drawable.

FBO drawables are resized w/o drawable destruction.

Offscreen resize operation is validated w/ drawable size in the end. An exception is thrown if not successful.

Parameters
newWidth:int

the new width, it's minimum is capped to 1

newHeight:int

the new height, it's minimum is capped to 1

Returns:GLDrawableImpl

the new drawable in case of an pbuffer/pixmap drawable, otherwise the passed drawable is being returned.

Exceptions
NativeWindowException:
is drawable is not offscreen or it's surface lock couldn't be claimed
GLException:
may be thrown a resize operation
runForAllGLEventListenerback to summary
public final void runForAllGLEventListener(final GLAutoDrawable drawable, final GLDrawableHelper.GLEventListenerAction action)

private static GLEventListenerAction displayAction = new GLEventListenerAction() { public void run(final GLAutoDrawable drawable, final GLEventListener listener) { listener.display(drawable); } };

runOutsideOfExclusiveContextThreadback to summary
public final void runOutsideOfExclusiveContextThread(final GLContext context, final Runnable runnable)

Runs given runnable outside of a probable claimed exclusive thread, i.e. releases the exclusive thread, executes the runnable and reclaims it.

Since
2.3.2
See Also
setExclusiveContextThread(Thread, GLContext)
setAnimatorback to summary
public final void setAnimator(final GLAnimatorControl animator) throws GLException
setAutoSwapBufferModeback to summary
public final void setAutoSwapBufferMode(final boolean enable)
setExclusiveContextThreadback to summary
public final Thread setExclusiveContextThread(final Thread t, final GLContext context) throws GLException

Dedicates this instance's GLContext to the given thread.
The thread will exclusively claim the GLContext via display() and not release it until destroy() or setExclusiveContextThread(null) has been called.

Default non-exclusive behavior is requested via setExclusiveContextThread(null), which will cause the next call of display() on the exclusive thread to release the GLContext. Only after it's async release, getExclusiveContextThread() will return null.

To release a previous made exclusive thread, a user issues setExclusiveContextThread(null) and may poll getExclusiveContextThread() until it returns null, while the exclusive thread is still running.

Note

Setting a new exclusive thread without properly releasing a previous one will throw an GLException.

One scenario could be to dedicate the context to the animator thread and spare redundant context switches.

Parameters
t:Thread

the exclusive thread to claim the context, or null for default operation.

Returns:Thread

previous exclusive context thread

Exceptions
GLException:
If an exclusive thread is still active but a new one is attempted to be set
setGLEventListenerInitStateback to summary
public final void setGLEventListenerInitState(final GLEventListener listener, final boolean initialized)
setLastInitActionback to summary
private static final void setLastInitAction(final Runnable initAction)
setSharedAutoDrawableback to summary
public final void setSharedAutoDrawable(final GLAutoDrawable thisAutoDrawable, final GLAutoDrawable sharedAutoDrawable) throws IllegalStateException
setSharedContextback to summary
public final void setSharedContext(final GLContext thisContext, final GLContext sharedContext) throws IllegalStateException
setViewportAndClearback to summary
private final void setViewportAndClear(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)

Bug 1206: Security: Clear exposed framebuffer after creation and before visibility - Clear framebuffer after setting viewport - Since we only attempt to help against leaking un-initialized framebuffer content not against user-app faults, we do not clear a 2nd-buffer (double-buffering).

switchContextback to summary
public static final void switchContext(final GLDrawable drawable, final GLContext oldCtx, final boolean destroyOldCtx, final GLContext newCtx, final int newCtxCreationFlags)

Switch GLContext / GLDrawable association.

Remarks:

Parameters
drawable:GLDrawable

the drawable which context is changed

oldCtx:GLContext

the old context, maybe null.

destroyOldCtx:boolean

if true, destroy the oldCtx

newCtx:GLContext

the new context, maybe null for dis-association.

newCtxCreationFlags:int

additional creation flags if newCtx is not null and not been created yet, see GLContext#setContextCreationFlags(int)

See Also
GLAutoDrawable#setContext(GLContext, boolean)
toStringback to summary
public final String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
jogamp.opengl back to summary

public Interface GLDrawableHelper.GLEventListenerAction


Method Summary

Modifier and TypeMethod and Description
public void
run(final GLAutoDrawable drawable, final GLEventListener listener)

Method Detail

runback to summary
public void run(final GLAutoDrawable drawable, final GLEventListener listener)