• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::sync::Arc;
6 
7 use vulkano::device::Device;
8 use vulkano::device::DeviceExtensions;
9 use vulkano::memory::DeviceMemory;
10 use vulkano::memory::ExternalMemoryHandleType;
11 use vulkano::memory::MemoryAllocateInfo;
12 use vulkano::memory::MemoryImportInfo;
13 
14 use crate::rutabaga_gralloc::vulkano_gralloc::VulkanoGralloc;
15 use crate::rutabaga_os::AsRawDescriptor;
16 use crate::rutabaga_utils::RUTABAGA_MEM_HANDLE_TYPE_OPAQUE_WIN32;
17 use crate::RutabagaError;
18 use crate::RutabagaHandle;
19 use crate::RutabagaResult;
20 
21 impl VulkanoGralloc {
22     /// Get the extensions that should be enabled.
get_desired_device_extensions() -> DeviceExtensions23     pub(crate) fn get_desired_device_extensions() -> DeviceExtensions {
24         DeviceExtensions {
25             khr_dedicated_allocation: true,
26             khr_get_memory_requirements2: true,
27             khr_external_memory: true,
28             khr_external_memory_win32: true,
29             ..DeviceExtensions::empty()
30         }
31     }
32 
33     /// Import memory from a handle.
34     ///
35     /// # Safety
36     /// Safe if the memory handle given is an opaque Win32 handle, and the allocation info matches
37     /// the information at the time the memory was created.
import_memory( device: Arc<Device>, allocate_info: MemoryAllocateInfo, handle: RutabagaHandle, ) -> RutabagaResult<DeviceMemory>38     pub(crate) unsafe fn import_memory(
39         device: Arc<Device>,
40         allocate_info: MemoryAllocateInfo,
41         handle: RutabagaHandle,
42     ) -> RutabagaResult<DeviceMemory> {
43         let import_info = MemoryImportInfo::Win32 {
44             handle_type: match handle.handle_type {
45                 RUTABAGA_MEM_HANDLE_TYPE_OPAQUE_WIN32 => ExternalMemoryHandleType::OpaqueWin32,
46                 _ => return Err(RutabagaError::InvalidRutabagaHandle),
47             },
48             handle: handle.os_handle.as_raw_descriptor(),
49         };
50 
51         Ok(DeviceMemory::import(device, allocate_info, import_info)?)
52     }
53 }
54