Skip to content

[Skia] Every displayed image is held twice in memory: CreateTexture copies the decoded pixels #25547

Open

Description

@mikernet

Current behavior

On every Skia target, an image is drawn through a texture that CompositionImageSurface.GetTexture creates with IDrawingFactory.CreateTexture(IImage) and keeps for as long as the surface shows that frame. The Skia factory (SkiaDrawingFactory.CreateTexture) builds that texture by:

  1. allocating a managed byte[] of width × height × 4 bytes,
  2. filling it with IImage.CopyPixels, which for a SkiaImage is a ReadPixels out of its SKImage,
  3. copying the array into a new SKImage with SKImage.FromPixelCopy.

The frame provider keeps the decoded SkiaImage as well, so every displayed image is resident twice, and each texture creation also allocates a temporary array of the same size on the large object heap, which only a full collection frees. A 3000x3600 image (43 MB decoded) costs 86 MB while it is shown, plus a 43 MB temporary array. An animated image pays the copy and the temporary array on every frame change, because the texture is recreated for each frame. On WebAssembly, linear memory never shrinks, so these allocations raise the heap size for the rest of the session.

Measured with master's CreateTexture (52dccee): Release SamplesApp, four 3000x3600 PNGs (43.2 MB each once decoded), a fresh process or page per run, after a full collection:

  • Windows desktop: CreateTexture on the four decoded images adds 344.9 MB of private bytes, which is the four copies (173 MB) plus 169 MB of GC heap that the 172.8 MB of temporary arrays leave committed. Displaying the four images in Image elements (opened with BitmapImage.SetSourceAsync) adds 753.8 MB of private bytes and 175.9 MB of managed allocations.
  • WebAssembly (Chrome): CreateTexture on the four decoded images grows the wasm memory from 475 MB to 789 MB and allocates 172.8 MB of managed memory. Displaying the four images grows it from 330 MB to 790 MB.

Before the drawing-factory seam (6.7 and earlier), CompositionSurfaceBrush drew the decoded SKImage directly, so this second copy only exists on master.

Separately, SkiaImage.CopyPixels ignores the result of ReadPixels: a failed read leaves the destination untouched and the caller gets a transparent image without any error. It also writes the whole image through a raw pointer without checking the destination's length.

Expected behavior

On the Skia backend a texture is an immutable SKImage, and a SkiaImage already holds immutable raster pixels. A texture created from a SkiaImage should draw those pixels without copying them, holding a reference on the image so that the two can be disposed in either order. Then an image is held once, and creating its texture allocates nothing. The texture should still be an SKImage of its own over those pixels rather than the image's own SKImage: Skia keeps a raster image's GPU upload until that image is gone, so sharing it would keep every frame of an animation uploaded while it plays. Images from other decoders (the managed decoder's ManagedImage) still need the copy.

SkiaImage.CopyPixels should fail visibly when it cannot read the pixels, and reject a destination that is too short.

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

On a Skia head, display a few large images, for example four 3000x3600 PNGs through BitmapImage.SetSourceAsync in Image elements, and compare, before and after they are shown (after a full collection):

  • the process private bytes on desktop, or the wasm memory size (HEAPU8.length) on WebAssembly,
  • GC.GetTotalAllocatedBytes(true).

Besides the decoded image itself, each displayed image adds its decoded size to the managed allocations (the temporary array) and, on desktop, the copy plus the GC heap that array leaves committed to private bytes.

Workaround

DecodePixelWidth/DecodePixelHeight make the decoded image, and so both copies, smaller. Nothing avoids the second copy.

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 (52dccee). 6.7 and earlier draw the decoded image directly.

IDE version

Not relevant.

Anything else we need to know?

The WebGPU backend's texture has to be a GPU upload, so it doesn't hold a second CPU copy. It does allocate two temporary arrays of the image's size per upload (the BGRA readback and the RGBA swizzle).

Related: #25124, #25541.

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