Files
qbz/crates/vendor/i-slint-renderer-femtovg-1.16.1/wgpu.rs
T
vicrodh fe111c3ff6 feat: renderer tier auto-detect with Settings override and auto-revert
Weak-GPU hardware (Raspberry Pi 4 / v3dv, Haswell / hasvk) exposes a
real Vulkan adapter, so the old binary wgpu-or-software detection handed
it the slowest possible path: femtovg-wgpu over an immature Vulkan
driver. Mesa's GL path is the fast one on that class of hardware.

- Three-tier chain: wgpu -> femtovg GL -> software. Auto-detect
  classifies adapters (weak-driver markers incl. hasvk, integrated-on-arm,
  tiny texture limits); adapters only reachable through wgpu's GL backend
  never select the wgpu tier (the renderer masks Backends::GL out, so
  detection must too). renderer-femtovg (GL) feature now compiled on
  Linux.
- wgpu tier requests PowerPreference::LowPower (WGPU_POWER_PREF still
  wins).
- Per-window swapchain alpha in the vendored femtovg renderer: only the
  miniplayer gets a transparent (blended) swapchain; the main window
  keeps an Opaque one, sparing a full-window compositor blend per frame.
- Settings > Appearance gains a Renderer row (Linux only): Auto
  (default) / GPU / GPU compatibility / Software, persisted in
  ui_prefs.json, applied on restart. QBZ_RENDERER env still overrides
  (now also accepts gl|gles|femtovg).
- Auto-revert sentinel: a non-auto choice is armed before backend init
  and disarmed after the window survives 5s; if the previous start died
  before that, the setting reverts to Auto with an explanatory toast, so
  a bad renderer pick can never lock the user out of Settings.
- Developer tools > Diagnostics now reports the active renderer, why it
  was chosen, and the enumerated GPU adapters (panel + markdown report).

cc
2026-07-01 18:05:28 -06:00

