Path: blob/master/src/java.desktop/windows/classes/sun/java2d/d3d/D3DGraphicsDevice.java
41159 views
/*1* Copyright (c) 2007, 2019, 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.d3d;2627import java.awt.Dialog;28import java.awt.DisplayMode;29import java.awt.Frame;30import java.awt.GraphicsConfiguration;31import java.awt.Rectangle;32import java.awt.Toolkit;33import java.awt.Window;34import java.awt.event.WindowAdapter;35import java.awt.event.WindowEvent;36import java.awt.event.WindowListener;37import java.awt.peer.WindowPeer;38import java.util.ArrayList;3940import sun.awt.AWTAccessor;41import sun.awt.AWTAccessor.ComponentAccessor;42import sun.awt.Win32GraphicsDevice;43import sun.awt.windows.WWindowPeer;44import sun.java2d.d3d.D3DContext.D3DContextCaps;45import sun.java2d.pipe.hw.ContextCapabilities;46import sun.java2d.windows.WindowsFlags;4748import static sun.java2d.d3d.D3DContext.D3DContextCaps.CAPS_DEVICE_OK;49import static sun.java2d.d3d.D3DContext.D3DContextCaps.CAPS_EMPTY;5051/**52* This class implements D3D-specific functionality, such as fullscreen53* exclusive mode and display changes. It is kept separate from54* Win32GraphicsDevice to help avoid overburdening the parent class.55*/56public final class D3DGraphicsDevice extends Win32GraphicsDevice {57private D3DContext context;5859private static boolean d3dAvailable;6061private ContextCapabilities d3dCaps;6263private static native boolean initD3D();6465static {66// loading the library doesn't help because we need the67// toolkit thread running, so we have to call getDefaultToolkit()68Toolkit.getDefaultToolkit();69d3dAvailable = initD3D();70if (d3dAvailable) {71// we don't use pixel formats for the d3d pipeline72pfDisabled = true;73}74}7576/**77* Used to construct a Direct3D-enabled GraphicsDevice.78*79* @return a D3DGraphicsDevice if it could be created80* successfully, null otherwise.81*/82public static D3DGraphicsDevice createDevice(int screen) {83if (!d3dAvailable) {84return null;85}8687ContextCapabilities d3dCaps = getDeviceCaps(screen);88// could not initialize the device successfully89if ((d3dCaps.getCaps() & CAPS_DEVICE_OK) == 0) {90if (WindowsFlags.isD3DVerbose()) {91System.out.println("Could not enable Direct3D pipeline on " +92"screen " + screen);93}94return null;95}96if (WindowsFlags.isD3DVerbose()) {97System.out.println("Direct3D pipeline enabled on screen " + screen);98}99100D3DGraphicsDevice gd = new D3DGraphicsDevice(screen, d3dCaps);101return gd;102}103104private static native int getDeviceCapsNative(int screen);105private static native String getDeviceIdNative(int screen);106private static ContextCapabilities getDeviceCaps(final int screen) {107ContextCapabilities d3dCaps = null;108D3DRenderQueue rq = D3DRenderQueue.getInstance();109rq.lock();110try {111class Result {112int caps;113String id;114};115final Result res = new Result();116rq.flushAndInvokeNow(new Runnable() {117public void run() {118res.caps = getDeviceCapsNative(screen);119res.id = getDeviceIdNative(screen);120}121});122d3dCaps = new D3DContextCaps(res.caps, res.id);123} finally {124rq.unlock();125}126127return d3dCaps != null ? d3dCaps : new D3DContextCaps(CAPS_EMPTY, null);128}129130public final boolean isCapPresent(int cap) {131return ((d3dCaps.getCaps() & cap) != 0);132}133134private D3DGraphicsDevice(int screennum, ContextCapabilities d3dCaps) {135super(screennum);136descString = "D3DGraphicsDevice[screen="+screennum;137this.d3dCaps = d3dCaps;138context = new D3DContext(D3DRenderQueue.getInstance(), this);139}140141public boolean isD3DEnabledOnDevice() {142return isValid() && isCapPresent(CAPS_DEVICE_OK);143}144145/**146* Returns true if d3d pipeline has been successfully initialized.147* @return true if d3d pipeline is initialized, false otherwise148*/149public static boolean isD3DAvailable() {150return d3dAvailable;151}152153/**154* Return the owning Frame for a given Window. Used in setFSWindow below155* to set the properties of the owning Frame when a Window goes156* into fullscreen mode.157*/158private Frame getToplevelOwner(Window w) {159Window owner = w;160while (owner != null) {161owner = owner.getOwner();162if (owner instanceof Frame) {163return (Frame) owner;164}165}166// could get here if passed Window is an owner-less Dialog167return null;168}169170private boolean fsStatus;171private Rectangle ownerOrigBounds = null;172private boolean ownerWasVisible;173private Window realFSWindow;174private WindowListener fsWindowListener;175private boolean fsWindowWasAlwaysOnTop;176private static native boolean enterFullScreenExclusiveNative(int screen,177long hwnd);178179@Override180protected void enterFullScreenExclusive(final int screen, WindowPeer wp)181{182final WWindowPeer wpeer = AWTAccessor.getComponentAccessor()183.getPeer(realFSWindow);184D3DRenderQueue rq = D3DRenderQueue.getInstance();185rq.lock();186try {187rq.flushAndInvokeNow(new Runnable() {188public void run() {189long hwnd = wpeer.getHWnd();190if (hwnd == 0l) {191// window is disposed192fsStatus = false;193return;194}195fsStatus = enterFullScreenExclusiveNative(screen, hwnd);196}197});198} finally {199rq.unlock();200}201if (!fsStatus) {202super.enterFullScreenExclusive(screen, wp);203}204}205206private static native boolean exitFullScreenExclusiveNative(int screen);207@Override208protected void exitFullScreenExclusive(final int screen, WindowPeer w) {209if (fsStatus) {210D3DRenderQueue rq = D3DRenderQueue.getInstance();211rq.lock();212try {213rq.flushAndInvokeNow(new Runnable() {214public void run() {215exitFullScreenExclusiveNative(screen);216}217});218} finally {219rq.unlock();220}221} else {222super.exitFullScreenExclusive(screen, w);223}224}225226/**227* WindowAdapter class for the full-screen frame, responsible for228* restoring the devices. This is important to do because unless the device229* is restored it will not go back into the FS mode once alt+tabbed out.230* This is a problem for windows for which we do not do any d3d-related231* operations (like when we disabled on-screen rendering).232*233* REMIND: we create an instance per each full-screen device while a single234* instance would suffice (but requires more management).235*/236private static class D3DFSWindowAdapter extends WindowAdapter {237@Override238@SuppressWarnings("static")239public void windowDeactivated(WindowEvent e) {240D3DRenderQueue.getInstance().restoreDevices();241}242@Override243@SuppressWarnings("static")244public void windowActivated(WindowEvent e) {245D3DRenderQueue.getInstance().restoreDevices();246}247}248249@Override250protected void addFSWindowListener(Window w) {251// if the window is not a toplevel (has an owner) we have to use the252// real toplevel to enter the full-screen mode with (4933099).253final ComponentAccessor acc = AWTAccessor.getComponentAccessor();254if (!(w instanceof Frame) && !(w instanceof Dialog) &&255(realFSWindow = getToplevelOwner(w)) != null)256{257ownerOrigBounds = realFSWindow.getBounds();258WWindowPeer fp = acc.getPeer(realFSWindow);259ownerWasVisible = realFSWindow.isVisible();260Rectangle r = w.getBounds();261// we use operations on peer instead of component because calling262// them on component will take the tree lock263fp.reshape(r.x, r.y, r.width, r.height);264fp.setVisible(true);265} else {266realFSWindow = w;267}268269fsWindowWasAlwaysOnTop = realFSWindow.isAlwaysOnTop();270((WWindowPeer) acc.getPeer(realFSWindow)).setAlwaysOnTop(true);271272fsWindowListener = new D3DFSWindowAdapter();273realFSWindow.addWindowListener(fsWindowListener);274}275276@Override277protected void removeFSWindowListener(Window w) {278realFSWindow.removeWindowListener(fsWindowListener);279fsWindowListener = null;280281/**282* Bug 4933099: There is some funny-business to deal with when this283* method is called with a Window instead of a Frame. See 4836744284* for more information on this. One side-effect of our workaround285* for the problem is that the owning Frame of a Window may end286* up getting resized during the fullscreen process. When we287* return from fullscreen mode, we should resize the Frame to288* its original size (just like the Window is being resized289* to its original size in GraphicsDevice).290*/291final WWindowPeer wpeer = AWTAccessor.getComponentAccessor()292.getPeer(realFSWindow);293if (wpeer != null) {294if (ownerOrigBounds != null) {295// if the window went into fs mode before it was realized it296// could have (0,0) dimensions297if (ownerOrigBounds.width == 0) ownerOrigBounds.width = 1;298if (ownerOrigBounds.height == 0) ownerOrigBounds.height = 1;299wpeer.reshape(ownerOrigBounds.x, ownerOrigBounds.y,300ownerOrigBounds.width, ownerOrigBounds.height);301if (!ownerWasVisible) {302wpeer.setVisible(false);303}304ownerOrigBounds = null;305}306if (!fsWindowWasAlwaysOnTop) {307wpeer.setAlwaysOnTop(false);308}309}310311realFSWindow = null;312}313314private static native DisplayMode getCurrentDisplayModeNative(int screen);315@Override316protected DisplayMode getCurrentDisplayMode(final int screen) {317D3DRenderQueue rq = D3DRenderQueue.getInstance();318rq.lock();319try {320class Result {321DisplayMode dm = null;322};323final Result res = new Result();324rq.flushAndInvokeNow(new Runnable() {325public void run() {326res.dm = getCurrentDisplayModeNative(screen);327}328});329if (res.dm == null) {330return super.getCurrentDisplayMode(screen);331}332return res.dm;333} finally {334rq.unlock();335}336}337private static native void configDisplayModeNative(int screen, long hwnd,338int width, int height,339int bitDepth,340int refreshRate);341@Override342protected void configDisplayMode(final int screen, final WindowPeer w,343final int width, final int height,344final int bitDepth, final int refreshRate)345{346// we entered fs mode via gdi347if (!fsStatus) {348super.configDisplayMode(screen, w, width, height, bitDepth,349refreshRate);350return;351}352final WWindowPeer wpeer = AWTAccessor.getComponentAccessor()353.getPeer(realFSWindow);354355// REMIND: we do this before we switch the display mode, so356// the dimensions may be exceeding the dimensions of the screen,357// is this a problem?358359// update the bounds of the owner frame360if (getFullScreenWindow() != realFSWindow) {361Rectangle screenBounds = getDefaultConfiguration().getBounds();362wpeer.reshape(screenBounds.x, screenBounds.y, width, height);363}364365D3DRenderQueue rq = D3DRenderQueue.getInstance();366rq.lock();367try {368rq.flushAndInvokeNow(new Runnable() {369public void run() {370long hwnd = wpeer.getHWnd();371if (hwnd == 0l) {372// window is disposed373return;374}375// REMIND: do we really need a window here?376// we should probably just use the current one377configDisplayModeNative(screen, hwnd, width, height,378bitDepth, refreshRate);379}380});381} finally {382rq.unlock();383}384}385386private static native void enumDisplayModesNative(int screen,387ArrayList<DisplayMode> modes);388@Override389protected void enumDisplayModes(final int screen, final ArrayList<DisplayMode> modes) {390D3DRenderQueue rq = D3DRenderQueue.getInstance();391rq.lock();392try {393rq.flushAndInvokeNow(new Runnable() {394public void run() {395enumDisplayModesNative(screen, modes);396}397});398if (modes.size() == 0) {399modes.add(getCurrentDisplayModeNative(screen));400}401} finally {402rq.unlock();403}404}405406private static native long getAvailableAcceleratedMemoryNative(int screen);407@Override408public int getAvailableAcceleratedMemory() {409D3DRenderQueue rq = D3DRenderQueue.getInstance();410rq.lock();411try {412class Result {413long mem = 0L;414};415final Result res = new Result();416rq.flushAndInvokeNow(new Runnable() {417public void run() {418res.mem = getAvailableAcceleratedMemoryNative(getScreen());419}420});421return (int)res.mem;422} finally {423rq.unlock();424}425}426427@Override428public GraphicsConfiguration[] getConfigurations() {429if (configs == null) {430if (isD3DEnabledOnDevice()) {431defaultConfig = getDefaultConfiguration();432if (defaultConfig != null) {433configs = new GraphicsConfiguration[1];434configs[0] = defaultConfig;435return configs.clone();436}437}438}439return super.getConfigurations();440}441442@Override443public GraphicsConfiguration getDefaultConfiguration() {444if (defaultConfig == null) {445if (isD3DEnabledOnDevice()) {446defaultConfig = new D3DGraphicsConfig(this);447} else {448defaultConfig = super.getDefaultConfiguration();449}450}451return defaultConfig;452}453454private static native boolean isD3DAvailableOnDeviceNative(int screen);455// REMIND: this method is not used now, we use caps instead456public static boolean isD3DAvailableOnDevice(final int screen) {457if (!d3dAvailable) {458return false;459}460461// REMIND: should we cache the result per device somehow,462// and then reset and retry it on display change?463D3DRenderQueue rq = D3DRenderQueue.getInstance();464rq.lock();465try {466class Result {467boolean avail = false;468};469final Result res = new Result();470rq.flushAndInvokeNow(new Runnable() {471public void run() {472res.avail = isD3DAvailableOnDeviceNative(screen);473}474});475return res.avail;476} finally {477rq.unlock();478}479}480481D3DContext getContext() {482return context;483}484485ContextCapabilities getContextCapabilities() {486return d3dCaps;487}488489@Override490public void displayChanged() {491super.displayChanged();492// REMIND: make sure this works when the device is lost and we don't493// disable d3d too eagerly494if (d3dAvailable) {495d3dCaps = getDeviceCaps(getScreen());496}497}498499@Override500protected void invalidate(int defaultScreen) {501super.invalidate(defaultScreen);502// REMIND: this is a bit excessive, isD3DEnabledOnDevice will return503// false anyway because the device is invalid504d3dCaps = new D3DContextCaps(CAPS_EMPTY, null);505}506}507508509