cognitive_graphics/attributes.rs
1// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
2// the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/
3
4//! Defines for attributes for creation/importing hardware images.
5
6use std;
7use std::os::unix::io::RawFd;
8
9// -------------------------------------------------------------------------------------------------
10
11pub const MAX_PLANES: usize = 3;
12
13// -------------------------------------------------------------------------------------------------
14
15/// Result of validation of image attributes.
16#[derive(PartialEq)]
17pub enum ValidationResult {
18 /// Plane index out of bounds.
19 PlaneIdx,
20
21 /// The plane index was already set.
22 PlaneSet,
23
24 /// Missing or too many planes to create a buffer.
25 Incomplete,
26
27 /// Format not supported.
28 InvalidFormat,
29
30 /// Invalid width or height.
31 InvalidDimensions,
32
33 /// Offset + stride * height goes out of dmabuf bounds.
34 OutOfBounds,
35
36 /// Everything Ok
37 Ok,
38}
39
40// -------------------------------------------------------------------------------------------------
41
42/// Raw hardware image.
43pub type RawHwImage = *const std::os::raw::c_void;
44
45// -------------------------------------------------------------------------------------------------
46
47/// Attributes for creation EGL image.
48#[derive(Debug, Clone)]
49pub struct EglAttributes {
50 pub name: u32,
51 pub width: i32,
52 pub height: i32,
53 pub stride: u32,
54 pub format: u32,
55}
56
57// -------------------------------------------------------------------------------------------------
58
59impl EglAttributes {
60 /// Constructs new `EglAttributes`.
61 pub fn new(name: u32, width: i32, height: i32, stride: u32, format: u32) -> Self {
62 EglAttributes {
63 name: name,
64 width: width,
65 height: height,
66 stride: stride,
67 format: format,
68 }
69 }
70}
71
72// -------------------------------------------------------------------------------------------------
73
74/// Attributes for creation of plane for EGL image from dmabuf.
75#[derive(Debug, Copy, Clone)]
76pub struct DmabufPlane {
77 pub fd: RawFd,
78 pub offset: u32,
79 pub stride: u32,
80 pub modifier_hi: u32,
81 pub modifier_lo: u32,
82}
83
84// -------------------------------------------------------------------------------------------------
85
86impl DmabufPlane {
87 /// Constructs new `DmabufPlane`.
88 fn new(fd: RawFd, offset: u32, stride: u32, modifier_hi: u32, modifier_lo: u32) -> Self {
89 DmabufPlane {
90 fd: fd,
91 offset: offset,
92 stride: stride,
93 modifier_hi: modifier_hi,
94 modifier_lo: modifier_lo,
95 }
96 }
97
98 /// Constructs default `DmabufPlane`.
99 fn default() -> Self {
100 DmabufPlane {
101 fd: -1,
102 offset: 0,
103 stride: 0,
104 modifier_hi: 0x0,
105 modifier_lo: 0x0,
106 }
107 }
108}
109
110// -------------------------------------------------------------------------------------------------
111
112/// Attributes for creation of EGL image from dmabuf.
113///
114/// TODO: Add unit tests for `DmabufAttributes`.
115#[derive(Debug, Clone)]
116pub struct DmabufAttributes {
117 pub width: i32,
118 pub height: i32,
119 pub format: u32,
120 pub flags: u32,
121 pub num_planes: usize,
122 pub planes: [DmabufPlane; MAX_PLANES],
123}
124
125// -------------------------------------------------------------------------------------------------
126
127impl DmabufAttributes {
128 /// Constructs new `DmabufAttributes`.
129 pub fn new() -> Self {
130 DmabufAttributes {
131 width: 0,
132 height: 0,
133 format: 0,
134 flags: 0x0,
135 num_planes: 0,
136 planes: [DmabufPlane::default(), DmabufPlane::default(), DmabufPlane::default()],
137 }
138 }
139
140 /// Adds attributes for plane.
141 pub fn add(&mut self,
142 plane_idx: usize,
143 fd: RawFd,
144 offset: u32,
145 stride: u32,
146 modifier_hi: u32,
147 modifier_lo: u32)
148 -> ValidationResult {
149 if plane_idx >= MAX_PLANES {
150 return ValidationResult::PlaneIdx;
151 }
152
153 if self.planes[plane_idx].fd != -1 {
154 return ValidationResult::PlaneSet;
155 }
156
157 self.planes[plane_idx] = DmabufPlane::new(fd, offset, stride, modifier_hi, modifier_lo);
158 self.num_planes += 1;
159 ValidationResult::Ok
160 }
161
162 /// Sets image parameters.
163 pub fn create(&mut self, width: i32, height: i32, format: u32, flags: u32) {
164 self.width = width;
165 self.height = height;
166 self.format = format;
167 self.flags = flags;
168 }
169
170 /// Validates the attributes.
171 ///
172 /// TODO: Add more validation checks.
173 pub fn validate(&self) -> ValidationResult {
174 for i in 0..self.num_planes {
175 if self.planes[i].fd == -1 {
176 return ValidationResult::Incomplete;
177 }
178 }
179 ValidationResult::Ok
180 }
181
182 /// Returns number of planes configured.
183 pub fn get_num_of_planes(&self) -> usize {
184 self.num_planes
185 }
186}
187
188// -------------------------------------------------------------------------------------------------