@@ -14,7 +14,7 @@ const FINAL_MIP_LEVEL: u32 = 4; // Don't generate mipmaps beyond this level - GP
14
14
const BUFFER_PIXELS : u64 = 500 * 4 ; // Pre-allocated buffer size, should be enough to fit FINAL_MIP_LEVEL
15
15
const FENCES_TIMEOUT_NS : u64 = 1_000_000_000 ;
16
16
17
- pub struct Processor {
17
+ pub struct Vulkan {
18
18
_entry : Entry , // must keep reference to prevent early memory release
19
19
instance : Instance ,
20
20
device : Device ,
@@ -29,63 +29,7 @@ pub struct Processor {
29
29
image_resolution : RefCell < Option < ( u32 , u32 ) > > ,
30
30
}
31
31
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 {
89
33
pub fn new ( ) -> Result < Self , Box < dyn Error > > {
90
34
let app_name = CString :: new ( "wluma" ) ?;
91
35
let app_info = vk:: ApplicationInfo :: builder ( )
@@ -197,6 +141,60 @@ impl Processor {
197
141
} )
198
142
}
199
143
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
+
200
198
fn init_image ( & self , frame : & Object ) -> Result < ( ) , Box < dyn Error > > {
201
199
let ( width, height, mip_levels) = image_dimensions ( frame) ;
202
200
@@ -525,7 +523,7 @@ impl Processor {
525
523
}
526
524
}
527
525
528
- impl Drop for Processor {
526
+ impl Drop for Vulkan {
529
527
fn drop ( & mut self ) {
530
528
unsafe {
531
529
self . device
0 commit comments