use crate::egl::types::EGLNativeWindowType;
use crate::Error;
use crate::GLApi;
use super::device::{Adapter, Device, NativeDevice, VendorPreference};
use super::surface::NativeWidget;
use euclid::default::Size2D;
use std::os::raw::c_void;
use winapi::shared::minwindef::UINT;
use winapi::um::d3dcommon::{D3D_DRIVER_TYPE_UNKNOWN, D3D_DRIVER_TYPE_WARP};
#[cfg(feature = "sm-winit")]
use winit::Window;
#[cfg(all(feature = "sm-winit", not(target_vendor = "uwp")))]
use winit::os::windows::WindowExt;
const INTEL_PCI_ID: UINT = 0x8086;
#[derive(Clone)]
pub struct Connection;
#[derive(Clone)]
pub struct NativeConnection;
impl Connection {
#[inline]
pub fn new() -> Result<Connection, Error> {
Ok(Connection)
}
#[inline]
pub unsafe fn from_native_connection(_: NativeConnection) -> Result<Connection, Error> {
Connection::new()
}
#[inline]
pub fn native_connection(&self) -> NativeConnection {
NativeConnection
}
#[inline]
pub fn gl_api(&self) -> GLApi {
GLApi::GLES
}
#[inline]
pub fn create_adapter(&self) -> Result<Adapter, Error> {
self.create_hardware_adapter()
}
#[inline]
pub fn create_hardware_adapter(&self) -> Result<Adapter, Error> {
Adapter::new(D3D_DRIVER_TYPE_UNKNOWN, VendorPreference::Avoid(INTEL_PCI_ID))
}
#[inline]
pub fn create_low_power_adapter(&self) -> Result<Adapter, Error> {
Adapter::new(D3D_DRIVER_TYPE_UNKNOWN, VendorPreference::Prefer(INTEL_PCI_ID))
}
#[inline]
pub fn create_software_adapter(&self) -> Result<Adapter, Error> {
Adapter::new(D3D_DRIVER_TYPE_WARP, VendorPreference::None)
}
#[inline]
pub fn create_device(&self, adapter: &Adapter) -> Result<Device, Error> {
Device::new(adapter)
}
#[inline]
pub unsafe fn create_device_from_native_device(&self, native_device: NativeDevice)
-> Result<Device, Error> {
Device::from_native_device(native_device)
}
#[cfg(feature = "sm-winit")]
pub fn from_winit_window(_: &Window) -> Result<Connection, Error> {
Connection::new()
}
#[cfg(all(feature = "sm-winit", not(target_vendor = "uwp")))]
#[inline]
pub fn create_native_widget_from_winit_window(&self, window: &Window)
-> Result<NativeWidget, Error> {
let hwnd = window.get_hwnd() as EGLNativeWindowType;
if hwnd.is_null() {
Err(Error::IncompatibleNativeWidget)
} else {
Ok(NativeWidget { egl_native_window: hwnd })
}
}
#[cfg(all(feature = "sm-winit", target_vendor = "uwp"))]
#[inline]
pub fn create_native_widget_from_winit_window(&self, _window: &Window)
-> Result<NativeWidget, Error> {
Err(Error::IncompatibleNativeWidget)
}
pub unsafe fn create_native_widget_from_ptr(&self, raw: *mut c_void, _size: Size2D<i32>) -> NativeWidget {
NativeWidget {
egl_native_window: raw as EGLNativeWindowType,
}
}
#[cfg(feature = "sm-raw-window-handle")]
#[inline]
pub fn create_native_widget_from_rwh(&self, _: raw_window_handle::RawWindowHandle)
-> Result<NativeWidget, Error> {
Err(Error::UnsupportedOnThisPlatform)
}
}
impl NativeConnection {
#[inline]
pub fn new() -> NativeConnection {
NativeConnection
}
}