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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2024.

//! Shares a screen among multiple userspace processes.
//!
//! The screen can be split into multiple regions, and regions are assigned to
//! processes by AppID.
//!
//! Boards should create an array of `AppScreenRegion` objects that assign apps
//! to specific regions (frames) within the screen.
//!
//! ```rust,ignore
//! AppScreenRegion {
//!     app_id: kernel::process:ShortId::new(id),
//!     frame: Frame {
//!         x: 0,
//!         y: 0,
//!         width: 8,
//!         height: 16,
//!     }
//! }
//! ```
//!
//! This driver uses a subset of the API from `Screen`. It does not support any
//! screen config settings (brightness, invert) as those operations affect the
//! entire screen.

use kernel::grant::{AllowRoCount, AllowRwCount, Grant, UpcallCount};
use kernel::hil;
use kernel::processbuffer::ReadableProcessBuffer;
use kernel::syscall::{CommandReturn, SyscallDriver};
use kernel::utilities::cells::{OptionalCell, TakeCell};
use kernel::utilities::leasable_buffer::SubSliceMut;
use kernel::{ErrorCode, ProcessId};

/// Syscall driver number.
use capsules_core::driver;
pub const DRIVER_NUM: usize = driver::NUM::Screen as usize;

/// Ids for read-only allow buffers
mod ro_allow {
    pub const SHARED: usize = 0;
    /// The number of allow buffers the kernel stores for this grant
    pub const COUNT: u8 = 1;
}

#[derive(Clone, Copy, PartialEq)]
enum ScreenCommand {
    WriteSetFrame,
    WriteBuffer,
}

fn pixels_in_bytes(pixels: usize, bits_per_pixel: usize) -> usize {
    let bytes = pixels * bits_per_pixel / 8;
    if pixels * bits_per_pixel % 8 != 0 {
        bytes + 1
    } else {
        bytes
    }
}

/// Rectangular region of a screen.
#[derive(Default, Clone, Copy, PartialEq)]
pub struct Frame {
    /// X coordinate of the upper left corner of the frame.
    x: usize,
    /// Y coordinate of the upper left corner of the frame.
    y: usize,
    /// Width of the frame.
    width: usize,
    /// Height of the frame.
    height: usize,
}

pub struct AppScreenRegion {
    app_id: kernel::process::ShortId,
    frame: Frame,
}

impl AppScreenRegion {
    pub fn new(
        app_id: kernel::process::ShortId,
        x: usize,
        y: usize,
        width: usize,
        height: usize,
    ) -> Self {
        Self {
            app_id,
            frame: Frame {
                x,
                y,
                width,
                height,
            },
        }
    }
}

#[derive(Default)]
pub struct App {
    /// The app has requested some screen operation, or `None()` if idle.
    command: Option<ScreenCommand>,
    /// The current frame the app is using.
    frame: Frame,
}

/// A userspace driver that allows multiple apps to use the same screen.
///
/// Each app is given a pre-set rectangular region of the screen to use.
pub struct ScreenShared<'a, S: hil::screen::Screen<'a>> {
    /// Underlying screen driver to use.
    screen: &'a S,

    /// Grant region for apps using the screen.
    apps: Grant<App, UpcallCount<1>, AllowRoCount<{ ro_allow::COUNT }>, AllowRwCount<0>>,

    /// Static allocations of screen regions for each app.
    apps_regions: &'a [AppScreenRegion],

    /// The process currently executing a command on the screen.
    current_process: OptionalCell<ProcessId>,

    /// Internal buffer for write commands.
    buffer: TakeCell<'static, [u8]>,
}

