xref: /aosp_15_r20/external/crosvm/gpu_display/src/sys/linux.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::path::Path;
6 
7 use base::AsRawDescriptor;
8 use base::RawDescriptor;
9 use base::WaitContext;
10 
11 use crate::gpu_display_wl::DisplayWl;
12 use crate::DisplayEventToken;
13 use crate::DisplayT;
14 use crate::EventDevice;
15 use crate::GpuDisplay;
16 use crate::GpuDisplayExt;
17 use crate::GpuDisplayResult;
18 
19 pub(crate) trait UnixDisplayT: DisplayT {}
20 
21 impl GpuDisplayExt for GpuDisplay {
import_event_device(&mut self, event_device: EventDevice) -> GpuDisplayResult<u32>22     fn import_event_device(&mut self, event_device: EventDevice) -> GpuDisplayResult<u32> {
23         let new_event_device_id = self.next_id;
24 
25         self.wait_ctx.add(
26             &event_device,
27             DisplayEventToken::EventDevice {
28                 event_device_id: new_event_device_id,
29             },
30         )?;
31         self.event_devices.insert(new_event_device_id, event_device);
32 
33         self.next_id += 1;
34         Ok(new_event_device_id)
35     }
36 
handle_event_device(&mut self, event_device_id: u32)37     fn handle_event_device(&mut self, event_device_id: u32) {
38         if let Some(event_device) = self.event_devices.get(&event_device_id) {
39             // TODO(zachr): decode the event and forward to the device.
40             let _ = event_device.recv_event_encoded();
41         }
42     }
43 }
44 
45 pub trait UnixGpuDisplayExt {
46     /// Opens a fresh connection to the compositor.
open_wayland<P: AsRef<Path>>(wayland_path: Option<P>) -> GpuDisplayResult<GpuDisplay>47     fn open_wayland<P: AsRef<Path>>(wayland_path: Option<P>) -> GpuDisplayResult<GpuDisplay>;
48 }
49 
50 impl UnixGpuDisplayExt for GpuDisplay {
open_wayland<P: AsRef<Path>>(wayland_path: Option<P>) -> GpuDisplayResult<GpuDisplay>51     fn open_wayland<P: AsRef<Path>>(wayland_path: Option<P>) -> GpuDisplayResult<GpuDisplay> {
52         let display = match wayland_path {
53             Some(s) => DisplayWl::new(Some(s.as_ref()))?,
54             None => DisplayWl::new(None)?,
55         };
56 
57         let wait_ctx = WaitContext::new()?;
58         wait_ctx.add(&display, DisplayEventToken::Display)?;
59 
60         Ok(GpuDisplay {
61             inner: Box::new(display),
62             next_id: 1,
63             event_devices: Default::default(),
64             surfaces: Default::default(),
65             wait_ctx,
66         })
67     }
68 }
69 
70 impl AsRawDescriptor for GpuDisplay {
as_raw_descriptor(&self) -> RawDescriptor71     fn as_raw_descriptor(&self) -> RawDescriptor {
72         self.wait_ctx.as_raw_descriptor()
73     }
74 }
75