Path: blob/master/src/java.desktop/share/classes/sun/java2d/opengl/OGLUtilities.java
41159 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.opengl;2627import java.awt.Graphics;28import java.awt.GraphicsConfiguration;29import java.awt.Rectangle;30import sun.java2d.SunGraphics2D;31import sun.java2d.SurfaceData;32import sun.java2d.pipe.Region;3334/**35* This class contains a number of static utility methods that may be36* called (via reflection) by a third-party library, such as JOGL, in order37* to interoperate with the OGL-based Java 2D pipeline.38*39* WARNING: These methods are being made available as a temporary measure40* until we offer a more complete, public solution. Like any sun.* class,41* this class is not an officially supported public API; it may be modified42* at will or removed completely in a future release.43*/44class OGLUtilities {4546/**47* These OGL-specific surface type constants are the same as those48* defined in the OGLSurfaceData class and are duplicated here so that49* clients of this API can access them more easily via reflection.50*/51public static final int UNDEFINED = OGLSurfaceData.UNDEFINED;52public static final int WINDOW = OGLSurfaceData.WINDOW;53public static final int TEXTURE = OGLSurfaceData.TEXTURE;54public static final int FLIP_BACKBUFFER = OGLSurfaceData.FLIP_BACKBUFFER;55public static final int FBOBJECT = OGLSurfaceData.FBOBJECT;5657private OGLUtilities() {58}5960/**61* Returns true if the current thread is the OGL QueueFlusher thread.62*/63public static boolean isQueueFlusherThread() {64return OGLRenderQueue.isQueueFlusherThread();65}6667/**68* Invokes the given Runnable on the OGL QueueFlusher thread with the69* OpenGL context corresponding to the given Graphics object made70* current. It is legal for OpenGL code executed in the given71* Runnable to change the current OpenGL context; it will be reset72* once the Runnable completes. No guarantees are made as to the73* state of the OpenGL context of the Graphics object; for74* example, calling code must set the scissor box using the return75* value from {@link #getOGLScissorBox} to avoid drawing76* over other Swing components, and must typically set the OpenGL77* viewport using the return value from {@link #getOGLViewport} to78* make the client's OpenGL rendering appear in the correct place79* relative to the scissor region.80*81* In order to avoid deadlock, it is important that the given Runnable82* does not attempt to acquire the AWT lock, as that will be handled83* automatically as part of the {@code rq.flushAndInvokeNow()} step.84*85* @param g the Graphics object for the corresponding destination surface;86* if null, the step making a context current to the destination surface87* will be skipped88* @param r the action to be performed on the QFT; cannot be null89* @return true if the operation completed successfully, or false if90* there was any problem making a context current to the surface91* associated with the given Graphics object92*/93public static boolean invokeWithOGLContextCurrent(Graphics g, Runnable r) {94OGLRenderQueue rq = OGLRenderQueue.getInstance();95rq.lock();96try {97if (g != null) {98if (!(g instanceof SunGraphics2D)) {99return false;100}101SurfaceData sData = ((SunGraphics2D)g).surfaceData;102if (!(sData instanceof OGLSurfaceData)) {103return false;104}105106// make a context current to the destination surface107OGLContext.validateContext((OGLSurfaceData)sData);108}109110// invoke the given runnable on the QFT111rq.flushAndInvokeNow(r);112113// invalidate the current context so that the next time we render114// with Java 2D, the context state will be completely revalidated115OGLContext.invalidateCurrentContext();116} finally {117rq.unlock();118}119120return true;121}122123/**124* Invokes the given Runnable on the OGL QueueFlusher thread with the125* "shared" OpenGL context (corresponding to the given126* GraphicsConfiguration object) made current. This method is typically127* used when the Runnable needs a current context to complete its128* operation, but does not require that the context be made current to129* a particular surface. For example, an application may call this130* method so that the given Runnable can query the OpenGL capabilities131* of the given GraphicsConfiguration, without making a context current132* to a dummy surface (or similar hacky techniques).133*134* In order to avoid deadlock, it is important that the given Runnable135* does not attempt to acquire the AWT lock, as that will be handled136* automatically as part of the {@code rq.flushAndInvokeNow()} step.137*138* @param config the GraphicsConfiguration object whose "shared"139* context will be made current during this operation; if this value is140* null or if OpenGL is not enabled for the GraphicsConfiguration, this141* method will return false142* @param r the action to be performed on the QFT; cannot be null143* @return true if the operation completed successfully, or false if144* there was any problem making the shared context current145*/146public static boolean147invokeWithOGLSharedContextCurrent(GraphicsConfiguration config,148Runnable r)149{150if (!(config instanceof OGLGraphicsConfig)) {151return false;152}153154OGLRenderQueue rq = OGLRenderQueue.getInstance();155rq.lock();156try {157// make the "shared" context current for the given GraphicsConfig158OGLContext.setScratchSurface((OGLGraphicsConfig)config);159160// invoke the given runnable on the QFT161rq.flushAndInvokeNow(r);162163// invalidate the current context so that the next time we render164// with Java 2D, the context state will be completely revalidated165OGLContext.invalidateCurrentContext();166} finally {167rq.unlock();168}169170return true;171}172173/**174* Returns the Rectangle describing the OpenGL viewport on the175* Java 2D surface associated with the given Graphics object and176* component width and height. When a third-party library is177* performing OpenGL rendering directly into the visible region of178* the associated surface, this viewport helps the application179* position the OpenGL output correctly on that surface.180*181* Note that the x/y values in the returned Rectangle object represent182* the lower-left corner of the viewport region, relative to the183* lower-left corner of the given surface.184*185* @param g the Graphics object for the corresponding destination surface;186* cannot be null187* @param componentWidth width of the component to be painted188* @param componentHeight height of the component to be painted189* @return a Rectangle describing the OpenGL viewport for the given190* destination surface and component dimensions, or null if the given191* Graphics object is invalid192*/193public static Rectangle getOGLViewport(Graphics g,194int componentWidth,195int componentHeight)196{197if (!(g instanceof SunGraphics2D)) {198return null;199}200201SunGraphics2D sg2d = (SunGraphics2D)g;202SurfaceData sData = sg2d.surfaceData;203204// this is the upper-left origin of the region to be painted,205// relative to the upper-left origin of the surface206// (in Java2D coordinates)207int x0 = sg2d.transX;208int y0 = sg2d.transY;209210// this is the lower-left origin of the region to be painted,211// relative to the lower-left origin of the surface212// (in OpenGL coordinates)213Rectangle surfaceBounds = sData.getBounds();214int x1 = x0;215int y1 = surfaceBounds.height - (y0 + componentHeight);216217return new Rectangle(x1, y1, componentWidth, componentHeight);218}219220/**221* Returns the Rectangle describing the OpenGL scissor box on the222* Java 2D surface associated with the given Graphics object. When a223* third-party library is performing OpenGL rendering directly224* into the visible region of the associated surface, this scissor box225* must be set to avoid drawing over existing rendering results.226*227* Note that the x/y values in the returned Rectangle object represent228* the lower-left corner of the scissor region, relative to the229* lower-left corner of the given surface.230*231* @param g the Graphics object for the corresponding destination surface;232* cannot be null233* @return a Rectangle describing the OpenGL scissor box for the given234* Graphics object and corresponding destination surface, or null if the235* given Graphics object is invalid or the clip region is non-rectangular236*/237public static Rectangle getOGLScissorBox(Graphics g) {238if (!(g instanceof SunGraphics2D)) {239return null;240}241242SunGraphics2D sg2d = (SunGraphics2D)g;243SurfaceData sData = sg2d.surfaceData;244Region r = sg2d.getCompClip();245if (!r.isRectangular()) {246// caller probably doesn't know how to handle shape clip247// appropriately, so just return null (Swing currently never248// sets a shape clip, but that could change in the future)249return null;250}251252// this is the upper-left origin of the scissor box relative to the253// upper-left origin of the surface (in Java 2D coordinates)254int x0 = r.getLoX();255int y0 = r.getLoY();256257// this is the width and height of the scissor region258int w = r.getWidth();259int h = r.getHeight();260261// this is the lower-left origin of the scissor box relative to the262// lower-left origin of the surface (in OpenGL coordinates)263Rectangle surfaceBounds = sData.getBounds();264int x1 = x0;265int y1 = surfaceBounds.height - (y0 + h);266267return new Rectangle(x1, y1, w, h);268}269270/**271* Returns an Object identifier for the Java 2D surface associated with272* the given Graphics object. This identifier may be used to determine273* whether the surface has changed since the last invocation of this274* operation, and thereby whether the OpenGL state corresponding to the275* old surface must be destroyed and recreated.276*277* @param g the Graphics object for the corresponding destination surface;278* cannot be null279* @return an identifier for the surface associated with the given280* Graphics object, or null if the given Graphics object is invalid281*/282public static Object getOGLSurfaceIdentifier(Graphics g) {283if (!(g instanceof SunGraphics2D)) {284return null;285}286return ((SunGraphics2D)g).surfaceData;287}288289/**290* Returns one of the OGL-specific surface type constants (defined in291* this class), which describes the surface associated with the given292* Graphics object.293*294* @param g the Graphics object for the corresponding destination surface;295* cannot be null296* @return a constant that describes the surface associated with the297* given Graphics object; if the given Graphics object is invalid (i.e.298* is not associated with an OpenGL surface) this method will return299* {@code OGLUtilities.UNDEFINED}300*/301public static int getOGLSurfaceType(Graphics g) {302if (!(g instanceof SunGraphics2D)) {303return UNDEFINED;304}305SurfaceData sData = ((SunGraphics2D)g).surfaceData;306if (!(sData instanceof OGLSurfaceData)) {307return UNDEFINED;308}309return ((OGLSurfaceData)sData).getType();310}311312/**313* Returns the OpenGL texture target constant (either GL_TEXTURE_2D314* or GL_TEXTURE_RECTANGLE_ARB) for the surface associated with the315* given Graphics object. This method is only useful for those surface316* types that are backed by an OpenGL texture, namely {@code TEXTURE},317* {@code FBOBJECT}, and (on Windows only) {@code PBUFFER}.318*319* @param g the Graphics object for the corresponding destination surface;320* cannot be null321* @return the texture target constant for the surface associated with the322* given Graphics object; if the given Graphics object is invalid (i.e.323* is not associated with an OpenGL surface), or the associated surface324* is not backed by an OpenGL texture, this method will return zero.325*/326public static int getOGLTextureType(Graphics g) {327if (!(g instanceof SunGraphics2D)) {328return 0;329}330SurfaceData sData = ((SunGraphics2D)g).surfaceData;331if (!(sData instanceof OGLSurfaceData)) {332return 0;333}334return ((OGLSurfaceData)sData).getTextureTarget();335}336}337338339