Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/sun/awt/CGraphicsDevice.java
38827 views
/*1* Copyright (c) 2012, 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.AWTPermission;28import java.awt.DisplayMode;29import java.awt.GraphicsConfiguration;30import java.awt.GraphicsDevice;31import java.awt.Insets;32import java.awt.Window;33import java.util.Objects;3435import sun.java2d.opengl.CGLGraphicsConfig;3637public final class CGraphicsDevice extends GraphicsDevice38implements DisplayChangedListener {3940/**41* CoreGraphics display ID. This identifier can become non-valid at any time42* therefore methods, which is using this id should be ready to it.43*/44private volatile int displayID;45private volatile double xResolution;46private volatile double yResolution;47private volatile int scale;4849// Array of all GraphicsConfig instances for this device50private final GraphicsConfiguration[] configs;5152// Default config (temporarily hard coded)53private final int DEFAULT_CONFIG = 0;5455private static AWTPermission fullScreenExclusivePermission;5657// Save/restore DisplayMode for the Full Screen mode58private DisplayMode originalMode;5960public CGraphicsDevice(final int displayID) {61this.displayID = displayID;62configs = new GraphicsConfiguration[] {63CGLGraphicsConfig.getConfig(this, 0)64};65}6667/**68* Returns CGDirectDisplayID, which is the same id as @"NSScreenNumber" in69* NSScreen.70*71* @return CoreGraphics display id.72*/73public int getCGDisplayID() {74return displayID;75}7677/**78* Return a list of all configurations.79*/80@Override81public GraphicsConfiguration[] getConfigurations() {82return configs.clone();83}8485/**86* Return the default configuration.87*/88@Override89public GraphicsConfiguration getDefaultConfiguration() {90return configs[DEFAULT_CONFIG];91}9293/**94* Return a human-readable screen description.95*/96@Override97public String getIDstring() {98return "Display " + displayID;99}100101/**102* Returns the type of the graphics device.103* @see #TYPE_RASTER_SCREEN104* @see #TYPE_PRINTER105* @see #TYPE_IMAGE_BUFFER106*/107@Override108public int getType() {109return TYPE_RASTER_SCREEN;110}111112public double getXResolution() {113return xResolution;114}115116public double getYResolution() {117return yResolution;118}119120public Insets getScreenInsets() {121// the insets are queried synchronously and are not cached122// since there are no Quartz or Cocoa means to receive notifications123// on insets changes (e.g. when the Dock is resized):124// the existing CGDisplayReconfigurationCallBack is not notified125// as well as the NSApplicationDidChangeScreenParametersNotification126// is fired on the Dock location changes only127return nativeGetScreenInsets(displayID);128}129130public int getScaleFactor() {131return scale;132}133134public void invalidate(final int defaultDisplayID) {135displayID = defaultDisplayID;136}137138@Override139public void displayChanged() {140xResolution = nativeGetXResolution(displayID);141yResolution = nativeGetYResolution(displayID);142scale = (int) nativeGetScaleFactor(displayID);143//TODO configs/fullscreenWindow/modes?144}145146@Override147public void paletteChanged() {148// devices do not need to react to this event.149}150151/**152* Enters full-screen mode, or returns to windowed mode.153*/154@Override155public synchronized void setFullScreenWindow(Window w) {156Window old = getFullScreenWindow();157if (w == old) {158return;159}160161boolean fsSupported = isFullScreenSupported();162163if (fsSupported && old != null) {164// enter windowed mode and restore original display mode165exitFullScreenExclusive(old);166if (originalMode != null) {167setDisplayMode(originalMode);168originalMode = null;169}170}171172super.setFullScreenWindow(w);173174if (fsSupported && w != null) {175if (isDisplayChangeSupported()) {176originalMode = getDisplayMode();177}178// enter fullscreen mode179enterFullScreenExclusive(w);180}181}182183/**184* Returns true if this GraphicsDevice supports185* full-screen exclusive mode and false otherwise.186*/187@Override188public boolean isFullScreenSupported() {189return isFSExclusiveModeAllowed();190}191192private static boolean isFSExclusiveModeAllowed() {193SecurityManager security = System.getSecurityManager();194if (security != null) {195if (fullScreenExclusivePermission == null) {196fullScreenExclusivePermission =197new AWTPermission("fullScreenExclusive");198}199try {200security.checkPermission(fullScreenExclusivePermission);201} catch (SecurityException e) {202return false;203}204}205return true;206}207208private static void enterFullScreenExclusive(Window w) {209FullScreenCapable peer = (FullScreenCapable)w.getPeer();210if (peer != null) {211peer.enterFullScreenMode();212}213}214215private static void exitFullScreenExclusive(Window w) {216FullScreenCapable peer = (FullScreenCapable)w.getPeer();217if (peer != null) {218peer.exitFullScreenMode();219}220}221222@Override223public boolean isDisplayChangeSupported() {224return true;225}226227@Override228public void setDisplayMode(final DisplayMode dm) {229if (dm == null) {230throw new IllegalArgumentException("Invalid display mode");231}232if (!Objects.equals(dm, getDisplayMode())) {233nativeSetDisplayMode(displayID, dm.getWidth(), dm.getHeight(),234dm.getBitDepth(), dm.getRefreshRate());235if (isFullScreenSupported() && getFullScreenWindow() != null) {236getFullScreenWindow().setSize(dm.getWidth(), dm.getHeight());237}238}239}240241@Override242public DisplayMode getDisplayMode() {243return nativeGetDisplayMode(displayID);244}245246@Override247public DisplayMode[] getDisplayModes() {248return nativeGetDisplayModes(displayID);249}250251private static native double nativeGetScaleFactor(int displayID);252253private static native void nativeSetDisplayMode(int displayID, int w, int h, int bpp, int refrate);254255private static native DisplayMode nativeGetDisplayMode(int displayID);256257private static native DisplayMode[] nativeGetDisplayModes(int displayID);258259private static native double nativeGetXResolution(int displayID);260261private static native double nativeGetYResolution(int displayID);262263private static native Insets nativeGetScreenInsets(int displayID);264}265266267