summaryrefslogtreecommitdiffstats
path: root/src/classes/javax/media/opengl/Threading.java
blob: 7475152e1786ea827cd1a721b3d215462aff39a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 * 
 * Sun gratefully acknowledges that this software was originally authored
 * and developed by Kenneth Bradley Russell and Christopher John Kline.
 */

package javax.media.opengl;

import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;
import java.security.AccessController;
import java.security.PrivilegedAction;

import com.sun.opengl.impl.*;

/** This API provides access to the threading model for the implementation of 
    the classes in this package.

    <P>

    OpenGL is specified as a thread-safe API, but in practice there
    are multithreading-related issues on most, if not all, of the
    platforms which support it. For example, some OpenGL
    implementations do not behave well when one context is made
    current first on one thread, released, and then made current on a
    second thread, although this is legal according to the OpenGL
    specification. On other platforms there are other problems.

    <P>

    Due to these limitations, and due to the inherent multithreading
    in the Java platform (in particular, in the Abstract Window
    Toolkit), it is often necessary to limit the multithreading 
    occurring in the typical application using the OpenGL API. 

    <P>

    In the current reference implementation, for instance, multithreading 
    has been limited by
    forcing all OpenGL-related work for GLAutoDrawables on to a single
    thread. In other words, if an application uses only the
    GLAutoDrawable and GLEventListener callback mechanism, it is
    guaranteed to have the most correct single-threaded behavior on
    all platforms.

    <P>

    Applications using the GLContext makeCurrent/release API directly
    will inherently break this single-threaded model, as these methods
    require that the OpenGL context be made current on the current
    thread immediately. For applications wishing to integrate better
    with an implementation that uses the single-threaded model, this
    class provides public access to the mechanism used by the implementation.

    <P>

    Users can execute Runnables on the
    internal thread used for performing OpenGL work, and query whether
    the current thread is already this thread. Using these mechanisms
    the user can move work from the current thread on to the internal
    OpenGL thread if desired.

    <P>

    This class also provides mechanisms for querying whether this
    internal serialization of OpenGL work is in effect, and a
    programmatic way of disabling it.  In the current reference 
    implementation it is enabled by default, although it could be 
    disabled in the future if OpenGL drivers become more robust on 
    all platforms.

    <P>

    In addition to specifying programmatically whether the single
    thread for OpenGL work is enabled, users may switch it on and off
    using the system property <code>opengl.1thread</code>. Valid values
    for this system property are:

    <PRE>
    -Dopengl.1thread=false     Disable single-threading of OpenGL work
    -Dopengl.1thread=true      Enable single-threading of OpenGL work (default -- on a newly-created worker thread)
    -Dopengl.1thread=auto      Select default single-threading behavior (currently on)
    -Dopengl.1thread=awt       Enable single-threading of OpenGL work on AWT event dispatch thread (current default on all
                                 platforms, and also the default behavior older releases)
    -Dopengl.1thread=worker    Enable single-threading of OpenGL work on newly-created worker thread (not suitable for Mac
                                 OS X or X11 platforms, and risky on Windows in applet environments)
    </PRE>    
*/

public class Threading {
  private static boolean singleThreaded = true;
  private static final int AWT    = 1;
  private static final int WORKER = 2;
  private static int mode;
  
