Repository navigation
Description
Current behavior
On WebAssembly, an image opened without DecodePixelWidth/DecodePixelHeight is decoded by the browser: ImageSourceHelpers.ReadFromStreamAsCompositionSurface hands the encoded bytes to a worker (ImageLoader.ts), which runs createImageBitmap, draws the bitmap to an OffscreenCanvas, reads it back with getImageData (RGBA) and premultiplies the alpha in a loop over every pixel (Skia's Unpremul does not work on WASM, #20727), then transfers the buffer back. What follows runs on the UI thread:
GetPropertyAsByteArray("bytes")copies all width × height × 4 bytes into a new managedbyte[]. For a 4000x2667 image that is a 40.7 MiB array in the large object space, which only a major collection frees, and WebAssembly's linear memory never shrinks, so each of these arrays the collector has not reached yet can extend the heap for the rest of the session.- A C# loop swaps R and B in that array, because the image surface takes BGRA: 10.7 million iterations for one 4000x2667 image.
CompositionImageSurface.CopyPixels→IImageEncoderDecoder.CreateImage→SKImage.FromPixelCopycopies the array again, into native memory.
Measured on master (74df0fb): Release SamplesApp in Chrome (interpreter, no AOT), on a machine that was running other builds, so the times are noisy; a 4000x2667 JPEG (1.4 MB) decoded through ReadFromStreamAsCompositionSurface 20 times in a row, each surface disposed at once, then six more held at the same time; two runs, each in a fresh page:
- The UI thread spends 46–51 ms per decode (median) between the worker's response and the finished surface, and another 27–33 ms before it hands the next image to the worker, most likely a major collection: 16 to 19 of them ran during the 20 decodes.
- Each decode allocates 42.0 MiB of managed memory (the 40.7 MiB array and the 1.3 MiB of encoded bytes), and the managed heap peaked 123 MiB above where the burst started.
- The wasm memory went from 315–378 MiB to 544 MiB over the 20 decodes, although each surface was disposed before the next decode started, and to 653–749 MiB with the six held, where six decoded images need 244 MiB. It never comes back down.
#25124 (for #25114) only changes when decoded images are released, not how they are decoded.
Expected behavior
A browser decode should allocate nothing managed beyond the encoded bytes, and the UI thread should not make a pass over the pixels:
- the worker already loops over every pixel to premultiply it, and the same loop can swap R and B;
- the pixels can be copied once, from the worker's buffer straight into the native image's storage: a .NET
Span<byte>over that storage, marshalled to JavaScript as aMemoryView, whichsetfills in the same call.
How to reproduce it (as minimally and precisely as possible)
On the WebAssembly head, open a large image (for example a 4000x2667 JPEG) several times through BitmapImage.SetSourceAsync or UriSource with the bitmap cache off and no DecodePixelWidth/DecodePixelHeight, and watch GC.GetTotalAllocatedBytes(true) (width × height × 4 bytes per decode beyond the encoded bytes), GC.GetTotalMemory(false), the wasm memory size (Module.HEAPU8.length) and the time the UI thread spends between the worker's response and ImageOpened.
Workaround
DecodePixelWidth/DecodePixelHeight routes the decode through SKCodec, which decodes into native memory with no managed copy, but it caps the image's resolution.
Affected platforms
WebAssembly
Uno.Sdk version (and other relevant versions)
master as of 2026-10-16 (74df0fb). 6.8 (Uno.WinUI 6.8.0-dev.212) takes the same managed copy, but creates the image as Rgba8888, with no swap, and pins the array instead of copying it again.
IDE version
Not relevant.
Activity