Skip to content

Commit

Permalink
fix: metal rendering at 2x (retina)
Browse files Browse the repository at this point in the history
  • Loading branch information
spouliot committed Feb 11, 2024
1 parent e20670a commit c255973
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/Uno.UI.Runtime.Skia.MacOS/MacOSWindowHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public MacOSWindowHost(MacOSWindowNative nativeWindow, Window winUIWindow)

private void UpdateWindowSize(double nativeWidth, double nativeHeight)
{
var sizeAdjustment = _displayInformation.FractionalScaleAdjustment;
SizeChanged?.Invoke(this, new Windows.Foundation.Size(nativeWidth / sizeAdjustment, nativeHeight / sizeAdjustment));
_initializationNotCompleted = SizeChanged is null;
SizeChanged?.Invoke(this, new Size(nativeWidth, nativeHeight));
}

private void Draw(SKSurface surface)
Expand All @@ -96,19 +94,25 @@ private unsafe void MetalDraw(double nativeWidth, double nativeHeight, nint text
this.Log().Trace($"Window {_nativeWindow.Handle} drawing {nativeWidth}x{nativeHeight} texture: {texture} FullScreen: {NativeUno.uno_application_is_full_screen()}");
}

// FIXME: we get the first update for windows sizes before we have completed the initialization
var scale = (float)_displayInformation.RawPixelsPerViewPixel;

// FIXME: we get the first (native) updates for window sizes before we have completed the (managed) host initialization
if (_initializationNotCompleted)
{
UpdateWindowSize(nativeWidth, nativeHeight);
UpdateWindowSize(nativeWidth / scale, nativeHeight / scale);
_initializationNotCompleted = SizeChanged is null;
if (_initializationNotCompleted)
{
return; // not yet...
}
}

// we can't cache anything since the texture will be different on next calls
using var target = MacOSMetalRenderer.CreateTarget(_context!, nativeWidth, nativeHeight, texture);
using var surface = SKSurface.Create(_context, target, GRSurfaceOrigin.TopLeft, SKColorType.Bgra8888);

surface.Canvas.Scale(scale, scale);

Draw(surface);

_context?.Flush();
Expand All @@ -121,10 +125,11 @@ private unsafe void SoftDraw(double nativeWidth, double nativeHeight, nint* data
this.Log().Trace($"Window {_nativeWindow.Handle} drawing {nativeWidth}x{nativeHeight} FullScreen: {NativeUno.uno_application_is_full_screen()}");
}

// FIXME: we get the first update for windows sizes before we have completed the initialization
// FIXME: we get the first (native) updates for window sizes before we have completed the (managed) host initialization
if (_initializationNotCompleted)
{
UpdateWindowSize(nativeWidth, nativeHeight);
_initializationNotCompleted = SizeChanged is null;
if (_initializationNotCompleted)
{
return; // not yet...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ - (void)drawInMTKView:(nonnull MTKView *)view

- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size
{
uno_get_resize_callback()((__bridge void*) view.window, size.width, size.height);
CGFloat scale = view.window.backingScaleFactor;
#if DEBUG
NSLog (@"drawableSizeWillChange: %p %f x %f @ %gx", view.window, size.width, size.height, scale);
#endif
uno_get_resize_callback()((__bridge void*) view.window, size.width / scale, size.height / scale);
}

@end

0 comments on commit c255973

Please sign in to comment.