Skip to content

Commit 3901acc

Browse files
committed
Initialize Vulkan directly from wlroots, refactor out the processor trait
1 parent a942090 commit 3901acc

File tree

5 files changed

+71
-81
lines changed

5 files changed

+71
-81
lines changed

src/frame/capturer/wlroots.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::frame::{object::Object, processor::Processor};
1+
use crate::frame::{object::Object, vulkan::Vulkan};
22
use crate::predictor::Controller;
33
use std::{cell::RefCell, rc::Rc, thread, time::Duration};
44
use wayland_client::{
@@ -21,7 +21,7 @@ pub struct Capturer {
2121
event_queue: Rc<RefCell<EventQueue>>,
2222
globals: GlobalManager,
2323
dmabuf_manager: Main<ZwlrExportDmabufManagerV1>,
24-
processor: Rc<dyn Processor>,
24+
vulkan: Rc<Vulkan>,
2525
registry: Main<WlRegistry>,
2626
xdg_output_manager: Main<ZxdgOutputManagerV1>,
2727
}
@@ -65,8 +65,8 @@ impl super::Capturer for Capturer {
6565
}
6666
}
6767

68-
impl Capturer {
69-
pub fn new(processor: Box<dyn Processor>) -> Self {
68+
impl Default for Capturer {
69+
fn default() -> Self {
7070
let display = Display::connect_to_env().unwrap();
7171
let mut event_queue = display.create_event_queue();
7272
let attached_display = display.attach(event_queue.token());
@@ -83,16 +83,20 @@ impl Capturer {
8383
.instantiate_exact::<ZxdgOutputManagerV1>(3)
8484
.expect("Unable to init xdg_output_manager");
8585

86+
let vulkan = Rc::new(Vulkan::new().expect("Unable to initialize Vulkan"));
87+
8688
Self {
8789
event_queue: Rc::new(RefCell::new(event_queue)),
8890
globals,
8991
registry,
9092
dmabuf_manager,
91-
processor: processor.into(),
93+
vulkan,
9294
xdg_output_manager,
9395
}
9496
}
97+
}
9598

99+
impl Capturer {
96100
fn capture_frame(
97101
self: Rc<Self>,
98102
controller: Rc<RefCell<Controller>>,
@@ -119,7 +123,7 @@ impl Capturer {
119123

120124
Event::Ready { .. } => {
121125
let luma = self
122-
.processor
126+
.vulkan
123127
.luma_percent(&frame)
124128
.expect("Unable to compute luma percent");
125129

src/frame/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use itertools::Itertools;
22

33
pub mod capturer;
44
mod object;
5-
pub mod processor;
5+
pub mod vulkan;
66

77
pub fn compute_perceived_lightness_percent(rgbas: &[u8], has_alpha: bool, pixels: usize) -> u8 {
88
let channels = if has_alpha { 4 } else { 3 };

src/frame/processor/mod.rs

-8
This file was deleted.

src/frame/processor/vulkan.rs renamed to src/frame/vulkan.rs

+57-59
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const FINAL_MIP_LEVEL: u32 = 4; // Don't generate mipmaps beyond this level - GP
1414
const BUFFER_PIXELS: u64 = 500 * 4; // Pre-allocated buffer size, should be enough to fit FINAL_MIP_LEVEL
1515
const FENCES_TIMEOUT_NS: u64 = 1_000_000_000;
1616

17-
pub struct Processor {
17+
pub struct Vulkan {
1818
_entry: Entry, // must keep reference to prevent early memory release
1919
instance: Instance,
2020
device: Device,
@@ -29,63 +29,7 @@ pub struct Processor {
2929
image_resolution: RefCell<Option<(u32, u32)>>,
3030
}
3131

32-
impl super::Processor for Processor {
33-
fn luma_percent(&self, frame: &Object) -> Result<u8, Box<dyn Error>> {
34-
assert_eq!(
35-
1, frame.num_objects,
36-
"Frames with multiple objects are not supported yet"
37-
);
38-
39-
if self.image.borrow().is_none() {
40-
self.init_image(frame)?;
41-
}
42-
assert_eq!(
43-
(frame.width, frame.height),
44-
self.image_resolution.borrow().unwrap(),
45-
"Handling screen resolution change is not supported yet"
46-
);
47-
48-
let image = self
49-
.image
50-
.borrow()
51-
.ok_or("Unable to borrow the Vulkan image")?;
52-
53-
let (frame_image, frame_image_memory) = self.init_frame_image(frame)?;
54-
55-
self.begin_commands()?;
56-
57-
let (target_mip_level, mip_width, mip_height) =
58-
self.generate_mipmaps(frame, &frame_image, &image);
59-
60-
self.copy_mipmap(&image, target_mip_level, mip_width, mip_height);
61-
62-
self.submit_commands()?;
63-
64-
let pixels = mip_width as usize * mip_height as usize;
65-
let rgbas = unsafe {
66-
let buffer_pointer = self.device.map_memory(
67-
self.buffer_memory,
68-
0,
69-
vk::WHOLE_SIZE,
70-
vk::MemoryMapFlags::empty(),
71-
)?;
72-
std::slice::from_raw_parts(buffer_pointer as *mut u8, pixels * 4)
73-
};
74-
75-
let result = compute_perceived_lightness_percent(rgbas, true, pixels);
76-
77-
unsafe {
78-
self.device.unmap_memory(self.buffer_memory);
79-
self.device.reset_fences(&[self.fence])?;
80-
self.device.destroy_image(frame_image, None);
81-
self.device.free_memory(frame_image_memory, None);
82-
}
83-
84-
Ok(result)
85-
}
86-
}
87-
88-
impl Processor {
32+
impl Vulkan {
8933
pub fn new() -> Result<Self, Box<dyn Error>> {
9034
let app_name = CString::new("wluma")?;
9135
let app_info = vk::ApplicationInfo::builder()
@@ -197,6 +141,60 @@ impl Processor {
197141
})
198142
}
199143

144+
pub fn luma_percent(&self, frame: &Object) -> Result<u8, Box<dyn Error>> {
145+
assert_eq!(
146+
1, frame.num_objects,
147+
"Frames with multiple objects are not supported yet"
148+
);
149+
150+
if self.image.borrow().is_none() {
151+
self.init_image(frame)?;
152+
}
153+
assert_eq!(
154+
(frame.width, frame.height),
155+
self.image_resolution.borrow().unwrap(),
156+
"Handling screen resolution change is not supported yet"
157+
);
158+
159+
let image = self
160+
.image
161+
.borrow()
162+
.ok_or("Unable to borrow the Vulkan image")?;
163+
164+
let (frame_image, frame_image_memory) = self.init_frame_image(frame)?;
165+
166+
self.begin_commands()?;
167+
168+
let (target_mip_level, mip_width, mip_height) =
169+
self.generate_mipmaps(frame, &frame_image, &image);
170+
171+
self.copy_mipmap(&image, target_mip_level, mip_width, mip_height);
172+
173+
self.submit_commands()?;
174+
175+
let pixels = mip_width as usize * mip_height as usize;
176+
let rgbas = unsafe {
177+
let buffer_pointer = self.device.map_memory(
178+
self.buffer_memory,
179+
0,
180+
vk::WHOLE_SIZE,
181+
vk::MemoryMapFlags::empty(),
182+
)?;
183+
std::slice::from_raw_parts(buffer_pointer as *mut u8, pixels * 4)
184+
};
185+
186+
let result = compute_perceived_lightness_percent(rgbas, true, pixels);
187+
188+
unsafe {
189+
self.device.unmap_memory(self.buffer_memory);
190+
self.device.reset_fences(&[self.fence])?;
191+
self.device.destroy_image(frame_image, None);
192+
self.device.free_memory(frame_image_memory, None);
193+
}
194+
195+
Ok(result)
196+
}
197+
200198
fn init_image(&self, frame: &Object) -> Result<(), Box<dyn Error>> {
201199
let (width, height, mip_levels) = image_dimensions(frame);
202200

@@ -525,7 +523,7 @@ impl Processor {
525523
}
526524
}
527525

528-
impl Drop for Processor {
526+
impl Drop for Vulkan {
529527
fn drop(&mut self) {
530528
unsafe {
531529
self.device

src/main.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,11 @@ fn main() {
6767
std::thread::Builder::new()
6868
.name(thread_name.clone())
6969
.spawn(move || {
70-
let frame_processor = Box::new(
71-
frame::processor::vulkan::Processor::new()
72-
.expect("Unable to initialize Vulkan"),
73-
);
7470
let frame_capturer: Box<dyn frame::capturer::Capturer> =
7571
match output_capturer {
76-
config::Capturer::Wlroots => Box::new(
77-
frame::capturer::wlroots::Capturer::new(frame_processor),
78-
),
72+
config::Capturer::Wlroots => {
73+
Box::new(frame::capturer::wlroots::Capturer::default())
74+
}
7975
config::Capturer::None => {
8076
Box::new(frame::capturer::none::Capturer::default())
8177
}

0 commit comments

Comments
 (0)