493 lines
18 KiB
Rust

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
use std::{cell::RefCell, pin::Pin, rc::Rc};
use i_slint_core::platform::PlatformError;
use i_slint_core::renderer::RendererSealed;
use i_slint_core::{api::PhysicalSize as PhysicalWindowSize, graphics::RequestedGraphicsAPI};
use crate::{FemtoVGRenderer, GraphicsBackend, WindowSurface};
use wgpu_28 as wgpu;
/// QBZ vendor patch: per-window swapchain alpha opt-in. When true, a
/// `WGPUBackend` constructed via `new_suspended` captures a preference for a
/// non-opaque composite alpha mode (needed for the borderless transparent
/// miniplayer); when false (default) its surfaces stay Opaque/Auto so the
/// compositor does not alpha-blend the whole main window every frame. The app
/// sets this from its winit window-attributes hook, which runs on the event
/// loop thread right before the window ADAPTER — and therefore this backend —
/// is created. The value is captured PER BACKEND at construction rather than
/// read in `set_surface`: `set_surface` re-runs at every winit re-realization
/// (Wayland destroys the window on hide, and even the first realization is
/// deferred to a later event-loop iteration), long after the hook fired, so
/// reading the global there would leak the last-created window's preference
/// into whichever window is (re)realized next.
static SURFACE_PREFERS_TRANSPARENT: core::sync::atomic::AtomicBool =
core::sync::atomic::AtomicBool::new(false);
/// QBZ vendor patch: mark whether `WGPUBackend`s constructed from now on
/// prefer a transparent (non-opaque) composite alpha mode for their surfaces.
pub fn set_surface_prefers_transparent(transparent: bool) {
SURFACE_PREFERS_TRANSPARENT.store(transparent, core::sync::atomic::Ordering::Relaxed);
}
fn preferred_alpha_mode(
modes: &[wgpu::CompositeAlphaMode],
transparent: bool,
) -> wgpu::CompositeAlphaMode {
if transparent {
[
wgpu::CompositeAlphaMode::PreMultiplied,
wgpu::CompositeAlphaMode::PostMultiplied,
wgpu::CompositeAlphaMode::Inherit,
]
.into_iter()
.find(|mode| modes.contains(mode))
.unwrap_or(wgpu::CompositeAlphaMode::Auto)
} else if modes.contains(&wgpu::CompositeAlphaMode::Opaque) {
wgpu::CompositeAlphaMode::Opaque
} else {
// Auto is always valid (wgpu resolves it to Opaque or Inherit) and is
// upstream Slint's behavior, so this is the graceful fallback.
wgpu::CompositeAlphaMode::Auto
}
}
pub struct WGPUBackend {
instance: RefCell<Option<wgpu::Instance>>,
device: RefCell<Option<wgpu::Device>>,
queue: RefCell<Option<wgpu::Queue>>,
surface_config: RefCell<Option<wgpu::SurfaceConfiguration>>,
surface: RefCell<Option<wgpu::Surface<'static>>>,
/// QBZ vendor patch: captured from `SURFACE_PREFERS_TRANSPARENT` at
/// construction — this backend's window keeps the same alpha preference
/// across every surface re-creation.
prefers_transparent: bool,
}
pub struct WGPUWindowSurface {
surface_texture: wgpu::SurfaceTexture,
}
impl WindowSurface<femtovg::renderer::WGPURenderer> for WGPUWindowSurface {
fn render_output(&self) -> impl Into<femtovg::renderer::WGPURenderOutput> {
&self.surface_texture.texture
}
}
impl GraphicsBackend for WGPUBackend {
type Renderer = femtovg::renderer::WGPURenderer;
type WindowSurface = WGPUWindowSurface;
const NAME: &'static str = "WGPU";
fn new_suspended() -> Self {
Self {
instance: Default::default(),
device: Default::default(),
queue: Default::default(),
surface_config: Default::default(),
surface: Default::default(),
prefers_transparent: SURFACE_PREFERS_TRANSPARENT
.load(core::sync::atomic::Ordering::Relaxed),
}
}
fn clear_graphics_context(&self) {
self.surface_config.borrow_mut().take();
self.surface.borrow_mut().take();
self.queue.borrow_mut().take();
self.device.borrow_mut().take();
}
fn begin_surface_rendering(
&self,
) -> Result<Self::WindowSurface, Box<dyn std::error::Error + Send + Sync>> {
let surface = self.surface.borrow();
let surface = surface.as_ref().unwrap();
let frame = match surface.get_current_texture() {
Ok(texture) => texture,
Err(wgpu::SurfaceError::Timeout) => surface.get_current_texture()?,
// Outdated or lost: re-configure and try again
Err(_) => {
let mut device = self.device.borrow_mut();
let device = device.as_mut().unwrap();
surface.configure(device, self.surface_config.borrow().as_ref().unwrap());
surface.get_current_texture()?
}
};
Ok(WGPUWindowSurface { surface_texture: frame })
}
fn submit_commands(&self, commands: <Self::Renderer as femtovg::Renderer>::CommandBuffer) {
self.queue.borrow().as_ref().unwrap().submit(commands);
}
fn present_surface(
&self,
surface: Self::WindowSurface,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
surface.surface_texture.present();
Ok(())
}
#[cfg(feature = "unstable-wgpu-28")]
fn with_graphics_api<R>(
&self,
callback: impl FnOnce(Option<i_slint_core::api::GraphicsAPI<'_>>) -> R,
) -> Result<R, i_slint_core::platform::PlatformError> {
let instance = self.instance.borrow().clone();
let device = self.device.borrow().clone();
let queue = self.queue.borrow().clone();
if let (Some(instance), Some(device), Some(queue)) = (instance, device, queue) {
Ok(callback(Some(i_slint_core::graphics::create_graphics_api_wgpu_28(
instance, device, queue,
))))
} else {
Ok(callback(None))
}
}
#[cfg(not(feature = "unstable-wgpu-28"))]
fn with_graphics_api<R>(
&self,
callback: impl FnOnce(Option<i_slint_core::api::GraphicsAPI<'_>>) -> R,
) -> Result<R, i_slint_core::platform::PlatformError> {
Ok(callback(None))
}
fn resize(
&self,
width: std::num::NonZeroU32,
height: std::num::NonZeroU32,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// Try to get hold of the wgpu types, but if we receive the resize event while suspended, ignore it.
let mut surface_config = self.surface_config.borrow_mut();
let Some(surface_config) = surface_config.as_mut() else { return Ok(()) };
let mut device = self.device.borrow_mut();
let Some(device) = device.as_mut() else { return Ok(()) };
let mut surface = self.surface.borrow_mut();
let Some(surface) = surface.as_mut() else { return Ok(()) };
// Prefer FIFO modes over possible Mailbox setting for frame pacing and better energy efficiency.
surface_config.present_mode = wgpu::PresentMode::AutoVsync;
surface_config.width = width.get();
surface_config.height = height.get();
surface.configure(device, surface_config);
Ok(())
}
}
impl FemtoVGRenderer<WGPUBackend> {
pub fn set_surface(
&self,
surface_target: impl Into<i_slint_core::graphics::wgpu_28::SurfaceTarget>,
size: PhysicalWindowSize,
requested_graphics_api: Option<RequestedGraphicsAPI>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (instance, adapter, device, queue, surface) =
i_slint_core::graphics::wgpu_28::init_instance_adapter_device_queue_surface(
surface_target,
requested_graphics_api,
/* rendering artifacts :( */
wgpu::Backends::GL,
)?;
let mut surface_config =
surface.get_default_config(&adapter, size.width, size.height).unwrap();
let swapchain_capabilities = surface.get_capabilities(&adapter);
let swapchain_format = swapchain_capabilities
.formats
.iter()
.find(|f| {
matches!(f, wgpu::TextureFormat::Rgba8Unorm | wgpu::TextureFormat::Bgra8Unorm)
})
.copied()
.unwrap_or_else(|| swapchain_capabilities.formats[0]);
surface_config.format = swapchain_format;
let wants_transparent = self.graphics_backend.prefers_transparent;
surface_config.alpha_mode =
preferred_alpha_mode(&swapchain_capabilities.alpha_modes, wants_transparent);
i_slint_core::debug_log!(
"[qbz-slint] femtovg-wgpu surface alpha modes: {:?}; transparent={}; selected: {:?}",
swapchain_capabilities.alpha_modes,
wants_transparent,
surface_config.alpha_mode
);
surface.configure(&device, &surface_config);
*self.graphics_backend.instance.borrow_mut() = Some(instance.clone());
*self.graphics_backend.device.borrow_mut() = Some(device.clone());
*self.graphics_backend.queue.borrow_mut() = Some(queue.clone());
*self.graphics_backend.surface_config.borrow_mut() = Some(surface_config);
*self.graphics_backend.surface.borrow_mut() = Some(surface);
let wgpu_renderer = femtovg::renderer::WGPURenderer::new(device, queue);
let femtovg_canvas = femtovg::Canvas::new_with_text_context(
wgpu_renderer,
crate::font_cache::FONT_CACHE.with(|cache| cache.borrow().text_context.clone()),
)
.unwrap();
let canvas = Rc::new(RefCell::new(femtovg_canvas));
self.reset_canvas(canvas);
Ok(())
}
}
struct TextureWindowSurface {
render_output: femtovg::renderer::WGPURenderOutput,
}
impl WindowSurface<femtovg::renderer::WGPURenderer> for TextureWindowSurface {
fn render_output(&self) -> impl Into<femtovg::renderer::WGPURenderOutput> {
self.render_output.clone()
}
}
struct WgpuTextureBackend {
instance: wgpu::Instance,
device: wgpu::Device,
queue: wgpu::Queue,
render_output: RefCell<Option<femtovg::renderer::WGPURenderOutput>>,
}
impl GraphicsBackend for WgpuTextureBackend {
type Renderer = femtovg::renderer::WGPURenderer;
type WindowSurface = TextureWindowSurface;
const NAME: &'static str = "WGPU Texture";
fn new_suspended() -> Self {
panic!("Suspended backend not supported for WgpuTextureBackend (requires device/queue)");
}
fn clear_graphics_context(&self) {
// Nothing to clear here, we don't own the device/queue/texture
}
fn begin_surface_rendering(
&self,
) -> Result<Self::WindowSurface, Box<dyn std::error::Error + Send + Sync>> {
let render_output =
self.render_output.borrow().clone().ok_or("No texture set for rendering")?;
Ok(TextureWindowSurface { render_output })
}
fn submit_commands(&self, commands: <Self::Renderer as femtovg::Renderer>::CommandBuffer) {
self.queue.submit(commands);
}
fn present_surface(
&self,
_surface: Self::WindowSurface,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// No presentation needed - the caller owns the texture and handles presenting it
Ok(())
}
#[cfg(feature = "unstable-wgpu-28")]
fn with_graphics_api<R>(
&self,
callback: impl FnOnce(Option<i_slint_core::api::GraphicsAPI<'_>>) -> R,
) -> Result<R, i_slint_core::platform::PlatformError> {
Ok(callback(Some(i_slint_core::graphics::create_graphics_api_wgpu_28(
self.instance.clone(),
self.device.clone(),
self.queue.clone(),
))))
}
#[cfg(not(feature = "unstable-wgpu-28"))]
fn with_graphics_api<R>(
&self,
callback: impl FnOnce(Option<i_slint_core::api::GraphicsAPI<'_>>) -> R,
) -> Result<R, i_slint_core::platform::PlatformError> {
Ok(callback(None))
}
fn resize(
&self,
_width: std::num::NonZeroU32,
_height: std::num::NonZeroU32,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// No resize needed - texture size is determined by the texture passed to render_to_texture
Ok(())
}
}
/// Use the FemtoVG renderer with WGPU when implementing a custom Slint platform where you want the scene to be rendered
/// into a WGPU texture. The rendering is done using the [FemtoVG](https://github.com/femtovg/femtovg) library.
pub struct FemtoVGWGPURenderer(FemtoVGRenderer<WgpuTextureBackend>);
impl FemtoVGWGPURenderer {
/// Creates a new FemtoVGWGPURenderer.
///
/// The `instance`, `device` and `queue` are the WGPU resources used for rendering.
/// These are also provided to [`Window::set_rendering_notifier()`](i_slint_core::api::Window::set_rendering_notifier) callbacks via [`GraphicsAPI::WGPU28`](i_slint_core::api::GraphicsAPI::WGPU28).
pub fn new(
instance: wgpu::Instance,
device: wgpu::Device,
queue: wgpu::Queue,
) -> Result<Self, PlatformError> {
let backend = WgpuTextureBackend {
instance,
device: device.clone(),
queue: queue.clone(),
render_output: RefCell::new(None),
};
let renderer = FemtoVGRenderer::new_internal(backend);
let wgpu_renderer = femtovg::renderer::WGPURenderer::new(device, queue);
let femtovg_canvas = femtovg::Canvas::new_with_text_context(
wgpu_renderer,
crate::font_cache::FONT_CACHE.with(|cache| cache.borrow().text_context.clone()),
)
.map_err(|e| format!("Failed to create femtovg canvas: {:?}", e))?;
let canvas = Rc::new(RefCell::new(femtovg_canvas));
renderer.reset_canvas(canvas);
Ok(Self(renderer))
}
/// Render the scene to the given texture view, with the specified size and format.
pub fn render_to_texture_view(
&self,
texture_view: &wgpu::TextureView,
width: u32,
height: u32,
format: wgpu::TextureFormat,
) -> Result<(), PlatformError> {
*self.0.graphics_backend.render_output.borrow_mut() =
Some(femtovg::renderer::WGPURenderOutput {
view: texture_view.clone(),
width,
height,
format,
});
let result = self.0.render();
*self.0.graphics_backend.render_output.borrow_mut() = None;
result
}
/// Render the scene to the given texture.
/// This is a convenience method that creates a texture view for the entire texture and calls `render_to_texture_view`.
pub fn render_to_texture(&self, texture: &wgpu::Texture) -> Result<(), PlatformError> {
let size = texture.size();
self.render_to_texture_view(
&texture.create_view(&wgpu::TextureViewDescriptor::default()),
size.width,
size.height,
texture.format(),
)
}
}
#[doc(hidden)]
impl RendererSealed for FemtoVGWGPURenderer {
fn text_size(
&self,
text_item: Pin<&dyn i_slint_core::item_rendering::RenderString>,
item_rc: &i_slint_core::items::ItemRc,
max_width: Option<i_slint_core::lengths::LogicalLength>,
text_wrap: i_slint_core::items::TextWrap,
) -> i_slint_core::lengths::LogicalSize {
self.0.text_size(text_item, item_rc, max_width, text_wrap)
}
fn char_size(
&self,
text_item: Pin<&dyn i_slint_core::item_rendering::HasFont>,
item_rc: &i_slint_core::items::ItemRc,
ch: char,
) -> i_slint_core::lengths::LogicalSize {
self.0.char_size(text_item, item_rc, ch)
}
fn font_metrics(
&self,
font_request: i_slint_core::graphics::FontRequest,
) -> i_slint_core::items::FontMetrics {
self.0.font_metrics(font_request)
}
fn text_input_byte_offset_for_position(
&self,
text_input: Pin<&i_slint_core::items::TextInput>,
item_rc: &i_slint_core::items::ItemRc,
pos: i_slint_core::lengths::LogicalPoint,
) -> usize {
self.0.text_input_byte_offset_for_position(text_input, item_rc, pos)
}
fn text_input_cursor_rect_for_byte_offset(
&self,
text_input: Pin<&i_slint_core::items::TextInput>,
item_rc: &i_slint_core::items::ItemRc,
byte_offset: usize,
) -> i_slint_core::lengths::LogicalRect {
self.0.text_input_cursor_rect_for_byte_offset(text_input, item_rc, byte_offset)
}
fn register_font_from_memory(
&self,
data: &'static [u8],
) -> Result<(), Box<dyn std::error::Error>> {
self.0.register_font_from_memory(data)
}
fn register_font_from_path(
&self,
path: &std::path::Path,
) -> Result<(), Box<dyn std::error::Error>> {
self.0.register_font_from_path(path)
}
fn default_font_size(&self) -> i_slint_core::lengths::LogicalLength {
self.0.default_font_size()
}
fn set_rendering_notifier(
&self,
callback: Box<dyn i_slint_core::api::RenderingNotifier>,
) -> Result<(), i_slint_core::api::SetRenderingNotifierError> {
self.0.set_rendering_notifier(callback)
}
fn free_graphics_resources(
&self,
component: i_slint_core::item_tree::ItemTreeRef,
items: &mut dyn Iterator<Item = Pin<i_slint_core::items::ItemRef<'_>>>,
) -> Result<(), PlatformError> {
self.0.free_graphics_resources(component, items)
}
fn set_window_adapter(&self, window_adapter: &Rc<dyn i_slint_core::window::WindowAdapter>) {
self.0.set_window_adapter(window_adapter)
}
fn window_adapter(&self) -> Option<Rc<dyn i_slint_core::window::WindowAdapter>> {
RendererSealed::window_adapter(&self.0)
}
fn resize(&self, size: i_slint_core::api::PhysicalSize) -> Result<(), PlatformError> {
self.0.resize(size)
}
fn take_snapshot(
&self,
) -> Result<
i_slint_core::graphics::SharedPixelBuffer<i_slint_core::graphics::Rgba8Pixel>,
PlatformError,
> {
self.0.take_snapshot()
}
fn supports_transformations(&self) -> bool {
self.0.supports_transformations()
}
}