Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

27.4 KB
Raw Blame
use crate::{
    SharedPauseState, StudioQuality,
    output_pipeline::*,
    sources::screen_capture::{self, CropBounds, ScreenCaptureFormat, ScreenCaptureTarget},
};

#[cfg(target_os = "macos")]
use crate::output_pipeline::{MacOSFragmentedM4SMuxer, MacOSFragmentedM4SMuxerConfig};
#[cfg(windows)]
use crate::output_pipeline::{WindowsFragmentedM4SMuxer, WindowsFragmentedM4SMuxerConfig};
use anyhow::anyhow;
#[cfg(any(target_os = "macos", windows))]
use cap_enc_ffmpeg::h264::H264EncoderBuilder;
#[cfg(target_os = "linux")]
use cap_enc_ffmpeg::h264::H264Preset;
#[cfg(windows)]
use cap_enc_ffmpeg::h264::H264Preset;
use cap_enc_ffmpeg::segmented_stream::SegmentCompletedEvent;
use cap_timestamp::Timestamps;
use std::path::PathBuf;

#[cfg(windows)]
use std::sync::Arc;
#[cfg(windows)]
use std::sync::atomic::{AtomicBool, Ordering};

#[cfg(windows)]
#[derive(Clone, Debug)]
pub struct EncoderPreferences {
    force_software: Arc<AtomicBool>,
}

#[cfg(windows)]
impl Default for EncoderPreferences {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(windows)]
impl EncoderPreferences {
    pub fn new() -> Self {
        Self {
            force_software: Arc::new(AtomicBool::new(false)),
        }
    }

    pub fn should_force_software(&self) -> bool {
        self.force_software.load(Ordering::Relaxed)
    }

    pub fn force_software_only(&self) {
        self.force_software.store(true, Ordering::Relaxed);
    }
}

pub trait MakeCapturePipeline: ScreenCaptureFormat + std::fmt::Debug + 'static {
    #[allow(clippy::too_many_arguments)]
    async fn make_studio_mode_pipeline(
        screen_capture: screen_capture::VideoSourceConfig,
        output_path: PathBuf,
        start_time: Timestamps,
        start_gate: Option<RecordingStartGate>,
        fragmented: bool,
        use_oop_muxer: bool,
        shared_pause_state: Option<SharedPauseState>,
        output_size: Option<(u32, u32)>,
        quality: StudioQuality,
        #[cfg(windows)] encoder_preferences: EncoderPreferences,
    ) -> anyhow::Result<OutputPipeline>
    where
        Self: Sized;

    async fn make_instant_segmented_video_pipeline(
        screen_capture: screen_capture::VideoSourceConfig,
        segments_dir: PathBuf,
        output_size: (u32, u32),
        start_time: Timestamps,
        start_gate: Option<RecordingStartGate>,
        segment_tx: Option<std::sync::mpsc::Sender<SegmentCompletedEvent>>,
    ) -> anyhow::Result<OutputPipeline>
    where
        Self: Sized;
}

pub struct Stop;