impl<'a, S: hil::screen::Screen<'a>> ScreenShared<'a, S> {
    pub fn new(
        screen: &'a S,
        grant: Grant<App, UpcallCount<1>, AllowRoCount<{ ro_allow::COUNT }>, AllowRwCount<0>>,
        buffer: &'static mut [u8],
        apps_regions: &'a [AppScreenRegion],
    ) -> ScreenShared<'a, S> {
        ScreenShared {
            screen,
            apps: grant,
            current_process: OptionalCell::empty(),
            buffer: TakeCell::new(buffer),
            apps_regions,
        }
    }

    // Enqueue a command for the given app.
    fn enqueue_command(&self, command: ScreenCommand, process_id: ProcessId) -> CommandReturn {
        let ret = self
            .apps
            .enter(process_id, |app, _| {
                if app.command.is_some() {
                    Err(ErrorCode::BUSY)
                } else {
                    app.command = Some(command);
                    Ok(())
                }
            })
            .map_err(ErrorCode::from)
            .and_then(|r| r)
            .into();

        if self.current_process.is_none() {
            self.run_next_command();
        }

        ret
    }

    /// Calculate the frame within the entire screen that the app is currently
    /// trying to use. This is the `app_frame` within the app's allocated
    /// `app_screen_region`.
    fn calculate_absolute_frame(&self, app_screen_region_frame: Frame, app_frame: Frame) -> Frame {
        // x and y are sums
        let mut absolute_x = app_screen_region_frame.x + app_frame.x;
        let mut absolute_y = app_screen_region_frame.y + app_frame.y;
        // width and height are simply the app_frame width and height.
        let mut absolute_w = app_frame.width;
        let mut absolute_h = app_frame.height;

        // Make sure that the calculate frame is within the allocated region.
        absolute_x = core::cmp::min(
            app_screen_region_frame.x + app_screen_region_frame.width,
            absolute_x,
        );
        absolute_y = core::cmp::min(
            app_screen_region_frame.y + app_screen_region_frame.height,
            absolute_y,
        );
        absolute_w = core::cmp::min(
            app_screen_region_frame.x + app_screen_region_frame.width - absolute_x,
            absolute_w,
        );
        absolute_h = core::cmp::min(
            app_screen_region_frame.y + app_screen_region_frame.height - absolute_y,
            absolute_h,
        );

        Frame {
            x: absolute_x,
            y: absolute_y,
            width: absolute_w,
            height: absolute_h,
        }
    }

    fn call_screen(
        &self,
        process_id: ProcessId,
        app_screen_region_frame: Frame,
    ) -> Result<(), ErrorCode> {
        self.apps
            .enter(process_id, |app, kernel_data| {
                match app.command {
                    Some(ScreenCommand::WriteSetFrame) => {
                        let absolute_frame =
                            self.calculate_absolute_frame(app_screen_region_frame, app.frame);

                        app.command = Some(ScreenCommand::WriteBuffer);
                        self.screen
                            .set_write_frame(
                                absolute_frame.x,
                                absolute_frame.y,
                                absolute_frame.width,
                                absolute_frame.height,
                            )
                            .inspect_err(|_| {
                                app.command = None;
                            })
                    }
                    Some(ScreenCommand::WriteBuffer) => {
                        app.command = None;
                        kernel_data
                            .get_readonly_processbuffer(ro_allow::SHARED)
                            .map(|allow_buf| {
                                let len = allow_buf.len();

                                if len == 0 {
                                    Err(ErrorCode::NOMEM)
                                } else if !self.is_len_multiple_color_depth(len) {
                                    Err(ErrorCode::INVAL)
                                } else {
                                    // All good, copy buffer.

                                    self.buffer.take().map_or(Err(ErrorCode::FAIL), |buffer| {
                                        let copy_len =
                                            core::cmp::min(buffer.len(), allow_buf.len());
                                        allow_buf.enter(|ab| {
                                            // buffer[..copy_len].copy_from_slice(ab[..copy_len]);
                                            ab[..copy_len].copy_to_slice(&mut buffer[..copy_len])
                                        })?;

                                        // Send to screen.
                                        let mut data = SubSliceMut::new(buffer);
                                        data.slice(..copy_len);
                                        self.screen.write(data, false)
                                    })
                                }
                            })
                            .map_err(ErrorCode::from)
                            .and_then(|r| r)
                    }
                    _ => Err(ErrorCode::NOSUPPORT),
                }
            })
            .map_err(ErrorCode::from)
            .and_then(|r| r)
    }

    fn schedule_callback(&self, process_id: ProcessId, data1: usize, data2: usize, data3: usize) {
        let _ = self.apps.enter(process_id, |_app, kernel_data| {
            kernel_data.schedule_upcall(0, (data1, data2, data3)).ok();
        });
    }

    fn get_app_screen_region_frame(&self, process_id: ProcessId) -> Option<Frame> {
        let short_id = process_id.short_app_id();

        for app_screen_region in self.apps_regions {
            if short_id == app_screen_region.app_id {
                return Some(app_screen_region.frame);
            }
        }
        None
    }

    fn run_next_command(&self) {
        let ran_cmd = self.current_process.map_or(false, |process_id| {
            let app_region_frame = self.get_app_screen_region_frame(process_id);

            app_region_frame.map_or(false, |frame| {
                let r = self.call_screen(process_id, frame);
                if r.is_err() {
                    // We were unable to run the screen operation meaning we
                    // will not get a callback and we need to report the error.
                    self.current_process.take().map(|process_id| {
                        self.schedule_callback(
                            process_id,
                            kernel::errorcode::into_statuscode(r),
                            0,
                            0,
                        );
                    });
                    false
                } else {
                    true
                }
            })
        });

        if !ran_cmd {
            // Check if there are any pending events.
            for app in self.apps.iter() {
                let process_id = app.processid();

                // Check if this process has both a pending command and is
                // allocated a region on the screen.
                let frame_maybe = app.enter(|app, _| {
                    if app.command.is_some() {
                        self.get_app_screen_region_frame(process_id)
                    } else {
                        None
                    }
                });

                // If we have a candidate, try to execute the screen operation.
                if frame_maybe.is_some() {
                    match frame_maybe {
                        Some(frame) => {
                            // Reserve the screen for this process and execute
                            // the operation.
                            self.current_process.set(process_id);
                            match self.call_screen(process_id, frame) {
                                Ok(()) => {
                                    // Everything is good, stop looking for apps
                                    // to execute.
                                    break;
                                }
                                Err(err) => {
                                    // Could not run the screen command.
                                    // Un-reserve the screen and do an upcall
                                    // with the bad news.
                                    self.current_process.clear();
                                    self.schedule_callback(
                                        process_id,
                                        kernel::errorcode::into_statuscode(Err(err)),
                                        0,
                                        0,
                                    );
                                }
                            }
                        }
                        None => {}
                    }
                }
            }
        }
    }

    fn is_len_multiple_color_depth(&self, len: usize) -> bool {
        let depth = pixels_in_bytes(1, self.screen.get_pixel_format().get_bits_per_pixel());
        (len % depth) == 0
    }
}

