Skip to content

Commit

Permalink
Hard code disabling hardware bitmaps on O/OMR1. (#5115)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd authored Apr 3, 2023
1 parent 7a02241 commit fe212a6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 219 deletions.
8 changes: 0 additions & 8 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.bumptech.glide.GlideBuilder.DisableHardwareBitmapsOnO;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.Engine;
import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool;
Expand Down Expand Up @@ -328,13 +327,6 @@ private static void throwIncorrectGlideModule(Exception e) {
this.connectivityMonitorFactory = connectivityMonitorFactory;
this.defaultRequestOptionsFactory = defaultRequestOptionsFactory;

DisableHardwareBitmapsOnO disableHardwareBitmapsOnO =
experiments.get(DisableHardwareBitmapsOnO.class);
if (disableHardwareBitmapsOnO != null) {
HardwareConfigState.setDisableHardwareBitmapsOnO(
disableHardwareBitmapsOnO.disableHardwareBitmapsOnO);
}

// This has a circular relationship with Glide and GlideContext in that it depends on both,
// but it's created by Glide's constructor. In practice this shouldn't matter because the
// supplier holding the registry should never be initialized before this constructor finishes.
Expand Down
14 changes: 1 addition & 13 deletions library/src/main/java/com/bumptech/glide/GlideBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,11 @@ public GlideBuilder setImageDecoderEnabledForBitmaps(boolean isEnabled) {
}

/**
* Disables hardware bitmaps if the sdk level is <= O and {@code disableHardwareBitmapsOnO} is
* {@code true}.
*
* @deprecated This method is experimental. It will be hard coded and removed in a future release
* @deprecated This method does nothing. It will be hard coded and removed in a future release
* without further warning.
*/
@Deprecated
public GlideBuilder setDisableHardwareBitmapsOnO(boolean disableHardwareBitmapsOnO) {
glideExperimentsBuilder.add(new DisableHardwareBitmapsOnO(disableHardwareBitmapsOnO));
return this;
}

Expand Down Expand Up @@ -595,14 +591,6 @@ Glide build(
experiments);
}

static final class DisableHardwareBitmapsOnO implements Experiment {
final boolean disableHardwareBitmapsOnO;

DisableHardwareBitmapsOnO(boolean disableHardwareBitmapsOnO) {
this.disableHardwareBitmapsOnO = disableHardwareBitmapsOnO;
}
}

static final class ManualOverrideHardwareBitmapMaxFdCount implements Experiment {

final int fdCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.util.Log;
import androidx.annotation.ChecksSdkIntAtLeast;
import androidx.annotation.GuardedBy;
import androidx.annotation.VisibleForTesting;
import com.bumptech.glide.util.Util;
Expand All @@ -29,21 +31,9 @@ public final class HardwareConfigState {
Build.VERSION.SDK_INT < Build.VERSION_CODES.Q;

/** Support for the hardware bitmap config was added in Android O. */
@ChecksSdkIntAtLeast(api = VERSION_CODES.P)
public static final boolean HARDWARE_BITMAPS_SUPPORTED =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;

/**
* The minimum size in pixels a {@link Bitmap} must be in both dimensions to be created with the
* {@link Bitmap.Config#HARDWARE} configuration.
*
* <p>This is a quick check that lets us skip wasting FDs (see {@link #FD_SIZE_LIST}) on small
* {@link Bitmap}s with relatively low memory costs.
*
* @see #FD_SIZE_LIST
*/
@VisibleForTesting static final int MIN_HARDWARE_DIMENSION_O = 128;

private static final int MIN_HARDWARE_DIMENSION_P = 0;
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;

/**
* Allows us to check to make sure we're not exceeding the FD limit for a process with hardware
Expand All @@ -65,15 +55,6 @@ public final class HardwareConfigState {
* arbitrary.
*/
private static final int MINIMUM_DECODES_BETWEEN_FD_CHECKS = 50;

/**
* 700 with an error of 50 Bitmaps in between at two FDs each lets us use up to 800 FDs for
* hardware Bitmaps.
*
* <p>Prior to P, the limit per process was 1024 FDs. In P, the limit was updated to 32k FDs per
* process.
*/
private static final int MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_O = 700;
// 20k.
private static final int MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_P = 20000;

Expand All @@ -90,12 +71,7 @@ public final class HardwareConfigState {

private static volatile HardwareConfigState instance;

private static boolean disableHardwareBitmapsOnO;

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

@GuardedBy("this")
private int decodesSinceLastFdCheck;

Expand Down Expand Up @@ -123,14 +99,7 @@ public static HardwareConfigState getInstance() {

@VisibleForTesting
HardwareConfigState() {
isHardwareConfigAllowedByDeviceModel = isHardwareConfigAllowedByDeviceModel();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
sdkBasedMaxFdCount = MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_P;
minHardwareDimension = MIN_HARDWARE_DIMENSION_P;
} else {
sdkBasedMaxFdCount = MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_O;
minHardwareDimension = MIN_HARDWARE_DIMENSION_O;
}
sdkBasedMaxFdCount = MAXIMUM_FDS_FOR_HARDWARE_CONFIGS_P;
}

public void blockHardwareBitmaps() {
Expand All @@ -143,22 +112,6 @@ public void unblockHardwareBitmaps() {
isHardwareConfigAllowedByAppState.set(true);
}

/**
* Temporary API to try disabling hardware Bitmaps < Android P.
*
* @deprecated Will be removed in a future version without further warning. This value will be
* hard coded for release.
*/
@Deprecated
public static void setDisableHardwareBitmapsOnO(boolean disableHardwareBitmapsOnO) {
HardwareConfigState.disableHardwareBitmapsOnO = disableHardwareBitmapsOnO;
}

private boolean areHardwareBitmapsAllowedBySdk() {
return HARDWARE_BITMAPS_SUPPORTED
&& (!disableHardwareBitmapsOnO || Build.VERSION.SDK_INT >= Build.VERSION_CODES.P);
}

public boolean isHardwareConfigAllowed(
int targetWidth,
int targetHeight,
Expand All @@ -170,13 +123,7 @@ public boolean isHardwareConfigAllowed(
}
return false;
}
if (!isHardwareConfigAllowedByDeviceModel) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Hardware config disallowed by device model");
}
return false;
}
if (!areHardwareBitmapsAllowedBySdk()) {
if (!HARDWARE_BITMAPS_SUPPORTED) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Hardware config disallowed by sdk");
}
Expand All @@ -194,15 +141,9 @@ public boolean isHardwareConfigAllowed(
}
return false;
}
if (targetWidth < minHardwareDimension) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Hardware config disallowed because width is too small");
}
return false;
}
if (targetHeight < minHardwareDimension) {
if (targetWidth < 0 || targetHeight < 0) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Hardware config disallowed because height is too small");
Log.v(TAG, "Hardware config disallowed because of invalid dimensions");
}
return false;
}
Expand Down Expand Up @@ -239,75 +180,6 @@ boolean setHardwareConfigIfAllowed(
return result;
}

