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