Skip to content

Commit

Permalink
Add a new experimental hardware bitmap fd limit parameter to Glide.
Browse files Browse the repository at this point in the history
For now this parameter isn't available publicy.

PiperOrigin-RevId: 313444962
  • Loading branch information
sjudd authored and glide-copybara-robot committed May 27, 2020
1 parent a77ed90 commit f903a73
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
3 changes: 2 additions & 1 deletion library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ private static void throwIncorrectGlideModule(Exception e) {
@NonNull List<RequestListener<Object>> defaultRequestListeners,
boolean isLoggingRequestOriginsEnabled,
boolean isImageDecoderEnabledForBitmaps,
boolean waitForFirstFrameBeforeEnablingHardwareBitmaps) {
boolean waitForFirstFrameBeforeEnablingHardwareBitmaps,
int manualOverrideHardwareBitmapMaxFdCount) {
this.engine = engine;
this.bitmapPool = bitmapPool;
this.arrayPool = arrayPool;
Expand Down
5 changes: 4 additions & 1 deletion library/src/main/java/com/bumptech/glide/GlideBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.bumptech.glide.load.engine.cache.MemoryCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.load.engine.executor.GlideExecutor;
import com.bumptech.glide.load.resource.bitmap.HardwareConfigState;
import com.bumptech.glide.manager.ConnectivityMonitorFactory;
import com.bumptech.glide.manager.DefaultConnectivityMonitorFactory;
import com.bumptech.glide.manager.RequestManagerRetriever;
Expand Down Expand Up @@ -66,6 +67,7 @@ public RequestOptions build() {

private boolean isImageDecoderEnabledForBitmaps;
private boolean waitForFirstFrameBeforeEnablingHardwareBitmaps;
private int manualOverrideHardwareBitmapMaxFdCount = HardwareConfigState.NO_MAX_FD_COUNT;

/**
* Sets the {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool} implementation to use
Expand Down Expand Up @@ -577,6 +579,7 @@ Glide build(@NonNull Context context) {
defaultRequestListeners,
isLoggingRequestOriginsEnabled,
isImageDecoderEnabledForBitmaps,
waitForFirstFrameBeforeEnablingHardwareBitmaps);
waitForFirstFrameBeforeEnablingHardwareBitmaps,
manualOverrideHardwareBitmapMaxFdCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ public final class HardwareConfigState {
// 20k.
private static final int MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_P = 20000;

/** This constant will be removed in a future version without deprecation, avoid using it. */
public static final int NO_MAX_FD_COUNT = -1;

private static volatile HardwareConfigState instance;
private static volatile boolean waitForFirstFrame;
private static volatile int manualOverrideMaxFdCount = NO_MAX_FD_COUNT;

private final boolean isHardwareConfigAllowedByDeviceModel;
private final int fdCountLimit;
private final int sdkBasedMaxFdCount;
private final int minHardwareDimension;

@GuardedBy("this")
Expand All @@ -92,10 +96,10 @@ public static HardwareConfigState getInstance() {
HardwareConfigState() {
isHardwareConfigAllowedByDeviceModel = isHardwareConfigAllowedByDeviceModel();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
fdCountLimit = MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_P;
sdkBasedMaxFdCount = MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_P;
minHardwareDimension = MIN_HARDWARE_DIMENSION_P;
} else {
fdCountLimit = MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_O;
sdkBasedMaxFdCount = MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_O;
minHardwareDimension = MIN_HARDWARE_DIMENSION_O;
}
}
Expand Down Expand Up @@ -197,11 +201,18 @@ private static boolean isHardwareConfigDisallowedByB112551574() {
}
}

private int getMaxFdCount() {
return manualOverrideMaxFdCount != NO_MAX_FD_COUNT
? manualOverrideMaxFdCount
: sdkBasedMaxFdCount;
}

private synchronized boolean isFdSizeBelowHardwareLimit() {
if (++decodesSinceLastFdCheck >= MINIMUM_DECODES_BETWEEN_FD_CHECKS) {
decodesSinceLastFdCheck = 0;
int currentFds = FD_SIZE_LIST.list().length;
isFdSizeBelowHardwareLimit = currentFds < fdCountLimit;
long maxFdCount = getMaxFdCount();
isFdSizeBelowHardwareLimit = currentFds < maxFdCount;

if (!isFdSizeBelowHardwareLimit && Log.isLoggable(Downsampler.TAG, Log.WARN)) {
Log.w(
Expand All @@ -210,7 +221,7 @@ private synchronized boolean isFdSizeBelowHardwareLimit() {
+ ", file descriptors "
+ currentFds
+ ", limit "
+ fdCountLimit);
+ maxFdCount);
}
}

Expand Down

0 comments on commit f903a73

Please sign in to comment.