Path: blob/master/src/java.desktop/macosx/classes/sun/java2d/opengl/CGLGraphicsConfig.java
41159 views
/*1* Copyright (c) 2011, 2021, 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 sun.lwawt.LWComponentPeer;57import sun.lwawt.macosx.CFRetainedResource;5859import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_DOUBLEBUFFERED;60import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_EXT_FBOBJECT;61import static sun.java2d.opengl.OGLSurfaceData.FBOBJECT;62import static sun.java2d.opengl.OGLSurfaceData.TEXTURE;6364public final class CGLGraphicsConfig extends CGraphicsConfig65implements OGLGraphicsConfig66{67private static boolean cglAvailable;68private static ImageCapabilities imageCaps = new CGLImageCaps();6970private BufferCapabilities bufferCaps;71private long pConfigInfo;72private ContextCapabilities oglCaps;73private final OGLContext context;74private final Object disposerReferent = new Object();75private final int maxTextureSize;7677private static native boolean initCGL();78private static native long getCGLConfigInfo();79private static native int getOGLCapabilities(long configInfo);8081/**82* Returns GL_MAX_TEXTURE_SIZE from the shared opengl context. Must be83* called under OGLRQ lock, because this method change current context.84*85* @return GL_MAX_TEXTURE_SIZE86*/87private static native int nativeGetMaxTextureSize();8889static {90cglAvailable = initCGL();91}9293private CGLGraphicsConfig(CGraphicsDevice device, long configInfo,94int maxTextureSize, ContextCapabilities oglCaps) {95super(device);96this.pConfigInfo = configInfo;97this.oglCaps = oglCaps;98this.maxTextureSize = maxTextureSize;99context = new OGLContext(OGLRenderQueue.getInstance());100101// add a record to the Disposer so that we destroy the native102// CGLGraphicsConfigInfo data when this object goes away103Disposer.addRecord(disposerReferent,104new CGLGCDisposerRecord(pConfigInfo));105}106107@Override108public Object getProxyKey() {109return this;110}111112@Override113public SurfaceData createManagedSurface(int w, int h, int transparency) {114return CGLSurfaceData.createData(this, w, h,115getColorModel(transparency),116null,117OGLSurfaceData.TEXTURE);118}119120public static CGLGraphicsConfig getConfig(CGraphicsDevice device)121{122if (!cglAvailable) {123return null;124}125126long cfginfo = 0;127int textureSize = 0;128final String[] ids = new String[1];129OGLRenderQueue rq = OGLRenderQueue.getInstance();130rq.lock();131try {132// getCGLConfigInfo() creates and destroys temporary133// surfaces/contexts, so we should first invalidate the current134// Java-level context and flush the queue...135OGLContext.invalidateCurrentContext();136cfginfo = getCGLConfigInfo();137if (cfginfo != 0L) {138textureSize = nativeGetMaxTextureSize();139// 7160609: GL still fails to create a square texture of this140// size. Half should be safe enough.141// Explicitly not support a texture more than 2^14, see 8010999.142textureSize = textureSize <= 16384 ? textureSize / 2 : 8192;143OGLContext.setScratchSurface(cfginfo);144rq.flushAndInvokeNow(() -> {145ids[0] = OGLContext.getOGLIdString();146});147}148} finally {149rq.unlock();150}151if (cfginfo == 0) {152return null;153}154155int oglCaps = getOGLCapabilities(cfginfo);156ContextCapabilities caps = new OGLContextCaps(oglCaps, ids[0]);157return new CGLGraphicsConfig(device, cfginfo, textureSize, caps);158}159160public static boolean isCGLAvailable() {161return cglAvailable;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 boolean isCapPresent(int cap) {170return ((oglCaps.getCaps() & cap) != 0);171}172173@Override174public long getNativeConfigInfo() {175return pConfigInfo;176}177178@Override179public 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 boolean isDoubleBuffered() {212return isCapPresent(CAPS_DOUBLEBUFFERED);213}214215private static class CGLGCDisposerRecord implements DisposerRecord {216private long pCfgInfo;217public CGLGCDisposerRecord(long pCfgInfo) {218this.pCfgInfo = pCfgInfo;219}220public void dispose() {221if (pCfgInfo != 0) {222OGLRenderQueue.disposeGraphicsConfig(pCfgInfo);223pCfgInfo = 0;224}225}226}227228// TODO: CGraphicsConfig doesn't implement displayChanged() yet229//@Override230public synchronized void displayChanged() {231//super.displayChanged();232233// the context could hold a reference to a CGLSurfaceData, which in234// turn has a reference back to this CGLGraphicsConfig, so in order235// for this instance to be disposed we need to break the connection236OGLRenderQueue rq = OGLRenderQueue.getInstance();237rq.lock();238try {239OGLContext.invalidateCurrentContext();240} finally {241rq.unlock();242}243}244245@Override246public String toString() {247return ("CGLGraphicsConfig[" + getDevice().getIDstring() + "]");248}249250@Override251public SurfaceData createSurfaceData(CFRetainedResource layer) {252return CGLSurfaceData.createData((CGLLayer) layer);253}254255@Override256public Image createAcceleratedImage(Component target,257int width, int height)258{259ColorModel model = getColorModel(Transparency.OPAQUE);260WritableRaster wr = model.createCompatibleWritableRaster(width, height);261return new OffScreenImage(target, model, wr,262model.isAlphaPremultiplied());263}264265@Override266public void assertOperationSupported(final int numBuffers,267final BufferCapabilities caps)268throws AWTException {269// Assume this method is never called with numBuffers != 2, as 0 is270// unsupported, and 1 corresponds to a SingleBufferStrategy which271// doesn't depend on the peer. Screen is considered as a separate272// "buffer".273if (numBuffers != 2) {274throw new AWTException("Only double buffering is supported");275}276final BufferCapabilities configCaps = getBufferCapabilities();277if (!configCaps.isPageFlipping()) {278throw new AWTException("Page flipping is not supported");279}280if (caps.getFlipContents() == BufferCapabilities.FlipContents.PRIOR) {281throw new AWTException("FlipContents.PRIOR is not supported");282}283}284285@Override286public Image createBackBuffer(final LWComponentPeer<?, ?> peer) {287final Rectangle r = peer.getBounds();288// It is possible for the component to have size 0x0, adjust it to289// be at least 1x1 to avoid IAE290final int w = Math.max(1, r.width);291final int h = Math.max(1, r.height);292final int transparency = peer.isTranslucent() ? Transparency.TRANSLUCENT293: Transparency.OPAQUE;294return new SunVolatileImage(this, w, h, transparency, null);295}296297@Override298public void destroyBackBuffer(final Image backBuffer) {299if (backBuffer != null) {300backBuffer.flush();301}302}303304@Override305public void flip(final LWComponentPeer<?, ?> peer, final Image backBuffer,306final int x1, final int y1, final int x2, final int y2,307final BufferCapabilities.FlipContents flipAction) {308final Graphics g = peer.getGraphics();309try {310g.drawImage(backBuffer, x1, y1, x2, y2, x1, y1, x2, y2, null);311} finally {312g.dispose();313}314if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {315final Graphics2D bg = (Graphics2D) backBuffer.getGraphics();316try {317bg.setBackground(peer.getBackground());318bg.clearRect(0, 0, backBuffer.getWidth(null),319backBuffer.getHeight(null));320} finally {321bg.dispose();322}323}324}325326private static class CGLBufferCaps extends BufferCapabilities {327public CGLBufferCaps(boolean dblBuf) {328super(imageCaps, imageCaps,329dblBuf ? FlipContents.UNDEFINED : null);330}331}332333@Override334public BufferCapabilities getBufferCapabilities() {335if (bufferCaps == null) {336bufferCaps = new CGLBufferCaps(isDoubleBuffered());337}338return bufferCaps;339}340341private static class CGLImageCaps extends ImageCapabilities {342private CGLImageCaps() {343super(true);344}345public boolean isTrueVolatile() {346return true;347}348}349350@Override351public ImageCapabilities getImageCapabilities() {352return imageCaps;353}354355@Override356public VolatileImage createCompatibleVolatileImage(int width, int height,357int transparency,358int type) {359if ((type != FBOBJECT && type != TEXTURE)360|| transparency == Transparency.BITMASK361|| type == FBOBJECT && !isCapPresent(CAPS_EXT_FBOBJECT)) {362return null;363}364SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,365transparency, type);366Surface sd = vi.getDestSurface();367if (!(sd instanceof AccelSurface) ||368((AccelSurface)sd).getType() != type)369{370vi.flush();371vi = null;372}373374return vi;375}376377@Override378public ContextCapabilities getContextCapabilities() {379return oglCaps;380}381382@Override383public int getMaxTextureWidth() {384return Math.max(maxTextureSize / getDevice().getScaleFactor(),385getBounds().width);386}387388@Override389public int getMaxTextureHeight() {390return Math.max(maxTextureSize / getDevice().getScaleFactor(),391getBounds().height);392}393}394395396