Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/opengl/OGLUtilities.java
38918 views
/*1* Copyright (c) 2005, 2006, 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 PBUFFER = OGLSurfaceData.PBUFFER;54public static final int TEXTURE = OGLSurfaceData.TEXTURE;55public static final int FLIP_BACKBUFFER = OGLSurfaceData.FLIP_BACKBUFFER;56public static final int FBOBJECT = OGLSurfaceData.FBOBJECT;5758private OGLUtilities() {59}6061/**62* Returns true if the current thread is the OGL QueueFlusher thread.63*/64public static boolean isQueueFlusherThread() {65return OGLRenderQueue.isQueueFlusherThread();66}6768/**69* Invokes the given Runnable on the OGL QueueFlusher thread with the70* OpenGL context corresponding to the given Graphics object made71* current. It is legal for OpenGL code executed in the given72* Runnable to change the current OpenGL context; it will be reset73* once the Runnable completes. No guarantees are made as to the74* state of the OpenGL context of the Graphics object; for75* example, calling code must set the scissor box using the return76* value from {@link #getOGLScissorBox} to avoid drawing77* over other Swing components, and must typically set the OpenGL78* viewport using the return value from {@link #getOGLViewport} to79* make the client's OpenGL rendering appear in the correct place80* relative to the scissor region.81*82* In order to avoid deadlock, it is important that the given Runnable83* does not attempt to acquire the AWT lock, as that will be handled84* automatically as part of the <code>rq.flushAndInvokeNow()</code> step.85*86* @param g the Graphics object for the corresponding destination surface;87* if null, the step making a context current to the destination surface88* will be skipped89* @param r the action to be performed on the QFT; cannot be null90* @return true if the operation completed successfully, or false if91* there was any problem making a context current to the surface92* associated with the given Graphics object93*/94public static boolean invokeWithOGLContextCurrent(Graphics g, Runnable r) {95OGLRenderQueue rq = OGLRenderQueue.getInstance();96rq.lock();97try {98if (g != null) {99if (!(g instanceof SunGraphics2D)) {100return false;101}102SurfaceData sData = ((SunGraphics2D)g).surfaceData;103if (!(sData instanceof OGLSurfaceData)) {104return false;105}106107// make a context current to the destination surface108OGLContext.validateContext((OGLSurfaceData)sData);109}110111// invoke the given runnable on the QFT112rq.flushAndInvokeNow(r);113114// invalidate the current context so that the next time we render115// with Java 2D, the context state will be completely revalidated116OGLContext.invalidateCurrentContext();117} finally {118rq.unlock();119}120121return true;122}123124/**125* Invokes the given Runnable on the OGL QueueFlusher thread with the126* "shared" OpenGL context (corresponding to the given127* GraphicsConfiguration object) made current. This method is typically128* used when the Runnable needs a current context to complete its129* operation, but does not require that the context be made current to130* a particular surface. For example, an application may call this131* method so that the given Runnable can query the OpenGL capabilities132* of the given GraphicsConfiguration, without making a context current133* to a dummy surface (or similar hacky techniques).134*135* In order to avoid deadlock, it is important that the given Runnable136* does not attempt to acquire the AWT lock, as that will be handled137* automatically as part of the <code>rq.flushAndInvokeNow()</code> step.138*139* @param config the GraphicsConfiguration object whose "shared"140* context will be made current during this operation; if this value is141* null or if OpenGL is not enabled for the GraphicsConfiguration, this142* method will return false143* @param r the action to be performed on the QFT; cannot be null144* @return true if the operation completed successfully, or false if145* there was any problem making the shared context current146*/147public static boolean148invokeWithOGLSharedContextCurrent(GraphicsConfiguration config,149Runnable r)150{151if (!(config instanceof OGLGraphicsConfig)) {152return false;153}154155OGLRenderQueue rq = OGLRenderQueue.getInstance();156rq.lock();157try {158// make the "shared" context current for the given GraphicsConfig159OGLContext.setScratchSurface((OGLGraphicsConfig)config);160161// invoke the given runnable on the QFT162rq.flushAndInvokeNow(r);163164// invalidate the current context so that the next time we render165// with Java 2D, the context state will be completely revalidated166OGLContext.invalidateCurrentContext();167} finally {168rq.unlock();169}170171return true;172}173174/**175* Returns the Rectangle describing the OpenGL viewport on the176* Java 2D surface associated with the given Graphics object and177* component width and height. When a third-party library is178* performing OpenGL rendering directly into the visible region of179* the associated surface, this viewport helps the application180* position the OpenGL output correctly on that surface.181*182* Note that the x/y values in the returned Rectangle object represent183* the lower-left corner of the viewport region, relative to the184* lower-left corner of the given surface.185*186* @param g the Graphics object for the corresponding destination surface;187* cannot be null188* @param componentWidth width of the component to be painted189* @param componentHeight height of the component to be painted190* @return a Rectangle describing the OpenGL viewport for the given191* destination surface and component dimensions, or null if the given192* Graphics object is invalid193*/194public static Rectangle getOGLViewport(Graphics g,195int componentWidth,196int componentHeight)197{198if (!(g instanceof SunGraphics2D)) {199return null;200}201202SunGraphics2D sg2d = (SunGraphics2D)g;203SurfaceData sData = (SurfaceData)sg2d.surfaceData;204205// this is the upper-left origin of the region to be painted,206// relative to the upper-left origin of the surface207// (in Java2D coordinates)208int x0 = sg2d.transX;209int y0 = sg2d.transY;210211// this is the lower-left origin of the region to be painted,212// relative to the lower-left origin of the surface213// (in OpenGL coordinates)214Rectangle surfaceBounds = sData.getBounds();215int x1 = x0;216int y1 = surfaceBounds.height - (y0 + componentHeight);217218return new Rectangle(x1, y1, componentWidth, componentHeight);219}220221/**222* Returns the Rectangle describing the OpenGL scissor box on the223* Java 2D surface associated with the given Graphics object. When a224* third-party library is performing OpenGL rendering directly225* into the visible region of the associated surface, this scissor box226* must be set to avoid drawing over existing rendering results.227*228* Note that the x/y values in the returned Rectangle object represent229* the lower-left corner of the scissor region, relative to the230* lower-left corner of the given surface.231*232* @param g the Graphics object for the corresponding destination surface;233* cannot be null234* @return a Rectangle describing the OpenGL scissor box for the given235* Graphics object and corresponding destination surface, or null if the236* given Graphics object is invalid or the clip region is non-rectangular237*/238public static Rectangle getOGLScissorBox(Graphics g) {239if (!(g instanceof SunGraphics2D)) {240return null;241}242243SunGraphics2D sg2d = (SunGraphics2D)g;244SurfaceData sData = (SurfaceData)sg2d.surfaceData;245Region r = sg2d.getCompClip();246if (!r.isRectangular()) {247// caller probably doesn't know how to handle shape clip248// appropriately, so just return null (Swing currently never249// sets a shape clip, but that could change in the future)250return null;251}252253// this is the upper-left origin of the scissor box relative to the254// upper-left origin of the surface (in Java 2D coordinates)255int x0 = r.getLoX();256int y0 = r.getLoY();257258// this is the width and height of the scissor region259int w = r.getWidth();260int h = r.getHeight();261262// this is the lower-left origin of the scissor box relative to the263// lower-left origin of the surface (in OpenGL coordinates)264Rectangle surfaceBounds = sData.getBounds();265int x1 = x0;266int y1 = surfaceBounds.height - (y0 + h);267268return new Rectangle(x1, y1, w, h);269}270271/**272* Returns an Object identifier for the Java 2D surface associated with273* the given Graphics object. This identifier may be used to determine274* whether the surface has changed since the last invocation of this275* operation, and thereby whether the OpenGL state corresponding to the276* old surface must be destroyed and recreated.277*278* @param g the Graphics object for the corresponding destination surface;279* cannot be null280* @return an identifier for the surface associated with the given281* Graphics object, or null if the given Graphics object is invalid282*/283public static Object getOGLSurfaceIdentifier(Graphics g) {284if (!(g instanceof SunGraphics2D)) {285return null;286}287return ((SunGraphics2D)g).surfaceData;288}289290/**291* Returns one of the OGL-specific surface type constants (defined in292* this class), which describes the surface associated with the given293* Graphics object.294*295* @param g the Graphics object for the corresponding destination surface;296* cannot be null297* @return a constant that describes the surface associated with the298* given Graphics object; if the given Graphics object is invalid (i.e.299* is not associated with an OpenGL surface) this method will return300* <code>OGLUtilities.UNDEFINED</code>301*/302public static int getOGLSurfaceType(Graphics g) {303if (!(g instanceof SunGraphics2D)) {304return UNDEFINED;305}306SurfaceData sData = ((SunGraphics2D)g).surfaceData;307if (!(sData instanceof OGLSurfaceData)) {308return UNDEFINED;309}310return ((OGLSurfaceData)sData).getType();311}312313/**314* Returns the OpenGL texture target constant (either GL_TEXTURE_2D315* or GL_TEXTURE_RECTANGLE_ARB) for the surface associated with the316* given Graphics object. This method is only useful for those surface317* types that are backed by an OpenGL texture, namely {@code TEXTURE},318* {@code FBOBJECT}, and (on Windows only) {@code PBUFFER}.319*320* @param g the Graphics object for the corresponding destination surface;321* cannot be null322* @return the texture target constant for the surface associated with the323* given Graphics object; if the given Graphics object is invalid (i.e.324* is not associated with an OpenGL surface), or the associated surface325* is not backed by an OpenGL texture, this method will return zero.326*/327public static int getOGLTextureType(Graphics g) {328if (!(g instanceof SunGraphics2D)) {329return 0;330}331SurfaceData sData = ((SunGraphics2D)g).surfaceData;332if (!(sData instanceof OGLSurfaceData)) {333return 0;334}335return ((OGLSurfaceData)sData).getTextureTarget();336}337}338339340