Git Product home page Git Product logo

Comments (6)

zjupure avatar zjupure commented on May 23, 2024

As I known, Glide library won' t cache gif frames result to memory. I think it is a balance of cpu and memory usage. It's hard to decide how many frames of one gif image should be cached. Some gif/webp might contains 100+ frames and many frame bitmaps in gif/webp image are similar with only minor difference. When an app need to display much animated image (gif/webp), caching the frame will cause the bitmap OOM easily. More complicated memory management are required and avoid flush static image caches meanwhile. So the gif/webp frames are always decoded and rendered in a realtime without cache.

You can test more gif/webp images in a screen to get more reliable performance data not just one specifical image.

from glidewebpdecoder.

AndroidDeveloperLB avatar AndroidDeveloperLB commented on May 23, 2024

It was just a guess, that others cache the frames.
I don't know about the library I've mentioned, but I think that ImageDecoder does something special. I can see in the code of AnimatedImageDrawable :

@Override
public void draw(@NonNull Canvas canvas) {
    if (mState.mNativePtr == 0) {
        throw new IllegalStateException("called draw on empty AnimatedImageDrawable");
    }

    if (mStarting) {
        mStarting = false;

        postOnAnimationStart();
    }

    long nextUpdate = nDraw(mState.mNativePtr, canvas.getNativeCanvasWrapper());
    // a value <= 0 indicates that the drawable is stopped or that renderThread
    // will manage the animation
    if (nextUpdate > 0) {
        if (mRunnable == null) {
            mRunnable = this::invalidateSelf;
        }
        scheduleSelf(mRunnable, nextUpdate + SystemClock.uptimeMillis());
    } else if (nextUpdate == FINISHED) {
        // This means the animation was drawn in software mode and ended.
        postOnAnimationEnd();
    }
}

It's written that renderThread will handle the animation, and indeed, after some frames, it doesn't call the runnable anymore, which means I can't use a similar solution to what I've written here: #22 .

If you know how to overcome this , please let me know. Here's a sample project:
ImageDecoderTest.zip

About caching, I know it can lead to OOM, but if it's dynamic up to some amount of memory to use, just like other images, it shouldn't reach OOM.
BTW, starting from Android O, it's a bit harder to reach OOM, as bitmaps seem to be stored on the normal memory of the device, and not the heap.

The solution of frame-by-frame animation might help with customized caching mechanism outside of the library, but I don't see if it's possible with ImageDecoder API.

from glidewebpdecoder.

zjupure avatar zjupure commented on May 23, 2024

AnimatedImageDrawable render code is in native and I have not investigated it. The memory cache mechaim for frame should be implemented by Glide library. If you have good idea or PR, you can submit feedback to Glide . Once Glide library support it, this library will follow the changes soon.

from glidewebpdecoder.

AndroidDeveloperLB avatar AndroidDeveloperLB commented on May 23, 2024

Since I don't have time to investigate how Glide works behind the scenes (let alone its caching mechanism), I only thought that since the caching doesn't exist in this case, and I have a solution for going frame-by-frame (you helped me a bit, here: #22 ) , I could cache some of the bitmaps by myself, outside of the Glide library. Not sure if it's possible. Just an idea.

Do you know perhaps how does the other library do it (from here: https://github.com/huzongyao/AndLibNsgif ) ? Sadly it's not available for animated WEBP, but on animated GIF it uses a bit less CPU than here (though uses a bit more RAM).

from glidewebpdecoder.

zjupure avatar zjupure commented on May 23, 2024

I do not known AndLibNsgif library as you mentioned. Glide library not support frame-by-frame cache and there is no easy way to extend the Glide interface or function.

from glidewebpdecoder.

AndroidDeveloperLB avatar AndroidDeveloperLB commented on May 23, 2024

Too bad. Thank you for your help and kindness !

from glidewebpdecoder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.