From 883e2d11d7b1aaf65f0629fd101c4932093e1e87 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 17 Nov 2022 22:17:58 -0700 Subject: [PATCH] make drawRect work on macos when using webgpu --- env-bootstrap/src/ringlog.rs | 3 +- wezterm-gui/src/main.rs | 2 +- wezterm-gui/src/renderstate.rs | 10 +++- wezterm-gui/src/termwindow/webgpu.rs | 5 ++ window/src/os/macos/window.rs | 82 ++++++++++++++++++++++++++-- 5 files changed, 92 insertions(+), 10 deletions(-) diff --git a/env-bootstrap/src/ringlog.rs b/env-bootstrap/src/ringlog.rs index 233f6550d..e12f46865 100644 --- a/env-bootstrap/src/ringlog.rs +++ b/env-bootstrap/src/ringlog.rs @@ -158,8 +158,8 @@ impl log::Log for Logger { } fn log(&self, record: &Record) { - RINGS.lock().unwrap().log(record); if self.filter.matches(record) { + RINGS.lock().unwrap().log(record); let ts = Local::now().format("%H:%M:%S%.3f").to_string(); let level = record.level().as_str(); let target = record.target().to_string(); @@ -274,6 +274,7 @@ fn setup_pretty() -> (LevelFilter, Logger) { let mut filters = FilterBuilder::new(); for (module, level) in [ ("wgpu_core", LevelFilter::Error), + ("wgpu_hal", LevelFilter::Error), ("gfx_backend_metal", LevelFilter::Error), ] { filters.filter_module(module, level); diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index c57e15b83..372c2386b 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -954,7 +954,7 @@ pub fn run_ls_fonts(config: config::ConfigHandle, cmd: &LsFontsCommand) -> anyho let mut glyph = String::new(); if let Some(texture) = &cached_glyph.texture { - use window::bitmaps::ImageTexture; + use ::window::bitmaps::ImageTexture; if let Some(tex) = texture.texture.downcast_ref::() { for y in texture.coords.min_y()..texture.coords.max_y() { for &px in tex.image.borrow().horizontal_pixel_range( diff --git a/wezterm-gui/src/renderstate.rs b/wezterm-gui/src/renderstate.rs index c88f22053..8dff0e8d6 100644 --- a/wezterm-gui/src/renderstate.rs +++ b/wezterm-gui/src/renderstate.rs @@ -3,6 +3,7 @@ use super::quad::*; use super::utilsprites::{RenderMetrics, UtilSprites}; use crate::termwindow::webgpu::{WebGpuState, WebGpuTexture}; use ::window::bitmaps::atlas::OutOfTextureSpace; +use ::window::bitmaps::Texture2d; use ::window::glium::backend::Context as GliumContext; use ::window::glium::buffer::{BufferMutSlice, Mapping}; use ::window::glium::{ @@ -15,7 +16,6 @@ use std::convert::TryInto; use std::rc::Rc; use wezterm_font::FontConfiguration; use wgpu::util::DeviceExt; -use window::bitmaps::Texture2d; const INDICES_PER_CELL: usize = 6; @@ -113,7 +113,13 @@ impl RenderContext { ctx.get_opengl_renderer_string(), ctx.get_opengl_version_string() ), - Self::WebGpu(_) => "WebGPU".to_string(), + Self::WebGpu(state) => { + let info = &state.adapter_info; + format!( + "WebGPU: adapter={}, device_type={:?}, driver={}, driver_info={}, backend={:?}", + info.name, info.device_type, info.driver, info.driver_info, info.backend + ) + } } } } diff --git a/wezterm-gui/src/termwindow/webgpu.rs b/wezterm-gui/src/termwindow/webgpu.rs index b9b9c0e03..f4295548f 100644 --- a/wezterm-gui/src/termwindow/webgpu.rs +++ b/wezterm-gui/src/termwindow/webgpu.rs @@ -21,6 +21,7 @@ pub struct ShaderUniform { } pub struct WebGpuState { + pub adapter_info: wgpu::AdapterInfo, pub surface: wgpu::Surface, pub device: wgpu::Device, pub queue: Arc, @@ -158,6 +159,9 @@ impl WebGpuState { .await .ok_or_else(|| anyhow!("no matching adapter found"))?; + let adapter_info = adapter.get_info(); + log::trace!("Using adapter: {adapter_info:?}"); + let (device, queue) = adapter .request_device( &wgpu::DeviceDescriptor { @@ -294,6 +298,7 @@ impl WebGpuState { }); Ok(Self { + adapter_info, surface, device, queue, diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 6eec7e781..5c8ffa5ee 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -52,6 +52,8 @@ use wezterm_input_types::is_ascii_control; #[allow(non_upper_case_globals)] const NSViewLayerContentsPlacementTopLeft: NSInteger = 11; +#[allow(non_upper_case_globals)] +const NSViewLayerContentsRedrawDuringViewResize: NSInteger = 2; fn round_away_from_zerof(value: f64) -> f64 { if value > 0. { @@ -520,6 +522,12 @@ impl Window { window.setContentView_(*view); window.setDelegate_(*view); + view.setWantsLayer(YES); + let () = msg_send![ + *view, + setLayerContentsRedrawPolicy: NSViewLayerContentsRedrawDuringViewResize + ]; + // register for drag and drop operations. let () = msg_send![ *window, @@ -959,6 +967,15 @@ impl WindowInner { unsafe { let current_app = NSRunningApplication::currentApplication(nil); current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps); + + // Stupid hack: adjust the window style mask and set it back + // to what it was. + // Without this, the CAMetalLayer used by webgpu seems to get + // stuck with a scale factor of 2 despite us having configured 1. + self.window + .setStyleMask_(NSWindowStyleMask::NSBorderlessWindowMask); + apply_decorations_to_window(&self.window, self.config.window_decorations); + self.window.makeKeyAndOrderFront_(nil) } } @@ -2510,23 +2527,60 @@ impl WindowView { } } - extern "C" fn update_layer(view: &mut Object, sel: Sel) { - log::info!("update_layer called"); + extern "C" fn update_layer(_view: &mut Object, _sel: Sel) { + log::trace!("update_layer called"); } - extern "C" fn wants_update_layer(view: &mut Object, sel: Sel) -> BOOL { - log::info!("wants_update_layer called"); + extern "C" fn wants_update_layer(_view: &mut Object, _sel: Sel) -> BOOL { + log::trace!("wants_update_layer called"); YES } extern "C" fn display_layer(view: &mut Object, sel: Sel, _layer_id: id) { - log::info!("display_layer"); + Self::draw_rect( + view, + sel, + NSRect::new(NSPoint::new(0., 0.), NSSize::new(0., 0.)), + ) + } + + extern "C" fn draw_layer_in_context( + _view: &mut Object, + _sel: Sel, + _layer_id: id, + _context: id, + ) { + } + + extern "C" fn layer_should_inherit_contents_scale_from_window( + _: &Object, + _: Sel, + layer: *mut Object, + _: CGFloat, + _: *mut Object, + ) -> BOOL { + log::trace!("layer_should_inherit_contents_scale_from_window"); + unsafe { + let () = msg_send![layer, setContentsScale: 1.0]; + } + YES + } + + extern "C" fn make_backing_layer(view: &mut Object, _: Sel) -> id { + log::trace!("make_backing_layer"); + let class = class!(CAMetalLayer); + unsafe { + let layer: id = msg_send![class, new]; + let () = msg_send![layer, setDelegate: view]; + let () = msg_send![layer, setContentsScale: 1.0]; + let () = msg_send![layer, setOpaque: NO]; + layer + } } extern "C" fn draw_rect(view: &mut Object, sel: Sel, _dirty_rect: NSRect) { if let Some(this) = Self::get_this(view) { let mut inner = this.inner.borrow_mut(); - log::info!("draw_rect called"); if inner.screen_changed { // If the screen resolution changed (which can also @@ -2666,6 +2720,17 @@ impl WindowView { Self::window_should_close as extern "C" fn(&mut Object, Sel, id) -> BOOL, ); + cls.add_method( + sel!(makeBackingLayer), + Self::make_backing_layer as extern "C" fn(&mut Object, Sel) -> id, + ); + + cls.add_method( + sel!(layer:shouldInheritContentsScale:fromWindow:), + Self::layer_should_inherit_contents_scale_from_window + as extern "C" fn(&Object, Sel, *mut Object, CGFloat, *mut Object) -> BOOL, + ); + cls.add_method( sel!(drawRect:), Self::draw_rect as extern "C" fn(&mut Object, Sel, NSRect), @@ -2686,6 +2751,11 @@ impl WindowView { Self::display_layer as extern "C" fn(&mut Object, Sel, id), ); + cls.add_method( + sel!(drawLayer:inContext:), + Self::draw_layer_in_context as extern "C" fn(&mut Object, Sel, id, id), + ); + cls.add_method( sel!(isFlipped), Self::is_flipped as extern "C" fn(&Object, Sel) -> BOOL,