use crate::gpu_windows::{function_in, inside, system_library};
use crate::layer_d3d11::{B8G8R8A8_UNORM, Box3, Com, TextureDescription, function, release};
use crate::windows::named;
use crate::windows::sys::{HANDLE, RECT};
use std::ffi::c_void;
use std::ptr::{null, null_mut};
type MakeDevice = unsafe extern "system" fn(Com, u32, isize, u32, *const u32, u32, u32, *mut Com, *mut u32, *mut Com) -> i32;
type Ask = unsafe extern "system" fn(Com, *const [u32; 4], *mut Com) -> i32;
const HARDWARE: u32 = 1;
const WARP: u32 = 5;
const DXGI_DEVICE: [u32; 4] = named(0x77DB_970F, 0x6276, 0x48BA, [0xBA, 0x28, 0x07, 0x01, 0x43, 0xB4, 0x39, 0x2C]);
const FACTORY: [u32; 4] = named(0x50C8_3A1C, 0xE072, 0x4C48, [0x87, 0xB0, 0x36, 0x30, 0xFA, 0x36, 0xA6, 0xD0]);
const TEXTURE: [u32; 4] = named(0x6F15_AAF2, 0xD208, 0x4E89, [0x9A, 0xB4, 0x48, 0x95, 0x35, 0xD3, 0x4F, 0x9C]);
#[repr(C)]
pub(crate) struct ChainDescription {
pub(crate) width: u32,
pub(crate) height: u32,
pub(crate) format: u32,
pub(crate) stereo: i32,
pub(crate) sample_count: u32,
pub(crate) sample_quality: u32,
pub(crate) usage: u32,
pub(crate) buffers: u32,
pub(crate) scaling: u32,
pub(crate) effect: u32,
pub(crate) alpha: u32,
pub(crate) flags: u32,
}
#[repr(C)]
pub(crate) struct Changed {
pub(crate) count: u32,
pub(crate) rects: *const RECT,
pub(crate) scroll: *const RECT,
pub(crate) offset: *const c_void,
}
pub(crate) struct Device {
device: Com,
context: Com,
factory: Com,
pub(crate) gpu: bool,
#[cfg(feature = "gpu-draw")]
pub(crate) drawing: Option<crate::gpu_draw_windows::Pipeline>,
}
impl Device {
pub(crate) fn gpu() -> Option<Device> {
#[cfg(test)]
if crate::gpu_windows::tests::faked().software {
let mut device = Device::made(WARP)?.drawing();
device.gpu = true;
return Some(device);
}
let device = Device::made(HARDWARE)?.drawing();
(!device.vendor_is_microsoft()).then_some(device)
}
pub(crate) fn software() -> Option<Device> {
#[cfg(test)]
if crate::gpu_windows::tests::faked().no_warp {
return None;
}
Device::made(WARP)
}
fn made(driver: u32) -> Option<Device> {
let mut made = Device {
device: null_mut(),
context: null_mut(),
factory: null_mut(),
gpu: driver == HARDWARE,
#[cfg(feature = "gpu-draw")]
drawing: None,
};
let (mut level, mut dxgi, mut adapter) = (0, null_mut(), null_mut());
let found = function_in(system_library("d3d11.dll"), c"D3D11CreateDevice");
unsafe {
if found == 0 || std::mem::transmute::<usize, MakeDevice>(found)(null_mut(), driver, 0, 0x29, null(), 0, 7, &mut made.device, &mut level, &mut made.context) < 0 {
return None;
}
if function::<Ask>(made.device, 0)(made.device, &DXGI_DEVICE, &mut dxgi) >= 0 {
function::<unsafe extern "system" fn(Com, u32) -> i32>(dxgi, 12)(dxgi, 1);
function::<unsafe extern "system" fn(Com, *mut Com) -> i32>(dxgi, 7)(dxgi, &mut adapter);
}
if !adapter.is_null() {
function::<Ask>(adapter, 6)(adapter, &FACTORY, &mut made.factory);
}
[adapter, dxgi].into_iter().for_each(|object| release(object));
}
(!made.factory.is_null()).then_some(made)
}
#[inline]
#[cfg_attr(not(feature = "gpu-draw"), allow(unused_mut))]
fn drawing(mut self) -> Device {
#[cfg(feature = "gpu-draw")]
{
self.drawing = crate::gpu_draw_windows::Pipeline::new(self.device);
}
self
}
fn vendor_is_microsoft(&self) -> bool {
let (mut dxgi, mut adapter, mut described) = (null_mut(), null_mut(), [0u64; 40]);
unsafe {
if function::<Ask>(self.device, 0)(self.device, &DXGI_DEVICE, &mut dxgi) >= 0 {
function::<unsafe extern "system" fn(Com, *mut Com) -> i32>(dxgi, 7)(dxgi, &mut adapter);
}
if !adapter.is_null() {
function::<unsafe extern "system" fn(Com, *mut u64) -> i32>(adapter, 10)(adapter, described.as_mut_ptr());
}
[adapter, dxgi].into_iter().for_each(|object| release(object));
}
described[32] as u32 == 0x1414
}
pub(crate) fn removed(&self) -> bool {
unsafe { function::<unsafe extern "system" fn(Com) -> i32>(self.device, 39)(self.device) != 0 }
}
}
unsafe impl Send for Device {}
impl Drop for Device {
fn drop(&mut self) {
#[cfg(feature = "gpu-draw")]
drop(self.drawing.take());
unsafe {
if !self.context.is_null() {
function::<unsafe extern "system" fn(Com)>(self.context, 111)(self.context);
}
[self.factory, self.context, self.device].into_iter().for_each(|object| release(object));
}
}
}
#[inline]
fn drawn(device: &Device) -> u32 {
#[cfg(feature = "gpu-draw")]
if device.drawing.is_some() {
return 0x20;
}
let _ = device;
0
}
pub(crate) struct Shown {
chain: Com,
picture: Com,
size: [i32; 2],
#[cfg(feature = "gpu-draw")]
target: Com,
}
impl Shown {
pub(crate) fn new(device: &Device, hwnd: HANDLE, size: [i32; 2]) -> Option<Shown> {
let [width, height] = size.map(|side| side.max(1) as u32);
let described = ChainDescription { width, height, format: B8G8R8A8_UNORM, stereo: 0, sample_count: 1, sample_quality: 0, usage: 0x20, buffers: 2, scaling: 1, effect: 3, alpha: 3, flags: 0 };
let mut shown = Shown {
chain: null_mut(),
picture: null_mut(),
size: [0; 2],
#[cfg(feature = "gpu-draw")]
target: null_mut(),
};
type MakeChain = unsafe extern "system" fn(Com, Com, HANDLE, *const ChainDescription, *const c_void, Com, *mut Com) -> i32;
unsafe {
if function::<MakeChain>(device.factory, 15)(device.factory, device.device, hwnd, &described, null(), null_mut(), &mut shown.chain) < 0 {
return None;
}
function::<unsafe extern "system" fn(Com, HANDLE, u32) -> i32>(device.factory, 8)(device.factory, hwnd, 3);
}
Some(shown)
}
pub(crate) fn fit(&mut self, device: &Device, size: [i32; 2]) -> Option<bool> {
if size == self.size || size[0] <= 0 || size[1] <= 0 {
return Some(false);
}
let [width, height] = size.map(|side| side as u32);
let described = TextureDescription {
width,
height,
mip_levels: 1,
array_size: 1,
format: B8G8R8A8_UNORM,
sample_count: 1,
sample_quality: 0,
usage: 0,
bind_flags: drawn(device),
cpu_access_flags: 0,
misc_flags: 0,
};
type MakeTexture = unsafe extern "system" fn(Com, *const TextureDescription, *const c_void, *mut Com) -> i32;
self.size = [0; 2];
unsafe {
#[cfg(feature = "gpu-draw")]
release(std::mem::replace(&mut self.target, null_mut()));
release(std::mem::replace(&mut self.picture, null_mut()));
if function::<unsafe extern "system" fn(Com, u32, u32, u32, u32, u32) -> i32>(self.chain, 13)(self.chain, 0, width, height, 0, 0) < 0 {
return None;
}
if function::<MakeTexture>(device.device, 5)(device.device, &described, null(), &mut self.picture) < 0 {
return None;
}
}
#[cfg(feature = "gpu-draw")]
if device.drawing.is_some() {
self.target = crate::gpu_draw_windows::Pipeline::target(device.device, self.picture);
}
self.size = size;
Some(true)
}
pub(crate) unsafe fn upload(&self, device: &Device, at: [i32; 2], size: [i32; 2], first: *const u32, pitch: usize) {
let [right, bottom] = [(at[0] + size[0]).min(self.size[0]), (at[1] + size[1]).min(self.size[1])];
if self.picture.is_null() || at[0] < 0 || at[1] < 0 || right <= at[0] || bottom <= at[1] {
return;
}
let place = Box3 { left: at[0] as u32, top: at[1] as u32, front: 0, right: right as u32, bottom: bottom as u32, back: 1 };
type Copy = unsafe extern "system" fn(Com, Com, u32, *const Box3, *const u32, u32, u32);
unsafe { function::<Copy>(device.context, 48)(device.context, self.picture, 0, &place, first, (pitch * 4) as u32, 0) };
}
#[cfg(feature = "gpu-draw")]
pub(crate) fn draw(&mut self, device: &mut Device, list: &crate::gpu_draw::DrawList, area: [i32; 4]) -> bool {
let Some(drawing) = device.drawing.as_mut() else { return false };
drawing.draw(device.device, device.context, self.target, list, area, self.size)
}
pub(crate) fn present(&mut self, device: &Device, dirty: &[RECT]) -> bool {
if self.picture.is_null() {
return true;
}
let dirty = inside(dirty, self.size);
let (changed, mut back) = (Changed { count: dirty.len() as u32, rects: dirty.as_ptr(), scroll: null(), offset: null() }, null_mut());
let presented = unsafe {
if function::<unsafe extern "system" fn(Com, u32, *const [u32; 4], *mut Com) -> i32>(self.chain, 9)(self.chain, 0, &TEXTURE, &mut back) < 0 {
return false;
}
function::<unsafe extern "system" fn(Com, Com, Com)>(device.context, 47)(device.context, back, self.picture);
#[cfg(test)]
tests::keep(device, back, self.size, self.chain, dirty);
release(back);
function::<unsafe extern "system" fn(Com, u32, u32, *const Changed) -> i32>(self.chain, 22)(self.chain, 1, 0, &changed)
};
presented >= 0
}
}
impl Drop for Shown {
fn drop(&mut self) {
unsafe { [self.picture, self.chain].into_iter().for_each(|object| release(object)) };
#[cfg(feature = "gpu-draw")]
unsafe {
release(self.target)
};
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::layer_d3d11::Pixels;
pub(crate) fn keep(device: &Device, back: Com, size: [i32; 2], chain: Com, dirty: &[RECT]) {
if !crate::gpu_windows::tests::faked().keep {
return;
}
let mut hwnd = 0;
unsafe { function::<unsafe extern "system" fn(Com, *mut HANDLE) -> i32>(chain, 20)(chain, &mut hwnd) };
let [width, height] = size.map(|side| side as u32);
let described = TextureDescription {
width,
height,
mip_levels: 1,
array_size: 1,
format: B8G8R8A8_UNORM,
sample_count: 1,
sample_quality: 0,
usage: 3,
bind_flags: 0,
cpu_access_flags: 0x20000,
misc_flags: 0,
};
let (mut staging, mut mapped) = (null_mut(), Pixels { first: null(), row_pitch: 0, slice_pitch: 0 });
type MakeTexture = unsafe extern "system" fn(Com, *const TextureDescription, *const c_void, *mut Com) -> i32;
unsafe {
assert!(function::<MakeTexture>(device.device, 5)(device.device, &described, null(), &mut staging) >= 0);
function::<unsafe extern "system" fn(Com, Com, Com)>(device.context, 47)(device.context, staging, back);
assert!(function::<unsafe extern "system" fn(Com, Com, u32, u32, u32, *mut Pixels) -> i32>(device.context, 14)(device.context, staging, 0, 1, 0, &mut mapped) >= 0);
let rows = (0..height as usize).map(|row| std::slice::from_raw_parts(mapped.first.cast::<u8>().add(row * mapped.row_pitch as usize).cast::<u32>(), width as usize));
let pixels = rows.flatten().copied().collect();
function::<unsafe extern "system" fn(Com, Com, u32)>(device.context, 15)(device.context, staging, 0);
release(staging);
crate::gpu_windows::tests::kept(hwnd, width as usize, pixels, dirty);
}
}
}