Index: ../../Applications/JOGLGIT/jogl/src/jogl/classes/javax/media/opengl/awt/GLCanvas.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP <+>/*\n * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.\n * Copyright (c) 2010 JogAmp Community. All rights reserved.\n * \n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are\n * met:\n * \n * - Redistribution of source code must retain the above copyright\n * notice, this list of conditions and the following disclaimer.\n * \n * - Redistribution in binary form must reproduce the above copyright\n * notice, this list of conditions and the following disclaimer in the\n * documentation and/or other materials provided with the distribution.\n * \n * Neither the name of Sun Microsystems, Inc. or the names of\n * contributors may be used to endorse or promote products derived from\n * this software without specific prior written permission.\n * \n * This software is provided \"AS IS,\" without a warranty of any kind. ALL\n * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,\n * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A\n * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN\n * MICROSYSTEMS, INC. (\"SUN\") AND ITS LICENSORS SHALL NOT BE LIABLE FOR\n * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR\n * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR\n * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR\n * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE\n * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,\n * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF\n * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\n * \n * You acknowledge that this software is not designed or intended for use\n * in the design, construction, operation or maintenance of any nuclear\n * facility.\n * \n * Sun gratefully acknowledges that this software was originally authored\n * and developed by Kenneth Bradley Russell and Christopher John Kline.\n */\n\npackage javax.media.opengl.awt;\n\nimport java.beans.Beans;\nimport java.lang.reflect.Method;\nimport java.security.AccessController;\nimport java.security.PrivilegedAction;\n\nimport java.awt.Canvas;\nimport java.awt.Color;\nimport java.awt.FontMetrics;\nimport java.awt.Frame;\nimport java.awt.Graphics;\nimport java.awt.GraphicsConfiguration;\nimport java.awt.GraphicsDevice;\nimport java.awt.geom.Rectangle2D;\n\nimport java.awt.EventQueue;\nimport java.lang.reflect.InvocationTargetException;\nimport java.util.ArrayList;\nimport java.util.List;\n\nimport javax.media.nativewindow.OffscreenLayerOption;\nimport javax.media.nativewindow.WindowClosingProtocol;\nimport javax.media.nativewindow.AbstractGraphicsDevice;\nimport javax.media.nativewindow.AbstractGraphicsScreen;\nimport javax.media.nativewindow.GraphicsConfigurationFactory;\nimport javax.media.nativewindow.NativeSurface;\nimport javax.media.nativewindow.NativeWindowFactory;\nimport javax.media.nativewindow.awt.AWTGraphicsConfiguration;\nimport javax.media.nativewindow.awt.AWTGraphicsDevice;\nimport javax.media.nativewindow.awt.AWTGraphicsScreen;\nimport javax.media.nativewindow.awt.AWTWindowClosingProtocol;\n\nimport javax.media.opengl.GL;\nimport javax.media.opengl.GLAnimatorControl;\nimport javax.media.opengl.GLAutoDrawable;\nimport javax.media.opengl.GLCapabilities;\nimport javax.media.opengl.GLCapabilitiesChooser;\nimport javax.media.opengl.GLCapabilitiesImmutable;\nimport javax.media.opengl.GLContext;\nimport javax.media.opengl.GLDrawable;\nimport javax.media.opengl.GLDrawableFactory;\nimport javax.media.opengl.GLEventListener;\nimport javax.media.opengl.GLException;\nimport javax.media.opengl.GLProfile;\nimport javax.media.opengl.GLRunnable;\nimport javax.media.opengl.Threading;\n\nimport com.jogamp.common.GlueGenVersion;\nimport com.jogamp.common.util.VersionUtil;\nimport com.jogamp.opengl.JoglVersion;\n\nimport com.jogamp.common.util.locks.LockFactory;\nimport com.jogamp.common.util.locks.RecursiveLock;\n\nimport jogamp.nativewindow.jawt.JAWTWindow;\nimport jogamp.opengl.Debug;\nimport jogamp.opengl.GLContextImpl;\nimport jogamp.opengl.GLDrawableHelper;\nimport jogamp.opengl.ThreadingImpl;\n\n// FIXME: Subclasses need to call resetGLFunctionAvailability() on their\n// context whenever the displayChanged() function is called on our\n// GLEventListeners\n\n/** A heavyweight AWT component which provides OpenGL rendering\n support. This is the primary implementation of an AWT {@link GLDrawable};\n {@link GLJPanel} is provided for compatibility with Swing user\n interfaces when adding a heavyweight doesn't work either because\n of Z-ordering or LayoutManager problems.\n *\n *
Java2D OpenGL Remarks
\n *\n * To avoid any conflicts with a potential Java2D OpenGL context,
\n * you shall consider setting the following JVM properties:
\n * \n * This is especially true in case you want to utilize a GLProfile other than\n * {@link GLProfile#GL2}, eg. using {@link GLProfile#getMaxFixedFunc()}.
\n * On the other hand, if you like to experiment with GLJPanel's utilization\n * of Java2D's OpenGL pipeline, you have to set them to\n * \n *\n *
Disable Background Erase
\n *\n * GLCanvas tries to disable background erase for the AWT Canvas\n * before native peer creation (X11) and after it (Windows),
\n * utilizing the optional {@link java.awt.Toolkit} method disableBeackgroundErase(java.awt.Canvas).
\n * However if this does not give you the desired results, you may want to disable AWT background erase in general:\n * \n */\n\n@SuppressWarnings(\"serial\")\npublic class GLCanvas extends Canvas implements AWTGLAutoDrawable, WindowClosingProtocol, OffscreenLayerOption {\n\n private static final boolean DEBUG;\n\n static {\n DEBUG = Debug.debug(\"GLCanvas\");\n }\n\n private GLDrawableHelper drawableHelper = new GLDrawableHelper();\n private AWTGraphicsConfiguration awtConfig;\n private GLDrawable drawable;\n private GLContextImpl context;\n private boolean sendReshape = false;\n \n // copy of the cstr args, mainly for recreation\n private GLCapabilitiesImmutable capsReqUser;\n private GLCapabilitiesChooser chooser;\n private GLContext shareWith;\n private int additionalCtxCreationFlags = 0; \n private GraphicsDevice device;\n private boolean shallUseOffscreenLayer = false;\n\n private AWTWindowClosingProtocol awtWindowClosingProtocol =\n new AWTWindowClosingProtocol(this, new Runnable() {\n public void run() {\n GLCanvas.this.destroy();\n }\n });\n\n /** Creates a new GLCanvas component with a default set of OpenGL\n capabilities, using the default OpenGL capabilities selection\n mechanism, on the default screen device. \n * @throws GLException if no default profile is available for the default desktop device.\n */\n public GLCanvas() throws GLException {\n this(null);\n }\n\n /** Creates a new GLCanvas component with the requested set of\n OpenGL capabilities, using the default OpenGL capabilities\n selection mechanism, on the default screen device. \n * @throws GLException if no GLCapabilities are given and no default profile is available for the default desktop device.\n * @see GLCanvas#GLCanvas(javax.media.opengl.GLCapabilitiesImmutable, javax.media.opengl.GLCapabilitiesChooser, javax.media.opengl.GLContext, java.awt.GraphicsDevice)\n */\n public GLCanvas(GLCapabilitiesImmutable capsReqUser) throws GLException {\n this(capsReqUser, null, null, null);\n }\n\n /** Creates a new GLCanvas component with the requested set of\n OpenGL capabilities, using the default OpenGL capabilities\n selection mechanism, on the default screen device.\n * This constructor variant also supports using a shared GLContext.\n *\n * @throws GLException if no GLCapabilities are given and no default profile is available for the default desktop device.\n * @see GLCanvas#GLCanvas(javax.media.opengl.GLCapabilitiesImmutable, javax.media.opengl.GLCapabilitiesChooser, javax.media.opengl.GLContext, java.awt.GraphicsDevice)\n */\n public GLCanvas(GLCapabilitiesImmutable capsReqUser, GLContext shareWith) \n throws GLException \n {\n this(capsReqUser, null, shareWith, null);\n }\n\n /** Creates a new GLCanvas component. The passed GLCapabilities\n specifies the OpenGL capabilities for the component; if null, a\n default set of capabilities is used. The GLCapabilitiesChooser\n specifies the algorithm for selecting one of the available\n GLCapabilities for the component; a DefaultGLCapabilitesChooser\n is used if null is passed for this argument. The passed\n GLContext specifies an OpenGL context with which to share\n textures, display lists and other OpenGL state, and may be null\n if sharing is not desired. See the note in the overview\n documentation on context\n sharing. The passed GraphicsDevice indicates the screen on\n which to create the GLCanvas; the GLDrawableFactory uses the\n default screen device of the local GraphicsEnvironment if null\n is passed for this argument. \n * @throws GLException if no GLCapabilities are given and no default profile is available for the default desktop device.\n */\n public GLCanvas(GLCapabilitiesImmutable capsReqUser,\n GLCapabilitiesChooser chooser,\n GLContext shareWith,\n GraphicsDevice device) \n throws GLException \n {\n /*\n * Determination of the native window is made in 'super.addNotify()',\n * which creates the native peer using AWT's GraphicsConfiguration.\n * GraphicsConfiguration is returned by this class overwritten\n * 'getGraphicsConfiguration()', which returns our OpenGL compatible\n * 'chosen' GraphicsConfiguration.\n */\n super();\n\n if(null==capsReqUser) {\n capsReqUser = new GLCapabilities(GLProfile.getDefault(GLProfile.getDefaultDesktopDevice()));\n } else {\n // don't allow the user to change data\n capsReqUser = (GLCapabilitiesImmutable) capsReqUser.cloneMutable();\n }\n\n if(null==device) {\n GraphicsConfiguration gc = super.getGraphicsConfiguration();\n if(null!=gc) {\n device = gc.getDevice();\n }\n }\n\n // instantiation will be issued in addNotify()\n this.capsReqUser = capsReqUser;\n this.chooser = chooser;\n this.shareWith = shareWith;\n this.device = device;\n }\n\n public void setShallUseOffscreenLayer(boolean v) {\n shallUseOffscreenLayer = v;\n }\n\n public final boolean getShallUseOffscreenLayer() {\n return shallUseOffscreenLayer; \n }\n\n public final boolean isOffscreenLayerSurfaceEnabled() {\n if(null != drawable) {\n return ((JAWTWindow)drawable.getNativeSurface()).isOffscreenLayerSurfaceEnabled();\n }\n return false;\n }\n\n \n /**\n * Overridden to choose a GraphicsConfiguration on a parent container's\n * GraphicsDevice because both devices\n */\n @Override\n public GraphicsConfiguration getGraphicsConfiguration() {\n /*\n * Workaround for problems with Xinerama and java.awt.Component.checkGD\n * when adding to a container on a different graphics device than the\n * one that this Canvas is associated with.\n * \n * GC will be null unless:\n * - A native peer has assigned it. This means we have a native\n * peer, and are already comitted to a graphics configuration.\n * - This canvas has been added to a component hierarchy and has\n * an ancestor with a non-null GC, but the native peer has not\n * yet been created. This means we can still choose the GC on\n * all platforms since the peer hasn't been created.\n */\n final GraphicsConfiguration gc = super.getGraphicsConfiguration();\n /*\n * chosen is only non-null on platforms where the GLDrawableFactory\n * returns a non-null GraphicsConfiguration (in the GLCanvas\n * constructor).\n * \n * if gc is from this Canvas' native peer then it should equal chosen,\n * otherwise it is from an ancestor component that this Canvas is being\n * added to, and we go into this block.\n */\n GraphicsConfiguration chosen = awtConfig.getAWTGraphicsConfiguration();\n\n if (gc != null && chosen != null && !chosen.equals(gc)) {\n /*\n * Check for compatibility with gc. If they differ by only the\n * device then return a new GCconfig with the super-class' GDevice\n * (and presumably the same visual ID in Xinerama).\n * \n */\n if (!chosen.getDevice().getIDstring().equals(gc.getDevice().getIDstring())) {\n /*\n * Here we select a GraphicsConfiguration on the alternate\n * device that is presumably identical to the chosen\n * configuration, but on the other device.\n * \n * Should really check to ensure that we select a configuration\n * with the same X visual ID for Xinerama screens, otherwise the\n * GLDrawable may have the wrong visual ID (I don't think this\n * ever gets updated). May need to add a method to\n * X11GLDrawableFactory to do this in a platform specific\n * manner.\n * \n * However, on platforms where we can actually get into this\n * block, both devices should have the same visual list, and the\n * same configuration should be selected here.\n */\n AWTGraphicsConfiguration config = chooseGraphicsConfiguration( (GLCapabilitiesImmutable)awtConfig.getChosenCapabilities(),\n (GLCapabilitiesImmutable)awtConfig.getRequestedCapabilities(),\n chooser, gc.getDevice());\n final GraphicsConfiguration compatible = (null!=config)?config.getAWTGraphicsConfiguration():null;\n boolean equalCaps = config.getChosenCapabilities().equals(awtConfig.getChosenCapabilities());\n if(DEBUG) {\n Exception e = new Exception(\"Info: Call Stack: \"+Thread.currentThread().getName());\n e.printStackTrace();\n System.err.println(\"!!! Created Config (n): HAVE GC \"+chosen);\n System.err.println(\"!!! Created Config (n): THIS GC \"+gc);\n System.err.println(\"!!! Created Config (n): Choosen GC \"+compatible);\n System.err.println(\"!!! Created Config (n): HAVE CF \"+awtConfig);\n System.err.println(\"!!! Created Config (n): Choosen CF \"+config);\n System.err.println(\"!!! Created Config (n): EQUALS CAPS \"+equalCaps);\n }\n\n if (compatible != null) {\n /*\n * Save the new GC for equals test above, and to return to\n * any outside callers of this method.\n */\n chosen = compatible;\n\n awtConfig = config;\n\n if( !equalCaps && GLAutoDrawable.SCREEN_CHANGE_ACTION_ENABLED ) {\n dispose(true);\n }\n }\n }\n\n /*\n * If a compatible GC was not found in the block above, this will\n * return the GC that was selected in the constructor (and might\n * cause an exception in Component.checkGD when adding to a\n * container, but in this case that would be the desired behavior).\n * \n */\n return chosen;\n } else if (gc == null) {\n /*\n * The GC is null, which means we have no native peer, and are not\n * part of a (realized) component hierarchy. So we return the\n * desired visual that was selected in the constructor (possibly\n * null).\n */\n return chosen;\n }\n\n /*\n * Otherwise we have not explicitly selected a GC in the constructor, so\n * just return what Canvas would have.\n */\n return gc;\n }\n \n public GLContext createContext(GLContext shareWith) {\n drawableSync.lock();\n try {\n return (null != drawable) ? drawable.createContext(shareWith) : null;\n } finally {\n drawableSync.unlock();\n }\n }\n\n public void setRealized(boolean realized) {\n }\n\n public boolean isRealized() {\n drawableSync.lock();\n try {\n return ( null != drawable ) ? drawable.isRealized() : false;\n } finally {\n drawableSync.unlock();\n }\n }\n\n public int getDefaultCloseOperation() {\n return awtWindowClosingProtocol.getDefaultCloseOperation();\n }\n\n public int setDefaultCloseOperation(int op) {\n return awtWindowClosingProtocol.setDefaultCloseOperation(op);\n }\n\n public void display() {\n if( !validateGLDrawable() ) {\n if(DEBUG) {\n System.err.println(\"Info: GLCanvas display - skipped GL render, drawable not valid yet\");\n }\n return; // not yet available ..\n }\n maybeDoSingleThreadedWorkaround(displayOnEventDispatchThreadAction,\n displayAction);\n\n awtWindowClosingProtocol.addClosingListenerOneShot();\n }\n\n private void dispose(boolean regenerate) {\n drawableSync.lock();\n try {\n final GLAnimatorControl animator = getAnimator();\n if(DEBUG) {\n Exception ex1 = new Exception(\"Info: dispose(\"+regenerate+\") - START, hasContext \" +\n (null!=context) + \", hasDrawable \" + (null!=drawable)+\", \"+animator);\n ex1.printStackTrace();\n }\n\n if(null!=context) {\n boolean animatorPaused = false;\n if(null!=animator) {\n // can't remove us from animator for recreational addNotify()\n animatorPaused = animator.pause();\n }\n\n disposeRegenerate=regenerate;\n\n if (Threading.isSingleThreaded() &&\n !Threading.isOpenGLThread()) {\n // Workaround for termination issues with applets --\n // sun.applet.AppletPanel should probably be performing the\n // remove() call on the EDT rather than on its own thread\n // Hint: User should run remove from EDT.\n if (ThreadingImpl.isAWTMode() &&\n Thread.holdsLock(getTreeLock())) {\n // The user really should not be invoking remove() from this\n // thread -- but since he/she is, we can not go over to the\n // EDT at this point. Try to destroy the context from here.\n if(context.isCreated()) {\n drawableHelper.invokeGL(drawable, context, disposeAction, null);\n }\n } else if(context.isCreated()) {\n Threading.invokeOnOpenGLThread(disposeOnEventDispatchThreadAction);\n }\n } else if(context.isCreated()) {\n drawableHelper.invokeGL(drawable, context, disposeAction, null);\n }\n\n if(animatorPaused) {\n animator.resume();\n }\n }\n if(!regenerate) {\n disposeAbstractGraphicsDevice();\n }\n\n if(DEBUG) {\n System.err.println(\"dispose(\"+regenerate+\") - END, \"+animator);\n }\n } finally {\n drawableSync.unlock();\n }\n }\n\n /**\n * Just an alias for removeNotify\n */\n public void destroy() {\n removeNotify();\n }\n\n /** Overridden to cause OpenGL rendering to be performed during\n repaint cycles. Subclasses which override this method must call\n super.paint() in their paint() method in order to function\n properly.

\n\n Overrides:\n

paint in class java.awt.Component
*/\n @Override\n public void paint(Graphics g) {\n if (Beans.isDesignTime()) {\n // Make GLCanvas behave better in NetBeans GUI builder\n g.setColor(Color.BLACK);\n g.fillRect(0, 0, getWidth(), getHeight());\n FontMetrics fm = g.getFontMetrics();\n String name = getName();\n if (name == null) {\n name = getClass().getName();\n int idx = name.lastIndexOf('.');\n if (idx >= 0) {\n name = name.substring(idx + 1);\n }\n }\n Rectangle2D bounds = fm.getStringBounds(name, g);\n g.setColor(Color.WHITE);\n g.drawString(name,\n (int) ((getWidth() - bounds.getWidth()) / 2),\n (int) ((getHeight() + bounds.getHeight()) / 2));\n return;\n }\n if( ! this.drawableHelper.isAnimatorAnimating() ) {\n display();\n }\n }\n\n RecursiveLock drawableSync = LockFactory.createRecursiveLock();\n\n /** Overridden to track when this component is added to a container.\n Subclasses which override this method must call\n super.addNotify() in their addNotify() method in order to\n function properly.

\n\n Overrides:\n

addNotify in class java.awt.Component
*/\n @SuppressWarnings(\"deprecation\")\n @Override\n public void addNotify() {\n if(DEBUG) {\n Exception ex1 = new Exception(Thread.currentThread().getName()+\" - Info: addNotify - start, bounds: \"+this.getBounds());\n ex1.printStackTrace();\n }\n\n drawableSync.lock();\n try {\n /**\n * 'super.addNotify()' determines the GraphicsConfiguration,\n * while calling this class's overriden 'getGraphicsConfiguration()' method\n * after which it creates the native peer.\n * Hence we have to set the 'awtConfig' before since it's GraphicsConfiguration\n * is being used in getGraphicsConfiguration().\n * This code order also allows recreation, ie re-adding the GLCanvas.\n */\n awtConfig = chooseGraphicsConfiguration(capsReqUser, capsReqUser, chooser, device);\n if(null==awtConfig) {\n throw new GLException(\"Error: NULL AWTGraphicsConfiguration\");\n }\n\n // before native peer is valid: X11\n disableBackgroundErase();\n\n // issues getGraphicsConfiguration() and creates the native peer\n super.addNotify();\n\n // after native peer is valid: Windows\n disableBackgroundErase();\n\n if (!Beans.isDesignTime()) {\n createDrawableAndContext();\n }\n\n // init drawable by paint/display makes the init sequence more equal\n // for all launch flavors (applet/javaws/..)\n // validateGLDrawable();\n\n if(DEBUG) {\n System.err.println(Thread.currentThread().getName()+\" - Info: addNotify - end: peer: \"+getPeer());\n }\n } finally {\n drawableSync.unlock();\n }\n }\n\n private void createDrawableAndContext() {\n // no lock required, since this resource ain't available yet\n final JAWTWindow jawtWindow = (JAWTWindow) NativeWindowFactory.getNativeWindow(this, awtConfig);\n jawtWindow.setShallUseOffscreenLayer(shallUseOffscreenLayer);\n jawtWindow.lockSurface();\n try {\n drawable = GLDrawableFactory.getFactory(capsReqUser.getGLProfile()).createGLDrawable(jawtWindow);\n context = (GLContextImpl) drawable.createContext(shareWith);\n context.setSynchronized(true);\n context.setContextCreationFlags(additionalCtxCreationFlags);\n } finally {\n jawtWindow.unlockSurface();\n }\n }\n \n private boolean validateGLDrawable() {\n boolean realized = false;\n if (!Beans.isDesignTime()) {\n drawableSync.lock();\n try {\n if ( null != drawable ) {\n realized = drawable.isRealized();\n if ( !realized && 0 < drawable.getWidth() * drawable.getHeight() ) {\n drawable.setRealized(true);\n realized = true;\n sendReshape=true; // ensure a reshape is being send ..\n if(DEBUG) {\n String msg = Thread.currentThread().getName()+\" - Realized Drawable: \"+drawable.toString();\n // System.err.println(msg);\n Throwable t = new Throwable(msg);\n t.printStackTrace();\n }\n }\n }\n } finally {\n drawableSync.unlock();\n }\n }\n return realized;\n }\n\n /**

Overridden to track when this component is removed from a\n container. Subclasses which override this method must call\n super.removeNotify() in their removeNotify() method in order to\n function properly.

\n

User shall not call this method outside of EDT, read the AWT/Swing specs\n about this.

\n Overrides:\n
removeNotify in class java.awt.Component
*/\n @SuppressWarnings(\"deprecation\")\n @Override\n public void removeNotify() {\n if(DEBUG) {\n Exception ex1 = new Exception(Thread.currentThread().getName()+\" - Info: removeNotify - start\");\n ex1.printStackTrace();\n }\n\n awtWindowClosingProtocol.removeClosingListener();\n\n if (Beans.isDesignTime()) {\n super.removeNotify();\n } else {\n drawableSync.lock();\n try {\n dispose(false);\n } finally {\n context=null;\n drawable=null;\n awtConfig=null;\n super.removeNotify();\n drawableSync.unlock();\n }\n }\n if(DEBUG) {\n System.err.println(Thread.currentThread().getName()+\" - Info: removeNotify - end, peer: \"+getPeer());\n }\n }\n\n /** Overridden to cause {@link GLDrawableHelper#reshape} to be\n called on all registered {@link GLEventListener}s. Subclasses\n which override this method must call super.reshape() in\n their reshape() method in order to function properly.

\n\n Overrides:\n

reshape in class java.awt.Component
*/\n @SuppressWarnings(\"deprecation\")\n @Override\n public void reshape(int x, int y, int width, int height) {\n super.reshape(x, y, width, height);\n if(null != drawable && drawable.isRealized() && !drawable.getChosenGLCapabilities().isOnscreen()) {\n dispose(true);\n } else {\n sendReshape = true;\n }\n }\n\n /** Overrides:\n
update in class java.awt.Component
*/\n /** \n * Overridden from Canvas to prevent the AWT's clearing of the\n * canvas from interfering with the OpenGL rendering.\n */\n @Override\n public void update(Graphics g) {\n paint(g);\n }\n \n public void addGLEventListener(GLEventListener listener) {\n drawableHelper.addGLEventListener(listener);\n }\n\n public void addGLEventListener(int index, GLEventListener listener) {\n drawableHelper.addGLEventListener(index, listener);\n }\n\n public void removeGLEventListener(GLEventListener listener) {\n drawableHelper.removeGLEventListener(listener);\n }\n\n public void setAnimator(GLAnimatorControl animatorControl) {\n drawableHelper.setAnimator(animatorControl);\n }\n\n public GLAnimatorControl getAnimator() {\n return drawableHelper.getAnimator();\n }\n\n public void invoke(boolean wait, GLRunnable glRunnable) {\n drawableHelper.invoke(this, wait, glRunnable);\n }\n\n public void setContext(GLContext ctx) {\n context=(GLContextImpl)ctx;\n if(null != context) {\n context.setContextCreationFlags(additionalCtxCreationFlags);\n }\n }\n\n public GLContext getContext() {\n return context;\n }\n\n public GL getGL() {\n if (Beans.isDesignTime()) {\n return null;\n }\n GLContext ctx = getContext();\n return (ctx == null) ? null : ctx.getGL();\n }\n\n public GL setGL(GL gl) {\n GLContext ctx = getContext();\n if (ctx != null) {\n ctx.setGL(gl);\n return gl;\n }\n return null;\n }\n\n\n public void setAutoSwapBufferMode(boolean onOrOff) {\n drawableHelper.setAutoSwapBufferMode(onOrOff);\n }\n\n public boolean getAutoSwapBufferMode() {\n return drawableHelper.getAutoSwapBufferMode();\n }\n\n public void swapBuffers() {\n maybeDoSingleThreadedWorkaround(swapBuffersOnEventDispatchThreadAction, swapBuffersAction);\n }\n\n public void setContextCreationFlags(int flags) {\n additionalCtxCreationFlags = flags;\n }\n \n public int getContextCreationFlags() {\n return additionalCtxCreationFlags; \n }\n \n public GLProfile getGLProfile() {\n return capsReqUser.getGLProfile();\n }\n\n public GLCapabilitiesImmutable getChosenGLCapabilities() {\n if (awtConfig == null) {\n throw new GLException(\"No AWTGraphicsConfiguration: \"+this);\n }\n\n return (GLCapabilitiesImmutable)awtConfig.getChosenCapabilities();\n }\n\n public GLCapabilitiesImmutable getRequestedGLCapabilities() {\n if (awtConfig == null) {\n return capsReqUser;\n }\n\n return (GLCapabilitiesImmutable)awtConfig.getRequestedCapabilities();\n }\n\n public NativeSurface getNativeSurface() {\n drawableSync.lock();\n try {\n return (null != drawable) ? drawable.getNativeSurface() : null;\n } finally {\n drawableSync.unlock();\n }\n }\n\n public long getHandle() {\n drawableSync.lock();\n try {\n return (null != drawable) ? drawable.getHandle() : 0;\n } finally {\n drawableSync.unlock();\n }\n }\n\n public GLDrawableFactory getFactory() {\n drawableSync.lock();\n try {\n return (null != drawable) ? drawable.getFactory() : null;\n } finally {\n drawableSync.unlock();\n }\n }\n\n @Override\n public String toString() {\n final int dw = (null!=drawable) ? drawable.getWidth() : -1;\n final int dh = (null!=drawable) ? drawable.getHeight() : -1;\n \n return \"AWT-GLCanvas[Realized \"+isRealized()+\n \",\\n\\t\"+((null!=drawable)?drawable.getClass().getName():\"null-drawable\")+ \n \",\\n\\tRealized \"+isRealized()+\n \",\\n\\tFactory \"+getFactory()+\n \",\\n\\thandle 0x\"+Long.toHexString(getHandle())+\n \",\\n\\tDrawable size \"+dw+\"x\"+dh+\n \",\\n\\tAWT pos \"+getX()+\"/\"+getY()+\", size \"+getWidth()+\"x\"+getHeight()+\n \",\\n\\tvisible \"+isVisible()+\n \",\\n\\t\"+awtConfig+\"]\";\n }\n \n //----------------------------------------------------------------------\n // Internals only below this point\n //\n\n private void maybeDoSingleThreadedWorkaround(Runnable eventDispatchThreadAction,\n Runnable invokeGLAction) {\n if (Threading.isSingleThreaded() &&\n !Threading.isOpenGLThread()) {\n Threading.invokeOnOpenGLThread(eventDispatchThreadAction);\n } else {\n drawableHelper.invokeGL(drawable, context, invokeGLAction, initAction);\n }\n }\n\n class DisposeAction implements Runnable {\n public void run() {\n drawableHelper.dispose(GLCanvas.this);\n\n if(null!=context) {\n context.makeCurrent(); // implicit wait for lock ..\n context.destroy();\n context=null;\n }\n\n if(null!=drawable) {\n final JAWTWindow jawtWindow = (JAWTWindow)drawable.getNativeSurface();\n drawable.setRealized(false);\n drawable=null;\n if(null!=jawtWindow) {\n jawtWindow.destroy();\n } \n }\n \n if(disposeRegenerate) {\n // Similar process as in addNotify()!\n \n // Recreate GLDrawable/GLContext to reflect it's new graphics configuration\n createDrawableAndContext();\n \n if(DEBUG) {\n System.err.println(\"GLCanvas.dispose(true): new drawable: \"+drawable);\n }\n validateGLDrawable(); // immediate attempt to recreate the drawable\n }\n }\n }\n private boolean disposeRegenerate;\n private DisposeAction disposeAction = new DisposeAction();\n\n private DisposeOnEventDispatchThreadAction disposeOnEventDispatchThreadAction =\n new DisposeOnEventDispatchThreadAction();\n\n class DisposeOnEventDispatchThreadAction implements Runnable {\n public void run() {\n drawableHelper.invokeGL(drawable, context, disposeAction, null);\n }\n }\n\n class DisposeAbstractGraphicsDeviceAction implements Runnable {\n public void run() {\n if(null != awtConfig) {\n final AbstractGraphicsDevice adevice = awtConfig.getScreen().getDevice();\n final String adeviceMsg;\n if(DEBUG) {\n adeviceMsg = adevice.toString();\n } else {\n adeviceMsg = null; \n }\n boolean closed = awtConfig.getScreen().getDevice().close();\n if(DEBUG) {\n System.err.println(Thread.currentThread().getName() + \" - GLCanvas.dispose(false): closed GraphicsDevice: \"+adeviceMsg+\", result: \"+closed);\n }\n awtConfig=null;\n }\n }\n }\n private DisposeAbstractGraphicsDeviceAction disposeAbstractGraphicsDeviceAction = new DisposeAbstractGraphicsDeviceAction();\n\n /**\n * Disposes the AbstractGraphicsDevice within EDT,\n * since resources created (X11: Display), must be destroyed in the same thread, where they have been created.\n *\n * @see #chooseGraphicsConfiguration(javax.media.opengl.GLCapabilitiesImmutable, javax.media.opengl.GLCapabilitiesImmutable, javax.media.opengl.GLCapabilitiesChooser, java.awt.GraphicsDevice)\n */\n void disposeAbstractGraphicsDevice() {\n if( EventQueue.isDispatchThread() || Thread.holdsLock(getTreeLock()) ) {\n disposeAbstractGraphicsDeviceAction.run();\n } else {\n try {\n EventQueue.invokeAndWait(disposeAbstractGraphicsDeviceAction);\n } catch (InvocationTargetException e) {\n throw new GLException(e.getTargetException());\n } catch (InterruptedException e) {\n throw new GLException(e);\n }\n }\n }\n\n class InitAction implements Runnable {\n public void run() {\n drawableHelper.init(GLCanvas.this);\n }\n }\n private InitAction initAction = new InitAction();\n \n class DisplayAction implements Runnable {\n public void run() {\n if (sendReshape) {\n if(DEBUG) {\n System.err.println(Thread.currentThread().getName()+\" - reshape: \"+getWidth()+\"x\"+getHeight());\n }\n // Note: we ignore the given x and y within the parent component\n // since we are drawing directly into this heavyweight component.\n drawableHelper.reshape(GLCanvas.this, 0, 0, getWidth(), getHeight());\n sendReshape = false;\n }\n\n drawableHelper.display(GLCanvas.this);\n }\n }\n private DisplayAction displayAction = new DisplayAction();\n\n class SwapBuffersAction implements Runnable {\n public void run() {\n drawable.swapBuffers();\n }\n }\n private SwapBuffersAction swapBuffersAction = new SwapBuffersAction();\n\n // Workaround for ATI driver bugs related to multithreading issues\n // like simultaneous rendering via Animators to canvases that are\n // being resized on the AWT event dispatch thread\n class DisplayOnEventDispatchThreadAction implements Runnable {\n public void run() {\n drawableHelper.invokeGL(drawable, context, displayAction, initAction);\n }\n }\n private DisplayOnEventDispatchThreadAction displayOnEventDispatchThreadAction =\n new DisplayOnEventDispatchThreadAction();\n class SwapBuffersOnEventDispatchThreadAction implements Runnable {\n public void run() {\n drawableHelper.invokeGL(drawable, context, swapBuffersAction, initAction);\n }\n }\n private SwapBuffersOnEventDispatchThreadAction swapBuffersOnEventDispatchThreadAction =\n new SwapBuffersOnEventDispatchThreadAction();\n\n // Disables the AWT's erasing of this Canvas's background on Windows\n // in Java SE 6. This internal API is not available in previous\n // releases, but the system property\n // -Dsun.awt.noerasebackground=true can be specified to get similar\n // results globally in previous releases.\n private static boolean disableBackgroundEraseInitialized;\n private static Method disableBackgroundEraseMethod;\n private void disableBackgroundErase() {\n if (!disableBackgroundEraseInitialized) {\n try {\n AccessController.doPrivileged(new PrivilegedAction() {\n public Object run() {\n try {\n Class clazz = getToolkit().getClass();\n while (clazz != null && disableBackgroundEraseMethod == null) {\n try {\n disableBackgroundEraseMethod =\n clazz.getDeclaredMethod(\"disableBackgroundErase\",\n new Class[] { Canvas.class });\n disableBackgroundEraseMethod.setAccessible(true);\n } catch (Exception e) {\n clazz = clazz.getSuperclass();\n }\n }\n } catch (Exception e) {\n }\n return null;\n }\n });\n } catch (Exception e) {\n }\n disableBackgroundEraseInitialized = true;\n if(DEBUG) {\n System.err.println(\"GLCanvas: TK disableBackgroundErase method found: \"+\n (null!=disableBackgroundEraseMethod));\n }\n }\n if (disableBackgroundEraseMethod != null) {\n Throwable t=null;\n try {\n disableBackgroundEraseMethod.invoke(getToolkit(), new Object[] { this });\n } catch (Exception e) {\n t = e;\n }\n if(DEBUG) {\n System.err.println(\"GLCanvas: TK disableBackgroundErase error: \"+t);\n }\n }\n }\n\n /**\n * Issues the GraphicsConfigurationFactory's choosing facility within EDT,\n * since resources created (X11: Display), must be destroyed in the same thread, where they have been created.\n *\n * @param capsChosen\n * @param capsRequested\n * @param chooser\n * @param device\n * @return the chosen AWTGraphicsConfiguration\n *\n * @see #disposeAbstractGraphicsDevice()\n */\n private AWTGraphicsConfiguration chooseGraphicsConfiguration(final GLCapabilitiesImmutable capsChosen,\n final GLCapabilitiesImmutable capsRequested,\n final GLCapabilitiesChooser chooser,\n final GraphicsDevice device) {\n // Make GLCanvas behave better in NetBeans GUI builder\n if (Beans.isDesignTime()) {\n return null;\n }\n\n final AbstractGraphicsScreen aScreen = null != device ? \n AWTGraphicsScreen.createScreenDevice(device, AbstractGraphicsDevice.DEFAULT_UNIT):\n AWTGraphicsScreen.createDefault();\n AWTGraphicsConfiguration config = null;\n\n if( EventQueue.isDispatchThread() || Thread.holdsLock(getTreeLock()) ) {\n config = (AWTGraphicsConfiguration)\n GraphicsConfigurationFactory.getFactory(AWTGraphicsDevice.class).chooseGraphicsConfiguration(capsChosen,\n capsRequested,\n chooser, aScreen);\n } else {\n try {\n final ArrayList bucket = new ArrayList(1);\n EventQueue.invokeAndWait(new Runnable() {\n public void run() {\n AWTGraphicsConfiguration c = (AWTGraphicsConfiguration)\n GraphicsConfigurationFactory.getFactory(AWTGraphicsDevice.class).chooseGraphicsConfiguration(capsChosen,\n capsRequested,\n chooser, aScreen);\n bucket.add(c);\n }\n });\n config = ( bucket.size() > 0 ) ? bucket.get(0) : null ;\n } catch (InvocationTargetException e) {\n throw new GLException(e.getTargetException());\n } catch (InterruptedException e) {\n throw new GLException(e);\n }\n }\n\n if (config == null) {\n throw new GLException(\"Error: Couldn't fetch AWTGraphicsConfiguration\");\n }\n\n return config;\n }\n \n /**\n * A most simple JOGL AWT test entry\n */\n public static void main(String args[]) {\n System.err.println(VersionUtil.getPlatformInfo());\n System.err.println(GlueGenVersion.getInstance());\n // System.err.println(NativeWindowVersion.getInstance());\n System.err.println(JoglVersion.getInstance());\n\n GLDrawableFactory factory = GLDrawableFactory.getDesktopFactory();\n List availCaps = factory.getAvailableCapabilities(null);\n for(int i=0; i