Skip to content

fix(imaging): Don't throw from SetSource on an undecodable stream (6.8) - #25550

Merged
MartinZikmund merged 2 commits into
unoplatform:servicing/6.8from
mikernet:dev/mikernet/68-setsource-undecodable
Nov 27, 2026
Merged

MartinZikmund merged 2 commits into
unoplatform:servicing/6.8from
mikernet:dev/mikernet/68-setsource-undecodable

Conversation

@mikernet

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #25549

PR Type:

🐞 Bugfix

What changed? 🚀

Targets servicing/6.8. master is not affected: its BitmapSource and WriteableBitmap decode through ImageEncoderDecoder.Current.TryDecode and skip the size and the pixels when it fails, which is the behavior this PR gives 6.8.

On Skia, BitmapSource.SetSource/SetSourceAsync threw a NullReferenceException for a stream SkiaSharp can't decode (HEIC, AVIF, PDF, corrupt bytes), because the size read up front used SKCodec.Create(stream).Info and SKCodec.Create returns null. Now:

  • BitmapSource.skia.cs leaves PixelWidth/PixelHeight alone when SKCodec.Create returns null. The load then fails through the normal path, which raises ImageFailed and sets the size to 0. On WebAssembly the browser decode, which the stream load tries first, now gets its chance: an AVIF stream loads in Chrome, and a HEIC stream should load in Safari, whose createImageBitmap decodes HEIC (not tested in Safari).
  • WriteableBitmap.skia.cs keeps its pixel buffer when SKImage.FromEncodedData returns null, instead of throwing the same way.

As on master, SetSourceAsync completes after raising ImageFailed. WinUI raises ImageFailed too, but its SetSourceAsync action then completes with the decode error; that difference is left as on master.

The other Skia decode calls (FrameProviderFactory, SkiaCompositionSurface.LoadFromStream, LoadedImageSurface, AcrylicBrush, the Win32 and X11 clipboard and window icon) already handle a null result.

Out of scope: the native renderers' BitmapSource.Apple.cs and WriteableBitmap.Android.cs have the same unchecked decode results; native renderers aren't changed here.

Tests

  • Given_BitmapSource.When_SetSource_Undecodable_Stream: random bytes and a PDF, through SetSource and SetSourceAsync. Neither throws, the size stays 0, BitmapImage.ImageFailed is raised (before SetSourceAsync completes), and so is Image.ImageFailed.
  • Given_BitmapSource.When_SetSource_Undecodable_Then_Decodable_Stream: a PNG set after random bytes gets its size and opens.
  • Given_BitmapSource.When_SetSource_Stream_Only_Browser_Decodes (WebAssembly): an AVIF stream, which Skia can't decode, opens through the browser with the right size.
  • Given_WriteableBitmap.When_WriteableBitmap_SetSource_Undecodable_Stream: random bytes don't throw and keep the size; a PNG then decodes.

All of them fail without the fix (the WriteableBitmap test also with only the BitmapSource fix). Runs of Given_BitmapSource, Given_WriteableBitmap, Given_RenderTargetBitmap, Given_Image, Given_ImageSource, Given_ImageBrush and Given_BitmapIcon:

  • Skia desktop (Windows): 78 passed, 6 skipped, 1 inconclusive, 1 failed.
  • WebAssembly (Chrome): 80 passed, 4 skipped, 2 inconclusive, 1 failed.

The failure, When_WriteableBitmap_Assigned_With_Data_Present, decodes no stream and fails the same way on servicing/6.8 without this change on this machine (125% scaling).

PR Checklist ✅

On Skia, BitmapSource.SetSource and SetSourceAsync read the size up
front with SKCodec.Create(stream).Info. SKCodec.Create returns null for
data Skia can't decode (HEIC, AVIF, PDF, corrupt bytes), so both threw
a NullReferenceException and ImageFailed was never raised. On
WebAssembly this also kept the browser, which the stream load tries
first, from decoding formats such as AVIF in Chrome or HEIC in Safari.

Leave the size alone when there is no codec, as master does: the load
then fails through ImageFailed, with the size at 0.
WriteableBitmap.SetSource decodes the stream into its pixel buffer with
SKImage.FromEncodedData, which returns null for data Skia can't decode,
and then threw a NullReferenceException. Keep the current buffer and
size instead, as master does.
@github-actions github-actions Bot added the area/skia ✏️ Categorizes an issue or PR as relevant to Skia label Oct 16, 2026
mikernet added a commit to Singulink/uno that referenced this pull request Oct 17, 2026
Port of unoplatform#25550 (6f9e5d3).

On Skia, BitmapSource.SetSource and SetSourceAsync read the size up
front with SKCodec.Create(stream).Info. SKCodec.Create returns null for
data Skia can't decode (HEIC, AVIF, PDF, corrupt bytes), so both threw
a NullReferenceException and ImageFailed was never raised. On
WebAssembly this also kept the browser, which the stream load tries
first, from decoding formats such as AVIF in Chrome or HEIC in Safari.
- The size is left alone when there is no codec, as master does: the
  load then fails through ImageFailed, with the size at 0.
- Tests: When_SetSource_Undecodable_Stream,
  When_SetSource_Undecodable_Then_Decodable_Stream and
  When_SetSource_Stream_Only_Browser_Decodes (WebAssembly).

6.8 differences:
- None: unoplatform#25550 targets servicing/6.8 and cherry-picks clean.
mikernet added a commit to Singulink/uno that referenced this pull request Oct 17, 2026
Port of unoplatform#25550 (45999b8).