impl<'a, S: hil::screen::Screen<'a>> hil::screen::ScreenClient for ScreenShared<'a, S> {
    fn command_complete(&self, r: Result<(), ErrorCode>) {
        if r.is_err() {
            self.current_process.take().map(|process_id| {
                self.schedule_callback(process_id, kernel::errorcode::into_statuscode(r), 0, 0);
            });
        }

        self.run_next_command();
    }

    fn write_complete(&self, data: SubSliceMut<'static, u8>, r: Result<(), ErrorCode>) {
        self.buffer.replace(data.take());

        // Notify that the write is finished.
        self.current_process.take().map(|process_id| {
            self.schedule_callback(process_id, kernel::errorcode::into_statuscode(r), 0, 0);
        });

        self.run_next_command();
    }

    fn screen_is_ready(&self) {
        self.run_next_command();
    }
}

impl<'a, S: hil::screen::Screen<'a>> SyscallDriver for ScreenShared<'a, S> {
    fn command(
        &self,
        command_num: usize,
        data1: usize,
        data2: usize,
        process_id: ProcessId,
    ) -> CommandReturn {
        match command_num {
            // Driver existence check
            0 => CommandReturn::success(),

            // Get Rotation
            21 => CommandReturn::success_u32(self.screen.get_rotation() as u32),

            // Get Resolution
            23 => match self.get_app_screen_region_frame(process_id) {
                Some(frame) => {
                    CommandReturn::success_u32_u32(frame.width as u32, frame.height as u32)
                }
                None => CommandReturn::failure(ErrorCode::NOSUPPORT),
            },

            // Get pixel format
            25 => CommandReturn::success_u32(self.screen.get_pixel_format() as u32),

            // Set Write Frame
            100 => {
                let frame = Frame {
                    x: (data1 >> 16) & 0xFFFF,
                    y: data1 & 0xFFFF,
                    width: (data2 >> 16) & 0xFFFF,
                    height: data2 & 0xFFFF,
                };

                self.apps
                    .enter(process_id, |app, kernel_data| {
                        app.frame = frame;

                        // Just issue upcall.
                        let _ = kernel_data
                            .schedule_upcall(0, (kernel::errorcode::into_statuscode(Ok(())), 0, 0));
                    })
                    .map_err(ErrorCode::from)
                    .into()
            }

            // Write
            200 => {
                // First check if this app has any screen real estate allocated.
                // If not, return error.
                if self.get_app_screen_region_frame(process_id).is_none() {
                    CommandReturn::failure(ErrorCode::NOSUPPORT)
                } else {
                    self.enqueue_command(ScreenCommand::WriteSetFrame, process_id)
                }
            }

            _ => CommandReturn::failure(ErrorCode::NOSUPPORT),
        }
    }

    fn allocate_grant(&self, processid: ProcessId) -> Result<(), kernel::process::Error> {
        self.apps.enter(processid, |_, _| {})
    }
}