#[cfg(target_os = "macos")]
impl MakeCapturePipeline for screen_capture::CMSampleBufferCapture {
    async fn make_studio_mode_pipeline(
        screen_capture: screen_capture::VideoSourceConfig,
        output_path: PathBuf,
        start_time: Timestamps,
        start_gate: Option<RecordingStartGate>,
        fragmented: bool,
        use_oop_muxer: bool,
        shared_pause_state: Option<SharedPauseState>,
        output_size: Option<(u32, u32)>,
        quality: StudioQuality,
    ) -> anyhow::Result<OutputPipeline> {
        let ultra = quality == StudioQuality::Ultra;
        let compatibility = quality == StudioQuality::Compatibility;

        tracing::debug!(
            ?quality,
            ultra,
            compatibility,
            fragmented,
            use_oop_muxer,
            "Studio mode capture pipeline quality selection"
        );

        if fragmented {
            let fragments_dir = output_path
                .parent()
                .map(|p| p.join("display"))
                .unwrap_or_else(|| output_path.with_file_name("display"));

            let bpp = if ultra {
                H264EncoderBuilder::ULTRA_BPP
            } else if compatibility {
                H264EncoderBuilder::QUALITY_BPP * 0.5
            } else {
                H264EncoderBuilder::QUALITY_BPP
            };

            let preset = if ultra {
                cap_enc_ffmpeg::h264::H264Preset::Medium
            } else {
                cap_enc_ffmpeg::h264::H264Preset::Ultrafast
            };

            tracing::debug!(bpp, ?preset, "Fragmented studio pipeline encoder config");

            let oop_ok = if use_oop_muxer {
                match crate::output_pipeline::oop_muxer::resolve_muxer_binary() {
                    Ok(bin_path) => {
                        tracing::info!(
                            bin_path = %bin_path.display(),
                            "Using out-of-process fragmented M4S muxer (Phase 5 OOP isolation)"
                        );
                        true
                    }
                    Err(err) => {
                        tracing::warn!(
                            error = %err,
                            "out_of_process_muxer requested but cap-muxer binary is unavailable; \
                             falling back to in-process muxer to preserve the recording"
                        );
                        false
                    }
                }
            } else {
                false
            };

            if oop_ok {
                use crate::output_pipeline::{
                    OutOfProcessFragmentedM4SMuxer, OutOfProcessFragmentedM4SMuxerConfig,
                };

                OutputPipeline::builder(fragments_dir)
                    .with_video::<screen_capture::VideoSource>(screen_capture)
                    .with_timestamps(start_time)
                    .with_start_gate(start_gate.clone())
                    .build::<OutOfProcessFragmentedM4SMuxer>(OutOfProcessFragmentedM4SMuxerConfig {
                        preset,
                        bpp,
                        output_size,
                        shared_pause_state,
                        ..Default::default()
                    })
                    .await
            } else {
                OutputPipeline::builder(fragments_dir)
                    .with_video::<screen_capture::VideoSource>(screen_capture)
                    .with_timestamps(start_time)
                    .with_start_gate(start_gate.clone())
                    .build::<MacOSFragmentedM4SMuxer>(MacOSFragmentedM4SMuxerConfig {
                        timed_fragments: true,
                        preset,
                        bpp,
                        output_size,
                        shared_pause_state,
                        ..Default::default()
                    })
                    .await
            }
        } else {
            tracing::debug!(
                ultra_quality = ultra,
                "Non-fragmented studio pipeline encoder config"
            );

            OutputPipeline::builder(output_path.clone())
                .with_video::<screen_capture::VideoSource>(screen_capture)
                .with_timestamps(start_time)
                .with_start_gate(start_gate.clone())
                .build::<AVFoundationMp4Muxer>(AVFoundationMp4MuxerConfig {
                    output_height: output_size.map(|(_, h)| h),
                    instant_mode: false,
                    ultra_quality: ultra,
                    compatibility_quality: compatibility,
                })
                .await
        }
    }

    async fn make_instant_segmented_video_pipeline(
        screen_capture: screen_capture::VideoSourceConfig,
        segments_dir: PathBuf,
        output_size: (u32, u32),
        start_time: Timestamps,
        start_gate: Option<RecordingStartGate>,
        segment_tx: Option<std::sync::mpsc::Sender<SegmentCompletedEvent>>,
    ) -> anyhow::Result<OutputPipeline> {
        OutputPipeline::builder(segments_dir)
            .with_video::<screen_capture::VideoSource>(screen_capture)
            .with_timestamps(start_time)
            .with_start_gate(start_gate.clone())
            .build::<MacOSFragmentedM4SMuxer>(MacOSFragmentedM4SMuxerConfig {
                timed_fragments: false,
                bpp: H264EncoderBuilder::INSTANT_MODE_BPP,
                output_size: Some(output_size),
                segment_tx,
                ..Default::default()
            })
            .await
    }
}