  static {
    AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
          String workaround = System.getProperty("opengl.1thread");
          // Default to using the AWT thread on all platforms except
          // Windows. On OS X there is instability apparently due to
          // using the JAWT on non-AWT threads. On X11 platforms there
          // are potential deadlocks which can be caused if the AWT
          // EventQueue thread hands work off to the GLWorkerThread
          // while holding the AWT lock. The optimization of
          // makeCurrent / release calls isn't worth these stability
          // problems.
          boolean isWindows = System.getProperty("os.name").startsWith("Windows");
          // int defaultMode = (isWindows ? WORKER : AWT);
          int defaultMode = AWT;
          mode = defaultMode;
          if (workaround != null) {
            workaround = workaround.toLowerCase();
            if (workaround.equals("true") ||
                workaround.equals("auto")) {
              // Nothing to do; singleThreaded and mode already set up
            } else if (workaround.equals("worker")) {
              singleThreaded = true;
              mode = WORKER;
            } else if (workaround.equals("awt")) {
              singleThreaded = true;
              mode = AWT;
            } else {
              singleThreaded = false;
            }
          }
          printWorkaroundNotice();
          return null;
        }
      });
  }

  /** No reason to ever instantiate this class */
  private Threading() {}

  /** If an implementation of the javax.media.opengl APIs offers a 
      multithreading option but the default behavior is single-threading, 
      this API provides a mechanism for end users to disable single-threading 
      in this implementation.  Users are strongly discouraged from
      calling this method unless they are aware of all of the
      consequences and are prepared to enforce some amount of
      threading restrictions in their applications. Disabling
      single-threading, for example, may have unintended consequences
      on GLAutoDrawable implementations such as GLCanvas, GLJPanel and
      GLPbuffer. Currently there is no supported way to re-enable it
      once disabled, partly to discourage careless use of this
      method. This method should be called as early as possible in an
      application. */ 
  public static void disableSingleThreading() {
    singleThreaded = false;
    if (Debug.verbose()) {
      System.err.println("Application forced disabling of single-threading of javax.media.opengl implementation");
    }
  }

  /** Indicates whether OpenGL work is being automatically forced to a
      single thread in this implementation. */
  public static boolean isSingleThreaded() {
    return singleThreaded;
  }

  /** Indicates whether the current thread is the single thread on
      which this implementation of the javax.media.opengl APIs
      performs all of its OpenGL-related work. This method should only
      be called if the single-thread model is in effect. */
  public static boolean isOpenGLThread() throws GLException {
    if (!isSingleThreaded()) {
      throw new GLException("Should only call this in single-threaded mode");
    }

    switch (mode) {
      case AWT:
        if (Java2D.isOGLPipelineActive()) {
          // FIXME: ideally only the QFT would be considered to be the
          // "OpenGL thread", but we can not currently run all of JOGL's
          // OpenGL work on that thread. For now, run the GLJPanel's
          // Java2D/JOGL bridge on the QFT but everything else on the
          // EDT, except when we're already on the QFT.
          return (Java2D.isQueueFlusherThread() ||
                  EventQueue.isDispatchThread());
        } else {
          return EventQueue.isDispatchThread();
        }
      case WORKER:
        if (Java2D.isOGLPipelineActive()) {
          // FIXME: ideally only the QFT would be considered to be the
          // "OpenGL thread", but we can not currently run all of JOGL's
          // OpenGL work on that thread. For now, run the GLJPanel's
          // Java2D/JOGL bridge on the QFT but everything else on the
          // worker thread, except when we're already on the QFT.
          return (Java2D.isQueueFlusherThread() ||
                  GLWorkerThread.isWorkerThread());
        } else {
          return GLWorkerThread.isWorkerThread();
        }
      default:
        throw new InternalError("Illegal single-threading mode " + mode);
    }
  }

  /** Executes the passed Runnable on the single thread used for all
      OpenGL work in this javax.media.opengl API implementation. It is
      not specified exactly which thread is used for this
      purpose. This method should only be called if the single-thread
      model is in use and if the current thread is not the OpenGL
      thread (i.e., if <code>isOpenGLThread()</code> returns
      false). It is up to the end user to check to see whether the
      current thread is the OpenGL thread and either execute the
      Runnable directly or perform the work inside it. */
  public static void invokeOnOpenGLThread(Runnable r) throws GLException {
    if (!isSingleThreaded()) {
      throw new GLException ("Should only call this in single-threaded mode");
    }

    if (isOpenGLThread()) {
      throw new GLException ("Should only call this from other threads than the OpenGL thread");
    }    

    switch (mode) {
      case AWT:
        // FIXME: ideally should run all OpenGL work on the Java2D QFT
        // thread when it's enabled, but there are issues with this when
        // the GLJPanel is not using the Java2D bridge; would like to run
        // its OpenGL work on the QFT, but do the image drawing from the
        // EDT. Other issues still remain with the GLCanvas as well.

        //    if (Java2D.isOGLPipelineActive()) {
        //      Java2D.invokeWithOGLContextCurrent(null, r);
        //    } else {
        try {
          EventQueue.invokeAndWait(r);
        } catch (InvocationTargetException e) {
          throw new GLException(e.getTargetException());
        } catch (InterruptedException e) {
          throw new GLException(e);
        }
        //    }    
        break;

      case WORKER:
        if (!GLWorkerThread.isStarted()) {
          synchronized (GLWorkerThread.class) {
            if (!GLWorkerThread.isStarted()) {
              GLWorkerThread.start();
            }
          }
        }
        try {
          GLWorkerThread.invokeAndWait(r);
        } catch (InvocationTargetException e) {
          throw new GLException(e.getTargetException());
        } catch (InterruptedException e) {
          throw new GLException(e);
        }
        break;

      default:
        throw new InternalError("Illegal single-threading mode " + mode);
    }
  }

  private static void printWorkaroundNotice() {
    if (singleThreaded && Debug.verbose()) {
      System.err.println("Using " +
                         (mode == AWT ? "AWT" : "OpenGL worker") +
                         " thread for performing OpenGL work in javax.media.opengl implementation");
    }
  }
}