summaryrefslogtreecommitdiff
path: root/libs/bufferstreams/rust/src/stream_config.rs
blob: 454bdf144eb1f8ee17ab8f2b995fcd5c541caeed (plain)
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
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use nativewindow::*;

/// The configuration of the buffers published by a [BufferPublisher] or
/// expected by a [BufferSubscriber].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct StreamConfig {
    /// Width in pixels of streaming buffers.
    pub width: u32,
    /// Height in pixels of streaming buffers.
    pub height: u32,
    /// Number of layers of streaming buffers.
    pub layers: u32,
    /// Format of streaming buffers.
    pub format: AHardwareBuffer_Format::Type,
    /// Usage of streaming buffers.
    pub usage: AHardwareBuffer_UsageFlags,
    /// Stride of streaming buffers.
    pub stride: u32,
}

impl StreamConfig {
    /// Tries to create a new HardwareBuffer from settings in a [StreamConfig].
    pub fn create_hardware_buffer(&self) -> Option<HardwareBuffer> {
        HardwareBuffer::new(self.width, self.height, self.layers, self.format, self.usage)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_create_hardware_buffer() {
        let config = StreamConfig {
            width: 123,
            height: 456,
            layers: 1,
            format: AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
            usage: AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN
                | AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
            stride: 0,
        };

        let maybe_buffer = config.create_hardware_buffer();
        assert!(maybe_buffer.is_some());

        let buffer = maybe_buffer.unwrap();
        assert_eq!(config.width, buffer.width());
        assert_eq!(config.height, buffer.height());
        assert_eq!(config.format, buffer.format());
        assert_eq!(config.usage, buffer.usage());
    }
}