Skip to main content

display_utils/
types.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::error::Result;
6use crate::pixel_format::PixelFormat;
7use fidl_fuchsia_hardware_display::{
8    BufferCollectionId as FidlBufferCollectionId, ClientPriority as FidlClientPriority,
9    EventId as FidlEventId, ImageId as FidlImageId, Info, LayerId as FidlLayerId,
10    TEST_UTILITY_CLIENT_PRIORITY_VALUE as FidlTestUtilityClientPriorityValue,
11};
12use fidl_fuchsia_hardware_display_types::{
13    AlphaMode, Color as FidlColor, DisplayId as FidlDisplayId, INVALID_DISP_ID,
14};
15use fuchsia_async::OnSignals;
16use std::fmt;
17
18/// Strongly typed wrapper around a display ID.
19#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
20pub struct DisplayId(pub u64);
21
22/// Represents an invalid DisplayId value.
23pub const INVALID_DISPLAY_ID: DisplayId = DisplayId(INVALID_DISP_ID);
24
25impl Default for DisplayId {
26    fn default() -> Self {
27        INVALID_DISPLAY_ID
28    }
29}
30
31impl From<FidlDisplayId> for DisplayId {
32    fn from(fidl_display_id: FidlDisplayId) -> Self {
33        DisplayId(fidl_display_id.value)
34    }
35}
36
37impl From<DisplayId> for FidlDisplayId {
38    fn from(display_id: DisplayId) -> Self {
39        FidlDisplayId { value: display_id.0 }
40    }
41}
42
43/// Strongly typed wrapper around a display driver event ID.
44#[derive(Clone, Copy, Debug, Eq, PartialEq)]
45pub struct EventId(pub u64);
46
47/// Represents an invalid EventId value.
48pub const INVALID_EVENT_ID: EventId = EventId(INVALID_DISP_ID);
49
50impl Default for EventId {
51    fn default() -> Self {
52        INVALID_EVENT_ID
53    }
54}
55
56impl From<FidlEventId> for EventId {
57    fn from(fidl_event_id: FidlEventId) -> Self {
58        EventId(fidl_event_id.value)
59    }
60}
61
62impl From<EventId> for FidlEventId {
63    fn from(event_id: EventId) -> Self {
64        FidlEventId { value: event_id.0 }
65    }
66}
67
68/// Strongly typed wrapper around a display layer ID.
69#[derive(Clone, Copy, Debug, Eq, PartialEq)]
70pub struct LayerId(pub u64);
71
72/// Represents an invalid LayerId value.
73pub const INVALID_LAYER_ID: LayerId = LayerId(INVALID_DISP_ID);
74
75impl Default for LayerId {
76    fn default() -> Self {
77        INVALID_LAYER_ID
78    }
79}
80
81impl From<FidlLayerId> for LayerId {
82    fn from(fidl_layer_id: FidlLayerId) -> Self {
83        LayerId(fidl_layer_id.value)
84    }
85}
86
87impl From<LayerId> for FidlLayerId {
88    fn from(layer_id: LayerId) -> Self {
89        FidlLayerId { value: layer_id.0 }
90    }
91}
92
93/// Strongly typed wrapper around an image ID.
94#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
95pub struct ImageId(pub u64);
96
97/// Represents an invalid ImageId value.
98pub const INVALID_IMAGE_ID: ImageId = ImageId(INVALID_DISP_ID);
99
100impl Default for ImageId {
101    fn default() -> Self {
102        INVALID_IMAGE_ID
103    }
104}
105
106impl From<FidlImageId> for ImageId {
107    fn from(fidl_image_id: FidlImageId) -> Self {
108        ImageId(fidl_image_id.value)
109    }
110}
111
112impl From<ImageId> for FidlImageId {
113    fn from(image_id: ImageId) -> Self {
114        FidlImageId { value: image_id.0 }
115    }
116}
117
118/// Strongly typed wrapper around a sysmem buffer collection ID.
119#[derive(Clone, Copy, Debug, Eq, PartialEq)]
120pub struct BufferCollectionId(pub u64);
121
122impl From<FidlBufferCollectionId> for BufferCollectionId {
123    fn from(fidl_buffer_collection_id: FidlBufferCollectionId) -> Self {
124        BufferCollectionId(fidl_buffer_collection_id.value)
125    }
126}
127
128impl From<BufferCollectionId> for FidlBufferCollectionId {
129    fn from(buffer_collection_id: BufferCollectionId) -> Self {
130        FidlBufferCollectionId { value: buffer_collection_id.0 }
131    }
132}
133
134/// Strongly typed wrapper around a client priority.
135#[derive(Clone, Copy, Debug, Eq, PartialEq)]
136pub struct ClientPriority(pub u32);
137
138impl From<FidlClientPriority> for ClientPriority {
139    fn from(fidl_client_priority: FidlClientPriority) -> Self {
140        ClientPriority(fidl_client_priority.value)
141    }
142}
143
144impl From<ClientPriority> for FidlClientPriority {
145    fn from(client_priority: ClientPriority) -> Self {
146        FidlClientPriority { value: client_priority.0 }
147    }
148}
149
150/// The priority level used by display test utilities.
151pub const TEST_UTILITY_CLIENT_PRIORITY: ClientPriority =
152    ClientPriority(FidlTestUtilityClientPriorityValue);
153
154/// Enhances the `fuchsia.hardware.display.Info` FIDL struct.
155#[derive(Clone, Debug)]
156pub struct DisplayInfo(pub Info);
157
158impl DisplayInfo {
159    /// Returns the ID for this display.
160    pub fn id(&self) -> DisplayId {
161        self.0.id.into()
162    }
163}
164
165/// Custom user-friendly format representation.
166impl fmt::Display for DisplayInfo {
167    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168        writeln!(f, "Display (id: {})", self.0.id.value)?;
169        writeln!(f, "\tManufacturer Name: \"{}\"", self.0.manufacturer_name)?;
170        writeln!(f, "\tMonitor Name: \"{}\"", self.0.monitor_name)?;
171        writeln!(f, "\tMonitor Serial: \"{}\"", self.0.monitor_serial)?;
172        writeln!(
173            f,
174            "\tPhysical Dimensions: {}mm x {}mm",
175            self.0.horizontal_size_mm, self.0.vertical_size_mm
176        )?;
177
178        writeln!(f, "\tPixel Formats:")?;
179        for (i, format) in self.0.pixel_format.iter().map(PixelFormat::from).enumerate() {
180            writeln!(f, "\t\t{}:\t{}", i, format)?;
181        }
182
183        writeln!(f, "\tDisplay Modes:")?;
184        for (i, mode) in self.0.modes.iter().enumerate() {
185            writeln!(
186                f,
187                "\t\t{}:\t{:.2} Hz @ {}x{}",
188                i,
189                (mode.refresh_rate_millihertz as f32) / 1000.,
190                mode.active_area.width,
191                mode.active_area.height
192            )?;
193        }
194
195        write!(f, "")
196    }
197}
198
199/// A zircon event that has been registered with the display driver.
200pub struct Event {
201    id: EventId,
202    event: zx::Event,
203}
204
205impl Event {
206    pub(crate) fn new(id: EventId, event: zx::Event) -> Event {
207        Event { id, event }
208    }
209
210    /// Returns the ID for this event.
211    pub fn id(&self) -> EventId {
212        self.id
213    }
214
215    /// Returns a future that completes when the event has been signaled.
216    pub async fn wait(&self) -> Result<()> {
217        OnSignals::new(&self.event, zx::Signals::EVENT_SIGNALED).await?;
218        self.event.as_handle_ref().signal(zx::Signals::EVENT_SIGNALED, zx::Signals::NONE)?;
219        Ok(())
220    }
221
222    /// Signals the event.
223    pub fn signal(&self) -> Result<()> {
224        self.event.as_handle_ref().signal(zx::Signals::NONE, zx::Signals::EVENT_SIGNALED)?;
225        Ok(())
226    }
227}
228
229/// Represents the alpha blending configuration for a layer.
230#[derive(Clone, Copy, Debug, PartialEq)]
231pub struct Alpha {
232    /// The blending mode to use.
233    pub mode: AlphaMode,
234    /// The constant alpha value to apply, in the range [0.0, 1.0].
235    pub val: f32,
236}
237
238/// Enhances the `fuchsia.hardware.display.typers.Color` FIDL struct.
239#[derive(Clone, Copy, Debug, PartialEq)]
240pub struct Color {
241    /// The format of `bytes`.
242    pub format: PixelFormat,
243
244    /// The constant color, represented as one pixel using `format`.
245    pub bytes: [u8; 8],
246}
247
248impl From<FidlColor> for Color {
249    fn from(fidl_color: FidlColor) -> Self {
250        Color { format: fidl_color.format.into(), bytes: fidl_color.bytes }
251    }
252}
253
254impl From<&FidlColor> for Color {
255    fn from(fidl_color: &FidlColor) -> Self {
256        Self::from(*fidl_color)
257    }
258}
259
260impl From<Color> for FidlColor {
261    fn from(color: Color) -> Self {
262        FidlColor { format: color.format.into(), bytes: color.bytes }
263    }
264}
265
266impl From<&Color> for FidlColor {
267    fn from(color: &Color) -> Self {
268        Self::from(*color)
269    }
270}
271
272#[cfg(test)]
273mod tests {
274    use super::*;
275    use fidl_fuchsia_images2::PixelFormat as FidlPixelFormat;
276
277    #[fuchsia::test]
278    fn layer_id_from_fidl_layer_id() {
279        assert_eq!(LayerId(1), LayerId::from(FidlLayerId { value: 1 }));
280        assert_eq!(LayerId(2), LayerId::from(FidlLayerId { value: 2 }));
281        const LARGE: u64 = 1 << 63;
282        assert_eq!(LayerId(LARGE), LayerId::from(FidlLayerId { value: LARGE }));
283        assert_eq!(INVALID_LAYER_ID, LayerId::from(FidlLayerId { value: INVALID_DISP_ID }));
284    }
285
286    #[fuchsia::test]
287    fn fidl_layer_id_from_layer_id() {
288        assert_eq!(FidlLayerId { value: 1 }, FidlLayerId::from(LayerId(1)));
289        assert_eq!(FidlLayerId { value: 2 }, FidlLayerId::from(LayerId(2)));
290        const LARGE: u64 = 1 << 63;
291        assert_eq!(FidlLayerId { value: LARGE }, FidlLayerId::from(LayerId(LARGE)));
292        assert_eq!(FidlLayerId { value: INVALID_DISP_ID }, FidlLayerId::from(INVALID_LAYER_ID));
293    }
294
295    #[fuchsia::test]
296    fn fidl_layer_id_to_layer_id() {
297        assert_eq!(LayerId(1), FidlLayerId { value: 1 }.into());
298        assert_eq!(LayerId(2), FidlLayerId { value: 2 }.into());
299        const LARGE: u64 = 1 << 63;
300        assert_eq!(LayerId(LARGE), FidlLayerId { value: LARGE }.into());
301        assert_eq!(INVALID_LAYER_ID, FidlLayerId { value: INVALID_DISP_ID }.into());
302    }
303
304    #[fuchsia::test]
305    fn layer_id_to_fidl_layer_id() {
306        assert_eq!(FidlLayerId { value: 1 }, LayerId(1).into());
307        assert_eq!(FidlLayerId { value: 2 }, LayerId(2).into());
308        const LARGE: u64 = 1 << 63;
309        assert_eq!(FidlLayerId { value: LARGE }, LayerId(LARGE).into());
310        assert_eq!(FidlLayerId { value: INVALID_DISP_ID }, INVALID_LAYER_ID.into());
311    }
312
313    #[fuchsia::test]
314    fn layer_id_default() {
315        let default: LayerId = Default::default();
316        assert_eq!(default, INVALID_LAYER_ID);
317    }
318
319    #[fuchsia::test]
320    fn display_id_from_fidl_display_id() {
321        assert_eq!(DisplayId(1), DisplayId::from(FidlDisplayId { value: 1 }));
322        assert_eq!(DisplayId(2), DisplayId::from(FidlDisplayId { value: 2 }));
323        const LARGE: u64 = 1 << 63;
324        assert_eq!(DisplayId(LARGE), DisplayId::from(FidlDisplayId { value: LARGE }));
325        assert_eq!(INVALID_DISPLAY_ID, DisplayId::from(FidlDisplayId { value: INVALID_DISP_ID }));
326    }
327
328    #[fuchsia::test]
329    fn fidl_display_id_from_display_id() {
330        assert_eq!(FidlDisplayId { value: 1 }, FidlDisplayId::from(DisplayId(1)));
331        assert_eq!(FidlDisplayId { value: 2 }, FidlDisplayId::from(DisplayId(2)));
332        const LARGE: u64 = 1 << 63;
333        assert_eq!(FidlDisplayId { value: LARGE }, FidlDisplayId::from(DisplayId(LARGE)));
334        assert_eq!(
335            FidlDisplayId { value: INVALID_DISP_ID },
336            FidlDisplayId::from(INVALID_DISPLAY_ID)
337        );
338    }
339
340    #[fuchsia::test]
341    fn fidl_display_id_to_display_id() {
342        assert_eq!(DisplayId(1), FidlDisplayId { value: 1 }.into());
343        assert_eq!(DisplayId(2), FidlDisplayId { value: 2 }.into());
344        const LARGE: u64 = 1 << 63;
345        assert_eq!(DisplayId(LARGE), FidlDisplayId { value: LARGE }.into());
346        assert_eq!(INVALID_DISPLAY_ID, FidlDisplayId { value: INVALID_DISP_ID }.into());
347    }
348
349    #[fuchsia::test]
350    fn display_id_to_fidl_display_id() {
351        assert_eq!(FidlDisplayId { value: 1 }, DisplayId(1).into());
352        assert_eq!(FidlDisplayId { value: 2 }, DisplayId(2).into());
353        const LARGE: u64 = 1 << 63;
354        assert_eq!(FidlDisplayId { value: LARGE }, DisplayId(LARGE).into());
355        assert_eq!(FidlDisplayId { value: INVALID_DISP_ID }, INVALID_DISPLAY_ID.into());
356    }
357
358    #[fuchsia::test]
359    fn display_id_default() {
360        let default: DisplayId = Default::default();
361        assert_eq!(default, INVALID_DISPLAY_ID);
362    }
363
364    #[fuchsia::test]
365    fn buffer_collection_id_from_fidl_buffer_collection_id() {
366        assert_eq!(
367            BufferCollectionId(1),
368            BufferCollectionId::from(FidlBufferCollectionId { value: 1 })
369        );
370        assert_eq!(
371            BufferCollectionId(2),
372            BufferCollectionId::from(FidlBufferCollectionId { value: 2 })
373        );
374        const LARGE: u64 = 1 << 63;
375        assert_eq!(
376            BufferCollectionId(LARGE),
377            BufferCollectionId::from(FidlBufferCollectionId { value: LARGE })
378        );
379    }
380
381    #[fuchsia::test]
382    fn fidl_buffer_collection_id_from_buffer_collection_id() {
383        assert_eq!(
384            FidlBufferCollectionId { value: 1 },
385            FidlBufferCollectionId::from(BufferCollectionId(1))
386        );
387        assert_eq!(
388            FidlBufferCollectionId { value: 2 },
389            FidlBufferCollectionId::from(BufferCollectionId(2))
390        );
391        const LARGE: u64 = 1 << 63;
392        assert_eq!(
393            FidlBufferCollectionId { value: LARGE },
394            FidlBufferCollectionId::from(BufferCollectionId(LARGE))
395        );
396    }
397
398    #[fuchsia::test]
399    fn fidl_buffer_collection_id_to_buffer_collection_id() {
400        assert_eq!(BufferCollectionId(1), FidlBufferCollectionId { value: 1 }.into());
401        assert_eq!(BufferCollectionId(2), FidlBufferCollectionId { value: 2 }.into());
402        const LARGE: u64 = 1 << 63;
403        assert_eq!(BufferCollectionId(LARGE), FidlBufferCollectionId { value: LARGE }.into());
404    }
405
406    #[fuchsia::test]
407    fn buffer_collection_id_to_fidl_buffer_collection_id() {
408        assert_eq!(FidlBufferCollectionId { value: 1 }, BufferCollectionId(1).into());
409        assert_eq!(FidlBufferCollectionId { value: 2 }, BufferCollectionId(2).into());
410        const LARGE: u64 = 1 << 63;
411        assert_eq!(FidlBufferCollectionId { value: LARGE }, BufferCollectionId(LARGE).into());
412    }
413
414    #[fuchsia::test]
415    fn event_id_from_fidl_event_id() {
416        assert_eq!(EventId(1), EventId::from(FidlEventId { value: 1 }));
417        assert_eq!(EventId(2), EventId::from(FidlEventId { value: 2 }));
418        const LARGE: u64 = 1 << 63;
419        assert_eq!(EventId(LARGE), EventId::from(FidlEventId { value: LARGE }));
420        assert_eq!(INVALID_EVENT_ID, EventId::from(FidlEventId { value: INVALID_DISP_ID }));
421    }
422
423    #[fuchsia::test]
424    fn fidl_event_id_from_event_id() {
425        assert_eq!(FidlEventId { value: 1 }, FidlEventId::from(EventId(1)));
426        assert_eq!(FidlEventId { value: 2 }, FidlEventId::from(EventId(2)));
427        const LARGE: u64 = 1 << 63;
428        assert_eq!(FidlEventId { value: LARGE }, FidlEventId::from(EventId(LARGE)));
429        assert_eq!(FidlEventId { value: INVALID_DISP_ID }, FidlEventId::from(INVALID_EVENT_ID));
430    }
431
432    #[fuchsia::test]
433    fn fidl_event_id_to_event_id() {
434        assert_eq!(EventId(1), FidlEventId { value: 1 }.into());
435        assert_eq!(EventId(2), FidlEventId { value: 2 }.into());
436        const LARGE: u64 = 1 << 63;
437        assert_eq!(EventId(LARGE), FidlEventId { value: LARGE }.into());
438        assert_eq!(INVALID_EVENT_ID, FidlEventId { value: INVALID_DISP_ID }.into());
439    }
440
441    #[fuchsia::test]
442    fn event_id_to_fidl_event_id() {
443        assert_eq!(FidlEventId { value: 1 }, EventId(1).into());
444        assert_eq!(FidlEventId { value: 2 }, EventId(2).into());
445        const LARGE: u64 = 1 << 63;
446        assert_eq!(FidlEventId { value: LARGE }, EventId(LARGE).into());
447        assert_eq!(FidlEventId { value: INVALID_DISP_ID }, INVALID_EVENT_ID.into());
448    }
449
450    #[fuchsia::test]
451    fn event_id_default() {
452        let default: EventId = Default::default();
453        assert_eq!(default, INVALID_EVENT_ID);
454    }
455
456    #[fuchsia::test]
457    fn image_id_from_fidl_image_id() {
458        assert_eq!(ImageId(1), ImageId::from(FidlImageId { value: 1 }));
459        assert_eq!(ImageId(2), ImageId::from(FidlImageId { value: 2 }));
460        const LARGE: u64 = 1 << 63;
461        assert_eq!(ImageId(LARGE), ImageId::from(FidlImageId { value: LARGE }));
462        assert_eq!(INVALID_IMAGE_ID, ImageId::from(FidlImageId { value: INVALID_DISP_ID }));
463    }
464
465    #[fuchsia::test]
466    fn fidl_image_id_from_image_id() {
467        assert_eq!(FidlImageId { value: 1 }, FidlImageId::from(ImageId(1)));
468        assert_eq!(FidlImageId { value: 2 }, FidlImageId::from(ImageId(2)));
469        const LARGE: u64 = 1 << 63;
470        assert_eq!(FidlImageId { value: LARGE }, FidlImageId::from(ImageId(LARGE)));
471        assert_eq!(FidlImageId { value: INVALID_DISP_ID }, FidlImageId::from(INVALID_IMAGE_ID));
472    }
473
474    #[fuchsia::test]
475    fn fidl_image_id_to_image_id() {
476        assert_eq!(ImageId(1), FidlImageId { value: 1 }.into());
477        assert_eq!(ImageId(2), FidlImageId { value: 2 }.into());
478        const LARGE: u64 = 1 << 63;
479        assert_eq!(ImageId(LARGE), FidlImageId { value: LARGE }.into());
480        assert_eq!(INVALID_IMAGE_ID, FidlImageId { value: INVALID_DISP_ID }.into());
481    }
482
483    #[fuchsia::test]
484    fn image_id_to_fidl_image_id() {
485        assert_eq!(FidlImageId { value: 1 }, ImageId(1).into());
486        assert_eq!(FidlImageId { value: 2 }, ImageId(2).into());
487        const LARGE: u64 = 1 << 63;
488        assert_eq!(FidlImageId { value: LARGE }, ImageId(LARGE).into());
489        assert_eq!(FidlImageId { value: INVALID_DISP_ID }, INVALID_IMAGE_ID.into());
490    }
491
492    #[fuchsia::test]
493    fn image_id_default() {
494        let default: ImageId = Default::default();
495        assert_eq!(default, INVALID_IMAGE_ID);
496    }
497
498    #[fuchsia::test]
499    fn color_from_fidl_color() {
500        assert_eq!(
501            Color {
502                format: PixelFormat::R8G8B8A8,
503                bytes: [0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48]
504            },
505            Color::from(FidlColor {
506                format: FidlPixelFormat::R8G8B8A8,
507                bytes: [0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48],
508            })
509        );
510    }
511
512    #[fuchsia::test]
513    fn fidl_color_from_color() {
514        assert_eq!(
515            FidlColor {
516                format: FidlPixelFormat::R8G8B8A8,
517                bytes: [0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48]
518            },
519            FidlColor::from(Color {
520                format: PixelFormat::R8G8B8A8,
521                bytes: [0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48],
522            })
523        );
524    }
525
526    #[fuchsia::test]
527    fn fidl_color_to_color() {
528        assert_eq!(
529            Color {
530                format: PixelFormat::R8G8B8A8,
531                bytes: [0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48]
532            },
533            FidlColor {
534                format: FidlPixelFormat::R8G8B8A8,
535                bytes: [0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48]
536            }
537            .into()
538        );
539    }
540
541    #[fuchsia::test]
542    fn color_to_fidl_color() {
543        assert_eq!(
544            FidlColor {
545                format: FidlPixelFormat::R8G8B8A8,
546                bytes: [0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48]
547            },
548            Color {
549                format: PixelFormat::R8G8B8A8,
550                bytes: [0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48]
551            }
552            .into()
553        );
554    }
555}