Path: blob/master/src/java.desktop/unix/classes/sun/java2d/opengl/GLXGraphicsConfig.java
41159 views
/*1* Copyright (c) 2003, 2019, 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 java.awt.AWTException;28import java.awt.BufferCapabilities;29import java.awt.Color;30import java.awt.Component;31import java.awt.Graphics;32import java.awt.Graphics2D;33import java.awt.Image;34import java.awt.ImageCapabilities;35import java.awt.Transparency;36import java.awt.color.ColorSpace;37import java.awt.image.BufferedImage;38import java.awt.image.ColorModel;39import java.awt.image.DataBuffer;40import java.awt.image.DirectColorModel;41import java.awt.image.VolatileImage;42import java.awt.image.WritableRaster;4344import sun.awt.X11ComponentPeer;45import sun.awt.X11GraphicsConfig;46import sun.awt.X11GraphicsDevice;47import sun.awt.X11GraphicsEnvironment;48import sun.awt.image.OffScreenImage;49import sun.awt.image.SunVolatileImage;50import sun.awt.image.SurfaceManager;51import sun.java2d.SunGraphics2D;52import sun.java2d.Surface;53import sun.java2d.SurfaceData;54import sun.java2d.opengl.GLXSurfaceData.GLXVSyncOffScreenSurfaceData;55import sun.java2d.pipe.hw.AccelSurface;56import sun.java2d.pipe.hw.AccelTypedVolatileImage;57import sun.java2d.pipe.hw.ContextCapabilities;5859import static sun.java2d.opengl.OGLContext.OGLContextCaps;60import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_DOUBLEBUFFERED;61import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_EXT_FBOBJECT;62import static sun.java2d.opengl.OGLSurfaceData.FBOBJECT;63import static sun.java2d.opengl.OGLSurfaceData.TEXTURE;6465public final class GLXGraphicsConfig66extends X11GraphicsConfig67implements OGLGraphicsConfig68{69private static ImageCapabilities imageCaps = new GLXImageCaps();70private BufferCapabilities bufferCaps;71private long pConfigInfo;72private ContextCapabilities oglCaps;73private final OGLContext context;7475private static native long getGLXConfigInfo(int screennum, int visualnum);76private static native int getOGLCapabilities(long configInfo);77private native void initConfig(long aData, long ctxinfo);7879private GLXGraphicsConfig(X11GraphicsDevice device, int visualnum,80long configInfo, ContextCapabilities oglCaps)81{82super(device, visualnum, 0, 0,83(oglCaps.getCaps() & CAPS_DOUBLEBUFFERED) != 0);84pConfigInfo = configInfo;85initConfig(getAData(), configInfo);86this.oglCaps = oglCaps;87context = new OGLContext(OGLRenderQueue.getInstance());88}8990@Override91public Object getProxyKey() {92return this;93}9495@Override96public SurfaceData createManagedSurface(int w, int h, int transparency) {97return GLXSurfaceData.createData(this, w, h,98getColorModel(transparency),99null,100OGLSurfaceData.TEXTURE);101}102103public static GLXGraphicsConfig getConfig(X11GraphicsDevice device,104int visualnum)105{106if (!X11GraphicsEnvironment.isGLXAvailable()) {107return null;108}109110long cfginfo = 0;111final String[] ids = new String[1];112OGLRenderQueue rq = OGLRenderQueue.getInstance();113rq.lock();114try {115// getGLXConfigInfo() creates and destroys temporary116// surfaces/contexts, so we should first invalidate the current117// Java-level context and flush the queue...118OGLContext.invalidateCurrentContext();119GLXGetConfigInfo action =120new GLXGetConfigInfo(device.getScreen(), visualnum);121rq.flushAndInvokeNow(action);122cfginfo = action.getConfigInfo();123if (cfginfo != 0L) {124OGLContext.setScratchSurface(cfginfo);125rq.flushAndInvokeNow(new Runnable() {126public void run() {127ids[0] = OGLContext.getOGLIdString();128}129});130}131} finally {132rq.unlock();133}134if (cfginfo == 0) {135return null;136}137138int oglCaps = getOGLCapabilities(cfginfo);139ContextCapabilities caps = new OGLContextCaps(oglCaps, ids[0]);140141return new GLXGraphicsConfig(device, visualnum, cfginfo, caps);142}143144/**145* This is a small helper class that allows us to execute146* getGLXConfigInfo() on the queue flushing thread.147*/148private static class GLXGetConfigInfo implements Runnable {149private int screen;150private int visual;151private long cfginfo;152private GLXGetConfigInfo(int screen, int visual) {153this.screen = screen;154this.visual = visual;155}156public void run() {157cfginfo = getGLXConfigInfo(screen, visual);158}159public long getConfigInfo() {160return cfginfo;161}162}163164/**165* Returns true if the provided capability bit is present for this config.166* See OGLContext.java for a list of supported capabilities.167*/168@Override169public final boolean isCapPresent(int cap) {170return ((oglCaps.getCaps() & cap) != 0);171}172173@Override174public final long getNativeConfigInfo() {175return pConfigInfo;176}177178@Override179public final OGLContext getContext() {180return context;181}182183@Override184public BufferedImage createCompatibleImage(int width, int height) {185ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);186WritableRaster187raster = model.createCompatibleWritableRaster(width, height);188return new BufferedImage(model, raster, model.isAlphaPremultiplied(),189null);190}191192@Override193public ColorModel getColorModel(int transparency) {194switch (transparency) {195case Transparency.OPAQUE:196// REMIND: once the ColorModel spec is changed, this should be197// an opaque premultiplied DCM...198return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);199case Transparency.BITMASK:200return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);201case Transparency.TRANSLUCENT:202ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);203return new DirectColorModel(cs, 32,2040xff0000, 0xff00, 0xff, 0xff000000,205true, DataBuffer.TYPE_INT);206default:207return null;208}209}210211public String toString() {212return ("GLXGraphicsConfig[dev="+getDevice()+213",vis=0x"+Integer.toHexString(visual)+214"]");215}216217/**218* The following methods are invoked from MToolkit or XToolkit.java and219* X11ComponentPeer.java rather than having the X11-dependent220* implementations hardcoded in those classes. This way the appropriate221* actions are taken based on the peer's GraphicsConfig, whether it is222* an X11GraphicsConfig or a GLXGraphicsConfig.223*/224225/**226* Creates a new SurfaceData that will be associated with the given227* X11ComponentPeer.228*/229@Override230public SurfaceData createSurfaceData(X11ComponentPeer peer) {231return GLXSurfaceData.createData(peer);232}233234/**235* Creates a new hidden-acceleration image of the given width and height236* that is associated with the target Component.237*/238@Override239public Image createAcceleratedImage(Component target,240int width, int height)241{242ColorModel model = getColorModel(Transparency.OPAQUE);243WritableRaster wr =244model.createCompatibleWritableRaster(width, height);245return new OffScreenImage(target, model, wr,246model.isAlphaPremultiplied());247}248249/**250* The following methods correspond to the multibuffering methods in251* X11ComponentPeer.java...252*/253254/**255* Attempts to create a GLX-based backbuffer for the given peer. If256* the requested configuration is not natively supported, an AWTException257* is thrown. Otherwise, if the backbuffer creation is successful, a258* value of 1 is returned.259*/260@Override261public long createBackBuffer(X11ComponentPeer peer,262int numBuffers, BufferCapabilities caps)263throws AWTException264{265if (numBuffers > 2) {266throw new AWTException(267"Only double or single buffering is supported");268}269BufferCapabilities configCaps = getBufferCapabilities();270if (!configCaps.isPageFlipping()) {271throw new AWTException("Page flipping is not supported");272}273if (caps.getFlipContents() == BufferCapabilities.FlipContents.PRIOR) {274throw new AWTException("FlipContents.PRIOR is not supported");275}276277// non-zero return value means backbuffer creation was successful278// (checked in X11ComponentPeer.flip(), etc.)279return 1;280}281282/**283* Destroys the backbuffer object represented by the given handle value.284*/285@Override286public void destroyBackBuffer(long backBuffer) {287}288289/**290* Creates a VolatileImage that essentially wraps the target Component's291* backbuffer (the provided backbuffer handle is essentially ignored).292*/293@Override294public VolatileImage createBackBufferImage(Component target,295long backBuffer)296{297return new SunVolatileImage(target,298target.getWidth(), target.getHeight(),299Boolean.TRUE);300}301302/**303* Performs the native GLX flip operation for the given target Component.304*/305@Override306public void flip(X11ComponentPeer peer,307Component target, VolatileImage xBackBuffer,308int x1, int y1, int x2, int y2,309BufferCapabilities.FlipContents flipAction)310{311if (flipAction == BufferCapabilities.FlipContents.COPIED) {312SurfaceManager vsm = SurfaceManager.getManager(xBackBuffer);313SurfaceData sd = vsm.getPrimarySurfaceData();314315if (sd instanceof GLXVSyncOffScreenSurfaceData) {316GLXVSyncOffScreenSurfaceData vsd =317(GLXVSyncOffScreenSurfaceData)sd;318SurfaceData bbsd = vsd.getFlipSurface();319Graphics2D bbg =320new SunGraphics2D(bbsd, Color.black, Color.white, null);321try {322bbg.drawImage(xBackBuffer, 0, 0, null);323} finally {324bbg.dispose();325}326} else {327Graphics g = peer.getGraphics();328try {329g.drawImage(xBackBuffer,330x1, y1, x2, y2,331x1, y1, x2, y2,332null);333} finally {334g.dispose();335}336return;337}338} else if (flipAction == BufferCapabilities.FlipContents.PRIOR) {339// not supported by GLX...340return;341}342343OGLSurfaceData.swapBuffers(peer.getContentWindow());344345if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {346Graphics g = xBackBuffer.getGraphics();347try {348g.setColor(target.getBackground());349g.fillRect(0, 0,350xBackBuffer.getWidth(),351xBackBuffer.getHeight());352} finally {353g.dispose();354}355}356}357358private static class GLXBufferCaps extends BufferCapabilities {359public GLXBufferCaps(boolean dblBuf) {360super(imageCaps, imageCaps,361dblBuf ? FlipContents.UNDEFINED : null);362}363}364365@Override366public BufferCapabilities getBufferCapabilities() {367if (bufferCaps == null) {368bufferCaps = new GLXBufferCaps(isDoubleBuffered());369}370return bufferCaps;371}372373private static class GLXImageCaps extends ImageCapabilities {374private GLXImageCaps() {375super(true);376}377public boolean isTrueVolatile() {378return true;379}380}381382@Override383public ImageCapabilities getImageCapabilities() {384return imageCaps;385}386387@Override388public VolatileImage389createCompatibleVolatileImage(int width, int height,390int transparency, int type)391{392if ((type != FBOBJECT && type != TEXTURE)393|| transparency == Transparency.BITMASK394|| type == FBOBJECT && !isCapPresent(CAPS_EXT_FBOBJECT)) {395return null;396}397SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,398transparency, type);399Surface sd = vi.getDestSurface();400if (!(sd instanceof AccelSurface) ||401((AccelSurface)sd).getType() != type)402{403vi.flush();404vi = null;405}406407return vi;408}409410@Override411public ContextCapabilities getContextCapabilities() {412return oglCaps;413}414}415416417