1 //
2 // Copyright 2018 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 // DisplayVkAndroid.cpp:
7 // Implements the class methods for DisplayVkAndroid.
8 //
9
10 #include "libANGLE/renderer/vulkan/android/DisplayVkAndroid.h"
11
12 #include <android/log.h>
13 #include <android/native_window.h>
14 #include <vulkan/vulkan.h>
15
16 #include "common/angle_version_info.h"
17 #include "libANGLE/renderer/driver_utils.h"
18 #include "libANGLE/renderer/vulkan/RendererVk.h"
19 #include "libANGLE/renderer/vulkan/android/HardwareBufferImageSiblingVkAndroid.h"
20 #include "libANGLE/renderer/vulkan/android/WindowSurfaceVkAndroid.h"
21 #include "libANGLE/renderer/vulkan/vk_caps_utils.h"
22
23 namespace rx
24 {
25
DisplayVkAndroid(const egl::DisplayState & state)26 DisplayVkAndroid::DisplayVkAndroid(const egl::DisplayState &state) : DisplayVk(state) {}
27
initialize(egl::Display * display)28 egl::Error DisplayVkAndroid::initialize(egl::Display *display)
29 {
30 ANGLE_TRY(DisplayVk::initialize(display));
31
32 std::stringstream strstr;
33 strstr << "Version (" << angle::GetANGLEVersionString() << "), ";
34 strstr << "Renderer (" << mRenderer->getRendererDescription() << ")";
35 __android_log_print(ANDROID_LOG_INFO, "ANGLE", "%s", strstr.str().c_str());
36
37 return egl::NoError();
38 }
39
isValidNativeWindow(EGLNativeWindowType window) const40 bool DisplayVkAndroid::isValidNativeWindow(EGLNativeWindowType window) const
41 {
42 return (ANativeWindow_getFormat(window) >= 0);
43 }
44
createWindowSurfaceVk(const egl::SurfaceState & state,EGLNativeWindowType window)45 SurfaceImpl *DisplayVkAndroid::createWindowSurfaceVk(const egl::SurfaceState &state,
46 EGLNativeWindowType window)
47 {
48 return new WindowSurfaceVkAndroid(state, window);
49 }
50
generateConfigs()51 egl::ConfigSet DisplayVkAndroid::generateConfigs()
52 {
53 // ANGLE's Vulkan back-end on Android traditionally supports EGLConfig's with GL_RGBA8,
54 // GL_RGB8, and GL_RGB565. The Android Vulkan loader used to support all three of these
55 // (e.g. Android 7), but this has changed as Android now supports Vulkan devices that do not
56 // support all of those formats. The loader always supports GL_RGBA8. Other formats are
57 // optionally supported, depending on the underlying driver support. This includes GL_RGB10_A2
58 // and GL_RGBA16F, which ANGLE also desires to support EGLConfig's with.
59 //
60 // The problem for ANGLE is that Vulkan requires a VkSurfaceKHR in order to query available
61 // formats from the loader, but ANGLE must determine which EGLConfig's to expose before it has
62 // a VkSurfaceKHR. The VK_GOOGLE_surfaceless_query extension allows ANGLE to query formats
63 // without having a VkSurfaceKHR. The old path is still kept until this extension becomes
64 // universally available.
65
66 // Assume GL_RGB8 and GL_RGBA8 is always available.
67 std::vector<GLenum> kColorFormats = {GL_RGBA8, GL_RGB8};
68 std::vector<GLenum> kDesiredColorFormats = {GL_RGB565, GL_RGB10_A2, GL_RGBA16F};
69 if (!getRenderer()->getFeatures().supportsSurfacelessQueryExtension.enabled)
70 {
71 // Old path: Assume GL_RGB565 is available, as it is generally available on the devices
72 // that support Vulkan.
73 kColorFormats.push_back(GL_RGB565);
74 }
75 else
76 {
77 // Use the VK_GOOGLE_surfaceless_query extension to query the available formats and
78 // colorspaces by using a VK_NULL_HANDLE for the VkSurfaceKHR handle.
79 VkPhysicalDevice physicalDevice = mRenderer->getPhysicalDevice();
80 VkPhysicalDeviceSurfaceInfo2KHR surfaceInfo2 = {};
81 surfaceInfo2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR;
82 surfaceInfo2.surface = VK_NULL_HANDLE;
83 uint32_t surfaceFormatCount = 0;
84
85 VkResult result = vkGetPhysicalDeviceSurfaceFormats2KHR(physicalDevice, &surfaceInfo2,
86 &surfaceFormatCount, nullptr);
87 if (result != VK_SUCCESS)
88 {
89 return egl::ConfigSet();
90 }
91 std::vector<VkSurfaceFormat2KHR> surfaceFormats2(surfaceFormatCount);
92 for (VkSurfaceFormat2KHR &surfaceFormat2 : surfaceFormats2)
93 {
94 surfaceFormat2.sType = VK_STRUCTURE_TYPE_SURFACE_FORMAT_2_KHR;
95 }
96 result = vkGetPhysicalDeviceSurfaceFormats2KHR(physicalDevice, &surfaceInfo2,
97 &surfaceFormatCount, surfaceFormats2.data());
98 if (result != VK_SUCCESS)
99 {
100 return egl::ConfigSet();
101 }
102
103 for (const VkSurfaceFormat2KHR &surfaceFormat2 : surfaceFormats2)
104 {
105 // Need to convert each Vulkan VkFormat into its GLES equivalent and
106 // add formats in kDesiredColorFormats.
107 angle::FormatID angleFormatID =
108 vk::GetFormatIDFromVkFormat(surfaceFormat2.surfaceFormat.format);
109 const angle::Format &angleFormat = angle::Format::Get(angleFormatID);
110 GLenum glFormat = angleFormat.glInternalFormat;
111
112 if (std::find(kDesiredColorFormats.begin(), kDesiredColorFormats.end(), glFormat) !=
113 kDesiredColorFormats.end())
114 {
115 kColorFormats.push_back(glFormat);
116 }
117 }
118 }
119
120 std::vector<GLenum> depthStencilFormats(
121 egl_vk::kConfigDepthStencilFormats,
122 egl_vk::kConfigDepthStencilFormats + ArraySize(egl_vk::kConfigDepthStencilFormats));
123
124 if (getCaps().stencil8)
125 {
126 depthStencilFormats.push_back(GL_STENCIL_INDEX8);
127 }
128 return egl_vk::GenerateConfigs(kColorFormats.data(), kColorFormats.size(),
129 depthStencilFormats.data(), depthStencilFormats.size(), this);
130 }
131
enableRecordableIfSupported(egl::Config * config)132 void DisplayVkAndroid::enableRecordableIfSupported(egl::Config *config)
133 {
134 // TODO(b/181163023): Determine how to properly query for support. This is a hack to unblock
135 // launching SwANGLE on Cuttlefish.
136 // anglebug.com/6612: This is also required for app compatiblity.
137 config->recordable = true;
138 }
139
checkConfigSupport(egl::Config * config)140 void DisplayVkAndroid::checkConfigSupport(egl::Config *config)
141 {
142 // TODO(geofflang): Test for native support and modify the config accordingly.
143 // anglebug.com/2692
144
145 enableRecordableIfSupported(config);
146 }
147
validateImageClientBuffer(const gl::Context * context,EGLenum target,EGLClientBuffer clientBuffer,const egl::AttributeMap & attribs) const148 egl::Error DisplayVkAndroid::validateImageClientBuffer(const gl::Context *context,
149 EGLenum target,
150 EGLClientBuffer clientBuffer,
151 const egl::AttributeMap &attribs) const
152 {
153 switch (target)
154 {
155 case EGL_NATIVE_BUFFER_ANDROID:
156 return HardwareBufferImageSiblingVkAndroid::ValidateHardwareBuffer(
157 mRenderer, clientBuffer, attribs);
158
159 default:
160 return DisplayVk::validateImageClientBuffer(context, target, clientBuffer, attribs);
161 }
162 }
163
createExternalImageSibling(const gl::Context * context,EGLenum target,EGLClientBuffer buffer,const egl::AttributeMap & attribs)164 ExternalImageSiblingImpl *DisplayVkAndroid::createExternalImageSibling(
165 const gl::Context *context,
166 EGLenum target,
167 EGLClientBuffer buffer,
168 const egl::AttributeMap &attribs)
169 {
170 switch (target)
171 {
172 case EGL_NATIVE_BUFFER_ANDROID:
173 return new HardwareBufferImageSiblingVkAndroid(buffer);
174
175 default:
176 return DisplayVk::createExternalImageSibling(context, target, buffer, attribs);
177 }
178 }
179
getWSIExtension() const180 const char *DisplayVkAndroid::getWSIExtension() const
181 {
182 return VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
183 }
184
IsVulkanAndroidDisplayAvailable()185 bool IsVulkanAndroidDisplayAvailable()
186 {
187 return true;
188 }
189
CreateVulkanAndroidDisplay(const egl::DisplayState & state)190 DisplayImpl *CreateVulkanAndroidDisplay(const egl::DisplayState &state)
191 {
192 return new DisplayVkAndroid(state);
193 }
194 } // namespace rx
195