Repository navigation
Conversation
The browser decode keeps only the first frame, so it was skipped for URLs ending in .gif or .webp. An animated image set from a stream, or served from a URL without an extension, lost its animation. Decide from the encoded bytes, which are already in memory: any GIF, and a WebP whose VP8X chunk has the animation flag, go to the codec. A still WebP now takes the browser decode even from a .webp URL. Fixes unoplatform#25543
There was a problem hiding this comment.
🟢 Approval recommended
The focused decoder-selection change has targeted regression coverage and no unresolved review findings.
No open issues 🎉
What changed in this PR
Fixes #25543 by selecting the WebAssembly image decoder from encoded content rather than URL extensions, allowing GIF and animated WebP content to use the codec regardless of source.
Changes:
- Replaces extension checks with GIF/WebP header detection.
- Preserves browser decoding for still WebP images.
- Adds regression tests for streams, extensionless URIs, and header detection.
| File | Description |
|---|---|
| src/Uno.UI/UI/Xaml/Media/Imaging/ImageSourceHelpers.cs | Selects the decoder using image content. |
| src/Uno.UI.RuntimeTests/Microsoft/UI/Xaml/Controls/Given_Image.cs | Adds animation and content-detection regression tests. |
Review effort: Balanced
Port of unoplatform#25544 (d3ab4a0). The browser decode keeps only the first frame, so it was skipped for URLs ending in .gif or .webp. An animated image set from a stream, or served from a URL without an extension, lost its animation. - MayBeAnimated decides from the encoded bytes, which are already in memory: any GIF (GIF87a or GIF89a), and a WebP whose VP8X chunk has the animation flag, go to SKCodec. A still WebP now takes the browser decode even from a .webp URL. - It replaces IsAnimatableImageFormat(uri) and the attemptLoadingWithBrowserCanvasApi parameter of ReadFromStreamAsCompositionSurface. - Tests: When_AnimatedGif_From_Stream_Changes_Frames, When_AnimatedWebP_From_Stream_Changes_Frames, When_AnimatedGif_From_Uri_Without_Extension_Changes_Frames and When_Encoded_Image_May_Be_Animated. 6.8 differences: - Callers: the stream load in BitmapImage.skia.cs passes the target size by name, so it binds the same without the flag. The URI load in GetImageDataFromUriAsCompositionSurface was the only one that passed the flag (positionally), and now passes the size alone. - The frame tests read SkiaCompositionSurface.Image (6.8 has no CompositionImageSurface); each animated frame is its own SKImage there too. The tests live in Tests/Windows_UI_Xaml_Controls, and the assets were already there.
Port of unoplatform#25559 (e61fa84, 98c5b27, e1e1955, 2d0e812, 6f7e0bb and d89edf0), the follow-up to unoplatform#25128, on top of the unoplatform#25124 port. With the bitmap cache on, the decode shared by the BitmapImages that load the same uri and decode size was owned by an LRU of MaxBitmapImageCacheCount (100) entries, released by no source. - SharedImageDecode counts a shared decode's uses (the opens waiting for it and the sources whose subscribers draw it or were given it). Once it has none, it waits on PendingImageReleases like an owned decode (1 s and MaxUndisplayedImageBytes, oldest first), then frees its frames and leaves the cache. Sources are held weakly, and a collected source's uses are ended on the UI thread by a finalizable object only it holds. A subscriber back with a shared decode its source replaced uses it again until it draws the replacement. - EncodedImageCache keeps the encoded bytes of images downloaded over http or https, bounded by the new MaxBitmapImageCacheBytes (64 MB on desktop, 32 MB on Android, iOS, tvOS and WebAssembly), least recently used first, and drops bytes unused for 5 minutes. Only successful, unredirected responses without no-store, no-cache or max-age=0 are kept. Turning EnableBitmapImageCache off drops them, checked under the cache's lock. AppDataUriEvaluator.DownloadAsync reports whether a response may be reused. - MaxBitmapImageCacheCount is obsolete and has no effect. - feature-flags.md describes the bitmap cache settings. - Tests: Given_Image_Release (76 cases), GatedImageServer's extra header, status and redirect, and on WebAssembly a stand-in for fetch. 6.8 differences: - As in the unoplatform#25124 port: SkiaCompositionSurface instead of CompositionImageSurface (a shared decode counts its frames only), the tests' TrackingDecoder over FrameProviderFactory, and SurfaceOf in ImageSource.crossruntime.cs. - SharedImageDecode is crossruntime, like ImageSource and PendingImageReleases, so its surface access, byte count and release are Skia only. ImageData.Share is crossruntime too; Android and iOS never shared data. - The decode split (ReadAllAsync, DecodeAsCompositionSurface) keeps the ImageSourceHelpers of the unoplatform#25542 and unoplatform#25544 ports: the browser decode into native memory, and MayBeAnimated from the bytes in place of upstream's attemptLoadingWithBrowserCanvasApi. A cached buffer is decoded through a read-only stream. - Uno.UWP keeps LRUCache, which TextBlock (Android), UnicodeText (Skia) and Shape (native WebAssembly) still use on 6.8.
|
/azp run |
|
Tick the box to add this pull request to the merge queue (same as
|
MartinZikmund
left a comment
There was a problem hiding this comment.
@mikernet thanks for this one. Sniffing the bytes instead of the URL is the right call, and it lines up with WinUI, which decides from the decoded frame count (dxaml/xcp/components/imaging/WicService.cpp reads GetFrameCount, ImageMetadata::IsAnimatedImage is frameCount > 1) and animates GIFs set through SetSource/SetSourceAsync too (AnimatedImageTests::AnimatedSetSourceAsyncPreLiveTree). I checked the VP8X offsets and the 0x02 flag against the WebP container spec and they're right. Still GIFs end up in SingleFrameProvider, so they don't get a timer. The tests are good, and your runs with the extension check put back show the frame tests catch the bug.
One thing I'd like changed before merging:
- Managed decoder regression: you mention this yourself in the description. With the managed decoder registered, an animated WebP from a stream or an extensionless URL used to show its first frame through the browser decode. Now it raises
ImageFailed. Since the content check is only a hint, could you fall back to the browser decode when the codec declines a buffer thatMayBeAnimatedrouted away from it (browser, no target size)? Then the worst case is "first frame only", like before, and never a failed image. For.webpURLs that would also be an improvement over master. See the inline comment.
FYI, no action needed here:
- This conflicts textually with your own #25559. That PR still calls
IsAnimatableImageFormat(uri)and passesattemptLoadingWithBrowserCanvasApithrough the newDecodeAsCompositionSurface, so whichever PR lands second needs to drop that parameter there. #25542 merges cleanly with this one. - Per-frame cost and memory don't change in kind.
AnimatedImageFrameProvideralready decodes every frame at full size up front and keeps its timer running (plus a High-priority dispatcher post per frame) whether or not the image is on screen. This PR just makes more images take that path, the same as desktop already does. WinUI suspends animated images that weren't rendered in a frame (CCoreServicesm_animatedImages->CImageSource::SuspendAnimation), andAutoPlay/Play/Stop/IsPlaying/IsAnimatedBitmapare stillNotImplementedin Uno. That's a separate gap, not something to fix in this PR.
The content check sends any GIF and any animated WebP to the codec, which decodes every frame. A codec that can't decode the file, such as the managed decoder with an animated WebP, then failed the image, where the browser decode used to show its first frame. The browser now gets that buffer when the codec declines it.
|
Done in 7fcf59e. In the browser with no decode size, a buffer that
I also removed "Falling back to SKCodec" from the browser-failure log message. For a buffer that may be animated, the codec has already declined it by the time the browser fails, so there is nothing left to fall back to. |
GitHub Issue: closes #25543
PR Type:
🐞 Bugfix
What changed? 🚀
On WebAssembly,
ImageSourceHelpers.ReadFromStreamAsCompositionSurfacedecodes with the browser Canvas API, which returns only the first frame. It skipped that decode only when the URL ended in.gifor.webp(or a decode size was set). So an animated GIF or WebP set withSetSource/SetSourceAsync, or served from a URL without an extension (/api/avatar/42), showed only its first frame. Desktop animates all of them, because it always uses the codec.The function already holds the whole encoded file before it chooses a decoder, so it now looks at the content instead of the URL:
GIF87a/GIF89a): always the codec, since telling a still GIF from an animated one means walking all its blocks. A still GIF decodes to one frame there, as a.gifURL already did.VP8Xwith the animation flag (0x02) set. A still WebP (VP8,VP8L, orVP8Xwithout the flag) now uses the browser decode even from a.webpURL, which had gone to the codec only because of its extension.SkPngCodec(libpng) but notSkPngRustCodec, the Skia codec that reads APNG, so the codec wouldn't animate it either.MayBeAnimated(ReadOnlySpan<byte>)replacesIsAnimatableImageFormat(Uri). TheattemptLoadingWithBrowserCanvasApiparameter ofReadFromStreamAsCompositionSurfaceis gone, since the extension check was its only user.The content check is only a hint. When the codec declines a buffer it sent there, the browser decode still gets it and shows the first frame, as before. That is the case with the managed (SkiaSharp-free) image decoder, which declines animated WebP. An animated
.webpURL, which failed with that decoder on master, now shows its first frame too.Tests
All in
Given_Image, on Skia:When_AnimatedGif_From_Stream_Changes_Frames,When_AnimatedWebP_From_Stream_Changes_Frames:SetSourceAsyncwith the SamplesApp'sanimated.gif/animated.webp. The surface's current frame must change within 5 s.When_AnimatedGif_From_Uri_Without_Extension_Changes_Frames: the same GIF, copied toLocalFolderunder a name with no extension and loaded fromms-appdata:///Local/.... It goes through the same URI path as an extensionlesshttpURL.When_Codec_Declines_Animated_Image_Then_Browser_Shows_It(WebAssembly): with a codec that declines every buffer, an animated GIF and an animated WebP set from a stream open, and the browser decode gives the first frame at the file's size.When_Encoded_Image_May_Be_Animated: the content check on still and animated GIFs, still and animated WebP (VP8,VP8L,VP8Xwith and without the flag, a truncated header) and PNG. A test can't see which decoder an image went through without a new seam, so the still cases are checked here.Results, Release builds:
When_Codec_Declines_Animated_Image_Then_Browser_Shows_It.Given_Image,Given_ImageSource,Given_BitmapImageandGiven_BitmapSource: 66 passed, 1 inconclusive (When_Transitive_Asset_With_Link_Loaded, by design), the desktop-only tests skipped. With the fallback taken out, both rows of the new test fail: the image raisesImageFailed("Failed to decode image").Given_Image,Given_ImageSource,Given_BitmapImageandGiven_BitmapSourceall pass: 54 passed, no failures.When_Transitive_Asset_With_Link_Loadedis inconclusive on the browser by design, and the desktop-only tests are skipped. With the extension check put back, the three new frame tests fail (the frame never changes), while the existing.webpURL testWhen_AnimatedWebP_Changes_Framesstill passes.When_Encoded_Image_May_Be_Animatedfails on the still WebP.PR Checklist ✅
Screenshots Compare Test Runresults.