0N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.java2d.opengl;
0N/A
0N/Aimport java.awt.Graphics;
0N/Aimport java.awt.GraphicsConfiguration;
0N/Aimport java.awt.Rectangle;
0N/Aimport sun.java2d.SunGraphics2D;
0N/Aimport sun.java2d.SurfaceData;
0N/Aimport sun.java2d.pipe.Region;
0N/A
0N/A/**
0N/A * This class contains a number of static utility methods that may be
0N/A * called (via reflection) by a third-party library, such as JOGL, in order
0N/A * to interoperate with the OGL-based Java 2D pipeline.
0N/A *
0N/A * WARNING: These methods are being made available as a temporary measure
0N/A * until we offer a more complete, public solution. Like any sun.* class,
0N/A * this class is not an officially supported public API; it may be modified
0N/A * at will or removed completely in a future release.
0N/A */
0N/Aclass OGLUtilities {
0N/A
0N/A /**
0N/A * These OGL-specific surface type constants are the same as those
0N/A * defined in the OGLSurfaceData class and are duplicated here so that
0N/A * clients of this API can access them more easily via reflection.
0N/A */
0N/A public static final int UNDEFINED = OGLSurfaceData.UNDEFINED;
0N/A public static final int WINDOW = OGLSurfaceData.WINDOW;
0N/A public static final int PBUFFER = OGLSurfaceData.PBUFFER;
0N/A public static final int TEXTURE = OGLSurfaceData.TEXTURE;
0N/A public static final int FLIP_BACKBUFFER = OGLSurfaceData.FLIP_BACKBUFFER;
0N/A public static final int FBOBJECT = OGLSurfaceData.FBOBJECT;
0N/A
0N/A private OGLUtilities() {
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the current thread is the OGL QueueFlusher thread.
0N/A */
0N/A public static boolean isQueueFlusherThread() {
0N/A return OGLRenderQueue.isQueueFlusherThread();
0N/A }
0N/A
0N/A /**
0N/A * Invokes the given Runnable on the OGL QueueFlusher thread with the
0N/A * OpenGL context corresponding to the given Graphics object made
0N/A * current. It is legal for OpenGL code executed in the given
0N/A * Runnable to change the current OpenGL context; it will be reset
0N/A * once the Runnable completes. No guarantees are made as to the
0N/A * state of the OpenGL context of the Graphics object; for
0N/A * example, calling code must set the scissor box using the return
0N/A * value from {@link #getOGLScissorBox} to avoid drawing
0N/A * over other Swing components, and must typically set the OpenGL
0N/A * viewport using the return value from {@link #getOGLViewport} to
0N/A * make the client's OpenGL rendering appear in the correct place
0N/A * relative to the scissor region.
0N/A *
0N/A * In order to avoid deadlock, it is important that the given Runnable
0N/A * does not attempt to acquire the AWT lock, as that will be handled
0N/A * automatically as part of the <code>rq.flushAndInvokeNow()</code> step.
0N/A *
0N/A * @param g the Graphics object for the corresponding destination surface;
0N/A * if null, the step making a context current to the destination surface
0N/A * will be skipped
0N/A * @param r the action to be performed on the QFT; cannot be null
0N/A * @return true if the operation completed successfully, or false if
0N/A * there was any problem making a context current to the surface
0N/A * associated with the given Graphics object
0N/A */
0N/A public static boolean invokeWithOGLContextCurrent(Graphics g, Runnable r) {
0N/A OGLRenderQueue rq = OGLRenderQueue.getInstance();
0N/A rq.lock();
0N/A try {
0N/A if (g != null) {
0N/A if (!(g instanceof SunGraphics2D)) {
0N/A return false;
0N/A }
0N/A SurfaceData sData = ((SunGraphics2D)g).surfaceData;
0N/A if (!(sData instanceof OGLSurfaceData)) {
0N/A return false;
0N/A }
0N/A
0N/A // make a context current to the destination surface
0N/A OGLContext.validateContext((OGLSurfaceData)sData);
0N/A }
0N/A
0N/A // invoke the given runnable on the QFT
0N/A rq.flushAndInvokeNow(r);
0N/A
0N/A // invalidate the current context so that the next time we render
0N/A // with Java 2D, the context state will be completely revalidated
0N/A OGLContext.invalidateCurrentContext();
0N/A } finally {
0N/A rq.unlock();
0N/A }
0N/A
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Invokes the given Runnable on the OGL QueueFlusher thread with the
0N/A * "shared" OpenGL context (corresponding to the given
0N/A * GraphicsConfiguration object) made current. This method is typically
0N/A * used when the Runnable needs a current context to complete its
0N/A * operation, but does not require that the context be made current to
0N/A * a particular surface. For example, an application may call this
0N/A * method so that the given Runnable can query the OpenGL capabilities
0N/A * of the given GraphicsConfiguration, without making a context current
0N/A * to a dummy surface (or similar hacky techniques).
0N/A *
0N/A * In order to avoid deadlock, it is important that the given Runnable
0N/A * does not attempt to acquire the AWT lock, as that will be handled
0N/A * automatically as part of the <code>rq.flushAndInvokeNow()</code> step.
0N/A *
0N/A * @param config the GraphicsConfiguration object whose "shared"
0N/A * context will be made current during this operation; if this value is
0N/A * null or if OpenGL is not enabled for the GraphicsConfiguration, this
0N/A * method will return false
0N/A * @param r the action to be performed on the QFT; cannot be null
0N/A * @return true if the operation completed successfully, or false if
0N/A * there was any problem making the shared context current
0N/A */
0N/A public static boolean
0N/A invokeWithOGLSharedContextCurrent(GraphicsConfiguration config,
0N/A Runnable r)
0N/A {
0N/A if (!(config instanceof OGLGraphicsConfig)) {
0N/A return false;
0N/A }
0N/A
0N/A OGLRenderQueue rq = OGLRenderQueue.getInstance();
0N/A rq.lock();
0N/A try {
0N/A // make the "shared" context current for the given GraphicsConfig
0N/A OGLContext.setScratchSurface((OGLGraphicsConfig)config);
0N/A
0N/A // invoke the given runnable on the QFT
0N/A rq.flushAndInvokeNow(r);
0N/A
0N/A // invalidate the current context so that the next time we render
0N/A // with Java 2D, the context state will be completely revalidated
0N/A OGLContext.invalidateCurrentContext();
0N/A } finally {
0N/A rq.unlock();
0N/A }
0N/A
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Returns the Rectangle describing the OpenGL viewport on the
0N/A * Java 2D surface associated with the given Graphics object and
0N/A * component width and height. When a third-party library is
0N/A * performing OpenGL rendering directly into the visible region of
0N/A * the associated surface, this viewport helps the application
0N/A * position the OpenGL output correctly on that surface.
0N/A *
0N/A * Note that the x/y values in the returned Rectangle object represent
0N/A * the lower-left corner of the viewport region, relative to the
0N/A * lower-left corner of the given surface.
0N/A *
0N/A * @param g the Graphics object for the corresponding destination surface;
0N/A * cannot be null
0N/A * @param componentWidth width of the component to be painted
0N/A * @param componentHeight height of the component to be painted
0N/A * @return a Rectangle describing the OpenGL viewport for the given
0N/A * destination surface and component dimensions, or null if the given
0N/A * Graphics object is invalid
0N/A */
0N/A public static Rectangle getOGLViewport(Graphics g,
0N/A int componentWidth,
0N/A int componentHeight)
0N/A {
0N/A if (!(g instanceof SunGraphics2D)) {
0N/A return null;
0N/A }
0N/A
0N/A SunGraphics2D sg2d = (SunGraphics2D)g;
0N/A SurfaceData sData = (SurfaceData)sg2d.surfaceData;
0N/A
0N/A // this is the upper-left origin of the region to be painted,
0N/A // relative to the upper-left origin of the surface
0N/A // (in Java2D coordinates)
0N/A int x0 = sg2d.transX;
0N/A int y0 = sg2d.transY;
0N/A
0N/A // this is the lower-left origin of the region to be painted,
0N/A // relative to the lower-left origin of the surface
0N/A // (in OpenGL coordinates)
0N/A Rectangle surfaceBounds = sData.getBounds();
0N/A int x1 = x0;
0N/A int y1 = surfaceBounds.height - (y0 + componentHeight);
0N/A
0N/A return new Rectangle(x1, y1, componentWidth, componentHeight);
0N/A }
0N/A
0N/A /**
0N/A * Returns the Rectangle describing the OpenGL scissor box on the
0N/A * Java 2D surface associated with the given Graphics object. When a
0N/A * third-party library is performing OpenGL rendering directly
0N/A * into the visible region of the associated surface, this scissor box
0N/A * must be set to avoid drawing over existing rendering results.
0N/A *
0N/A * Note that the x/y values in the returned Rectangle object represent
0N/A * the lower-left corner of the scissor region, relative to the
0N/A * lower-left corner of the given surface.
0N/A *
0N/A * @param g the Graphics object for the corresponding destination surface;
0N/A * cannot be null
0N/A * @return a Rectangle describing the OpenGL scissor box for the given
0N/A * Graphics object and corresponding destination surface, or null if the
0N/A * given Graphics object is invalid or the clip region is non-rectangular
0N/A */
0N/A public static Rectangle getOGLScissorBox(Graphics g) {
0N/A if (!(g instanceof SunGraphics2D)) {
0N/A return null;
0N/A }
0N/A
0N/A SunGraphics2D sg2d = (SunGraphics2D)g;
0N/A SurfaceData sData = (SurfaceData)sg2d.surfaceData;
0N/A Region r = sg2d.getCompClip();
0N/A if (!r.isRectangular()) {
0N/A // caller probably doesn't know how to handle shape clip
0N/A // appropriately, so just return null (Swing currently never
0N/A // sets a shape clip, but that could change in the future)
0N/A return null;
0N/A }
0N/A
0N/A // this is the upper-left origin of the scissor box relative to the
0N/A // upper-left origin of the surface (in Java 2D coordinates)
0N/A int x0 = r.getLoX();
0N/A int y0 = r.getLoY();
0N/A
0N/A // this is the width and height of the scissor region
0N/A int w = r.getWidth();
0N/A int h = r.getHeight();
0N/A
0N/A // this is the lower-left origin of the scissor box relative to the
0N/A // lower-left origin of the surface (in OpenGL coordinates)
0N/A Rectangle surfaceBounds = sData.getBounds();
0N/A int x1 = x0;
0N/A int y1 = surfaceBounds.height - (y0 + h);
0N/A
0N/A return new Rectangle(x1, y1, w, h);
0N/A }
0N/A
0N/A /**
0N/A * Returns an Object identifier for the Java 2D surface associated with
0N/A * the given Graphics object. This identifier may be used to determine
0N/A * whether the surface has changed since the last invocation of this
0N/A * operation, and thereby whether the OpenGL state corresponding to the
0N/A * old surface must be destroyed and recreated.
0N/A *
0N/A * @param g the Graphics object for the corresponding destination surface;
0N/A * cannot be null
0N/A * @return an identifier for the surface associated with the given
0N/A * Graphics object, or null if the given Graphics object is invalid
0N/A */
0N/A public static Object getOGLSurfaceIdentifier(Graphics g) {
0N/A if (!(g instanceof SunGraphics2D)) {
0N/A return null;
0N/A }
0N/A return ((SunGraphics2D)g).surfaceData;
0N/A }
0N/A
0N/A /**
0N/A * Returns one of the OGL-specific surface type constants (defined in
0N/A * this class), which describes the surface associated with the given
0N/A * Graphics object.
0N/A *
0N/A * @param g the Graphics object for the corresponding destination surface;
0N/A * cannot be null
0N/A * @return a constant that describes the surface associated with the
0N/A * given Graphics object; if the given Graphics object is invalid (i.e.
0N/A * is not associated with an OpenGL surface) this method will return
0N/A * <code>OGLUtilities.UNDEFINED</code>
0N/A */
0N/A public static int getOGLSurfaceType(Graphics g) {
0N/A if (!(g instanceof SunGraphics2D)) {
0N/A return UNDEFINED;
0N/A }
0N/A SurfaceData sData = ((SunGraphics2D)g).surfaceData;
0N/A if (!(sData instanceof OGLSurfaceData)) {
0N/A return UNDEFINED;
0N/A }
0N/A return ((OGLSurfaceData)sData).getType();
0N/A }
0N/A
0N/A /**
0N/A * Returns the OpenGL texture target constant (either GL_TEXTURE_2D
0N/A * or GL_TEXTURE_RECTANGLE_ARB) for the surface associated with the
0N/A * given Graphics object. This method is only useful for those surface
0N/A * types that are backed by an OpenGL texture, namely {@code TEXTURE},
0N/A * {@code FBOBJECT}, and (on Windows only) {@code PBUFFER}.
0N/A *
0N/A * @param g the Graphics object for the corresponding destination surface;
0N/A * cannot be null
0N/A * @return the texture target constant for the surface associated with the
0N/A * given Graphics object; if the given Graphics object is invalid (i.e.
0N/A * is not associated with an OpenGL surface), or the associated surface
0N/A * is not backed by an OpenGL texture, this method will return zero.
0N/A */
0N/A public static int getOGLTextureType(Graphics g) {
0N/A if (!(g instanceof SunGraphics2D)) {
0N/A return 0;
0N/A }
0N/A SurfaceData sData = ((SunGraphics2D)g).surfaceData;
0N/A if (!(sData instanceof OGLSurfaceData)) {
0N/A return 0;
0N/A }
0N/A return ((OGLSurfaceData)sData).getTextureTarget();
0N/A }
0N/A}