0N/A/*
2362N/A * Copyright (c) 2004, 2008, 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 sun.java2d.pipe.BufferedContext;
0N/Aimport sun.java2d.pipe.RenderBuffer;
0N/Aimport sun.java2d.pipe.RenderQueue;
430N/Aimport sun.java2d.pipe.hw.ContextCapabilities;
0N/Aimport static sun.java2d.pipe.BufferedOpCodes.*;
430N/Aimport static sun.java2d.pipe.hw.ContextCapabilities.*;
0N/A
0N/A/**
0N/A * Note that the RenderQueue lock must be acquired before calling any of
0N/A * the methods in this class.
0N/A */
430N/Apublic class OGLContext extends BufferedContext {
430N/A
430N/A private final OGLGraphicsConfig config;
0N/A
430N/A OGLContext(RenderQueue rq, OGLGraphicsConfig config) {
0N/A super(rq);
430N/A this.config = config;
0N/A }
0N/A
0N/A /**
0N/A * Convenience method that delegates to setScratchSurface() below.
0N/A */
0N/A static void setScratchSurface(OGLGraphicsConfig gc) {
0N/A setScratchSurface(gc.getNativeConfigInfo());
0N/A }
0N/A
0N/A /**
0N/A * Makes the given GraphicsConfig's context current to its associated
0N/A * "scratch surface". Each GraphicsConfig maintains a native context
0N/A * (GLXContext on Unix, HGLRC on Windows) as well as a native pbuffer
0N/A * known as the "scratch surface". By making the context current to the
0N/A * scratch surface, we are assured that we have a current context for
0N/A * the relevant GraphicsConfig, and can therefore perform operations
0N/A * depending on the capabilities of that GraphicsConfig. For example,
0N/A * if the GraphicsConfig supports the GL_ARB_texture_non_power_of_two
0N/A * extension, then we should be able to make a non-pow2 texture for this
0N/A * GraphicsConfig once we make the context current to the scratch surface.
0N/A *
0N/A * This method should be used for operations with an OpenGL texture
0N/A * as the destination surface (e.g. a sw->texture blit loop), or in those
0N/A * situations where we may not otherwise have a current context (e.g.
0N/A * when disposing a texture-based surface).
0N/A */
0N/A static void setScratchSurface(long pConfigInfo) {
0N/A // assert OGLRenderQueue.getInstance().lock.isHeldByCurrentThread();
0N/A
0N/A // invalidate the current context
0N/A currentContext = null;
0N/A
0N/A // set the scratch context
0N/A OGLRenderQueue rq = OGLRenderQueue.getInstance();
0N/A RenderBuffer buf = rq.getBuffer();
0N/A rq.ensureCapacityAndAlignment(12, 4);
0N/A buf.putInt(SET_SCRATCH_SURFACE);
0N/A buf.putLong(pConfigInfo);
0N/A }
0N/A
0N/A /**
0N/A * Invalidates the currentContext field to ensure that we properly
0N/A * revalidate the OGLContext (make it current, etc.) next time through
0N/A * the validate() method. This is typically invoked from methods
0N/A * that affect the current context state (e.g. disposing a context or
0N/A * surface).
0N/A */
0N/A static void invalidateCurrentContext() {
0N/A // assert OGLRenderQueue.getInstance().lock.isHeldByCurrentThread();
0N/A
430N/A // invalidate the current Java-level context so that we
430N/A // revalidate everything the next time around
430N/A if (currentContext != null) {
430N/A currentContext.invalidateContext();
430N/A currentContext = null;
430N/A }
430N/A
430N/A // invalidate the context reference at the native level, and
0N/A // then flush the queue so that we have no pending operations
0N/A // dependent on the current context
0N/A OGLRenderQueue rq = OGLRenderQueue.getInstance();
0N/A rq.ensureCapacity(4);
0N/A rq.getBuffer().putInt(INVALIDATE_CONTEXT);
0N/A rq.flushNow();
430N/A }
0N/A
430N/A public RenderQueue getRenderQueue() {
430N/A return OGLRenderQueue.getInstance();
430N/A }
430N/A
430N/A /**
430N/A * Returns a string representing adapter id (vendor, renderer, version).
430N/A * Must be called on the rendering thread.
430N/A *
430N/A * @return an id string for the adapter
430N/A */
430N/A static final native String getOGLIdString();
430N/A
430N/A @Override
430N/A public void saveState() {
430N/A // assert rq.lock.isHeldByCurrentThread();
430N/A
430N/A // reset all attributes of this and current contexts
430N/A invalidateContext();
430N/A invalidateCurrentContext();
430N/A
430N/A setScratchSurface(config);
430N/A
430N/A // save the state on the native level
430N/A rq.ensureCapacity(4);
430N/A buf.putInt(SAVE_STATE);
430N/A rq.flushNow();
430N/A }
430N/A
430N/A @Override
430N/A public void restoreState() {
430N/A // assert rq.lock.isHeldByCurrentThread();
430N/A
430N/A // reset all attributes of this and current contexts
430N/A invalidateContext();
430N/A invalidateCurrentContext();
430N/A
430N/A setScratchSurface(config);
430N/A
430N/A // restore the state on the native level
430N/A rq.ensureCapacity(4);
430N/A buf.putInt(RESTORE_STATE);
430N/A rq.flushNow();
430N/A }
430N/A
430N/A static class OGLContextCaps extends ContextCapabilities {
430N/A /**
430N/A * Indicates the presence of the GL_EXT_framebuffer_object extension.
430N/A * This cap will only be set if the fbobject system property has been
430N/A * enabled and we are able to create an FBO with depth buffer.
430N/A */
430N/A static final int CAPS_EXT_FBOBJECT =
430N/A (CAPS_RT_TEXTURE_ALPHA | CAPS_RT_TEXTURE_OPAQUE);
430N/A /** Indicates that the context supports a stored alpha channel. */
430N/A static final int CAPS_STORED_ALPHA = CAPS_RT_PLAIN_ALPHA;
430N/A /** Indicates that the context is doublebuffered. */
430N/A static final int CAPS_DOUBLEBUFFERED = (FIRST_PRIVATE_CAP << 0);
430N/A /**
430N/A * Indicates the presence of the GL_ARB_fragment_shader extension.
430N/A * This cap will only be set if the lcdshader system property has been
430N/A * enabled and the hardware supports the minimum number of texture units
430N/A */
430N/A static final int CAPS_EXT_LCD_SHADER = (FIRST_PRIVATE_CAP << 1);
430N/A /**
430N/A * Indicates the presence of the GL_ARB_fragment_shader extension.
430N/A * This cap will only be set if the biopshader system property has been
430N/A * enabled and the hardware meets our minimum requirements.
430N/A */
430N/A static final int CAPS_EXT_BIOP_SHADER = (FIRST_PRIVATE_CAP << 2);
430N/A /**
430N/A * Indicates the presence of the GL_ARB_fragment_shader extension.
430N/A * This cap will only be set if the gradshader system property has been
430N/A * enabled and the hardware meets our minimum requirements.
430N/A */
430N/A static final int CAPS_EXT_GRAD_SHADER = (FIRST_PRIVATE_CAP << 3);
430N/A /** Indicates the presence of the GL_ARB_texture_rectangle extension. */
430N/A static final int CAPS_EXT_TEXRECT = (FIRST_PRIVATE_CAP << 4);
430N/A
430N/A OGLContextCaps(int caps, String adapterId) {
430N/A super(caps, adapterId);
430N/A }
430N/A
430N/A @Override
430N/A public String toString() {
430N/A StringBuffer buf = new StringBuffer(super.toString());
430N/A if ((caps & CAPS_EXT_FBOBJECT) != 0) {
430N/A buf.append("CAPS_EXT_FBOBJECT|");
430N/A }
430N/A if ((caps & CAPS_STORED_ALPHA) != 0) {
430N/A buf.append("CAPS_STORED_ALPHA|");
430N/A }
430N/A if ((caps & CAPS_DOUBLEBUFFERED) != 0) {
430N/A buf.append("CAPS_DOUBLEBUFFERED|");
430N/A }
430N/A if ((caps & CAPS_EXT_LCD_SHADER) != 0) {
430N/A buf.append("CAPS_EXT_LCD_SHADER|");
430N/A }
430N/A if ((caps & CAPS_EXT_BIOP_SHADER) != 0) {
430N/A buf.append("CAPS_BIOP_SHADER|");
430N/A }
430N/A if ((caps & CAPS_EXT_GRAD_SHADER) != 0) {
430N/A buf.append("CAPS_EXT_GRAD_SHADER|");
430N/A }
430N/A if ((caps & CAPS_EXT_TEXRECT) != 0) {
430N/A buf.append("CAPS_EXT_TEXRECT|");
430N/A }
430N/A return buf.toString();
0N/A }
0N/A }
0N/A}