Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/awt/Win32GraphicsEnvironment.java
32287 views
/*1* Copyright (c) 1997, 2013, 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.awt;2627import java.awt.AWTError;28import java.awt.GraphicsConfiguration;29import java.awt.GraphicsDevice;30import java.awt.GraphicsEnvironment;31import java.awt.Toolkit;32import java.awt.peer.ComponentPeer;33import java.io.File;34import java.io.IOException;35import java.lang.ref.WeakReference;36import java.util.ArrayList;37import java.util.ListIterator;38import java.util.NoSuchElementException;39import java.util.StringTokenizer;40import sun.awt.DisplayChangedListener;41import sun.awt.SunDisplayChanger;42import sun.awt.windows.WPrinterJob;43import sun.awt.windows.WToolkit;44import sun.java2d.SunGraphicsEnvironment;45import sun.java2d.SurfaceManagerFactory;46import sun.java2d.WindowsSurfaceManagerFactory;47import sun.java2d.d3d.D3DGraphicsDevice;48import sun.java2d.windows.WindowsFlags;4950/**51* This is an implementation of a GraphicsEnvironment object for the52* default local GraphicsEnvironment used by the Java Runtime Environment53* for Windows.54*55* @see GraphicsDevice56* @see GraphicsConfiguration57*/5859public class Win32GraphicsEnvironment60extends SunGraphicsEnvironment61{62static {63// Ensure awt is loaded already. Also, this forces static init64// of WToolkit and Toolkit, which we depend upon65WToolkit.loadLibraries();66// setup flags before initializing native layer67WindowsFlags.initFlags();68initDisplayWrapper();6970// Install correct surface manager factory.71SurfaceManagerFactory.setInstance(new WindowsSurfaceManagerFactory());72}7374/**75* Initializes native components of the graphics environment. This76* includes everything from the native GraphicsDevice elements to77* the DirectX rendering layer.78*/79private static native void initDisplay();8081private static boolean displayInitialized; // = false;82public static void initDisplayWrapper() {83if (!displayInitialized) {84displayInitialized = true;85initDisplay();86}87}8889public Win32GraphicsEnvironment() {90}9192protected native int getNumScreens();93protected native int getDefaultScreen();9495public GraphicsDevice getDefaultScreenDevice() {96GraphicsDevice[] screens = getScreenDevices();97if (screens.length == 0) {98throw new AWTError("no screen devices");99}100int index = getDefaultScreen();101return screens[0 < index && index < screens.length ? index : 0];102}103104/**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 for107* all monitors.108* @returns number of pixels per logical inch in X direction109*/110public 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 for114* all monitors.115* @returns number of pixels per logical inch in Y direction116*/117public native int getYResolution();118119120/*121* ----DISPLAY CHANGE SUPPORT----122*/123124// list of invalidated graphics devices (those which were removed)125private ArrayList<WeakReference<Win32GraphicsDevice>> oldDevices;126/*127* From DisplayChangeListener interface.128* Called from WToolkit and executed on the event thread when the129* display settings are changed.130*/131@Override132public void displayChanged() {133// getNumScreens() will return the correct current number of screens134GraphicsDevice newDevices[] = new GraphicsDevice[getNumScreens()];135GraphicsDevice oldScreens[] = screens;136// go through the list of current devices and determine if they137// could be reused, or will have to be replaced138if (oldScreens != null) {139for (int i = 0; i < oldScreens.length; i++) {140if (!(screens[i] instanceof Win32GraphicsDevice)) {141// REMIND: can we ever have anything other than Win32GD?142assert (false) : oldScreens[i];143continue;144}145Win32GraphicsDevice gd = (Win32GraphicsDevice)oldScreens[i];146// devices may be invalidated from the native code when the147// display change happens (device add/removal also causes a148// display change)149if (!gd.isValid()) {150if (oldDevices == null) {151oldDevices =152new ArrayList<WeakReference<Win32GraphicsDevice>>();153}154oldDevices.add(new WeakReference<Win32GraphicsDevice>(gd));155} else if (i < newDevices.length) {156// reuse the device157newDevices[i] = gd;158}159}160oldScreens = null;161}162// create the new devices (those that weren't reused)163for (int i = 0; i < newDevices.length; i++) {164if (newDevices[i] == null) {165newDevices[i] = makeScreenDevice(i);166}167}168// install the new array of devices169// Note: no synchronization here, it doesn't matter if a thread gets170// a new or an old array this time around171screens = newDevices;172for (GraphicsDevice gd : screens) {173if (gd instanceof DisplayChangedListener) {174((DisplayChangedListener)gd).displayChanged();175}176}177// re-invalidate all old devices. It's needed because those in the list178// may become "invalid" again - if the current default device is removed,179// for example. Also, they need to be notified about display180// changes as well.181if (oldDevices != null) {182int defScreen = getDefaultScreen();183for (ListIterator<WeakReference<Win32GraphicsDevice>> it =184oldDevices.listIterator(); it.hasNext();)185{186Win32GraphicsDevice gd = it.next().get();187if (gd != null) {188gd.invalidate(defScreen);189gd.displayChanged();190} else {191// no more references to this device, remove it192it.remove();193}194}195}196// Reset the static GC for the (possibly new) default screen197WToolkit.resetGC();198199// notify SunDisplayChanger list (e.g. VolatileSurfaceManagers and200// CachingSurfaceManagers) about the display change event201displayChanger.notifyListeners();202// note: do not call super.displayChanged, we've already done everything203}204205206/*207* ----END DISPLAY CHANGE SUPPORT----208*/209210protected GraphicsDevice makeScreenDevice(int screennum) {211GraphicsDevice device = null;212if (WindowsFlags.isD3DEnabled()) {213device = D3DGraphicsDevice.createDevice(screennum);214}215if (device == null) {216device = new Win32GraphicsDevice(screennum);217}218return device;219}220221public boolean isDisplayLocal() {222return true;223}224225@Override226public boolean isFlipStrategyPreferred(ComponentPeer peer) {227GraphicsConfiguration gc;228if (peer != null && (gc = peer.getGraphicsConfiguration()) != null) {229GraphicsDevice gd = gc.getDevice();230if (gd instanceof D3DGraphicsDevice) {231return ((D3DGraphicsDevice)gd).isD3DEnabledOnDevice();232}233}234return false;235}236237private 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 otherwise242*/243public static boolean isDWMCompositionEnabled() {244return isDWMCompositionEnabled;245}246247/**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 composition255*/256private static void dwmCompositionChanged(boolean enabled) {257isDWMCompositionEnabled = enabled;258}259260/**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} otherwise264*/265public static native boolean isVistaOS();266}267268269