Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/sun/awt/CGraphicsEnvironment.java
38827 views
/*1* Copyright (c) 2011, 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.*;28import java.util.*;2930import sun.java2d.*;3132/**33* This is an implementation of a GraphicsEnvironment object for the default34* local GraphicsEnvironment used by the Java Runtime Environment for Mac OS X35* GUI environments.36*37* @see GraphicsDevice38* @see GraphicsConfiguration39*/40public final class CGraphicsEnvironment extends SunGraphicsEnvironment {4142// Global initialization of the Cocoa runtime.43private static native void initCocoa();4445/**46* Fetch an array of all valid CoreGraphics display identifiers.47*/48private static native int[] getDisplayIDs();4950/**51* Fetch the CoreGraphics display ID for the 'main' display.52*/53private static native int getMainDisplayID();5455/**56* Noop function that just acts as an entry point for someone to force a57* static initialization of this class.58*/59public static void init() { }6061static {62java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {63public Void run() {64System.loadLibrary("awt");65return null;66}67});6869java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {70public Void run() {71if (isHeadless()) return null;72initCocoa();73return null;74}75});7677// Install the correct surface manager factory.78SurfaceManagerFactory.setInstance(new MacosxSurfaceManagerFactory());79}8081/**82* Register the instance with CGDisplayRegisterReconfigurationCallback().83* The registration uses a weak global reference -- if our instance is84* garbage collected, the reference will be dropped.85*86* @return Return the registration context (a pointer).87*/88private native long registerDisplayReconfiguration();8990/**91* Remove the instance's registration with CGDisplayRemoveReconfigurationCallback()92*/93private native void deregisterDisplayReconfiguration(long context);9495/** Available CoreGraphics displays. */96private final Map<Integer, CGraphicsDevice> devices = new HashMap<>(5);9798/** Reference to the display reconfiguration callback context. */99private final long displayReconfigContext;100101/**102* Construct a new instance.103*/104public CGraphicsEnvironment() {105if (isHeadless()) {106displayReconfigContext = 0L;107return;108}109110/* Populate the device table */111initDevices();112113/* Register our display reconfiguration listener */114displayReconfigContext = registerDisplayReconfiguration();115if (displayReconfigContext == 0L) {116throw new RuntimeException("Could not register CoreGraphics display reconfiguration callback");117}118}119120/**121* Called by the CoreGraphics Display Reconfiguration Callback.122*123* @param displayId CoreGraphics displayId124* @param removed true if displayId was removed, false otherwise.125*/126void _displayReconfiguration(final int displayId, final boolean removed) {127synchronized (this) {128if (removed && devices.containsKey(displayId)) {129final CGraphicsDevice gd = devices.remove(displayId);130gd.invalidate(getMainDisplayID());131gd.displayChanged();132}133}134initDevices();135}136137@Override138protected void finalize() throws Throwable {139try {140super.finalize();141} finally {142deregisterDisplayReconfiguration(displayReconfigContext);143}144}145146/**147* (Re)create all CGraphicsDevices, reuses a devices if it is possible.148*/149private void initDevices() {150synchronized (this) {151final Map<Integer, CGraphicsDevice> old = new HashMap<>(devices);152devices.clear();153154int mainID = getMainDisplayID();155156// initialization of the graphics device may change157// list of displays on hybrid systems via an activation158// of discrete video.159// So, we initialize the main display first, and then160// retrieve actual list of displays.161if (!old.containsKey(mainID)) {162old.put(mainID, new CGraphicsDevice(mainID));163}164165for (final int id : getDisplayIDs()) {166devices.put(id, old.containsKey(id) ? old.get(id)167: new CGraphicsDevice(id));168}169}170displayChanged();171}172173@Override174public synchronized GraphicsDevice getDefaultScreenDevice() throws HeadlessException {175final int mainDisplayID = getMainDisplayID();176CGraphicsDevice d = devices.get(mainDisplayID);177if (d == null) {178// we do not expect that this may happen, the only response179// is to re-initialize the list of devices180initDevices();181182d = devices.get(mainDisplayID);183if (d == null) {184throw new AWTError("no screen devices");185}186}187return d;188}189190@Override191public synchronized GraphicsDevice[] getScreenDevices() throws HeadlessException {192return devices.values().toArray(new CGraphicsDevice[devices.values().size()]);193}194195public synchronized GraphicsDevice getScreenDevice(int displayID) {196return devices.get(displayID);197}198199@Override200protected synchronized int getNumScreens() {201return devices.size();202}203204@Override205protected GraphicsDevice makeScreenDevice(int screennum) {206throw new UnsupportedOperationException("This method is unused and should not be called in this implementation");207}208209@Override210public boolean isDisplayLocal() {211return true;212}213214static String[] sLogicalFonts = { "Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput" };215216@Override217public Font[] getAllFonts() {218219Font[] newFonts;220Font[] superFonts = super.getAllFonts();221222int numLogical = sLogicalFonts.length;223int numOtherFonts = superFonts.length;224225newFonts = new Font[numOtherFonts + numLogical];226System.arraycopy(superFonts,0,newFonts,numLogical,numOtherFonts);227228for (int i = 0; i < numLogical; i++)229{230newFonts[i] = new Font(sLogicalFonts[i], Font.PLAIN, 1);231}232return newFonts;233}234235}236237238