rutabaga_gfx/rutabaga_gralloc/
system_gralloc.rs

1// Copyright 2021 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Utility file for allocating exportable system memory.  On Linux systems,
6//! this is is often done with memfd.
7
8use crate::rutabaga_gralloc::formats::canonical_image_requirements;
9use crate::rutabaga_gralloc::gralloc::Gralloc;
10use crate::rutabaga_gralloc::gralloc::ImageAllocationInfo;
11use crate::rutabaga_gralloc::gralloc::ImageMemoryRequirements;
12use crate::rutabaga_os::SharedMemory;
13use crate::rutabaga_utils::*;
14
15/// A gralloc implementation capable of allocation from system memory.
16pub struct SystemGralloc(());
17
18impl SystemGralloc {
19    fn new() -> Self {
20        SystemGralloc(())
21    }
22
23    /// Returns a new `SystemGralloc` instance.
24    pub fn init() -> RutabagaResult<Box<dyn Gralloc>> {
25        Ok(Box::new(SystemGralloc::new()))
26    }
27}
28
29impl Gralloc for SystemGralloc {
30    fn supports_external_gpu_memory(&self) -> bool {
31        false
32    }
33
34    fn supports_dmabuf(&self) -> bool {
35        false
36    }
37
38    fn get_image_memory_requirements(
39        &mut self,
40        info: ImageAllocationInfo,
41    ) -> RutabagaResult<ImageMemoryRequirements> {
42        let mut reqs = canonical_image_requirements(info)?;
43        reqs.map_info = RUTABAGA_MAP_CACHE_CACHED;
44        Ok(reqs)
45    }
46
47    fn allocate_memory(&mut self, reqs: ImageMemoryRequirements) -> RutabagaResult<RutabagaHandle> {
48        let shm = SharedMemory::new("rutabaga_gralloc", reqs.size)?;
49        Ok(RutabagaHandle {
50            os_handle: shm.into(),
51            handle_type: RUTABAGA_MEM_HANDLE_TYPE_SHM,
52        })
53    }
54}