mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-22 00:28:12 +08:00
+1
-3
@@ -32,9 +32,7 @@ mod platform;
|
||||
mod utility;
|
||||
|
||||
#[cfg(feature = "accelerated_paint")]
|
||||
mod texture_import;
|
||||
#[cfg(feature = "accelerated_paint")]
|
||||
use texture_import::SharedTextureHandle;
|
||||
use cef::osr_texture_import::SharedTextureHandle;
|
||||
|
||||
pub(crate) use context::{CefContext, CefContextBuilder, InitError};
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ impl<H: CefEventHandler> ImplRenderHandler for RenderHandlerImpl<H> {
|
||||
|
||||
#[cfg(feature = "accelerated_paint")]
|
||||
fn on_accelerated_paint(&self, _browser: Option<&mut Browser>, type_: PaintElementType, _dirty_rect_count: usize, _dirty_rects: Option<&Rect>, info: Option<&cef::AcceleratedPaintInfo>) {
|
||||
use crate::cef::texture_import::shared_texture_handle::SharedTextureHandle;
|
||||
use cef::osr_texture_import::SharedTextureHandle;
|
||||
|
||||
if type_ != PaintElementType::default() {
|
||||
return;
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
//! Common utilities and traits for texture import across platforms
|
||||
|
||||
use crate::cef::texture_import::*;
|
||||
use cef::sys::cef_color_type_t;
|
||||
use wgpu::Device;
|
||||
|
||||
/// Common format conversion utilities
|
||||
pub mod format {
|
||||
use super::*;
|
||||
|
||||
/// Convert CEF color type to wgpu texture format
|
||||
pub fn cef_to_wgpu(format: cef_color_type_t) -> Result<wgpu::TextureFormat, TextureImportError> {
|
||||
match format {
|
||||
cef_color_type_t::CEF_COLOR_TYPE_BGRA_8888 => Ok(wgpu::TextureFormat::Bgra8UnormSrgb),
|
||||
cef_color_type_t::CEF_COLOR_TYPE_RGBA_8888 => Ok(wgpu::TextureFormat::Rgba8UnormSrgb),
|
||||
_ => Err(TextureImportError::UnsupportedFormat { format }),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
/// Convert CEF color type to Vulkan format
|
||||
pub fn cef_to_vulkan(format: cef_color_type_t) -> Result<ash::vk::Format, TextureImportError> {
|
||||
match format {
|
||||
cef_color_type_t::CEF_COLOR_TYPE_BGRA_8888 => Ok(ash::vk::Format::B8G8R8A8_UNORM),
|
||||
cef_color_type_t::CEF_COLOR_TYPE_RGBA_8888 => Ok(ash::vk::Format::R8G8B8A8_UNORM),
|
||||
_ => Err(TextureImportError::UnsupportedFormat { format }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Common texture creation utilities
|
||||
pub mod texture {
|
||||
use super::*;
|
||||
|
||||
/// Create a fallback CPU texture with the given dimensions and format
|
||||
pub fn create_fallback(device: &Device, width: u32, height: u32, format: cef_color_type_t, label: &str) -> TextureImportResult {
|
||||
let wgpu_format = format::cef_to_wgpu(format)?;
|
||||
|
||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some(label),
|
||||
size: wgpu::Extent3d {
|
||||
width,
|
||||
height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu_format,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||
view_formats: &[],
|
||||
});
|
||||
|
||||
tracing::warn!(
|
||||
"Using fallback CPU texture for CEF rendering ({}x{}, {:?}) - hardware acceleration failed or unavailable. Consider checking GPU driver support.",
|
||||
width,
|
||||
height,
|
||||
format
|
||||
);
|
||||
Ok(texture)
|
||||
}
|
||||
}
|
||||
|
||||
/// Common Vulkan utilities
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
pub mod vulkan {
|
||||
use super::*;
|
||||
use ash::vk;
|
||||
|
||||
/// Find a suitable memory type index for Vulkan allocation
|
||||
pub fn find_memory_type_index(type_filter: u32, properties: vk::MemoryPropertyFlags, mem_properties: &vk::PhysicalDeviceMemoryProperties) -> Option<u32> {
|
||||
(0..mem_properties.memory_type_count).find(|&i| (type_filter & (1 << i)) != 0 && mem_properties.memory_types[i as usize].property_flags.contains(properties))
|
||||
}
|
||||
|
||||
/// Check if the wgpu device is using Vulkan backend
|
||||
pub fn is_vulkan_backend(device: &Device) -> bool {
|
||||
use wgpu::hal::api;
|
||||
let mut is_vulkan = false;
|
||||
unsafe {
|
||||
device.as_hal::<api::Vulkan, _, _>(|device| {
|
||||
is_vulkan = device.is_some();
|
||||
});
|
||||
}
|
||||
is_vulkan
|
||||
}
|
||||
|
||||
/// Check if the wgpu device is using D3D12 backend
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn is_d3d12_backend(device: &Device) -> bool {
|
||||
use wgpu::hal::api;
|
||||
let mut is_d3d12 = false;
|
||||
unsafe {
|
||||
device.as_hal::<api::Dx12, _, _>(|device| {
|
||||
is_d3d12 = device.is_some();
|
||||
});
|
||||
}
|
||||
is_d3d12
|
||||
}
|
||||
}
|
||||
@@ -1,289 +0,0 @@
|
||||
//! Windows D3D11 shared texture import implementation
|
||||
|
||||
use super::common::{format, texture, vulkan};
|
||||
use super::{TextureImportError, TextureImportResult, TextureImporter};
|
||||
use ash::vk;
|
||||
use cef::{AcceleratedPaintInfo, sys::cef_color_type_t};
|
||||
use std::os::raw::c_void;
|
||||
use wgpu::hal::api;
|
||||
|
||||
pub struct D3D11Importer {
|
||||
pub handle: *mut c_void,
|
||||
pub format: cef_color_type_t,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
impl TextureImporter for D3D11Importer {
|
||||
fn new(info: &AcceleratedPaintInfo) -> Self {
|
||||
Self {
|
||||
handle: info.shared_texture_handle,
|
||||
format: *info.format.as_ref(),
|
||||
width: info.extra.coded_size.width as u32,
|
||||
height: info.extra.coded_size.height as u32,
|
||||
}
|
||||
}
|
||||
|
||||
fn import_to_wgpu(&self, device: &wgpu::Device) -> TextureImportResult {
|
||||
// Try hardware acceleration first
|
||||
if self.supports_hardware_acceleration(device) {
|
||||
// Try D3D12 first (most efficient on Windows)
|
||||
if vulkan::is_d3d12_backend(device) {
|
||||
match self.import_via_d3d12(device) {
|
||||
Ok(texture) => {
|
||||
tracing::info!("Successfully imported D3D11 shared texture via D3D12");
|
||||
return Ok(texture);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to import D3D11 via D3D12: {}, trying Vulkan fallback", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Try Vulkan as fallback
|
||||
if vulkan::is_vulkan_backend(device) {
|
||||
match self.import_via_vulkan(device) {
|
||||
Ok(texture) => {
|
||||
tracing::info!("Successfully imported D3D11 shared texture via Vulkan");
|
||||
return Ok(texture);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to import D3D11 via Vulkan: {}, falling back to CPU texture", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to CPU texture
|
||||
texture::create_fallback(device, self.width, self.height, self.format, "CEF D3D11 Texture (fallback)")
|
||||
}
|
||||
|
||||
fn supports_hardware_acceleration(&self, device: &wgpu::Device) -> bool {
|
||||
// Check if handle is valid
|
||||
if self.handle.is_null() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if wgpu is using D3D12 or Vulkan backend
|
||||
vulkan::is_d3d12_backend(device) || vulkan::is_vulkan_backend(device)
|
||||
}
|
||||
}
|
||||
|
||||
impl D3D11Importer {
|
||||
fn import_via_d3d12(&self, device: &wgpu::Device) -> TextureImportResult {
|
||||
// Get wgpu's D3D12 device
|
||||
use wgpu::hal::api;
|
||||
let hal_texture = unsafe {
|
||||
device.as_hal::<api::Dx12, _, _>(|device| {
|
||||
let Some(device) = device else {
|
||||
return Err(TextureImportError::HardwareUnavailable {
|
||||
reason: "Device is not using D3D12 backend".to_string(),
|
||||
});
|
||||
};
|
||||
|
||||
// Import D3D11 shared handle directly into D3D12 resource
|
||||
let d3d12_resource = self.import_d3d11_handle_to_d3d12(device)?;
|
||||
|
||||
// Wrap D3D12 resource in wgpu-hal texture
|
||||
let hal_texture = <api::Dx12 as wgpu::hal::Api>::Device::texture_from_raw(
|
||||
d3d12_resource,
|
||||
format::cef_to_wgpu(self.format)?,
|
||||
wgpu::TextureDimension::D2,
|
||||
wgpu::Extent3d {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
1, // mip_level_count
|
||||
1, // sample_count
|
||||
);
|
||||
|
||||
Ok(hal_texture)
|
||||
})
|
||||
}?;
|
||||
|
||||
// Import hal texture into wgpu
|
||||
let texture = unsafe {
|
||||
device.create_texture_from_hal::<api::Dx12>(
|
||||
hal_texture,
|
||||
&wgpu::TextureDescriptor {
|
||||
label: Some("CEF D3D11→D3D12 Shared Texture"),
|
||||
size: wgpu::Extent3d {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: format::cef_to_wgpu(self.format)?,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING,
|
||||
view_formats: &[],
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
Ok(texture)
|
||||
}
|
||||
|
||||
fn import_via_vulkan(&self, device: &wgpu::Device) -> TextureImportResult {
|
||||
// Get wgpu's Vulkan instance and device
|
||||
use wgpu::{TextureUses, wgc::api::Vulkan};
|
||||
let hal_texture = unsafe {
|
||||
device.as_hal::<api::Vulkan, _, _>(|device| {
|
||||
let Some(device) = device else {
|
||||
return Err(TextureImportError::HardwareUnavailable {
|
||||
reason: "Device is not using Vulkan backend".to_string(),
|
||||
});
|
||||
};
|
||||
|
||||
// Import D3D11 shared handle into Vulkan
|
||||
let vk_image = self.import_d3d11_handle_to_vulkan(device)?;
|
||||
|
||||
// Wrap VkImage in wgpu-hal texture
|
||||
let hal_texture = <api::Vulkan as wgpu::hal::Api>::Device::texture_from_raw(
|
||||
vk_image,
|
||||
&wgpu::hal::TextureDescriptor {
|
||||
label: Some("CEF D3D11 Shared Texture"),
|
||||
size: wgpu::Extent3d {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: format::cef_to_wgpu(self.format)?,
|
||||
usage: TextureUses::COPY_DST | TextureUses::RESOURCE,
|
||||
memory_flags: wgpu::hal::MemoryFlags::empty(),
|
||||
view_formats: vec![],
|
||||
},
|
||||
None, // drop_callback
|
||||
);
|
||||
|
||||
Ok(hal_texture)
|
||||
})
|
||||
}?;
|
||||
|
||||
// Import hal texture into wgpu
|
||||
let texture = unsafe {
|
||||
device.create_texture_from_hal::<Vulkan>(
|
||||
hal_texture,
|
||||
&wgpu::TextureDescriptor {
|
||||
label: Some("CEF D3D11 Shared Texture"),
|
||||
size: wgpu::Extent3d {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: format::cef_to_wgpu(self.format)?,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING,
|
||||
view_formats: &[],
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
Ok(texture)
|
||||
}
|
||||
|
||||
fn import_d3d11_handle_to_vulkan(&self, hal_device: &<api::Vulkan as wgpu::hal::Api>::Device) -> Result<vk::Image, TextureImportError> {
|
||||
// Get raw Vulkan handles
|
||||
let device = hal_device.raw_device();
|
||||
let _instance = hal_device.shared_instance().raw_instance();
|
||||
|
||||
// Validate dimensions
|
||||
if self.width == 0 || self.height == 0 {
|
||||
return Err(TextureImportError::InvalidHandle("Invalid D3D11 texture dimensions".to_string()));
|
||||
}
|
||||
|
||||
// Create external memory image info
|
||||
let mut external_memory_info = vk::ExternalMemoryImageCreateInfo::default().handle_types(vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE);
|
||||
|
||||
// Create image create info
|
||||
let image_create_info = vk::ImageCreateInfo::default()
|
||||
.image_type(vk::ImageType::TYPE_2D)
|
||||
.format(format::cef_to_vulkan(self.format)?)
|
||||
.extent(vk::Extent3D {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth: 1,
|
||||
})
|
||||
.mip_levels(1)
|
||||
.array_layers(1)
|
||||
.samples(vk::SampleCountFlags::TYPE_1)
|
||||
.tiling(vk::ImageTiling::OPTIMAL)
|
||||
.usage(vk::ImageUsageFlags::SAMPLED | vk::ImageUsageFlags::COLOR_ATTACHMENT)
|
||||
.sharing_mode(vk::SharingMode::EXCLUSIVE)
|
||||
.push_next(&mut external_memory_info);
|
||||
|
||||
// Create the image
|
||||
let image = unsafe {
|
||||
device.create_image(&image_create_info, None).map_err(|e| TextureImportError::VulkanError {
|
||||
operation: format!("Failed to create Vulkan image: {:?}", e),
|
||||
})?
|
||||
};
|
||||
|
||||
// Get memory requirements
|
||||
let memory_requirements = unsafe { device.get_image_memory_requirements(image) };
|
||||
|
||||
// Import D3D11 handle
|
||||
let mut import_memory_win32 = vk::ImportMemoryWin32HandleInfoKHR::default()
|
||||
.handle_type(vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE)
|
||||
.handle(self.handle as isize);
|
||||
|
||||
// Find a suitable memory type
|
||||
let memory_properties = unsafe { hal_device.shared_instance().raw_instance().get_physical_device_memory_properties(hal_device.raw_physical_device()) };
|
||||
|
||||
let memory_type_index =
|
||||
vulkan::find_memory_type_index(memory_requirements.memory_type_bits, vk::MemoryPropertyFlags::empty(), &memory_properties).ok_or_else(|| TextureImportError::VulkanError {
|
||||
operation: "Failed to find suitable memory type for D3D11 texture".to_string(),
|
||||
})?;
|
||||
|
||||
let allocate_info = vk::MemoryAllocateInfo::default()
|
||||
.allocation_size(memory_requirements.size)
|
||||
.memory_type_index(memory_type_index)
|
||||
.push_next(&mut import_memory_win32);
|
||||
|
||||
let device_memory = unsafe {
|
||||
device.allocate_memory(&allocate_info, None).map_err(|e| TextureImportError::VulkanError {
|
||||
operation: format!("Failed to allocate memory for D3D11 texture: {:?}", e),
|
||||
})?
|
||||
};
|
||||
|
||||
// Bind memory to image
|
||||
unsafe {
|
||||
device.bind_image_memory(image, device_memory, 0).map_err(|e| TextureImportError::VulkanError {
|
||||
operation: format!("Failed to bind memory to image: {:?}", e),
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(image)
|
||||
}
|
||||
|
||||
fn import_d3d11_handle_to_d3d12(&self, hal_device: &<wgpu::hal::api::Dx12 as wgpu::hal::Api>::Device) -> Result<windows::Win32::Graphics::Direct3D12::ID3D12Resource, TextureImportError> {
|
||||
use windows::Win32::Graphics::Direct3D12::*;
|
||||
|
||||
// Get D3D12 device from wgpu-hal
|
||||
let d3d12_device = hal_device.raw_device();
|
||||
|
||||
// Validate dimensions
|
||||
if self.width == 0 || self.height == 0 {
|
||||
return Err(TextureImportError::InvalidHandle("Invalid D3D11 texture dimensions".to_string()));
|
||||
}
|
||||
|
||||
// Open D3D11 shared handle on D3D12 device
|
||||
unsafe {
|
||||
let mut shared_resource: Option<ID3D12Resource> = None;
|
||||
d3d12_device
|
||||
.OpenSharedHandle(windows::Win32::Foundation::HANDLE(self.handle), &mut shared_resource)
|
||||
.map_err(|e| TextureImportError::PlatformError {
|
||||
message: format!("Failed to open D3D11 shared handle on D3D12: {:?}", e),
|
||||
})?;
|
||||
|
||||
shared_resource.ok_or_else(|| TextureImportError::InvalidHandle("Failed to get D3D12 resource from shared handle".to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,273 +0,0 @@
|
||||
//! Linux DMA-BUF texture import implementation
|
||||
|
||||
use super::common::{format, texture, vulkan};
|
||||
use super::{TextureImportError, TextureImportResult, TextureImporter};
|
||||
use ash::vk;
|
||||
use cef::{AcceleratedPaintInfo, sys::cef_color_type_t};
|
||||
use wgpu::hal::api;
|
||||
|
||||
pub(crate) struct DmaBufImporter {
|
||||
fds: Vec<std::os::fd::RawFd>,
|
||||
format: cef_color_type_t,
|
||||
modifier: u64,
|
||||
width: u32,
|
||||
height: u32,
|
||||
strides: Vec<u32>,
|
||||
offsets: Vec<u32>,
|
||||
}
|
||||
|
||||
impl TextureImporter for DmaBufImporter {
|
||||
fn new(info: &AcceleratedPaintInfo) -> Self {
|
||||
Self {
|
||||
fds: extract_fds_from_info(info),
|
||||
format: *info.format.as_ref(),
|
||||
modifier: info.modifier,
|
||||
width: info.extra.coded_size.width as u32,
|
||||
height: info.extra.coded_size.height as u32,
|
||||
strides: extract_strides_from_info(info),
|
||||
offsets: extract_offsets_from_info(info),
|
||||
}
|
||||
}
|
||||
|
||||
fn import_to_wgpu(&self, device: &wgpu::Device) -> TextureImportResult {
|
||||
// Try hardware acceleration first
|
||||
if self.supports_hardware_acceleration(device) {
|
||||
match self.import_via_vulkan(device) {
|
||||
Ok(texture) => {
|
||||
tracing::info!("Successfully imported DMA-BUF texture via Vulkan");
|
||||
return Ok(texture);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to import DMA-BUF via Vulkan: {}, falling back to CPU texture", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to CPU texture
|
||||
texture::create_fallback(device, self.width, self.height, self.format, "CEF DMA-BUF Texture (fallback)")
|
||||
}
|
||||
|
||||
fn supports_hardware_acceleration(&self, device: &wgpu::Device) -> bool {
|
||||
// Check if we have valid file descriptors
|
||||
if self.fds.is_empty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
for &fd in &self.fds {
|
||||
if fd < 0 {
|
||||
return false;
|
||||
}
|
||||
// Check if file descriptor is valid
|
||||
let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
|
||||
if flags == -1 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if wgpu is using Vulkan backend
|
||||
vulkan::is_vulkan_backend(device)
|
||||
}
|
||||
}
|
||||
|
||||
impl DmaBufImporter {
|
||||
fn import_via_vulkan(&self, device: &wgpu::Device) -> TextureImportResult {
|
||||
// Get wgpu's Vulkan instance and device
|
||||
use wgpu::{TextureUses, wgc::api::Vulkan};
|
||||
let hal_texture = unsafe {
|
||||
device.as_hal::<api::Vulkan, _, _>(|device| {
|
||||
let Some(device) = device else {
|
||||
return Err(TextureImportError::HardwareUnavailable {
|
||||
reason: "Device is not using Vulkan backend".to_string(),
|
||||
});
|
||||
};
|
||||
|
||||
// Create VkImage from DMA-BUF using external memory
|
||||
let vk_image = self.create_vulkan_image_from_dmabuf(device)?;
|
||||
|
||||
// Wrap VkImage in wgpu-hal texture
|
||||
let hal_texture = <api::Vulkan as wgpu::hal::Api>::Device::texture_from_raw(
|
||||
vk_image,
|
||||
&wgpu::hal::TextureDescriptor {
|
||||
label: Some("CEF DMA-BUF Texture"),
|
||||
size: wgpu::Extent3d {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: format::cef_to_wgpu(self.format)?,
|
||||
usage: TextureUses::COPY_DST | TextureUses::RESOURCE,
|
||||
memory_flags: wgpu::hal::MemoryFlags::empty(),
|
||||
view_formats: vec![],
|
||||
},
|
||||
None, // drop_callback
|
||||
);
|
||||
|
||||
Ok(hal_texture)
|
||||
})
|
||||
}?;
|
||||
|
||||
// Import hal texture into wgpu
|
||||
let texture = unsafe {
|
||||
device.create_texture_from_hal::<Vulkan>(
|
||||
hal_texture,
|
||||
&wgpu::TextureDescriptor {
|
||||
label: Some("CEF DMA-BUF Texture"),
|
||||
size: wgpu::Extent3d {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: format::cef_to_wgpu(self.format)?,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING,
|
||||
view_formats: &[],
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
Ok(texture)
|
||||
}
|
||||
|
||||
fn create_vulkan_image_from_dmabuf(&self, hal_device: &<api::Vulkan as wgpu::hal::Api>::Device) -> Result<vk::Image, TextureImportError> {
|
||||
// Get raw Vulkan handles
|
||||
let device = hal_device.raw_device();
|
||||
let _instance = hal_device.shared_instance().raw_instance();
|
||||
|
||||
// Validate dimensions
|
||||
if self.width == 0 || self.height == 0 {
|
||||
return Err(TextureImportError::InvalidHandle("Invalid DMA-BUF dimensions".to_string()));
|
||||
}
|
||||
|
||||
// Create external memory image
|
||||
let image_create_info = vk::ImageCreateInfo::default()
|
||||
.image_type(vk::ImageType::TYPE_2D)
|
||||
.format(format::cef_to_vulkan(self.format)?)
|
||||
.extent(vk::Extent3D {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth: 1,
|
||||
})
|
||||
.mip_levels(1)
|
||||
.array_layers(1)
|
||||
.samples(vk::SampleCountFlags::TYPE_1)
|
||||
.tiling(vk::ImageTiling::DRM_FORMAT_MODIFIER_EXT)
|
||||
.usage(vk::ImageUsageFlags::SAMPLED | vk::ImageUsageFlags::COLOR_ATTACHMENT)
|
||||
.sharing_mode(vk::SharingMode::EXCLUSIVE);
|
||||
|
||||
// Set up DRM format modifier
|
||||
let plane_layouts = self.create_subresource_layouts()?;
|
||||
let mut drm_format_modifier = vk::ImageDrmFormatModifierExplicitCreateInfoEXT::default()
|
||||
.drm_format_modifier(self.modifier)
|
||||
.plane_layouts(&plane_layouts);
|
||||
|
||||
let image_create_info = image_create_info.push_next(&mut drm_format_modifier);
|
||||
|
||||
// Create the image
|
||||
let image = unsafe {
|
||||
device.create_image(&image_create_info, None).map_err(|e| TextureImportError::VulkanError {
|
||||
operation: format!("Failed to create Vulkan image: {e:?}"),
|
||||
})?
|
||||
};
|
||||
|
||||
// Import memory from DMA-BUF
|
||||
let memory_requirements = unsafe { device.get_image_memory_requirements(image) };
|
||||
|
||||
// Duplicate the file descriptor to avoid ownership issues
|
||||
let dup_fd = unsafe { libc::dup(self.fds[0]) };
|
||||
if dup_fd == -1 {
|
||||
return Err(TextureImportError::PlatformError {
|
||||
message: "Failed to duplicate DMA-BUF file descriptor".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
let mut import_memory_fd = vk::ImportMemoryFdInfoKHR::default().handle_type(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT).fd(dup_fd);
|
||||
|
||||
// Find a suitable memory type
|
||||
let memory_properties = unsafe { hal_device.shared_instance().raw_instance().get_physical_device_memory_properties(hal_device.raw_physical_device()) };
|
||||
|
||||
let memory_type_index =
|
||||
vulkan::find_memory_type_index(memory_requirements.memory_type_bits, vk::MemoryPropertyFlags::empty(), &memory_properties).ok_or_else(|| TextureImportError::VulkanError {
|
||||
operation: "Failed to find suitable memory type for DMA-BUF".to_string(),
|
||||
})?;
|
||||
|
||||
let allocate_info = vk::MemoryAllocateInfo::default()
|
||||
.allocation_size(memory_requirements.size)
|
||||
.memory_type_index(memory_type_index)
|
||||
.push_next(&mut import_memory_fd);
|
||||
|
||||
let device_memory = unsafe {
|
||||
device.allocate_memory(&allocate_info, None).map_err(|e| TextureImportError::VulkanError {
|
||||
operation: format!("Failed to allocate memory for DMA-BUF: {e:?}"),
|
||||
})?
|
||||
};
|
||||
|
||||
// Bind memory to image
|
||||
unsafe {
|
||||
device.bind_image_memory(image, device_memory, 0).map_err(|e| TextureImportError::VulkanError {
|
||||
operation: format!("Failed to bind memory to image: {e:?}"),
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(image)
|
||||
}
|
||||
|
||||
fn create_subresource_layouts(&self) -> Result<Vec<vk::SubresourceLayout>, TextureImportError> {
|
||||
let mut layouts = Vec::new();
|
||||
|
||||
for i in 0..self.fds.len() {
|
||||
layouts.push(vk::SubresourceLayout {
|
||||
offset: self.offsets.get(i).copied().unwrap_or(0) as u64,
|
||||
size: 0, // Will be calculated by driver
|
||||
row_pitch: self.strides.get(i).copied().unwrap_or(0) as u64,
|
||||
array_pitch: 0,
|
||||
depth_pitch: 0,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(layouts)
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_fds_from_info(info: &cef::AcceleratedPaintInfo) -> Vec<std::os::fd::RawFd> {
|
||||
let plane_count = info.plane_count as usize;
|
||||
let mut fds = Vec::with_capacity(plane_count);
|
||||
|
||||
for i in 0..plane_count {
|
||||
if let Some(plane) = info.planes.get(i) {
|
||||
fds.push(plane.fd);
|
||||
}
|
||||
}
|
||||
|
||||
fds
|
||||
}
|
||||
|
||||
fn extract_strides_from_info(info: &cef::AcceleratedPaintInfo) -> Vec<u32> {
|
||||
let plane_count = info.plane_count as usize;
|
||||
let mut strides = Vec::with_capacity(plane_count);
|
||||
|
||||
for i in 0..plane_count {
|
||||
if let Some(plane) = info.planes.get(i) {
|
||||
strides.push(plane.stride);
|
||||
}
|
||||
}
|
||||
|
||||
strides
|
||||
}
|
||||
|
||||
fn extract_offsets_from_info(info: &cef::AcceleratedPaintInfo) -> Vec<u32> {
|
||||
let plane_count = info.plane_count as usize;
|
||||
let mut offsets = Vec::with_capacity(plane_count);
|
||||
|
||||
for i in 0..plane_count {
|
||||
if let Some(plane) = info.planes.get(i) {
|
||||
offsets.push(plane.offset as u32);
|
||||
}
|
||||
}
|
||||
|
||||
offsets
|
||||
}
|
||||
@@ -1,190 +0,0 @@
|
||||
//! macOS IOSurface texture import implementation
|
||||
|
||||
use super::common::{format, texture};
|
||||
use super::{TextureImportError, TextureImportResult, TextureImporter};
|
||||
use cef::{AcceleratedPaintInfo, sys::cef_color_type_t};
|
||||
use metal::foreign_types::ForeignType;
|
||||
use metal::{MTLPixelFormat, MTLTextureType, MTLTextureUsage, Texture};
|
||||
use std::os::raw::c_void;
|
||||
use wgpu::hal::api;
|
||||
|
||||
pub struct IOSurfaceImporter {
|
||||
pub handle: *mut c_void,
|
||||
pub format: cef_color_type_t,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
impl TextureImporter for IOSurfaceImporter {
|
||||
fn new(info: &AcceleratedPaintInfo) -> Self {
|
||||
Self {
|
||||
handle: info.shared_texture_io_surface,
|
||||
format: *info.format.as_ref(),
|
||||
width: info.extra.coded_size.width as u32,
|
||||
height: info.extra.coded_size.height as u32,
|
||||
}
|
||||
}
|
||||
|
||||
fn import_to_wgpu(&self, device: &wgpu::Device) -> TextureImportResult {
|
||||
// Try hardware acceleration first
|
||||
if self.supports_hardware_acceleration(device) {
|
||||
match self.import_via_metal(device) {
|
||||
Ok(texture) => {
|
||||
tracing::trace!("Successfully imported IOSurface texture via Metal");
|
||||
return Ok(texture);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to import IOSurface via Metal: {}, falling back to CPU texture", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to CPU texture
|
||||
texture::create_fallback(device, self.width, self.height, self.format, "CEF IOSurface Texture (fallback)")
|
||||
}
|
||||
|
||||
fn supports_hardware_acceleration(&self, device: &wgpu::Device) -> bool {
|
||||
// Check if handle is valid
|
||||
if self.handle.is_null() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if wgpu is using Metal backend
|
||||
self.is_metal_backend(device)
|
||||
}
|
||||
}
|
||||
|
||||
impl IOSurfaceImporter {
|
||||
fn import_via_metal(&self, device: &wgpu::Device) -> TextureImportResult {
|
||||
// Get wgpu's Metal device
|
||||
use wgpu::wgc::api::Metal;
|
||||
let hal_texture = unsafe {
|
||||
device.as_hal::<api::Metal, _, _>(|hal_device| {
|
||||
let Some(hal_device) = hal_device else {
|
||||
return Err(TextureImportError::HardwareUnavailable {
|
||||
reason: "Device is not using Metal backend".to_string(),
|
||||
});
|
||||
};
|
||||
|
||||
// Import IOSurface handle into Metal texture
|
||||
let metal_texture = self.import_iosurface_to_metal_texture(hal_device)?;
|
||||
|
||||
// Wrap Metal texture in wgpu-hal texture
|
||||
// texture_from_raw signature: (texture, format, texture_type, mip_levels, sample_count, copy_extent)
|
||||
let hal_texture = <api::Metal as wgpu::hal::Api>::Device::texture_from_raw(
|
||||
metal_texture,
|
||||
format::cef_to_wgpu(self.format)?,
|
||||
MTLTextureType::D2,
|
||||
1, // mip_level_count
|
||||
1, // sample_count
|
||||
wgpu::hal::CopyExtent {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth: 1,
|
||||
},
|
||||
);
|
||||
|
||||
Ok(hal_texture)
|
||||
})
|
||||
}?;
|
||||
|
||||
// Import hal texture into wgpu
|
||||
let texture = unsafe {
|
||||
device.create_texture_from_hal::<Metal>(
|
||||
hal_texture,
|
||||
&wgpu::TextureDescriptor {
|
||||
label: Some("CEF IOSurface Texture"),
|
||||
size: wgpu::Extent3d {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: format::cef_to_wgpu(self.format)?,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING,
|
||||
view_formats: &[],
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
Ok(texture)
|
||||
}
|
||||
|
||||
fn import_iosurface_to_metal_texture(&self, hal_device: &<api::Metal as wgpu::hal::Api>::Device) -> Result<Texture, TextureImportError> {
|
||||
// Validate dimensions
|
||||
if self.width == 0 || self.height == 0 {
|
||||
return Err(TextureImportError::InvalidHandle("Invalid IOSurface texture dimensions".to_string()));
|
||||
}
|
||||
|
||||
// Get the Metal device from wgpu-hal
|
||||
let metal_device = hal_device.raw_device();
|
||||
|
||||
// Convert CEF format to Metal pixel format
|
||||
let metal_format = self.cef_to_metal_format(self.format)?;
|
||||
|
||||
// Create Metal texture from IOSurface using objc runtime
|
||||
// We need to use raw objc because the metal crate doesn't expose IOSurface creation directly
|
||||
#[allow(unexpected_cfgs)] // Suppress objc crate internal cfg warnings
|
||||
unsafe {
|
||||
use objc::runtime::Object;
|
||||
use objc::{class, msg_send, sel, sel_impl};
|
||||
|
||||
let iosurface = self.handle;
|
||||
|
||||
// Create texture descriptor using NSObject/Objective-C
|
||||
let descriptor_class = class!(MTLTextureDescriptor);
|
||||
let descriptor: *mut Object = msg_send![descriptor_class, new];
|
||||
|
||||
// Set descriptor properties
|
||||
let _: () = msg_send![descriptor, setTextureType: MTLTextureType::D2];
|
||||
let _: () = msg_send![descriptor, setPixelFormat: metal_format];
|
||||
let _: () = msg_send![descriptor, setWidth: self.width as u64];
|
||||
let _: () = msg_send![descriptor, setHeight: self.height as u64];
|
||||
let _: () = msg_send![descriptor, setDepth: 1u64];
|
||||
let _: () = msg_send![descriptor, setMipmapLevelCount: 1u64];
|
||||
let _: () = msg_send![descriptor, setSampleCount: 1u64];
|
||||
let _: () = msg_send![descriptor, setArrayLength: 1u64];
|
||||
let _: () = msg_send![descriptor, setUsage: MTLTextureUsage::ShaderRead.bits()];
|
||||
|
||||
// Get device pointer
|
||||
let device_ptr = metal_device.lock().as_ptr();
|
||||
|
||||
// Call newTextureWithDescriptor:iosurface:plane:
|
||||
let metal_texture: *mut Object = msg_send![device_ptr, newTextureWithDescriptor:descriptor iosurface:iosurface plane:0u64];
|
||||
|
||||
// Release the descriptor
|
||||
let _: () = msg_send![descriptor, release];
|
||||
|
||||
if metal_texture.is_null() {
|
||||
return Err(TextureImportError::PlatformError {
|
||||
message: "Failed to create Metal texture from IOSurface".to_string(),
|
||||
});
|
||||
}
|
||||
|
||||
// Cast to correct type and wrap in metal::Texture
|
||||
let mtl_texture = metal_texture as *mut metal::MTLTexture;
|
||||
Ok(Texture::from_ptr(mtl_texture))
|
||||
}
|
||||
}
|
||||
|
||||
fn cef_to_metal_format(&self, format: cef_color_type_t) -> Result<MTLPixelFormat, TextureImportError> {
|
||||
match format {
|
||||
cef_color_type_t::CEF_COLOR_TYPE_BGRA_8888 => Ok(MTLPixelFormat::BGRA8Unorm_sRGB),
|
||||
cef_color_type_t::CEF_COLOR_TYPE_RGBA_8888 => Ok(MTLPixelFormat::RGBA8Unorm_sRGB),
|
||||
_ => Err(TextureImportError::UnsupportedFormat { format }),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_metal_backend(&self, device: &wgpu::Device) -> bool {
|
||||
use wgpu::hal::api;
|
||||
let mut is_metal = false;
|
||||
unsafe {
|
||||
device.as_hal::<api::Metal, _, _>(|device| {
|
||||
is_metal = device.is_some();
|
||||
});
|
||||
}
|
||||
is_metal
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
//! Unified texture import system for CEF hardware acceleration
|
||||
//!
|
||||
//! This module provides a platform-agnostic interface for importing shared textures
|
||||
//! from CEF into wgpu, with automatic fallback to CPU textures when hardware
|
||||
//! acceleration is not available.
|
||||
//!
|
||||
//! # Supported Platforms
|
||||
//!
|
||||
//! - **Linux**: DMA-BUF via Vulkan external memory
|
||||
//! - **Windows**: D3D11 shared textures via Vulkan interop
|
||||
//! - **macOS**: IOSurface via Metal native API
|
||||
//!
|
||||
//! # Features
|
||||
//!
|
||||
//! - `accelerated_paint` - Base feature for texture import
|
||||
//! - `accelerated_paint_dmabuf` - Linux DMA-BUF support
|
||||
//! - `accelerated_paint_d3d11` - Windows D3D11 support
|
||||
//! - `accelerated_paint_iosurface` - macOS IOSurface support
|
||||
|
||||
pub(crate) mod common;
|
||||
|
||||
pub(crate) mod shared_texture_handle;
|
||||
pub(crate) use shared_texture_handle::SharedTextureHandle;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub(crate) mod dmabuf;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
pub(crate) mod d3d11;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub(crate) mod iosurface;
|
||||
|
||||
/// Result type for texture import operations
|
||||
pub type TextureImportResult = Result<wgpu::Texture, TextureImportError>;
|
||||
|
||||
/// Errors that can occur during texture import
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum TextureImportError {
|
||||
#[error("Invalid texture handle: {0}")]
|
||||
InvalidHandle(String),
|
||||
|
||||
#[error("Unsupported texture format: {format:?}")]
|
||||
UnsupportedFormat { format: cef::sys::cef_color_type_t },
|
||||
|
||||
#[error("Hardware acceleration not available: {reason}")]
|
||||
HardwareUnavailable { reason: String },
|
||||
|
||||
#[error("Vulkan operation failed: {operation}")]
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
VulkanError { operation: String },
|
||||
|
||||
#[error("Platform-specific error: {message}")]
|
||||
PlatformError { message: String },
|
||||
|
||||
#[error("Unsupported platform for texture import")]
|
||||
UnsupportedPlatform,
|
||||
}
|
||||
|
||||
/// Trait for platform-specific texture importers
|
||||
pub trait TextureImporter {
|
||||
fn new(info: &cef::AcceleratedPaintInfo) -> Self;
|
||||
|
||||
/// Import the texture into wgpu, with automatic fallback to CPU texture
|
||||
fn import_to_wgpu(&self, device: &wgpu::Device) -> TextureImportResult;
|
||||
|
||||
/// Check if hardware acceleration is available for this texture
|
||||
fn supports_hardware_acceleration(&self, device: &wgpu::Device) -> bool;
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
use cef::AcceleratedPaintInfo;
|
||||
|
||||
use super::{TextureImportError, TextureImportResult, TextureImporter};
|
||||
|
||||
pub(crate) enum SharedTextureHandle {
|
||||
#[cfg(target_os = "linux")]
|
||||
DmaBuf(super::dmabuf::DmaBufImporter),
|
||||
#[cfg(target_os = "windows")]
|
||||
D3D11(super::d3d11::D3D11Importer),
|
||||
#[cfg(target_os = "macos")]
|
||||
IOSurface(super::iosurface::IOSurfaceImporter),
|
||||
Unsupported,
|
||||
}
|
||||
|
||||
impl SharedTextureHandle {
|
||||
pub(crate) fn new(info: &AcceleratedPaintInfo) -> Self {
|
||||
// Extract DMA-BUF information
|
||||
#[cfg(target_os = "linux")]
|
||||
return Self::DmaBuf(super::dmabuf::DmaBufImporter::new(info));
|
||||
|
||||
// Extract D3D11 shared handle with texture metadata
|
||||
#[cfg(target_os = "windows")]
|
||||
return Self::D3D11(super::d3d11::D3D11Importer::new(info));
|
||||
|
||||
// Extract IOSurface handle with texture metadata
|
||||
#[cfg(target_os = "macos")]
|
||||
return Self::IOSurface(super::iosurface::IOSurfaceImporter::new(info));
|
||||
|
||||
#[allow(unreachable_code)]
|
||||
Self::Unsupported
|
||||
}
|
||||
|
||||
/// Import a texture using the appropriate platform-specific importer
|
||||
pub(crate) fn import_texture(self, device: &wgpu::Device) -> TextureImportResult {
|
||||
match self {
|
||||
#[cfg(target_os = "linux")]
|
||||
SharedTextureHandle::DmaBuf(importer) => importer.import_to_wgpu(device),
|
||||
#[cfg(target_os = "windows")]
|
||||
SharedTextureHandle::D3D11(importer) => importer.import_to_wgpu(device),
|
||||
#[cfg(target_os = "macos")]
|
||||
SharedTextureHandle::IOSurface(importer) => importer.import_to_wgpu(device),
|
||||
SharedTextureHandle::Unsupported => Err(TextureImportError::UnsupportedPlatform),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -250,6 +250,7 @@ impl GraphicsState {
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color { r: 0.01, g: 0.01, b: 0.01, a: 1.0 }),
|
||||
store: wgpu::StoreOp::Store,
|
||||
},
|
||||
depth_slice: None,
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
occlusion_query_set: None,
|
||||
|
||||
Reference in New Issue
Block a user