• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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_ANDROID_HPP_
16 #define VK_DEVICE_MEMORY_EXTERNAL_ANDROID_HPP_
17 
18 #include "VkBuffer.hpp"
19 #include "VkDevice.hpp"
20 #include "VkDeviceMemory.hpp"
21 #include "VkImage.hpp"
22 
23 #include <vndk/hardware_buffer.h>
24 
25 class AHardwareBufferExternalMemory : public vk::DeviceMemory, public vk::ObjectBase<AHardwareBufferExternalMemory, VkDeviceMemory>
26 {
27 public:
28 	// Helper struct which reads the parsed allocation info and
29 	// extracts relevant information related to the handle type
30 	// supported by this DeviceMemory subclass.
31 	struct AllocateInfo
32 	{
33 		bool importAhb = false;
34 		bool exportAhb = false;
35 		AHardwareBuffer *ahb = nullptr;
36 		vk::Image *dedicatedImageHandle = nullptr;
37 		vk::Buffer *dedicatedBufferHandle = nullptr;
38 
39 		AllocateInfo() = default;
40 
41 		// Use the parsed allocation info to initialize a AllocateInfo.
42 		AllocateInfo(const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo);
43 	};
44 
45 	static const VkExternalMemoryHandleTypeFlagBits typeFlagBit = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
46 
SupportsAllocateInfo(const vk::DeviceMemory::ExtendedAllocationInfo & extendedAllocationInfo)47 	static bool SupportsAllocateInfo(const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo)
48 	{
49 		AllocateInfo info(extendedAllocationInfo);
50 		return info.importAhb || info.exportAhb;
51 	}
52 
53 	explicit AHardwareBufferExternalMemory(const VkMemoryAllocateInfo *pCreateInfo, void *mem, const vk::DeviceMemory::ExtendedAllocationInfo &extendedAllocationInfo, vk::Device *pDevice);
54 	~AHardwareBufferExternalMemory();
55 
56 	VkResult allocateBuffer() override;
57 	void freeBuffer() override;
58 
getFlagBit() const59 	VkExternalMemoryHandleTypeFlagBits getFlagBit() const override { return typeFlagBit; }
60 
61 	virtual VkResult exportAndroidHardwareBuffer(AHardwareBuffer **pAhb) const override final;
62 
63 	static VkFormat GetVkFormatFromAHBFormat(uint32_t ahbFormat);
64 	static VkResult GetAndroidHardwareBufferFormatProperties(const AHardwareBuffer_Desc &ahbDesc, VkAndroidHardwareBufferFormatPropertiesANDROID *pFormat);
65 	static VkResult GetAndroidHardwareBufferProperties(VkDevice &device, const AHardwareBuffer *buffer, VkAndroidHardwareBufferPropertiesANDROID *pProperties);
66 
hasExternalImageProperties() const67 	bool hasExternalImageProperties() const override final { return true; }
68 	int externalImageRowPitchBytes(VkImageAspectFlagBits aspect) const override final;
69 	VkDeviceSize externalImageMemoryOffset(VkImageAspectFlagBits aspect) const override final;
70 
71 #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
isImport() const72 	bool isImport() const override
73 	{
74 		return allocateInfo.importAhb;
75 	}
76 	uint64_t getMemoryObjectId() const override;
77 #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
78 
79 private:
80 	VkResult importAndroidHardwareBuffer(AHardwareBuffer *buffer, void **pBuffer);
81 	VkResult allocateAndroidHardwareBuffer(size_t size, void **pBuffer);
82 	VkResult lockAndroidHardwareBuffer(void **pBuffer);
83 	VkResult unlockAndroidHardwareBuffer();
84 
85 	AHardwareBuffer *ahb = nullptr;
86 	AHardwareBuffer_Desc ahbDesc = {};
87 	AHardwareBuffer_Planes ahbPlanes = {};
88 	vk::Device *device = nullptr;
89 	AllocateInfo allocateInfo;
90 };
91 
92 #endif  // VK_DEVICE_MEMORY_EXTERNAL_ANDROID_HPP_
93