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