1 // Copyright 2020 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_EXTERNAL_BASE_HPP_
16 #define VK_DEVICE_MEMORY_EXTERNAL_BASE_HPP_
17 
18 #include "VkDeviceMemory.hpp"
19 
20 namespace vk {
21 
22 // Base abstract interface for a device memory implementation.
23 class DeviceMemory::ExternalBase
24 {
25 public:
26 	virtual ~ExternalBase() = default;
27 
28 	// Allocate the memory according to |size|. On success return VK_SUCCESS
29 	// and sets |*pBuffer|.
30 	virtual VkResult allocate(size_t size, void **pBuffer) = 0;
31 
32 	// Deallocate previously allocated memory at |buffer|.
33 	virtual void deallocate(void *buffer, size_t size) = 0;
34 
35 	// Return the handle type flag bit supported by this implementation.
36 	// A value of 0 corresponds to non-external memory.
37 	virtual VkExternalMemoryHandleTypeFlagBits getFlagBit() const = 0;
38 
setDevicePtr(Device * pDevice)39 	virtual void setDevicePtr(Device *pDevice) {}
40 
41 #if SWIFTSHADER_EXTERNAL_MEMORY_OPAQUE_FD
exportFd(int * pFd) const42 	virtual VkResult exportFd(int *pFd) const
43 	{
44 		return VK_ERROR_INVALID_EXTERNAL_HANDLE;
45 	}
46 #endif
47 
48 	// Some external device memories, such as Android hardware buffers, represent
49 	// specific images with requirements.
hasExternalImageProperties() const50 	virtual bool hasExternalImageProperties() const { return false; }
externalImageRowPitchBytes(VkImageAspectFlagBits aspect) const51 	virtual int externalImageRowPitchBytes(VkImageAspectFlagBits aspect) const { return 0; }
externalImageMemoryOffset(VkImageAspectFlagBits aspect) const52 	virtual VkDeviceSize externalImageMemoryOffset(VkImageAspectFlagBits aspect) const { return 0; }
53 
54 #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
isImport() const55 	virtual bool isImport() const
56 	{
57 		return false;
58 	}
59 
getMemoryObjectId() const60 	virtual uint64_t getMemoryObjectId() const
61 	{
62 		return 0;
63 	}
64 #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
65 
66 protected:
67 	ExternalBase() = default;
68 };
69 
70 }  // namespace vk
71 
72 #endif  // VK_DEVICE_MEMORY_EXTERNAL_BASE_HPP_
73