src/macosx/classes/sun/awt/CGraphicsEnvironment.java

Print this page


   1 /*
   2  * Copyright (c) 2011, 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.*;
  29 import java.awt.print.*;
  30 import java.util.*;
  31 
  32 import sun.java2d.*;
  33 
  34 /**
  35  * This is an implementation of a GraphicsEnvironment object for the default local GraphicsEnvironment used by the Java
  36  * Runtime Environment for Mac OS X GUI environments.

  37  *
  38  * @see GraphicsDevice
  39  * @see GraphicsConfiguration
  40  */
  41 public class CGraphicsEnvironment extends SunGraphicsEnvironment {

  42     // Global initialization of the Cocoa runtime.
  43     private static native void initCocoa();
  44 
  45     /**
  46      * Fetch an array of all valid CoreGraphics display identifiers.
  47      */
  48     private static native int[] getDisplayIDs();
  49 
  50     /**
  51      * Fetch the CoreGraphics display ID for the 'main' display.
  52      */
  53     private static native int getMainDisplayID();
  54 
  55     /**
  56      * Noop function that just acts as an entry point for someone to force a static initialization of this class.

  57      */
  58     public static void init() { }
  59 
  60     static {
  61         java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
  62             public Void run() {
  63                 System.loadLibrary("awt");
  64                 return null;
  65             }
  66         });
  67 
  68         java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
  69             public Void run() {
  70                 if (isHeadless()) return null;
  71                 initCocoa();
  72                 return null;
  73             }
  74         });
  75 
  76         // Install the correct surface manager factory.
  77         SurfaceManagerFactory.setInstance(new MacosxSurfaceManagerFactory());
  78     }
  79 
  80     /**
  81      * Register the instance with CGDisplayRegisterReconfigurationCallback()
  82      * The registration uses a weak global reference -- if our instance is garbage collected, the reference will be dropped.

  83      *
  84      * @return Return the registration context (a pointer).
  85      */
  86     private native long registerDisplayReconfiguration();
  87 
  88     /**
  89      * Remove the instance's registration with CGDisplayRemoveReconfigurationCallback()
  90      */
  91     private native void deregisterDisplayReconfiguration(long context);
  92 
  93     /** Available CoreGraphics displays. */
  94     private final Map<Integer, CGraphicsDevice> devices = new HashMap<Integer, CGraphicsDevice>();
  95 
  96     /** Reference to the display reconfiguration callback context. */
  97     private final long displayReconfigContext;
  98 
  99     /**
 100      * Construct a new instance.
 101      */
 102     public CGraphicsEnvironment() {
 103         if (isHeadless()) {
 104             displayReconfigContext = 0L;
 105             return;
 106         }
 107 
 108         /* Populate the device table */
 109         initDevices();
 110 
 111         /* Register our display reconfiguration listener */
 112         displayReconfigContext = registerDisplayReconfiguration();
 113         if (displayReconfigContext == 0L) {
 114             throw new RuntimeException("Could not register CoreGraphics display reconfiguration callback");
 115         }
 116     }
 117 
 118     /**
 119      * Called by the CoreGraphics Display Reconfiguration Callback.
 120      *
 121      * @param displayId
 122      *            CoreGraphics displayId
 123      */
 124     void _displayReconfiguration(long displayId) {
 125         displayChanged();







 126     }
 127 
 128     @Override
 129     protected void finalize() throws Throwable {
 130         try {
 131             super.finalize();
 132         } finally {
 133             deregisterDisplayReconfiguration(displayReconfigContext);
 134         }
 135     }
 136 
 137     /**
 138      * (Re)create all CGraphicsDevices
 139      *
 140      * @return
 141      */
 142     private synchronized void initDevices() {


 143         devices.clear();
 144 
 145         int mainID = getMainDisplayID();
 146 
 147         // initialization of the graphics device may change
 148         // list of displays on hybrid systems via an activation
 149         // of discrete video.
 150         // So, we initialize the main display first, and then
 151         // retrieve actual list of displays.
 152         CGraphicsDevice mainDevice = new CGraphicsDevice(mainID);
 153 
 154         final int[] displayIDs = getDisplayIDs();
 155 
 156         for (int displayID : displayIDs) {
 157             if (displayID != mainID) {
 158                 devices.put(displayID, new CGraphicsDevice(displayID));
 159             } else {
 160                 devices.put(mainID, mainDevice);
 161             }
 162         }

 163     }
 164 
 165     @Override
 166     public synchronized GraphicsDevice getDefaultScreenDevice() throws HeadlessException {
 167         final int mainDisplayID = getMainDisplayID();
 168         CGraphicsDevice d = devices.get(mainDisplayID);
 169         if (d == null) {
 170             // we do not exepct that this may happen, the only responce
 171             // is to re-initialize the list of devices
 172             initDevices();
 173 
 174             d = devices.get(mainDisplayID);
 175         }
 176         return d;
 177     }
 178 
 179     @Override
 180     public synchronized GraphicsDevice[] getScreenDevices() throws HeadlessException {
 181         return devices.values().toArray(new CGraphicsDevice[devices.values().size()]);
 182     }
 183 
 184     public synchronized GraphicsDevice getScreenDevice(int displayID) {
 185         return devices.get(displayID);
 186     }
 187 
 188     @Override
 189     protected synchronized int getNumScreens() {
 190         return devices.size();


   1 /*
   2  * Copyright (c) 2011, 2013, 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.*;

  29 import java.util.*;
  30 
  31 import sun.java2d.*;
  32 
  33 /**
  34  * This is an implementation of a GraphicsEnvironment object for the default
  35  * local GraphicsEnvironment used by the Java Runtime Environment for Mac OS X
  36  * GUI environments.
  37  *
  38  * @see GraphicsDevice
  39  * @see GraphicsConfiguration
  40  */
  41 public final class CGraphicsEnvironment extends SunGraphicsEnvironment {
  42 
  43     // Global initialization of the Cocoa runtime.
  44     private static native void initCocoa();
  45 
  46     /**
  47      * Fetch an array of all valid CoreGraphics display identifiers.
  48      */
  49     private static native int[] getDisplayIDs();
  50 
  51     /**
  52      * Fetch the CoreGraphics display ID for the 'main' display.
  53      */
  54     private static native int getMainDisplayID();
  55 
  56     /**
  57      * Noop function that just acts as an entry point for someone to force a
  58      * static initialization of this class.
  59      */
  60     public static void init() { }
  61 
  62     static {
  63         java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
  64             public Void run() {
  65                 System.loadLibrary("awt");
  66                 return null;
  67             }
  68         });
  69 
  70         java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
  71             public Void run() {
  72                 if (isHeadless()) return null;
  73                 initCocoa();
  74                 return null;
  75             }
  76         });
  77 
  78         // Install the correct surface manager factory.
  79         SurfaceManagerFactory.setInstance(new MacosxSurfaceManagerFactory());
  80     }
  81 
  82     /**
  83      * Register the instance with CGDisplayRegisterReconfigurationCallback().
  84      * The registration uses a weak global reference -- if our instance is
  85      * garbage collected, the reference will be dropped.
  86      *
  87      * @return Return the registration context (a pointer).
  88      */
  89     private native long registerDisplayReconfiguration();
  90 
  91     /**
  92      * Remove the instance's registration with CGDisplayRemoveReconfigurationCallback()
  93      */
  94     private native void deregisterDisplayReconfiguration(long context);
  95 
  96     /** Available CoreGraphics displays. */
  97     private final Map<Integer, CGraphicsDevice> devices = new HashMap<>(5);
  98 
  99     /** Reference to the display reconfiguration callback context. */
 100     private final long displayReconfigContext;
 101 
 102     /**
 103      * Construct a new instance.
 104      */
 105     public CGraphicsEnvironment() {
 106         if (isHeadless()) {
 107             displayReconfigContext = 0L;
 108             return;
 109         }
 110 
 111         /* Populate the device table */
 112         initDevices();
 113 
 114         /* Register our display reconfiguration listener */
 115         displayReconfigContext = registerDisplayReconfiguration();
 116         if (displayReconfigContext == 0L) {
 117             throw new RuntimeException("Could not register CoreGraphics display reconfiguration callback");
 118         }
 119     }
 120 
 121     /**
 122      * Called by the CoreGraphics Display Reconfiguration Callback.
 123      *
 124      * @param displayId CoreGraphics displayId
 125      * @param removed   true if displayId was removed, false otherwise.
 126      */
 127     void _displayReconfiguration(final int displayId, final boolean removed) {
 128         synchronized (this) {
 129             if (removed && devices.containsKey(displayId)) {
 130                 final CGraphicsDevice gd = devices.remove(displayId);
 131                 gd.invalidate(getMainDisplayID());
 132                 gd.displayChanged();
 133             }
 134         }
 135         initDevices();
 136     }
 137 
 138     @Override
 139     protected void finalize() throws Throwable {
 140         try {
 141             super.finalize();
 142         } finally {
 143             deregisterDisplayReconfiguration(displayReconfigContext);
 144         }
 145     }
 146 
 147     /**
 148      * (Re)create all CGraphicsDevices, reuses a devices if it is possible.


 149      */
 150     private void initDevices() {
 151         synchronized (this) {
 152             final Map<Integer, CGraphicsDevice> old = new HashMap<>(devices);
 153             devices.clear();
 154 
 155             int mainID = getMainDisplayID();
 156 
 157             // initialization of the graphics device may change
 158             // list of displays on hybrid systems via an activation
 159             // of discrete video.
 160             // So, we initialize the main display first, and then
 161             // retrieve actual list of displays.
 162             if (!old.containsKey(mainID)) {
 163                 old.put(mainID, new CGraphicsDevice(mainID));
 164             }
 165 
 166             for (final int id : getDisplayIDs()) {
 167                 devices.put(id, old.containsKey(id) ? old.get(id)
 168                                                     : new CGraphicsDevice(id));


 169             }
 170         }
 171         displayChanged();
 172     }
 173 
 174     @Override
 175     public synchronized GraphicsDevice getDefaultScreenDevice() throws HeadlessException {
 176         final int mainDisplayID = getMainDisplayID();
 177         CGraphicsDevice d = devices.get(mainDisplayID);
 178         if (d == null) {
 179             // we do not expect that this may happen, the only response
 180             // is to re-initialize the list of devices
 181             initDevices();
 182 
 183             d = devices.get(mainDisplayID);
 184         }
 185         return d;
 186     }
 187 
 188     @Override
 189     public synchronized GraphicsDevice[] getScreenDevices() throws HeadlessException {
 190         return devices.values().toArray(new CGraphicsDevice[devices.values().size()]);
 191     }
 192 
 193     public synchronized GraphicsDevice getScreenDevice(int displayID) {
 194         return devices.get(displayID);
 195     }
 196 
 197     @Override
 198     protected synchronized int getNumScreens() {
 199         return devices.size();