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 : public Object<DeviceMemory, VkDeviceMemory>
26 {
27 public:
28 DeviceMemory(const VkMemoryAllocateInfo *pCreateInfo, void *mem, Device *pDevice);
29
30 static size_t ComputeRequiredAllocationSize(const VkMemoryAllocateInfo *pCreateInfo);
31
32 #if SWIFTSHADER_EXTERNAL_MEMORY_OPAQUE_FD
33 VkResult exportFd(int *pFd) const;
34 #endif
35
36 #if SWIFTSHADER_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER
37 VkResult exportAndroidHardwareBuffer(struct AHardwareBuffer **pAhb) const;
38 static VkResult GetAndroidHardwareBufferProperties(VkDevice &device, const struct AHardwareBuffer *buffer, VkAndroidHardwareBufferPropertiesANDROID *pProperties);
39 #endif
40
41 #if VK_USE_PLATFORM_FUCHSIA
42 VkResult exportHandle(zx_handle_t *pHandle) const;
43 #endif
44
45 void destroy(const VkAllocationCallbacks *pAllocator);
46 VkResult allocate();
47 VkResult map(VkDeviceSize offset, VkDeviceSize size, void **ppData);
48 VkDeviceSize getCommittedMemoryInBytes() const;
49 void *getOffsetPointer(VkDeviceSize pOffset) const;
getMemoryTypeIndex() const50 uint32_t getMemoryTypeIndex() const { return memoryTypeIndex; }
51
52 // If this is external memory, return true iff its handle type matches the bitmask
53 // provided by |supportedExternalHandleTypes|. Otherwise, always return true.
54 bool checkExternalMemoryHandleType(
55 VkExternalMemoryHandleTypeFlags supportedExternalMemoryHandleType) const;
56
57 // Internal implementation class for external memory. Platform-specific.
58 class ExternalBase;
59
60 bool hasExternalImageProperties() const;
61 int externalImageRowPitchBytes(VkImageAspectFlagBits aspect) const;
62 VkDeviceSize externalImageMemoryOffset(VkImageAspectFlagBits aspect) const;
63
64 private:
65 void *buffer = nullptr;
66 VkDeviceSize size = 0;
67 uint32_t memoryTypeIndex = 0;
68 ExternalBase *external = nullptr;
69 Device *device;
70 };
71
Cast(VkDeviceMemory object)72 static inline DeviceMemory *Cast(VkDeviceMemory object)
73 {
74 return DeviceMemory::Cast(object);
75 }
76
77 } // namespace vk
78
79 #endif // VK_DEVICE_MEMORY_HPP_
80