-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathdrm.rs
More file actions
244 lines (210 loc) · 7.24 KB
/
drm.rs
File metadata and controls
244 lines (210 loc) · 7.24 KB
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
//! Example of using softbuffer with drm-rs.
mod util;
#[cfg(all(
feature = "kms",
not(any(
target_os = "android",
target_vendor = "apple",
target_os = "redox",
target_family = "wasm",
target_os = "windows"
))
))]
mod imple {
use drm::control::{connector, Device as CtrlDevice, Event, ModeTypeFlags, PlaneType};
use drm::Device;
use raw_window_handle::{DisplayHandle, DrmDisplayHandle, DrmWindowHandle, WindowHandle};
use softbuffer::{Context, Surface};
use std::num::NonZeroU32;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd};
use std::path::Path;
use std::time::{Duration, Instant};
pub(super) fn entry() -> Result<(), Box<dyn std::error::Error>> {
// Open a new device.
let device = Card::find()?;
// Create the softbuffer context.
let context = unsafe {
Context::new(DisplayHandle::borrow_raw({
let handle = DrmDisplayHandle::new(device.as_fd().as_raw_fd());
handle.into()
}))
}?;
// Get the DRM handles.
let handles = device.resource_handles()?;
// Get the list of connectors and CRTCs.
let connectors = handles
.connectors()
.iter()
.map(|&con| device.get_connector(con, true))
.collect::<Result<Vec<_>, _>>()?;
let crtcs = handles
.crtcs()
.iter()
.map(|&crtc| device.get_crtc(crtc))
.collect::<Result<Vec<_>, _>>()?;
// Find a connected crtc.
let con = connectors
.iter()
.find(|con| con.state() == connector::State::Connected)
.ok_or("No connected connectors")?;
// Get the first CRTC.
let crtc = crtcs.first().ok_or("No CRTCs")?;
// Find a mode to use.
let mode = con
.modes()
.iter()
.find(|mode| mode.mode_type().contains(ModeTypeFlags::PREFERRED))
.or_else(|| con.modes().first())
.ok_or("No modes")?;
// Look for a primary plane compatible with our CRTC.
let planes = device.plane_handles()?;
let planes = planes
.iter()
.filter(|&&plane| {
device.get_plane(plane).is_ok_and(|plane| {
let crtcs = handles.filter_crtcs(plane.possible_crtcs());
crtcs.contains(&crtc.handle())
})
})
.collect::<Vec<_>>();
// Find the first primary plane or take the first one period.
let plane = planes
.iter()
.find(|&&&plane| {
if let Ok(props) = device.get_properties(plane) {
let (ids, vals) = props.as_props_and_values();
for (&id, &val) in ids.iter().zip(vals.iter()) {
if let Ok(info) = device.get_property(id) {
if info.name().to_str() == Ok("type") {
return val == PlaneType::Primary as u32 as u64;
}
}
}
}
false
})
.or(planes.first())
.ok_or("No planes")?;
// Create the surface on top of this plane.
// Note: This requires root on DRM/KMS.
let mut surface = unsafe {
Surface::new(
&context,
WindowHandle::borrow_raw({
let handle = DrmWindowHandle::new((**plane).into());
handle.into()
}),
)
}?;
// Resize the surface.
let (width, height) = mode.size();
surface.resize(
NonZeroU32::new(width as u32).unwrap(),
NonZeroU32::new(height as u32).unwrap(),
)?;
// Start drawing to it.
let start = Instant::now();
let mut tick = 0;
while Instant::now().duration_since(start) < Duration::from_secs(2) {
tick += 1;
tracing::info!("Drawing tick {tick}");
// Start drawing.
let mut buffer = surface.buffer_mut()?;
draw_to_buffer(buffer.pixels(), tick);
buffer.present()?;
// Wait for the page flip to happen.
rustix::event::poll(
&mut [rustix::event::PollFd::new(
&device,
rustix::event::PollFlags::IN,
)],
None,
)?;
// Receive the events.
let events = device.receive_events()?;
tracing::info!("Got some events...");
for event in events {
match event {
Event::PageFlip(_) => {
tracing::info!("Page flip event.");
}
Event::Vblank(_) => {
tracing::info!("Vblank event.");
}
_ => {
tracing::info!("Unknown event.");
}
}
}
}
Ok(())
}
fn draw_to_buffer(buf: &mut [u32], tick: usize) {
let scale = colorous::SINEBOW;
let mut i = (tick as f64) / 20.0;
while i > 1.0 {
i -= 1.0;
}
let color = scale.eval_continuous(i);
let pixel = ((color.r as u32) << 16) | ((color.g as u32) << 8) | (color.b as u32);
buf.fill(pixel);
}
struct Card(std::fs::File);
impl Card {
fn find() -> Result<Card, Box<dyn std::error::Error>> {
for i in 0..10 {
let path = format!("/dev/dri/card{i}");
// Card enumeration may not start at zero, allow failures while opening
let Ok(device) = Card::open(path) else {
continue;
};
// Only use it if it has connectors.
let Ok(handles) = device.resource_handles() else {
continue;
};
if handles
.connectors
.iter()
.filter_map(|c| device.get_connector(*c, false).ok())
.any(|c| c.state() == connector::State::Connected)
{
return Ok(device);
}
}
Err("No DRM device found".into())
}
fn open(path: impl AsRef<Path>) -> Result<Card, Box<dyn std::error::Error>> {
let file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.open(path)?;
Ok(Card(file))
}
}
impl AsFd for Card {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}
impl Device for Card {}
impl CtrlDevice for Card {}
}
#[cfg(not(all(
feature = "kms",
not(any(
target_os = "android",
target_vendor = "apple",
target_os = "redox",
target_family = "wasm",
target_os = "windows"
))
)))]
mod imple {
pub(super) fn entry() -> Result<(), Box<dyn std::error::Error>> {
panic!("This example requires the `kms` feature.")
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
util::setup();
imple::entry()
}