Skip to content

Commit 387aba8

Browse files
stephengoldAli-RS
authored andcommitted
solve issue #1806 (global FrameInterpolator violates threading model) (#1943)
* solve issue #1806 (global FrameInterpolator violates threading model) * FrameInterpolator: deprecate the global instance
1 parent 524c57f commit 387aba8

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

jme3-core/src/main/java/com/jme3/anim/MorphTrack.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2023 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -52,7 +52,11 @@ public class MorphTrack implements AnimTrack<float[]> {
5252
* Weights and times for track.
5353
*/
5454
private float[] weights;
55-
private FrameInterpolator interpolator = FrameInterpolator.DEFAULT;
55+
/**
56+
* The interpolator to use, or null to always use the default interpolator
57+
* of the current thread.
58+
*/
59+
private FrameInterpolator interpolator = null;
5660
private float[] times;
5761
private int nbMorphTargets;
5862

@@ -219,7 +223,9 @@ public void getDataAtTime(double t, float[] store) {
219223
/ (times[endFrame] - times[startFrame]);
220224
}
221225

222-
interpolator.interpolateWeights(blend, startFrame, weights, nbMorphTargets, store);
226+
FrameInterpolator fi = (interpolator == null)
227+
? FrameInterpolator.getThreadDefault() : interpolator;
228+
fi.interpolateWeights(blend, startFrame, weights, nbMorphTargets, store);
223229
}
224230

225231
/**

jme3-core/src/main/java/com/jme3/anim/TransformTrack.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2023 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -50,7 +50,11 @@
5050
public class TransformTrack implements AnimTrack<Transform> {
5151

5252
private double length;
53-
private FrameInterpolator interpolator = FrameInterpolator.DEFAULT;
53+
/**
54+
* The interpolator to use, or null to always use the default interpolator
55+
* of the current thread.
56+
*/
57+
private FrameInterpolator interpolator = null;
5458
private HasLocalTransform target;
5559

5660
/**
@@ -281,7 +285,10 @@ public void getDataAtTime(double t, Transform transform) {
281285
/ (times[endFrame] - times[startFrame]);
282286
}
283287

284-
Transform interpolated = interpolator.interpolate(blend, startFrame, translations, rotations, scales, times);
288+
FrameInterpolator fi = (interpolator == null)
289+
? FrameInterpolator.getThreadDefault() : interpolator;
290+
Transform interpolated = fi.interpolate(
291+
blend, startFrame, translations, rotations, scales, times);
285292

286293
if (translations != null) {
287294
transform.setTranslation(interpolated.getTranslation());

jme3-core/src/main/java/com/jme3/anim/interpolator/FrameInterpolator.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2023 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -38,7 +38,19 @@
3838
* Created by nehon on 15/04/17.
3939
*/
4040
public class FrameInterpolator {
41+
/**
42+
* A global default instance of this class, for compatibility with JME v3.5.
43+
* Due to issue #1806, use of this instance is discouraged.
44+
*
45+
* @deprecated use {@link #getThreadDefault()}
46+
*/
47+
@Deprecated
4148
public static final FrameInterpolator DEFAULT = new FrameInterpolator();
49+
/**
50+
* The per-thread default instances of this class.
51+
*/
52+
private static final ThreadLocal<FrameInterpolator> THREAD_DEFAULT
53+
= ThreadLocal.withInitial(() -> new FrameInterpolator());
4254

4355
private AnimInterpolator<Float> timeInterpolator;
4456
private AnimInterpolator<Vector3f> translationInterpolator = AnimInterpolators.LinearVec3f;
@@ -52,6 +64,16 @@ public class FrameInterpolator {
5264

5365
final private Transform transforms = new Transform();
5466

67+
/**
68+
* Obtain the default interpolator for the current thread.
69+
*
70+
* @return the pre-existing instance (not null)
71+
*/
72+
public static FrameInterpolator getThreadDefault() {
73+
FrameInterpolator result = THREAD_DEFAULT.get();
74+
return result;
75+
}
76+
5577
public Transform interpolate(float t, int currentIndex, CompactVector3Array translations,
5678
CompactQuaternionArray rotations, CompactVector3Array scales, float[] times) {
5779
timesReader.setData(times);

0 commit comments

Comments
 (0)