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/android/HardwareBufferImageSiblingVkAndroid.h"
19 #include "libANGLE/renderer/vulkan/android/WindowSurfaceVkAndroid.h"
20 #include "libANGLE/renderer/vulkan/vk_caps_utils.h"
21 #include "libANGLE/renderer/vulkan/vk_renderer.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 // DisplayVk should have already queried and cached supported surface formats.
78 for (GLenum glFormat : kDesiredColorFormats)
79 {
80 VkFormat vkFormat = mRenderer->getFormat(glFormat).getActualRenderableImageVkFormat();
81 ASSERT(vkFormat != VK_FORMAT_UNDEFINED);
82 if (isConfigFormatSupported(vkFormat))
83 {
84 kColorFormats.push_back(glFormat);
85 }
86 }
87 }
88
89 std::vector<GLenum> depthStencilFormats(
90 egl_vk::kConfigDepthStencilFormats,
91 egl_vk::kConfigDepthStencilFormats + ArraySize(egl_vk::kConfigDepthStencilFormats));
92
93 if (getCaps().stencil8)
94 {
95 depthStencilFormats.push_back(GL_STENCIL_INDEX8);
96 }
97 return egl_vk::GenerateConfigs(kColorFormats.data(), kColorFormats.size(),
98 depthStencilFormats.data(), depthStencilFormats.size(), this);
99 }
100
enableRecordableIfSupported(egl::Config * config)101 void DisplayVkAndroid::enableRecordableIfSupported(egl::Config *config)
102 {
103 // TODO(b/181163023): Determine how to properly query for support. This is a hack to unblock
104 // launching SwANGLE on Cuttlefish.
105 // anglebug.com/6612: This is also required for app compatiblity.
106
107 const bool isRGBA8888Config = (config->redSize == 8 && config->greenSize == 8 &&
108 config->blueSize == 8 && config->alphaSize == 8);
109 const bool isRGB888Config = (config->redSize == 8 && config->greenSize == 8 &&
110 config->blueSize == 8 && config->alphaSize == 0);
111 const bool isRGB10A2Config = (config->redSize == 10 && config->greenSize == 10 &&
112 config->blueSize == 10 && config->alphaSize == 2);
113
114 // enabled recordable only for RGBA8888, RGB888 and RGB10_A2 configs
115 const EGLBoolean enableRecordableBit =
116 (isRGBA8888Config || isRGB888Config || isRGB10A2Config) ? EGL_TRUE : EGL_FALSE;
117
118 config->recordable = enableRecordableBit;
119 }
120
checkConfigSupport(egl::Config * config)121 void DisplayVkAndroid::checkConfigSupport(egl::Config *config)
122 {
123 // TODO(geofflang): Test for native support and modify the config accordingly.
124 // anglebug.com/2692
125
126 enableRecordableIfSupported(config);
127 }
128
validateImageClientBuffer(const gl::Context * context,EGLenum target,EGLClientBuffer clientBuffer,const egl::AttributeMap & attribs) const129 egl::Error DisplayVkAndroid::validateImageClientBuffer(const gl::Context *context,
130 EGLenum target,
131 EGLClientBuffer clientBuffer,
132 const egl::AttributeMap &attribs) const
133 {
134 switch (target)
135 {
136 case EGL_NATIVE_BUFFER_ANDROID:
137 return HardwareBufferImageSiblingVkAndroid::ValidateHardwareBuffer(
138 mRenderer, clientBuffer, attribs);
139
140 default:
141 return DisplayVk::validateImageClientBuffer(context, target, clientBuffer, attribs);
142 }
143 }
144
createExternalImageSibling(const gl::Context * context,EGLenum target,EGLClientBuffer buffer,const egl::AttributeMap & attribs)145 ExternalImageSiblingImpl *DisplayVkAndroid::createExternalImageSibling(
146 const gl::Context *context,
147 EGLenum target,
148 EGLClientBuffer buffer,
149 const egl::AttributeMap &attribs)
150 {
151 switch (target)
152 {
153 case EGL_NATIVE_BUFFER_ANDROID:
154 return new HardwareBufferImageSiblingVkAndroid(buffer);
155
156 default:
157 return DisplayVk::createExternalImageSibling(context, target, buffer, attribs);
158 }
159 }
160
getWSIExtension() const161 const char *DisplayVkAndroid::getWSIExtension() const
162 {
163 return VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
164 }
165
IsVulkanAndroidDisplayAvailable()166 bool IsVulkanAndroidDisplayAvailable()
167 {
168 return true;
169 }
170
CreateVulkanAndroidDisplay(const egl::DisplayState & state)171 DisplayImpl *CreateVulkanAndroidDisplay(const egl::DisplayState &state)
172 {
173 return new DisplayVkAndroid(state);
174 }
175 } // namespace rx
176