xref: /aosp_15_r20/external/swiftshader/src/Vulkan/VkDeviceMemory.hpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef VK_DEVICE_MEMORY_HPP_
16 #define VK_DEVICE_MEMORY_HPP_
17 
18 #include "VkConfig.hpp"
19 #include "VkObject.hpp"
20 
21 namespace vk {
22 
23 class Device;
24 
25 class DeviceMemory
26 {
27 public:
28 	struct ExtendedAllocationInfo
29 	{
30 		VkDeviceSize allocationSize = 0;
31 		uint32_t memoryTypeIndex = 0;
32 		uint64_t opaqueCaptureAddress = 0;
33 		const VkExportMemoryAllocateInfo *exportMemoryAllocateInfo = nullptr;
34 		const VkImportMemoryHostPointerInfoEXT *importMemoryHostPointerInfo = nullptr;
35 #if SWIFTSHADER_EXTERNAL_MEMORY_OPAQUE_FD
36 		const VkImportMemoryFdInfoKHR *importMemoryFdInfo = nullptr;
37 #endif
38 #if SWIFTSHADER_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER
39 		const VkImportAndroidHardwareBufferInfoANDROID *importAndroidHardwareBufferInfo = nullptr;
40 		const VkMemoryDedicatedAllocateInfo *dedicatedAllocateInfo = nullptr;
41 #endif
42 #if VK_USE_PLATFORM_FUCHSIA
43 		const VkImportMemoryZirconHandleInfoFUCHSIA *importMemoryZirconHandleInfo = nullptr;
44 #endif
45 	};
46 
47 protected:
48 	DeviceMemory(const VkMemoryAllocateInfo *pCreateInfo, const DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, Device *pDevice);
49 
50 public:
~DeviceMemory()51 	virtual ~DeviceMemory() {}
52 
53 	static VkResult Allocate(const VkAllocationCallbacks *pAllocator, const VkMemoryAllocateInfo *pAllocateInfo, VkDeviceMemory *pMemory, Device *device);
54 
operator VkDeviceMemory()55 	operator VkDeviceMemory()
56 	{
57 		return vk::TtoVkT<DeviceMemory, VkDeviceMemory>(this);
58 	}
59 
Cast(VkDeviceMemory object)60 	static inline DeviceMemory *Cast(VkDeviceMemory object)
61 	{
62 		return vk::VkTtoT<DeviceMemory, VkDeviceMemory>(object);
63 	}
64 
65 	static size_t ComputeRequiredAllocationSize(const VkMemoryAllocateInfo *pCreateInfo);
66 
67 #if SWIFTSHADER_EXTERNAL_MEMORY_OPAQUE_FD
68 	virtual VkResult exportFd(int *pFd) const;
69 #endif
70 
71 #if SWIFTSHADER_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER
72 	virtual VkResult exportAndroidHardwareBuffer(struct AHardwareBuffer **pAhb) const;
73 	static VkResult GetAndroidHardwareBufferProperties(VkDevice &device, const struct AHardwareBuffer *buffer, VkAndroidHardwareBufferPropertiesANDROID *pProperties);
74 #endif
75 
76 #if VK_USE_PLATFORM_FUCHSIA
77 	virtual VkResult exportHandle(zx_handle_t *pHandle) const;
78 #endif
79 
80 	void destroy(const VkAllocationCallbacks *pAllocator);
81 	VkResult allocate();
82 	VkResult map(VkDeviceSize offset, VkDeviceSize size, void **ppData);
83 	VkDeviceSize getCommittedMemoryInBytes() const;
84 	void *getOffsetPointer(VkDeviceSize pOffset) const;
85 	uint64_t getOpaqueCaptureAddress() const;
getMemoryTypeIndex() const86 	uint32_t getMemoryTypeIndex() const { return memoryTypeIndex; }
87 
88 	// If this is external memory, return true iff its handle type matches the bitmask
89 	// provided by |supportedExternalHandleTypes|. Otherwise, always return true.
90 	bool checkExternalMemoryHandleType(
91 	    VkExternalMemoryHandleTypeFlags supportedExternalMemoryHandleType) const;
92 
93 	// Some external device memories, such as Android hardware buffers, store per-plane properties.
hasExternalImagePlanes() const94 	virtual bool hasExternalImagePlanes() const { return false; }
externalImageRowPitchBytes(VkImageAspectFlagBits aspect) const95 	virtual int externalImageRowPitchBytes(VkImageAspectFlagBits aspect) const { return 0; }
externalImageMemoryOffset(VkImageAspectFlagBits aspect) const96 	virtual VkDeviceSize externalImageMemoryOffset(VkImageAspectFlagBits aspect) const { return 0; }
97 
98 protected:
99 	// Allocate the memory according to `allocationSize`. On success return VK_SUCCESS and sets `buffer`.
100 	virtual VkResult allocateBuffer();
101 
102 	// Free previously allocated memory at `buffer`.
103 	virtual void freeBuffer();
104 
105 	// Return the handle type flag bit supported by this implementation.
106 	// A value of 0 corresponds to non-external memory.
107 	virtual VkExternalMemoryHandleTypeFlagBits getFlagBit() const;
108 
109 #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
isImport() const110 	virtual bool isImport() const
111 	{
112 		return false;
113 	}
114 
getMemoryObjectId() const115 	virtual uint64_t getMemoryObjectId() const
116 	{
117 		return (uint64_t)buffer;
118 	}
119 #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
120 
121 	void *buffer = nullptr;
122 	const VkDeviceSize allocationSize;
123 	const uint32_t memoryTypeIndex;
124 	uint64_t opaqueCaptureAddress = 0;
125 	Device *const device;
126 
127 private:
128 	static VkResult ParseAllocationInfo(const VkMemoryAllocateInfo *pAllocateInfo, DeviceMemory::ExtendedAllocationInfo *extendedAllocationInfo);
129 	static VkResult Allocate(const VkAllocationCallbacks *pAllocator, const VkMemoryAllocateInfo *pAllocateInfo, VkDeviceMemory *pMemory,
130 	                         const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, Device *device);
131 };
132 
133 // This class represents a DeviceMemory object with no external memory
134 class DeviceMemoryInternal : public DeviceMemory, public ObjectBase<DeviceMemoryInternal, VkDeviceMemory>
135 {
136 public:
DeviceMemoryInternal(const VkMemoryAllocateInfo * pCreateInfo,void * mem,const DeviceMemory::ExtendedAllocationInfo & extendedAllocationInfo,Device * pDevice)137 	DeviceMemoryInternal(const VkMemoryAllocateInfo *pCreateInfo, void *mem, const DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, Device *pDevice)
138 	    : DeviceMemory(pCreateInfo, extendedAllocationInfo, pDevice)
139 	{}
140 };
141 
Cast(VkDeviceMemory object)142 static inline DeviceMemory *Cast(VkDeviceMemory object)
143 {
144 	return DeviceMemory::Cast(object);
145 }
146 
147 }  // namespace vk
148 
149 #endif  // VK_DEVICE_MEMORY_HPP_
150