forked from bumptech/glide
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes bumptech#738: Add optional threading lock for bitmap manipulati…
…on (affects only Moto X 2nd gen on api 22) this addresses a threading bug on this specific device, the bug manifests itself by displaying black images instead of resized images.
- Loading branch information
Matthijs Mullender
committed
Nov 20, 2015
1 parent
def92c7
commit 8315299
Showing
3 changed files
with
139 additions
and
43 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
library/src/main/java/com/bumptech/glide/load/resource/bitmap/BitmapDrawLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.bumptech.glide.load.resource.bitmap; | ||
|
||
import android.os.Build; | ||
import android.support.annotation.NonNull; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.Condition; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* https://github.com/bumptech/glide/issues/738 On some devices (Moto X with android 5.1) bitmap | ||
* drawing is not thread safe. | ||
* | ||
* This class provides a lock implementation that only really locks for these devices. For other | ||
* types of devices the lock is always available and therefore does not impact performance The | ||
* actual lock logic is delegated to {@see ReentrantLock} | ||
*/ | ||
final class BitmapDrawLock implements Lock { | ||
private static final BitmapDrawLock INSTANCE = new BitmapDrawLock(); | ||
private final Lock lock = new ReentrantLock(); | ||
// only enable the lock for Moto X 2nd gen on android 5.1 | ||
private final boolean shouldUseLock = "XT1097".equals(Build.MODEL) | ||
&& Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1; | ||
|
||
private BitmapDrawLock() { | ||
} | ||
|
||
public static BitmapDrawLock getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public void lock() { | ||
if (!shouldUseLock) { | ||
return; | ||
} | ||
lock.lock(); | ||
} | ||
|
||
@Override | ||
public void lockInterruptibly() throws InterruptedException { | ||
if (!shouldUseLock) { | ||
return; | ||
} | ||
lock.lockInterruptibly(); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Condition newCondition() { | ||
if (!shouldUseLock) { | ||
return null; | ||
} | ||
return lock.newCondition(); | ||
} | ||
|
||
@Override | ||
public boolean tryLock() { | ||
if (!shouldUseLock) { | ||
return true; | ||
} | ||
return lock.tryLock(); | ||
} | ||
|
||
@Override | ||
public boolean tryLock(long l, TimeUnit timeUnit) throws InterruptedException { | ||
if (!shouldUseLock) { | ||
return true; | ||
} | ||
return lock.tryLock(l, timeUnit); | ||
} | ||
|
||
@Override | ||
public void unlock() { | ||
if (!shouldUseLock) { | ||
return; | ||
} | ||
lock.unlock(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters