From fbc48512ef2c844c35a6511acfaa386b6dda8e42 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Tue, 13 Dec 2022 14:53:40 +0100 Subject: [PATCH 1/2] Modify the fruit example to show any logical vs. physical pixel mismatches Resize the window to the physical size of the image. If softbuffer scales the contents incorrectly, only parts of the fruit basket will be visible. --- examples/fruit.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/fruit.rs b/examples/fruit.rs index d0497d6..fabf979 100644 --- a/examples/fruit.rs +++ b/examples/fruit.rs @@ -16,7 +16,10 @@ fn main() { }).collect::>(); let event_loop = EventLoop::new(); - let window = WindowBuilder::new().build(&event_loop).unwrap(); + let window = WindowBuilder::new() + .with_inner_size(winit::dpi::PhysicalSize::new(fruit.width(), fruit.height())) + .build(&event_loop) + .unwrap(); #[cfg(target_arch = "wasm32")] { From b74047ae860da99344fb1ee8407e434368f29a84 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Tue, 13 Dec 2022 14:54:40 +0100 Subject: [PATCH 2/2] Fix scaling of the buffer on macOS Currently the size of the buffer on macOS is interpreted in logical pixels, which is inconsistent with the other platforms. Instead, we should apply the same scale factor that applies to regular rendering. --- src/cg.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cg.rs b/src/cg.rs index 901600e..f38d2f2 100644 --- a/src/cg.rs +++ b/src/cg.rs @@ -6,7 +6,7 @@ use core_graphics::data_provider::CGDataProvider; use core_graphics::image::CGImage; use cocoa::base::{id, nil}; -use cocoa::appkit::{NSView, NSViewWidthSizable, NSViewHeightSizable}; +use cocoa::appkit::{NSView, NSViewWidthSizable, NSViewHeightSizable, NSWindow}; use cocoa::quartzcore::{CALayer, ContentsGravity}; use foreign_types::ForeignType; @@ -18,11 +18,13 @@ pub struct CGImpl { impl CGImpl { pub unsafe fn new(handle: AppKitWindowHandle) -> Result { + let window = handle.ns_window as id; let view = handle.ns_view as id; let layer = CALayer::new(); - let subview: id = NSView::alloc(nil).initWithFrame_(view.frame()); + let subview: id = NSView::alloc(nil).initWithFrame_(NSView::frame(view)); layer.set_contents_gravity(ContentsGravity::TopLeft); layer.set_needs_display_on_bounds_change(false); + layer.set_contents_scale(window.backingScaleFactor()); subview.setLayer(layer.id()); subview.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable);