Skip to content

[Skia] BitmapSource.SetSource decodes the whole image on the UI thread to read its size #25556

Open

Description

@mikernet

Current behavior 🐛

BitmapSource.SetSource and SetSourceAsync set PixelWidth and PixelHeight synchronously, on the UI thread. On master they get them from IImageEncoderDecoder.TryDecode(stream, null, null, ...): a full decode of the image, every frame of an animated GIF or WebP, whose pixels are thrown away once the first frame's size is read. The image is decoded again when it opens.

  • For a 4000x2667 JPEG (2.4 MB) that is a 43 MB decode on every SetSource call, a median of 76 ms on the UI thread on desktop, against under 1 ms to read the header. Re-running master's SetSource steps (the two stream clones and the decode) on WebAssembly (Chrome, Release) takes 68 to 76 ms.
  • From reading the code, each of those decodes holds two 43 MB native buffers at once: the decode bitmap, and the copy SKImage.FromBitmap makes of it. On WebAssembly that memory comes out of the linear memory, which never shrinks.
  • WriteableBitmap.SetSource decodes the stream twice: once for the size, then again into its pixel buffer.

This is a regression from 6.8, which read only the header (SKCodec.Create(stream).Info in BitmapSource.skia.cs). Master's backend-neutral codec seam, IImageEncoderDecoder, has no way to read an image's size without decoding it, so the size read moved to TryDecode.

Not regressions, and in 6.8 too:

  • SetSource doesn't reload the source. An Image already showing the bitmap keeps the previous picture, or nothing if the bitmap had no stream yet, and an Image that shows it later gets the image opened from the previous stream.
  • When nothing else is subscribed to the source, SetSourceAsync opens it twice. On desktop a BitmapImage can then decode twice, since the first open is only cancelled if it hasn't started yet, and a WriteableBitmap copies its pixels into an image twice. When the source already has an image open, SetSourceAsync completes with that image, before the new one opens.

Expected behavior 🎯

SetSource reads the size from the image's header, as 6.8 did, and WriteableBitmap.SetSource decodes the stream once. Setting a stream replaces the image wherever the bitmap is shown, as WinUI's CImageSource::SetSource does, and SetSourceAsync opens the new source once and completes when it has opened.

How to reproduce it (as minimally and precisely as possible) 🔬

var bytes = File.ReadAllBytes("photo.jpg"); // e.g. 4000x2667
var stopwatch = Stopwatch.StartNew();
new BitmapImage().SetSource(new MemoryStream(bytes).AsRandomAccessStream());
Debug.WriteLine(stopwatch.ElapsedMilliseconds); // the time of a full decode

For the reload: show a BitmapImage in an Image, then call SetSource on it; the Image doesn't change.

Workaround 🛠️

Loading the image through UriSource instead of SetSource: its size is set when it opens, not up front. For the reload, calling SetSourceAsync, which does reload.

Affected platforms 📱💻🖥️

WebAssembly, Android, iOS, tvOS, Desktop (Windows), Desktop (macOS), Desktop (X11), Desktop (Linux Framebuffer)

Uno.Sdk version (and other relevant versions) 📦

master as of 2026-10-16 (7872853). In 6.8, only the reload and SetSourceAsync parts.

IDE version 🧑‍💻

Not relevant.

Anything else we need to know? 💬

None.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions