bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.72k stars 6.13k forks source link

DecodeFormat PREFER_RGB_565 doesn't work #4490

Open runInFuture opened 3 years ago

runInFuture commented 3 years ago

glide version: 4.11.0 integration library: none model: ANA-AN00 os version: EMUI 11.0.0 base on Android 10

Issue details:

When set DecodeFormat to PREFER_RGB_565,glide apply Config.ARGB_8888 to ANY decoded bitmap,even though when a bitmap doesn't has alpha channel. According to the document:

Bitmaps decoded from image formats that support and/or use alpha (some types of PNGs, GIFs etc) should return {@link android.graphics.Bitmap.Config#ARGB_8888} for {@link android.graphics.Bitmap#getConfig()}. Bitmaps decoded from formats that don't support or use alpha should return {@link android.graphics.Bitmap.Config#RGB_565} for {@link android.graphics.Bitmap#getConfig()}.

it seems something wrong happed.

Or put another way,how can i make sure glide apply RGB_565 config when decoded a bitmap which has no alpha channel?

The reason why I want to use RGB_565 is for memory consider. RGB_565 use less memory compared to ARGB_8888.

Code

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView imageView = findViewById(R.id.image);
        loadImage(imageView, "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3363295869,2467511306&fm=26&gp=0.jpg");
    }

    private void loadImage(ImageView imageView, String url) {
        Glide.with(imageView)
                .load(url)
                .transform(UnitTransformation.get()) // for skip centerCrop transformation
                .set(Downsampler.DECODE_FORMAT, DecodeFormat.PREFER_RGB_565)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
                        Log.d("diagnosis", String.format("config: %s hasAlpha: %b bytePerPixel: %d",
                                bitmap.getConfig().name(),
                                bitmap.hasAlpha(),
                                bitmap.getAllocationByteCount() / bitmap.getHeight() / bitmap.getWidth()));
                        return false;
                    }
                })
                .into(imageView);
    }
}

Layout XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:scaleType="centerCrop"
        android:layout_width="300dp"
        android:layout_height="300dp" />
</FrameLayout>

With code list above,issue can be reproduce.

Stack trace / LogCat

LruBitmapPool: Missing bitmap=[4320000](ARGB_8888)
LruBitmapPool: Get bitmap=[4320000](ARGB_8888)
LruBitmapPool: Hits=0, misses=1, puts=0, evictions=0, currentSize=0, maxSize=9551520
    Strategy=SizeConfigStrategy{groupedMap=GroupedLinkedMap( {[4320000](ARGB_8888):0} ), sortedSizes=(null[{}], ARGB_8888[{}], RGBA_F16[{}])}
diagnosis: config: RGB_565 hasAlpha: false bytePerPixel: 4

As log message show,the final bitmap display by imageView's config is has no alpha channel, config is RGB_565. RGB_565 should 2 byte per pixel,but this bitmap is as big as ARGB_8888 actually.

Analysis

And then i dig in glide's source code,find some point:

There is my questions

runInFuture commented 3 years ago

@sjudd any reply is appreciated

kyze8439690 commented 2 years ago

I meet this issue too. I guess maybe it is caused by bitmap reuse issue.

AlexanderGH commented 2 years ago

Just wanted to add my +1 that this issue exists and makes it hard to optimize an app for low-memory scenarios, given that bitmaps are commonly one of the largest, if not the largest source of memory usage in apps. @sjudd

mwshubham commented 1 year ago

+1

Highly appreciate if someone can refer to the duplicate links or reference related to this problem

ErgooLee commented 9 months ago

+1