Skip to content

fix(wasm): Animate GIF and WebP from any source - #25544

Open
mikernet wants to merge 2 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/wasm-animated-image-sniff
Open

mikernet wants to merge 2 commits into
unoplatform:masterfrom
mikernet:dev/mikernet/wasm-animated-image-sniff

Conversation

@mikernet

@mikernet mikernet commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #25543

PR Type:

🐞 Bugfix

What changed? 🚀

On WebAssembly, ImageSourceHelpers.ReadFromStreamAsCompositionSurface decodes with the browser Canvas API, which returns only the first frame. It skipped that decode only when the URL ended in .gif or .webp (or a decode size was set). So an animated GIF or WebP set with SetSource/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:

  • GIF (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 .gif URL already did.
  • WebP: the codec only when the first chunk is VP8X with the animation flag (0x02) set. A still WebP (VP8, VP8L, or VP8X without the flag) now uses the browser decode even from a .webp URL, which had gone to the codec only because of its extension.
  • APNG stays on the browser decode. The SkiaSharp 4.151.1 native libraries contain SkPngCodec (libpng) but not SkPngRustCodec, the Skia codec that reads APNG, so the codec wouldn't animate it either.

MayBeAnimated(ReadOnlySpan<byte>) replaces IsAnimatableImageFormat(Uri). The attemptLoadingWithBrowserCanvasApi parameter of ReadFromStreamAsCompositionSurface is 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 .webp URL, 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: SetSourceAsync with the SamplesApp's animated.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 to LocalFolder under a name with no extension and loaded from ms-appdata:///Local/.... It goes through the same URI path as an extensionless http URL.
  • 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, VP8X with 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:

  • After the browser fallback, Skia WebAssembly (Chrome, net11.0): the animated and content-check tests 8/8, including both rows of When_Codec_Declines_Animated_Image_Then_Browser_Shows_It. Given_Image, Given_ImageSource, Given_BitmapImage and Given_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 raises ImageFailed ("Failed to decode image").
  • Skia WebAssembly (Chrome): Given_Image, Given_ImageSource, Given_BitmapImage and Given_BitmapSource all pass: 54 passed, no failures. When_Transitive_Asset_With_Link_Loaded is 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 .webp URL test When_AnimatedWebP_Changes_Frames still passes.
  • Skia desktop (Win32): the same classes, 58 passed, no failures. Desktop always decodes with the codec, so the new frame tests pass there with or without the change. With the content check made to count any WebP as animated, When_Encoded_Image_May_Be_Animated fails on the still WebP.

PR Checklist ✅

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
Copilot AI balanced review requested due to automatic review settings October 16, 2026 21:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

mikernet added a commit to Singulink/uno that referenced this pull request Oct 16, 2026
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.
mikernet added a commit to Singulink/uno that referenced this pull request Oct 17, 2026
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.
@MartinZikmund

Copy link
Copy Markdown
Member

/azp run

@mergify

mergify Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

@MartinZikmund MartinZikmund left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 that MayBeAnimated routed away from it (browser, no target size)? Then the worst case is "first frame only", like before, and never a failed image. For .webp URLs 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 passes attemptLoadingWithBrowserCanvasApi through the new DecodeAsCompositionSurface, 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. AnimatedImageFrameProvider already 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 (CCoreServices m_animatedImages -> CImageSource::SuspendAnimation), and AutoPlay/Play/Stop/IsPlaying/IsAnimatedBitmap are still NotImplemented in 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.
@mikernet

Copy link
Copy Markdown
Contributor Author

Done in 7fcf59e. In the browser with no decode size, a buffer that MayBeAnimated matches now goes to the codec first. If the codec declines it, the existing browser decode gets it and shows the first frame, as before this PR. If the browser fails too, the codec's error is returned. Every other buffer takes the same path as before. I left the browser block where it was instead of moving it into a helper, which keeps #25542 merging cleanly with this PR. The conflict with #25559 is the same as before.

When_Codec_Declines_Animated_Image_Then_Browser_Shows_It (WebAssembly only) swaps in a decoder that declines every buffer and forwards the rest, as in Given_Image_Release. With it, an animated GIF and an animated WebP set from a stream open, at the files' sizes. With the fallback taken out, both rows fail with ImageFailed. On Skia WebAssembly, Given_Image, Given_ImageSource, Given_BitmapImage and Given_BitmapSource pass: 66 passed, 1 inconclusive (When_Transitive_Asset_With_Link_Loaded, by design).

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Animated GIF/WebP from a stream or extensionless URL shows only its first frame on WebAssembly

3 participants