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.h"
19#include "VkObject.hpp"
20
21namespace vk
22{
23
24class DeviceMemory : public Object<DeviceMemory, VkDeviceMemory>
25{
26public:
27 DeviceMemory(const VkMemoryAllocateInfo* pCreateInfo, void* mem);
28
29 static size_t ComputeRequiredAllocationSize(const VkMemoryAllocateInfo* pCreateInfo);
30
31#if SWIFTSHADER_EXTERNAL_MEMORY_LINUX_MEMFD
32 VkResult exportFd(int* pFd) const;
33#endif
34
35 void destroy(const VkAllocationCallbacks* pAllocator);
36 VkResult allocate();
37 VkResult map(VkDeviceSize offset, VkDeviceSize size, void** ppData);
38 VkDeviceSize getCommittedMemoryInBytes() const;
39 void* getOffsetPointer(VkDeviceSize pOffset) const;
40 uint32_t getMemoryTypeIndex() const { return memoryTypeIndex; }
41
42 // If this is external memory, return true iff its handle type matches the bitmask
43 // provided by |supportedExternalHandleTypes|. Otherwise, always return true.
44 bool checkExternalMemoryHandleType(
45 VkExternalMemoryHandleTypeFlags supportedExternalMemoryHandleType) const;
46
47 // Internal implementation class for external memory. Platform-specific.
48 class ExternalBase;
49
50private:
51
52 void* buffer = nullptr;
53 VkDeviceSize size = 0;
54 uint32_t memoryTypeIndex = 0;
55 ExternalBase* external = nullptr;
56};
57
58static inline DeviceMemory* Cast(VkDeviceMemory object)
59{
60 return DeviceMemory::Cast(object);
61}
62
63
64} // namespace vk
65
66#endif // VK_DEVICE_MEMORY_HPP_
67