Skip to content

SvgImageSource: SetSourceAsync failure and the Opened/ImageOpened events differ from WinUI #25557

Done

Description

@mikernet

Current behavior 🐛

On the Skia renderer, SvgImageSource differs from WinUI in three ways (seen on master with both the Svg.Skia add-in and the managed SVG engine, and on 6.8):

  1. When the stream isn't SVG (for example a PNG), await SetSourceAsync(stream) returns SvgImageSourceLoadStatus.InvalidFormat. WinUI throws ArgumentException (E_INVALIDARG). Both raise OpenFailed with InvalidFormat.
  2. Opened is raised for every successful load of the same source: each SetSourceAsync, each new UriSource. WinUI raises it only for the first successful load of a given SvgImageSource.
  3. Image.ImageOpened and ImageBrush.ImageOpened are raised for every load of an SVG source they show. WinUI raises them together with the source's Opened, so only for its first successful load.

Expected behavior 🎯

Same as WinUI:

  1. A failed load still raises OpenFailed with the status, and the operation fails, so awaiting it throws ArgumentException for content that isn't SVG. WinUI completes the operation with the failure's HRESULT (SvgImageSourceSetSourceAsyncOperation::CoreSetError, CImageSource::OnImageViewUpdated).
  2. Opened is raised once per source. CSvgImageSource::OnDownloadImageAvailableImpl fires it only while m_frameIndex is 0, and for an SVG nothing resets that index (CBitmapImage::PrepareDecode resets it only for bitmaps). WinUI's own SvgImageSourceTests uses a new instance when it expects Opened a second time.
  3. CImageSource::FireImageOpened notifies the images and brushes showing the source at the same time as it raises Opened, so their ImageOpened follows the same rule.

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

var source = new SvgImageSource();
var opened = 0;
var imageOpened = 0;
source.Opened += (_, _) => opened++;
MyImage.ImageOpened += (_, _) => imageOpened++;
MyImage.Source = source;

await source.SetSourceAsync(svgStream1);
await source.SetSourceAsync(svgStream2);
// Uno: opened == 2, imageOpened == 2. WinUI: 1 and 1.

await source.SetSourceAsync(pngStream);
// Uno: returns InvalidFormat. WinUI: throws ArgumentException (0x80070057).

Measured on WinAppSDK with runtime tests that make the same calls with file streams and ms-appdata URIs.

Workaround 🛠️

Handle both outcomes: check the returned status and catch ArgumentException. Use OpenFailed for the failure status, and don't rely on Opened/ImageOpened after the first load (or create a new SvgImageSource for each load).

Affected platforms 📱💻🖥️

WebAssembly, Android, iOS, Desktop (Windows), Desktop (macOS), Desktop (X11) (Skia renderer)

Uno.Sdk version (and other relevant versions) 📦

Master (7.0 dev) and 6.8.

IDE version 🧑‍💻

Not IDE related

Anything else we need to know? 💬

Found while fixing #25554 (stream SVG not drawn on 6.x).

One related difference remains. WinUI decodes a UriSource SVG only once an element shows it, while Uno opens it as soon as UriSource is set. So on WinUI an Image given a source that already opened from a stream doesn't get ImageOpened, but it does for a URI source. Uno can't tell those two cases apart without changing when URI sources load.

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