Skip to content

Commit

Permalink
Finalize Glide hardware Bitmap dimension/count limits for P+.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 277184075
  • Loading branch information
sjudd authored and glide-copybara-robot committed Oct 29, 2019
1 parent b5a0ed6 commit e8c841c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 36 deletions.
4 changes: 1 addition & 3 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,7 @@ private static void throwIncorrectGlideModule(Exception e) {
@NonNull Map<Class<?>, TransitionOptions<?, ?>> defaultTransitionOptions,
@NonNull List<RequestListener<Object>> defaultRequestListeners,
boolean isLoggingRequestOriginsEnabled,
boolean isImageDecoderEnabledForBitmaps,
int hardwareBitmapFdLimit,
int minHardwareDimension) {
boolean isImageDecoderEnabledForBitmaps) {
this.engine = engine;
this.bitmapPool = bitmapPool;
this.arrayPool = arrayPool;
Expand Down
8 changes: 1 addition & 7 deletions library/src/main/java/com/bumptech/glide/GlideBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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 @@ -67,9 +66,6 @@ public RequestOptions build() {

private boolean isImageDecoderEnabledForBitmaps;

private int hardwareBitmapFdLimit = HardwareConfigState.DEFAULT_MAXIMUM_FDS_FOR_HARDWARE_CONFIGS;
private int minHardwareDimension = HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION;

/**
* Sets the {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool} implementation to use
* to store and retrieve reused {@link android.graphics.Bitmap}s.
Expand Down Expand Up @@ -578,8 +574,6 @@ Glide build(@NonNull Context context) {
defaultTransitionOptions,
defaultRequestListeners,
isLoggingRequestOriginsEnabled,
isImageDecoderEnabledForBitmaps,
hardwareBitmapFdLimit,
minHardwareDimension);
isImageDecoderEnabledForBitmaps);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public final class HardwareConfigState {
*
* @see #FD_SIZE_LIST
*/
public static final int DEFAULT_MIN_HARDWARE_DIMENSION = 128;
@VisibleForTesting static final int MIN_HARDWARE_DIMENSION_O = 128;

private static final int MIN_HARDWARE_DIMENSION_P = 0;

/**
* Allows us to check to make sure we're not exceeding the FD limit for a process with hardware
Expand Down Expand Up @@ -55,14 +57,15 @@ public final class HardwareConfigState {
*
* <p>Access to this variable will be removed in a future version without deprecation.
*/
public static final int DEFAULT_MAXIMUM_FDS_FOR_HARDWARE_CONFIGS = 700;

private static volatile int fdSizeLimit = DEFAULT_MAXIMUM_FDS_FOR_HARDWARE_CONFIGS;
private static volatile int minHardwareDimension = DEFAULT_MIN_HARDWARE_DIMENSION;
private static final int MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_O = 700;
// 20k.
private static final int MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_P = 20000;

private static volatile HardwareConfigState instance;

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

@GuardedBy("this")
private int decodesSinceLastFdCheck;
Expand All @@ -84,7 +87,13 @@ public static HardwareConfigState getInstance() {
@VisibleForTesting
HardwareConfigState() {
isHardwareConfigAllowedByDeviceModel = isHardwareConfigAllowedByDeviceModel();
// Singleton constructor.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
fdCountLimit = MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_P;
minHardwareDimension = MIN_HARDWARE_DIMENSION_P;
} else {
fdCountLimit = MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_O;
minHardwareDimension = MIN_HARDWARE_DIMENSION_O;
}
}

public boolean isHardwareConfigAllowed(
Expand Down Expand Up @@ -151,7 +160,7 @@ private synchronized boolean isFdSizeBelowHardwareLimit() {
if (++decodesSinceLastFdCheck >= MINIMUM_DECODES_BETWEEN_FD_CHECKS) {
decodesSinceLastFdCheck = 0;
int currentFds = FD_SIZE_LIST.list().length;
isFdSizeBelowHardwareLimit = currentFds < fdSizeLimit;
isFdSizeBelowHardwareLimit = currentFds < fdCountLimit;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class HardwareConfigStateTest {
BitmapFactory.Options options = new BitmapFactory.Options();
boolean result =
state.setHardwareConfigIfAllowed(
/*targetWidth=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetHeight=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetWidth=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
/*targetHeight=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
options,
/*isHardwareConfigAllowed=*/ true,
/*isExifOrientationRequired=*/ false);
Expand All @@ -45,8 +45,8 @@ public void setHardwareConfigIfAllowed_withSmallerThanMinWidth_returnsFalse_does

boolean result =
state.setHardwareConfigIfAllowed(
/*targetWidth=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION - 1,
/*targetHeight=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetWidth=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O - 1,
/*targetHeight=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
options,
/*isHardwareConfigAllowed=*/ true,
/*isExifOrientationRequired=*/ false);
Expand All @@ -66,8 +66,8 @@ public void setHardwareConfigIfAllowed_withSmallerThanMinHeight_returnsFalse_doe

boolean result =
state.setHardwareConfigIfAllowed(
/*targetWidth=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetHeight=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION - 1,
/*targetWidth=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
/*targetHeight=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O - 1,
options,
/*isHardwareConfigAllowed=*/ true,
/*isExifOrientationRequired=*/ false);
Expand All @@ -88,8 +88,8 @@ public void setHardwareConfigIfAllowed_withSmallerThanMinHeight_returnsFalse_doe

boolean result =
state.setHardwareConfigIfAllowed(
/*targetWidth=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetHeight=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetWidth=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
/*targetHeight=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
options,
/*isHardwareConfigAllowed=*/ false,
/*isExifOrientationRequired=*/ false);
Expand All @@ -110,8 +110,8 @@ public void setHardwareConfigIfAllowed_withSmallerThanMinHeight_returnsFalse_doe

boolean result =
state.setHardwareConfigIfAllowed(
/*targetWidth=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetHeight=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetWidth=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
/*targetHeight=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
options,
/*isHardwareConfigAllowed=*/ true,
/*isExifOrientationRequired=*/ true);
Expand All @@ -131,8 +131,8 @@ public void setHardwareConfigIfAllowed_withOsLessThanO_returnsFalse_doesNotSetVa

boolean result =
state.setHardwareConfigIfAllowed(
/*targetWidth=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetHeight=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetWidth=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
/*targetHeight=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
options,
/*isHardwareConfigAllowed=*/ true,
/*isExifOrientationRequired=*/ false);
Expand All @@ -158,8 +158,8 @@ public void setHardwareConfigIfAllowed_withOsLessThanO_returnsFalse_doesNotSetVa

boolean result =
state.setHardwareConfigIfAllowed(
/*targetWidth=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetHeight=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetWidth=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
/*targetHeight=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
options,
/*isHardwareConfigAllowed=*/ true,
/*isExifOrientationRequired=*/ false);
Expand All @@ -185,8 +185,8 @@ public void setHardwareConfigIfAllowed_withDisallowedSamsungDevices_OMR1_returns

boolean result =
state.setHardwareConfigIfAllowed(
/*targetWidth=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetHeight=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetWidth=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
/*targetHeight=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
options,
/*isHardwareConfigAllowed=*/ true,
/*isExifOrientationRequired=*/ false);
Expand All @@ -212,8 +212,8 @@ public void setHardwareConfigIfAllowed_withShortEmptyOrNullModelNames_returnsTru

boolean result =
state.setHardwareConfigIfAllowed(
/*targetWidth=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetHeight=*/ HardwareConfigState.DEFAULT_MIN_HARDWARE_DIMENSION,
/*targetWidth=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
/*targetHeight=*/ HardwareConfigState.MIN_HARDWARE_DIMENSION_O,
options,
/*isHardwareConfigAllowed=*/ true,
/*isExifOrientationRequired=*/ false);
Expand Down

0 comments on commit e8c841c

Please sign in to comment.