author | serb |
Fri, 22 May 2015 23:26:00 +0300 | |
changeset 30948 | 0a0972d3b58d |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
887 | 1 |
/* |
16734
da1901d79073
8000406: change files using @GenerateNativeHeader to use @Native
dxu
parents:
12813
diff
changeset
|
2 |
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. |
887 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
887 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
887 | 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 |
* |
|
5506 | 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. |
|
887 | 24 |
*/ |
25 |
||
26 |
package sun.java2d.pipe.hw; |
|
27 |
||
28 |
import java.util.Collections; |
|
29 |
import java.util.HashMap; |
|
30 |
import java.util.Iterator; |
|
31 |
import java.util.Map; |
|
32 |
import java.util.Set; |
|
16734
da1901d79073
8000406: change files using @GenerateNativeHeader to use @Native
dxu
parents:
12813
diff
changeset
|
33 |
import java.lang.annotation.Native; |
12813
c10ab96dcf41
7170969: Add @GenerateNativeHeader to classes whose fields need to be exported for JNI
erikj
parents:
5506
diff
changeset
|
34 |
|
887 | 35 |
|
36 |
/** |
|
37 |
* This class is used to notify listeners about accelerated device's |
|
38 |
* events such as device reset or dispose that are about to occur. |
|
39 |
*/ |
|
40 |
public class AccelDeviceEventNotifier { |
|
41 |
||
42 |
private static AccelDeviceEventNotifier theInstance; |
|
43 |
||
44 |
/** |
|
45 |
* A device is about to be reset. The listeners have to release all |
|
46 |
* resources associated with the device which are required for the device |
|
47 |
* to be reset. |
|
48 |
*/ |
|
16734
da1901d79073
8000406: change files using @GenerateNativeHeader to use @Native
dxu
parents:
12813
diff
changeset
|
49 |
@Native public static final int DEVICE_RESET = 0; |
887 | 50 |
|
51 |
/** |
|
52 |
* A device is about to be disposed. The listeners have to release all |
|
53 |
* resources associated with the device. |
|
54 |
*/ |
|
16734
da1901d79073
8000406: change files using @GenerateNativeHeader to use @Native
dxu
parents:
12813
diff
changeset
|
55 |
@Native public static final int DEVICE_DISPOSED = 1; |
887 | 56 |
|
57 |
private final Map<AccelDeviceEventListener, Integer> listeners; |
|
58 |
||
59 |
private AccelDeviceEventNotifier() { |
|
60 |
listeners = Collections.synchronizedMap( |
|
61 |
new HashMap<AccelDeviceEventListener, Integer>(1)); |
|
62 |
} |
|
63 |
||
64 |
/** |
|
65 |
* Returns a singleton of AccelDeviceEventNotifier if it exists. If the |
|
66 |
* passed boolean is false and singleton doesn't exist yet, null is |
|
67 |
* returned. If the passed boolean is {@code true} and singleton doesn't |
|
68 |
* exist it will be created and returned. |
|
69 |
* |
|
70 |
* @param create whether to create a singleton instance if doesn't yet |
|
71 |
* exist |
|
72 |
* @return a singleton instance or null |
|
73 |
*/ |
|
74 |
private static synchronized |
|
75 |
AccelDeviceEventNotifier getInstance(boolean create) |
|
76 |
{ |
|
77 |
if (theInstance == null && create) { |
|
78 |
theInstance = new AccelDeviceEventNotifier(); |
|
79 |
} |
|
80 |
return theInstance; |
|
81 |
} |
|
82 |
||
83 |
/** |
|
21278 | 84 |
* Called to indicate that a device event had occurred. |
887 | 85 |
* If a singleton exists, the listeners (those associated with |
86 |
* the device) will be notified. |
|
87 |
* |
|
88 |
* @param screen a screen number of the device which is a source of |
|
89 |
* the event |
|
90 |
* @param eventType a type of the event |
|
91 |
* @see #DEVICE_DISPOSED |
|
92 |
* @see #DEVICE_RESET |
|
93 |
*/ |
|
94 |
public static final void eventOccured(int screen, int eventType) { |
|
95 |
AccelDeviceEventNotifier notifier = getInstance(false); |
|
96 |
if (notifier != null) { |
|
97 |
notifier.notifyListeners(eventType, screen); |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
/** |
|
102 |
* Adds the listener associated with a device on particular screen. |
|
103 |
* |
|
104 |
* Note: the listener must be removed as otherwise it will forever |
|
105 |
* be referenced by the notifier. |
|
106 |
* |
|
107 |
* @param l the listener |
|
108 |
* @param screen the screen number indicating which device the listener is |
|
109 |
* interested in. |
|
110 |
*/ |
|
111 |
public static final void addListener(AccelDeviceEventListener l,int screen){ |
|
112 |
getInstance(true).add(l, screen); |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Removes the listener. |
|
117 |
* |
|
118 |
* @param l the listener |
|
119 |
*/ |
|
120 |
public static final void removeListener(AccelDeviceEventListener l) { |
|
121 |
getInstance(true).remove(l); |
|
122 |
} |
|
123 |
||
124 |
private final void add(AccelDeviceEventListener theListener, int screen) { |
|
125 |
listeners.put(theListener, screen); |
|
126 |
} |
|
127 |
private final void remove(AccelDeviceEventListener theListener) { |
|
128 |
listeners.remove(theListener); |
|
129 |
} |
|
130 |
||
131 |
/** |
|
132 |
* Notifies the listeners associated with the screen's device about the |
|
133 |
* event. |
|
134 |
* |
|
135 |
* Implementation note: the current list of listeners is first duplicated |
|
136 |
* which allows the listeners to remove themselves during the iteration. |
|
137 |
* |
|
138 |
* @param screen a screen number with which the device which is a source of |
|
139 |
* the event is associated with |
|
30948
0a0972d3b58d
6587235: Incorrect javadoc: "no parameter" in 2d source code
serb
parents:
25859
diff
changeset
|
140 |
* @param deviceEventType a type of the event |
887 | 141 |
* @see #DEVICE_DISPOSED |
142 |
* @see #DEVICE_RESET |
|
143 |
*/ |
|
144 |
private final void notifyListeners(int deviceEventType, int screen) { |
|
145 |
HashMap<AccelDeviceEventListener, Integer> listClone; |
|
146 |
Set<AccelDeviceEventListener> cloneSet; |
|
147 |
||
148 |
synchronized(listeners) { |
|
149 |
listClone = |
|
150 |
new HashMap<AccelDeviceEventListener, Integer>(listeners); |
|
151 |
} |
|
152 |
||
153 |
cloneSet = listClone.keySet(); |
|
154 |
Iterator<AccelDeviceEventListener> itr = cloneSet.iterator(); |
|
155 |
while (itr.hasNext()) { |
|
156 |
AccelDeviceEventListener current = itr.next(); |
|
157 |
Integer i = listClone.get(current); |
|
158 |
// only notify listeners which are interested in this device |
|
159 |
if (i != null && i.intValue() != screen) { |
|
160 |
continue; |
|
161 |
} |
|
162 |
if (deviceEventType == DEVICE_RESET) { |
|
163 |
current.onDeviceReset(); |
|
164 |
} else if (deviceEventType == DEVICE_DISPOSED) { |
|
165 |
current.onDeviceDispose(); |
|
166 |
} |
|
167 |
} |
|
168 |
} |
|
169 |
} |