Skip to content

Commit

Permalink
Persist OpenGL context creation flags
Browse files Browse the repository at this point in the history
This commit persists OpenGL context creation flags from previous window
build attempts saving time in multiwindow context.

It also creates window as srgb by default, since it's what Alacritty is
rendering in. For reference [1] and [2]. Moreover the fallback for 10
bit colors is also added.

[1] - alacritty#4939 and
[2] - alacritty#3756.

Fixes alacritty#4703.
  • Loading branch information
kchibisov authored Mar 24, 2022
1 parent f4bdf5f commit e7bb6c2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Creating the IPC socket failing if WAYLAND_DISPLAY contains an absolute path
- Crash when resetting the terminal while in vi mode
- `font.glyph_offset` not live reloading
- Failure when running on 10-bit color system
- The colors being slightly different when using srgb displays on macOS

## 0.10.1

Expand Down
55 changes: 45 additions & 10 deletions alacritty/src/display/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ use {

use std::fmt::{self, Display, Formatter};
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicU8, Ordering};

use bitflags::bitflags;
#[cfg(target_os = "macos")]
use cocoa::base::{id, NO, YES};
use glutin::dpi::{PhysicalPosition, PhysicalSize};
Expand Down Expand Up @@ -62,6 +64,17 @@ static WINDOW_ICON: &[u8] = include_bytes!("../../alacritty.png");
#[cfg(windows)]
const IDI_ICON: WORD = 0x101;

/// Context creation flags from probing config.
static GL_CONTEXT_CREATION_FLAGS: AtomicU8 = AtomicU8::new(GlContextFlags::SRGB.bits);

bitflags! {
pub struct GlContextFlags: u8 {
const EMPTY = 0b000000000;
const SRGB = 0b0000_0001;
const DEEP_COLOR = 0b0000_0010;
}
}

/// Window errors.
#[derive(Debug)]
pub enum Error {
Expand Down Expand Up @@ -119,19 +132,24 @@ impl From<crossfont::Error> for Error {
fn create_gl_window<E>(
mut window: WindowBuilder,
event_loop: &EventLoopWindowTarget<E>,
srgb: bool,
flags: GlContextFlags,
vsync: bool,
dimensions: Option<PhysicalSize<u32>>,
) -> Result<WindowedContext<PossiblyCurrent>> {
if let Some(dimensions) = dimensions {
window = window.with_inner_size(dimensions);
}

let windowed_context = ContextBuilder::new()
.with_srgb(srgb)
let mut windowed_context_builder = ContextBuilder::new()
.with_srgb(flags.contains(GlContextFlags::SRGB))
.with_vsync(vsync)
.with_hardware_acceleration(None)
.build_windowed(window, event_loop)?;
.with_hardware_acceleration(None);

if flags.contains(GlContextFlags::DEEP_COLOR) {
windowed_context_builder = windowed_context_builder.with_pixel_format(30, 2);
}

let windowed_context = windowed_context_builder.build_windowed(window, event_loop)?;

// Make the context current so OpenGL operations can run.
let windowed_context = unsafe { windowed_context.make_current().map_err(|(_, err)| err)? };
Expand Down Expand Up @@ -188,11 +206,28 @@ impl Window {
#[cfg(any(not(feature = "wayland"), target_os = "macos", windows))]
let is_wayland = false;

let windowed_context =
create_gl_window(window_builder.clone(), event_loop, false, !is_wayland, size)
.or_else(|_| {
create_gl_window(window_builder, event_loop, true, !is_wayland, size)
})?;
let mut windowed_context = None;
let current_flags =
GlContextFlags::from_bits_truncate(GL_CONTEXT_CREATION_FLAGS.load(Ordering::Relaxed));
for flags in [
current_flags,
GlContextFlags::EMPTY,
GlContextFlags::SRGB | GlContextFlags::DEEP_COLOR,
GlContextFlags::DEEP_COLOR,
] {
windowed_context = Some(create_gl_window(
window_builder.clone(),
event_loop,
flags,
!is_wayland,
size,
));
if windowed_context.as_ref().unwrap().is_ok() {
GL_CONTEXT_CREATION_FLAGS.store(flags.bits, Ordering::Relaxed);
break;
}
}
let windowed_context = windowed_context.unwrap()?;

// Text cursor.
let current_mouse_cursor = CursorIcon::Text;
Expand Down

0 comments on commit e7bb6c2

Please sign in to comment.