Path: blob/master/src/java.desktop/windows/classes/sun/java2d/opengl/WGLSurfaceData.java
41159 views
/*1* Copyright (c) 2004, 2015, 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.Component;28import java.awt.GraphicsConfiguration;29import java.awt.GraphicsDevice;30import java.awt.GraphicsEnvironment;31import java.awt.Image;32import java.awt.Rectangle;33import java.awt.geom.AffineTransform;34import java.awt.image.ColorModel;35import sun.awt.SunToolkit;36import sun.awt.Win32GraphicsDevice;37import sun.awt.windows.WComponentPeer;38import sun.java2d.SurfaceData;39import sun.java2d.pipe.Region;4041public abstract class WGLSurfaceData extends OGLSurfaceData {4243protected WComponentPeer peer;44private WGLGraphicsConfig graphicsConfig;45protected double scaleX = 1;46protected double scaleY = 1;4748private native void initOps(OGLGraphicsConfig gc, long pConfigInfo,49WComponentPeer peer, long hwnd);5051protected WGLSurfaceData(WComponentPeer peer, WGLGraphicsConfig gc,52ColorModel cm, int type)53{54super(gc, cm, type);55this.peer = peer;56this.graphicsConfig = gc;57Win32GraphicsDevice device = gc.getDevice();58this.scaleX = type == TEXTURE ? 1 : device.getDefaultScaleX();59this.scaleY = type == TEXTURE ? 1 : device.getDefaultScaleY();6061long pConfigInfo = gc.getNativeConfigInfo();62long hwnd = peer != null ? peer.getHWnd() : 0L;6364initOps(gc, pConfigInfo, peer, hwnd);65}6667@Override68public double getDefaultScaleX() {69return scaleX;70}7172@Override73public double getDefaultScaleY() {74return scaleY;75}7677public GraphicsConfiguration getDeviceConfiguration() {78return graphicsConfig;79}8081/**82* Creates a SurfaceData object representing the primary (front) buffer83* of an on-screen Window.84*/85public static WGLWindowSurfaceData createData(WComponentPeer peer) {86// the OGL pipeline can render directly to the screen and interfere87// with layered windows, which is why we don't allow accelerated88// surfaces in this case89if (!peer.isAccelCapable() ||90!SunToolkit.isContainingTopLevelOpaque((Component)peer.getTarget()))91{92return null;93}94WGLGraphicsConfig gc = getGC(peer);95return new WGLWindowSurfaceData(peer, gc);96}9798/**99* Creates a SurfaceData object representing the back buffer of a100* double-buffered on-screen Window.101*/102public static WGLOffScreenSurfaceData createData(WComponentPeer peer,103Image image,104int type)105{106// the OGL pipeline can render directly to the screen and interfere107// with layered windows, which is why we don't allow accelerated108// surfaces in this case109if (!peer.isAccelCapable() ||110!SunToolkit.isContainingTopLevelOpaque((Component)peer.getTarget()))111{112return null;113}114WGLGraphicsConfig gc = getGC(peer);115Rectangle r = peer.getBounds();116if (type == FLIP_BACKBUFFER) {117return new WGLOffScreenSurfaceData(peer, gc, r.width, r.height,118image, peer.getColorModel(),119type);120} else {121return new WGLVSyncOffScreenSurfaceData(peer, gc, r.width, r.height,122image, peer.getColorModel(),123type);124}125}126127/**128* Creates a SurfaceData object representing an off-screen buffer (either129* a Pbuffer or Texture).130*/131public static WGLOffScreenSurfaceData createData(WGLGraphicsConfig gc,132int width, int height,133ColorModel cm,134Image image, int type)135{136return new WGLOffScreenSurfaceData(null, gc, width, height,137image, cm, type);138}139140public static WGLGraphicsConfig getGC(WComponentPeer peer) {141if (peer != null) {142return (WGLGraphicsConfig)peer.getGraphicsConfiguration();143} else {144// REMIND: this should rarely (never?) happen, but what if145// default config is not WGL?146GraphicsEnvironment env =147GraphicsEnvironment.getLocalGraphicsEnvironment();148GraphicsDevice gd = env.getDefaultScreenDevice();149return (WGLGraphicsConfig)gd.getDefaultConfiguration();150}151}152153public static class WGLWindowSurfaceData extends WGLSurfaceData {154155public WGLWindowSurfaceData(WComponentPeer peer,156WGLGraphicsConfig gc)157{158super(peer, gc, peer.getColorModel(), WINDOW);159}160161public SurfaceData getReplacement() {162return peer.getSurfaceData();163}164165public Rectangle getBounds() {166Rectangle r = peer.getBounds();167r.x = r.y = 0;168r.width = Region.clipRound(r.width * scaleX);169r.height = Region.clipRound(r.height * scaleY);170return r;171}172173/**174* Returns destination Component associated with this SurfaceData.175*/176public Object getDestination() {177return peer.getTarget();178}179}180181/**182* A surface which implements a v-synced flip back-buffer with COPIED183* FlipContents.184*185* This surface serves as a back-buffer to the outside world, while186* it is actually an offscreen surface. When the BufferStrategy this surface187* belongs to is showed, it is first copied to the real private188* FLIP_BACKBUFFER, which is then flipped.189*/190public static class WGLVSyncOffScreenSurfaceData extends191WGLOffScreenSurfaceData192{193private WGLOffScreenSurfaceData flipSurface;194195public WGLVSyncOffScreenSurfaceData(WComponentPeer peer,196WGLGraphicsConfig gc,197int width, int height,198Image image, ColorModel cm,199int type)200{201super(peer, gc, width, height, image, cm, type);202flipSurface = WGLSurfaceData.createData(peer, image, FLIP_BACKBUFFER);203}204205public SurfaceData getFlipSurface() {206return flipSurface;207}208209@Override210public void flush() {211flipSurface.flush();212super.flush();213}214215}216217public static class WGLOffScreenSurfaceData extends WGLSurfaceData {218219private Image offscreenImage;220private int width, height;221222public WGLOffScreenSurfaceData(WComponentPeer peer,223WGLGraphicsConfig gc,224int width, int height,225Image image, ColorModel cm,226int type)227{228super(peer, gc, cm, type);229230this.width = Region.clipRound(width * scaleX);231this.height = Region.clipRound(height * scaleY);232offscreenImage = image;233234initSurface(this.width, this.height);235}236237public SurfaceData getReplacement() {238return restoreContents(offscreenImage);239}240241public Rectangle getBounds() {242if (type == FLIP_BACKBUFFER) {243Rectangle r = peer.getBounds();244r.width = Region.clipRound(r.width * scaleX);245r.height = Region.clipRound(r.height * scaleY);246r.x = r.y = 0;247return r;248} else {249return new Rectangle(width, height);250}251}252253/**254* Returns destination Image associated with this SurfaceData.255*/256public Object getDestination() {257return offscreenImage;258}259}260261/**262* Updates the layered window with the contents of the surface.263*264* @param psdops pointer to the native ogl sd structure265* @param peer pointer to the AwtWindow peer data266* @param w width of the window267* @param h height of the window268* @see sun.awt.windows.TranslucentWindowPainter269*/270public static native boolean updateWindowAccelImpl(long psdops,271WComponentPeer peer,272int w, int h);273}274275276