Skip to content

[Skia] ImageBrush draws its previous ImageSource when that source opens again while the brush switches #26368

Open

Description

@mikernet

Current behavior 🐛

When an ImageBrush's ImageSource changes, the brush subscribes to the new source before it unsubscribes from the old one (ImageBrush.crossruntime.cs, OnSourceChangedPartial). A new source that already has its image (or a WriteableBitmap, which opens synchronously) delivers it while the brush subscribes, and the brush raises ImageOpened then. If that handler, or any other app code run meanwhile, makes the old source open again synchronously (for example oldWriteableBitmap.Invalidate()), the old source still notifies the brush: the brush takes the old image and draws it for the new source, until the new source opens again.

The same happens with a source that a handler replaced while the brush subscribed to it: when ImageOpened switches the brush to a third source and then makes the replaced one open again, the brush draws the replaced source, because its subscription is only disposed once the outer subscribe returns.

Expected behavior 🎯

Once its ImageSource changes, a brush draws only what that source delivers. WinUI removes the brush's callback from the old source before it sets the new one (CImageBrush::SetValue, RemoveCallback), and Uno's Image already releases its old subscription before subscribing to the new source.

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

var previous = new WriteableBitmap(4, 4);
var next = new WriteableBitmap(4, 4);
var brush = new ImageBrush { ImageSource = previous };
border.Background = brush; // a loaded Border

var reopened = false;
brush.ImageOpened += (_, _) =>
{
	if (brush.ImageSource == next && !reopened)
	{
		reopened = true;
		previous.Invalidate();
	}
};

brush.ImageSource = next;
// The brush now draws previous's pixels, while its ImageSource is next.

Workaround 🛠️

Don't make the old source open again from ImageOpened (or other code run while the brush subscribes); defer it, for example with DispatcherQueue.TryEnqueue.

Affected platforms 📱💻🖥️

WebAssembly, Android, iOS, tvOS, Desktop (Windows), Desktop (macOS), Desktop (X11), Desktop (Linux Framebuffer)

Uno.Sdk version (and other relevant versions) 📦

master (7.1 dev); every release since 5.0, where 0015c9b made the brush subscribe to the new source before disposing its old subscription (it disposed it first before that commit).

IDE version 🧑‍💻

N/A

Anything else we need to know? 💬

Found while working on #25633. I found no path through which the old subscription notifies the brush once it is disposed: a source's asynchronous open (including a completed bitmap-cache entry, whose delivery is queued on the dispatcher) skips disposed subscriptions.

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