diff options
Diffstat (limited to 'gl4java/awt')
-rw-r--r-- | gl4java/awt/GLAnimCanvas.java | 149 | ||||
-rw-r--r-- | gl4java/awt/GLCanvas.java | 180 |
2 files changed, 115 insertions, 214 deletions
diff --git a/gl4java/awt/GLAnimCanvas.java b/gl4java/awt/GLAnimCanvas.java index b16206c..9268db8 100644 --- a/gl4java/awt/GLAnimCanvas.java +++ b/gl4java/awt/GLAnimCanvas.java @@ -131,7 +131,7 @@ import java.lang.Math; * */ public class GLAnimCanvas extends GLCanvas - implements Runnable + implements GLRunnable { /** * To support frames per scounds, @@ -173,7 +173,7 @@ public class GLAnimCanvas extends GLCanvas protected boolean threadSuspended = false; static { - if(GLContext.loadNativeLibraries(null, null, null)==false) + if(GLContext.doLoadNativeLibraries(null, null, null)==false) System.out.println("GLAnimCanvas could not load def. native libs."); } @@ -286,14 +286,12 @@ public class GLAnimCanvas extends GLCanvas { } - protected boolean useRepaint = true; + protected boolean useRepaint = false; protected boolean useFpsSleep = true; protected boolean useYield = true; - protected boolean useSDisplay = true; - /** * The normal behavior is to use 'repaint' * within the AWT-Event Thread to render. @@ -310,7 +308,7 @@ public class GLAnimCanvas extends GLCanvas * @see gl4java.awt.GLCanvas#sDisplay * @see gl4java.awt.GLAnimCanvas#setUseFpsSleep */ - public void setUseRepaint(boolean b) + public synchronized void setUseRepaint(boolean b) { useRepaint = b; } @@ -327,7 +325,7 @@ public class GLAnimCanvas extends GLCanvas * @see gl4java.awt.GLCanvas#sDisplay * @see gl4java.awt.GLAnimCanvas#setUseRepaint */ - public void setUseFpsSleep(boolean b) + public synchronized void setUseFpsSleep(boolean b) { useFpsSleep = b; } @@ -338,13 +336,6 @@ public class GLAnimCanvas extends GLCanvas useYield = b; } - /** The default behavior, if not using repaints, is to call - sDisplay() in the thread's main loop; set this to false to - call display() directly. */ - public void setUseSDisplay(boolean val) { - useSDisplay = val; - } - public boolean getUseRepaint() { return useRepaint; @@ -360,15 +351,19 @@ public class GLAnimCanvas extends GLCanvas return useYield; } - public boolean getUseSDisplay() - { - return useSDisplay; - } + /** + * Identifies this object with the given thread .. + * If this object owns this thread, it must return true ! + */ + public boolean ownsThread(Thread thread) + { + return killme!=null && killme==thread ; + } - /** - * HERE WE DO HAVE OUR RUNNING THREAD ! - * WE NEED STUFF LIKE THAT FOR ANIMATION ;-) - */ + /** + * HERE WE DO HAVE OUR RUNNING THREAD ! + * WE NEED STUFF LIKE THAT FOR ANIMATION ;-) + */ public void start() { if(killme == null) @@ -409,6 +404,7 @@ public class GLAnimCanvas extends GLCanvas protected boolean shallWeRender = true; protected boolean isRunning = false; + protected boolean forceGLFree = false; private long _fDelay = 0; private long _fDelay_Frames = 10; @@ -416,6 +412,20 @@ public class GLAnimCanvas extends GLCanvas private boolean _fDelayRun=false; /** + * Forcec this thread to release it's GLContext ! + * + * To ensure this, this thread enables itself, + * and calls gljFree(true) to force the release ! + * + * @see gl4java.awt.GLAnimCanvas#run + * @see gl4java.GLContext#gljMakeCurrent + */ + public void freeGLContext() + { + forceGLFree=true; + } + + /** * The running loop for animations * which initiates the call of display * @@ -428,10 +438,7 @@ public class GLAnimCanvas extends GLCanvas isRunning = true; - boolean firstRender = true; - int numInitRetries = 1; - int numMakeCurrentRetries = 1; synchronized (this) { globalThreadNumber++; @@ -441,56 +448,54 @@ public class GLAnimCanvas extends GLCanvas { if(cvsIsInit()) { - if (firstRender) { - if (!getAutoMakeContextCurrent()) { - synchronized (this) { - if (!glj.gljMakeCurrent()) { - System.err.println("Error making context current (" + - numMakeCurrentRetries + ")..."); - ++numMakeCurrentRetries; - try { - Thread.currentThread().sleep(100); - } catch (Exception e) { - } - continue; - } - } - System.err.println("Context made current in AnimCanvas's thread"); - } - firstRender = false; - } - - /* DRAW THE TINGS .. */ - if (shallWeRender) - { - if(useRepaint) - repaint(); - else { - if (useSDisplay) { - sDisplay(); - } else { - display(); - } - } - } else { - synchronized (this) { - threadSuspended=true; - } - } - - if(fps_isCounting) - fps_frames++; + // if another thread want's to do use do .. + if(forceGLFree) + { + glj.gljFree(true); + forceGLFree=false; + if(GLContext.gljThreadDebug) + System.out.println("GLAnimCanvas: forceGLFree(1) - gljFree"); + } + else if (shallWeRender) + { + /* DRAW THE TINGS .. */ + if(useRepaint) + repaint(); + else + sDisplay(); + + if(fps_isCounting) + fps_frames++; + + } else { + synchronized (this) { + glj.gljFree(true); + threadSuspended=true; + } + } } else { - System.err.println("Waiting for canvas to initialize (" + - numInitRetries + ")..."); - ++numInitRetries; + if(GLContext.gljThreadDebug) + { + System.err.println("Waiting for canvas to initialize (" + + numInitRetries + ")..."); + } + ++numInitRetries; try { Thread.currentThread().sleep(100); } catch (Exception e) { } } + // if another thread want's to do use do .. + if(forceGLFree) + { + glj.gljFree(true); + forceGLFree=false; + if(GLContext.gljThreadDebug) + System.out.println("GLAnimCanvas: forceGLFree(2) - gljFree"); + } + try { if(useFpsSleep) { @@ -516,6 +521,7 @@ public class GLAnimCanvas extends GLCanvas } if (threadSuspended) { + glj.gljFree(true); stopFpsCounter(); synchronized (this) { while (threadSuspended) @@ -526,10 +532,8 @@ public class GLAnimCanvas extends GLCanvas {} } - if (getAutoMakeContextCurrent()) { - if(glj!=null) - glj.gljFree(); // just to be sure .. - } + if(glj!=null) + glj.gljFree(true); // just to be sure .. force freeing the context synchronized (this) { globalThreadNumber--; @@ -550,7 +554,7 @@ public class GLAnimCanvas extends GLCanvas * @see gl4java.awt.GLAnimCanvas#isAlive * @see gl4java.awt.GLAnimCanvas#start */ - public void setSuspended(boolean suspend) + public synchronized void setSuspended(boolean suspend) { setSuspended(suspend, false); } @@ -567,6 +571,7 @@ public class GLAnimCanvas extends GLCanvas * @param reInit if true the ReInit will be called additionally, * where the user can set additional initialisations * + * @see gl4java.awt.GLAnimCanvas#ReInit * @see gl4java.awt.GLAnimCanvas#isAlive * @see gl4java.awt.GLAnimCanvas#start * @see gl4java.awt.GLAnimCanvas#run diff --git a/gl4java/awt/GLCanvas.java b/gl4java/awt/GLCanvas.java index a7ade2a..041fabb 100644 --- a/gl4java/awt/GLCanvas.java +++ b/gl4java/awt/GLCanvas.java @@ -173,24 +173,6 @@ public class GLCanvas extends Canvas // The list of GLEventListeners private GLEventListenerList listeners = new GLEventListenerList(); - // Indicates whether init() has been called yet. - private volatile boolean initCalled = false; - - // Indicates whether the canvas will permit any calls to init() or - // display() from within the paint() method; defaults to true for - // backward compatibility. - private boolean enableAWTThreadRendering = true; - - // Indicates whether display() automatically makes the canvas's - // GLContext current and frees it each call; defaults to true for - // backward compatibility. On higher-end graphics cards it is - // important to minimize the number of "make current" calls even - // across frames; a GLAnimCanvas running in its own thread, and - // with AWT thread rendering disabled, will only make its - // associated context current once, at the beginning of its - // rendering loop. - private boolean autoMakeContextCurrent = true; - static { if(GLContext.doLoadNativeLibraries(null, null, null)==false) System.out.println("GLCanvas could not load def. native libs."); @@ -403,61 +385,6 @@ public class GLCanvas extends Canvas public final Window getTopLevelWindow() { return topLevelWindow; } - /** Enables/disables calls to init() and display() from within the - AWT thread. If this is enabled (the default, for backward - compatibility with earlier releases), the first call to - paint() from within the AWT thread (typically prompted by a - repaint()) will cause the canvas to be initialized, and - subsequent calls to paint() will cause display() to be called. - If AWT thread rendering is disabled, the AWT thread will not - cause init() or display() to be called if repaint() is called, - and the first call to display() will cause the canvas to be - initialized. This function is present both to work around bugs - in certain vendors' drivers which do not function properly in - multithreaded settings, and to allow improved performance of - GLAnimCanvas. */ - public void setAWTThreadRenderingEnabled(boolean val) { - enableAWTThreadRendering = val; - } - - /** Indicates whether AWT thread rendering is enabled; see {@link - #setAWTThreadRenderingEnabled}. */ - public boolean getAWTThreadRenderingEnabled() { - return enableAWTThreadRendering; - } - - /** Indicates whether the canvas automatically makes its - underlying GLContext current and frees it during each call to - display(); defaults to true for backward compatibility. On - higher-end graphics cards it is important to minimize the - number of "make current" calls even across frames; a - GLAnimCanvas running in its own thread and with AWT thread - rendering disabled will only make its associated context - current once, at the beginning of its rendering loop. */ - public void setAutoMakeContextCurrent(boolean val) { - autoMakeContextCurrent = val; - } - - /** Indicates whether the canvas automatically makes its - underlying GLContext current and frees it during each call to - display(); see {@link #setAutoMakeContextCurrent}. */ - public boolean getAutoMakeContextCurrent() { - return autoMakeContextCurrent; - } - - /** Convenience routine which Enables or disables optimized - context handling by calling {@link - #setAWTThreadRenderingEnabled} and {@link - #setAutoMakeContextCurrent} with the given boolean. */ - public void setOptimizeContextHandling(boolean yesOrNo) { - setAWTThreadRenderingEnabled(yesOrNo); - setAutoMakeContextCurrent(yesOrNo); - } - - public boolean getOptimizeContextHandling() { - return getAWTThreadRenderingEnabled() || getAutoMakeContextCurrent(); - } - /** * this function overrides the Canvas paint method ! * @@ -482,7 +409,7 @@ public class GLCanvas extends Canvas * @see gl4java.awt.GLCanvas#preInit * @see gl4java.awt.GLCanvas#init */ - public synchronized final void paint( Graphics g ) + public final void paint( Graphics g ) { if(glj == null || ( !glj.gljIsInit() && isGLEnabled() ) ) { @@ -521,10 +448,7 @@ public class GLCanvas extends Canvas (float)col.getGreen()/255.0f, (float)col.getBlue()/255.0f, 0.0f); - if (getAWTThreadRenderingEnabled()) { - init(); - initCalled = true; - } + init(); // fetch the top-level window , // to add us as the windowListener @@ -560,14 +484,9 @@ public class GLCanvas extends Canvas /* force a reshape, to be sure .. */ mustResize = true; - - // Free up the OpenGL context for another thread to use - glj.gljFree(); } - if (getAWTThreadRenderingEnabled()) { - sDisplay(); - } + sDisplay(); } /** @@ -711,15 +630,14 @@ public class GLCanvas extends Canvas protected long _f_dur = 0; /** + * Return the uses milli secounds of the last frame + */ + public long getLastFrameMillis() + { return _f_dur; } + + /** * * This is the thread save rendering-method called by paint. - * The actual thread will be set to highes priority befor calling - * 'display'. After 'display' the priority will be reset ! - * - * 'gljFree' will be NOT called after 'display'. - * - * We tested the above to use multi-threading and - * for the demonstration 'glDemos' it works ;-)) ! * * BE SURE, if you want to call 'display' by yourself * (e.g. in the run method for animation) @@ -730,11 +648,35 @@ public class GLCanvas extends Canvas * @see gl4java.awt.GLCanvas#paint * @see gl4java.awt.GLCanvas#display */ - public synchronized final void sDisplay() + public final void sDisplay() { + boolean ok = true; + + if(!cvsIsInit()) + { + return; + } + + if( mustResize ) + { + if( glj.gljMakeCurrent() == true ) + { + size = getSize(); + glj.gljResize( size.width, size.height ) ; + reshape(size.width, size.height); + mustResize = false; + invalidate(); + repaint(100); + glj.gljFree(true); /* force freeing the context here .. */ + } + } + long _s = System.currentTimeMillis(); - display(); + if(ok) + { + display(); + } _f_dur = System.currentTimeMillis()-_s; } @@ -747,7 +689,7 @@ public class GLCanvas extends Canvas * <p> * The default implementation of display() sends * preDisplay, display and postDisplay events to - * all {@link gl4java.GLEventListener}s associated with this + * all {@link gl4java.drawable.GLEventListener}s associated with this * GLCanvas in the above order. * * <p> @@ -761,8 +703,8 @@ public class GLCanvas extends Canvas { for_all(gl4java.GLEventListener) SEND display - gljFree() gljSwap() + gljFree() for_all(gl4java.GLEventListener) SEND postDisplay @@ -801,49 +743,9 @@ public class GLCanvas extends Canvas */ public void display() { - if(!cvsIsInit()) - { - return; - } - - if( mustResize ) - { - if (getAutoMakeContextCurrent()) { - if( glj.gljMakeCurrent() == true ) - { - size = getSize(); - glj.gljResize( size.width, size.height ) ; - reshape(size.width, size.height); - mustResize = false; - invalidate(); - repaint(100); - glj.gljFree(); - } - } else { - size = getSize(); - glj.gljResize( size.width, size.height ) ; - if (getAWTThreadRenderingEnabled()) { - reshape(size.width, size.height); - } - mustResize = false; - if (getAWTThreadRenderingEnabled()) { - invalidate(); - repaint(100); - } - } - } - - if (!getAWTThreadRenderingEnabled()) { - if (!initCalled) { - init(); - initCalled = true; - } - } - listeners.sendPreDisplayEvent(this); - if (getAutoMakeContextCurrent()) { - if (glj.gljMakeCurrent()) { + if (glj.gljMakeCurrent()) { listeners.sendDisplayEvent(this); glj.gljSwap(); @@ -851,12 +753,6 @@ public class GLCanvas extends Canvas glj.gljFree(); listeners.sendPostDisplayEvent(this); - } - } else { - listeners.sendDisplayEvent(this); - glj.gljSwap(); - glj.gljCheckGL(); - listeners.sendPostDisplayEvent(this); } } |