#[cfg(windows)]
impl MakeCapturePipeline for screen_capture::Direct3DCapture {
    #[allow(clippy::too_many_arguments)]
    async fn make_studio_mode_pipeline(
        screen_capture: screen_capture::VideoSourceConfig,
        output_path: PathBuf,
        start_time: Timestamps,
        start_gate: Option<RecordingStartGate>,
        fragmented: bool,
        use_oop_muxer: bool,
        shared_pause_state: Option<SharedPauseState>,
        output_size: Option<(u32, u32)>,
        quality: StudioQuality,
        encoder_preferences: EncoderPreferences,
    ) -> anyhow::Result<OutputPipeline> {
        let ultra = quality == StudioQuality::Ultra;

        if fragmented {
            let fragments_dir = output_path
                .parent()
                .map(|p| p.join("display"))
                .unwrap_or_else(|| output_path.with_file_name("display"));

            let bpp = if ultra {
                H264EncoderBuilder::ULTRA_BPP
            } else {
                H264EncoderBuilder::QUALITY_BPP
            };

            let preset = if ultra {
                H264Preset::Medium
            } else {
                H264Preset::Ultrafast
            };

            let oop_ok = if use_oop_muxer {
                match crate::output_pipeline::oop_muxer::resolve_muxer_binary() {
                    Ok(bin_path) => {
                        tracing::info!(
                            bin_path = %bin_path.display(),
                            "Using Windows out-of-process fragmented M4S muxer (Phase 5 OOP isolation)"
                        );
                        true
                    }
                    Err(err) => {
                        tracing::warn!(
                            error = %err,
                            "out_of_process_muxer requested but cap-muxer binary is unavailable; \
                             falling back to in-process muxer to preserve the recording"
                        );
                        false
                    }
                }
            } else {
                false
            };

            if oop_ok {
                use crate::output_pipeline::{
                    WindowsOOPFragmentedM4SMuxer, WindowsOOPFragmentedM4SMuxerConfig,
                };

                OutputPipeline::builder(fragments_dir)
                    .with_video::<screen_capture::VideoSource>(screen_capture)
                    .with_timestamps(start_time)
                    .with_start_gate(start_gate.clone())
                    .build::<WindowsOOPFragmentedM4SMuxer>(WindowsOOPFragmentedM4SMuxerConfig {
                        segment_duration: std::time::Duration::from_secs(2),
                        preset,
                        bpp,
                        output_size,
                        shared_pause_state,
                        disk_space_callback: None,
                        segment_tx: None,
                        ..Default::default()
                    })
                    .await
            } else {
                OutputPipeline::builder(fragments_dir)
                    .with_video::<screen_capture::VideoSource>(screen_capture)
                    .with_timestamps(start_time)
                    .with_start_gate(start_gate.clone())
                    .build::<WindowsFragmentedM4SMuxer>(WindowsFragmentedM4SMuxerConfig {
                        timed_fragments: true,
                        segment_duration: std::time::Duration::from_secs(2),
                        preset,
                        bpp,
                        output_size,
                        shared_pause_state,
                        disk_space_callback: None,
                        segment_tx: None,
                    })
                    .await
            }
        } else {
            let d3d_device = screen_capture.d3d_device.clone();
            let bitrate_multiplier = if ultra { 0.3f32 } else { 0.15f32 };

            OutputPipeline::builder(output_path.clone())
                .with_video::<screen_capture::VideoSource>(screen_capture)
                .with_timestamps(start_time)
                .with_start_gate(start_gate.clone())
                .build::<WindowsMuxer>(WindowsMuxerConfig {
                    pixel_format: screen_capture::Direct3DCapture::PIXEL_FORMAT.as_dxgi(),
                    d3d_device,
                    bitrate_multiplier,
                    frame_rate: 30u32,
                    output_size: output_size.map(|(w, h)| windows::Graphics::SizeInt32 {
                        Width: w as i32,
                        Height: h as i32,
                    }),
                    encoder_preferences,
                    fragmented: false,
                    frag_duration_us: 2_000_000,
                })
                .await
        }
    }

    async fn make_instant_segmented_video_pipeline(
        screen_capture: screen_capture::VideoSourceConfig,
        segments_dir: PathBuf,
        output_size: (u32, u32),
        start_time: Timestamps,
        start_gate: Option<RecordingStartGate>,
        segment_tx: Option<std::sync::mpsc::Sender<SegmentCompletedEvent>>,
    ) -> anyhow::Result<OutputPipeline> {
        OutputPipeline::builder(segments_dir)
            .with_video::<screen_capture::VideoSource>(screen_capture)
            .with_timestamps(start_time)
            .with_start_gate(start_gate.clone())
            .build::<WindowsFragmentedM4SMuxer>(WindowsFragmentedM4SMuxerConfig {
                timed_fragments: false,
                segment_duration: std::time::Duration::from_secs(2),
                preset: H264Preset::Ultrafast,
                bpp: H264EncoderBuilder::INSTANT_MODE_BPP,
                output_size: Some(output_size),
                shared_pause_state: None,
                disk_space_callback: None,
                segment_tx,
            })
            .await
    }
}

