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 GraphicsDevice[] screens = getScreenDevices(); 97 if (screens.length == 0) { 98 return null; 99 } 100 int index = getDefaultScreen(); 101 return screens[0 < index && index < screens.length ? index : 0]; 102 } 103 104 /** 105 * Returns the number of pixels per logical inch along the screen width. 106 * In a system with multiple display monitors, this value is the same for 107 * all monitors. 108 * @returns number of pixels per logical inch in X direction 109 */ 110 public native int getXResolution(); 111 /** 112 * Returns the number of pixels per logical inch along the screen height. 113 * In a system with multiple display monitors, this value is the same for 114 * all monitors. 115 * @returns number of pixels per logical inch in Y direction 116 */ 117 public native int getYResolution(); 118 119 120 /* 121 * ----DISPLAY CHANGE SUPPORT---- 122 */ 123 124 // list of invalidated graphics devices (those which were removed) 125 private ArrayList<WeakReference<Win32GraphicsDevice>> oldDevices; 126 /* 127 * From DisplayChangeListener interface. 128 * Called from WToolkit and executed on the event thread when the 129 * display settings are changed. 130 */ 131 @Override 132 public void displayChanged() { 133 // getNumScreens() will return the correct current number of screens 134 GraphicsDevice newDevices[] = new GraphicsDevice[getNumScreens()]; 135 GraphicsDevice oldScreens[] = screens; 136 // go through the list of current devices and determine if they 137 // could be reused, or will have to be replaced 138 if (oldScreens != null) { 139 for (int i = 0; i < oldScreens.length; i++) { 140 if (!(screens[i] instanceof Win32GraphicsDevice)) { 141 // REMIND: can we ever have anything other than Win32GD? 142 assert (false) : oldScreens[i]; 143 continue; 144 } 145 Win32GraphicsDevice gd = (Win32GraphicsDevice)oldScreens[i]; 146 // devices may be invalidated from the native code when the 147 // display change happens (device add/removal also causes a 148 // display change) 149 if (!gd.isValid()) { 150 if (oldDevices == null) { 151 oldDevices = 152 new ArrayList<WeakReference<Win32GraphicsDevice>>(); 153 } 154 oldDevices.add(new WeakReference<Win32GraphicsDevice>(gd)); 155 } else if (i < newDevices.length) { 156 // reuse the device 157 newDevices[i] = gd; 158 } 159 } 160 oldScreens = null; 161 } 162 // create the new devices (those that weren't reused) 163 for (int i = 0; i < newDevices.length; i++) { 164 if (newDevices[i] == null) { 165 newDevices[i] = makeScreenDevice(i); 166 } 167 } 168 // install the new array of devices 169 // Note: no synchronization here, it doesn't matter if a thread gets 170 // a new or an old array this time around 171 screens = newDevices; 172 for (GraphicsDevice gd : screens) { 173 if (gd instanceof DisplayChangedListener) { 174 ((DisplayChangedListener)gd).displayChanged(); 175 } 176 } 177 // re-invalidate all old devices. It's needed because those in the list 178 // may become "invalid" again - if the current default device is removed, 179 // for example. Also, they need to be notified about display 180 // changes as well. 181 if (oldDevices != null) { 182 int defScreen = getDefaultScreen(); 183 for (ListIterator<WeakReference<Win32GraphicsDevice>> it = 184 oldDevices.listIterator(); it.hasNext();) 185 { 186 Win32GraphicsDevice gd = it.next().get(); 187 if (gd != null) { 188 gd.invalidate(defScreen); 189 gd.displayChanged(); 190 } else { 191 // no more references to this device, remove it 192 it.remove(); 193 } 194 } 195 } 196 // Reset the static GC for the (possibly new) default screen 197 WToolkit.resetGC(); 198 199 // notify SunDisplayChanger list (e.g. VolatileSurfaceManagers and 200 // CachingSurfaceManagers) about the display change event 201 displayChanger.notifyListeners(); 202 // note: do not call super.displayChanged, we've already done everything 203 } 204 205 206 /* 207 * ----END DISPLAY CHANGE SUPPORT---- 208 */ 209 210 protected GraphicsDevice makeScreenDevice(int screennum) { 211 GraphicsDevice device = null; 212 if (WindowsFlags.isD3DEnabled()) { 213 device = D3DGraphicsDevice.createDevice(screennum); 214 } 215 if (device == null) { 216 device = new Win32GraphicsDevice(screennum); 217 } 218 return device; 219 } 220 221 public boolean isDisplayLocal() { 222 return true; 223 } 224 225 @Override 226 public boolean isFlipStrategyPreferred(ComponentPeer peer) { 227 GraphicsConfiguration gc; 228 if (peer != null && (gc = peer.getGraphicsConfiguration()) != null) { 229 GraphicsDevice gd = gc.getDevice(); 230 if (gd instanceof D3DGraphicsDevice) { 231 return ((D3DGraphicsDevice)gd).isD3DEnabledOnDevice(); 232 } 233 } 234 return false; 235 } 236 237 private static volatile boolean isDWMCompositionEnabled; 238 /** 239 * Returns true if dwm composition is currently enabled, false otherwise. 240 * 241 * @return true if dwm composition is enabled, false otherwise 242 */ 243 public static boolean isDWMCompositionEnabled() { 244 return isDWMCompositionEnabled; 245 } 246 247 /** 248 * Called from the native code when DWM composition state changed. 249 * May be called multiple times during the lifetime of the application. 250 * REMIND: we may want to create a listener mechanism for this. 251 * 252 * Note: called on the Toolkit thread, no user code or locks are allowed. 253 * 254 * @param enabled indicates the state of dwm composition 255 */ 256 private static void dwmCompositionChanged(boolean enabled) { 257 isDWMCompositionEnabled = enabled; 258 } 259 260 /** 261 * Used to find out if the OS is Windows Vista or later. 262 * 263 * @return {@code true} if the OS is Vista or later, {@code false} otherwise 264 */ 265 public static native boolean isVistaOS(); 266 }