• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         // 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     config->recordable = true;
107 }
108 
checkConfigSupport(egl::Config * config)109 void DisplayVkAndroid::checkConfigSupport(egl::Config *config)
110 {
111     // TODO(geofflang): Test for native support and modify the config accordingly.
112     // anglebug.com/2692
113 
114     enableRecordableIfSupported(config);
115 }
116 
validateImageClientBuffer(const gl::Context * context,EGLenum target,EGLClientBuffer clientBuffer,const egl::AttributeMap & attribs) const117 egl::Error DisplayVkAndroid::validateImageClientBuffer(const gl::Context *context,
118                                                        EGLenum target,
119                                                        EGLClientBuffer clientBuffer,
120                                                        const egl::AttributeMap &attribs) const
121 {
122     switch (target)
123     {
124         case EGL_NATIVE_BUFFER_ANDROID:
125             return HardwareBufferImageSiblingVkAndroid::ValidateHardwareBuffer(
126                 mRenderer, clientBuffer, attribs);
127 
128         default:
129             return DisplayVk::validateImageClientBuffer(context, target, clientBuffer, attribs);
130     }
131 }
132 
createExternalImageSibling(const gl::Context * context,EGLenum target,EGLClientBuffer buffer,const egl::AttributeMap & attribs)133 ExternalImageSiblingImpl *DisplayVkAndroid::createExternalImageSibling(
134     const gl::Context *context,
135     EGLenum target,
136     EGLClientBuffer buffer,
137     const egl::AttributeMap &attribs)
138 {
139     switch (target)
140     {
141         case EGL_NATIVE_BUFFER_ANDROID:
142             return new HardwareBufferImageSiblingVkAndroid(buffer);
143 
144         default:
145             return DisplayVk::createExternalImageSibling(context, target, buffer, attribs);
146     }
147 }
148 
getWSIExtension() const149 const char *DisplayVkAndroid::getWSIExtension() const
150 {
151     return VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
152 }
153 
IsVulkanAndroidDisplayAvailable()154 bool IsVulkanAndroidDisplayAvailable()
155 {
156     return true;
157 }
158 
CreateVulkanAndroidDisplay(const egl::DisplayState & state)159 DisplayImpl *CreateVulkanAndroidDisplay(const egl::DisplayState &state)
160 {
161     return new DisplayVkAndroid(state);
162 }
163 }  // namespace rx
164