#[cfg(target_os = "linux")]
impl MakeCapturePipeline for screen_capture::X11Capture {
    async fn make_studio_mode_pipeline(
        screen_capture: screen_capture::VideoSourceConfig,
        output_path: PathBuf,
        start_time: Timestamps,
        start_gate: Option<RecordingStartGate>,
        _fragmented: bool,
        _use_oop_muxer: bool,
        shared_pause_state: Option<SharedPauseState>,
        output_size: Option<(u32, u32)>,
        quality: StudioQuality,
    ) -> anyhow::Result<OutputPipeline> {
        let fragments_dir = output_path
            .parent()
            .map(|p| p.join("display"))
            .unwrap_or_else(|| output_path.with_file_name("display"));

        let ultra = quality == StudioQuality::Ultra;
        OutputPipeline::builder(fragments_dir)
            .with_video::<screen_capture::VideoSource>(screen_capture)
            .with_timestamps(start_time)
            .with_start_gate(start_gate.clone())
            .build::<crate::ffmpeg::SegmentedVideoMuxer>(crate::ffmpeg::SegmentedVideoMuxerConfig {
                timed_fragments: true,
                segment_duration: std::time::Duration::from_secs(2),
                preset: if ultra {
                    H264Preset::Medium
                } else {
                    H264Preset::Ultrafast
                },
                output_size,
                shared_pause_state,
                segment_tx: None,
            })
            .await
    }

    async fn make_instant_segmented_video_pipeline(
        screen_capture: screen_capture::VideoSourceConfig,
        segments_dir: PathBuf,
        output_size: (u32, u32),
        start_time: Timestamps,
        start_gate: Option<RecordingStartGate>,
        segment_tx: Option<std::sync::mpsc::Sender<SegmentCompletedEvent>>,
    ) -> anyhow::Result<OutputPipeline> {
        OutputPipeline::builder(segments_dir)
            .with_video::<screen_capture::VideoSource>(screen_capture)
            .with_timestamps(start_time)
            .with_start_gate(start_gate.clone())
            .build::<crate::ffmpeg::SegmentedVideoMuxer>(crate::ffmpeg::SegmentedVideoMuxerConfig {
                timed_fragments: false,
                segment_duration: std::time::Duration::from_secs(2),
                preset: H264Preset::Ultrafast,
                output_size: Some(output_size),
                shared_pause_state: None,
                segment_tx,
            })
            .await
    }
}

#[cfg(target_os = "macos")]
pub type ScreenCaptureMethod = screen_capture::CMSampleBufferCapture;

#[cfg(windows)]
pub type ScreenCaptureMethod = screen_capture::Direct3DCapture;

#[cfg(target_os = "linux")]
pub type ScreenCaptureMethod = screen_capture::X11Capture;

