Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/sun/java2d/opengl/CGLGraphicsConfig.java
38918 views
/*1* Copyright (c) 2011, 2014, 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.Component;30import java.awt.Graphics;31import java.awt.Graphics2D;32import java.awt.Image;33import java.awt.ImageCapabilities;34import java.awt.Rectangle;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.CGraphicsConfig;45import sun.awt.CGraphicsDevice;46import sun.awt.image.OffScreenImage;47import sun.awt.image.SunVolatileImage;48import sun.java2d.Disposer;49import sun.java2d.DisposerRecord;50import sun.java2d.Surface;51import sun.java2d.SurfaceData;52import sun.java2d.opengl.OGLContext.OGLContextCaps;53import sun.java2d.pipe.hw.AccelSurface;54import sun.java2d.pipe.hw.AccelTypedVolatileImage;55import sun.java2d.pipe.hw.ContextCapabilities;56import static sun.java2d.opengl.OGLSurfaceData.*;57import static sun.java2d.opengl.OGLContext.OGLContextCaps.*;58import sun.java2d.pipe.hw.AccelDeviceEventListener;59import sun.java2d.pipe.hw.AccelDeviceEventNotifier;6061import sun.lwawt.LWComponentPeer;62import sun.lwawt.macosx.CPlatformView;6364public final class CGLGraphicsConfig extends CGraphicsConfig65implements OGLGraphicsConfig66{67//private static final int kOpenGLSwapInterval =68// RuntimeOptions.getCurrentOptions().OpenGLSwapInterval;69private static final int kOpenGLSwapInterval = 0; // TODO70private static boolean cglAvailable;71private static ImageCapabilities imageCaps = new CGLImageCaps();7273private int pixfmt;74private BufferCapabilities bufferCaps;75private long pConfigInfo;76private ContextCapabilities oglCaps;77private OGLContext context;78private final Object disposerReferent = new Object();79private final int maxTextureSize;8081private static native boolean initCGL();82private static native long getCGLConfigInfo(int displayID, int visualnum,83int swapInterval);84private static native int getOGLCapabilities(long configInfo);8586/**87* Returns GL_MAX_TEXTURE_SIZE from the shared opengl context. Must be88* called under OGLRQ lock, because this method change current context.89*90* @return GL_MAX_TEXTURE_SIZE91*/92private static native int nativeGetMaxTextureSize();9394static {95cglAvailable = initCGL();96}9798private CGLGraphicsConfig(CGraphicsDevice device, int pixfmt,99long configInfo, int maxTextureSize,100ContextCapabilities oglCaps) {101super(device);102103this.pixfmt = pixfmt;104this.pConfigInfo = configInfo;105this.oglCaps = oglCaps;106this.maxTextureSize = maxTextureSize;107context = new OGLContext(OGLRenderQueue.getInstance(), this);108109// add a record to the Disposer so that we destroy the native110// CGLGraphicsConfigInfo data when this object goes away111Disposer.addRecord(disposerReferent,112new CGLGCDisposerRecord(pConfigInfo));113}114115@Override116public Object getProxyKey() {117return this;118}119120@Override121public SurfaceData createManagedSurface(int w, int h, int transparency) {122return CGLSurfaceData.createData(this, w, h,123getColorModel(transparency),124null,125OGLSurfaceData.TEXTURE);126}127128public static CGLGraphicsConfig getConfig(CGraphicsDevice device,129int pixfmt)130{131if (!cglAvailable) {132return null;133}134135long cfginfo = 0;136int textureSize = 0;137final String ids[] = new String[1];138OGLRenderQueue rq = OGLRenderQueue.getInstance();139rq.lock();140try {141// getCGLConfigInfo() creates and destroys temporary142// surfaces/contexts, so we should first invalidate the current143// Java-level context and flush the queue...144OGLContext.invalidateCurrentContext();145146cfginfo = getCGLConfigInfo(device.getCGDisplayID(), pixfmt,147kOpenGLSwapInterval);148if (cfginfo != 0L) {149textureSize = nativeGetMaxTextureSize();150// 7160609: GL still fails to create a square texture of this151// size. Half should be safe enough.152// Explicitly not support a texture more than 2^14, see 8010999.153textureSize = textureSize <= 16384 ? textureSize / 2 : 8192;154OGLContext.setScratchSurface(cfginfo);155rq.flushAndInvokeNow(() -> {156ids[0] = OGLContext.getOGLIdString();157});158}159} finally {160rq.unlock();161}162if (cfginfo == 0) {163return null;164}165166int oglCaps = getOGLCapabilities(cfginfo);167ContextCapabilities caps = new OGLContextCaps(oglCaps, ids[0]);168return new CGLGraphicsConfig(device, pixfmt, cfginfo, textureSize, caps);169}170171public static boolean isCGLAvailable() {172return cglAvailable;173}174175/**176* Returns true if the provided capability bit is present for this config.177* See OGLContext.java for a list of supported capabilities.178*/179@Override180public boolean isCapPresent(int cap) {181return ((oglCaps.getCaps() & cap) != 0);182}183184@Override185public long getNativeConfigInfo() {186return pConfigInfo;187}188189/**190* {@inheritDoc}191*192* @see sun.java2d.pipe.hw.BufferedContextProvider#getContext193*/194@Override195public OGLContext getContext() {196return context;197}198199@Override200public BufferedImage createCompatibleImage(int width, int height) {201ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);202WritableRaster203raster = model.createCompatibleWritableRaster(width, height);204return new BufferedImage(model, raster, model.isAlphaPremultiplied(),205null);206}207208@Override209public ColorModel getColorModel(int transparency) {210switch (transparency) {211case Transparency.OPAQUE:212// REMIND: once the ColorModel spec is changed, this should be213// an opaque premultiplied DCM...214return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);215case Transparency.BITMASK:216return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);217case Transparency.TRANSLUCENT:218ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);219return new DirectColorModel(cs, 32,2200xff0000, 0xff00, 0xff, 0xff000000,221true, DataBuffer.TYPE_INT);222default:223return null;224}225}226227public boolean isDoubleBuffered() {228return isCapPresent(CAPS_DOUBLEBUFFERED);229}230231private static class CGLGCDisposerRecord implements DisposerRecord {232private long pCfgInfo;233public CGLGCDisposerRecord(long pCfgInfo) {234this.pCfgInfo = pCfgInfo;235}236public void dispose() {237if (pCfgInfo != 0) {238OGLRenderQueue.disposeGraphicsConfig(pCfgInfo);239pCfgInfo = 0;240}241}242}243244// TODO: CGraphicsConfig doesn't implement displayChanged() yet245//@Override246public synchronized void displayChanged() {247//super.displayChanged();248249// the context could hold a reference to a CGLSurfaceData, which in250// turn has a reference back to this CGLGraphicsConfig, so in order251// for this instance to be disposed we need to break the connection252OGLRenderQueue rq = OGLRenderQueue.getInstance();253rq.lock();254try {255OGLContext.invalidateCurrentContext();256} finally {257rq.unlock();258}259}260261@Override262public String toString() {263int displayID = getDevice().getCGDisplayID();264return ("CGLGraphicsConfig[dev="+displayID+",pixfmt="+pixfmt+"]");265}266267@Override268public SurfaceData createSurfaceData(CPlatformView pView) {269return CGLSurfaceData.createData(pView);270}271272@Override273public SurfaceData createSurfaceData(CGLLayer layer) {274return CGLSurfaceData.createData(layer);275}276277@Override278public Image createAcceleratedImage(Component target,279int width, int height)280{281ColorModel model = getColorModel(Transparency.OPAQUE);282WritableRaster wr = model.createCompatibleWritableRaster(width, height);283return new OffScreenImage(target, model, wr,284model.isAlphaPremultiplied());285}286287@Override288public void assertOperationSupported(final int numBuffers,289final BufferCapabilities caps)290throws AWTException {291// Assume this method is never called with numBuffers != 2, as 0 is292// unsupported, and 1 corresponds to a SingleBufferStrategy which293// doesn't depend on the peer. Screen is considered as a separate294// "buffer".295if (numBuffers != 2) {296throw new AWTException("Only double buffering is supported");297}298final BufferCapabilities configCaps = getBufferCapabilities();299if (!configCaps.isPageFlipping()) {300throw new AWTException("Page flipping is not supported");301}302if (caps.getFlipContents() == BufferCapabilities.FlipContents.PRIOR) {303throw new AWTException("FlipContents.PRIOR is not supported");304}305}306307@Override308public Image createBackBuffer(final LWComponentPeer<?, ?> peer) {309final Rectangle r = peer.getBounds();310// It is possible for the component to have size 0x0, adjust it to311// be at least 1x1 to avoid IAE312final int w = Math.max(1, r.width);313final int h = Math.max(1, r.height);314final int transparency = peer.isTranslucent() ? Transparency.TRANSLUCENT315: Transparency.OPAQUE;316return new SunVolatileImage(this, w, h, transparency, null);317}318319@Override320public void destroyBackBuffer(final Image backBuffer) {321if (backBuffer != null) {322backBuffer.flush();323}324}325326@Override327public void flip(final LWComponentPeer<?, ?> peer, final Image backBuffer,328final int x1, final int y1, final int x2, final int y2,329final BufferCapabilities.FlipContents flipAction) {330final Graphics g = peer.getGraphics();331try {332g.drawImage(backBuffer, x1, y1, x2, y2, x1, y1, x2, y2, null);333} finally {334g.dispose();335}336if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {337final Graphics2D bg = (Graphics2D) backBuffer.getGraphics();338try {339bg.setBackground(peer.getBackground());340bg.clearRect(0, 0, backBuffer.getWidth(null),341backBuffer.getHeight(null));342} finally {343bg.dispose();344}345}346}347348private static class CGLBufferCaps extends BufferCapabilities {349public CGLBufferCaps(boolean dblBuf) {350super(imageCaps, imageCaps,351dblBuf ? FlipContents.UNDEFINED : null);352}353}354355@Override356public BufferCapabilities getBufferCapabilities() {357if (bufferCaps == null) {358bufferCaps = new CGLBufferCaps(isDoubleBuffered());359}360return bufferCaps;361}362363private static class CGLImageCaps extends ImageCapabilities {364private CGLImageCaps() {365super(true);366}367public boolean isTrueVolatile() {368return true;369}370}371372@Override373public ImageCapabilities getImageCapabilities() {374return imageCaps;375}376377@Override378public VolatileImage createCompatibleVolatileImage(int width, int height,379int transparency,380int type) {381if (type == FLIP_BACKBUFFER || type == WINDOW || type == UNDEFINED ||382transparency == Transparency.BITMASK)383{384return null;385}386387if (type == FBOBJECT) {388if (!isCapPresent(CAPS_EXT_FBOBJECT)) {389return null;390}391} else if (type == PBUFFER) {392boolean isOpaque = transparency == Transparency.OPAQUE;393if (!isOpaque && !isCapPresent(CAPS_STORED_ALPHA)) {394return null;395}396}397398SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,399transparency, type);400Surface sd = vi.getDestSurface();401if (!(sd instanceof AccelSurface) ||402((AccelSurface)sd).getType() != type)403{404vi.flush();405vi = null;406}407408return vi;409}410411/**412* {@inheritDoc}413*414* @see sun.java2d.pipe.hw.AccelGraphicsConfig#getContextCapabilities415*/416@Override417public ContextCapabilities getContextCapabilities() {418return oglCaps;419}420421@Override422public void addDeviceEventListener(AccelDeviceEventListener l) {423int displayID = getDevice().getCGDisplayID();424AccelDeviceEventNotifier.addListener(l, displayID);425}426427@Override428public void removeDeviceEventListener(AccelDeviceEventListener l) {429AccelDeviceEventNotifier.removeListener(l);430}431432@Override433public int getMaxTextureWidth() {434return Math.max(maxTextureSize / getDevice().getScaleFactor(),435getBounds().width);436}437438@Override439public int getMaxTextureHeight() {440return Math.max(maxTextureSize / getDevice().getScaleFactor(),441getBounds().height);442}443}444445446