Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/opengl/OGLContext.java
38918 views
/*1* Copyright (c) 2004, 2013, 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 sun.java2d.pipe.BufferedContext;28import sun.java2d.pipe.RenderBuffer;29import sun.java2d.pipe.RenderQueue;30import sun.java2d.pipe.hw.ContextCapabilities;31import static sun.java2d.pipe.BufferedOpCodes.*;32import static sun.java2d.pipe.hw.ContextCapabilities.*;3334import java.lang.annotation.Native;3536/**37* Note that the RenderQueue lock must be acquired before calling any of38* the methods in this class.39*/40public class OGLContext extends BufferedContext {4142private final OGLGraphicsConfig config;4344OGLContext(RenderQueue rq, OGLGraphicsConfig config) {45super(rq);46this.config = config;47}4849/**50* Convenience method that delegates to setScratchSurface() below.51*/52static void setScratchSurface(OGLGraphicsConfig gc) {53setScratchSurface(gc.getNativeConfigInfo());54}5556/**57* Makes the given GraphicsConfig's context current to its associated58* "scratch surface". Each GraphicsConfig maintains a native context59* (GLXContext on Unix, HGLRC on Windows) as well as a native pbuffer60* known as the "scratch surface". By making the context current to the61* scratch surface, we are assured that we have a current context for62* the relevant GraphicsConfig, and can therefore perform operations63* depending on the capabilities of that GraphicsConfig. For example,64* if the GraphicsConfig supports the GL_ARB_texture_non_power_of_two65* extension, then we should be able to make a non-pow2 texture for this66* GraphicsConfig once we make the context current to the scratch surface.67*68* This method should be used for operations with an OpenGL texture69* as the destination surface (e.g. a sw->texture blit loop), or in those70* situations where we may not otherwise have a current context (e.g.71* when disposing a texture-based surface).72*/73static void setScratchSurface(long pConfigInfo) {74// assert OGLRenderQueue.getInstance().lock.isHeldByCurrentThread();7576// invalidate the current context77currentContext = null;7879// set the scratch context80OGLRenderQueue rq = OGLRenderQueue.getInstance();81RenderBuffer buf = rq.getBuffer();82rq.ensureCapacityAndAlignment(12, 4);83buf.putInt(SET_SCRATCH_SURFACE);84buf.putLong(pConfigInfo);85}8687/**88* Invalidates the currentContext field to ensure that we properly89* revalidate the OGLContext (make it current, etc.) next time through90* the validate() method. This is typically invoked from methods91* that affect the current context state (e.g. disposing a context or92* surface).93*/94static void invalidateCurrentContext() {95// assert OGLRenderQueue.getInstance().lock.isHeldByCurrentThread();9697// invalidate the current Java-level context so that we98// revalidate everything the next time around99if (currentContext != null) {100currentContext.invalidateContext();101currentContext = null;102}103104// invalidate the context reference at the native level, and105// then flush the queue so that we have no pending operations106// dependent on the current context107OGLRenderQueue rq = OGLRenderQueue.getInstance();108rq.ensureCapacity(4);109rq.getBuffer().putInt(INVALIDATE_CONTEXT);110rq.flushNow();111}112113public RenderQueue getRenderQueue() {114return OGLRenderQueue.getInstance();115}116117/**118* Returns a string representing adapter id (vendor, renderer, version).119* Must be called on the rendering thread.120*121* @return an id string for the adapter122*/123static final native String getOGLIdString();124125@Override126public void saveState() {127// assert rq.lock.isHeldByCurrentThread();128129// reset all attributes of this and current contexts130invalidateContext();131invalidateCurrentContext();132133setScratchSurface(config);134135// save the state on the native level136rq.ensureCapacity(4);137buf.putInt(SAVE_STATE);138rq.flushNow();139}140141@Override142public void restoreState() {143// assert rq.lock.isHeldByCurrentThread();144145// reset all attributes of this and current contexts146invalidateContext();147invalidateCurrentContext();148149setScratchSurface(config);150151// restore the state on the native level152rq.ensureCapacity(4);153buf.putInt(RESTORE_STATE);154rq.flushNow();155}156157static class OGLContextCaps extends ContextCapabilities {158/**159* Indicates the presence of the GL_EXT_framebuffer_object extension.160* This cap will only be set if the fbobject system property has been161* enabled and we are able to create an FBO with depth buffer.162*/163@Native164static final int CAPS_EXT_FBOBJECT =165(CAPS_RT_TEXTURE_ALPHA | CAPS_RT_TEXTURE_OPAQUE);166/** Indicates that the context supports a stored alpha channel. */167@Native168static final int CAPS_STORED_ALPHA = CAPS_RT_PLAIN_ALPHA;169/** Indicates that the context is doublebuffered. */170@Native171static final int CAPS_DOUBLEBUFFERED = (FIRST_PRIVATE_CAP << 0);172/**173* Indicates the presence of the GL_ARB_fragment_shader extension.174* This cap will only be set if the lcdshader system property has been175* enabled and the hardware supports the minimum number of texture units176*/177@Native178static final int CAPS_EXT_LCD_SHADER = (FIRST_PRIVATE_CAP << 1);179/**180* Indicates the presence of the GL_ARB_fragment_shader extension.181* This cap will only be set if the biopshader system property has been182* enabled and the hardware meets our minimum requirements.183*/184@Native185static final int CAPS_EXT_BIOP_SHADER = (FIRST_PRIVATE_CAP << 2);186/**187* Indicates the presence of the GL_ARB_fragment_shader extension.188* This cap will only be set if the gradshader system property has been189* enabled and the hardware meets our minimum requirements.190*/191@Native192static final int CAPS_EXT_GRAD_SHADER = (FIRST_PRIVATE_CAP << 3);193/** Indicates the presence of the GL_ARB_texture_rectangle extension. */194@Native195static final int CAPS_EXT_TEXRECT = (FIRST_PRIVATE_CAP << 4);196/** Indicates the presence of the GL_NV_texture_barrier extension. */197@Native198static final int CAPS_EXT_TEXBARRIER = (FIRST_PRIVATE_CAP << 5);199200201OGLContextCaps(int caps, String adapterId) {202super(caps, adapterId);203}204205@Override206public String toString() {207StringBuffer buf = new StringBuffer(super.toString());208if ((caps & CAPS_EXT_FBOBJECT) != 0) {209buf.append("CAPS_EXT_FBOBJECT|");210}211if ((caps & CAPS_STORED_ALPHA) != 0) {212buf.append("CAPS_STORED_ALPHA|");213}214if ((caps & CAPS_DOUBLEBUFFERED) != 0) {215buf.append("CAPS_DOUBLEBUFFERED|");216}217if ((caps & CAPS_EXT_LCD_SHADER) != 0) {218buf.append("CAPS_EXT_LCD_SHADER|");219}220if ((caps & CAPS_EXT_BIOP_SHADER) != 0) {221buf.append("CAPS_BIOP_SHADER|");222}223if ((caps & CAPS_EXT_GRAD_SHADER) != 0) {224buf.append("CAPS_EXT_GRAD_SHADER|");225}226if ((caps & CAPS_EXT_TEXRECT) != 0) {227buf.append("CAPS_EXT_TEXRECT|");228}229if ((caps & CAPS_EXT_TEXBARRIER) != 0) {230buf.append("CAPS_EXT_TEXBARRIER|");231}232return buf.toString();233}234}235}236237238