macOS: bundle and use MetalANGLE to enable Metal rendering
This commit adjusts the window layer to have it try to load EGL implementations on macOS. This is important as the system provided OpenGL implementation is deprecated and I wanted to have a path forward for when it is finally removed. If EGL fails to initialize, we fall back to the CGL/OpenGL implementation that we used previously. I've included binaries built for 64-bit intel from the MetalANGLE project; here's how I built them: ``` git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git --depth 1 git clone https://github.com/kakashidinho/metalangle --depth 1 cd metalangle PATH=$PWD/../depot_tools:$PATH python scripts/bootstrap.py PATH=$PWD/../depot_tools:$PATH gclient sync PATH=$PWD/../depot_tools:$PATH gn --args="is_debug=false angle_enable_metal=true angle_enable_vulkan=false angle_enable_gl=false angle_build_all=false" gen out/Release PATH=$PWD/../depot_tools:$PATH autoninja -C out/Release ``` Those steps are a little too long to want to put them directly into the wezterm CI. It is important for metalangle to be >= https://github.com/kakashidinho/metalangle/commit/8230df39a500e1db62fdd68d2b1e767120747c39 in order for scaling to be handled correctly when dragging windows between monitors. refs: https://github.com/kakashidinho/metalangle/issues/34
This commit is contained in:
1 parent
2bac3a90bc
commit
4d71a7913a
10 files changed
+262
-133
No files matched your search
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -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
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -0,0 +1,2 @@
|
||||
WezTerm bundles some components provided by third parties.
|
||||
Their respective licenses are included in this directory.
|
||||
+19
@@ -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
|
||||
|
||||
+31
-12
@@ -368,6 +368,7 @@ impl EglWrapper {
|
||||
}
|
||||
|
||||
impl GlState {
|
||||
#[cfg_attr(target_os = "macos", allow(unused))]
|
||||
pub fn get_connection(&self) -> &Rc<GlConnection> {
|
||||
&self.connection
|
||||
}
|
||||
@@ -375,19 +376,33 @@ impl GlState {
|
||||
fn with_egl_lib<F: FnMut(EglWrapper) -> anyhow::Result<Self>>(
|
||||
mut func: F,
|
||||
) -> anyhow::Result<Self> {
|
||||
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<std::path::PathBuf> = 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-5
@@ -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")]
|
||||
|
||||
+175
-116
@@ -104,142 +104,191 @@ mod opengl {
|
||||
use core_foundation::string::CFString;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum BackendImpl {
|
||||
Cgl(Rc<cglbits::GlState>),
|
||||
Egl(Rc<crate::egl::GlState>),
|
||||
}
|
||||
|
||||
impl BackendImpl {
|
||||
pub fn update(&self) {
|
||||
if let Self::Cgl(be) = self {
|
||||
be.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GlContextPair {
|
||||
pub context: Rc<glium::backend::Context>,
|
||||
pub backend: Rc<GlState>,
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
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<Self> {
|
||||
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),
|
||||
|
||||
Reference in new issue
Block a user