summary refs log tree commit diff
path: root/gpu_display/src/gpu_display_stub.rs
blob: 3089f1f399441ad9b7dd52b2929fa1c9611fdf2b (plain) (blame)
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
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::collections::BTreeMap;
use std::num::NonZeroU32;
use std::os::unix::io::{AsRawFd, RawFd};

use crate::{DisplayT, EventDevice, GpuDisplayError, GpuDisplayFramebuffer};

use data_model::VolatileSlice;
use sys_util::EventFd;

type SurfaceId = NonZeroU32;

#[allow(dead_code)]
struct Buffer {
    width: u32,
    height: u32,
    bytes_per_pixel: u32,
    bytes: Vec<u8>,
}

impl Drop for Buffer {
    fn drop(&mut self) {}
}

impl Buffer {
    fn as_volatile_slice(&mut self) -> VolatileSlice {
        VolatileSlice::new(self.bytes.as_mut_slice())
    }

    fn stride(&self) -> usize {
        return (self.bytes_per_pixel as usize) * (self.width as usize);
    }

    fn bytes_per_pixel(&self) -> usize {
        return self.bytes_per_pixel as usize;
    }
}

struct Surface {
    width: u32,
    height: u32,
    buffer: Option<Buffer>,
}

impl Surface {
    fn create(width: u32, height: u32) -> Result<Surface, GpuDisplayError> {
        Ok(Surface {
            width,
            height,
            buffer: None,
        })
    }

    /// Gets the buffer at buffer_index, allocating it if necessary.
    fn lazily_allocate_buffer(&mut self) -> Option<&mut Buffer> {
        if self.buffer.is_none() {
            // XRGB8888
            let bytes_per_pixel = 4;
            let bytes_total = (self.width as u64) * (self.height as u64) * (bytes_per_pixel as u64);

            self.buffer = Some(Buffer {
                width: self.width,
                height: self.height,
                bytes_per_pixel,
                bytes: vec![0; bytes_total as usize],
            });
        }

        self.buffer.as_mut()
    }

    /// Gets the next framebuffer, allocating if necessary.
    fn framebuffer(&mut self) -> Option<GpuDisplayFramebuffer> {
        let framebuffer = self.lazily_allocate_buffer()?;
        let framebuffer_stride = framebuffer.stride() as u32;
        let framebuffer_bytes_per_pixel = framebuffer.bytes_per_pixel() as u32;
        Some(GpuDisplayFramebuffer::new(
            framebuffer.as_volatile_slice(),
            framebuffer_stride,
            framebuffer_bytes_per_pixel,
        ))
    }

    fn flip(&mut self) {}
}

impl Drop for Surface {
    fn drop(&mut self) {}
}

struct SurfacesHelper {
    next_surface_id: SurfaceId,
    surfaces: BTreeMap<SurfaceId, Surface>,
}

impl SurfacesHelper {
    fn new() -> SurfacesHelper {
        SurfacesHelper {
            next_surface_id: SurfaceId::new(1).unwrap(),
            surfaces: Default::default(),
        }
    }

    fn create_surface(&mut self, width: u32, height: u32) -> Result<u32, GpuDisplayError> {
        let new_surface = Surface::create(width, height)?;
        let new_surface_id = self.next_surface_id;

        self.surfaces.insert(new_surface_id, new_surface);
        self.next_surface_id = SurfaceId::new(self.next_surface_id.get() + 1).unwrap();

        Ok(new_surface_id.get())
    }

    fn get_surface(&mut self, surface_id: u32) -> Option<&mut Surface> {
        SurfaceId::new(surface_id).and_then(move |id| self.surfaces.get_mut(&id))
    }

    fn destroy_surface(&mut self, surface_id: u32) {
        SurfaceId::new(surface_id).and_then(|id| self.surfaces.remove(&id));
    }

    fn flip_surface(&mut self, surface_id: u32) {
        if let Some(surface) = self.get_surface(surface_id) {
            surface.flip();
        }
    }
}

pub struct DisplayStub {
    /// This eventfd is never triggered and is used solely to fulfill AsRawFd.
    eventfd: EventFd,
    surfaces: SurfacesHelper,
}

impl DisplayStub {
    pub fn new() -> Result<DisplayStub, GpuDisplayError> {
        let eventfd = EventFd::new().map_err(|_| GpuDisplayError::CreateEventFd)?;

        Ok(DisplayStub {
            eventfd,
            surfaces: SurfacesHelper::new(),
        })
    }
}

impl DisplayT for DisplayStub {
    fn dispatch_events(&mut self) {}

    fn create_surface(
        &mut self,
        parent_surface_id: Option<u32>,
        width: u32,
        height: u32,
    ) -> Result<u32, GpuDisplayError> {
        if parent_surface_id.is_some() {
            return Err(GpuDisplayError::Unsupported);
        }
        self.surfaces.create_surface(width, height)
    }

    fn release_surface(&mut self, surface_id: u32) {
        self.surfaces.destroy_surface(surface_id);
    }

    fn framebuffer(&mut self, surface_id: u32) -> Option<GpuDisplayFramebuffer> {
        self.surfaces
            .get_surface(surface_id)
            .and_then(|s| s.framebuffer())
    }

    fn next_buffer_in_use(&self, _surface_id: u32) -> bool {
        false
    }

    fn flip(&mut self, surface_id: u32) {
        self.surfaces.flip_surface(surface_id);
    }

    fn close_requested(&self, _surface_id: u32) -> bool {
        false
    }

    fn import_dmabuf(
        &mut self,
        _fd: RawFd,
        _offset: u32,
        _stride: u32,
        _modifiers: u64,
        _width: u32,
        _height: u32,
        _fourcc: u32,
    ) -> Result<u32, GpuDisplayError> {
        Err(GpuDisplayError::Unsupported)
    }

    fn release_import(&mut self, _import_id: u32) {
        // unsupported
    }

    fn commit(&mut self, _surface_id: u32) {
        // unsupported
    }

    fn flip_to(&mut self, _surface_id: u32, _import_id: u32) {
        // unsupported
    }

    fn set_position(&mut self, _surface_id: u32, _x: u32, _y: u32) {
        // unsupported
    }

    fn import_event_device(&mut self, _event_device: EventDevice) -> Result<u32, GpuDisplayError> {
        Err(GpuDisplayError::Unsupported)
    }

    fn release_event_device(&mut self, _event_device_id: u32) {
        // unsupported
    }

    fn attach_event_device(&mut self, _surface_id: u32, _event_device_id: u32) {
        // unsupported
    }
}

impl AsRawFd for DisplayStub {
    fn as_raw_fd(&self) -> RawFd {
        self.eventfd.as_raw_fd()
    }
}