Skip to content

Commit

Permalink
Fix blinking black in multithreading mode (#1575)
Browse files Browse the repository at this point in the history
Co-authored-by: Nikita Bukhal <n.bukhal@corp.vk.com>
  • Loading branch information
tttzof351 and Nikita Bukhal authored Aug 27, 2020
1 parent 937f535 commit dfe7ced
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions lottie/src/main/java/com/airbnb/lottie/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,37 @@
public final class Utils {
public static final int SECOND_IN_NANOS = 1000000000;

private static final PathMeasure pathMeasure = new PathMeasure();
private static final Path tempPath = new Path();
private static final Path tempPath2 = new Path();
private static final float[] points = new float[4];
/**
* Wrap in Local Thread is necessary for prevent race condition in multi-threaded mode
*/
private static final ThreadLocal<PathMeasure> threadLocalPathMeasure = new ThreadLocal<PathMeasure>() {
@Override
protected PathMeasure initialValue() {
return new PathMeasure();
}
};

private static final ThreadLocal<Path> threadLocalTempPath = new ThreadLocal<Path>() {
@Override
protected Path initialValue() {
return new Path();
}
};

private static final ThreadLocal<Path> threadLocalTempPath2 = new ThreadLocal<Path>() {
@Override
protected Path initialValue() {
return new Path();
}
};

private static final ThreadLocal<float[]> threadLocalPoints = new ThreadLocal<float[]>() {
@Override
protected float[] initialValue() {
return new float[4];
}
};

private static final float INV_SQRT_2 = (float) (Math.sqrt(2) / 2.0);
private static float dpScale = -1;

Expand Down Expand Up @@ -71,6 +98,8 @@ public static void closeQuietly(Closeable closeable) {
}

public static float getScale(Matrix matrix) {
final float[] points = threadLocalPoints.get();

points[0] = 0;
points[1] = 0;
// Use 1/sqrt(2) so that the hypotenuse is of length 1.
Expand All @@ -84,6 +113,8 @@ public static float getScale(Matrix matrix) {
}

public static boolean hasZeroScaleAxis(Matrix matrix) {
final float[] points = threadLocalPoints.get();

points[0] = 0;
points[1] = 0;
// Random numbers. The only way these should map to the same thing as 0,0 is if the scale is 0.
Expand All @@ -109,6 +140,10 @@ public static void applyTrimPathIfNeeded(Path path, @Nullable TrimPathContent tr
public static void applyTrimPathIfNeeded(
Path path, float startValue, float endValue, float offsetValue) {
L.beginSection("applyTrimPathIfNeeded");
final PathMeasure pathMeasure = threadLocalPathMeasure.get();
final Path tempPath = threadLocalTempPath.get();
final Path tempPath2 = threadLocalTempPath2.get();

pathMeasure.setPath(path, false);

float length = pathMeasure.getLength();
Expand Down

0 comments on commit dfe7ced

Please sign in to comment.