Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/java2d/opengl/GLXGraphicsConfig.java
32288 views
/*1* Copyright (c) 2003, 2008, 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.BufferCapabilities.FlipContents;30import java.awt.Color;31import java.awt.Component;32import java.awt.Graphics;33import java.awt.Graphics2D;34import java.awt.Image;35import java.awt.ImageCapabilities;36import java.awt.Transparency;37import java.awt.color.ColorSpace;38import java.awt.image.BufferedImage;39import java.awt.image.ColorModel;40import java.awt.image.DataBuffer;41import java.awt.image.DirectColorModel;42import java.awt.image.VolatileImage;43import java.awt.image.WritableRaster;44import 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.pipe.hw.AccelSurface;55import sun.java2d.pipe.hw.AccelTypedVolatileImage;56import sun.java2d.pipe.hw.ContextCapabilities;57import static sun.java2d.opengl.OGLSurfaceData.*;58import static sun.java2d.opengl.OGLContext.*;59import static sun.java2d.opengl.OGLContext.OGLContextCaps.*;60import sun.java2d.opengl.GLXSurfaceData.GLXVSyncOffScreenSurfaceData;61import sun.java2d.pipe.hw.AccelDeviceEventListener;62import sun.java2d.pipe.hw.AccelDeviceEventNotifier;6364public class GLXGraphicsConfig65extends X11GraphicsConfig66implements OGLGraphicsConfig67{68private static ImageCapabilities imageCaps = new GLXImageCaps();69private BufferCapabilities bufferCaps;70private long pConfigInfo;71private ContextCapabilities oglCaps;72private OGLContext context;7374private static native long getGLXConfigInfo(int screennum, int visualnum);75private static native int getOGLCapabilities(long configInfo);76private native void initConfig(long aData, long ctxinfo);7778private GLXGraphicsConfig(X11GraphicsDevice device, int visualnum,79long configInfo, ContextCapabilities oglCaps)80{81super(device, visualnum, 0, 0,82(oglCaps.getCaps() & CAPS_DOUBLEBUFFERED) != 0);83pConfigInfo = configInfo;84initConfig(getAData(), configInfo);85this.oglCaps = oglCaps;86context = new OGLContext(OGLRenderQueue.getInstance(), this);87}8889@Override90public Object getProxyKey() {91return this;92}9394@Override95public SurfaceData createManagedSurface(int w, int h, int transparency) {96return GLXSurfaceData.createData(this, w, h,97getColorModel(transparency),98null,99OGLSurfaceData.TEXTURE);100}101102public static GLXGraphicsConfig getConfig(X11GraphicsDevice device,103int visualnum)104{105if (!X11GraphicsEnvironment.isGLXAvailable()) {106return null;107}108109long cfginfo = 0;110final String ids[] = new String[1];111OGLRenderQueue rq = OGLRenderQueue.getInstance();112rq.lock();113try {114// getGLXConfigInfo() creates and destroys temporary115// surfaces/contexts, so we should first invalidate the current116// Java-level context and flush the queue...117OGLContext.invalidateCurrentContext();118GLXGetConfigInfo action =119new GLXGetConfigInfo(device.getScreen(), visualnum);120rq.flushAndInvokeNow(action);121cfginfo = action.getConfigInfo();122if (cfginfo != 0L) {123OGLContext.setScratchSurface(cfginfo);124rq.flushAndInvokeNow(new Runnable() {125public void run() {126ids[0] = OGLContext.getOGLIdString();127}128});129}130} finally {131rq.unlock();132}133if (cfginfo == 0) {134return null;135}136137int oglCaps = getOGLCapabilities(cfginfo);138ContextCapabilities caps = new OGLContextCaps(oglCaps, ids[0]);139140return new GLXGraphicsConfig(device, visualnum, cfginfo, caps);141}142143/**144* This is a small helper class that allows us to execute145* getGLXConfigInfo() on the queue flushing thread.146*/147private static class GLXGetConfigInfo implements Runnable {148private int screen;149private int visual;150private long cfginfo;151private GLXGetConfigInfo(int screen, int visual) {152this.screen = screen;153this.visual = visual;154}155public void run() {156cfginfo = getGLXConfigInfo(screen, visual);157}158public long getConfigInfo() {159return cfginfo;160}161}162163/**164* Returns true if the provided capability bit is present for this config.165* See OGLContext.java for a list of supported capabilities.166*/167@Override168public final boolean isCapPresent(int cap) {169return ((oglCaps.getCaps() & cap) != 0);170}171172@Override173public final long getNativeConfigInfo() {174return pConfigInfo;175}176177/**178* {@inheritDoc}179*180* @see sun.java2d.pipe.hw.BufferedContextProvider#getContext181*/182@Override183public final OGLContext getContext() {184return context;185}186187@Override188public BufferedImage createCompatibleImage(int width, int height) {189ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);190WritableRaster191raster = model.createCompatibleWritableRaster(width, height);192return new BufferedImage(model, raster, model.isAlphaPremultiplied(),193null);194}195196@Override197public ColorModel getColorModel(int transparency) {198switch (transparency) {199case Transparency.OPAQUE:200// REMIND: once the ColorModel spec is changed, this should be201// an opaque premultiplied DCM...202return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);203case Transparency.BITMASK:204return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);205case Transparency.TRANSLUCENT:206ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);207return new DirectColorModel(cs, 32,2080xff0000, 0xff00, 0xff, 0xff000000,209true, DataBuffer.TYPE_INT);210default:211return null;212}213}214215public String toString() {216return ("GLXGraphicsConfig[dev="+screen+217",vis=0x"+Integer.toHexString(visual)+218"]");219}220221/**222* The following methods are invoked from MToolkit or XToolkit.java and223* X11ComponentPeer.java rather than having the X11-dependent224* implementations hardcoded in those classes. This way the appropriate225* actions are taken based on the peer's GraphicsConfig, whether it is226* an X11GraphicsConfig or a GLXGraphicsConfig.227*/228229/**230* Creates a new SurfaceData that will be associated with the given231* X11ComponentPeer.232*/233@Override234public SurfaceData createSurfaceData(X11ComponentPeer peer) {235return GLXSurfaceData.createData(peer);236}237238/**239* Creates a new hidden-acceleration image of the given width and height240* that is associated with the target Component.241*/242@Override243public Image createAcceleratedImage(Component target,244int width, int height)245{246ColorModel model = getColorModel(Transparency.OPAQUE);247WritableRaster wr =248model.createCompatibleWritableRaster(width, height);249return new OffScreenImage(target, model, wr,250model.isAlphaPremultiplied());251}252253/**254* The following methods correspond to the multibuffering methods in255* X11ComponentPeer.java...256*/257258/**259* Attempts to create a GLX-based backbuffer for the given peer. If260* the requested configuration is not natively supported, an AWTException261* is thrown. Otherwise, if the backbuffer creation is successful, a262* value of 1 is returned.263*/264@Override265public long createBackBuffer(X11ComponentPeer peer,266int numBuffers, BufferCapabilities caps)267throws AWTException268{269if (numBuffers > 2) {270throw new AWTException(271"Only double or single buffering is supported");272}273BufferCapabilities configCaps = getBufferCapabilities();274if (!configCaps.isPageFlipping()) {275throw new AWTException("Page flipping is not supported");276}277if (caps.getFlipContents() == BufferCapabilities.FlipContents.PRIOR) {278throw new AWTException("FlipContents.PRIOR is not supported");279}280281// non-zero return value means backbuffer creation was successful282// (checked in X11ComponentPeer.flip(), etc.)283return 1;284}285286/**287* Destroys the backbuffer object represented by the given handle value.288*/289@Override290public void destroyBackBuffer(long backBuffer) {291}292293/**294* Creates a VolatileImage that essentially wraps the target Component's295* backbuffer (the provided backbuffer handle is essentially ignored).296*/297@Override298public VolatileImage createBackBufferImage(Component target,299long backBuffer)300{301return new SunVolatileImage(target,302target.getWidth(), target.getHeight(),303Boolean.TRUE);304}305306/**307* Performs the native GLX flip operation for the given target Component.308*/309@Override310public void flip(X11ComponentPeer peer,311Component target, VolatileImage xBackBuffer,312int x1, int y1, int x2, int y2,313BufferCapabilities.FlipContents flipAction)314{315if (flipAction == BufferCapabilities.FlipContents.COPIED) {316SurfaceManager vsm = SurfaceManager.getManager(xBackBuffer);317SurfaceData sd = vsm.getPrimarySurfaceData();318319if (sd instanceof GLXVSyncOffScreenSurfaceData) {320GLXVSyncOffScreenSurfaceData vsd =321(GLXVSyncOffScreenSurfaceData)sd;322SurfaceData bbsd = vsd.getFlipSurface();323Graphics2D bbg =324new SunGraphics2D(bbsd, Color.black, Color.white, null);325try {326bbg.drawImage(xBackBuffer, 0, 0, null);327} finally {328bbg.dispose();329}330} else {331Graphics g = peer.getGraphics();332try {333g.drawImage(xBackBuffer,334x1, y1, x2, y2,335x1, y1, x2, y2,336null);337} finally {338g.dispose();339}340return;341}342} else if (flipAction == BufferCapabilities.FlipContents.PRIOR) {343// not supported by GLX...344return;345}346347OGLSurfaceData.swapBuffers(peer.getContentWindow());348349if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {350Graphics g = xBackBuffer.getGraphics();351try {352g.setColor(target.getBackground());353g.fillRect(0, 0,354xBackBuffer.getWidth(),355xBackBuffer.getHeight());356} finally {357g.dispose();358}359}360}361362private static class GLXBufferCaps extends BufferCapabilities {363public GLXBufferCaps(boolean dblBuf) {364super(imageCaps, imageCaps,365dblBuf ? FlipContents.UNDEFINED : null);366}367}368369@Override370public BufferCapabilities getBufferCapabilities() {371if (bufferCaps == null) {372bufferCaps = new GLXBufferCaps(isDoubleBuffered());373}374return bufferCaps;375}376377private static class GLXImageCaps extends ImageCapabilities {378private GLXImageCaps() {379super(true);380}381public boolean isTrueVolatile() {382return true;383}384}385386@Override387public ImageCapabilities getImageCapabilities() {388return imageCaps;389}390391/**392* {@inheritDoc}393*394* @see sun.java2d.pipe.hw.AccelGraphicsConfig#createCompatibleVolatileImage395*/396@Override397public VolatileImage398createCompatibleVolatileImage(int width, int height,399int transparency, int type)400{401if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||402transparency == Transparency.BITMASK)403{404return null;405}406407if (type == FBOBJECT) {408if (!isCapPresent(CAPS_EXT_FBOBJECT)) {409return null;410}411} else if (type == PBUFFER) {412boolean isOpaque = transparency == Transparency.OPAQUE;413if (!isOpaque && !isCapPresent(CAPS_STORED_ALPHA)) {414return null;415}416}417418SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,419transparency, type);420Surface sd = vi.getDestSurface();421if (!(sd instanceof AccelSurface) ||422((AccelSurface)sd).getType() != type)423{424vi.flush();425vi = null;426}427428return vi;429}430431/**432* {@inheritDoc}433*434* @see sun.java2d.pipe.hw.AccelGraphicsConfig#getContextCapabilities435*/436@Override437public ContextCapabilities getContextCapabilities() {438return oglCaps;439}440441@Override442public void addDeviceEventListener(AccelDeviceEventListener l) {443AccelDeviceEventNotifier.addListener(l, screen.getScreen());444}445446@Override447public void removeDeviceEventListener(AccelDeviceEventListener l) {448AccelDeviceEventNotifier.removeListener(l);449}450}451452453