Path: blob/master/src/java.desktop/share/classes/sun/java2d/opengl/OGLContext.java
41159 views
/*1* Copyright (c) 2004, 2019, 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.lang.annotation.Native;2829import sun.java2d.pipe.BufferedContext;30import sun.java2d.pipe.RenderBuffer;31import sun.java2d.pipe.RenderQueue;32import sun.java2d.pipe.hw.ContextCapabilities;3334import static sun.java2d.pipe.BufferedOpCodes.INVALIDATE_CONTEXT;35import static sun.java2d.pipe.BufferedOpCodes.SET_SCRATCH_SURFACE;3637/**38* Note that the RenderQueue lock must be acquired before calling any of39* the methods in this class.40*/41final class OGLContext extends BufferedContext {4243OGLContext(RenderQueue rq) {44super(rq);45}4647/**48* Convenience method that delegates to setScratchSurface() below.49*/50static void setScratchSurface(OGLGraphicsConfig gc) {51setScratchSurface(gc.getNativeConfigInfo());52}5354/**55* Makes the given GraphicsConfig's context current to its associated56* "scratch surface". Each GraphicsConfig maintains a native context57* (GLXContext on Unix, HGLRC on Windows) as well as a native pbuffer58* known as the "scratch surface". By making the context current to the59* scratch surface, we are assured that we have a current context for60* the relevant GraphicsConfig, and can therefore perform operations61* depending on the capabilities of that GraphicsConfig. For example,62* if the GraphicsConfig supports the GL_ARB_texture_non_power_of_two63* extension, then we should be able to make a non-pow2 texture for this64* GraphicsConfig once we make the context current to the scratch surface.65*66* This method should be used for operations with an OpenGL texture67* as the destination surface (e.g. a sw->texture blit loop), or in those68* situations where we may not otherwise have a current context (e.g.69* when disposing a texture-based surface).70*/71static void setScratchSurface(long pConfigInfo) {72// assert OGLRenderQueue.getInstance().lock.isHeldByCurrentThread();7374// invalidate the current context75currentContext = null;7677// set the scratch context78OGLRenderQueue rq = OGLRenderQueue.getInstance();79RenderBuffer buf = rq.getBuffer();80rq.ensureCapacityAndAlignment(12, 4);81buf.putInt(SET_SCRATCH_SURFACE);82buf.putLong(pConfigInfo);83}8485/**86* Invalidates the currentContext field to ensure that we properly87* revalidate the OGLContext (make it current, etc.) next time through88* the validate() method. This is typically invoked from methods89* that affect the current context state (e.g. disposing a context or90* surface).91*/92static void invalidateCurrentContext() {93// assert OGLRenderQueue.getInstance().lock.isHeldByCurrentThread();9495// invalidate the current Java-level context so that we96// revalidate everything the next time around97if (currentContext != null) {98currentContext.invalidateContext();99currentContext = null;100}101102// invalidate the context reference at the native level, and103// then flush the queue so that we have no pending operations104// dependent on the current context105OGLRenderQueue rq = OGLRenderQueue.getInstance();106rq.ensureCapacity(4);107rq.getBuffer().putInt(INVALIDATE_CONTEXT);108rq.flushNow();109}110111/**112* Returns a string representing adapter id (vendor, renderer, version).113* Must be called on the rendering thread.114*115* @return an id string for the adapter116*/117static final native String getOGLIdString();118119static class OGLContextCaps extends ContextCapabilities {120/**121* Indicates the presence of the GL_EXT_framebuffer_object extension.122* This cap will only be set if the fbobject system property has been123* enabled and we are able to create an FBO with depth buffer.124*/125@Native126static final int CAPS_EXT_FBOBJECT =127(CAPS_RT_TEXTURE_ALPHA | CAPS_RT_TEXTURE_OPAQUE);128/** Indicates that the context is doublebuffered. */129@Native130static final int CAPS_DOUBLEBUFFERED = (FIRST_PRIVATE_CAP << 0);131/**132* Indicates the presence of the GL_ARB_fragment_shader extension.133* This cap will only be set if the lcdshader system property has been134* enabled and the hardware supports the minimum number of texture units135*/136@Native137static final int CAPS_EXT_LCD_SHADER = (FIRST_PRIVATE_CAP << 1);138/**139* Indicates the presence of the GL_ARB_fragment_shader extension.140* This cap will only be set if the biopshader system property has been141* enabled and the hardware meets our minimum requirements.142*/143@Native144static final int CAPS_EXT_BIOP_SHADER = (FIRST_PRIVATE_CAP << 2);145/**146* Indicates the presence of the GL_ARB_fragment_shader extension.147* This cap will only be set if the gradshader system property has been148* enabled and the hardware meets our minimum requirements.149*/150@Native151static final int CAPS_EXT_GRAD_SHADER = (FIRST_PRIVATE_CAP << 3);152/** Indicates the presence of the GL_ARB_texture_rectangle extension. */153@Native154static final int CAPS_EXT_TEXRECT = (FIRST_PRIVATE_CAP << 4);155/** Indicates the presence of the GL_NV_texture_barrier extension. */156@Native157static final int CAPS_EXT_TEXBARRIER = (FIRST_PRIVATE_CAP << 5);158159160OGLContextCaps(int caps, String adapterId) {161super(caps, adapterId);162}163164@Override165public String toString() {166StringBuilder sb = new StringBuilder(super.toString());167if ((caps & CAPS_EXT_FBOBJECT) != 0) {168sb.append("CAPS_EXT_FBOBJECT|");169}170if ((caps & CAPS_DOUBLEBUFFERED) != 0) {171sb.append("CAPS_DOUBLEBUFFERED|");172}173if ((caps & CAPS_EXT_LCD_SHADER) != 0) {174sb.append("CAPS_EXT_LCD_SHADER|");175}176if ((caps & CAPS_EXT_BIOP_SHADER) != 0) {177sb.append("CAPS_BIOP_SHADER|");178}179if ((caps & CAPS_EXT_GRAD_SHADER) != 0) {180sb.append("CAPS_EXT_GRAD_SHADER|");181}182if ((caps & CAPS_EXT_TEXRECT) != 0) {183sb.append("CAPS_EXT_TEXRECT|");184}185if ((caps & CAPS_EXT_TEXBARRIER) != 0) {186sb.append("CAPS_EXT_TEXBARRIER|");187}188return sb.toString();189}190}191}192193194