Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminRomano committed Apr 3, 2024
2 parents a9d7065 + acced70 commit 09c334f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 102 deletions.
Expand Up @@ -444,8 +444,7 @@ private static String runScaleTest(
int expectedWidth,
int expectedHeight)
throws IOException {
Downsampler downsampler =
hasGainmap ? buildDownsamplerWithGainmapBugFixes() : buildDownsampler();
Downsampler downsampler = buildDownsampler();

InputStream is =
openBitmapStream(format, initialWidth, initialHeight, exifOrientation, hasGainmap);
Expand Down Expand Up @@ -553,18 +552,6 @@ private static Downsampler buildDownsampler() {
return new Downsampler(parsers, displayMetrics, bitmapPool, arrayPool);
}

private static Downsampler buildDownsamplerWithGainmapBugFixes() {
List<ImageHeaderParser> parsers =
Collections.<ImageHeaderParser>singletonList(new DefaultImageHeaderParser());
DisplayMetrics displayMetrics = new DisplayMetrics();
// XHDPI.
displayMetrics.densityDpi = 320;
BitmapPool bitmapPool = new BitmapPoolAdapter();
ArrayPool arrayPool = new LruArrayPool();
return new Downsampler(
parsers, displayMetrics, bitmapPool, arrayPool, /* enableHardwareGainmapFixOnU= */ true);
}

private static InputStream openBitmapStream(
CompressFormat format, int width, int height, int exifOrientation, boolean hasGainmap) {
Preconditions.checkArgument(
Expand Down
11 changes: 3 additions & 8 deletions library/src/main/java/com/bumptech/glide/GlideBuilder.java
Expand Up @@ -497,13 +497,11 @@ public GlideBuilder setPreserveGainmapAndColorSpaceForTransformations(boolean is
}

/**
* Fixes decoding of hardware gainmaps from Ultra HDR images on Android U.
*
* <p>Without this flag on, gainmaps may be dropped when decoding Ultra HDR on Android U devices
* using skiagl for hwui as described in https://github.com/bumptech/glide/issues/5362.
* @deprecated This method does nothing. It will be hard coded and removed in a future release
* without further warning.
*/
@Deprecated
public GlideBuilder setEnableHardwareGainmapFixOnU(boolean isEnabled) {
glideExperimentsBuilder.update(new EnableHardwareGainmapFixOnU(), isEnabled);
return this;
}

Expand Down Expand Up @@ -620,9 +618,6 @@ static final class ManualOverrideHardwareBitmapMaxFdCount implements Experiment
}
}

/** Fixes decoding of hardware gainmaps from Ultra HDR images on Android U. */
static final class EnableHardwareGainmapFixOnU implements Experiment {}

static final class EnableImageDecoderForBitmaps implements Experiment {}

/** See {@link #setLogRequestOrigins(boolean)}. */
Expand Down
Expand Up @@ -12,7 +12,6 @@
import android.os.ParcelFileDescriptor;
import androidx.annotation.Nullable;
import androidx.tracing.Trace;
import com.bumptech.glide.GlideBuilder.EnableHardwareGainmapFixOnU;
import com.bumptech.glide.GlideBuilder.EnableImageDecoderForBitmaps;
import com.bumptech.glide.gifdecoder.GifDecoder;
import com.bumptech.glide.load.ImageHeaderParser;
Expand Down Expand Up @@ -159,8 +158,7 @@ private static void initializeDefaults(
registry.getImageHeaderParsers(),
resources.getDisplayMetrics(),
bitmapPool,
arrayPool,
experiments.isEnabled(EnableHardwareGainmapFixOnU.class));
arrayPool);

