xref: /aosp_15_r20/external/crosvm/rutabaga_gfx/src/rutabaga_gralloc/system_gralloc.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
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 
8 use crate::rutabaga_gralloc::formats::canonical_image_requirements;
9 use crate::rutabaga_gralloc::gralloc::Gralloc;
10 use crate::rutabaga_gralloc::gralloc::ImageAllocationInfo;
11 use crate::rutabaga_gralloc::gralloc::ImageMemoryRequirements;
12 use crate::rutabaga_os::SharedMemory;
13 use crate::rutabaga_utils::*;
14 
15 /// A gralloc implementation capable of allocation from system memory.
16 pub struct SystemGralloc(());
17 
18 impl SystemGralloc {
new() -> Self19     fn new() -> Self {
20         SystemGralloc(())
21     }
22 
23     /// Returns a new `SystemGralloc` instance.
init() -> RutabagaResult<Box<dyn Gralloc>>24     pub fn init() -> RutabagaResult<Box<dyn Gralloc>> {
25         Ok(Box::new(SystemGralloc::new()))
26     }
27 }
28 
29 impl Gralloc for SystemGralloc {
supports_external_gpu_memory(&self) -> bool30     fn supports_external_gpu_memory(&self) -> bool {
31         false
32     }
33 
supports_dmabuf(&self) -> bool34     fn supports_dmabuf(&self) -> bool {
35         false
36     }
37 
get_image_memory_requirements( &mut self, info: ImageAllocationInfo, ) -> RutabagaResult<ImageMemoryRequirements>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 
allocate_memory(&mut self, reqs: ImageMemoryRequirements) -> RutabagaResult<RutabagaHandle>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 }
55