Repository navigation
fix(imaging): Don't throw from SetSource on an undecodable stream (6.8) - #25550
MartinZikmund merged 2 commits into
Conversation
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.
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.
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.
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).
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. |
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
|
The build 244751 did not find any UI Test snapshots differences. |
|
This pull request is merged. |
MartinZikmund
left a comment
There was a problem hiding this comment.
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.
GitHub Issue: closes #25549
PR Type:
🐞 Bugfix
What changed? 🚀
Targets
servicing/6.8. master is not affected: itsBitmapSourceandWriteableBitmapdecode throughImageEncoderDecoder.Current.TryDecodeand skip the size and the pixels when it fails, which is the behavior this PR gives 6.8.On Skia,
BitmapSource.SetSource/SetSourceAsyncthrew aNullReferenceExceptionfor a stream SkiaSharp can't decode (HEIC, AVIF, PDF, corrupt bytes), because the size read up front usedSKCodec.Create(stream).InfoandSKCodec.Createreturns null. Now:BitmapSource.skia.csleavesPixelWidth/PixelHeightalone whenSKCodec.Createreturns null. The load then fails through the normal path, which raisesImageFailedand 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, whosecreateImageBitmapdecodes HEIC (not tested in Safari).WriteableBitmap.skia.cskeeps its pixel buffer whenSKImage.FromEncodedDatareturns null, instead of throwing the same way.As on master,
SetSourceAsynccompletes after raisingImageFailed. WinUI raisesImageFailedtoo, but itsSetSourceAsyncaction 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.csandWriteableBitmap.Android.cshave the same unchecked decode results; native renderers aren't changed here.Tests
Given_BitmapSource.When_SetSource_Undecodable_Stream: random bytes and a PDF, throughSetSourceandSetSourceAsync. Neither throws, the size stays 0,BitmapImage.ImageFailedis raised (beforeSetSourceAsynccompletes), and so isImage.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
BitmapSourcefix). Runs ofGiven_BitmapSource,Given_WriteableBitmap,Given_RenderTargetBitmap,Given_Image,Given_ImageSource,Given_ImageBrushandGiven_BitmapIcon:The failure,
When_WriteableBitmap_Assigned_With_Data_Present, decodes no stream and fails the same way onservicing/6.8without this change on this machine (125% scaling).PR Checklist ✅
Screenshots Compare Test Runresults.