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 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.ImageCapabilities;
0N/Aimport java.awt.Transparency;
0N/Aimport java.awt.color.ColorSpace;
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 sun.awt.Win32GraphicsConfig;
0N/Aimport sun.awt.Win32GraphicsDevice;
0N/Aimport sun.awt.image.SunVolatileImage;
430N/Aimport sun.awt.image.SurfaceManager;
0N/Aimport sun.awt.windows.WComponentPeer;
0N/Aimport sun.java2d.Disposer;
0N/Aimport sun.java2d.DisposerRecord;
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.OGLContext.OGLContextCaps.*;
430N/Aimport static sun.java2d.opengl.WGLSurfaceData.*;
430N/Aimport sun.java2d.opengl.OGLContext.OGLContextCaps;
430N/Aimport sun.java2d.pipe.hw.AccelDeviceEventListener;
430N/Aimport sun.java2d.pipe.hw.AccelDeviceEventNotifier;
430N/Aimport sun.java2d.windows.GDIWindowSurfaceData;
0N/A
0N/Apublic class WGLGraphicsConfig
0N/A extends Win32GraphicsConfig
0N/A implements OGLGraphicsConfig
0N/A{
0N/A protected static boolean wglAvailable;
0N/A private static ImageCapabilities imageCaps = new WGLImageCaps();
0N/A
0N/A private BufferCapabilities bufferCaps;
0N/A private long pConfigInfo;
430N/A private ContextCapabilities oglCaps;
0N/A private OGLContext context;
0N/A private Object disposerReferent = new Object();
0N/A
0N/A public static native int getDefaultPixFmt(int screennum);
0N/A private static native boolean initWGL();
0N/A private static native long getWGLConfigInfo(int screennum, int visualnum);
0N/A private static native int getOGLCapabilities(long configInfo);
0N/A
0N/A static {
0N/A wglAvailable = initWGL();
0N/A }
0N/A
0N/A protected WGLGraphicsConfig(Win32GraphicsDevice device, int visualnum,
430N/A long configInfo, ContextCapabilities oglCaps)
0N/A {
0N/A super(device, visualnum);
0N/A this.pConfigInfo = configInfo;
0N/A this.oglCaps = oglCaps;
430N/A context = new OGLContext(OGLRenderQueue.getInstance(), this);
0N/A
0N/A // add a record to the Disposer so that we destroy the native
0N/A // WGLGraphicsConfigInfo data when this object goes away
0N/A Disposer.addRecord(disposerReferent,
430N/A new WGLGCDisposerRecord(pConfigInfo,
430N/A device.getScreen()));
0N/A }
0N/A
0N/A public Object getProxyKey() {
0N/A return this;
0N/A }
0N/A
0N/A public SurfaceData createManagedSurface(int w, int h, int transparency) {
0N/A return WGLSurfaceData.createData(this, w, h,
0N/A getColorModel(transparency),
0N/A null,
0N/A OGLSurfaceData.TEXTURE);
0N/A }
0N/A
0N/A public static WGLGraphicsConfig getConfig(Win32GraphicsDevice device,
0N/A int pixfmt)
0N/A {
0N/A if (!wglAvailable) {
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 // getWGLConfigInfo() 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 WGLGetConfigInfo action =
0N/A new WGLGetConfigInfo(device.getScreen(), pixfmt);
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 WGLGraphicsConfig(device, pixfmt, cfginfo, caps);
0N/A }
0N/A
0N/A /**
0N/A * This is a small helper class that allows us to execute
0N/A * getWGLConfigInfo() on the queue flushing thread.
0N/A */
0N/A private static class WGLGetConfigInfo implements Runnable {
0N/A private int screen;
0N/A private int pixfmt;
0N/A private long cfginfo;
0N/A private WGLGetConfigInfo(int screen, int pixfmt) {
0N/A this.screen = screen;
0N/A this.pixfmt = pixfmt;
0N/A }
0N/A public void run() {
0N/A cfginfo = getWGLConfigInfo(screen, pixfmt);
0N/A }
0N/A public long getConfigInfo() {
0N/A return cfginfo;
0N/A }
0N/A }
0N/A
0N/A public static boolean isWGLAvailable() {
0N/A return wglAvailable;
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 private static class WGLGCDisposerRecord implements DisposerRecord {
0N/A private long pCfgInfo;
430N/A private int screen;
430N/A public WGLGCDisposerRecord(long pCfgInfo, int screen) {
0N/A this.pCfgInfo = pCfgInfo;
0N/A }
0N/A public void dispose() {
430N/A OGLRenderQueue rq = OGLRenderQueue.getInstance();
430N/A rq.lock();
430N/A try {
430N/A rq.flushAndInvokeNow(new Runnable() {
430N/A public void run() {
430N/A AccelDeviceEventNotifier.
430N/A eventOccured(screen,
430N/A AccelDeviceEventNotifier.DEVICE_RESET);
430N/A AccelDeviceEventNotifier.
430N/A eventOccured(screen,
430N/A AccelDeviceEventNotifier.DEVICE_DISPOSED);
430N/A }
430N/A });
430N/A } finally {
430N/A rq.unlock();
430N/A }
0N/A if (pCfgInfo != 0) {
0N/A OGLRenderQueue.disposeGraphicsConfig(pCfgInfo);
0N/A pCfgInfo = 0;
0N/A }
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public synchronized void displayChanged() {
0N/A super.displayChanged();
0N/A // the context could hold a reference to a WGLSurfaceData, which in
0N/A // turn has a reference back to this WGLGraphicsConfig, so in order
0N/A // for this instance to be disposed we need to break the connection
0N/A OGLRenderQueue rq = OGLRenderQueue.getInstance();
0N/A rq.lock();
0N/A try {
0N/A OGLContext.invalidateCurrentContext();
0N/A } finally {
0N/A rq.unlock();
0N/A }
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 @Override
0N/A public String toString() {
0N/A return ("WGLGraphicsConfig[dev="+screen+",pixfmt="+visual+"]");
0N/A }
0N/A
0N/A /**
0N/A * The following methods are invoked from WComponentPeer.java rather
0N/A * than having the Win32-dependent implementations hardcoded in that
0N/A * class. This way the appropriate actions are taken based on the peer's
0N/A * GraphicsConfig, whether it is a Win32GraphicsConfig or a
0N/A * WGLGraphicsConfig.
0N/A */
0N/A
0N/A /**
0N/A * Creates a new SurfaceData that will be associated with the given
0N/A * WComponentPeer.
0N/A */
0N/A @Override
0N/A public SurfaceData createSurfaceData(WComponentPeer peer,
0N/A int numBackBuffers)
0N/A {
430N/A SurfaceData sd = WGLSurfaceData.createData(peer);
430N/A if (sd == null) {
430N/A sd = GDIWindowSurfaceData.createData(peer);
430N/A }
430N/A return sd;
0N/A }
0N/A
0N/A /**
0N/A * The following methods correspond to the multibuffering methods in
0N/A * WComponentPeer.java...
0N/A */
0N/A
0N/A /**
0N/A * Checks that the requested configuration is natively supported; if not,
0N/A * an AWTException is thrown.
0N/A */
0N/A @Override
0N/A public void assertOperationSupported(Component target,
0N/A int numBuffers,
0N/A 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
0N/A /**
0N/A * Creates a WGL-based backbuffer for the given peer and returns the
0N/A * image wrapper.
0N/A */
0N/A @Override
0N/A public VolatileImage createBackBuffer(WComponentPeer peer) {
0N/A Component target = (Component)peer.getTarget();
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 WGL flip operation for the given target Component.
0N/A */
0N/A @Override
0N/A public void flip(WComponentPeer peer,
0N/A Component target, VolatileImage backBuffer,
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(backBuffer);
430N/A SurfaceData sd = vsm.getPrimarySurfaceData();
430N/A
430N/A if (sd instanceof WGLVSyncOffScreenSurfaceData) {
430N/A WGLVSyncOffScreenSurfaceData vsd =
430N/A (WGLVSyncOffScreenSurfaceData)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(backBuffer, 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(backBuffer,
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 WGL...
0N/A return;
0N/A }
0N/A
0N/A OGLSurfaceData.swapBuffers(peer.getData());
0N/A
0N/A if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
0N/A Graphics g = backBuffer.getGraphics();
0N/A try {
0N/A g.setColor(target.getBackground());
0N/A g.fillRect(0, 0,
0N/A backBuffer.getWidth(),
0N/A backBuffer.getHeight());
0N/A } finally {
0N/A g.dispose();
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static class WGLBufferCaps extends BufferCapabilities {
0N/A public WGLBufferCaps(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) {
430N/A boolean dblBuf = isCapPresent(CAPS_DOUBLEBUFFERED);
0N/A bufferCaps = new WGLBufferCaps(dblBuf);
0N/A }
0N/A return bufferCaps;
0N/A }
0N/A
0N/A private static class WGLImageCaps extends ImageCapabilities {
0N/A private WGLImageCaps() {
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}