Showing the changes to java/pkg/sun/java2d/opengl/OGLUtilities.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,338 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package sun.java2d.opengl; | ||
| 27 | |||
| 28 | import java.awt.Graphics; | ||
| 29 | import java.awt.GraphicsConfiguration; | ||
| 30 | import java.awt.Rectangle; | ||
| 31 | import sun.java2d.SunGraphics2D; | ||
| 32 | import sun.java2d.SurfaceData; | ||
| 33 | import sun.java2d.pipe.Region; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * This class contains a number of static utility methods that may be | ||
| 37 | * called (via reflection) by a third-party library, such as JOGL, in order | ||
| 38 | * to interoperate with the OGL-based Java 2D pipeline. | ||
| 39 | * | ||
| 40 | * WARNING: These methods are being made available as a temporary measure | ||
| 41 | * until we offer a more complete, public solution. Like any sun.* class, | ||
| 42 | * this class is not an officially supported public API; it may be modified | ||
| 43 | * at will or removed completely in a future release. | ||
| 44 | */ | ||
| 45 | class OGLUtilities { | ||
| 46 | |||
| 47 | /** | ||
| 48 | * These OGL-specific surface type constants are the same as those | ||
| 49 | * defined in the OGLSurfaceData class and are duplicated here so that | ||
| 50 | * clients of this API can access them more easily via reflection. | ||
| 51 | */ | ||
| 52 | public static final int UNDEFINED = OGLSurfaceData.UNDEFINED; | ||
| 53 | public static final int WINDOW = OGLSurfaceData.WINDOW; | ||
| 54 | public static final int PBUFFER = OGLSurfaceData.PBUFFER; | ||
| 55 | public static final int TEXTURE = OGLSurfaceData.TEXTURE; | ||
| 56 | public static final int FLIP_BACKBUFFER = OGLSurfaceData.FLIP_BACKBUFFER; | ||
| 57 | public static final int FBOBJECT = OGLSurfaceData.FBOBJECT; | ||
| 58 | |||
| 59 | private OGLUtilities() { | ||
| 60 | } | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Returns true if the current thread is the OGL QueueFlusher thread. | ||
| 64 | */ | ||
| 65 | public static boolean isQueueFlusherThread() { | ||
| 66 | return OGLRenderQueue.isQueueFlusherThread(); | ||
| 67 | } | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Invokes the given Runnable on the OGL QueueFlusher thread with the | ||
| 71 | * OpenGL context corresponding to the given Graphics object made | ||
| 72 | * current. It is legal for OpenGL code executed in the given | ||
| 73 | * Runnable to change the current OpenGL context; it will be reset | ||
| 74 | * once the Runnable completes. No guarantees are made as to the | ||
| 75 | * state of the OpenGL context of the Graphics object; for | ||
| 76 | * example, calling code must set the scissor box using the return | ||
| 77 | * value from {@link #getOGLScissorBox} to avoid drawing | ||
| 78 | * over other Swing components, and must typically set the OpenGL | ||
| 79 | * viewport using the return value from {@link #getOGLViewport} to | ||
| 80 | * make the client's OpenGL rendering appear in the correct place | ||
| 81 | * relative to the scissor region. | ||
| 82 | * | ||
| 83 | * In order to avoid deadlock, it is important that the given Runnable | ||
| 84 | * does not attempt to acquire the AWT lock, as that will be handled | ||
| 85 | * automatically as part of the <code>rq.flushAndInvokeNow()</code> step. | ||
| 86 | * | ||
| 87 | * @param g the Graphics object for the corresponding destination surface; | ||
| 88 | * if null, the step making a context current to the destination surface | ||
| 89 | * will be skipped | ||
| 90 | * @param r the action to be performed on the QFT; cannot be null | ||
| 91 | * @return true if the operation completed successfully, or false if | ||
| 92 | * there was any problem making a context current to the surface | ||
| 93 | * associated with the given Graphics object | ||
| 94 | */ | ||
| 95 | public static boolean invokeWithOGLContextCurrent(Graphics g, Runnable r) { | ||
| 96 | OGLRenderQueue rq = OGLRenderQueue.getInstance(); | ||
| 97 | rq.lock(); | ||
| 98 | try { | ||
| 99 | if (g != null) { | ||
| 100 | if (!(g instanceof SunGraphics2D)) { | ||
| 101 | return false; | ||
| 102 | } | ||
| 103 | SurfaceData sData = ((SunGraphics2D)g).surfaceData; | ||
| 104 | if (!(sData instanceof OGLSurfaceData)) { | ||
| 105 | return false; | ||
| 106 | } | ||
| 107 | |||
| 108 | // make a context current to the destination surface | ||
| 109 | OGLContext.validateContext((OGLSurfaceData)sData); | ||
| 110 | } | ||
| 111 | |||
| 112 | // invoke the given runnable on the QFT | ||
| 113 | rq.flushAndInvokeNow(r); | ||
| 114 | |||
| 115 | // invalidate the current context so that the next time we render | ||
| 116 | // with Java 2D, the context state will be completely revalidated | ||
| 117 | OGLContext.invalidateCurrentContext(); | ||
| 118 | } finally { | ||
| 119 | rq.unlock(); | ||
| 120 | } | ||
| 121 | |||
| 122 | return true; | ||
| 123 | } | ||
| 124 | |||
| 125 | /** | ||
| 126 | * Invokes the given Runnable on the OGL QueueFlusher thread with the | ||
| 127 | * "shared" OpenGL context (corresponding to the given | ||
| 128 | * GraphicsConfiguration object) made current. This method is typically | ||
| 129 | * used when the Runnable needs a current context to complete its | ||
| 130 | * operation, but does not require that the context be made current to | ||
| 131 | * a particular surface. For example, an application may call this | ||
| 132 | * method so that the given Runnable can query the OpenGL capabilities | ||
| 133 | * of the given GraphicsConfiguration, without making a context current | ||
| 134 | * to a dummy surface (or similar hacky techniques). | ||
| 135 | * | ||
| 136 | * In order to avoid deadlock, it is important that the given Runnable | ||
| 137 | * does not attempt to acquire the AWT lock, as that will be handled | ||
| 138 | * automatically as part of the <code>rq.flushAndInvokeNow()</code> step. | ||
| 139 | * | ||
| 140 | * @param config the GraphicsConfiguration object whose "shared" | ||
| 141 | * context will be made current during this operation; if this value is | ||
| 142 | * null or if OpenGL is not enabled for the GraphicsConfiguration, this | ||
| 143 | * method will return false | ||
| 144 | * @param r the action to be performed on the QFT; cannot be null | ||
| 145 | * @return true if the operation completed successfully, or false if | ||
| 146 | * there was any problem making the shared context current | ||
| 147 | */ | ||
| 148 | public static boolean | ||
| 149 | invokeWithOGLSharedContextCurrent(GraphicsConfiguration config, | ||
| 150 | Runnable r) | ||
| 151 | { | ||
| 152 | if (!(config instanceof OGLGraphicsConfig)) { | ||
| 153 | return false; | ||
| 154 | } | ||
| 155 | |||
| 156 | OGLRenderQueue rq = OGLRenderQueue.getInstance(); | ||
| 157 | rq.lock(); | ||
| 158 | try { | ||
| 159 | // make the "shared" context current for the given GraphicsConfig | ||
| 160 | OGLContext.setScratchSurface((OGLGraphicsConfig)config); | ||
| 161 | |||
| 162 | // invoke the given runnable on the QFT | ||
| 163 | rq.flushAndInvokeNow(r); | ||
| 164 | |||
| 165 | // invalidate the current context so that the next time we render | ||
| 166 | // with Java 2D, the context state will be completely revalidated | ||
| 167 | OGLContext.invalidateCurrentContext(); | ||
| 168 | } finally { | ||
| 169 | rq.unlock(); | ||
| 170 | } | ||
| 171 | |||
| 172 | return true; | ||
| 173 | } | ||
| 174 | |||
| 175 | /** | ||
| 176 | * Returns the Rectangle describing the OpenGL viewport on the | ||
| 177 | * Java 2D surface associated with the given Graphics object and | ||
| 178 | * component width and height. When a third-party library is | ||
| 179 | * performing OpenGL rendering directly into the visible region of | ||
| 180 | * the associated surface, this viewport helps the application | ||
| 181 | * position the OpenGL output correctly on that surface. | ||
| 182 | * | ||
| 183 | * Note that the x/y values in the returned Rectangle object represent | ||
| 184 | * the lower-left corner of the viewport region, relative to the | ||
| 185 | * lower-left corner of the given surface. | ||
| 186 | * | ||
| 187 | * @param g the Graphics object for the corresponding destination surface; | ||
| 188 | * cannot be null | ||
| 189 | * @param componentWidth width of the component to be painted | ||
| 190 | * @param componentHeight height of the component to be painted | ||
| 191 | * @return a Rectangle describing the OpenGL viewport for the given | ||
| 192 | * destination surface and component dimensions, or null if the given | ||
| 193 | * Graphics object is invalid | ||
| 194 | */ | ||
| 195 | public static Rectangle getOGLViewport(Graphics g, | ||
| 196 | int componentWidth, | ||
| 197 | int componentHeight) | ||
| 198 | { | ||
| 199 | if (!(g instanceof SunGraphics2D)) { | ||
| 200 | return null; | ||
| 201 | } | ||
| 202 | |||
| 203 | SunGraphics2D sg2d = (SunGraphics2D)g; | ||
| 204 | SurfaceData sData = (SurfaceData)sg2d.surfaceData; | ||
| 205 | |||
| 206 | // this is the upper-left origin of the region to be painted, | ||
| 207 | // relative to the upper-left origin of the surface | ||
| 208 | // (in Java2D coordinates) | ||
| 209 | int x0 = sg2d.transX; | ||
| 210 | int y0 = sg2d.transY; | ||
| 211 | |||
| 212 | // this is the lower-left origin of the region to be painted, | ||
| 213 | // relative to the lower-left origin of the surface | ||
| 214 | // (in OpenGL coordinates) | ||
| 215 | Rectangle surfaceBounds = sData.getBounds(); | ||
| 216 | int x1 = x0; | ||
| 217 | int y1 = surfaceBounds.height - (y0 + componentHeight); | ||
| 218 | |||
| 219 | return new Rectangle(x1, y1, componentWidth, componentHeight); | ||
| 220 | } | ||
| 221 | |||
| 222 | /** | ||
| 223 | * Returns the Rectangle describing the OpenGL scissor box on the | ||
| 224 | * Java 2D surface associated with the given Graphics object. When a | ||
| 225 | * third-party library is performing OpenGL rendering directly | ||
| 226 | * into the visible region of the associated surface, this scissor box | ||
| 227 | * must be set to avoid drawing over existing rendering results. | ||
| 228 | * | ||
| 229 | * Note that the x/y values in the returned Rectangle object represent | ||
| 230 | * the lower-left corner of the scissor region, relative to the | ||
| 231 | * lower-left corner of the given surface. | ||
| 232 | * | ||
| 233 | * @param g the Graphics object for the corresponding destination surface; | ||
| 234 | * cannot be null | ||
| 235 | * @return a Rectangle describing the OpenGL scissor box for the given | ||
| 236 | * Graphics object and corresponding destination surface, or null if the | ||
| 237 | * given Graphics object is invalid or the clip region is non-rectangular | ||
| 238 | */ | ||
| 239 | public static Rectangle getOGLScissorBox(Graphics g) { | ||
| 240 | if (!(g instanceof SunGraphics2D)) { | ||
| 241 | return null; | ||
| 242 | } | ||
| 243 | |||
| 244 | SunGraphics2D sg2d = (SunGraphics2D)g; | ||
| 245 | SurfaceData sData = (SurfaceData)sg2d.surfaceData; | ||
| 246 | Region r = sg2d.getCompClip(); | ||
| 247 | if (!r.isRectangular()) { | ||
| 248 | // caller probably doesn't know how to handle shape clip | ||
| 249 | // appropriately, so just return null (Swing currently never | ||
| 250 | // sets a shape clip, but that could change in the future) | ||
| 251 | return null; | ||
| 252 | } | ||
| 253 | |||
| 254 | // this is the upper-left origin of the scissor box relative to the | ||
| 255 | // upper-left origin of the surface (in Java 2D coordinates) | ||
| 256 | int x0 = r.getLoX(); | ||
| 257 | int y0 = r.getLoY(); | ||
| 258 | |||
| 259 | // this is the width and height of the scissor region | ||
| 260 | int w = r.getWidth(); | ||
| 261 | int h = r.getHeight(); | ||
| 262 | |||
| 263 | // this is the lower-left origin of the scissor box relative to the | ||
| 264 | // lower-left origin of the surface (in OpenGL coordinates) | ||
| 265 | Rectangle surfaceBounds = sData.getBounds(); | ||
| 266 | int x1 = x0; | ||
| 267 | int y1 = surfaceBounds.height - (y0 + h); | ||
| 268 | |||
| 269 | return new Rectangle(x1, y1, w, h); | ||
| 270 | } | ||
| 271 | |||
| 272 | /** | ||
| 273 | * Returns an Object identifier for the Java 2D surface associated with | ||
| 274 | * the given Graphics object. This identifier may be used to determine | ||
| 275 | * whether the surface has changed since the last invocation of this | ||
| 276 | * operation, and thereby whether the OpenGL state corresponding to the | ||
| 277 | * old surface must be destroyed and recreated. | ||
| 278 | * | ||
| 279 | * @param g the Graphics object for the corresponding destination surface; | ||
| 280 | * cannot be null | ||
| 281 | * @return an identifier for the surface associated with the given | ||
| 282 | * Graphics object, or null if the given Graphics object is invalid | ||
| 283 | */ | ||
| 284 | public static Object getOGLSurfaceIdentifier(Graphics g) { | ||
| 285 | if (!(g instanceof SunGraphics2D)) { | ||
| 286 | return null; | ||
| 287 | } | ||
| 288 | return ((SunGraphics2D)g).surfaceData; | ||
| 289 | } | ||
| 290 | |||
| 291 | /** | ||
| 292 | * Returns one of the OGL-specific surface type constants (defined in | ||
| 293 | * this class), which describes the surface associated with the given | ||
| 294 | * Graphics object. | ||
| 295 | * | ||
| 296 | * @param g the Graphics object for the corresponding destination surface; | ||
| 297 | * cannot be null | ||
| 298 | * @return a constant that describes the surface associated with the | ||
| 299 | * given Graphics object; if the given Graphics object is invalid (i.e. | ||
| 300 | * is not associated with an OpenGL surface) this method will return | ||
| 301 | * <code>OGLUtilities.UNDEFINED</code> | ||
| 302 | */ | ||
| 303 | public static int getOGLSurfaceType(Graphics g) { | ||
| 304 | if (!(g instanceof SunGraphics2D)) { | ||
| 305 | return UNDEFINED; | ||
| 306 | } | ||
| 307 | SurfaceData sData = ((SunGraphics2D)g).surfaceData; | ||
| 308 | if (!(sData instanceof OGLSurfaceData)) { | ||
| 309 | return UNDEFINED; | ||
| 310 | } | ||
| 311 | return ((OGLSurfaceData)sData).getType(); | ||
| 312 | } | ||
| 313 | |||
| 314 | /** | ||
| 315 | * Returns the OpenGL texture target constant (either GL_TEXTURE_2D | ||
| 316 | * or GL_TEXTURE_RECTANGLE_ARB) for the surface associated with the | ||
| 317 | * given Graphics object. This method is only useful for those surface | ||
| 318 | * types that are backed by an OpenGL texture, namely {@code TEXTURE}, | ||
| 319 | * {@code FBOBJECT}, and (on Windows only) {@code PBUFFER}. | ||
| 320 | * | ||
| 321 | * @param g the Graphics object for the corresponding destination surface; | ||
| 322 | * cannot be null | ||
| 323 | * @return the texture target constant for the surface associated with the | ||
| 324 | * given Graphics object; if the given Graphics object is invalid (i.e. | ||
| 325 | * is not associated with an OpenGL surface), or the associated surface | ||
| 326 | * is not backed by an OpenGL texture, this method will return zero. | ||
| 327 | */ | ||
| 328 | public static int getOGLTextureType(Graphics g) { | ||
| 329 | if (!(g instanceof SunGraphics2D)) { | ||
| 330 | return 0; | ||
| 331 | } | ||
| 332 | SurfaceData sData = ((SunGraphics2D)g).surfaceData; | ||
| 333 | if (!(sData instanceof OGLSurfaceData)) { | ||
| 334 | return 0; | ||
| 335 | } | ||
| 336 | return ((OGLSurfaceData)sData).getTextureTarget(); | ||
| 337 | } | ||
| 338 | } | ||