File: dmabuf_formats.rs

package info (click to toggle)
rust-smithay-client-toolkit 0.19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 912 kB
  • sloc: makefile: 2
file content (133 lines) | stat: -rw-r--r-- 4,022 bytes parent folder | download
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
use drm_fourcc::{DrmFourcc, DrmModifier};
use smithay_client_toolkit::{
    dmabuf::{DmabufFeedback, DmabufFormat, DmabufHandler, DmabufState},
    registry::{ProvidesRegistryState, RegistryState},
    registry_handlers,
};
use wayland_client::{globals::registry_queue_init, protocol::wl_buffer, Connection, QueueHandle};
use wayland_protocols::wp::linux_dmabuf::zv1::client::{
    zwp_linux_buffer_params_v1, zwp_linux_dmabuf_feedback_v1,
};

struct AppData {
    registry_state: RegistryState,
    dmabuf_state: DmabufState,
    feedback: Option<DmabufFeedback>,
}

impl DmabufHandler for AppData {
    fn dmabuf_state(&mut self) -> &mut DmabufState {
        &mut self.dmabuf_state
    }

    fn dmabuf_feedback(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        _proxy: &zwp_linux_dmabuf_feedback_v1::ZwpLinuxDmabufFeedbackV1,
        feedback: DmabufFeedback,
    ) {
        self.feedback = Some(feedback);
    }

    fn created(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        _params: &zwp_linux_buffer_params_v1::ZwpLinuxBufferParamsV1,
        _buffer: wl_buffer::WlBuffer,
    ) {
    }

    fn failed(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        _params: &zwp_linux_buffer_params_v1::ZwpLinuxBufferParamsV1,
    ) {
    }

    fn released(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        _buffer: &wl_buffer::WlBuffer,
    ) {
    }
}

impl ProvidesRegistryState for AppData {
    fn registry(&mut self) -> &mut RegistryState {
        &mut self.registry_state
    }
    registry_handlers![,];
}

fn main() {
    env_logger::init();

    let conn = Connection::connect_to_env().unwrap();

    let (globals, mut event_queue) = registry_queue_init(&conn).unwrap();
    let qh = event_queue.handle();

    let mut app_data = AppData {
        registry_state: RegistryState::new(&globals),
        dmabuf_state: DmabufState::new(&globals, &qh),
        feedback: None,
    };

    match app_data.dmabuf_state.version() {
        None => println!("`zwp_linux_dmabuf_v1` version `>3` not supported by compositor."),
        Some(0..=2) => unreachable!(),
        Some(3) => {
            println!("Version `3` of `zwp_linux_dmabuf_v1` supported. Showing modifiers.\n");

            // Roundtrip after binding global to receive modifier events.
            event_queue.roundtrip(&mut app_data).unwrap();

            for entry in app_data.dmabuf_state.modifiers() {
                print_format(entry);
            }

            return;
        }
        Some(ver @ 4..) => {
            println!("Version `{}` of `zwp_linux_dmabuf_v1` supported. Showing default dmabuf feedback.\n", ver);

            app_data.dmabuf_state.get_default_feedback(&qh).unwrap();

            let feedback = loop {
                event_queue.blocking_dispatch(&mut app_data).unwrap();
                if let Some(feedback) = app_data.feedback.as_ref() {
                    break feedback;
                }
            };

            println!("Main device: 0x{:x}", feedback.main_device());
            println!("Tranches:");
            let format_table = feedback.format_table();
            for tranche in feedback.tranches() {
                println!("  Device: 0x{:x}", tranche.device);
                println!("  Flags: {:?}", tranche.flags);
                println!("  Formats");
                for idx in &tranche.formats {
                    print!("    ");
                    print_format(&format_table[*idx as usize]);
                }
            }
        }
    }
}

fn print_format(format: &DmabufFormat) {
    print!("Format: ");
    match DrmFourcc::try_from(format.format) {
        Ok(format) => print!("{:?}", format),
        Err(err) => print!("{:?}", err),
    }
    println!(", Modifier: {:?}", DrmModifier::from(format.modifier));
}

smithay_client_toolkit::delegate_dmabuf!(AppData);
smithay_client_toolkit::delegate_registry!(AppData);