SDWebImageSwiftUI icon indicating copy to clipboard operation
SDWebImageSwiftUI copied to clipboard

load animated webP format image very slow than gif when show 2 or more images in a view

Open piaozhx opened this issue 5 years ago • 4 comments

I found that if there are 2 or more webp images, they will animate very slow... I build a very simple project to help figure it out.

piaozhx avatar Sep 10 '20 11:09 piaozhx

WebP docoding use Software and libwebp (CPU) GIF decoding use Hardware and ImageIO framework (GPU)

dreampiggy avatar Sep 10 '20 12:09 dreampiggy

Trick: Don't share the same URL for same WebP. You can copy that WebP into a new URL (test2.webp), then showing one (test.webp) another (test2.webp).

Because We have logic: If we found when one SDAnimatedImage(UIImage) object, rendering in different SDAnimatedImageView(UIImageView), we will using Lock and many complicated logic to ensure the frames does not get collapse.

Learn what behind the scene here: https://github.com/SDWebImage/SDWebImageWebPCoder/blob/master/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m#L1065.

Explain

  • WebP each individual frames need previous render canvas state to show. Which means if we render different frame index (UIImageView1 showing frame1, UIImageView2 showing frame2), then we need to clear canvas and re-draw. Think like Video. What you do it's to play two videos with different playback time at the same time, high cost.
  • GIF each individual frames don't share any state. Which means if we render different frame index (UIImageView1 showing frame1, UIImageView2 showing frame2), they just decode and get. No re-draw.

Which means, in general, because of what Animated WebP render tech, it will have bad performance when rendering same Object into different Views

dreampiggy avatar Sep 10 '20 12:09 dreampiggy

Another trick, if you really want to reduce CPU usage, but not RAM usage. You can use SDWebImagePreloadAllFrames options.

dreampiggy avatar Sep 10 '20 12:09 dreampiggy

Maybe we need to transfer this issue to https://github.com/SDWebImage/SDWebImage

dreampiggy avatar Sep 10 '20 12:09 dreampiggy