0N/A/*
3261N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/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
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/Apackage sun.awt;
0N/A
0N/Aimport java.awt.AWTPermission;
0N/Aimport java.awt.GraphicsDevice;
0N/Aimport java.awt.GraphicsConfiguration;
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.awt.DisplayMode;
3177N/Aimport java.awt.EventQueue;
0N/Aimport java.awt.Frame;
0N/Aimport java.awt.Rectangle;
0N/Aimport java.awt.Window;
430N/Aimport java.awt.event.WindowAdapter;
430N/Aimport java.awt.event.WindowEvent;
430N/Aimport java.awt.event.WindowListener;
0N/Aimport java.awt.image.ColorModel;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Vector;
0N/Aimport java.awt.peer.WindowPeer;
0N/Aimport sun.awt.windows.WWindowPeer;
0N/Aimport sun.java2d.opengl.WGLGraphicsConfig;
0N/Aimport sun.java2d.windows.WindowsFlags;
0N/A
0N/A/**
0N/A * This is an implementation of a GraphicsDevice object for a single
0N/A * Win32 screen.
0N/A *
0N/A * @see GraphicsEnvironment
0N/A * @see GraphicsConfiguration
0N/A */
0N/Apublic class Win32GraphicsDevice extends GraphicsDevice implements
0N/A DisplayChangedListener {
0N/A int screen;
0N/A ColorModel dynamicColorModel; // updated with dev changes
0N/A ColorModel colorModel; // static for device
430N/A protected GraphicsConfiguration[] configs;
430N/A protected GraphicsConfiguration defaultConfig;
0N/A
0N/A private final String idString;
430N/A protected String descString;
0N/A // Note that we do not synchronize access to this variable - it doesn't
0N/A // really matter if a thread does an operation on graphics device which is
0N/A // about to become invalid (or already become) - we are prepared to deal
0N/A // with this on the native level.
0N/A private boolean valid;
0N/A
0N/A // keep track of top-level windows on this display
0N/A private SunDisplayChanger topLevels = new SunDisplayChanger();
430N/A // REMIND: we may disable the use of pixel formats for some accelerated
430N/A // pipelines which are mutually exclusive with opengl, for which
430N/A // pixel formats were added in the first place
430N/A protected static boolean pfDisabled;
0N/A private static AWTPermission fullScreenExclusivePermission;
0N/A // the original display mode we had before entering the fullscreen
0N/A // mode
0N/A private DisplayMode defaultDisplayMode;
430N/A // activation/deactivation listener for the full-screen window
430N/A private WindowListener fsWindowListener;
0N/A
0N/A static {
0N/A
0N/A // 4455041 - Even when ddraw is disabled, ddraw.dll is loaded when
0N/A // pixel format calls are made. This causes problems when a Java app
0N/A // is run as an NT service. To prevent the loading of ddraw.dll
0N/A // completely, sun.awt.nopixfmt should be set as well. Apps which use
0N/A // OpenGL w/ Java probably don't want to set this.
0N/A String nopixfmt = (String)java.security.AccessController.doPrivileged(
0N/A new sun.security.action.GetPropertyAction("sun.awt.nopixfmt"));
0N/A pfDisabled = (nopixfmt != null);
0N/A initIDs();
0N/A }
0N/A
0N/A private static native void initIDs();
0N/A
0N/A native void initDevice(int screen);
0N/A
0N/A public Win32GraphicsDevice(int screennum) {
0N/A this.screen = screennum;
0N/A // we cache the strings because we want toString() and getIDstring
0N/A // to reflect the original screen number (which may change if the
0N/A // device is removed)
0N/A idString = "\\Display"+screen;
430N/A // REMIND: may be should use class name?
0N/A descString = "Win32GraphicsDevice[screen=" + screen;
0N/A valid = true;
0N/A
0N/A initDevice(screennum);
0N/A }
0N/A
0N/A /**
0N/A * Returns the type of the graphics device.
0N/A * @see #TYPE_RASTER_SCREEN
0N/A * @see #TYPE_PRINTER
0N/A * @see #TYPE_IMAGE_BUFFER
0N/A */
0N/A public int getType() {
0N/A return TYPE_RASTER_SCREEN;
0N/A }
0N/A
0N/A /**
0N/A * Returns the Win32 screen of the device.
0N/A */
0N/A public int getScreen() {
0N/A return screen;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether this is a valid devicie. Device can become
0N/A * invalid as a result of device removal event.
0N/A */
430N/A public boolean isValid() {
0N/A return valid;
0N/A }
0N/A
0N/A /**
0N/A * Called from native code when the device was removed.
0N/A *
0N/A * @param defaultScreen the current default screen
0N/A */
430N/A protected void invalidate(int defaultScreen) {
0N/A valid = false;
0N/A screen = defaultScreen;
0N/A }
0N/A
0N/A /**
0N/A * Returns the identification string associated with this graphics
0N/A * device.
0N/A */
0N/A public String getIDstring() {
0N/A return idString;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns all of the graphics
0N/A * configurations associated with this graphics device.
0N/A */
0N/A public GraphicsConfiguration[] getConfigurations() {
0N/A if (configs==null) {
0N/A if (WindowsFlags.isOGLEnabled() && isDefaultDevice()) {
0N/A defaultConfig = getDefaultConfiguration();
0N/A if (defaultConfig != null) {
0N/A configs = new GraphicsConfiguration[1];
0N/A configs[0] = defaultConfig;
1838N/A return configs.clone();
0N/A }
0N/A }
0N/A
0N/A int max = getMaxConfigs(screen);
0N/A int defaultPixID = getDefaultPixID(screen);
0N/A Vector v = new Vector( max );
0N/A if (defaultPixID == 0) {
430N/A // Workaround for failing GDI calls
0N/A defaultConfig = Win32GraphicsConfig.getConfig(this,
0N/A defaultPixID);
0N/A v.addElement(defaultConfig);
0N/A }
0N/A else {
0N/A for (int i = 1; i <= max; i++) {
0N/A if (isPixFmtSupported(i, screen)) {
0N/A if (i == defaultPixID) {
0N/A defaultConfig = Win32GraphicsConfig.getConfig(
0N/A this, i);
0N/A v.addElement(defaultConfig);
0N/A }
0N/A else {
0N/A v.addElement(Win32GraphicsConfig.getConfig(
0N/A this, i));
0N/A }
0N/A }
0N/A }
0N/A }
0N/A configs = new GraphicsConfiguration[v.size()];
0N/A v.copyInto(configs);
0N/A }
1838N/A return configs.clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns the maximum number of graphics configurations available, or 1
0N/A * if PixelFormat calls fail or are disabled.
0N/A * This number is less than or equal to the number of graphics
0N/A * configurations supported.
0N/A */
0N/A protected int getMaxConfigs(int screen) {
0N/A if (pfDisabled) {
0N/A return 1;
0N/A } else {
0N/A return getMaxConfigsImpl(screen);
0N/A }
0N/A }
0N/A
0N/A private native int getMaxConfigsImpl(int screen);
0N/A
0N/A /**
0N/A * Returns whether or not the PixelFormat indicated by index is
0N/A * supported. Supported PixelFormats support drawing to a Window
0N/A * (PFD_DRAW_TO_WINDOW), support GDI (PFD_SUPPORT_GDI), and in the
0N/A * case of an 8-bit format (cColorBits <= 8) uses indexed colors
0N/A * (iPixelType == PFD_TYPE_COLORINDEX).
0N/A * We use the index 0 to indicate that PixelFormat calls don't work, or
0N/A * are disabled. Do not call this function with an index of 0.
0N/A * @param index a PixelFormat index
0N/A */
0N/A protected native boolean isPixFmtSupported(int index, int screen);
0N/A
0N/A /**
0N/A * Returns the PixelFormatID of the default graphics configuration
0N/A * associated with this graphics device, or 0 if PixelFormats calls fail or
0N/A * are disabled.
0N/A */
0N/A protected int getDefaultPixID(int screen) {
0N/A if (pfDisabled) {
0N/A return 0;
0N/A } else {
0N/A return getDefaultPixIDImpl(screen);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the default PixelFormat ID from GDI. Do not call if PixelFormats
0N/A * are disabled.
0N/A */
0N/A private native int getDefaultPixIDImpl(int screen);
0N/A
0N/A /**
0N/A * Returns the default graphics configuration
0N/A * associated with this graphics device.
0N/A */
0N/A public GraphicsConfiguration getDefaultConfiguration() {
0N/A if (defaultConfig == null) {
0N/A // first try to create a WGLGraphicsConfig if OGL is enabled
0N/A // REMIND: the WGL code does not yet work properly in multimon
0N/A // situations, so we will fallback on GDI if we are not on the
0N/A // default device...
0N/A if (WindowsFlags.isOGLEnabled() && isDefaultDevice()) {
0N/A int defPixID = WGLGraphicsConfig.getDefaultPixFmt(screen);
0N/A defaultConfig = WGLGraphicsConfig.getConfig(this, defPixID);
0N/A if (WindowsFlags.isOGLVerbose()) {
0N/A if (defaultConfig != null) {
0N/A System.out.print("OpenGL pipeline enabled");
0N/A } else {
0N/A System.out.print("Could not enable OpenGL pipeline");
0N/A }
0N/A System.out.println(" for default config on screen " +
0N/A screen);
0N/A }
0N/A }
0N/A
0N/A // Fix for 4669614. Most apps are not concerned with PixelFormats,
0N/A // yet we ALWAYS used them for determining ColorModels and such.
0N/A // By passing in 0 as the PixelFormatID here, we signal that
0N/A // PixelFormats should not be used, thus avoid loading the opengl
0N/A // library. Apps concerned with PixelFormats can still use
0N/A // GraphicsConfiguration.getConfigurations().
0N/A // Note that calling native pixel format functions tends to cause
0N/A // problems between those functions (which are OpenGL-related)
0N/A // and our use of DirectX. For example, some Matrox boards will
0N/A // crash or hang calling these functions when any app is running
0N/A // in DirectX fullscreen mode. So avoiding these calls unless
0N/A // absolutely necessary is preferable.
0N/A if (defaultConfig == null) {
0N/A defaultConfig = Win32GraphicsConfig.getConfig(this, 0);
0N/A }
0N/A }
0N/A return defaultConfig;
0N/A }
0N/A
0N/A public String toString() {
0N/A return valid ? descString + "]" : descString + ", removed]";
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this is the default GraphicsDevice for the
0N/A * GraphicsEnvironment.
0N/A */
0N/A private boolean isDefaultDevice() {
0N/A return (this ==
0N/A GraphicsEnvironment.
0N/A getLocalGraphicsEnvironment().getDefaultScreenDevice());
0N/A }
0N/A
0N/A private static boolean isFSExclusiveModeAllowed() {
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (security != null) {
0N/A if (fullScreenExclusivePermission == null) {
0N/A fullScreenExclusivePermission =
0N/A new AWTPermission("fullScreenExclusive");
0N/A }
0N/A try {
0N/A security.checkPermission(fullScreenExclusivePermission);
0N/A } catch (SecurityException e) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
430N/A * returns true unless we're not allowed to use fullscreen mode.
0N/A */
430N/A @Override
0N/A public boolean isFullScreenSupported() {
0N/A return isFSExclusiveModeAllowed();
0N/A }
0N/A
430N/A @Override
0N/A public synchronized void setFullScreenWindow(Window w) {
0N/A Window old = getFullScreenWindow();
0N/A if (w == old) {
0N/A return;
0N/A }
0N/A if (!isFullScreenSupported()) {
0N/A super.setFullScreenWindow(w);
0N/A return;
0N/A }
0N/A
0N/A // Enter windowed mode.
0N/A if (old != null) {
0N/A // restore the original display mode
0N/A if (defaultDisplayMode != null) {
0N/A setDisplayMode(defaultDisplayMode);
0N/A // we set the default display mode to null here
0N/A // because the default mode could change during
0N/A // the life of the application (user can change it through
0N/A // the desktop properties dialog, for example), so
0N/A // we need to record it every time prior to
0N/A // entering the fullscreen mode.
0N/A defaultDisplayMode = null;
0N/A }
0N/A WWindowPeer peer = (WWindowPeer)old.getPeer();
0N/A if (peer != null) {
1890N/A peer.setFullScreenExclusiveModeState(false);
430N/A // we used to destroy the buffers on exiting fs mode, this
430N/A // is no longer needed since fs change will cause a surface
430N/A // data replacement
0N/A synchronized(peer) {
430N/A exitFullScreenExclusive(screen, peer);
0N/A }
0N/A }
430N/A removeFSWindowListener(old);
0N/A }
0N/A super.setFullScreenWindow(w);
0N/A if (w != null) {
0N/A // always record the default display mode prior to going
0N/A // fullscreen
0N/A defaultDisplayMode = getDisplayMode();
430N/A addFSWindowListener(w);
0N/A // Enter full screen exclusive mode.
0N/A WWindowPeer peer = (WWindowPeer)w.getPeer();
1890N/A if (peer != null) {
1890N/A synchronized(peer) {
1890N/A enterFullScreenExclusive(screen, peer);
1890N/A // Note: removed replaceSurfaceData() call because
1890N/A // changing the window size or making it visible
1890N/A // will cause this anyway, and both of these events happen
1890N/A // as part of switching into fullscreen mode.
1890N/A }
1890N/A peer.setFullScreenExclusiveModeState(true);
0N/A }
0N/A
0N/A // fix for 4868278
0N/A peer.updateGC();
0N/A }
0N/A }
0N/A
0N/A // Entering and exiting full-screen mode are done within a
0N/A // tree-lock and should never lock on any resources which are
0N/A // required by other threads which may have them and may require
0N/A // the tree-lock.
430N/A // REMIND: in the future these methods may need to become protected so that
430N/A // subclasses could override them and use appropriate api other than GDI
430N/A // for implementing these functions.
430N/A protected native void enterFullScreenExclusive(int screen, WindowPeer w);
430N/A protected native void exitFullScreenExclusive(int screen, WindowPeer w);
0N/A
430N/A @Override
0N/A public boolean isDisplayChangeSupported() {
0N/A return (isFullScreenSupported() && getFullScreenWindow() != null);
0N/A }
0N/A
430N/A @Override
0N/A public synchronized void setDisplayMode(DisplayMode dm) {
0N/A if (!isDisplayChangeSupported()) {
0N/A super.setDisplayMode(dm);
0N/A return;
0N/A }
0N/A if (dm == null || (dm = getMatchingDisplayMode(dm)) == null) {
0N/A throw new IllegalArgumentException("Invalid display mode");
0N/A }
0N/A if (getDisplayMode().equals(dm)) {
0N/A return;
0N/A }
0N/A Window w = getFullScreenWindow();
0N/A if (w != null) {
0N/A WWindowPeer peer = (WWindowPeer)w.getPeer();
0N/A configDisplayMode(screen, peer, dm.getWidth(), dm.getHeight(),
0N/A dm.getBitDepth(), dm.getRefreshRate());
0N/A // resize the fullscreen window to the dimensions of the new
0N/A // display mode
0N/A Rectangle screenBounds = getDefaultConfiguration().getBounds();
0N/A w.setBounds(screenBounds.x, screenBounds.y,
0N/A dm.getWidth(), dm.getHeight());
0N/A // Note: no call to replaceSurfaceData is required here since
0N/A // replacement will be caused by an upcoming display change event
0N/A } else {
0N/A throw new IllegalStateException("Must be in fullscreen mode " +
0N/A "in order to set display mode");
0N/A }
0N/A }
0N/A
430N/A protected native DisplayMode getCurrentDisplayMode(int screen);
430N/A protected native void configDisplayMode(int screen, WindowPeer w, int width,
0N/A int height, int bitDepth,
0N/A int refreshRate);
430N/A protected native void enumDisplayModes(int screen, ArrayList modes);
0N/A
430N/A @Override
0N/A public synchronized DisplayMode getDisplayMode() {
0N/A DisplayMode res = getCurrentDisplayMode(screen);
0N/A return res;
0N/A }
0N/A
430N/A @Override
0N/A public synchronized DisplayMode[] getDisplayModes() {
0N/A ArrayList modes = new ArrayList();
0N/A enumDisplayModes(screen, modes);
0N/A int listSize = modes.size();
0N/A DisplayMode[] retArray = new DisplayMode[listSize];
0N/A for (int i = 0; i < listSize; i++) {
0N/A retArray[i] = (DisplayMode)modes.get(i);
0N/A }
0N/A return retArray;
0N/A }
0N/A
430N/A protected synchronized DisplayMode getMatchingDisplayMode(DisplayMode dm) {
0N/A if (!isDisplayChangeSupported()) {
0N/A return null;
0N/A }
430N/A DisplayMode[] modes = getDisplayModes();
430N/A for (DisplayMode mode : modes) {
430N/A if (dm.equals(mode) ||
430N/A (dm.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN &&
430N/A dm.getWidth() == mode.getWidth() &&
430N/A dm.getHeight() == mode.getHeight() &&
430N/A dm.getBitDepth() == mode.getBitDepth()))
430N/A {
430N/A return mode;
0N/A }
0N/A }
430N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * From the DisplayChangeListener interface.
0N/A * Called from Win32GraphicsEnvironment when the display settings have
0N/A * changed.
0N/A */
0N/A public void displayChanged() {
0N/A dynamicColorModel = null;
0N/A defaultConfig = null;
0N/A configs = null;
0N/A // pass on to all top-level windows on this display
0N/A topLevels.notifyListeners();
0N/A }
0N/A
0N/A /**
0N/A * Part of the DisplayChangedListener interface: devices
0N/A * do not need to react to this event
0N/A */
0N/A public void paletteChanged() {
0N/A }
0N/A
0N/A /*
0N/A * Add a DisplayChangeListener to be notified when the display settings
0N/A * are changed. Typically, only top-level containers need to be added
0N/A * to Win32GraphicsDevice.
0N/A */
0N/A public void addDisplayChangedListener(DisplayChangedListener client) {
0N/A topLevels.add(client);
0N/A }
0N/A
0N/A /*
0N/A * Remove a DisplayChangeListener from this Win32GraphicsDevice
0N/A */
0N/A public void removeDisplayChangedListener(DisplayChangedListener client) {
0N/A topLevels.remove(client);
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns the color model associated with this device
0N/A */
0N/A private native ColorModel makeColorModel (int screen,
0N/A boolean dynamic);
0N/A
0N/A /**
0N/A * Returns a dynamic ColorModel which is updated when there
0N/A * are any changes (e.g., palette changes) in the device
0N/A */
0N/A public ColorModel getDynamicColorModel() {
0N/A if (dynamicColorModel == null) {
0N/A dynamicColorModel = makeColorModel(screen, true);
0N/A }
0N/A return dynamicColorModel;
0N/A }
0N/A
0N/A /**
0N/A * Returns the non-dynamic ColorModel associated with this device
0N/A */
0N/A public ColorModel getColorModel() {
0N/A if (colorModel == null) {
0N/A colorModel = makeColorModel(screen, false);
0N/A }
0N/A return colorModel;
0N/A }
0N/A
430N/A /**
430N/A * WindowAdapter class responsible for de/iconifying full-screen window
430N/A * of this device.
430N/A *
430N/A * The listener restores the default display mode when window is iconified
430N/A * and sets it back to the one set by the user on de-iconification.
430N/A */
430N/A private static class Win32FSWindowAdapter extends WindowAdapter {
430N/A private Win32GraphicsDevice device;
430N/A private DisplayMode dm;
430N/A
430N/A Win32FSWindowAdapter(Win32GraphicsDevice device) {
430N/A this.device = device;
430N/A }
430N/A
430N/A private void setFSWindowsState(Window other, int state) {
430N/A GraphicsDevice gds[] =
430N/A GraphicsEnvironment.getLocalGraphicsEnvironment().
430N/A getScreenDevices();
430N/A // check if the de/activation was caused by other
430N/A // fs window and ignore the event if that's the case
430N/A if (other != null) {
430N/A for (GraphicsDevice gd : gds) {
430N/A if (other == gd.getFullScreenWindow()) {
430N/A return;
430N/A }
430N/A }
430N/A }
430N/A // otherwise apply state to all fullscreen windows
430N/A for (GraphicsDevice gd : gds) {
430N/A Window fsw = gd.getFullScreenWindow();
430N/A if (fsw instanceof Frame) {
430N/A ((Frame)fsw).setExtendedState(state);
430N/A }
430N/A }
430N/A }
430N/A
430N/A @Override
430N/A public void windowDeactivated(WindowEvent e) {
430N/A setFSWindowsState(e.getOppositeWindow(), Frame.ICONIFIED);
430N/A }
430N/A
430N/A @Override
430N/A public void windowActivated(WindowEvent e) {
430N/A setFSWindowsState(e.getOppositeWindow(), Frame.NORMAL);
430N/A }
430N/A
430N/A @Override
430N/A public void windowIconified(WindowEvent e) {
430N/A // restore the default display mode for this device
430N/A DisplayMode ddm = device.defaultDisplayMode;
430N/A if (ddm != null) {
430N/A dm = device.getDisplayMode();
430N/A device.setDisplayMode(ddm);
430N/A }
430N/A }
430N/A
430N/A @Override
430N/A public void windowDeiconified(WindowEvent e) {
430N/A // restore the user-set display mode for this device
430N/A if (dm != null) {
430N/A device.setDisplayMode(dm);
430N/A dm = null;
430N/A }
430N/A }
430N/A }
0N/A
0N/A /**
430N/A * Adds a WindowListener to be used as
430N/A * activation/deactivation listener for the current full-screen window.
430N/A *
430N/A * @param w full-screen window
0N/A */
3177N/A protected void addFSWindowListener(final Window w) {
430N/A // Note: even though we create a listener for Window instances of
430N/A // fs windows they will not receive window events.
430N/A fsWindowListener = new Win32FSWindowAdapter(this);
3177N/A
3177N/A // Fix for 6709453. Using invokeLater to avoid listening
3177N/A // for the events already posted to the queue.
3177N/A EventQueue.invokeLater(new Runnable() {
3177N/A public void run() {
3177N/A w.addWindowListener(fsWindowListener);
3177N/A }
3177N/A });
430N/A }
430N/A
430N/A /**
430N/A * Removes the fs window listener.
430N/A *
430N/A * @param w full-screen window
430N/A */
430N/A protected void removeFSWindowListener(Window w) {
430N/A w.removeWindowListener(fsWindowListener);
430N/A fsWindowListener = null;
0N/A }
0N/A}