1 //
2 // Copyright 2020 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // vk_android_utils.cpp: Vulkan utilities for using the Android platform
8
9 #include "libANGLE/renderer/vulkan/android/vk_android_utils.h"
10
11 #include "common/android_util.h"
12 #include "libANGLE/renderer/vulkan/ContextVk.h"
13 #include "libANGLE/renderer/vulkan/vk_utils.h"
14
15 #if defined(ANGLE_PLATFORM_ANDROID)
16 # include "libANGLE/Display.h"
17 # include "libANGLE/renderer/vulkan/android/AHBFunctions.h"
18 # include "libANGLE/renderer/vulkan/android/DisplayVkAndroid.h"
19 #endif
20
21 namespace rx
22 {
23 namespace vk
24 {
GetClientBufferMemoryRequirements(ContextVk * contextVk,const AHardwareBuffer * hardwareBuffer,VkMemoryRequirements & memRequirements)25 angle::Result GetClientBufferMemoryRequirements(ContextVk *contextVk,
26 const AHardwareBuffer *hardwareBuffer,
27 VkMemoryRequirements &memRequirements)
28 {
29 #if defined(ANGLE_PLATFORM_ANDROID)
30 ASSERT(GetImplAs<DisplayVkAndroid>(contextVk->getRenderer()->getDisplay())
31 ->getAHBFunctions()
32 .valid());
33
34 // Get Android Buffer Properties
35 VkAndroidHardwareBufferFormatPropertiesANDROID bufferFormatProperties = {};
36 bufferFormatProperties.sType =
37 VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID;
38
39 VkAndroidHardwareBufferPropertiesANDROID bufferProperties = {};
40 bufferProperties.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID;
41 bufferProperties.pNext = &bufferFormatProperties;
42
43 VkDevice device = contextVk->getRenderer()->getDevice();
44 ANGLE_VK_TRY(contextVk, vkGetAndroidHardwareBufferPropertiesANDROID(device, hardwareBuffer,
45 &bufferProperties));
46
47 memRequirements.size = bufferProperties.allocationSize;
48 memRequirements.alignment = 0;
49 memRequirements.memoryTypeBits = bufferProperties.memoryTypeBits;
50
51 return angle::Result::Continue;
52 #else
53 ANGLE_VK_UNREACHABLE(contextVk);
54 return angle::Result::Stop;
55
56 #endif
57 }
58
InitAndroidExternalMemory(ContextVk * contextVk,EGLClientBuffer clientBuffer,VkMemoryPropertyFlags memoryProperties,Buffer * buffer,VkMemoryPropertyFlags * memoryPropertyFlagsOut,DeviceMemory * deviceMemoryOut)59 angle::Result InitAndroidExternalMemory(ContextVk *contextVk,
60 EGLClientBuffer clientBuffer,
61 VkMemoryPropertyFlags memoryProperties,
62 Buffer *buffer,
63 VkMemoryPropertyFlags *memoryPropertyFlagsOut,
64 DeviceMemory *deviceMemoryOut)
65 {
66 #if defined(ANGLE_PLATFORM_ANDROID)
67 const AHBFunctions &functions =
68 GetImplAs<DisplayVkAndroid>(contextVk->getRenderer()->getDisplay())->getAHBFunctions();
69 ASSERT(functions.valid());
70
71 struct AHardwareBuffer *hardwareBuffer =
72 angle::android::ClientBufferToAHardwareBuffer(clientBuffer);
73
74 VkMemoryRequirements externalMemoryRequirements = {};
75 ANGLE_TRY(
76 GetClientBufferMemoryRequirements(contextVk, hardwareBuffer, externalMemoryRequirements));
77
78 // Import Vulkan DeviceMemory from Android Hardware Buffer.
79 VkImportAndroidHardwareBufferInfoANDROID importHardwareBufferInfo = {};
80 importHardwareBufferInfo.sType = VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID;
81 importHardwareBufferInfo.buffer = hardwareBuffer;
82
83 ANGLE_TRY(AllocateBufferMemoryWithRequirements(
84 contextVk, memoryProperties, externalMemoryRequirements, &importHardwareBufferInfo, buffer,
85 memoryPropertyFlagsOut, deviceMemoryOut));
86
87 functions.acquire(hardwareBuffer);
88
89 return angle::Result::Continue;
90 #else
91 ANGLE_VK_UNREACHABLE(contextVk);
92 return angle::Result::Stop;
93 #endif
94 }
95
ReleaseAndroidExternalMemory(RendererVk * rendererVk,EGLClientBuffer clientBuffer)96 void ReleaseAndroidExternalMemory(RendererVk *rendererVk, EGLClientBuffer clientBuffer)
97 {
98 #if defined(ANGLE_PLATFORM_ANDROID)
99 const AHBFunctions &functions =
100 GetImplAs<DisplayVkAndroid>(rendererVk->getDisplay())->getAHBFunctions();
101 ASSERT(functions.valid());
102 struct AHardwareBuffer *hardwareBuffer =
103 angle::android::ClientBufferToAHardwareBuffer(clientBuffer);
104 functions.release(hardwareBuffer);
105 #endif
106 }
107 } // namespace vk
108 } // namespace rx
109