• 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, store per-plane properties.
hasExternalImagePlanes() const92 	virtual bool hasExternalImagePlanes() const { return false; }
externalImageRowPitchBytes(VkImageAspectFlagBits aspect) const93 	virtual int externalImageRowPitchBytes(VkImageAspectFlagBits aspect) const { return 0; }
externalImageMemoryOffset(VkImageAspectFlagBits aspect) const94 	virtual VkDeviceSize externalImageMemoryOffset(VkImageAspectFlagBits aspect) const { return 0; }
95 
96 protected:
97 	// Allocate the memory according to `allocationSize`. On success return VK_SUCCESS and sets `buffer`.
98 	virtual VkResult allocateBuffer();
99 
100 	// Free previously allocated memory at `buffer`.
101 	virtual void freeBuffer();
102 
103 	// Return the handle type flag bit supported by this implementation.
104 	// A value of 0 corresponds to non-external memory.
105 	virtual VkExternalMemoryHandleTypeFlagBits getFlagBit() const;
106 
107 #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
isImport() const108 	virtual bool isImport() const
109 	{
110 		return false;
111 	}
112 
getMemoryObjectId() const113 	virtual uint64_t getMemoryObjectId() const
114 	{
115 		return (uint64_t)buffer;
116 	}
117 #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
118 
119 	void *buffer = nullptr;
120 	const VkDeviceSize allocationSize;
121 	const uint32_t memoryTypeIndex;
122 	Device *const device;
123 
124 private:
125 	static VkResult ParseAllocationInfo(const VkMemoryAllocateInfo *pAllocateInfo, DeviceMemory::ExtendedAllocationInfo *extendedAllocationInfo);
126 	static VkResult Allocate(const VkAllocationCallbacks *pAllocator, const VkMemoryAllocateInfo *pAllocateInfo, VkDeviceMemory *pMemory,
127 	                         const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, Device *device);
128 };
129 
130 // This class represents a DeviceMemory object with no external memory
131 class DeviceMemoryInternal : public DeviceMemory, public ObjectBase<DeviceMemoryInternal, VkDeviceMemory>
132 {
133 public:
DeviceMemoryInternal(const VkMemoryAllocateInfo * pCreateInfo,void * mem,const DeviceMemory::ExtendedAllocationInfo & extendedAllocationInfo,Device * pDevice)134 	DeviceMemoryInternal(const VkMemoryAllocateInfo *pCreateInfo, void *mem, const DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, Device *pDevice)
135 	    : DeviceMemory(pCreateInfo, pDevice)
136 	{}
137 };
138 
Cast(VkDeviceMemory object)139 static inline DeviceMemory *Cast(VkDeviceMemory object)
140 {
141 	return DeviceMemory::Cast(object);
142 }
143 
144 }  // namespace vk
145 
146 #endif  // VK_DEVICE_MEMORY_HPP_
147