Repository: alacritty/alacritty
File: alacritty/src/renderer/platform.rs (Rust)

Code before:
) -> GlutinResult<NotCurrentContext> {
    let debug = log::max_level() >= LevelFilter::Debug;
    let builder = ContextAttributesBuilder::new().with_debug(debug);

    let mut builders = [
        builder.clone().with_context_api(ContextApi::OpenGl(Some(Version::new(3, 3)))),
        // Try gles before OpenGL 2.1 as it tends to be more stable.
        builder.clone().with_context_api(ContextApi::Gles(Some(Version::new(2, 0)))),
        builder
            .with_profile(GlProfile::Compatibility)
            .with_context_api(ContextApi::OpenGl(Some(Version::new(2, 1)))),
    ]
    .into_iter();

    // Even if robustness is present it's not guaranteed to work.
    let robustness = gl_display.supported_features().contains(DisplayFeatures::CONTEXT_ROBUSTNESS);
    let builders: &mut dyn Iterator<Item = ContextAttributesBuilder> = if robustness {
        &mut builders.flat_map(|default| {
            let robust = default.clone().with_robustness(Robustness::RobustLoseContextOnReset);
            [robust, default]
        })
    } else {
        &mut builders
    };

    let mut profiles = builders.map(|builder| builder.build(raw_window_handle));

    // Try the optimal config first.
    let mut picked_context =
        unsafe { gl_display.create_context(gl_config, &profiles.next().unwrap()) };

    // Try the fallback ones.
    while let (Err(_), Some(profile)) = (picked_context.as_ref(), profiles.next()) {
        picked_context = unsafe { gl_display.create_context(gl_config, &profile) };
    }

    picked_context
}

pub fn create_gl_surface(
    gl_context: &NotCurrentContext,
    size: PhysicalSize<u32>,
    raw_window_handle: RawWindowHandle,
) -> GlutinResult<Surface<WindowSurface>> {
    // Get the display and the config used to create that context.
    let gl_display = gl_context.display();
    let gl_config = gl_context.config();

    let surface_attributes =
        SurfaceAttributesBuilder::<WindowSurface>::new().with_srgb(Some(false)).build(
            raw_window_handle,

Review comment (bug):
Wouldn't it be easier to just try robustness in here first and fall back if build fails? That's where the error occurs, right?

This whole builder construction just seems a bit convoluted to me at this point, might be better to start from scratch.

Code after:
) -> GlutinResult<NotCurrentContext> {
    let debug = log::max_level() >= LevelFilter::Debug;

    let apis = [
        (ContextApi::OpenGl(Some(Version::new(3, 3))), GlProfile::Core),
        // Try gles before OpenGL 2.1 as it tends to be more stable.
        (ContextApi::Gles(Some(Version::new(2, 0))), GlProfile::Core),
        (ContextApi::OpenGl(Some(Version::new(2, 1))), GlProfile::Compatibility),
    ];

    let robustness = gl_display.supported_features().contains(DisplayFeatures::CONTEXT_ROBUSTNESS);
    let robustness: &[Robustness] = if robustness {
        &[Robustness::RobustLoseContextOnReset, Robustness::NotRobust]
    } else {
        &[Robustness::NotRobust]
    };

    // Find the first context that builds without any errors.
    let mut error = None;
    for (api, profile) in apis {
        for robustness in robustness {
            let attributes = ContextAttributesBuilder::new()
                .with_debug(debug)
                .with_context_api(api)
                .with_profile(profile)
                .with_robustness(*robustness)
                .build(raw_window_handle);

            match unsafe { gl_display.create_context(gl_config, &attributes) } {
                Ok(profile) => return Ok(profile),
                Err(err) => error = Some(err),
            }
        }
    }

    // If no context was built successfully, return an error for the most permissive one.
    Err(error.unwrap())
}

pub fn create_gl_surface(
    gl_context: &NotCurrentContext,
    size: PhysicalSize<u32>,
    raw_window_handle: RawWindowHandle,
) -> GlutinResult<Surface<WindowSurface>> {
    // Get the display and the config used to create that context.
    let gl_display = gl_context.display();
    let gl_config = gl_context.config();

    let surface_attributes =
        SurfaceAttributesBuilder::<WindowSurface>::new().with_srgb(Some(false)).build(
            raw_window_handle,