Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/pipe/hw/AccelDeviceEventNotifier.java
38918 views
/*1* Copyright (c) 2007, 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.java2d.pipe.hw;2627import java.util.Collections;28import java.util.HashMap;29import java.util.Iterator;30import java.util.Map;31import java.util.Set;32import java.lang.annotation.Native;333435/**36* This class is used to notify listeners about accelerated device's37* events such as device reset or dispose that are about to occur.38*/39public class AccelDeviceEventNotifier {4041private static AccelDeviceEventNotifier theInstance;4243/**44* A device is about to be reset. The listeners have to release all45* resources associated with the device which are required for the device46* to be reset.47*/48@Native public static final int DEVICE_RESET = 0;4950/**51* A device is about to be disposed. The listeners have to release all52* resources associated with the device.53*/54@Native public static final int DEVICE_DISPOSED = 1;5556private final Map<AccelDeviceEventListener, Integer> listeners;5758private AccelDeviceEventNotifier() {59listeners = Collections.synchronizedMap(60new HashMap<AccelDeviceEventListener, Integer>(1));61}6263/**64* Returns a singleton of AccelDeviceEventNotifier if it exists. If the65* passed boolean is false and singleton doesn't exist yet, null is66* returned. If the passed boolean is {@code true} and singleton doesn't67* exist it will be created and returned.68*69* @param create whether to create a singleton instance if doesn't yet70* exist71* @return a singleton instance or null72*/73private static synchronized74AccelDeviceEventNotifier getInstance(boolean create)75{76if (theInstance == null && create) {77theInstance = new AccelDeviceEventNotifier();78}79return theInstance;80}8182/**83* Called to indicate that a device event had occurred.84* If a singleton exists, the listeners (those associated with85* the device) will be notified.86*87* @param screen a screen number of the device which is a source of88* the event89* @param eventType a type of the event90* @see #DEVICE_DISPOSED91* @see #DEVICE_RESET92*/93public static final void eventOccured(int screen, int eventType) {94AccelDeviceEventNotifier notifier = getInstance(false);95if (notifier != null) {96notifier.notifyListeners(eventType, screen);97}98}99100/**101* Adds the listener associated with a device on particular screen.102*103* Note: the listener must be removed as otherwise it will forever104* be referenced by the notifier.105*106* @param l the listener107* @param screen the screen number indicating which device the listener is108* interested in.109*/110public static final void addListener(AccelDeviceEventListener l,int screen){111getInstance(true).add(l, screen);112}113114/**115* Removes the listener.116*117* @param l the listener118*/119public static final void removeListener(AccelDeviceEventListener l) {120getInstance(true).remove(l);121}122123private final void add(AccelDeviceEventListener theListener, int screen) {124listeners.put(theListener, screen);125}126private final void remove(AccelDeviceEventListener theListener) {127listeners.remove(theListener);128}129130/**131* Notifies the listeners associated with the screen's device about the132* event.133*134* Implementation note: the current list of listeners is first duplicated135* which allows the listeners to remove themselves during the iteration.136*137* @param screen a screen number with which the device which is a source of138* the event is associated with139* @param eventType a type of the event140* @see #DEVICE_DISPOSED141* @see #DEVICE_RESET142*/143private final void notifyListeners(int deviceEventType, int screen) {144HashMap<AccelDeviceEventListener, Integer> listClone;145Set<AccelDeviceEventListener> cloneSet;146147synchronized(listeners) {148listClone =149new HashMap<AccelDeviceEventListener, Integer>(listeners);150}151152cloneSet = listClone.keySet();153Iterator<AccelDeviceEventListener> itr = cloneSet.iterator();154while (itr.hasNext()) {155AccelDeviceEventListener current = itr.next();156Integer i = listClone.get(current);157// only notify listeners which are interested in this device158if (i != null && i.intValue() != screen) {159continue;160}161if (deviceEventType == DEVICE_RESET) {162current.onDeviceReset();163} else if (deviceEventType == DEVICE_DISPOSED) {164current.onDeviceDispose();165}166}167}168}169170171