1 /* 2 * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.awt; 27 28 import java.awt.GraphicsConfiguration; 29 import java.awt.GraphicsDevice; 30 import java.awt.GraphicsEnvironment; 31 import java.awt.Toolkit; 32 import java.awt.peer.ComponentPeer; 33 import java.io.File; 34 import java.io.IOException; 35 import java.lang.ref.WeakReference; 36 import java.util.ArrayList; 37 import java.util.ListIterator; 38 import java.util.NoSuchElementException; 39 import java.util.StringTokenizer; 40 import sun.awt.DisplayChangedListener; 41 import sun.awt.SunDisplayChanger; 42 import sun.awt.windows.WPrinterJob; 43 import sun.awt.windows.WToolkit; 44 import sun.java2d.SunGraphicsEnvironment; 45 import sun.java2d.SurfaceManagerFactory; 46 import sun.java2d.WindowsSurfaceManagerFactory; 47 import sun.java2d.d3d.D3DGraphicsDevice; 48 import sun.java2d.windows.WindowsFlags; 49 50 /** 51 * This is an implementation of a GraphicsEnvironment object for the 52 * default local GraphicsEnvironment used by the Java Runtime Environment 53 * for Windows. 54 * 55 * @see GraphicsDevice 56 * @see GraphicsConfiguration 57 */ 58 59 public class Win32GraphicsEnvironment 60 extends SunGraphicsEnvironment 61 { 62 static { 63 // Ensure awt is loaded already. Also, this forces static init 64 // of WToolkit and Toolkit, which we depend upon 65 WToolkit.loadLibraries(); 66 // setup flags before initializing native layer 67 WindowsFlags.initFlags(); 68 initDisplayWrapper(); 69 70 // Install correct surface manager factory. 71 SurfaceManagerFactory.setInstance(new WindowsSurfaceManagerFactory()); 72 } 73 74 /** 75 * Initializes native components of the graphics environment. This 76 * includes everything from the native GraphicsDevice elements to 77 * the DirectX rendering layer. 78 */ 79 private static native void initDisplay(); 80 81 private static boolean displayInitialized; // = false; 82 public static void initDisplayWrapper() { 83 if (!displayInitialized) { 84 displayInitialized = true; 85 initDisplay(); 86 } 87 } 88 89 public Win32GraphicsEnvironment() { 90 } 91 92 protected native int getNumScreens(); 93 protected native int getDefaultScreen(); 94 95 public GraphicsDevice getDefaultScreenDevice() { 96 return getScreenDevices()[getDefaultScreen()]; 97 } 98 99 /** 100 * Returns the number of pixels per logical inch along the screen width. 101 * In a system with multiple display monitors, this value is the same for 102 * all monitors. 103 * @returns number of pixels per logical inch in X direction 104 */ 105 public native int getXResolution(); 106 /** 107 * Returns the number of pixels per logical inch along the screen height. 108 * In a system with multiple display monitors, this value is the same for 109 * all monitors. 110 * @returns number of pixels per logical inch in Y direction 111 */ 112 public native int getYResolution(); 113 114 115 /* 116 * ----DISPLAY CHANGE SUPPORT---- 117 */ 118 119 // list of invalidated graphics devices (those which were removed) 120 private ArrayList<WeakReference<Win32GraphicsDevice>> oldDevices; 121 /* 122 * From DisplayChangeListener interface. 123 * Called from WToolkit and executed on the event thread when the 124 * display settings are changed. 125 */ 126 @Override 127 public void displayChanged() { 128 // getNumScreens() will return the correct current number of screens 129 GraphicsDevice newDevices[] = new GraphicsDevice[getNumScreens()]; 130 GraphicsDevice oldScreens[] = screens; 131 // go through the list of current devices and determine if they 132 // could be reused, or will have to be replaced 133 if (oldScreens != null) { 134 for (int i = 0; i < oldScreens.length; i++) { 135 if (!(screens[i] instanceof Win32GraphicsDevice)) { 136 // REMIND: can we ever have anything other than Win32GD? 137 assert (false) : oldScreens[i]; 138 continue; 139 } 140 Win32GraphicsDevice gd = (Win32GraphicsDevice)oldScreens[i]; 141 // devices may be invalidated from the native code when the 142 // display change happens (device add/removal also causes a 143 // display change) 144 if (!gd.isValid()) { 145 if (oldDevices == null) { 146 oldDevices = 147 new ArrayList<WeakReference<Win32GraphicsDevice>>(); 148 } 149 oldDevices.add(new WeakReference<Win32GraphicsDevice>(gd)); 150 } else if (i < newDevices.length) { 151 // reuse the device 152 newDevices[i] = gd; 153 } 154 } 155 oldScreens = null; 156 } 157 // create the new devices (those that weren't reused) 158 for (int i = 0; i < newDevices.length; i++) { 159 if (newDevices[i] == null) { 160 newDevices[i] = makeScreenDevice(i); 161 } 162 } 163 // install the new array of devices 164 // Note: no synchronization here, it doesn't matter if a thread gets 165 // a new or an old array this time around 166 screens = newDevices; 167 for (GraphicsDevice gd : screens) { 168 if (gd instanceof DisplayChangedListener) { 169 ((DisplayChangedListener)gd).displayChanged(); 170 } 171 } 172 // re-invalidate all old devices. It's needed because those in the list 173 // may become "invalid" again - if the current default device is removed, 174 // for example. Also, they need to be notified about display 175 // changes as well. 176 if (oldDevices != null) { 177 int defScreen = getDefaultScreen(); 178 for (ListIterator<WeakReference<Win32GraphicsDevice>> it = 179 oldDevices.listIterator(); it.hasNext();) 180 { 181 Win32GraphicsDevice gd = it.next().get(); 182 if (gd != null) { 183 gd.invalidate(defScreen); 184 gd.displayChanged(); 185 } else { 186 // no more references to this device, remove it 187 it.remove(); 188 } 189 } 190 } 191 // Reset the static GC for the (possibly new) default screen 192 WToolkit.resetGC(); 193 194 // notify SunDisplayChanger list (e.g. VolatileSurfaceManagers and 195 // CachingSurfaceManagers) about the display change event 196 displayChanger.notifyListeners(); 197 // note: do not call super.displayChanged, we've already done everything 198 } 199 200 201 /* 202 * ----END DISPLAY CHANGE SUPPORT---- 203 */ 204 205 protected GraphicsDevice makeScreenDevice(int screennum) { 206 GraphicsDevice device = null; 207 if (WindowsFlags.isD3DEnabled()) { 208 device = D3DGraphicsDevice.createDevice(screennum); 209 } 210 if (device == null) { 211 device = new Win32GraphicsDevice(screennum); 212 } 213 return device; 214 } 215 216 public boolean isDisplayLocal() { 217 return true; 218 } 219 220 @Override 221 public boolean isFlipStrategyPreferred(ComponentPeer peer) { 222 GraphicsConfiguration gc; 223 if (peer != null && (gc = peer.getGraphicsConfiguration()) != null) { 224 GraphicsDevice gd = gc.getDevice(); 225 if (gd instanceof D3DGraphicsDevice) { 226 return ((D3DGraphicsDevice)gd).isD3DEnabledOnDevice(); 227 } 228 } 229 return false; 230 } 231 232 private static volatile boolean isDWMCompositionEnabled; 233 /** 234 * Returns true if dwm composition is currently enabled, false otherwise. 235 * 236 * @return true if dwm composition is enabled, false otherwise 237 */ 238 public static boolean isDWMCompositionEnabled() { 239 return isDWMCompositionEnabled; 240 } 241 242 /** 243 * Called from the native code when DWM composition state changed. 244 * May be called multiple times during the lifetime of the application. 245 * REMIND: we may want to create a listener mechanism for this. 246 * 247 * Note: called on the Toolkit thread, no user code or locks are allowed. 248 * 249 * @param enabled indicates the state of dwm composition 250 */ 251 private static void dwmCompositionChanged(boolean enabled) { 252 isDWMCompositionEnabled = enabled; 253 } 254 255 /** 256 * Used to find out if the OS is Windows Vista or later. 257 * 258 * @return {@code true} if the OS is Vista or later, {@code false} otherwise 259 */ 260 public static native boolean isVistaOS(); 261 }