Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-renderer/src/platform/mac/rendering/renderer.rs
1use crate::platform::mac::rendering::is_integrated_gpu;
2use crate::platform::mac::window::WindowState;
3use cocoa::base::id;
4use strato_ui_core::rendering::{
5 GPUBackend, GPUDeviceInfo, GPUDeviceType, GPUPowerPreference, OnGPUDeviceSelected,
6};
7use strato_ui_core::{fonts, Scene};
8 
9/// Trait to render the [`Scene`] onto the screen using the provided [`WindowState`].
10pub trait Renderer {
11 fn render(&mut self, scene: &Scene, window: &WindowState, font_cache: &fonts::Cache);
12 
13 fn resize(&mut self, window: &WindowState);
14}
15 
16/// Set of available physical graphics devices that can be used to render.
17#[allow(clippy::upper_case_acronyms)]
18pub enum Device {
19 #[allow(dead_code)]
20 Metal(metal::Device),
21 #[cfg(wgpu)]
22 WGPU(Box<crate::rendering::wgpu::Resources>),
23}
24impl Device {
25 pub fn new(
26 _metal_device: metal::Device,
27 _native_view: id,
28 _native_window: id,
29 _gpu_power_preference: GPUPowerPreference,
30 on_gpu_device_info: Box<OnGPUDeviceSelected>,
31 ) -> Self {
32 #[cfg(not(wgpu))]
33 {
34 let gpu_device_info = get_gpu_device_info(&_metal_device);
35 on_gpu_device_info(gpu_device_info);
36 Device::Metal(_metal_device)
37 }
38 
39 #[cfg(wgpu)]
40 {
41 Device::new_wgpu(_native_view, _gpu_power_preference, on_gpu_device_info)
42 .expect("unable to create wgpu device")
43 }
44 }
45}
46 
47#[cfg_attr(wgpu, allow(dead_code))]
48fn get_gpu_device_info(device: &metal::Device) -> GPUDeviceInfo {
49 let device_type = if is_integrated_gpu(device) {
50 GPUDeviceType::IntegratedGpu
51 } else {
52 GPUDeviceType::DiscreteGpu
53 };
54 GPUDeviceInfo {
55 device_type,
56 device_name: device.name().into(),
57 // Mimic wgpu by setting the driver name and info to empty strings when
58 // rendering on Metal. See https://github.com/gfx-rs/wgpu/blob/8129897ccbff869ef48a3b53a4cdd8a8a21840f9/wgpu-hal/src/metal/mod.rs#L135.
59 driver_name: String::new(),
60 driver_info: String::new(),
61 backend: GPUBackend::Metal,
62 }
63}
64