pub fn target_to_display_and_crop(
    target: &ScreenCaptureTarget,
) -> anyhow::Result<(scap_targets::Display, Option<CropBounds>)> {
    use scap_targets::{bounds::*, *};

    let display = target
        .display()
        .ok_or_else(|| anyhow!("Display not found"))?;

    let crop_bounds = match target {
        ScreenCaptureTarget::Display { .. } => None,
        ScreenCaptureTarget::Window { id } => {
            let window = Window::from_id(id).ok_or_else(|| anyhow!("Window not found"))?;

            #[cfg(target_os = "macos")]
            {
                let raw_display_bounds = display
                    .raw_handle()
                    .logical_bounds()
                    .ok_or_else(|| anyhow!("No display bounds"))?;
                let raw_window_bounds = window
                    .raw_handle()
                    .logical_bounds()
                    .ok_or_else(|| anyhow!("No window bounds"))?;

                Some(LogicalBounds::new(
                    LogicalPosition::new(
                        raw_window_bounds.position().x() - raw_display_bounds.position().x(),
                        raw_window_bounds.position().y() - raw_display_bounds.position().y(),
                    ),
                    raw_window_bounds.size(),
                ))
            }

            #[cfg(windows)]
            {
                let raw_display_position = display
                    .raw_handle()
                    .physical_position()
                    .ok_or_else(|| anyhow!("No display bounds"))?;
                let raw_window_bounds = window
                    .raw_handle()
                    .physical_bounds()
                    .ok_or_else(|| anyhow!("No window bounds"))?;

                Some(PhysicalBounds::new(
                    PhysicalPosition::new(
                        raw_window_bounds.position().x() - raw_display_position.x(),
                        raw_window_bounds.position().y() - raw_display_position.y(),
                    ),
                    raw_window_bounds.size(),
                ))
            }

            #[cfg(target_os = "linux")]
            {
                let raw_display_position = display
                    .raw_handle()
                    .physical_position()
                    .ok_or_else(|| anyhow!("No display bounds"))?;
                let raw_window_bounds = window
                    .raw_handle()
                    .physical_bounds()
                    .ok_or_else(|| anyhow!("No window bounds"))?;

                Some(PhysicalBounds::new(
                    PhysicalPosition::new(
                        raw_window_bounds.position().x() - raw_display_position.x(),
                        raw_window_bounds.position().y() - raw_display_position.y(),
                    ),
                    raw_window_bounds.size(),
                ))
            }
        }
        ScreenCaptureTarget::Area {
            bounds: relative_bounds,
            ..
        } => {
            #[cfg(target_os = "macos")]
            {
                Some(*relative_bounds)
            }

            #[cfg(windows)]
            {
                let raw_display_size = display
                    .physical_size()
                    .ok_or_else(|| anyhow!("No display bounds"))?;
                let logical_display_size = display
                    .logical_size()
                    .ok_or_else(|| anyhow!("No display logical size"))?;
                Some(
                    screen_capture::logical_area_to_physical_bounds(
                        *relative_bounds,
                        logical_display_size,
                        raw_display_size,
                    )
                    .ok_or_else(|| anyhow!("Invalid display bounds"))?,
                )
            }

            #[cfg(target_os = "linux")]
            {
                let raw_display_size = display
                    .physical_size()
                    .ok_or_else(|| anyhow!("No display bounds"))?;
                let logical_display_size = display
                    .logical_size()
                    .ok_or_else(|| anyhow!("No display logical size"))?;
                Some(
                    screen_capture::logical_area_to_physical_bounds(
                        *relative_bounds,
                        logical_display_size,
                        raw_display_size,
                    )
                    .ok_or_else(|| anyhow!("Invalid display bounds"))?,
                )
            }
        }
        ScreenCaptureTarget::CameraOnly => {
            return Err(anyhow!("Camera-only target has no display"));
        }
    };

    Ok((display, crop_bounds))
}

/// Locates the recording display's physical notch within the frames this target
/// will produce.
///
/// Window captures get `None`: they record the window's own surface rather than
/// a screen region, and the window moves, so there is no stable position.
/// Area captures also return `None` when they contain only part of the notch,
/// because `DisplayNotch` cannot encode a cropped source shape.
pub fn resolve_display_notch(target: &ScreenCaptureTarget) -> Option<cap_project::DisplayNotch> {
    let display = target.display()?;
    let notch = display.notch()?;

    match target {
        ScreenCaptureTarget::Display { .. } => Some(cap_project::DisplayNotch {
            x: notch.x,
            width: notch.width,
            height: notch.height,
        }),
        ScreenCaptureTarget::Area { bounds, .. } => {
            let display_size = display.logical_size()?;
            resolve_area_display_notch(notch, display_size, *bounds)
        }
        ScreenCaptureTarget::Window { .. } | ScreenCaptureTarget::CameraOnly => None,
    }
}

fn resolve_area_display_notch(
    notch: scap_targets::NotchGeometry,
    display_size: scap_targets::bounds::LogicalSize,
    bounds: scap_targets::bounds::LogicalBounds,
) -> Option<cap_project::DisplayNotch> {
    let area_left = bounds.position().x();
    let area_top = bounds.position().y();
    let area_width = bounds.size().width();
    let area_height = bounds.size().height();
    if area_width <= 0.0 || area_height <= 0.0 || area_top != 0.0 {
        return None;
    }

    let notch_left = notch.x * display_size.width();
    let notch_width = notch.width * display_size.width();
    let notch_right = notch_left + notch_width;
    let notch_height = notch.height * display_size.height();
    let area_right = area_left + area_width;
    let area_bottom = area_height;
    if area_left > notch_left || area_right < notch_right || area_bottom < notch_height {
        return None;
    }

    Some(cap_project::DisplayNotch {
        x: (notch_left - area_left) / area_width,
        width: notch_width / area_width,
        height: notch_height / area_height,
    })
}

#[cfg(test)]
mod display_notch_tests {
    use super::*;
    use scap_targets::bounds::{LogicalBounds, LogicalPosition, LogicalSize};

    const NOTCH: scap_targets::NotchGeometry = scap_targets::NotchGeometry {
        x: 0.4,
        width: 0.2,
        height: 0.1,
    };

