-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathAbstractPrimaryTimer.java
338 lines (297 loc) · 13 KB
/
AbstractPrimaryTimer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.scenario.animation;
import java.util.Arrays;
import javafx.util.Callback;
import com.sun.javafx.animation.TickCalculation;
import com.sun.scenario.DelayedRunnable;
import com.sun.scenario.Settings;
import com.sun.scenario.animation.shared.PulseReceiver;
import com.sun.scenario.animation.shared.TimerReceiver;
public abstract class AbstractPrimaryTimer {
protected final static String FULLSPEED_PROP = "javafx.animation.fullspeed";
private static boolean fullspeed = Settings.getBoolean(FULLSPEED_PROP);
// enables the code path which estimates the next pulse time to be just
// enough in advance of the vsync to complete rendering before it happens
protected final static String ADAPTIVE_PULSE_PROP = "com.sun.scenario.animation.adaptivepulse";
private static boolean useAdaptivePulse = Settings.getBoolean(ADAPTIVE_PULSE_PROP);
// another property which is controlling whether vsync is enabled:
// "com.sun.scenario.animation.vsync". if true, JSGPanel will enable vsync
// for the toplevel it's in. See JSGPanel.
// properties to override the default pulse rate (set in hz - number of
// pulses per second)
protected final static String PULSE_PROP = "javafx.animation.pulse";
protected final static String FRAMERATE_PROP = "javafx.animation.framerate";
protected final static String FIXED_PULSE_LENGTH_PROP = "com.sun.scenario.animation.fixed.pulse.length";
// property to enable AnimationPulse data gathering
// note: it can be enabled via the MBean itself too
protected final static String ANIMATION_MBEAN_ENABLED = "com.sun.scenario.animation.AnimationMBean.enabled";
protected final static boolean enableAnimationMBean = false;
private final int PULSE_DURATION_NS = getPulseDuration(1000000000);
private final int PULSE_DURATION_TICKS = getPulseDuration((int)TickCalculation.fromMillis(1000));
// This PropertyChangeListener is added to Settings to listen for changes
// to the nogap and fullspeed properties.
private static Callback<String, Void> pcl = key -> {
switch (key) {
case FULLSPEED_PROP:
fullspeed = Settings.getBoolean(FULLSPEED_PROP);
break;
case ADAPTIVE_PULSE_PROP:
useAdaptivePulse = Settings.getBoolean(ADAPTIVE_PULSE_PROP);
break;
case ANIMATION_MBEAN_ENABLED:
AnimationPulse.getDefaultBean()
.setEnabled(Settings.getBoolean(ANIMATION_MBEAN_ENABLED));
break;
}
return null;
};
private PulseReceiver receivers[] = new PulseReceiver[2];
private int receiversLength;
private boolean receiversLocked;
// synchronize to update frameJobList and frameJobs
private TimerReceiver animationTimers[] = new TimerReceiver[2]; // frameJobList
// snapshot
private int animationTimersLength;
private boolean animationTimersLocked;
// These two variables are ONLY USED if FIXED_PULSE_LENGTH_PROP is true. In this
// case, instead of advancing time based on the system time (nanos etc) we instead
// increment each animation by a fixed length of time for each pulse. This is
// handy while debugging.
private final long fixedPulseLength = Boolean.getBoolean(FIXED_PULSE_LENGTH_PROP) ? PULSE_DURATION_NS : 0;
private long debugNanos = 0;
private final MainLoop theMainLoop = new MainLoop();
static {
Settings.addPropertyChangeListener(pcl);
int pulse = Settings.getInt(PULSE_PROP, -1);
boolean verbose = Boolean.getBoolean("javafx.verbose");
if (verbose && pulse != -1) {
System.out.println("Setting PULSE_DURATION to " + pulse + " hz");
}
}
// Used by Clip.create() method that doesn't take a resolution argument
public int getDefaultResolution() {
return PULSE_DURATION_TICKS;
}
public long nanos() {
if (fixedPulseLength > 0) {
return debugNanos;
}
return System.nanoTime();
}
public boolean isFullspeed() {
return fullspeed;
}
protected AbstractPrimaryTimer() {
}
/**
* Adds a PulseReceiver to the list of targets being tracked against the
* global schedule. The target should already have an absolute start time
* recorded in it and that time will be used to start the clip at the
* appropriate wall clock time as defined by milliTime().
*
* Note that pulseReceiver cannot be removed from the PrimaryTimer directly.
* It is removed automatically in the timePulse-iteration if timePulse
* returns true.
*
* @param target
* the Clip to be added to the scheduling queue
*/
public void addPulseReceiver(PulseReceiver target) {
boolean needMoreSize = receiversLength == receivers.length;
if (receiversLocked || needMoreSize) {
receivers = Arrays.copyOf(receivers, needMoreSize ? receivers.length * 3 / 2 + 1 : receivers.length);
receiversLocked = false;
}
receivers[receiversLength++] = target;
if (receiversLength == 1) {
theMainLoop.updateAnimationRunnable();
}
}
public void removePulseReceiver(PulseReceiver target) {
if (receiversLocked) {
receivers = receivers.clone();
receiversLocked = false;
}
for (int i = 0; i < receiversLength; ++i) {
if (target == receivers[i]) {
if (i == receiversLength - 1) {
receivers[i] = null;
} else {
System.arraycopy(receivers, i + 1, receivers, i, receiversLength - i - 1);
receivers[receiversLength - 1] = null;
}
--receiversLength;
break;
}
}
if (receiversLength == 0) {
theMainLoop.updateAnimationRunnable();
}
}
public void addAnimationTimer(TimerReceiver timer) {
boolean needMoreSize = animationTimersLength == animationTimers.length;
if (animationTimersLocked || needMoreSize) {
animationTimers = Arrays.copyOf(animationTimers, needMoreSize ? animationTimers.length * 3 / 2 + 1 : animationTimers.length);
animationTimersLocked = false;
}
animationTimers[animationTimersLength++] = timer;
if (animationTimersLength == 1) {
theMainLoop.updateAnimationRunnable();
}
}
public void removeAnimationTimer(TimerReceiver timer) {
if (animationTimersLocked) {
animationTimers = animationTimers.clone();
animationTimersLocked = false;
}
for (int i = 0; i < animationTimersLength; ++i) {
if (timer == animationTimers[i]) {
if (i == animationTimersLength - 1) {
animationTimers[i] = null;
} else {
System.arraycopy(animationTimers, i + 1, animationTimers, i, animationTimersLength - i - 1);
animationTimers[animationTimersLength - 1] = null;
}
--animationTimersLength;
break;
}
}
if (animationTimersLength == 0) {
theMainLoop.updateAnimationRunnable();
}
}
/*
* methods to record times for different stages of a pulse overriden in
* PrimaryTimer to collect data for AnimationPulse Mbean
*/
protected void recordStart(long shiftMillis) {
}
protected void recordEnd() {
}
protected void recordAnimationEnd() {
}
/**
* Hidden inner class to run the main timing loop. This is the
* "AnimationRunnable" for Desktop and TV
*/
private final class MainLoop implements DelayedRunnable {
private boolean inactive = true;
private long nextPulseTime = nanos();
private long lastPulseDuration = Integer.MIN_VALUE;
@Override
public void run() {
final long now = nanos();
recordStart((nextPulseTime - now) / 1000000);
timePulseImpl(now);
recordEnd();
updateNextPulseTime(now);
// reschedule animation runnable if needed
updateAnimationRunnable();
}
@Override
public long getDelay() {
final long now = nanos();
final long timeUntilPulse = (nextPulseTime - now) / 1000000;
return Math.max(0, timeUntilPulse);
}
private void updateNextPulseTime(long pulseStarted) {
final long now = nanos();
if (fullspeed) {
nextPulseTime = now;
} else {
if (useAdaptivePulse) {
// Estimate the next pulse time such that we wake up just
// early enough to finish up the painting and call swap
// before vsync happens. We try to minimize the amount of
// time we wait for vsync blocking the EDT thread.
nextPulseTime += PULSE_DURATION_NS;
long pulseDuration = now - pulseStarted;
// if the new duration was smaller than the previous one
// we don't need to do anything (we have decreased the
// duration), but if it's longer to within 1/2ms then we
// try to halve the next anticipated duration (but not
// closer
// than 2ms within the next expected pulse)
if (pulseDuration - lastPulseDuration > 500000) {
pulseDuration /= 2;
}
if (pulseDuration < 2000000) {
pulseDuration = 2000000;
}
// if the pulse took longer than pulse_duration_ns we
// probably missed the vsync
if (pulseDuration >= PULSE_DURATION_NS) {
pulseDuration = 3 * PULSE_DURATION_NS / 4;
}
lastPulseDuration = pulseDuration;
nextPulseTime = nextPulseTime - pulseDuration;
} else {
nextPulseTime = ((nextPulseTime + PULSE_DURATION_NS) / PULSE_DURATION_NS)
* PULSE_DURATION_NS;
}
}
}
private void updateAnimationRunnable() {
final boolean newInactive = (animationTimersLength == 0 && receiversLength == 0);
if (inactive != newInactive) {
inactive = newInactive;
final DelayedRunnable animationRunnable = inactive? null : this;
postUpdateAnimationRunnable(animationRunnable);
}
}
}
protected abstract void postUpdateAnimationRunnable(
DelayedRunnable animationRunnable);
protected abstract int getPulseDuration(int precision);
protected void timePulseImpl(long now) {
if (fixedPulseLength > 0) {
debugNanos += fixedPulseLength;
now = debugNanos;
}
final PulseReceiver receiversSnapshot[] = receivers;
final int rLength = receiversLength;
try {
receiversLocked = true;
for (int i = 0; i < rLength; i++) {
receiversSnapshot[i].timePulse(TickCalculation.fromNano(now));
}
} finally {
receiversLocked = false;
}
recordAnimationEnd();
final TimerReceiver animationTimersSnapshot[] = animationTimers;
final int aTLength = animationTimersLength;
try {
animationTimersLocked = true;
// After every frame, call any frame jobs
for (int i = 0; i < aTLength; i++) {
animationTimersSnapshot[i].handle(now);
}
} finally {
animationTimersLocked = false;
}
}
}