private static boolean isHardwareConfigAllowedByDeviceModel() {
return !isHardwareConfigDisallowedByB112551574() && !isHardwareConfigDisallowedByB147430447();
}

private static boolean isHardwareConfigDisallowedByB147430447() {
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O_MR1) {
return false;
}
// This method will only be called once, so simple iteration is reasonable.
return Arrays.asList(
"LG-M250",
"LG-M320",
"LG-Q710AL",
"LG-Q710PL",
"LGM-K121K",
"LGM-K121L",
"LGM-K121S",
"LGM-X320K",
"LGM-X320L",
"LGM-X320S",
"LGM-X401L",
"LGM-X401S",
"LM-Q610.FG",
"LM-Q610.FGN",
"LM-Q617.FG",
"LM-Q617.FGN",
"LM-Q710.FG",
"LM-Q710.FGN",
"LM-X220PM",
"LM-X220QMA",
"LM-X410PM")
.contains(Build.MODEL);
}

private static boolean isHardwareConfigDisallowedByB112551574() {
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
return false;
}
// This method will only be called once, so simple iteration is reasonable.
for (String prefixOrModelName :
// This is sadly a list of prefixes, not models. We no longer have the data that shows us
// all the explicit models, so we have to live with the prefixes.
Arrays.asList(
// Samsung
"SC-04J",
"SM-N935",
"SM-J720",
"SM-G570F",
"SM-G570M",
"SM-G960",
"SM-G965",
"SM-G935",
"SM-G930",
"SM-A520",
"SM-A720F",
// Moto
"moto e5",
"moto e5 play",
"moto e5 plus",
"moto e5 cruise",
"moto g(6) forge",
"moto g(6) play")) {
if (Build.MODEL.startsWith(prefixOrModelName)) {
return true;
}
}
return false;
}

private static boolean isHardwareBitmapCountReducedOnApi28ByB139097735() {
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.P) {
return false;
Expand Down
Loading

0 comments on commit fe212a6

Please sign in to comment.