diff --git a/assets/macos/WezTerm.app/libEGL.dylib b/assets/macos/WezTerm.app/libEGL.dylib new file mode 100755 index 000000000..138be5453 Binary files /dev/null and b/assets/macos/WezTerm.app/libEGL.dylib differ diff --git a/assets/macos/WezTerm.app/libGLESv1_CM.dylib b/assets/macos/WezTerm.app/libGLESv1_CM.dylib new file mode 100755 index 000000000..71cc374d2 Binary files /dev/null and b/assets/macos/WezTerm.app/libGLESv1_CM.dylib differ diff --git a/assets/macos/WezTerm.app/libGLESv2.dylib b/assets/macos/WezTerm.app/libGLESv2.dylib new file mode 100755 index 000000000..44e87cd67 Binary files /dev/null and b/assets/macos/WezTerm.app/libGLESv2.dylib differ diff --git a/docs/changelog.markdown b/docs/changelog.markdown index ac27213b8..409ff4735 100644 --- a/docs/changelog.markdown +++ b/docs/changelog.markdown @@ -76,6 +76,8 @@ brief notes about them may accumulate here. * Windows: Fixed an issue with the `Hide` key assignment; it would hide the window with no way to show it again! `Hide` now minimizes the window instead. +* macOS: we now use Metal to render the gui, via + [MetalANGLE](https://github.com/kakashidinho/metalangle) ### 20200909-002054-4c9af461 diff --git a/licenses/ANGLE.md b/licenses/ANGLE.md new file mode 100644 index 000000000..0f65fd60f --- /dev/null +++ b/licenses/ANGLE.md @@ -0,0 +1,32 @@ +// Copyright 2018 The ANGLE Project Authors. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// +// Neither the name of TransGaming Inc., Google Inc., 3DLabs Inc. +// Ltd., nor the names of their contributors may be used to endorse +// or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. diff --git a/licenses/README.md b/licenses/README.md new file mode 100644 index 000000000..6ec7c11c2 --- /dev/null +++ b/licenses/README.md @@ -0,0 +1,2 @@ +WezTerm bundles some components provided by third parties. +Their respective licenses are included in this directory. diff --git a/wezterm.rb b/wezterm.rb new file mode 100644 index 000000000..26d108454 --- /dev/null +++ b/wezterm.rb @@ -0,0 +1,19 @@ +# Note: if you are viewing this from the tap repo, this file is automatically +# updated from: +# https://github.com/wez/wezterm/blob/master/ci/wezterm-homebrew-macos.rb.template +# by automation in the wezterm repo. + +class Wezterm < Formula + desc "A GPU-accelerated cross-platform terminal emulator and multiplexer written by @wez and implemented in Rust" + homepage "https://wezfurlong.org/wezterm/" + url "https://github.com/wez/wezterm/releases/download/20200909-002054-4c9af461-174-g2bac3a90/WezTerm-macos-20200909-002054-4c9af461-174-g2bac3a90.zip" + sha256 "503b9d8ea797dc9689310c9eaaeb31ed1f6b30e68a0aaba3f4851dc7004ee6f8" + head "https://github.com/wez/wezterm/releases/download/nightly/WezTerm-macos-nightly.zip" + + def install + prefix.install "WezTerm.app" + bin.write_exec_script "#{prefix}/WezTerm.app/wezterm" + bin.write_exec_script "#{prefix}/WezTerm.app/strip-ansi-escapes" + end +end + diff --git a/window/src/egl.rs b/window/src/egl.rs index 294769711..99697c1b9 100644 --- a/window/src/egl.rs +++ b/window/src/egl.rs @@ -368,6 +368,7 @@ impl EglWrapper { } impl GlState { + #[cfg_attr(target_os = "macos", allow(unused))] pub fn get_connection(&self) -> &Rc { &self.connection } @@ -375,19 +376,33 @@ impl GlState { fn with_egl_lib anyhow::Result>( mut func: F, ) -> anyhow::Result { - let paths = [ - // While EGL is cross platform, it isn't available on macOS nor is it - // available on my nvidia based system + let mut paths: Vec = vec![ #[cfg(target_os = "windows")] - "libEGL.dll", + "libEGL.dll".into(), #[cfg(target_os = "windows")] - "atioglxx.dll", - #[cfg(not(target_os = "windows"))] - "libEGL.so.1", - #[cfg(not(target_os = "windows"))] - "libEGL.so", + "atioglxx.dll".into(), + #[cfg(all(not(target_os = "macos"), not(target_os = "windows")))] + "libEGL.so.1".into(), + #[cfg(all(not(target_os = "macos"), not(target_os = "windows")))] + "libEGL.so".into(), ]; + if cfg!(target_os = "macos") { + // On macOS, let's look in the application directory to see + // if we've deployed libEGL.dylib alongside; if so, we want + // to try loading that. + paths.push( + std::env::current_exe()? + .parent() + .ok_or_else(|| anyhow!("current_exe isn't in a directory!?"))? + .join("libEGL.dylib"), + ); + + // And just in case, let's also allow loading via + // DYLD_LIBRARY_PATH + paths.push("libEGL.dylib".into()); + } + let mut errors = vec![]; for _ in 0..2 { @@ -401,15 +416,19 @@ impl GlState { match EglWrapper::load_egl(lib) { Ok(egl) => match func(egl) { Ok(result) => { - log::info!("initialized {}", path); + log::info!("initialized {}", path.display()); return Ok(result); } Err(e) => { - errors.push(format!("with_egl_lib({}) failed: {}", path, e)); + errors.push(format!( + "with_egl_lib({}) failed: {}", + path.display(), + e + )); } }, Err(e) => { - errors.push(format!("load_egl {} failed: {}", path, e)); + errors.push(format!("load_egl {} failed: {}", path.display(), e)); } } } diff --git a/window/src/lib.rs b/window/src/lib.rs index b48cfe812..0005dd2f9 100644 --- a/window/src/lib.rs +++ b/window/src/lib.rs @@ -9,11 +9,7 @@ pub mod os; mod spawn; mod timerlist; -#[cfg(all( - not(target_os = "macos"), - not(target_os = "windows"), - feature = "opengl" -))] +#[cfg(all(not(target_os = "windows"), feature = "opengl"))] mod egl; #[cfg(feature = "opengl")] diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 4668c4733..0d2003f00 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -104,142 +104,191 @@ mod opengl { use core_foundation::string::CFString; use std::str::FromStr; + #[derive(Clone)] + pub enum BackendImpl { + Cgl(Rc), + Egl(Rc), + } + + impl BackendImpl { + pub fn update(&self) { + if let Self::Cgl(be) = self { + be.update(); + } + } + } + #[derive(Clone)] pub struct GlContextPair { pub context: Rc, - pub backend: Rc, + pub backend: BackendImpl, } impl GlContextPair { + /// on macOS we first try to initialize EGL by dynamically loading it. + /// The system doesn't provide an EGL implementation, but the ANGLE + /// project (and MetalANGLE) both provide implementations. + /// The ANGLE EGL implementation wants a CALayer descendant passed + /// as the EGLNativeWindowType. pub fn create(view: id) -> anyhow::Result { - let backend = Rc::new(GlState::create(view)?); + let layer: id; - let context = unsafe { - glium::backend::Context::new( - Rc::clone(&backend), - true, - if cfg!(debug_assertions) { - glium::debug::DebugCallbackBehavior::DebugMessageOnError - } else { - glium::debug::DebugCallbackBehavior::Ignore - }, - ) - }?; + // ANGLE wants a layer, so tell the view to create one. + // Importantly, we must set its scale to 1.0 prior to initializing + // EGL to prevent undesirable scaling. + unsafe { + let _: () = msg_send![view, setWantsLayer: YES]; + layer = msg_send![view, layer]; + let _: () = msg_send![layer, setContentsScale: 1.0f64]; + }; + + let behavior = if cfg!(debug_assertions) { + glium::debug::DebugCallbackBehavior::DebugMessageOnError + } else { + glium::debug::DebugCallbackBehavior::Ignore + }; + + // Let's first try to initialize EGL... + let (context, backend) = match crate::egl::GlState::create(None, layer as *const c_void) + { + Ok(backend) => { + let backend = Rc::new(backend); + let context = unsafe { + glium::backend::Context::new(Rc::clone(&backend), true, behavior) + }?; + (context, BackendImpl::Egl(backend)) + } + // ... and then fallback to the deprecated platform provided CGL + Err(err) => { + log::warn!("EGL init failed: {:#}, falling back to CGL", err); + let backend = Rc::new(cglbits::GlState::create(view)?); + let context = unsafe { + glium::backend::Context::new(Rc::clone(&backend), true, behavior) + }?; + (context, BackendImpl::Cgl(backend)) + } + }; Ok(Self { context, backend }) } } - pub struct GlState { - _pixel_format: StrongPtr, - gl_context: StrongPtr, - } + mod cglbits { + use super::*; - impl GlState { - pub fn create(view: id) -> anyhow::Result { - let pixel_format = unsafe { - StrongPtr::new(NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&[ - appkit::NSOpenGLPFAOpenGLProfile as u32, - appkit::NSOpenGLProfileVersion3_2Core as u32, - appkit::NSOpenGLPFAClosestPolicy as u32, - appkit::NSOpenGLPFAColorSize as u32, - 32, - appkit::NSOpenGLPFAAlphaSize as u32, - 8, - appkit::NSOpenGLPFADepthSize as u32, - 24, - appkit::NSOpenGLPFAStencilSize as u32, - 8, - appkit::NSOpenGLPFAAllowOfflineRenderers as u32, - appkit::NSOpenGLPFAAccelerated as u32, - appkit::NSOpenGLPFADoubleBuffer as u32, - 0, - ])) - }; - ensure!( - !pixel_format.is_null(), - "failed to create NSOpenGLPixelFormat" - ); - - // Allow using retina resolutions; without this we're forced into low res - // and the system will scale us up, resulting in blurry rendering - unsafe { - let _: () = msg_send![view, setWantsBestResolutionOpenGLSurface: YES]; - } - - let gl_context = unsafe { - StrongPtr::new( - NSOpenGLContext::alloc(nil).initWithFormat_shareContext_(*pixel_format, nil), - ) - }; - ensure!(!gl_context.is_null(), "failed to create NSOpenGLContext"); - unsafe { - gl_context.setView_(view); - } - - Ok(Self { - _pixel_format: pixel_format, - gl_context, - }) + pub struct GlState { + _pixel_format: StrongPtr, + gl_context: StrongPtr, } - /// Calls NSOpenGLContext update; we need to do this on resize - pub fn update(&self) { - unsafe { - let _: () = msg_send![*self.gl_context, update]; - } - } - } - - unsafe impl glium::backend::Backend for GlState { - fn swap_buffers(&self) -> Result<(), glium::SwapBuffersError> { - unsafe { - let pool = NSAutoreleasePool::new(nil); - self.gl_context.flushBuffer(); - let _: () = msg_send![pool, release]; - } - Ok(()) - } - - unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void { - let symbol_name: CFString = FromStr::from_str(symbol).unwrap(); - let framework_name: CFString = FromStr::from_str("com.apple.opengl").unwrap(); - let framework = CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef()); - let symbol = - CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef()); - symbol as *const _ - } - - fn get_framebuffer_dimensions(&self) -> (u32, u32) { - unsafe { - let view = self.gl_context.view(); - let frame = NSView::frame(view); - let backing_frame = NSView::convertRectToBacking(view, frame); - ( - backing_frame.size.width as u32, - backing_frame.size.height as u32, - ) - } - } - - fn is_current(&self) -> bool { - unsafe { - let pool = NSAutoreleasePool::new(nil); - let current = NSOpenGLContext::currentContext(nil); - let res = if current != nil { - let is_equal: BOOL = msg_send![current, isEqual: *self.gl_context]; - is_equal != NO - } else { - false + impl GlState { + pub fn create(view: id) -> anyhow::Result { + let pixel_format = unsafe { + StrongPtr::new(NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&[ + appkit::NSOpenGLPFAOpenGLProfile as u32, + appkit::NSOpenGLProfileVersion3_2Core as u32, + appkit::NSOpenGLPFAClosestPolicy as u32, + appkit::NSOpenGLPFAColorSize as u32, + 32, + appkit::NSOpenGLPFAAlphaSize as u32, + 8, + appkit::NSOpenGLPFADepthSize as u32, + 24, + appkit::NSOpenGLPFAStencilSize as u32, + 8, + appkit::NSOpenGLPFAAllowOfflineRenderers as u32, + appkit::NSOpenGLPFAAccelerated as u32, + appkit::NSOpenGLPFADoubleBuffer as u32, + 0, + ])) }; - let _: () = msg_send![pool, release]; - res + ensure!( + !pixel_format.is_null(), + "failed to create NSOpenGLPixelFormat" + ); + + // Allow using retina resolutions; without this we're forced into low res + // and the system will scale us up, resulting in blurry rendering + unsafe { + let _: () = msg_send![view, setWantsBestResolutionOpenGLSurface: YES]; + } + + let gl_context = unsafe { + StrongPtr::new( + NSOpenGLContext::alloc(nil) + .initWithFormat_shareContext_(*pixel_format, nil), + ) + }; + ensure!(!gl_context.is_null(), "failed to create NSOpenGLContext"); + unsafe { + gl_context.setView_(view); + } + + Ok(Self { + _pixel_format: pixel_format, + gl_context, + }) + } + + /// Calls NSOpenGLContext update; we need to do this on resize + pub fn update(&self) { + unsafe { + let _: () = msg_send![*self.gl_context, update]; + } } } - unsafe fn make_current(&self) { - let _: () = msg_send![*self.gl_context, update]; - self.gl_context.makeCurrentContext(); + unsafe impl glium::backend::Backend for GlState { + fn swap_buffers(&self) -> Result<(), glium::SwapBuffersError> { + unsafe { + let pool = NSAutoreleasePool::new(nil); + self.gl_context.flushBuffer(); + let _: () = msg_send![pool, release]; + } + Ok(()) + } + + unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void { + let symbol_name: CFString = FromStr::from_str(symbol).unwrap(); + let framework_name: CFString = FromStr::from_str("com.apple.opengl").unwrap(); + let framework = + CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef()); + let symbol = + CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef()); + symbol as *const _ + } + + fn get_framebuffer_dimensions(&self) -> (u32, u32) { + unsafe { + let view = self.gl_context.view(); + let frame = NSView::frame(view); + let backing_frame = NSView::convertRectToBacking(view, frame); + ( + backing_frame.size.width as u32, + backing_frame.size.height as u32, + ) + } + } + + fn is_current(&self) -> bool { + unsafe { + let pool = NSAutoreleasePool::new(nil); + let current = NSOpenGLContext::currentContext(nil); + let res = if current != nil { + let is_equal: BOOL = msg_send![current, isEqual: *self.gl_context]; + is_equal != NO + } else { + false + }; + let _: () = msg_send![pool, release]; + res + } + } + + unsafe fn make_current(&self) { + let _: () = msg_send![*self.gl_context, update]; + self.gl_context.makeCurrentContext(); + } } } } @@ -993,6 +1042,12 @@ impl WindowView { YES } + // Tell the window/view/layer stuff that we only have a single opaque + // thing in the window so that it can optimize rendering + extern "C" fn is_opaque(_this: &Object, _sel: Sel) -> BOOL { + YES + } + // Don't use Cocoa native window tabbing extern "C" fn allow_automatic_tabbing(_this: &Object, _sel: Sel) -> BOOL { NO @@ -1428,6 +1483,10 @@ impl WindowView { sel!(isFlipped), Self::is_flipped as extern "C" fn(&Object, Sel) -> BOOL, ); + cls.add_method( + sel!(isOpaque), + Self::is_opaque as extern "C" fn(&Object, Sel) -> BOOL, + ); cls.add_method( sel!(allowsAutomaticWindowTabbing),