• 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 "VkDeviceMemoryExternalBase.hpp"
22 #include "VkImage.hpp"
23 
24 #include <vndk/hardware_buffer.h>
25 
26 class AHardwareBufferExternalMemory : public vk::DeviceMemory::ExternalBase
27 {
28 public:
29 	// Helper struct to parse the VkMemoryAllocateInfo.pNext chain and
30 	// extract relevant information related to the handle type supported
31 	// by this DeviceMemory::ExternalBase subclass.
32 	struct AllocateInfo
33 	{
34 		bool importAhb = false;
35 		bool exportAhb = false;
36 		AHardwareBuffer *ahb = nullptr;
37 		vk::Image *dedicatedImageHandle = nullptr;
38 		vk::Buffer *dedicatedBufferHandle = nullptr;
39 
40 		AllocateInfo() = default;
41 
42 		// Parse the VkMemoryAllocateInfo.pNext chain to initialize an AllocateInfo.
43 		AllocateInfo(const VkMemoryAllocateInfo *pAllocateInfo);
44 	};
45 
46 	static const VkExternalMemoryHandleTypeFlagBits typeFlagBit = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
47 
SupportsAllocateInfo(const VkMemoryAllocateInfo * pAllocateInfo)48 	static bool SupportsAllocateInfo(const VkMemoryAllocateInfo *pAllocateInfo)
49 	{
50 		AllocateInfo info(pAllocateInfo);
51 		return info.importAhb || info.exportAhb;
52 	}
53 
54 	explicit AHardwareBufferExternalMemory(const VkMemoryAllocateInfo *pAllocateInfo);
55 	~AHardwareBufferExternalMemory();
56 
57 	VkResult allocate(size_t size, void **pBuffer) override;
58 	void deallocate(void *buffer, size_t size) override;
59 
getFlagBit() const60 	VkExternalMemoryHandleTypeFlagBits getFlagBit() const override { return typeFlagBit; }
61 
62 	VkResult exportAndroidHardwareBuffer(AHardwareBuffer **pAhb) const;
63 
setDevicePtr(vk::Device * pDevice)64 	void setDevicePtr(vk::Device *pDevice) override { device = pDevice; }
65 
66 	static VkFormat GetVkFormatFromAHBFormat(uint32_t ahbFormat);
67 	static VkResult GetAndroidHardwareBufferFormatProperties(const AHardwareBuffer_Desc &ahbDesc, VkAndroidHardwareBufferFormatPropertiesANDROID *pFormat);
68 	static VkResult GetAndroidHardwareBufferProperties(VkDevice &device, const AHardwareBuffer *buffer, VkAndroidHardwareBufferPropertiesANDROID *pProperties);
69 
hasExternalImageProperties() const70 	bool hasExternalImageProperties() const override final { return true; }
71 	int externalImageRowPitchBytes(VkImageAspectFlagBits aspect) const override final;
72 	VkDeviceSize externalImageMemoryOffset(VkImageAspectFlagBits aspect) const override final;
73 
74 #ifdef SWIFTSHADER_DEVICE_MEMORY_REPORT
isImport() const75 	bool isImport() const override
76 	{
77 		return allocateInfo.importAhb;
78 	}
79 	uint64_t getMemoryObjectId() const override;
80 #endif  // SWIFTSHADER_DEVICE_MEMORY_REPORT
81 
82 private:
83 	VkResult importAndroidHardwareBuffer(AHardwareBuffer *buffer, void **pBuffer);
84 	VkResult allocateAndroidHardwareBuffer(size_t size, void **pBuffer);
85 	VkResult lockAndroidHardwareBuffer(void **pBuffer);
86 	VkResult unlockAndroidHardwareBuffer();
87 
88 	AHardwareBuffer *ahb = nullptr;
89 	AHardwareBuffer_Desc ahbDesc = {};
90 	AHardwareBuffer_Planes ahbPlanes = {};
91 	vk::Device *device = nullptr;
92 	AllocateInfo allocateInfo;
93 };
94 
95 #endif  // VK_DEVICE_MEMORY_EXTERNAL_ANDROID_HPP_
96