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 From<StreamConfig> for HardwareBufferDescription { from(config: StreamConfig) -> Self36 fn from(config: StreamConfig) -> Self { 37 HardwareBufferDescription::new( 38 config.width, 39 config.height, 40 config.layers, 41 config.format, 42 config.usage, 43 config.stride, 44 ) 45 } 46 } 47 48 impl StreamConfig { 49 /// Tries to create a new HardwareBuffer from settings in a [StreamConfig]. create_hardware_buffer(&self) -> Option<HardwareBuffer>50 pub fn create_hardware_buffer(&self) -> Option<HardwareBuffer> { 51 HardwareBuffer::new(&(*self).into()) 52 } 53 } 54 55 #[cfg(test)] 56 mod test { 57 use super::*; 58 59 #[test] test_create_hardware_buffer()60 fn test_create_hardware_buffer() { 61 let config = StreamConfig { 62 width: 123, 63 height: 456, 64 layers: 1, 65 format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM, 66 usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN 67 | AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, 68 stride: 0, 69 }; 70 71 let maybe_buffer = config.create_hardware_buffer(); 72 assert!(maybe_buffer.is_some()); 73 74 let buffer = maybe_buffer.unwrap(); 75 let description = buffer.description(); 76 assert_eq!(config.width, description.width()); 77 assert_eq!(config.height, description.height()); 78 assert_eq!(config.format, description.format()); 79 assert_eq!(config.usage, description.usage()); 80 } 81 } 82