Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/apple/laf/JRSUIControl.java
38829 views
1
/*
2
* Copyright (c) 2011, 2012, 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 apple.laf;
27
28
import java.nio.*;
29
import java.util.*;
30
31
import apple.laf.JRSUIConstants.*;
32
33
public final class JRSUIControl {
34
private static native int initNativeJRSUI();
35
36
private static native long getPtrOfBuffer(ByteBuffer byteBuffer);
37
private static native long getCFDictionary(boolean flipped);
38
private static native void disposeCFDictionary(long cfDictionaryPtr);
39
40
private static native int syncChanges(long cfDictionaryPtr, long byteBufferPtr);
41
42
// private static native int paint(long cfDictionaryPtr, long oldProperties, long newProperties, OSXSurfaceData osxsd, double x, double y, double w, double h);
43
// private static native int paintChanges(long cfDictionaryPtr, long byteBufferPtr, long oldProperties, long newProperties, OSXSurfaceData osxsd, double x, double y, double w, double h);
44
45
private static native int paintToCGContext (long cgContext, long cfDictionaryPtr, long oldProperties, long newProperties, double x, double y, double w, double h);
46
private static native int paintChangesToCGContext (long cgContext, long cfDictionaryPtr, long oldProperties, long newProperties, double x, double y, double w, double h, long byteBufferPtr);
47
48
private static native int paintImage (int[] data, int imgW, int imgH, long cfDictionaryPtr, long oldProperties, long newProperties, double x, double y, double w, double h);
49
private static native int paintChangesImage (int[] data, int imgW, int imgH, long cfDictionaryPtr, long oldProperties, long newProperties, double x, double y, double w, double h, long byteBufferPtr);
50
51
private static native int getNativeHitPart( long cfDictionaryPtr, long oldProperties, long newProperties, double x, double y, double w, double h, double hitX, double hitY);
52
private static native void getNativePartBounds(final double[] rect, long cfDictionaryPtr, long oldProperties, long newProperties, double x, double y, double w, double h, int part);
53
private static native double getNativeScrollBarOffsetChange( long cfDictionaryPtr, long oldProperties, long newProperties, double x, double y, double w, double h, int offset, int visibleAmount, int extent);
54
55
private static final int INCOHERENT = 2;
56
private static final int NOT_INIT = 1;
57
private static final int SUCCESS = 0;
58
private static final int NULL_PTR = -1;
59
private static final int NULL_CG_REF = -2;
60
61
private static int nativeJRSInitialized = NOT_INIT;
62
63
64
public static void initJRSUI() {
65
if (nativeJRSInitialized == SUCCESS) return;
66
nativeJRSInitialized = initNativeJRSUI();
67
if (nativeJRSInitialized != SUCCESS) throw new RuntimeException("JRSUI could not be initialized (" + nativeJRSInitialized + ").");
68
}
69
70
private static final int NIO_BUFFER_SIZE = 128;
71
private static class ThreadLocalByteBuffer {
72
final ByteBuffer buffer;
73
final long ptr;
74
75
public ThreadLocalByteBuffer() {
76
buffer = ByteBuffer.allocateDirect(NIO_BUFFER_SIZE);
77
buffer.order(ByteOrder.nativeOrder());
78
ptr = getPtrOfBuffer(buffer);
79
}
80
}
81
82
private static final ThreadLocal<ThreadLocalByteBuffer> threadLocal = new ThreadLocal<ThreadLocalByteBuffer>();
83
private static ThreadLocalByteBuffer getThreadLocalBuffer() {
84
ThreadLocalByteBuffer byteBuffer = threadLocal.get();
85
if (byteBuffer != null) return byteBuffer;
86
87
byteBuffer = new ThreadLocalByteBuffer();
88
threadLocal.set(byteBuffer);
89
return byteBuffer;
90
}
91
92
private final HashMap<Key, DoubleValue> nativeMap;
93
private final HashMap<Key, DoubleValue> changes;
94
private long cfDictionaryPtr;
95
96
private long priorEncodedProperties;
97
private long currentEncodedProperties;
98
private final boolean flipped;
99
100
public JRSUIControl(final boolean flipped){
101
this.flipped = flipped;
102
cfDictionaryPtr = getCFDictionary(flipped);
103
if (cfDictionaryPtr == 0) throw new RuntimeException("Unable to create native representation");
104
nativeMap = new HashMap<Key, DoubleValue>();
105
changes = new HashMap<Key, DoubleValue>();
106
}
107
108
JRSUIControl(final JRSUIControl other) {
109
flipped = other.flipped;
110
cfDictionaryPtr = getCFDictionary(flipped);
111
if (cfDictionaryPtr == 0) throw new RuntimeException("Unable to create native representation");
112
nativeMap = new HashMap<Key, DoubleValue>();
113
changes = new HashMap<Key, DoubleValue>(other.nativeMap);
114
changes.putAll(other.changes);
115
}
116
117
protected synchronized final void finalize() throws Throwable {
118
if (cfDictionaryPtr == 0) return;
119
disposeCFDictionary(cfDictionaryPtr);
120
cfDictionaryPtr = 0;
121
}
122
123
124
enum BufferState {
125
NO_CHANGE,
126
ALL_CHANGES_IN_BUFFER,
127
SOME_CHANGES_IN_BUFFER,
128
CHANGE_WONT_FIT_IN_BUFFER;
129
}
130
131
private BufferState loadBufferWithChanges(final ThreadLocalByteBuffer localByteBuffer) {
132
final ByteBuffer buffer = localByteBuffer.buffer;
133
buffer.rewind();
134
135
for (final JRSUIConstants.Key key : new HashSet<JRSUIConstants.Key>(changes.keySet())) {
136
final int changeIndex = buffer.position();
137
final JRSUIConstants.DoubleValue value = changes.get(key);
138
139
try {
140
buffer.putLong(key.getConstantPtr());
141
buffer.put(value.getTypeCode());
142
value.putValueInBuffer(buffer);
143
} catch (final BufferOverflowException e) {
144
return handleBufferOverflow(buffer, changeIndex);
145
} catch (final RuntimeException e) {
146
System.err.println(this);
147
throw e;
148
}
149
150
if (buffer.position() >= NIO_BUFFER_SIZE - 8) {
151
return handleBufferOverflow(buffer, changeIndex);
152
}
153
154
changes.remove(key);
155
nativeMap.put(key, value);
156
}
157
158
buffer.putLong(0);
159
return BufferState.ALL_CHANGES_IN_BUFFER;
160
}
161
162
private BufferState handleBufferOverflow(final ByteBuffer buffer, final int changeIndex) {
163
if (changeIndex == 0) {
164
buffer.putLong(0, 0);
165
return BufferState.CHANGE_WONT_FIT_IN_BUFFER;
166
}
167
168
buffer.putLong(changeIndex, 0);
169
return BufferState.SOME_CHANGES_IN_BUFFER;
170
}
171
172
private synchronized void set(final JRSUIConstants.Key key, final JRSUIConstants.DoubleValue value) {
173
final JRSUIConstants.DoubleValue existingValue = nativeMap.get(key);
174
175
if (existingValue != null && existingValue.equals(value)) {
176
changes.remove(key);
177
return;
178
}
179
180
changes.put(key, value);
181
}
182
183
public void set(final JRSUIState state) {
184
state.apply(this);
185
}
186
187
void setEncodedState(final long state) {
188
currentEncodedProperties = state;
189
}
190
191
void set(final JRSUIConstants.Key key, final double value) {
192
set(key, new JRSUIConstants.DoubleValue(value));
193
}
194
195
// private static final Color blue = new Color(0x00, 0x00, 0xFF, 0x40);
196
// private static void paintDebug(Graphics2D g, double x, double y, double w, double h) {
197
// final Color prev = g.getColor();
198
// g.setColor(blue);
199
// g.drawRect((int)x, (int)y, (int)w, (int)h);
200
// g.setColor(prev);
201
// }
202
203
// private static int paintsWithNoChange = 0;
204
// private static int paintsWithChangesThatFit = 0;
205
// private static int paintsWithChangesThatOverflowed = 0;
206
207
public void paint(final int[] data, final int imgW, final int imgH, final double x, final double y, final double w, final double h) {
208
paintImage(data, imgW, imgH, x, y, w, h);
209
priorEncodedProperties = currentEncodedProperties;
210
}
211
212
private synchronized int paintImage(final int[] data, final int imgW, final int imgH, final double x, final double y, final double w, final double h) {
213
if (changes.isEmpty()) {
214
// paintsWithNoChange++;
215
return paintImage(data, imgW, imgH, cfDictionaryPtr, priorEncodedProperties, currentEncodedProperties, x, y, w, h);
216
}
217
218
final ThreadLocalByteBuffer localByteBuffer = getThreadLocalBuffer();
219
BufferState bufferState = loadBufferWithChanges(localByteBuffer);
220
221
// fast tracking this, since it's the likely scenario
222
if (bufferState == BufferState.ALL_CHANGES_IN_BUFFER) {
223
// paintsWithChangesThatFit++;
224
return paintChangesImage(data, imgW, imgH, cfDictionaryPtr, priorEncodedProperties, currentEncodedProperties, x, y, w, h, localByteBuffer.ptr);
225
}
226
227
while (bufferState == BufferState.SOME_CHANGES_IN_BUFFER) {
228
final int status = syncChanges(cfDictionaryPtr, localByteBuffer.ptr);
229
if (status != SUCCESS) throw new RuntimeException("JRSUI failed to sync changes into the native buffer: " + this);
230
bufferState = loadBufferWithChanges(localByteBuffer);
231
}
232
233
if (bufferState == BufferState.CHANGE_WONT_FIT_IN_BUFFER) {
234
throw new RuntimeException("JRSUI failed to sync changes to the native buffer, because some change was too big: " + this);
235
}
236
237
// implicitly ALL_CHANGES_IN_BUFFER, now that we sync'd the buffer down to native a few times
238
// paintsWithChangesThatOverflowed++;
239
return paintChangesImage(data, imgW, imgH, cfDictionaryPtr, priorEncodedProperties, currentEncodedProperties, x, y, w, h, localByteBuffer.ptr);
240
}
241
242
public void paint(final long cgContext, final double x, final double y, final double w, final double h) {
243
paintToCGContext(cgContext, x, y, w, h);
244
priorEncodedProperties = currentEncodedProperties;
245
}
246
247
private synchronized int paintToCGContext(final long cgContext, final double x, final double y, final double w, final double h) {
248
if (changes.isEmpty()) {
249
// paintsWithNoChange++;
250
return paintToCGContext(cgContext, cfDictionaryPtr, priorEncodedProperties, currentEncodedProperties, x, y, w, h);
251
}
252
253
final ThreadLocalByteBuffer localByteBuffer = getThreadLocalBuffer();
254
BufferState bufferState = loadBufferWithChanges(localByteBuffer);
255
256
// fast tracking this, since it's the likely scenario
257
if (bufferState == BufferState.ALL_CHANGES_IN_BUFFER) {
258
// paintsWithChangesThatFit++;
259
return paintChangesToCGContext(cgContext, cfDictionaryPtr, priorEncodedProperties, currentEncodedProperties, x, y, w, h, localByteBuffer.ptr);
260
}
261
262
while (bufferState == BufferState.SOME_CHANGES_IN_BUFFER) {
263
final int status = syncChanges(cfDictionaryPtr, localByteBuffer.ptr);
264
if (status != SUCCESS) throw new RuntimeException("JRSUI failed to sync changes into the native buffer: " + this);
265
bufferState = loadBufferWithChanges(localByteBuffer);
266
}
267
268
if (bufferState == BufferState.CHANGE_WONT_FIT_IN_BUFFER) {
269
throw new RuntimeException("JRSUI failed to sync changes to the native buffer, because some change was too big: " + this);
270
}
271
272
// implicitly ALL_CHANGES_IN_BUFFER, now that we sync'd the buffer down to native a few times
273
// paintsWithChangesThatOverflowed++;
274
return paintChangesToCGContext(cgContext, cfDictionaryPtr, priorEncodedProperties, currentEncodedProperties, x, y, w, h, localByteBuffer.ptr);
275
}
276
277
278
Hit getHitForPoint(final double x, final double y, final double w, final double h, final double hitX, final double hitY) {
279
sync();
280
// reflect hitY about the midline of the control before sending to native
281
final Hit hit = JRSUIConstants.getHit(getNativeHitPart(cfDictionaryPtr, priorEncodedProperties, currentEncodedProperties, x, y, w, h, hitX, 2 * y + h - hitY));
282
priorEncodedProperties = currentEncodedProperties;
283
return hit;
284
}
285
286
void getPartBounds(final double[] rect, final double x, final double y, final double w, final double h, final int part) {
287
if (rect == null) throw new NullPointerException("Cannot load null rect");
288
if (rect.length != 4) throw new IllegalArgumentException("Rect must have four elements");
289
290
sync();
291
getNativePartBounds(rect, cfDictionaryPtr, priorEncodedProperties, currentEncodedProperties, x, y, w, h, part);
292
priorEncodedProperties = currentEncodedProperties;
293
}
294
295
double getScrollBarOffsetChange(final double x, final double y, final double w, final double h, final int offset, final int visibleAmount, final int extent) {
296
sync();
297
final double offsetChange = getNativeScrollBarOffsetChange(cfDictionaryPtr, priorEncodedProperties, currentEncodedProperties, x, y, w, h, offset, visibleAmount, extent);
298
priorEncodedProperties = currentEncodedProperties;
299
return offsetChange;
300
}
301
302
private void sync() {
303
if (changes.isEmpty()) return;
304
305
final ThreadLocalByteBuffer localByteBuffer = getThreadLocalBuffer();
306
BufferState bufferState = loadBufferWithChanges(localByteBuffer);
307
if (bufferState == BufferState.ALL_CHANGES_IN_BUFFER) {
308
final int status = syncChanges(cfDictionaryPtr, localByteBuffer.ptr);
309
if (status != SUCCESS) throw new RuntimeException("JRSUI failed to sync changes into the native buffer: " + this);
310
return;
311
}
312
313
while (bufferState == BufferState.SOME_CHANGES_IN_BUFFER) {
314
final int status = syncChanges(cfDictionaryPtr, localByteBuffer.ptr);
315
if (status != SUCCESS) throw new RuntimeException("JRSUI failed to sync changes into the native buffer: " + this);
316
bufferState = loadBufferWithChanges(localByteBuffer);
317
}
318
319
if (bufferState == BufferState.CHANGE_WONT_FIT_IN_BUFFER) {
320
throw new RuntimeException("JRSUI failed to sync changes to the native buffer, because some change was too big: " + this);
321
}
322
}
323
324
@Override
325
public int hashCode() {
326
int bits = (int)(currentEncodedProperties ^ (currentEncodedProperties >>> 32));
327
bits ^= nativeMap.hashCode();
328
bits ^= changes.hashCode();
329
return bits;
330
}
331
332
@Override
333
public boolean equals(final Object obj) {
334
if (!(obj instanceof JRSUIControl)) return false;
335
final JRSUIControl other = (JRSUIControl)obj;
336
if (currentEncodedProperties != other.currentEncodedProperties) return false;
337
if (!nativeMap.equals(other.nativeMap)) return false;
338
if (!changes.equals(other.changes)) return false;
339
return true;
340
}
341
342
@Override
343
public String toString() {
344
final StringBuilder builder = new StringBuilder("JRSUIControl[inNative:");
345
builder.append(Arrays.toString(nativeMap.entrySet().toArray()));
346
builder.append(", changes:");
347
builder.append(Arrays.toString(changes.entrySet().toArray()));
348
builder.append("]");
349
return builder.toString();
350
}
351
}
352
353