430N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
430N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
430N/A *
430N/A * This code is free software; you can redistribute it and/or modify it
430N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
430N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
430N/A *
430N/A * This code is distributed in the hope that it will be useful, but WITHOUT
430N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
430N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
430N/A * version 2 for more details (a copy is included in the LICENSE file that
430N/A * accompanied this code).
430N/A *
430N/A * You should have received a copy of the GNU General Public License version
430N/A * 2 along with this work; if not, write to the Free Software Foundation,
430N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
430N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
430N/A */
430N/A
430N/Apackage sun.java2d.d3d;
430N/A
430N/Aimport java.awt.Dialog;
430N/Aimport java.awt.DisplayMode;
430N/Aimport java.awt.Frame;
430N/Aimport java.awt.GraphicsConfiguration;
430N/Aimport java.awt.Rectangle;
430N/Aimport java.awt.Toolkit;
430N/Aimport java.awt.Window;
430N/Aimport java.awt.event.WindowAdapter;
430N/Aimport java.awt.event.WindowEvent;
430N/Aimport java.awt.event.WindowListener;
430N/Aimport java.awt.peer.WindowPeer;
430N/Aimport java.util.ArrayList;
430N/Aimport sun.awt.Win32GraphicsDevice;
430N/Aimport sun.awt.windows.WWindowPeer;
430N/Aimport sun.java2d.pipe.hw.ContextCapabilities;
430N/Aimport sun.java2d.windows.WindowsFlags;
430N/Aimport static sun.java2d.pipe.BufferedOpCodes.*;
430N/Aimport static sun.java2d.d3d.D3DContext.D3DContextCaps.*;
430N/Aimport sun.java2d.d3d.D3DContext.D3DContextCaps;
430N/A
430N/A/**
430N/A * This class implements D3D-specific functionality, such as fullscreen
430N/A * exclusive mode and display changes. It is kept separate from
430N/A * Win32GraphicsDevice to help avoid overburdening the parent class.
430N/A */
430N/Apublic class D3DGraphicsDevice extends Win32GraphicsDevice {
430N/A private D3DContext context;
430N/A
430N/A private static boolean d3dAvailable;
430N/A
430N/A private ContextCapabilities d3dCaps;
430N/A
430N/A private static native boolean initD3D();
430N/A
430N/A static {
430N/A // loading the library doesn't help because we need the
430N/A // toolkit thread running, so we have to call getDefaultToolkit()
430N/A Toolkit.getDefaultToolkit();
430N/A d3dAvailable = initD3D();
430N/A if (d3dAvailable) {
430N/A // we don't use pixel formats for the d3d pipeline
430N/A pfDisabled = true;
1660N/A sun.misc.PerfCounter.getD3DAvailable().set(1);
1660N/A } else {
1660N/A sun.misc.PerfCounter.getD3DAvailable().set(0);
430N/A }
430N/A }
430N/A
430N/A /**
430N/A * Used to construct a Direct3D-enabled GraphicsDevice.
430N/A *
430N/A * @return a D3DGraphicsDevice if it could be created
430N/A * successfully, null otherwise.
430N/A */
430N/A public static D3DGraphicsDevice createDevice(int screen) {
430N/A if (!d3dAvailable) {
430N/A return null;
430N/A }
430N/A
430N/A ContextCapabilities d3dCaps = getDeviceCaps(screen);
430N/A // could not initialize the device successfully
430N/A if ((d3dCaps.getCaps() & CAPS_DEVICE_OK) == 0) {
430N/A if (WindowsFlags.isD3DVerbose()) {
430N/A System.out.println("Could not enable Direct3D pipeline on " +
430N/A "screen " + screen);
430N/A }
430N/A return null;
430N/A }
430N/A if (WindowsFlags.isD3DVerbose()) {
430N/A System.out.println("Direct3D pipeline enabled on screen " + screen);
430N/A }
430N/A
430N/A D3DGraphicsDevice gd = new D3DGraphicsDevice(screen, d3dCaps);
430N/A return gd;
430N/A }
430N/A
430N/A private static native int getDeviceCapsNative(int screen);
430N/A private static native String getDeviceIdNative(int screen);
430N/A private static ContextCapabilities getDeviceCaps(final int screen) {
430N/A ContextCapabilities d3dCaps = null;
430N/A D3DRenderQueue rq = D3DRenderQueue.getInstance();
430N/A rq.lock();
430N/A try {
430N/A class Result {
430N/A int caps;
430N/A String id;
430N/A };
430N/A final Result res = new Result();
430N/A rq.flushAndInvokeNow(new Runnable() {
430N/A public void run() {
430N/A res.caps = getDeviceCapsNative(screen);
430N/A res.id = getDeviceIdNative(screen);
430N/A }
430N/A });
430N/A d3dCaps = new D3DContextCaps(res.caps, res.id);
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A
430N/A return d3dCaps != null ? d3dCaps : new D3DContextCaps(CAPS_EMPTY, null);
430N/A }
430N/A
430N/A public final boolean isCapPresent(int cap) {
430N/A return ((d3dCaps.getCaps() & cap) != 0);
430N/A }
430N/A
430N/A private D3DGraphicsDevice(int screennum, ContextCapabilities d3dCaps) {
430N/A super(screennum);
430N/A descString = "D3DGraphicsDevice[screen="+screennum;
430N/A this.d3dCaps = d3dCaps;
430N/A context = new D3DContext(D3DRenderQueue.getInstance(), this);
430N/A }
430N/A
430N/A public boolean isD3DEnabledOnDevice() {
430N/A return isValid() && isCapPresent(CAPS_DEVICE_OK);
430N/A }
430N/A
430N/A /**
430N/A * Returns true if d3d pipeline has been successfully initialized.
430N/A * @return true if d3d pipeline is initialized, false otherwise
430N/A */
430N/A public static boolean isD3DAvailable() {
430N/A return d3dAvailable;
430N/A }
430N/A
430N/A /**
430N/A * Return the owning Frame for a given Window. Used in setFSWindow below
430N/A * to set the properties of the owning Frame when a Window goes
430N/A * into fullscreen mode.
430N/A */
430N/A private Frame getToplevelOwner(Window w) {
430N/A Window owner = w;
430N/A while (owner != null) {
430N/A owner = owner.getOwner();
430N/A if (owner instanceof Frame) {
430N/A return (Frame) owner;
430N/A }
430N/A }
430N/A // could get here if passed Window is an owner-less Dialog
430N/A return null;
430N/A }
430N/A
430N/A private boolean fsStatus;
430N/A private Rectangle ownerOrigBounds = null;
430N/A private boolean ownerWasVisible;
430N/A private Window realFSWindow;
430N/A private WindowListener fsWindowListener;
430N/A private boolean fsWindowWasAlwaysOnTop;
430N/A private static native boolean enterFullScreenExclusiveNative(int screen,
430N/A long hwnd);
430N/A
430N/A @Override
430N/A protected void enterFullScreenExclusive(final int screen, WindowPeer wp)
430N/A {
430N/A final WWindowPeer wpeer = (WWindowPeer)realFSWindow.getPeer();
430N/A
430N/A D3DRenderQueue rq = D3DRenderQueue.getInstance();
430N/A rq.lock();
430N/A try {
430N/A rq.flushAndInvokeNow(new Runnable() {
430N/A public void run() {
430N/A long hwnd = wpeer.getHWnd();
430N/A if (hwnd == 0l) {
430N/A // window is disposed
430N/A fsStatus = false;
430N/A return;
430N/A }
430N/A fsStatus = enterFullScreenExclusiveNative(screen, hwnd);
430N/A }
430N/A });
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A if (!fsStatus) {
430N/A super.enterFullScreenExclusive(screen, wp);
430N/A }
430N/A }
430N/A
430N/A private static native boolean exitFullScreenExclusiveNative(int screen);
430N/A @Override
430N/A protected void exitFullScreenExclusive(final int screen, WindowPeer w) {
430N/A if (fsStatus) {
430N/A D3DRenderQueue rq = D3DRenderQueue.getInstance();
430N/A rq.lock();
430N/A try {
430N/A rq.flushAndInvokeNow(new Runnable() {
430N/A public void run() {
430N/A exitFullScreenExclusiveNative(screen);
430N/A }
430N/A });
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A } else {
430N/A super.exitFullScreenExclusive(screen, w);
430N/A }
430N/A }
430N/A
430N/A /**
430N/A * WindowAdapter class for the full-screen frame, responsible for
430N/A * restoring the devices. This is important to do because unless the device
430N/A * is restored it will not go back into the FS mode once alt+tabbed out.
430N/A * This is a problem for windows for which we do not do any d3d-related
430N/A * operations (like when we disabled on-screen rendering).
430N/A *
430N/A * REMIND: we create an instance per each full-screen device while a single
430N/A * instance would suffice (but requires more management).
430N/A */
430N/A private static class D3DFSWindowAdapter extends WindowAdapter {
430N/A @Override
430N/A public void windowDeactivated(WindowEvent e) {
430N/A D3DRenderQueue.getInstance().restoreDevices();
430N/A }
430N/A @Override
430N/A public void windowActivated(WindowEvent e) {
430N/A D3DRenderQueue.getInstance().restoreDevices();
430N/A }
430N/A }
430N/A
430N/A @Override
430N/A protected void addFSWindowListener(Window w) {
430N/A // if the window is not a toplevel (has an owner) we have to use the
430N/A // real toplevel to enter the full-screen mode with (4933099).
430N/A if (!(w instanceof Frame) && !(w instanceof Dialog) &&
430N/A (realFSWindow = getToplevelOwner(w)) != null)
430N/A {
430N/A ownerOrigBounds = realFSWindow.getBounds();
430N/A WWindowPeer fp = (WWindowPeer)realFSWindow.getPeer();
430N/A
430N/A ownerWasVisible = realFSWindow.isVisible();
430N/A Rectangle r = w.getBounds();
430N/A // we use operations on peer instead of component because calling
430N/A // them on component will take the tree lock
430N/A fp.reshape(r.x, r.y, r.width, r.height);
430N/A fp.setVisible(true);
430N/A } else {
430N/A realFSWindow = w;
430N/A }
430N/A
430N/A fsWindowWasAlwaysOnTop = realFSWindow.isAlwaysOnTop();
430N/A ((WWindowPeer)realFSWindow.getPeer()).setAlwaysOnTop(true);
430N/A
430N/A fsWindowListener = new D3DFSWindowAdapter();
430N/A realFSWindow.addWindowListener(fsWindowListener);
430N/A }
430N/A
430N/A @Override
430N/A protected void removeFSWindowListener(Window w) {
430N/A realFSWindow.removeWindowListener(fsWindowListener);
430N/A fsWindowListener = null;
430N/A
430N/A /**
430N/A * Bug 4933099: There is some funny-business to deal with when this
430N/A * method is called with a Window instead of a Frame. See 4836744
430N/A * for more information on this. One side-effect of our workaround
430N/A * for the problem is that the owning Frame of a Window may end
430N/A * up getting resized during the fullscreen process. When we
430N/A * return from fullscreen mode, we should resize the Frame to
430N/A * its original size (just like the Window is being resized
430N/A * to its original size in GraphicsDevice).
430N/A */
430N/A WWindowPeer wpeer = (WWindowPeer)realFSWindow.getPeer();
430N/A if (wpeer != null) {
430N/A if (ownerOrigBounds != null) {
430N/A // if the window went into fs mode before it was realized it
430N/A // could have (0,0) dimensions
430N/A if (ownerOrigBounds.width == 0) ownerOrigBounds.width = 1;
430N/A if (ownerOrigBounds.height == 0) ownerOrigBounds.height = 1;
430N/A wpeer.reshape(ownerOrigBounds.x, ownerOrigBounds.y,
430N/A ownerOrigBounds.width, ownerOrigBounds.height);
430N/A if (!ownerWasVisible) {
430N/A wpeer.setVisible(false);
430N/A }
430N/A ownerOrigBounds = null;
430N/A }
430N/A if (!fsWindowWasAlwaysOnTop) {
430N/A wpeer.setAlwaysOnTop(false);
430N/A }
430N/A }
430N/A
430N/A realFSWindow = null;
430N/A }
430N/A
430N/A private static native DisplayMode getCurrentDisplayModeNative(int screen);
430N/A @Override
430N/A protected DisplayMode getCurrentDisplayMode(final int screen) {
430N/A D3DRenderQueue rq = D3DRenderQueue.getInstance();
430N/A rq.lock();
430N/A try {
430N/A class Result {
430N/A DisplayMode dm = null;
430N/A };
430N/A final Result res = new Result();
430N/A rq.flushAndInvokeNow(new Runnable() {
430N/A public void run() {
430N/A res.dm = getCurrentDisplayModeNative(screen);
430N/A }
430N/A });
430N/A if (res.dm == null) {
430N/A return super.getCurrentDisplayMode(screen);
430N/A }
430N/A return res.dm;
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A private static native void configDisplayModeNative(int screen, long hwnd,
430N/A int width, int height,
430N/A int bitDepth,
430N/A int refreshRate);
430N/A @Override
430N/A protected void configDisplayMode(final int screen, final WindowPeer w,
430N/A final int width, final int height,
430N/A final int bitDepth, final int refreshRate)
430N/A {
430N/A // we entered fs mode via gdi
430N/A if (!fsStatus) {
430N/A super.configDisplayMode(screen, w, width, height, bitDepth,
430N/A refreshRate);
430N/A return;
430N/A }
430N/A
430N/A final WWindowPeer wpeer = (WWindowPeer)realFSWindow.getPeer();
430N/A
430N/A // REMIND: we do this before we switch the display mode, so
430N/A // the dimensions may be exceeding the dimensions of the screen,
430N/A // is this a problem?
430N/A
430N/A // update the bounds of the owner frame
430N/A if (getFullScreenWindow() != realFSWindow) {
430N/A Rectangle screenBounds = getDefaultConfiguration().getBounds();
430N/A wpeer.reshape(screenBounds.x, screenBounds.y, width, height);
430N/A }
430N/A
430N/A D3DRenderQueue rq = D3DRenderQueue.getInstance();
430N/A rq.lock();
430N/A try {
430N/A rq.flushAndInvokeNow(new Runnable() {
430N/A public void run() {
430N/A long hwnd = wpeer.getHWnd();
430N/A if (hwnd == 0l) {
430N/A // window is disposed
430N/A return;
430N/A }
430N/A // REMIND: do we really need a window here?
430N/A // we should probably just use the current one
430N/A configDisplayModeNative(screen, hwnd, width, height,
430N/A bitDepth, refreshRate);
430N/A }
430N/A });
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A
430N/A private static native void enumDisplayModesNative(int screen,
430N/A ArrayList modes);
430N/A @Override
430N/A protected void enumDisplayModes(final int screen, final ArrayList modes) {
430N/A D3DRenderQueue rq = D3DRenderQueue.getInstance();
430N/A rq.lock();
430N/A try {
430N/A rq.flushAndInvokeNow(new Runnable() {
430N/A public void run() {
430N/A enumDisplayModesNative(screen, modes);
430N/A }
430N/A });
430N/A if (modes.size() == 0) {
430N/A modes.add(getCurrentDisplayModeNative(screen));
430N/A }
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A
430N/A private static native long getAvailableAcceleratedMemoryNative(int screen);
430N/A @Override
430N/A public int getAvailableAcceleratedMemory() {
430N/A D3DRenderQueue rq = D3DRenderQueue.getInstance();
430N/A rq.lock();
430N/A try {
430N/A class Result {
430N/A long mem = 0L;
430N/A };
430N/A final Result res = new Result();
430N/A rq.flushAndInvokeNow(new Runnable() {
430N/A public void run() {
430N/A res.mem = getAvailableAcceleratedMemoryNative(getScreen());
430N/A }
430N/A });
430N/A return (int)res.mem;
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A
430N/A @Override
430N/A public GraphicsConfiguration[] getConfigurations() {
430N/A if (configs == null) {
430N/A if (isD3DEnabledOnDevice()) {
430N/A defaultConfig = getDefaultConfiguration();
430N/A if (defaultConfig != null) {
430N/A configs = new GraphicsConfiguration[1];
430N/A configs[0] = defaultConfig;
1838N/A return configs.clone();
430N/A }
430N/A }
430N/A }
430N/A return super.getConfigurations();
430N/A }
430N/A
430N/A @Override
430N/A public GraphicsConfiguration getDefaultConfiguration() {
430N/A if (defaultConfig == null) {
430N/A if (isD3DEnabledOnDevice()) {
430N/A defaultConfig = new D3DGraphicsConfig(this);
430N/A } else {
430N/A defaultConfig = super.getDefaultConfiguration();
430N/A }
430N/A }
430N/A return defaultConfig;
430N/A }
430N/A
430N/A private static native boolean isD3DAvailableOnDeviceNative(int screen);
430N/A // REMIND: this method is not used now, we use caps instead
430N/A public static boolean isD3DAvailableOnDevice(final int screen) {
430N/A if (!d3dAvailable) {
430N/A return false;
430N/A }
430N/A
430N/A // REMIND: should we cache the result per device somehow,
430N/A // and then reset and retry it on display change?
430N/A D3DRenderQueue rq = D3DRenderQueue.getInstance();
430N/A rq.lock();
430N/A try {
430N/A class Result {
430N/A boolean avail = false;
430N/A };
430N/A final Result res = new Result();
430N/A rq.flushAndInvokeNow(new Runnable() {
430N/A public void run() {
430N/A res.avail = isD3DAvailableOnDeviceNative(screen);
430N/A }
430N/A });
430N/A return res.avail;
430N/A } finally {
430N/A rq.unlock();
430N/A }
430N/A }
430N/A
430N/A D3DContext getContext() {
430N/A return context;
430N/A }
430N/A
430N/A ContextCapabilities getContextCapabilities() {
430N/A return d3dCaps;
430N/A }
430N/A
430N/A @Override
430N/A public void displayChanged() {
430N/A super.displayChanged();
430N/A // REMIND: make sure this works when the device is lost and we don't
430N/A // disable d3d too eagerly
430N/A if (d3dAvailable) {
430N/A d3dCaps = getDeviceCaps(getScreen());
430N/A }
430N/A }
430N/A
430N/A @Override
430N/A protected void invalidate(int defaultScreen) {
430N/A super.invalidate(defaultScreen);
430N/A // REMIND: this is a bit excessive, isD3DEnabledOnDevice will return
430N/A // false anyway because the device is invalid
430N/A d3dCaps = new D3DContextCaps(CAPS_EMPTY, null);
430N/A }
430N/A}