1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use fragile::Fragile;
use log::trace;
use owning_ref::OwningRefMut;
use smithay_client_toolkit::utils::MemPool;
use std::{
cell::{Cell, RefCell},
fmt,
ops::{Deref, DerefMut},
os::raw::c_void,
rc::Rc,
};
use wayland_client::{
self as wl,
protocol::{wl_buffer, wl_display, wl_shm, wl_surface},
};
use wayland_sys::{client::WAYLAND_CLIENT_HANDLE, ffi_dispatch};
use winit::window::WindowId;
use super::super::{align::Align, Config, ContextBuilder, Format, ImageInfo, ReadyCb};
#[derive(Clone)]
pub struct ContextImpl {
// The following objects' lifetime is bound to the originating
// `winit::event_loop::EventLoop`'s underlying `wl::Display` object. They
// are valid as long as the `winit::event_loop_EventLoop` or
// at least one instance of `winit::window::Window` created from it are
// alive.
wl_dpy: wl_display::WlDisplay,
wl_shm: wl_shm::WlShm,
ready_cb: Rc<ReadyCb>,
}
impl fmt::Debug for ContextImpl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ContextImpl").finish()
}
}
impl ContextImpl {
pub unsafe fn new<T: 'static>(wl_dpy_ptr: *mut c_void, builder: ContextBuilder<'_, T>) -> Self {
let wl_dpy: wl_display::WlDisplay = wl::Proxy::from_c_ptr(wl_dpy_ptr as _).into();
let manager = wl::GlobalManager::new(&wl_dpy);
// Retrieve the globals metadata (without this, we will fail to get
// the global `wl_shm`)
for _ in 0..2 {
ffi_dispatch!(WAYLAND_CLIENT_HANDLE, wl_display_roundtrip, wl_dpy_ptr as _);
}
let wl_shm: wl_shm::WlShm = manager
.instantiate_range(1, 1, |wl_shm| {
wl_shm.implement_closure(
move |evt, _| {
// `wl_shm` sends suppored formats via events
if let wl_shm::Event::Format { format } = evt {
let _ = format;
// TODO: examine supported formats
}
},
(),
)
})
.expect("server does not advertise `wl_shm`");
Self {
wl_dpy,
wl_shm,
ready_cb: Rc::new(builder.ready_cb),
}
}
}
#[derive(Debug)]
pub struct SurfaceImpl {
state: Rc<State>,
}
/// This object is shared between `SharedImpl` and the event handler of
/// `wl_buffer`.
struct State {
ctx: ContextImpl,
wnd_id: WindowId,
wl_srf: wl_surface::WlSurface,
images: Box<[Image]>,
/// If `true`, the `release` event handler will call `ready_cb` when
/// called for the next time.
enable_ready_cb: Cell<bool>,
image_info: Cell<ImageInfo>,
scanline_align: Align,
}
impl fmt::Debug for State {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("State")
.field("ctx", &self.ctx)
.field("wnd_id", &self.wnd_id)
.field("images", &self.images)
.field("enable_ready_cb", &self.enable_ready_cb)
.field("image_info", &self.image_info)
.finish()
}
}
struct Image {
/// `wl_shm_pool`, an associated shared memory region, and a `wl_buffer`
/// created from it.
///
/// `None` at the initial state (i.e., before `update_surface` is called
/// for the first time).
///
/// `wl_buffer` is created only when we are about to present it. Thus, the
/// valid states are:
///
/// 1. `mem = Some(_, None)`, `presenting = false`
/// 1. `mem = Some(_, Some(_))`, `presenting = true`
/// 1. `mem = Some(_, Some(_))`, `presenting = false`
///
mem: RefCell<Option<(MemPool, Option<wl_buffer::WlBuffer>)>>,
/// `true` if `mem` is currently in use by the server, i.e., we have sent
/// it via `wl_surface::attach` but haven't received the `release` event.
/// FIXME: Could be merged into `MemPool::is_used()`
presenting: Cell<bool>,
}
impl Drop for Image {
fn drop(&mut self) {
let mem = self.mem.get_mut();
if let Some(mem) = mem {
if let Some(wl_buf) = mem.1.take() {
trace!("Destroying `wl_buffer` {:?}", wl_buf.as_ref().c_ptr());
// `wl_buf` could be still in use by the presenter, but there
// isn't much we can do. The Wayland connection might not even
// exist after this call to `drop`... (Remember that the
// connection is managed by `winit`)
wl_buf.destroy();
}
}
}
}
impl fmt::Debug for Image {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Image").finish()
}
}
impl SurfaceImpl {
pub unsafe fn new(
wl_dpy: *mut c_void,
wl_srf_ptr: *mut c_void,
wnd_id: WindowId,
context: &ContextImpl,
config: &Config,
scanline_align: Align,
) -> Self {
assert_eq!(wl_dpy, context.wl_dpy.as_ref().c_ptr() as _);
let images: Vec<_> = (0..config.image_count)
.map(|_| Image {
mem: RefCell::new(None),
presenting: Cell::new(false),
})
.collect();
let wl_srf: wl_surface::WlSurface = wl::Proxy::from_c_ptr(wl_srf_ptr as _).into();
Self {
state: Rc::new(State {
ctx: context.clone(),
wnd_id,
wl_srf,
images: images.into_boxed_slice(),
enable_ready_cb: Cell::new(false),
image_info: Cell::new(ImageInfo::default()),
scanline_align,
}),
}
}
pub fn update_surface(&self, extent: [u32; 2], format: Format) {
assert_ne!(extent[0], 0);
assert_ne!(extent[1], 0);
// Fail-fast if some images are locked by the appliction
let mut mems: Vec<_> = self
.state
.images
.iter()
.map(|image| image.mem.try_borrow_mut().expect("some images are locked"))
.collect();
// Check the value range
assert!(extent[0] <= <i32>::max_value() as u32);
assert!(extent[1] <= <i32>::max_value() as u32);
use std::convert::TryInto;
let extent_usize: [usize; 2] = [
extent[0].try_into().expect("overflow"),
extent[1].try_into().expect("overflow"),
];
let stride = extent_usize[0]
.checked_mul(4)
.and_then(|x| self.state.scanline_align.align_up(x))
.expect("overflow");
// `stride` must fit in `i32`
let _bytes_per_line: i32 = stride.try_into().unwrap();
// Calculate a new `ImageInfo`
let image_info = ImageInfo {
extent,
stride,
format,
};
trace!("{:?}: New image info = {:?}", self.state.wnd_id, image_info);
let size = stride
.checked_mul(image_info.extent[1] as usize)
.expect("overflow");
// Resize mempools
for (i, mem) in mems.iter_mut().enumerate() {
let (mem_pool, _) = mem.get_or_insert_with(|| {
// `MemPool` isn't created yet, so make one now
let state = Rc::clone(&self.state);
// `MemPool` doesn't call the event handler from another thread
// (AFAIK). It requires it to be `Send` only to allow you to
// create a `MemPool` for a `WlShm` originaing from another
// thread. So assert that `state` will be used by the current
// thread.
let state = Fragile::new(state);
let on_release = move || {
// Assert that we are using it from the correct thread
let state = state.get();
trace!("{:?}: Swapchain image {} was released", state.wnd_id, i);
state.images[i].presenting.set(false);
// Does the application want to receive a notification?
// If so, reset this flag and call the ready callback.
if state.enable_ready_cb.replace(false) {
trace!("Calling `ready_cb`");
(state.ctx.ready_cb)(state.wnd_id);
}
};
trace!("Creating `MemPool`");
let mem_pool = MemPool::new(&self.state.ctx.wl_shm, on_release)
.expect("could not create `wl_shm_pool`");
(mem_pool, None)
});
trace!("Resizing `MemPool` to {}", size);
mem_pool
.resize(size)
.expect("could not resize the memory-mapped file");
}
self.state.image_info.set(image_info);
}
pub fn supported_formats(&self) -> impl Iterator<Item = Format> + '_ {
[Format::Argb8888].iter().cloned()
}
pub fn image_info(&self) -> ImageInfo {
self.state.image_info.get()
}
pub fn num_images(&self) -> usize {
self.state.images.len()
}
pub fn does_preserve_image(&self) -> bool {
true
}
pub fn poll_next_image(&self) -> Option<usize> {
let result = self
.state
.images
.iter()
.position(|image| image.presenting.get() == false);
if let Some(i) = result {
trace!(
"{:?}: Swapchain image {} is available, returning it",
self.state.wnd_id,
i
);
} else {
if self.state.enable_ready_cb.get() {
trace!(
"{:?}: No swapchain image is available. `ready_cb` is already enabled.",
self.state.wnd_id
);
} else {
trace!(
"{:?}: No swapchain image is available. Enabling `ready_cb`.",
self.state.wnd_id
);
}
// Enable the ready callback
self.state.enable_ready_cb.set(true);
}
result
}
pub fn lock_image(&self, i: usize) -> impl Deref<Target = [u8]> + DerefMut + '_ {
let image = &self.state.images[i];
assert_eq!(
image.presenting.get(),
false,
"the image is currently in use by the compositor"
);
OwningRefMut::new(image.mem.borrow_mut()).map_mut(|x| {
// `update_surface` should have been called at least one.
// Otherwise, panic
x.as_mut()
.expect("surface is not initialized")
.0
// Get the underlying data of the memory-mapped file
.mmap()
.as_mut()
})
}
pub fn present_image(&self, i: usize) {
let image = &self.state.images[i];
assert_eq!(
image.presenting.get(),
false,
"the image is currently in use by the compositor"
);
let mut mem = image.mem.try_borrow_mut().expect("the image is locked");
let (mem_pool, buffer_cell) = mem.as_mut().expect("surface is not initialized");
let image_info = self.state.image_info.get();
let format = match image_info.format {
Format::Argb8888 => wl_shm::Format::Argb8888,
Format::Xrgb8888 => wl_shm::Format::Xrgb8888,
};
// Create `wl_buffer`.
let buffer = mem_pool.buffer(
0,
image_info.extent[0] as i32,
image_info.extent[1] as i32,
image_info.stride as i32,
format,
);
trace!(
"{:?}: Presenting swapchain image {} using `wl_buffer` {:?}",
self.state.wnd_id,
i,
buffer.as_ref().c_ptr()
);
// The previous statement also updates `MemPool`'s flag to indicate
// that `wl_buffer` is attached to a `wl_surface` and will raise the
// `release` event in the near future.
debug_assert!(mem_pool.is_used());
// Attach the `wl_buffer` to the `wl_surface`.
self.state.wl_srf.attach(Some(&buffer), 0, 0);
self.state
.wl_srf
.damage_buffer(0, 0, image_info.extent[0] as _, image_info.extent[1] as _);
self.state.wl_srf.commit();
if let Some(old_buffer) = buffer_cell.take() {
old_buffer.destroy();
}
*buffer_cell = Some(buffer);
image.presenting.set(true);
}
}