Path: blob/master/src/java.desktop/unix/classes/sun/java2d/opengl/GLXSurfaceData.java
41159 views
/*1* Copyright (c) 2003, 2017, 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.GraphicsConfiguration;28import java.awt.GraphicsDevice;29import java.awt.GraphicsEnvironment;30import java.awt.Image;31import java.awt.Rectangle;32import java.awt.image.ColorModel;3334import sun.awt.X11ComponentPeer;35import sun.java2d.SurfaceData;3637public abstract class GLXSurfaceData extends OGLSurfaceData {3839protected X11ComponentPeer peer;40private GLXGraphicsConfig graphicsConfig;4142private native void initOps(OGLGraphicsConfig gc, X11ComponentPeer peer,43long aData);4445protected GLXSurfaceData(X11ComponentPeer peer, GLXGraphicsConfig gc,46ColorModel cm, int type)47{48super(gc, cm, type);49this.peer = peer;50this.graphicsConfig = gc;51initOps(gc, peer, graphicsConfig.getAData());52}5354public GraphicsConfiguration getDeviceConfiguration() {55return graphicsConfig;56}5758/**59* Creates a SurfaceData object representing the primary (front) buffer60* of an on-screen Window.61*/62public static GLXWindowSurfaceData createData(X11ComponentPeer peer) {63GLXGraphicsConfig gc = getGC(peer);64return new GLXWindowSurfaceData(peer, gc);65}6667/**68* Creates a SurfaceData object representing the back buffer of a69* double-buffered on-screen Window.70*/71public static GLXOffScreenSurfaceData createData(X11ComponentPeer peer,72Image image,73int type)74{75GLXGraphicsConfig gc = getGC(peer);76Rectangle r = peer.getBounds();77if (type == FLIP_BACKBUFFER) {78return new GLXOffScreenSurfaceData(peer, gc, r.width, r.height,79image, peer.getColorModel(),80FLIP_BACKBUFFER);81} else {82return new GLXVSyncOffScreenSurfaceData(peer, gc, r.width, r.height,83image, peer.getColorModel(),84type);85}86}8788/**89* Creates a SurfaceData object representing an off-screen buffer (either90* a FBO or Texture).91*/92public static GLXOffScreenSurfaceData createData(GLXGraphicsConfig gc,93int width, int height,94ColorModel cm,95Image image, int type)96{97return new GLXOffScreenSurfaceData(null, gc, width, height,98image, cm, type);99}100101public static GLXGraphicsConfig getGC(X11ComponentPeer peer) {102if (peer != null) {103return (GLXGraphicsConfig)peer.getGraphicsConfiguration();104} else {105// REMIND: this should rarely (never?) happen, but what if106// default config is not GLX?107GraphicsEnvironment env =108GraphicsEnvironment.getLocalGraphicsEnvironment();109GraphicsDevice gd = env.getDefaultScreenDevice();110return (GLXGraphicsConfig)gd.getDefaultConfiguration();111}112}113114public static class GLXWindowSurfaceData extends GLXSurfaceData {115protected final int scale;116117public GLXWindowSurfaceData(X11ComponentPeer peer,118GLXGraphicsConfig gc)119{120super(peer, gc, peer.getColorModel(), WINDOW);121scale = gc.getScale();122}123124public SurfaceData getReplacement() {125return peer.getSurfaceData();126}127128public Rectangle getBounds() {129Rectangle r = peer.getBounds();130r.x = r.y = 0;131r.width = (int) Math.ceil(r.width * scale);132r.height = (int) Math.ceil(r.height * scale);133return r;134}135136/**137* Returns destination Component associated with this SurfaceData.138*/139public Object getDestination() {140return peer.getTarget();141}142143@Override144public double getDefaultScaleX() {145return scale;146}147148@Override149public double getDefaultScaleY() {150return scale;151}152}153154/**155* A surface which implements a v-synced flip back-buffer with COPIED156* FlipContents.157*158* This surface serves as a back-buffer to the outside world, while159* it is actually an offscreen surface. When the BufferStrategy this surface160* belongs to is showed, it is first copied to the real private161* FLIP_BACKBUFFER, which is then flipped.162*/163public static class GLXVSyncOffScreenSurfaceData extends164GLXOffScreenSurfaceData165{166private GLXOffScreenSurfaceData flipSurface;167168public GLXVSyncOffScreenSurfaceData(X11ComponentPeer peer,169GLXGraphicsConfig gc,170int width, int height,171Image image, ColorModel cm,172int type)173{174super(peer, gc, width, height, image, cm, type);175flipSurface = GLXSurfaceData.createData(peer, image, FLIP_BACKBUFFER);176}177178public SurfaceData getFlipSurface() {179return flipSurface;180}181182@Override183public void flush() {184flipSurface.flush();185super.flush();186}187188}189190public static class GLXOffScreenSurfaceData extends GLXSurfaceData {191192private Image offscreenImage;193private int width, height;194private final int scale;195196public GLXOffScreenSurfaceData(X11ComponentPeer peer,197GLXGraphicsConfig gc,198int width, int height,199Image image, ColorModel cm,200int type)201{202super(peer, gc, cm, type);203204scale = gc.getDevice().getScaleFactor();205this.width = width * scale;206this.height = height * scale;207offscreenImage = image;208209initSurface(this.width, this.height);210}211212public SurfaceData getReplacement() {213return restoreContents(offscreenImage);214}215216public Rectangle getBounds() {217if (type == FLIP_BACKBUFFER) {218Rectangle r = peer.getBounds();219r.x = r.y = 0;220r.width = (int) Math.ceil(r.width * scale);221r.height = (int) Math.ceil(r.height * scale);222return r;223} else {224return new Rectangle(width, height);225}226}227228/**229* Returns destination Image associated with this SurfaceData.230*/231public Object getDestination() {232return offscreenImage;233}234235@Override236public double getDefaultScaleX() {237return scale;238}239240@Override241public double getDefaultScaleY() {242return scale;243}244}245}246247248