0N/A/*
2362N/A * Copyright (c) 2003, 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 java.awt.AWTException;
0N/Aimport java.awt.BufferCapabilities;
430N/Aimport java.awt.BufferCapabilities.FlipContents;
430N/Aimport java.awt.Color;
0N/Aimport java.awt.Component;
0N/Aimport java.awt.Graphics;
430N/Aimport java.awt.Graphics2D;
0N/Aimport java.awt.Image;
0N/Aimport java.awt.ImageCapabilities;
0N/Aimport java.awt.Transparency;
0N/Aimport java.awt.color.ColorSpace;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.awt.image.ColorModel;
0N/Aimport java.awt.image.DataBuffer;
0N/Aimport java.awt.image.DirectColorModel;
0N/Aimport java.awt.image.VolatileImage;
0N/Aimport java.awt.image.WritableRaster;
0N/Aimport sun.awt.X11ComponentPeer;
0N/Aimport sun.awt.X11GraphicsConfig;
0N/Aimport sun.awt.X11GraphicsDevice;
0N/Aimport sun.awt.X11GraphicsEnvironment;
0N/Aimport sun.awt.image.OffScreenImage;
0N/Aimport sun.awt.image.SunVolatileImage;
430N/Aimport sun.awt.image.SurfaceManager;
430N/Aimport sun.java2d.SunGraphics2D;
430N/Aimport sun.java2d.Surface;
0N/Aimport sun.java2d.SurfaceData;
430N/Aimport sun.java2d.pipe.hw.AccelSurface;
430N/Aimport sun.java2d.pipe.hw.AccelTypedVolatileImage;
430N/Aimport sun.java2d.pipe.hw.ContextCapabilities;
430N/Aimport static sun.java2d.opengl.OGLSurfaceData.*;
430N/Aimport static sun.java2d.opengl.OGLContext.*;
430N/Aimport static sun.java2d.opengl.OGLContext.OGLContextCaps.*;
430N/Aimport sun.java2d.opengl.GLXSurfaceData.GLXVSyncOffScreenSurfaceData;
430N/Aimport sun.java2d.pipe.hw.AccelDeviceEventListener;
430N/Aimport sun.java2d.pipe.hw.AccelDeviceEventNotifier;
0N/A
0N/Apublic class GLXGraphicsConfig
0N/A extends X11GraphicsConfig
0N/A implements OGLGraphicsConfig
0N/A{
0N/A private static ImageCapabilities imageCaps = new GLXImageCaps();
0N/A private BufferCapabilities bufferCaps;
0N/A private long pConfigInfo;
430N/A private ContextCapabilities oglCaps;
0N/A private OGLContext context;
0N/A
0N/A private static native long getGLXConfigInfo(int screennum, int visualnum);
0N/A private static native int getOGLCapabilities(long configInfo);
0N/A private native void initConfig(long aData, long ctxinfo);
0N/A
0N/A private GLXGraphicsConfig(X11GraphicsDevice device, int visualnum,
430N/A long configInfo, ContextCapabilities oglCaps)
0N/A {
0N/A super(device, visualnum, 0, 0,
430N/A (oglCaps.getCaps() & CAPS_DOUBLEBUFFERED) != 0);
0N/A pConfigInfo = configInfo;
0N/A initConfig(getAData(), configInfo);
0N/A this.oglCaps = oglCaps;
430N/A context = new OGLContext(OGLRenderQueue.getInstance(), this);
0N/A }
0N/A
430N/A @Override
0N/A public Object getProxyKey() {
0N/A return this;
0N/A }
0N/A
430N/A @Override
0N/A public SurfaceData createManagedSurface(int w, int h, int transparency) {
0N/A return GLXSurfaceData.createData(this, w, h,
0N/A getColorModel(transparency),
0N/A null,
0N/A OGLSurfaceData.TEXTURE);
0N/A }
0N/A
0N/A public static GLXGraphicsConfig getConfig(X11GraphicsDevice device,
0N/A int visualnum)
0N/A {
0N/A if (!X11GraphicsEnvironment.isGLXAvailable()) {
0N/A return null;
0N/A }
0N/A
0N/A long cfginfo = 0;
430N/A final String ids[] = new String[1];
0N/A OGLRenderQueue rq = OGLRenderQueue.getInstance();
0N/A rq.lock();
0N/A try {
0N/A // getGLXConfigInfo() creates and destroys temporary
0N/A // surfaces/contexts, so we should first invalidate the current
0N/A // Java-level context and flush the queue...
0N/A OGLContext.invalidateCurrentContext();
0N/A GLXGetConfigInfo action =
0N/A new GLXGetConfigInfo(device.getScreen(), visualnum);
0N/A rq.flushAndInvokeNow(action);
0N/A cfginfo = action.getConfigInfo();
766N/A if (cfginfo != 0L) {
766N/A OGLContext.setScratchSurface(cfginfo);
766N/A rq.flushAndInvokeNow(new Runnable() {
766N/A public void run() {
766N/A ids[0] = OGLContext.getOGLIdString();
766N/A }
766N/A });
766N/A }
0N/A } finally {
0N/A rq.unlock();
0N/A }
0N/A if (cfginfo == 0) {
0N/A return null;
0N/A }
0N/A
0N/A int oglCaps = getOGLCapabilities(cfginfo);
430N/A ContextCapabilities caps = new OGLContextCaps(oglCaps, ids[0]);
0N/A
430N/A return new GLXGraphicsConfig(device, visualnum, cfginfo, caps);
0N/A }
0N/A
0N/A /**
0N/A * This is a small helper class that allows us to execute
0N/A * getGLXConfigInfo() on the queue flushing thread.
0N/A */
0N/A private static class GLXGetConfigInfo implements Runnable {
0N/A private int screen;
0N/A private int visual;
0N/A private long cfginfo;
0N/A private GLXGetConfigInfo(int screen, int visual) {
0N/A this.screen = screen;
0N/A this.visual = visual;
0N/A }
0N/A public void run() {
0N/A cfginfo = getGLXConfigInfo(screen, visual);
0N/A }
0N/A public long getConfigInfo() {
0N/A return cfginfo;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the provided capability bit is present for this config.
0N/A * See OGLContext.java for a list of supported capabilities.
0N/A */
430N/A @Override
0N/A public final boolean isCapPresent(int cap) {
430N/A return ((oglCaps.getCaps() & cap) != 0);
0N/A }
0N/A
430N/A @Override
0N/A public final long getNativeConfigInfo() {
0N/A return pConfigInfo;
0N/A }
0N/A
430N/A /**
430N/A * {@inheritDoc}
430N/A *
430N/A * @see sun.java2d.pipe.hw.BufferedContextProvider#getContext
430N/A */
430N/A @Override
0N/A public final OGLContext getContext() {
0N/A return context;
0N/A }
0N/A
0N/A @Override
0N/A public BufferedImage createCompatibleImage(int width, int height) {
0N/A ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
0N/A WritableRaster
0N/A raster = model.createCompatibleWritableRaster(width, height);
0N/A return new BufferedImage(model, raster, model.isAlphaPremultiplied(),
0N/A null);
0N/A }
0N/A
0N/A @Override
0N/A public ColorModel getColorModel(int transparency) {
0N/A switch (transparency) {
0N/A case Transparency.OPAQUE:
0N/A // REMIND: once the ColorModel spec is changed, this should be
0N/A // an opaque premultiplied DCM...
0N/A return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
0N/A case Transparency.BITMASK:
0N/A return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
0N/A case Transparency.TRANSLUCENT:
0N/A ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
0N/A return new DirectColorModel(cs, 32,
0N/A 0xff0000, 0xff00, 0xff, 0xff000000,
0N/A true, DataBuffer.TYPE_INT);
0N/A default:
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public String toString() {
0N/A return ("GLXGraphicsConfig[dev="+screen+
0N/A ",vis=0x"+Integer.toHexString(visual)+
0N/A "]");
0N/A }
0N/A
0N/A /**
0N/A * The following methods are invoked from MToolkit or XToolkit.java and
0N/A * X11ComponentPeer.java rather than having the X11-dependent
0N/A * implementations hardcoded in those classes. This way the appropriate
0N/A * actions are taken based on the peer's GraphicsConfig, whether it is
0N/A * an X11GraphicsConfig or a GLXGraphicsConfig.
0N/A */
0N/A
0N/A /**
0N/A * Creates a new SurfaceData that will be associated with the given
0N/A * X11ComponentPeer.
0N/A */
0N/A @Override
0N/A public SurfaceData createSurfaceData(X11ComponentPeer peer) {
0N/A return GLXSurfaceData.createData(peer);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new hidden-acceleration image of the given width and height
0N/A * that is associated with the target Component.
0N/A */
0N/A @Override
0N/A public Image createAcceleratedImage(Component target,
0N/A int width, int height)
0N/A {
0N/A ColorModel model = getColorModel(Transparency.OPAQUE);
0N/A WritableRaster wr =
0N/A model.createCompatibleWritableRaster(width, height);
0N/A return new OffScreenImage(target, model, wr,
0N/A model.isAlphaPremultiplied());
0N/A }
0N/A
0N/A /**
0N/A * The following methods correspond to the multibuffering methods in
0N/A * X11ComponentPeer.java...
0N/A */
0N/A
0N/A /**
0N/A * Attempts to create a GLX-based backbuffer for the given peer. If
0N/A * the requested configuration is not natively supported, an AWTException
0N/A * is thrown. Otherwise, if the backbuffer creation is successful, a
0N/A * value of 1 is returned.
0N/A */
0N/A @Override
0N/A public long createBackBuffer(X11ComponentPeer peer,
0N/A int numBuffers, BufferCapabilities caps)
0N/A throws AWTException
0N/A {
0N/A if (numBuffers > 2) {
0N/A throw new AWTException(
0N/A "Only double or single buffering is supported");
0N/A }
0N/A BufferCapabilities configCaps = getBufferCapabilities();
0N/A if (!configCaps.isPageFlipping()) {
0N/A throw new AWTException("Page flipping is not supported");
0N/A }
0N/A if (caps.getFlipContents() == BufferCapabilities.FlipContents.PRIOR) {
0N/A throw new AWTException("FlipContents.PRIOR is not supported");
0N/A }
0N/A
0N/A // non-zero return value means backbuffer creation was successful
0N/A // (checked in X11ComponentPeer.flip(), etc.)
0N/A return 1;
0N/A }
0N/A
0N/A /**
0N/A * Destroys the backbuffer object represented by the given handle value.
0N/A */
0N/A @Override
0N/A public void destroyBackBuffer(long backBuffer) {
0N/A }
0N/A
0N/A /**
0N/A * Creates a VolatileImage that essentially wraps the target Component's
0N/A * backbuffer (the provided backbuffer handle is essentially ignored).
0N/A */
0N/A @Override
0N/A public VolatileImage createBackBufferImage(Component target,
0N/A long backBuffer)
0N/A {
0N/A return new SunVolatileImage(target,
0N/A target.getWidth(), target.getHeight(),
0N/A Boolean.TRUE);
0N/A }
0N/A
0N/A /**
0N/A * Performs the native GLX flip operation for the given target Component.
0N/A */
0N/A @Override
0N/A public void flip(X11ComponentPeer peer,
0N/A Component target, VolatileImage xBackBuffer,
430N/A int x1, int y1, int x2, int y2,
0N/A BufferCapabilities.FlipContents flipAction)
0N/A {
0N/A if (flipAction == BufferCapabilities.FlipContents.COPIED) {
430N/A SurfaceManager vsm = SurfaceManager.getManager(xBackBuffer);
430N/A SurfaceData sd = vsm.getPrimarySurfaceData();
430N/A
430N/A if (sd instanceof GLXVSyncOffScreenSurfaceData) {
430N/A GLXVSyncOffScreenSurfaceData vsd =
430N/A (GLXVSyncOffScreenSurfaceData)sd;
430N/A SurfaceData bbsd = vsd.getFlipSurface();
430N/A Graphics2D bbg =
430N/A new SunGraphics2D(bbsd, Color.black, Color.white, null);
430N/A try {
430N/A bbg.drawImage(xBackBuffer, 0, 0, null);
430N/A } finally {
430N/A bbg.dispose();
430N/A }
430N/A } else {
430N/A Graphics g = peer.getGraphics();
430N/A try {
430N/A g.drawImage(xBackBuffer,
430N/A x1, y1, x2, y2,
430N/A x1, y1, x2, y2,
430N/A null);
430N/A } finally {
430N/A g.dispose();
430N/A }
430N/A return;
0N/A }
0N/A } else if (flipAction == BufferCapabilities.FlipContents.PRIOR) {
0N/A // not supported by GLX...
0N/A return;
0N/A }
0N/A
0N/A OGLSurfaceData.swapBuffers(peer.getContentWindow());
0N/A
0N/A if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
0N/A Graphics g = xBackBuffer.getGraphics();
0N/A try {
0N/A g.setColor(target.getBackground());
0N/A g.fillRect(0, 0,
0N/A xBackBuffer.getWidth(),
0N/A xBackBuffer.getHeight());
0N/A } finally {
0N/A g.dispose();
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static class GLXBufferCaps extends BufferCapabilities {
0N/A public GLXBufferCaps(boolean dblBuf) {
0N/A super(imageCaps, imageCaps,
0N/A dblBuf ? FlipContents.UNDEFINED : null);
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public BufferCapabilities getBufferCapabilities() {
0N/A if (bufferCaps == null) {
0N/A bufferCaps = new GLXBufferCaps(isDoubleBuffered());
0N/A }
0N/A return bufferCaps;
0N/A }
0N/A
0N/A private static class GLXImageCaps extends ImageCapabilities {
0N/A private GLXImageCaps() {
0N/A super(true);
0N/A }
0N/A public boolean isTrueVolatile() {
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public ImageCapabilities getImageCapabilities() {
0N/A return imageCaps;
0N/A }
430N/A
430N/A /**
430N/A * {@inheritDoc}
430N/A *
430N/A * @see sun.java2d.pipe.hw.AccelGraphicsConfig#createCompatibleVolatileImage
430N/A */
430N/A @Override
430N/A public VolatileImage
430N/A createCompatibleVolatileImage(int width, int height,
430N/A int transparency, int type)
430N/A {
430N/A if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||
430N/A transparency == Transparency.BITMASK)
430N/A {
430N/A return null;
430N/A }
430N/A
430N/A if (type == FBOBJECT) {
430N/A if (!isCapPresent(CAPS_EXT_FBOBJECT)) {
430N/A return null;
430N/A }
430N/A } else if (type == PBUFFER) {
430N/A boolean isOpaque = transparency == Transparency.OPAQUE;
430N/A if (!isOpaque && !isCapPresent(CAPS_STORED_ALPHA)) {
430N/A return null;
430N/A }
430N/A }
430N/A
430N/A SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
430N/A transparency, type);
430N/A Surface sd = vi.getDestSurface();
430N/A if (!(sd instanceof AccelSurface) ||
430N/A ((AccelSurface)sd).getType() != type)
430N/A {
430N/A vi.flush();
430N/A vi = null;
430N/A }
430N/A
430N/A return vi;
430N/A }
430N/A
430N/A /**
430N/A * {@inheritDoc}
430N/A *
430N/A * @see sun.java2d.pipe.hw.AccelGraphicsConfig#getContextCapabilities
430N/A */
430N/A @Override
430N/A public ContextCapabilities getContextCapabilities() {
430N/A return oglCaps;
430N/A }
430N/A
430N/A @Override
430N/A public void addDeviceEventListener(AccelDeviceEventListener l) {
430N/A AccelDeviceEventNotifier.addListener(l, screen.getScreen());
430N/A }
430N/A
430N/A @Override
430N/A public void removeDeviceEventListener(AccelDeviceEventListener l) {
430N/A AccelDeviceEventNotifier.removeListener(l);
430N/A }
0N/A}