001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.java2d.opengl;
027:
028: import java.awt.Graphics;
029: import java.awt.GraphicsConfiguration;
030: import java.awt.Rectangle;
031: import sun.java2d.SunGraphics2D;
032: import sun.java2d.SurfaceData;
033: import sun.java2d.pipe.Region;
034:
035: /**
036: * This class contains a number of static utility methods that may be
037: * called (via reflection) by a third-party library, such as JOGL, in order
038: * to interoperate with the OGL-based Java 2D pipeline.
039: *
040: * WARNING: These methods are being made available as a temporary measure
041: * until we offer a more complete, public solution. Like any sun.* class,
042: * this class is not an officially supported public API; it may be modified
043: * at will or removed completely in a future release.
044: */
045: class OGLUtilities {
046:
047: /**
048: * These OGL-specific surface type constants are the same as those
049: * defined in the OGLSurfaceData class and are duplicated here so that
050: * clients of this API can access them more easily via reflection.
051: */
052: public static final int UNDEFINED = OGLSurfaceData.UNDEFINED;
053: public static final int WINDOW = OGLSurfaceData.WINDOW;
054: public static final int PBUFFER = OGLSurfaceData.PBUFFER;
055: public static final int TEXTURE = OGLSurfaceData.TEXTURE;
056: public static final int FLIP_BACKBUFFER = OGLSurfaceData.FLIP_BACKBUFFER;
057: public static final int FBOBJECT = OGLSurfaceData.FBOBJECT;
058:
059: private OGLUtilities() {
060: }
061:
062: /**
063: * Returns true if the current thread is the OGL QueueFlusher thread.
064: */
065: public static boolean isQueueFlusherThread() {
066: return OGLRenderQueue.isQueueFlusherThread();
067: }
068:
069: /**
070: * Invokes the given Runnable on the OGL QueueFlusher thread with the
071: * OpenGL context corresponding to the given Graphics object made
072: * current. It is legal for OpenGL code executed in the given
073: * Runnable to change the current OpenGL context; it will be reset
074: * once the Runnable completes. No guarantees are made as to the
075: * state of the OpenGL context of the Graphics object; for
076: * example, calling code must set the scissor box using the return
077: * value from {@link #getOGLScissorBox} to avoid drawing
078: * over other Swing components, and must typically set the OpenGL
079: * viewport using the return value from {@link #getOGLViewport} to
080: * make the client's OpenGL rendering appear in the correct place
081: * relative to the scissor region.
082: *
083: * In order to avoid deadlock, it is important that the given Runnable
084: * does not attempt to acquire the AWT lock, as that will be handled
085: * automatically as part of the <code>rq.flushAndInvokeNow()</code> step.
086: *
087: * @param g the Graphics object for the corresponding destination surface;
088: * if null, the step making a context current to the destination surface
089: * will be skipped
090: * @param r the action to be performed on the QFT; cannot be null
091: * @return true if the operation completed successfully, or false if
092: * there was any problem making a context current to the surface
093: * associated with the given Graphics object
094: */
095: public static boolean invokeWithOGLContextCurrent(Graphics g,
096: Runnable r) {
097: OGLRenderQueue rq = OGLRenderQueue.getInstance();
098: rq.lock();
099: try {
100: if (g != null) {
101: if (!(g instanceof SunGraphics2D)) {
102: return false;
103: }
104: SurfaceData sData = ((SunGraphics2D) g).surfaceData;
105: if (!(sData instanceof OGLSurfaceData)) {
106: return false;
107: }
108:
109: // make a context current to the destination surface
110: OGLContext.validateContext((OGLSurfaceData) sData);
111: }
112:
113: // invoke the given runnable on the QFT
114: rq.flushAndInvokeNow(r);
115:
116: // invalidate the current context so that the next time we render
117: // with Java 2D, the context state will be completely revalidated
118: OGLContext.invalidateCurrentContext();
119: } finally {
120: rq.unlock();
121: }
122:
123: return true;
124: }
125:
126: /**
127: * Invokes the given Runnable on the OGL QueueFlusher thread with the
128: * "shared" OpenGL context (corresponding to the given
129: * GraphicsConfiguration object) made current. This method is typically
130: * used when the Runnable needs a current context to complete its
131: * operation, but does not require that the context be made current to
132: * a particular surface. For example, an application may call this
133: * method so that the given Runnable can query the OpenGL capabilities
134: * of the given GraphicsConfiguration, without making a context current
135: * to a dummy surface (or similar hacky techniques).
136: *
137: * In order to avoid deadlock, it is important that the given Runnable
138: * does not attempt to acquire the AWT lock, as that will be handled
139: * automatically as part of the <code>rq.flushAndInvokeNow()</code> step.
140: *
141: * @param config the GraphicsConfiguration object whose "shared"
142: * context will be made current during this operation; if this value is
143: * null or if OpenGL is not enabled for the GraphicsConfiguration, this
144: * method will return false
145: * @param r the action to be performed on the QFT; cannot be null
146: * @return true if the operation completed successfully, or false if
147: * there was any problem making the shared context current
148: */
149: public static boolean invokeWithOGLSharedContextCurrent(
150: GraphicsConfiguration config, Runnable r) {
151: if (!(config instanceof OGLGraphicsConfig)) {
152: return false;
153: }
154:
155: OGLRenderQueue rq = OGLRenderQueue.getInstance();
156: rq.lock();
157: try {
158: // make the "shared" context current for the given GraphicsConfig
159: OGLContext.setScratchSurface((OGLGraphicsConfig) config);
160:
161: // invoke the given runnable on the QFT
162: rq.flushAndInvokeNow(r);
163:
164: // invalidate the current context so that the next time we render
165: // with Java 2D, the context state will be completely revalidated
166: OGLContext.invalidateCurrentContext();
167: } finally {
168: rq.unlock();
169: }
170:
171: return true;
172: }
173:
174: /**
175: * Returns the Rectangle describing the OpenGL viewport on the
176: * Java 2D surface associated with the given Graphics object and
177: * component width and height. When a third-party library is
178: * performing OpenGL rendering directly into the visible region of
179: * the associated surface, this viewport helps the application
180: * position the OpenGL output correctly on that surface.
181: *
182: * Note that the x/y values in the returned Rectangle object represent
183: * the lower-left corner of the viewport region, relative to the
184: * lower-left corner of the given surface.
185: *
186: * @param g the Graphics object for the corresponding destination surface;
187: * cannot be null
188: * @param componentWidth width of the component to be painted
189: * @param componentHeight height of the component to be painted
190: * @return a Rectangle describing the OpenGL viewport for the given
191: * destination surface and component dimensions, or null if the given
192: * Graphics object is invalid
193: */
194: public static Rectangle getOGLViewport(Graphics g,
195: int componentWidth, int componentHeight) {
196: if (!(g instanceof SunGraphics2D)) {
197: return null;
198: }
199:
200: SunGraphics2D sg2d = (SunGraphics2D) g;
201: SurfaceData sData = (SurfaceData) sg2d.surfaceData;
202:
203: // this is the upper-left origin of the region to be painted,
204: // relative to the upper-left origin of the surface
205: // (in Java2D coordinates)
206: int x0 = sg2d.transX;
207: int y0 = sg2d.transY;
208:
209: // this is the lower-left origin of the region to be painted,
210: // relative to the lower-left origin of the surface
211: // (in OpenGL coordinates)
212: Rectangle surfaceBounds = sData.getBounds();
213: int x1 = x0;
214: int y1 = surfaceBounds.height - (y0 + componentHeight);
215:
216: return new Rectangle(x1, y1, componentWidth, componentHeight);
217: }
218:
219: /**
220: * Returns the Rectangle describing the OpenGL scissor box on the
221: * Java 2D surface associated with the given Graphics object. When a
222: * third-party library is performing OpenGL rendering directly
223: * into the visible region of the associated surface, this scissor box
224: * must be set to avoid drawing over existing rendering results.
225: *
226: * Note that the x/y values in the returned Rectangle object represent
227: * the lower-left corner of the scissor region, relative to the
228: * lower-left corner of the given surface.
229: *
230: * @param g the Graphics object for the corresponding destination surface;
231: * cannot be null
232: * @return a Rectangle describing the OpenGL scissor box for the given
233: * Graphics object and corresponding destination surface, or null if the
234: * given Graphics object is invalid or the clip region is non-rectangular
235: */
236: public static Rectangle getOGLScissorBox(Graphics g) {
237: if (!(g instanceof SunGraphics2D)) {
238: return null;
239: }
240:
241: SunGraphics2D sg2d = (SunGraphics2D) g;
242: SurfaceData sData = (SurfaceData) sg2d.surfaceData;
243: Region r = sg2d.getCompClip();
244: if (!r.isRectangular()) {
245: // caller probably doesn't know how to handle shape clip
246: // appropriately, so just return null (Swing currently never
247: // sets a shape clip, but that could change in the future)
248: return null;
249: }
250:
251: // this is the upper-left origin of the scissor box relative to the
252: // upper-left origin of the surface (in Java 2D coordinates)
253: int x0 = r.getLoX();
254: int y0 = r.getLoY();
255:
256: // this is the width and height of the scissor region
257: int w = r.getWidth();
258: int h = r.getHeight();
259:
260: // this is the lower-left origin of the scissor box relative to the
261: // lower-left origin of the surface (in OpenGL coordinates)
262: Rectangle surfaceBounds = sData.getBounds();
263: int x1 = x0;
264: int y1 = surfaceBounds.height - (y0 + h);
265:
266: return new Rectangle(x1, y1, w, h);
267: }
268:
269: /**
270: * Returns an Object identifier for the Java 2D surface associated with
271: * the given Graphics object. This identifier may be used to determine
272: * whether the surface has changed since the last invocation of this
273: * operation, and thereby whether the OpenGL state corresponding to the
274: * old surface must be destroyed and recreated.
275: *
276: * @param g the Graphics object for the corresponding destination surface;
277: * cannot be null
278: * @return an identifier for the surface associated with the given
279: * Graphics object, or null if the given Graphics object is invalid
280: */
281: public static Object getOGLSurfaceIdentifier(Graphics g) {
282: if (!(g instanceof SunGraphics2D)) {
283: return null;
284: }
285: return ((SunGraphics2D) g).surfaceData;
286: }
287:
288: /**
289: * Returns one of the OGL-specific surface type constants (defined in
290: * this class), which describes the surface associated with the given
291: * Graphics object.
292: *
293: * @param g the Graphics object for the corresponding destination surface;
294: * cannot be null
295: * @return a constant that describes the surface associated with the
296: * given Graphics object; if the given Graphics object is invalid (i.e.
297: * is not associated with an OpenGL surface) this method will return
298: * <code>OGLUtilities.UNDEFINED</code>
299: */
300: public static int getOGLSurfaceType(Graphics g) {
301: if (!(g instanceof SunGraphics2D)) {
302: return UNDEFINED;
303: }
304: SurfaceData sData = ((SunGraphics2D) g).surfaceData;
305: if (!(sData instanceof OGLSurfaceData)) {
306: return UNDEFINED;
307: }
308: return ((OGLSurfaceData) sData).getType();
309: }
310:
311: /**
312: * Returns the OpenGL texture target constant (either GL_TEXTURE_2D
313: * or GL_TEXTURE_RECTANGLE_ARB) for the surface associated with the
314: * given Graphics object. This method is only useful for those surface
315: * types that are backed by an OpenGL texture, namely {@code TEXTURE},
316: * {@code FBOBJECT}, and (on Windows only) {@code PBUFFER}.
317: *
318: * @param g the Graphics object for the corresponding destination surface;
319: * cannot be null
320: * @return the texture target constant for the surface associated with the
321: * given Graphics object; if the given Graphics object is invalid (i.e.
322: * is not associated with an OpenGL surface), or the associated surface
323: * is not backed by an OpenGL texture, this method will return zero.
324: */
325: public static int getOGLTextureType(Graphics g) {
326: if (!(g instanceof SunGraphics2D)) {
327: return 0;
328: }
329: SurfaceData sData = ((SunGraphics2D) g).surfaceData;
330: if (!(sData instanceof OGLSurfaceData)) {
331: return 0;
332: }
333: return ((OGLSurfaceData) sData).getTextureTarget();
334: }
335: }
|