• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 		const VkExportMemoryAllocateInfo *exportMemoryAllocateInfo = nullptr;
33 		const VkImportMemoryHostPointerInfoEXT *importMemoryHostPointerInfo = nullptr;
34 #if SWIFTSHADER_EXTERNAL_MEMORY_OPAQUE_FD
35 		const VkImportMemoryFdInfoKHR *importMemoryFdInfo = nullptr;
36 #endif
37 #if SWIFTSHADER_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER
38 		const VkImportAndroidHardwareBufferInfoANDROID *importAndroidHardwareBufferInfo = nullptr;
39 		const VkMemoryDedicatedAllocateInfo *dedicatedAllocateInfo = nullptr;
40 #endif
41 #if VK_USE_PLATFORM_FUCHSIA
42 		const VkImportMemoryZirconHandleInfoFUCHSIA *importMemoryZirconHandleInfo = nullptr;
43 #endif
44 	};
45 
46 protected:
47 	DeviceMemory(const VkMemoryAllocateInfo *pCreateInfo, Device *pDevice);
48 
49 public:
~DeviceMemory()50 	virtual ~DeviceMemory() {}
51 
52 	static VkResult Allocate(const VkAllocationCallbacks *pAllocator, const VkMemoryAllocateInfo *pAllocateInfo, VkDeviceMemory *pMemory, Device *device);
53 
operator VkDeviceMemory()54 	operator VkDeviceMemory()
55 	{
56 		return vk::TtoVkT<DeviceMemory, VkDeviceMemory>(this);
57 	}
58 
Cast(VkDeviceMemory object)59 	static inline DeviceMemory *Cast(VkDeviceMemory object)
60 	{
61 		return vk::VkTtoT<DeviceMemory, VkDeviceMemory>(object);
62 	}
63 
64 	static size_t ComputeRequiredAllocationSize(const VkMemoryAllocateInfo *pCreateInfo);
65 
66 #if SWIFTSHADER_EXTERNAL_MEMORY_OPAQUE_FD
67 	virtual VkResult exportFd(int *pFd) const;
68 #endif
69 
70 #if SWIFTSHADER_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER
71 	virtual VkResult exportAndroidHardwareBuffer(struct AHardwareBuffer **pAhb) const;
72 	static VkResult GetAndroidHardwareBufferProperties(VkDevice &device, const struct AHardwareBuffer *buffer, VkAndroidHardwareBufferPropertiesANDROID *pProperties);
73 #endif
74 
75 #if VK_USE_PLATFORM_FUCHSIA
76 	virtual VkResult exportHandle(zx_handle_t *pHandle) const;
77 #endif
78 
79 	void destroy(const VkAllocationCallbacks *pAllocator);
80 	VkResult allocate();
81 	VkResult map(VkDeviceSize offset, VkDeviceSize size, void **ppData);
82 	VkDeviceSize getCommittedMemoryInBytes() const;
83 	void *getOffsetPointer(VkDeviceSize pOffset) const;
getMemoryTypeIndex() const84 	uint32_t getMemoryTypeIndex() const { return memoryTypeIndex; }
85 
86 	// If this is external memory, return true iff its handle type matches the bitmask
87 	// provided by |supportedExternalHandleTypes|. Otherwise, always return true.
88 	bool checkExternalMemoryHandleType(
89 	    VkExternalMemoryHandleTypeFlags supportedExternalMemoryHandleType) const;
90 
91 	// Some external device memories, such as Android hardware buffers, represent
92 	// specific images with requirements.
hasExternalImageProperties() const93 	virtual bool hasExternalImageProperties() const { return false; }
externalImageRowPitchBytes(VkImageAspectFlagBits aspect) const94 	virtual int externalImageRowPitchBytes(VkImageAspectFlagBits aspect) const { return 0; }
externalImageMemoryOffset(VkImageAspectFlagBits aspect) const95 	virtual VkDeviceSize externalImageMemoryOffset(VkImageAspectFlagBits aspect) const { return 0; }
96 
97 protected:
98 	// Allocate the memory according to `allocationSize`. On success return VK_SUCCESS and sets `buffer`.
99 	virtual VkResult allocateBuffer();
100 
101 	// Free previously allocated memory at `buffer`.
102 	virtual void freeBuffer();
103 
104 	// Return the handle type flag bit supported by this implementation.
105 	// A value of 0 corresponds to non-external memory.
106 	virtual VkExternalMemoryHandleTypeFlagBits getFlagBit() const;
107 
108 #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
isImport() const109 	virtual bool isImport() const
110 	{
111 		return false;
112 	}
113 
getMemoryObjectId() const114 	virtual uint64_t getMemoryObjectId() const
115 	{
116 		return (uint64_t)buffer;
117 	}
118 #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
119 
120 	void *buffer = nullptr;
121 	const VkDeviceSize allocationSize;
122 	const uint32_t memoryTypeIndex;
123 	Device *const device;
124 
125 private:
126 	static VkResult ParseAllocationInfo(const VkMemoryAllocateInfo *pAllocateInfo, DeviceMemory::ExtendedAllocationInfo *extendedAllocationInfo);
127 	static VkResult Allocate(const VkAllocationCallbacks *pAllocator, const VkMemoryAllocateInfo *pAllocateInfo, VkDeviceMemory *pMemory,
128 	                         const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, Device *device);
129 };
130 
131 // This class represents a DeviceMemory object with no external memory
132 class DeviceMemoryInternal : public DeviceMemory, public ObjectBase<DeviceMemoryInternal, VkDeviceMemory>
133 {
134 public:
DeviceMemoryInternal(const VkMemoryAllocateInfo * pCreateInfo,void * mem,const DeviceMemory::ExtendedAllocationInfo & extendedAllocationInfo,Device * pDevice)135 	DeviceMemoryInternal(const VkMemoryAllocateInfo *pCreateInfo, void *mem, const DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, Device *pDevice)
136 	    : DeviceMemory(pCreateInfo, pDevice)
137 	{}
138 };
139 
Cast(VkDeviceMemory object)140 static inline DeviceMemory *Cast(VkDeviceMemory object)
141 {
142 	return DeviceMemory::Cast(object);
143 }
144 
145 }  // namespace vk
146 
147 #endif  // VK_DEVICE_MEMORY_HPP_
148