1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use nativewindow::*;
16 
17 /// The configuration of the buffers published by a [BufferPublisher] or
18 /// expected by a [BufferSubscriber].
19 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
20 pub struct StreamConfig {
21     /// Width in pixels of streaming buffers.
22     pub width: u32,
23     /// Height in pixels of streaming buffers.
24     pub height: u32,
25     /// Number of layers of streaming buffers.
26     pub layers: u32,
27     /// Format of streaming buffers.
28     pub format: AHardwareBuffer_Format::Type,
29     /// Usage of streaming buffers.
30     pub usage: AHardwareBuffer_UsageFlags,
31     /// Stride of streaming buffers.
32     pub stride: u32,
33 }
34 
35 impl StreamConfig {
36     /// Tries to create a new HardwareBuffer from settings in a [StreamConfig].
37     pub fn create_hardware_buffer(&self) -> Option<HardwareBuffer> {
38         HardwareBuffer::new(self.width, self.height, self.layers, self.format, self.usage)
39     }
40 }
41 
42 #[cfg(test)]
43 mod test {
44     use super::*;
45 
46     #[test]
47     fn test_create_hardware_buffer() {
48         let config = StreamConfig {
49             width: 123,
50             height: 456,
51             layers: 1,
52             format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
53             usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
54                 | AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
55             stride: 0,
56         };
57 
58         let maybe_buffer = config.create_hardware_buffer();
59         assert!(maybe_buffer.is_some());
60 
61         let buffer = maybe_buffer.unwrap();
62         assert_eq!(config.width, buffer.width());
63         assert_eq!(config.height, buffer.height());
64         assert_eq!(config.format, buffer.format());
65         assert_eq!(config.usage, buffer.usage());
66     }
67 }
68