WriteableBitmap.SetSource decodes the stream into its pixel buffer with
SKImage.FromEncodedData, which returns null for data Skia can't decode,
and then threw a NullReferenceException.
- The current buffer and size are kept instead, as master does.
- Test: When_WriteableBitmap_SetSource_Undecodable_Stream.

6.8 differences:
- None: unoplatform#25550 targets servicing/6.8 and cherry-picks clean.
mikernet added a commit to Singulink/uno that referenced this pull request Oct 17, 2026
Port of unoplatform#25124 at its final state (c910af6: 0bcb64b,
6208008, c76b176, f1ddb6d, 5cece07, cd79eb1, 16de186,
0d7f73a and c910af6). It replaces the early port (c13eba7,
f443e7c, d648708 and 765c7eb), which released a source's pixels
as soon as its last subscriber left.
- PendingImageReleases keeps what nothing displays for 1 s, as WinUI
  does (sc_imageSurfaceWrapperReleaseDelayMilliseconds), and releases it
  at the end of a layout pass, so an element re-parented or recycled
  meanwhile keeps its pixels. The waiting images are bounded, oldest
  first, by the new FeatureConfiguration.Image.MaxUndisplayedImageBytes:
  128 MB on desktop, 64 MB on Android and iOS, 32 MB on WebAssembly
  (documented in feature-flags.md).
- ImageSource tracks what each subscription draws (Shown) apart from
  what it was given (Pending), with a Held record per surface: replaced
  pixels stay until every subscriber draws the replacement, and an Image
  back in the tree keeps what it left with (KeepReplaced). A failed
  source is not retried for an element that attaches to it, as on WinUI.
- Image subscribes only while loaded, keeps its sprite while unloaded,
  reports what it draws once measured, raises ImageOpened once per
  source and shows nothing when its source fails. ImageBrush keeps one
  subscription across UriSource changes and lets go of its surface when
  given another ImageSource. Both keep only the last subscription when a
  handler changes the source while they subscribe.
- A superseded open releases what it decoded. BitmapImage checks after
  each size setter, so a superseded open can't overwrite the
  replacement's size or raise its events, and raises ImageOpened once
  per source. RenderTargetBitmap's open no longer runs on the open's
  token. An open is marked in flight before it runs app code.
- Tests: Given_Image_Release (45 tests, 47 cases), the GatedImageServer
  helper shared with Given_BitmapImage, whose own copy is gone, as is
  its abandoned-surface test, now in Given_Image_Release.

6.8 differences:
- 6.8 has no codec layer (IImageEncoderDecoder) and no
  CompositionImageSurface. As in the early port, the surface is
  SkiaCompositionSurface, over SKImage frame providers. Its
  DecodedByteCount adds up its frames (IFrameProvider.ByteCount, the
  size the providers already report as memory pressure). 6.8 holds no
  texture of its own (Skia's GPU cache does), so an image counts its
  frames only; the doc and the XML comment say so.
- The tests' TrackingDecoder records the images FrameProviderFactory
  hands to a provider (a test hook, ImageCreatedForTesting), and a
  disposed SKImage counts as released. It stands in for upstream's codec
  wrapper, and replaces the early port's counters
  (ImageData.CompositionSurfacesCreatedForTesting and
  ImageSource.ReleasedSurfacesForTesting).
- 6.8 lays out only while its first root has bounds, so
  CoreServices.HasRootWithBounds checks that root, as
  RequestAdditionalFrame already did. The release check runs after
  6.8's loop that lays out, raises Loaded and renders each window.
- ImageSource.crossruntime.cs and PendingImageReleases.crossruntime.cs
  also compile for native WebAssembly and the reference API. There the
  surface (SurfaceOf), its byte count and its release are Skia only, and
  the reference API has no frame loop to request.
- An Image draws an SvgImageSource through a child canvas, not through a
  subscription. It never subscribes to one, and leaves that canvas alone
  when unloaded, as before the early port. An ImageBrush draws an SVG
  through a raster SkiaCompositionSurface, released like a bitmap once
  replaced.
- CompositionSurfaceBrush.Paint read scs.Image! and now draws nothing for
  a released surface, as master's does, since an unloaded Image keeps
  its sprite brush.
- BitmapImage opens in BitmapImage.skia.cs, which also has the
  per-version RaiseImageOpened: Android, iOS and native WebAssembly
  still call RaiseImageOpened(). ImageBrush keeps its UriSource hook for
  the native Android and iOS brushes, which load the image themselves.
- SetSourceAsync reads the pixel size from the codec without decoding,
  so When_Stream_Load_Superseded_After_Decode waits for one decode, not
  two.
- An Image attached after SetSourceAsync failed now raises nothing, so
  the async rows of When_SetSource_Undecodable_Stream (unoplatform#25550) check
  that instead of waiting for ImageFailed. WinUI's CImageSource only
  queues the callback of an element that attaches later (AddCallback).
@agneszitte

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 244751 did not find any UI Test snapshots differences.

@mergify

mergify Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@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.

Thanks @mikernet, nice and tight backport. Skipping the size when Skia can't decode matches what master does through ImageEncoderDecoder.TryDecode, and it's in line with WinUI, where SetSource never decodes synchronously and a bad stream surfaces as ImageFailed instead of an exception. Good call on the AVIF test proving the browser decoder now gets its turn on WASM.

The red CI jobs aren't from this change: the iOS shards died on simulator boot timeouts and Android shard 4 hit the 45-minute job limit with no test failures reported. They just need a re-run.

@MartinZikmund
MartinZikmund merged commit e8c1ece into unoplatform:servicing/6.8 Nov 27, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/skia ✏️ Categorizes an issue or PR as relevant to Skia

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants