4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/Apackage sun.java2d.opengl;
4632N/A
4632N/Aimport java.awt.AWTException;
4632N/Aimport java.awt.BufferCapabilities;
4632N/Aimport java.awt.Color;
4632N/Aimport java.awt.Component;
4632N/Aimport java.awt.Graphics;
4632N/Aimport java.awt.Graphics2D;
5276N/Aimport java.awt.GraphicsConfiguration;
5276N/Aimport java.awt.GraphicsDevice;
5276N/Aimport java.awt.GraphicsEnvironment;
4632N/Aimport java.awt.Image;
4632N/Aimport java.awt.ImageCapabilities;
5276N/Aimport java.awt.Rectangle;
4632N/Aimport java.awt.Transparency;
4632N/Aimport java.awt.color.ColorSpace;
4632N/Aimport java.awt.image.BufferedImage;
4632N/Aimport java.awt.image.ColorModel;
4632N/Aimport java.awt.image.DataBuffer;
4632N/Aimport java.awt.image.DirectColorModel;
4632N/Aimport java.awt.image.VolatileImage;
4632N/Aimport java.awt.image.WritableRaster;
4632N/A
4632N/Aimport sun.awt.CGraphicsConfig;
4632N/Aimport sun.awt.CGraphicsDevice;
5276N/Aimport sun.awt.TextureSizeConstraining;
4632N/Aimport sun.awt.image.OffScreenImage;
4632N/Aimport sun.awt.image.SunVolatileImage;
4632N/Aimport sun.awt.image.SurfaceManager;
4632N/Aimport sun.java2d.Disposer;
4632N/Aimport sun.java2d.DisposerRecord;
4632N/Aimport sun.java2d.SunGraphics2D;
4632N/Aimport sun.java2d.Surface;
4632N/Aimport sun.java2d.SurfaceData;
4632N/Aimport sun.java2d.opengl.OGLContext.OGLContextCaps;
4632N/Aimport sun.java2d.pipe.hw.AccelSurface;
4632N/Aimport sun.java2d.pipe.hw.AccelTypedVolatileImage;
4632N/Aimport sun.java2d.pipe.hw.ContextCapabilities;
4632N/Aimport static sun.java2d.opengl.OGLSurfaceData.*;
4632N/Aimport static sun.java2d.opengl.OGLContext.OGLContextCaps.*;
4632N/Aimport sun.java2d.opengl.CGLSurfaceData.CGLVSyncOffScreenSurfaceData;
4632N/Aimport sun.java2d.pipe.hw.AccelDeviceEventListener;
4632N/Aimport sun.java2d.pipe.hw.AccelDeviceEventNotifier;
4632N/A
4632N/Aimport sun.lwawt.macosx.CPlatformView;
4632N/A
4632N/Apublic class CGLGraphicsConfig extends CGraphicsConfig
5276N/A implements OGLGraphicsConfig, TextureSizeConstraining
4632N/A{
4632N/A //private static final int kOpenGLSwapInterval = RuntimeOptions.getCurrentOptions().OpenGLSwapInterval;
4632N/A private static final int kOpenGLSwapInterval = 0; // TODO
4632N/A protected static boolean cglAvailable;
4632N/A private static ImageCapabilities imageCaps = new CGLImageCaps();
4632N/A
4632N/A private int pixfmt;
4632N/A private BufferCapabilities bufferCaps;
4632N/A private long pConfigInfo;
4632N/A private ContextCapabilities oglCaps;
4632N/A private OGLContext context;
4632N/A private Object disposerReferent = new Object();
4632N/A
5366N/A private final int cachedMaxTextureSize;
4632N/A private static native boolean initCGL();
6062N/A private static native long getCGLConfigInfo(int displayID, int visualnum,
4632N/A int swapInterval);
4632N/A private static native int getOGLCapabilities(long configInfo);
5366N/A private static native int _getMaxTextureSize();
4632N/A
4632N/A static {
4632N/A cglAvailable = initCGL();
4632N/A }
4632N/A
4632N/A protected CGLGraphicsConfig(CGraphicsDevice device, int pixfmt,
4632N/A long configInfo, ContextCapabilities oglCaps)
4632N/A {
4632N/A super(device);
4632N/A
4632N/A this.pixfmt = pixfmt;
4632N/A this.pConfigInfo = configInfo;
4632N/A this.oglCaps = oglCaps;
4632N/A context = new OGLContext(OGLRenderQueue.getInstance(), this);
4632N/A
4632N/A // add a record to the Disposer so that we destroy the native
4632N/A // CGLGraphicsConfigInfo data when this object goes away
4632N/A Disposer.addRecord(disposerReferent,
4632N/A new CGLGCDisposerRecord(pConfigInfo));
5366N/A
5366N/A // 7200762: Workaround a deadlock by caching the value
5366N/A // A fix for JDK 8 will remove the workaround
5366N/A this.cachedMaxTextureSize = _getMaxTextureSize();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public Object getProxyKey() {
4632N/A return this;
4632N/A }
4632N/A
4632N/A @Override
4632N/A public SurfaceData createManagedSurface(int w, int h, int transparency) {
4632N/A return CGLSurfaceData.createData(this, w, h,
4632N/A getColorModel(transparency),
4632N/A null,
4632N/A OGLSurfaceData.TEXTURE);
4632N/A }
4632N/A
4632N/A public static CGLGraphicsConfig getConfig(CGraphicsDevice device,
4632N/A int pixfmt)
4632N/A {
4632N/A if (!cglAvailable) {
4632N/A return null;
4632N/A }
4632N/A
4632N/A long cfginfo = 0;
4632N/A final String ids[] = new String[1];
4632N/A OGLRenderQueue rq = OGLRenderQueue.getInstance();
4632N/A rq.lock();
4632N/A try {
4632N/A // getCGLConfigInfo() creates and destroys temporary
4632N/A // surfaces/contexts, so we should first invalidate the current
4632N/A // Java-level context and flush the queue...
4632N/A OGLContext.invalidateCurrentContext();
4670N/A
6062N/A cfginfo = getCGLConfigInfo(device.getCGDisplayID(), pixfmt,
4670N/A kOpenGLSwapInterval);
6062N/A if (cfginfo != 0L) {
6062N/A OGLContext.setScratchSurface(cfginfo);
6062N/A rq.flushAndInvokeNow(new Runnable() {
6062N/A public void run() {
6062N/A ids[0] = OGLContext.getOGLIdString();
6062N/A }
6062N/A });
6062N/A }
4632N/A } finally {
4632N/A rq.unlock();
4632N/A }
4632N/A if (cfginfo == 0) {
4632N/A return null;
4632N/A }
4632N/A
4632N/A int oglCaps = getOGLCapabilities(cfginfo);
4632N/A ContextCapabilities caps = new OGLContextCaps(oglCaps, ids[0]);
4632N/A
4632N/A return new CGLGraphicsConfig(device, pixfmt, cfginfo, caps);
4632N/A }
4632N/A
4632N/A public static boolean isCGLAvailable() {
4632N/A return cglAvailable;
4632N/A }
4632N/A
4632N/A /**
4632N/A * Returns true if the provided capability bit is present for this config.
4632N/A * See OGLContext.java for a list of supported capabilities.
4632N/A */
4632N/A public final boolean isCapPresent(int cap) {
4632N/A return ((oglCaps.getCaps() & cap) != 0);
4632N/A }
4632N/A
4632N/A public final long getNativeConfigInfo() {
4632N/A return pConfigInfo;
4632N/A }
4632N/A
4632N/A /**
4632N/A * {@inheritDoc}
4632N/A *
4632N/A * @see sun.java2d.pipe.hw.BufferedContextProvider#getContext
4632N/A */
4632N/A public final OGLContext getContext() {
4632N/A return context;
4632N/A }
4632N/A
4632N/A @Override
4632N/A public BufferedImage createCompatibleImage(int width, int height) {
4632N/A ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
4632N/A WritableRaster
4632N/A raster = model.createCompatibleWritableRaster(width, height);
4632N/A return new BufferedImage(model, raster, model.isAlphaPremultiplied(),
4632N/A null);
4632N/A }
4632N/A
4632N/A @Override
4632N/A public ColorModel getColorModel(int transparency) {
4632N/A switch (transparency) {
4632N/A case Transparency.OPAQUE:
4632N/A // REMIND: once the ColorModel spec is changed, this should be
4632N/A // an opaque premultiplied DCM...
4632N/A return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
4632N/A case Transparency.BITMASK:
4632N/A return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
4632N/A case Transparency.TRANSLUCENT:
4632N/A ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
4632N/A return new DirectColorModel(cs, 32,
4632N/A 0xff0000, 0xff00, 0xff, 0xff000000,
4632N/A true, DataBuffer.TYPE_INT);
4632N/A default:
4632N/A return null;
4632N/A }
4632N/A }
4632N/A
4632N/A public boolean isDoubleBuffered() {
4632N/A return isCapPresent(CAPS_DOUBLEBUFFERED);
4632N/A }
4632N/A
4632N/A private static class CGLGCDisposerRecord implements DisposerRecord {
4632N/A private long pCfgInfo;
4632N/A public CGLGCDisposerRecord(long pCfgInfo) {
4632N/A this.pCfgInfo = pCfgInfo;
4632N/A }
4632N/A public void dispose() {
4632N/A if (pCfgInfo != 0) {
4632N/A OGLRenderQueue.disposeGraphicsConfig(pCfgInfo);
4632N/A pCfgInfo = 0;
4632N/A }
4632N/A }
4632N/A }
4632N/A
4632N/A // TODO: CGraphicsConfig doesn't implement displayChanged() yet
4632N/A //@Override
4632N/A public synchronized void displayChanged() {
4632N/A //super.displayChanged();
4632N/A
4632N/A // the context could hold a reference to a CGLSurfaceData, which in
4632N/A // turn has a reference back to this CGLGraphicsConfig, so in order
4632N/A // for this instance to be disposed we need to break the connection
4632N/A OGLRenderQueue rq = OGLRenderQueue.getInstance();
4632N/A rq.lock();
4632N/A try {
4632N/A OGLContext.invalidateCurrentContext();
4632N/A } finally {
4632N/A rq.unlock();
4632N/A }
5276N/A
5276N/A updateTotalDisplayBounds();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public String toString() {
6062N/A int displayID = getDevice().getCGDisplayID();
6062N/A return ("CGLGraphicsConfig[dev="+displayID+",pixfmt="+pixfmt+"]");
4632N/A }
4632N/A
4632N/A
4632N/A /**
4632N/A * The following methods are invoked from ComponentModel.java rather
4632N/A * than having the Mac OS X-dependent implementations hardcoded in that
4632N/A * class. This way the appropriate actions are taken based on the peer's
4632N/A * GraphicsConfig, whether it is a CGraphicsConfig or a
4632N/A * CGLGraphicsConfig.
4632N/A */
4632N/A
4632N/A /**
4632N/A * Creates a new SurfaceData that will be associated with the given
4632N/A * LWWindowPeer.
4632N/A */
4632N/A @Override
4632N/A public SurfaceData createSurfaceData(CPlatformView pView) {
4632N/A return CGLSurfaceData.createData(pView);
4632N/A }
4632N/A
4632N/A /**
4639N/A * Creates a new SurfaceData that will be associated with the given
4639N/A * CGLLayer.
4639N/A */
4639N/A @Override
4639N/A public SurfaceData createSurfaceData(CGLLayer layer) {
4639N/A return CGLSurfaceData.createData(layer);
4639N/A }
4639N/A
4639N/A /**
4632N/A * Creates a new hidden-acceleration image of the given width and height
4632N/A * that is associated with the target Component.
4632N/A */
4632N/A @Override
4632N/A public Image createAcceleratedImage(Component target,
4632N/A int width, int height)
4632N/A {
4632N/A ColorModel model = getColorModel(Transparency.OPAQUE);
4632N/A WritableRaster wr =
4632N/A model.createCompatibleWritableRaster(width, height);
4632N/A return new OffScreenImage(target, model, wr,
4632N/A model.isAlphaPremultiplied());
4632N/A }
4632N/A
4632N/A /**
4632N/A * The following methods correspond to the multibuffering methods in
4632N/A * CWindowPeer.java...
4632N/A */
4632N/A
4632N/A /**
4632N/A * Attempts to create a OGL-based backbuffer for the given peer. If
4632N/A * the requested configuration is not natively supported, an AWTException
4632N/A * is thrown. Otherwise, if the backbuffer creation is successful, a
4632N/A * value of 1 is returned.
4632N/A */
4632N/A @Override
4632N/A public long createBackBuffer(CPlatformView pView,
4632N/A int numBuffers, BufferCapabilities caps)
4632N/A throws AWTException
4632N/A {
4632N/A if (numBuffers > 2) {
4632N/A throw new AWTException(
4632N/A "Only double or single buffering is supported");
4632N/A }
4632N/A BufferCapabilities configCaps = getBufferCapabilities();
4632N/A if (!configCaps.isPageFlipping()) {
4632N/A throw new AWTException("Page flipping is not supported");
4632N/A }
4632N/A if (caps.getFlipContents() == BufferCapabilities.FlipContents.PRIOR) {
4632N/A throw new AWTException("FlipContents.PRIOR is not supported");
4632N/A }
4632N/A
4632N/A // non-zero return value means backbuffer creation was successful
4632N/A // (checked in CPlatformWindow.flip(), etc.)
4632N/A return 1;
4632N/A }
4632N/A
4632N/A /**
4632N/A * Destroys the backbuffer object represented by the given handle value.
4632N/A */
4632N/A @Override
4632N/A public void destroyBackBuffer(long backBuffer) {
4632N/A }
4632N/A
4632N/A /**
4632N/A * Creates a VolatileImage that essentially wraps the target Component's
4632N/A * backbuffer (the provided backbuffer handle is essentially ignored).
4632N/A */
4632N/A @Override
4632N/A public VolatileImage createBackBufferImage(Component target,
4632N/A long backBuffer)
4632N/A {
4632N/A return new SunVolatileImage(target,
4632N/A target.getWidth(), target.getHeight(),
4632N/A Boolean.TRUE);
4632N/A }
4632N/A
4632N/A /**
4632N/A * Performs the native OGL flip operation for the given target Component.
4632N/A */
4632N/A @Override
4632N/A public void flip(CPlatformView pView,
4632N/A Component target, VolatileImage xBackBuffer,
4632N/A int x1, int y1, int x2, int y2,
4632N/A BufferCapabilities.FlipContents flipAction)
4632N/A {
4632N/A if (flipAction == BufferCapabilities.FlipContents.COPIED) {
4632N/A SurfaceManager vsm = SurfaceManager.getManager(xBackBuffer);
4632N/A SurfaceData sd = vsm.getPrimarySurfaceData();
4632N/A
4632N/A if (sd instanceof CGLVSyncOffScreenSurfaceData) {
4632N/A CGLVSyncOffScreenSurfaceData vsd =
4632N/A (CGLVSyncOffScreenSurfaceData)sd;
4632N/A SurfaceData bbsd = vsd.getFlipSurface();
4632N/A Graphics2D bbg =
4632N/A new SunGraphics2D(bbsd, Color.black, Color.white, null);
4632N/A try {
4632N/A bbg.drawImage(xBackBuffer, 0, 0, null);
4632N/A } finally {
4632N/A bbg.dispose();
4632N/A }
4632N/A } else {
4632N/A pView.drawImageOnPeer(xBackBuffer, x1, y1, x2, y2);
4632N/A return;
4632N/A }
4632N/A } else if (flipAction == BufferCapabilities.FlipContents.PRIOR) {
4632N/A // not supported by CGL...
4632N/A return;
4632N/A }
4632N/A
4632N/A OGLSurfaceData.swapBuffers(pView.getAWTView());
4632N/A
4632N/A if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
4632N/A Graphics g = xBackBuffer.getGraphics();
4632N/A try {
4632N/A g.setColor(target.getBackground());
4632N/A g.fillRect(0, 0,
4632N/A xBackBuffer.getWidth(),
4632N/A xBackBuffer.getHeight());
4632N/A } finally {
4632N/A g.dispose();
4632N/A }
4632N/A }
4632N/A }
4632N/A
4632N/A private static class CGLBufferCaps extends BufferCapabilities {
4632N/A public CGLBufferCaps(boolean dblBuf) {
4632N/A super(imageCaps, imageCaps,
4632N/A dblBuf ? FlipContents.UNDEFINED : null);
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public BufferCapabilities getBufferCapabilities() {
4632N/A if (bufferCaps == null) {
4632N/A bufferCaps = new CGLBufferCaps(isDoubleBuffered());
4632N/A }
4632N/A return bufferCaps;
4632N/A }
4632N/A
4632N/A private static class CGLImageCaps extends ImageCapabilities {
4632N/A private CGLImageCaps() {
4632N/A super(true);
4632N/A }
4632N/A public boolean isTrueVolatile() {
4632N/A return true;
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public ImageCapabilities getImageCapabilities() {
4632N/A return imageCaps;
4632N/A }
4632N/A
4632N/A /**
4632N/A * {@inheritDoc}
4632N/A *
4632N/A * @see sun.java2d.pipe.hw.AccelGraphicsConfig#createCompatibleVolatileImage
4632N/A */
4632N/A public VolatileImage
4632N/A createCompatibleVolatileImage(int width, int height,
4632N/A int transparency, int type)
4632N/A {
4632N/A if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||
4632N/A transparency == Transparency.BITMASK)
4632N/A {
4632N/A return null;
4632N/A }
4632N/A
4632N/A if (type == FBOBJECT) {
4632N/A if (!isCapPresent(CAPS_EXT_FBOBJECT)) {
4632N/A return null;
4632N/A }
4632N/A } else if (type == PBUFFER) {
4632N/A boolean isOpaque = transparency == Transparency.OPAQUE;
4632N/A if (!isOpaque && !isCapPresent(CAPS_STORED_ALPHA)) {
4632N/A return null;
4632N/A }
4632N/A }
4632N/A
4632N/A SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
4632N/A transparency, type);
4632N/A Surface sd = vi.getDestSurface();
4632N/A if (!(sd instanceof AccelSurface) ||
4632N/A ((AccelSurface)sd).getType() != type)
4632N/A {
4632N/A vi.flush();
4632N/A vi = null;
4632N/A }
4632N/A
4632N/A return vi;
4632N/A }
4632N/A
4632N/A /**
4632N/A * {@inheritDoc}
4632N/A *
4632N/A * @see sun.java2d.pipe.hw.AccelGraphicsConfig#getContextCapabilities
4632N/A */
4632N/A public ContextCapabilities getContextCapabilities() {
4632N/A return oglCaps;
4632N/A }
4632N/A
4632N/A public void addDeviceEventListener(AccelDeviceEventListener l) {
6062N/A int displayID = getDevice().getCGDisplayID();
6062N/A AccelDeviceEventNotifier.addListener(l, displayID);
4632N/A }
4632N/A
4632N/A public void removeDeviceEventListener(AccelDeviceEventListener l) {
4632N/A AccelDeviceEventNotifier.removeListener(l);
4632N/A }
5276N/A
5276N/A private static final Rectangle totalDisplayBounds = new Rectangle();
5276N/A
5276N/A private static void updateTotalDisplayBounds() {
5276N/A synchronized (totalDisplayBounds) {
5276N/A Rectangle virtualBounds = new Rectangle();
5276N/A for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
5276N/A for (GraphicsConfiguration gc : gd.getConfigurations()) {
5276N/A virtualBounds = virtualBounds.union(gc.getBounds());
5276N/A }
5276N/A }
5276N/A totalDisplayBounds.setBounds(virtualBounds);
5276N/A }
5276N/A }
5276N/A
5366N/A
5276N/A // 7160609: GL still fails to create a square texture of this size,
5276N/A // so we use this value to cap the total display bounds.
5366N/A private int getMaxTextureSize() {
5366N/A return cachedMaxTextureSize;
5366N/A }
5276N/A
5276N/A @Override
5276N/A public int getMaxTextureWidth() {
6084N/A //Temporary disable this logic and use some magic constrain.
6084N/A /*
6084N/A int width;
5276N/A
6084N/A synchronized (totalDisplayBounds) {
6084N/A if (totalDisplayBounds.width == 0) {
6084N/A updateTotalDisplayBounds();
6084N/A }
6084N/A width = totalDisplayBounds.width;
6084N/A }
5276N/A
6084N/A return Math.min(width, getMaxTextureSize());
6084N/A */
6084N/A return getMaxTextureSize() / (getDevice().getScaleFactor() * 2);
5276N/A }
5276N/A
5276N/A @Override
5276N/A public int getMaxTextureHeight() {
6084N/A //Temporary disable this logic and use some magic constrain.
6084N/A /*
6084N/A int height;
5276N/A
6084N/A synchronized (totalDisplayBounds) {
6084N/A if (totalDisplayBounds.height == 0) {
6084N/A updateTotalDisplayBounds();
6084N/A }
6084N/A height = totalDisplayBounds.height;
6084N/A }
5276N/A
6084N/A return Math.min(height, getMaxTextureSize());
6084N/A */
6084N/A return getMaxTextureSize() / (getDevice().getScaleFactor() * 2);
5276N/A }
4632N/A}