1 // Copyright 2022 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 use std::fs::File; 6 use std::sync::Arc; 7 8 use vulkano::device::Device; 9 use vulkano::device::DeviceExtensions; 10 use vulkano::memory::DeviceMemory; 11 use vulkano::memory::ExternalMemoryHandleType; 12 use vulkano::memory::MemoryAllocateInfo; 13 use vulkano::memory::MemoryImportInfo; 14 15 use crate::rutabaga_gralloc::vulkano_gralloc::VulkanoGralloc; 16 use crate::rutabaga_os::FromRawDescriptor; 17 use crate::rutabaga_os::IntoRawDescriptor; 18 use crate::rutabaga_utils::RUTABAGA_HANDLE_TYPE_MEM_DMABUF; 19 use crate::rutabaga_utils::RUTABAGA_HANDLE_TYPE_MEM_OPAQUE_FD; 20 use crate::RutabagaError; 21 use crate::RutabagaHandle; 22 use crate::RutabagaResult; 23 24 impl VulkanoGralloc { 25 /// Get the extensions that should be enabled. get_desired_device_extensions() -> DeviceExtensions26 pub(crate) fn get_desired_device_extensions() -> DeviceExtensions { 27 DeviceExtensions { 28 khr_dedicated_allocation: true, 29 khr_get_memory_requirements2: true, 30 khr_external_memory: true, 31 khr_external_memory_fd: true, 32 ext_external_memory_dma_buf: true, 33 ..DeviceExtensions::empty() 34 } 35 } 36 37 /// Import memory from a handle. 38 /// 39 /// # Safety 40 /// Safe if the memory handle given is an opaque FD or a DMA buffer handle, and the allocation 41 /// info matches the information at the time the memory was created. import_memory( device: Arc<Device>, allocate_info: MemoryAllocateInfo, handle: RutabagaHandle, ) -> RutabagaResult<DeviceMemory>42 pub(crate) unsafe fn import_memory( 43 device: Arc<Device>, 44 allocate_info: MemoryAllocateInfo, 45 handle: RutabagaHandle, 46 ) -> RutabagaResult<DeviceMemory> { 47 let import_info = MemoryImportInfo::Fd { 48 handle_type: match handle.handle_type { 49 RUTABAGA_HANDLE_TYPE_MEM_DMABUF => ExternalMemoryHandleType::DmaBuf, 50 RUTABAGA_HANDLE_TYPE_MEM_OPAQUE_FD => ExternalMemoryHandleType::OpaqueFd, 51 _ => return Err(RutabagaError::InvalidRutabagaHandle), 52 }, 53 // Safe because we own the handle. 54 file: File::from_raw_descriptor(handle.os_handle.into_raw_descriptor()), 55 }; 56 57 Ok(DeviceMemory::import(device, allocate_info, import_info)?) 58 } 59 } 60