ResourceDecoder<ByteBuffer, Bitmap> byteBufferBitmapDecoder;
ResourceDecoder<InputStream, Bitmap> streamBitmapDecoder;
Expand Down
Expand Up @@ -141,35 +141,16 @@ public void onDecodeComplete(BitmapPool bitmapPool, Bitmap downsampled) {
private final ArrayPool byteArrayPool;
private final List<ImageHeaderParser> parsers;
private final HardwareConfigState hardwareConfigState = HardwareConfigState.getInstance();
private final boolean enableHardwareGainmapFixOnU;

public Downsampler(
List<ImageHeaderParser> parsers,
DisplayMetrics displayMetrics,
BitmapPool bitmapPool,
ArrayPool byteArrayPool) {
this(
parsers,
displayMetrics,
bitmapPool,
byteArrayPool,
/* enableHardwareGainmapFixOnU= */ false);
}

/**
* @param enableHardwareGainmapFixOnU Fixes issues with hardware gainmaps on U.
*/
public Downsampler(
List<ImageHeaderParser> parsers,
DisplayMetrics displayMetrics,
BitmapPool bitmapPool,
ArrayPool byteArrayPool,
boolean enableHardwareGainmapFixOnU) {
this.parsers = parsers;
this.displayMetrics = Preconditions.checkNotNull(displayMetrics);
this.bitmapPool = Preconditions.checkNotNull(bitmapPool);
this.byteArrayPool = Preconditions.checkNotNull(byteArrayPool);
this.enableHardwareGainmapFixOnU = enableHardwareGainmapFixOnU;
}

public boolean handles(@SuppressWarnings("unused") InputStream is) {
Expand Down Expand Up @@ -206,8 +187,7 @@ public Resource<Bitmap> decode(
ByteBuffer buffer, int requestedWidth, int requestedHeight, Options options)
throws IOException {
return decode(
new ImageReader.ByteBufferReader(
buffer, parsers, byteArrayPool, enableHardwareGainmapFixOnU),
new ImageReader.ByteBufferReader(buffer, parsers, byteArrayPool),
requestedWidth,
requestedHeight,
options,
Expand Down Expand Up @@ -242,8 +222,7 @@ public Resource<Bitmap> decode(
DecodeCallbacks callbacks)
throws IOException {
return decode(
new ImageReader.InputStreamImageReader(
is, parsers, byteArrayPool, enableHardwareGainmapFixOnU),
new ImageReader.InputStreamImageReader(is, parsers, byteArrayPool),
requestedWidth,
requestedHeight,
options,
Expand All @@ -254,7 +233,7 @@ public Resource<Bitmap> decode(
void decode(byte[] bytes, int requestedWidth, int requestedHeight, Options options)
throws IOException {
decode(
new ImageReader.ByteArrayReader(bytes, parsers, byteArrayPool, enableHardwareGainmapFixOnU),
new ImageReader.ByteArrayReader(bytes, parsers, byteArrayPool),
requestedWidth,
requestedHeight,
options,
Expand All @@ -265,7 +244,7 @@ void decode(byte[] bytes, int requestedWidth, int requestedHeight, Options optio
void decode(File file, int requestedWidth, int requestedHeight, Options options)
throws IOException {
decode(
new ImageReader.FileReader(file, parsers, byteArrayPool, enableHardwareGainmapFixOnU),
new ImageReader.FileReader(file, parsers, byteArrayPool),
requestedWidth,
requestedHeight,
options,
Expand All @@ -278,7 +257,7 @@ public Resource<Bitmap> decode(
throws IOException {
return decode(
new ImageReader.ParcelFileDescriptorImageReader(
parcelFileDescriptor, parsers, byteArrayPool, enableHardwareGainmapFixOnU),
parcelFileDescriptor, parsers, byteArrayPool),
outWidth,
outHeight,
options,
Expand Down
Expand Up @@ -249,8 +249,8 @@ public static Gainmap convertSingleChannelGainmapToTripleChannelGainmap(Gainmap
private static Bitmap copyAlpha8ToOpaqueArgb888(Bitmap bitmap) {
Preconditions.checkArgument(bitmap.getConfig() == Config.ALPHA_8);
// We have to use a canvas operation with an opaque alpha filter to draw the gainmap. We can't
// use bitmap.copy(Config.ARGB_8888, /* isMutable= */ false) because the output bitmap will
// have zero values for alpha.
// use bitmap.copy(Config.ARGB_8888, /* isMutable= */ false) because copying from A8 to RBGA
// will result in zero-valued RGB values.
Bitmap newContents =
Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(newContents);
Expand Down
Expand Up @@ -43,25 +43,17 @@ final class ByteArrayReader implements ImageReader {
private final byte[] bytes;
private final List<ImageHeaderParser> parsers;
private final ArrayPool byteArrayPool;
private final boolean enableHardwareGainmapFixOnU;

ByteArrayReader(
byte[] bytes,
List<ImageHeaderParser> parsers,
ArrayPool byteArrayPool,
boolean enableHardwareGainmapFixOnU) {
ByteArrayReader(byte[] bytes, List<ImageHeaderParser> parsers, ArrayPool byteArrayPool) {
this.bytes = bytes;
this.parsers = parsers;
this.byteArrayPool = byteArrayPool;
this.enableHardwareGainmapFixOnU = enableHardwareGainmapFixOnU;
}

@Nullable
@Override
public Bitmap decodeBitmap(Options options) {
return enableHardwareGainmapFixOnU
? GlideBitmapFactory.decodeByteArray(bytes, options)
: BitmapFactory.decodeByteArray(bytes, /* offset= */ 0, bytes.length, options);
return GlideBitmapFactory.decodeByteArray(bytes, options);
}

@Override
Expand All @@ -76,25 +68,18 @@ public int getImageOrientation() throws IOException {

@Override
public void stopGrowingBuffers() {}

}

final class FileReader implements ImageReader {

private final File file;
private final List<ImageHeaderParser> parsers;
private final ArrayPool byteArrayPool;
private final boolean enableHardwareGainmapFixOnU;

FileReader(
File file,
List<ImageHeaderParser> parsers,
ArrayPool byteArrayPool,
boolean enableHardwareGainmapFixOnU) {
FileReader(File file, List<ImageHeaderParser> parsers, ArrayPool byteArrayPool) {
this.file = file;
this.parsers = parsers;
this.byteArrayPool = byteArrayPool;
this.enableHardwareGainmapFixOnU = enableHardwareGainmapFixOnU;
}

@Nullable
Expand All @@ -103,9 +88,7 @@ public Bitmap decodeBitmap(Options options) throws FileNotFoundException {
InputStream is = null;
try {
is = new RecyclableBufferedInputStream(new FileInputStream(file), byteArrayPool);
return enableHardwareGainmapFixOnU
? GlideBitmapFactory.decodeStream(is, options)
: BitmapFactory.decodeStream(is, /* outPadding= */ null, options);
return GlideBitmapFactory.decodeStream(is, options);
} finally {
if (is != null) {
try {
Expand Down Expand Up @@ -160,26 +143,18 @@ final class ByteBufferReader implements ImageReader {
private final ByteBuffer buffer;
private final List<ImageHeaderParser> parsers;
private final ArrayPool byteArrayPool;
private final boolean enableHardwareGainmapFixOnU;

ByteBufferReader(
ByteBuffer buffer,
List<ImageHeaderParser> parsers,
ArrayPool byteArrayPool,
boolean enableHardwareGainmapFixOnU) {
ByteBufferReader(ByteBuffer buffer, List<ImageHeaderParser> parsers, ArrayPool byteArrayPool) {
this.buffer = buffer;
this.parsers = parsers;
this.byteArrayPool = byteArrayPool;
this.enableHardwareGainmapFixOnU = enableHardwareGainmapFixOnU;
}

@Nullable
@Override
public Bitmap decodeBitmap(Options options) {
InputStream inputStream = stream();
return enableHardwareGainmapFixOnU
? GlideBitmapFactory.decodeStream(inputStream, options)
: BitmapFactory.decodeStream(inputStream, /* outPadding= */ null, options);
return GlideBitmapFactory.decodeStream(inputStream, options);
}

@Override
Expand All @@ -205,27 +180,20 @@ final class InputStreamImageReader implements ImageReader {
private final InputStreamRewinder dataRewinder;
private final ArrayPool byteArrayPool;
private final List<ImageHeaderParser> parsers;
private final boolean enableHardwareGainmapFixOnU;

InputStreamImageReader(
InputStream is,
List<ImageHeaderParser> parsers,
ArrayPool byteArrayPool,
boolean enableHardwareGainmapFixOnU) {
InputStream is, List<ImageHeaderParser> parsers, ArrayPool byteArrayPool) {
this.byteArrayPool = Preconditions.checkNotNull(byteArrayPool);
this.parsers = Preconditions.checkNotNull(parsers);

dataRewinder = new InputStreamRewinder(is, byteArrayPool);
this.enableHardwareGainmapFixOnU = enableHardwareGainmapFixOnU;
}

@Nullable
@Override
public Bitmap decodeBitmap(BitmapFactory.Options options) throws IOException {
InputStream inputStream = dataRewinder.rewindAndGet();
return enableHardwareGainmapFixOnU
? GlideBitmapFactory.decodeStream(inputStream, options)
: BitmapFactory.decodeStream(inputStream, /* outPadding= */ null, options);
return GlideBitmapFactory.decodeStream(inputStream, options);
}

@Override
Expand All @@ -249,29 +217,23 @@ final class ParcelFileDescriptorImageReader implements ImageReader {
private final ArrayPool byteArrayPool;
private final List<ImageHeaderParser> parsers;
private final ParcelFileDescriptorRewinder dataRewinder;
private final boolean enableHardwareGainmapFixOnU;

ParcelFileDescriptorImageReader(
ParcelFileDescriptor parcelFileDescriptor,
List<ImageHeaderParser> parsers,
ArrayPool byteArrayPool,
boolean enableHardwareGainmapFixOnU) {
ArrayPool byteArrayPool) {
this.byteArrayPool = Preconditions.checkNotNull(byteArrayPool);
this.parsers = Preconditions.checkNotNull(parsers);

dataRewinder = new ParcelFileDescriptorRewinder(parcelFileDescriptor);
this.enableHardwareGainmapFixOnU = enableHardwareGainmapFixOnU;
}

@Nullable
@Override
public Bitmap decodeBitmap(BitmapFactory.Options options) throws IOException {
ParcelFileDescriptor parcelFileDescriptor = dataRewinder.rewindAndGet();
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
return enableHardwareGainmapFixOnU
? GlideBitmapFactory.decodeFileDescriptor(fileDescriptor, options)
: BitmapFactory.decodeFileDescriptor(
parcelFileDescriptor.getFileDescriptor(), /* outPadding= */ null, options);
return GlideBitmapFactory.decodeFileDescriptor(fileDescriptor, options);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Expand Up @@ -74,7 +74,7 @@ dependencyResolutionManagement {
library('android-gradle', 'com.android.tools.build:gradle:8.1.1')
library('androidx-cardview', 'androidx.cardview:cardview:1.0.0')
library('androidx-core', 'androidx.core:core:1.12.0')
library('androidx-annotation', 'androidx.annotation:annotation:1.6.0')
library('androidx-annotation', 'androidx.annotation:annotation:1.7.1')
library('androidx-appcompat', 'androidx.appcompat:appcompat:1.6.1')
library('androidx-benchmark.gradle', 'androidx.benchmark', 'benchmark-gradle-plugin').versionRef('androidx-benchmark')
library('androidx-benchmark.junit', 'androidx.benchmark', 'benchmark-junit4').versionRef('androidx-benchmark')
Expand Down

0 comments on commit 09c334f

Please sign in to comment.