    fn display_size() -> LogicalSize {
        LogicalSize::new(1_000.0, 800.0)
    }

    #[test]
    fn area_containing_the_full_notch_rebases_it() {
        let bounds = LogicalBounds::new(
            LogicalPosition::new(300.0, 0.0),
            LogicalSize::new(400.0, 200.0),
        );

        assert_eq!(
            resolve_area_display_notch(NOTCH, display_size(), bounds),
            Some(cap_project::DisplayNotch {
                x: 0.25,
                width: 0.5,
                height: 0.4,
            })
        );
    }

    #[test]
    fn partially_intersected_notches_are_omitted() {
        let horizontal = LogicalBounds::new(
            LogicalPosition::new(500.0, 0.0),
            LogicalSize::new(200.0, 200.0),
        );
        let vertical = LogicalBounds::new(
            LogicalPosition::new(300.0, 40.0),
            LogicalSize::new(400.0, 200.0),
        );

        assert_eq!(
            resolve_area_display_notch(NOTCH, display_size(), horizontal),
            None
        );
        assert_eq!(
            resolve_area_display_notch(NOTCH, display_size(), vertical),
            None
        );
    }
}

#[cfg(windows)]
pub fn create_d3d_device()
-> windows::core::Result<windows::Win32::Graphics::Direct3D11::ID3D11Device> {
    use windows::Win32::Graphics::{
        Direct3D::{D3D_DRIVER_TYPE, D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_UNKNOWN},
        Direct3D11::{D3D11_CREATE_DEVICE_FLAG, ID3D11Device},
    };

    let mut device = None;
    let flags = {
        use windows::Win32::Graphics::Direct3D11::D3D11_CREATE_DEVICE_BGRA_SUPPORT;

        let mut flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
        if cfg!(feature = "d3ddebug") {
            use windows::Win32::Graphics::Direct3D11::D3D11_CREATE_DEVICE_DEBUG;

            flags |= D3D11_CREATE_DEVICE_DEBUG;
        }
        flags
    };

    if let Ok(selected) = cap_d3d_adapter::select_capture_adapter(None) {
        if let Err(error) = create_d3d_device_on_adapter(&selected.adapter, flags, &mut device) {
            tracing::warn!(
                adapter = %selected.description,
                error = ?error,
                "capture_pipeline: pinned-adapter D3D11CreateDevice failed, falling back"
            );
        } else {
            return Ok(device.unwrap());
        }
    }

    let mut result = create_d3d_device_with_type(D3D_DRIVER_TYPE_HARDWARE, flags, &mut device);
    if let Err(error) = &result {
        use windows::Win32::Graphics::Dxgi::DXGI_ERROR_UNSUPPORTED;

        if error.code() == DXGI_ERROR_UNSUPPORTED {
            use windows::Win32::Graphics::Direct3D::D3D_DRIVER_TYPE_WARP;

            result = create_d3d_device_with_type(D3D_DRIVER_TYPE_WARP, flags, &mut device);
        }
    }
    result?;

    fn create_d3d_device_on_adapter(
        adapter: &windows::Win32::Graphics::Dxgi::IDXGIAdapter,
        flags: D3D11_CREATE_DEVICE_FLAG,
        device: *mut Option<ID3D11Device>,
    ) -> windows::core::Result<()> {
        unsafe {
            use windows::Win32::{
                Foundation::HMODULE,
                Graphics::Direct3D11::{D3D11_SDK_VERSION, D3D11CreateDevice},
            };

            D3D11CreateDevice(
                Some(adapter),
                D3D_DRIVER_TYPE_UNKNOWN,
                HMODULE(std::ptr::null_mut()),
                flags,
                None,
                D3D11_SDK_VERSION,
                Some(device),
                None,
                None,
            )
        }
    }

    fn create_d3d_device_with_type(
        driver_type: D3D_DRIVER_TYPE,
        flags: D3D11_CREATE_DEVICE_FLAG,
        device: *mut Option<ID3D11Device>,
    ) -> windows::core::Result<()> {
        unsafe {
            use windows::Win32::{
                Foundation::HMODULE,
                Graphics::Direct3D11::{D3D11_SDK_VERSION, D3D11CreateDevice},
            };

            D3D11CreateDevice(
                None,
                driver_type,
                HMODULE(std::ptr::null_mut()),
                flags,
                None,
                D3D11_SDK_VERSION,
                Some(device),
                None,
                None,
            )
        }
    }

    Ok(device.unwrap())
}