• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "host/libs/graphics_detector/egl.h"
18 
19 #include <GLES/gl.h>
20 #include <android-base/logging.h>
21 #include <android-base/strings.h>
22 
23 namespace cuttlefish {
24 namespace {
25 
26 constexpr const char kEglLib[] = "libEGL.so";
27 constexpr const char kEglLibAlt[] = "libEGL.so.1";
28 
LoadEglLib()29 std::optional<Lib> LoadEglLib() {
30   for (const auto* possible_name : {kEglLib, kEglLibAlt}) {
31     auto lib_opt = Lib::Load(possible_name);
32     if (!lib_opt) {
33       LOG(VERBOSE) << "Failed to load " << possible_name;
34     } else {
35       LOG(VERBOSE) << "Loaded " << possible_name;
36       return std::move(lib_opt);
37     }
38   }
39   return std::nullopt;
40 }
41 
42 }  // namespace
43 
44 /*static*/
Load()45 std::optional<Egl> Egl::Load() {
46   auto lib_opt = LoadEglLib();
47   if (!lib_opt) {
48     return std::nullopt;
49   }
50 
51   Egl egl;
52   egl.lib_ = std::move(*lib_opt);
53 
54 #define LOAD_EGL_FUNCTION_POINTER(return_type, function_name, signature)       \
55   egl.function_name = reinterpret_cast<return_type(GL_APIENTRY*) signature>(   \
56       egl.lib_.GetSymbol(#function_name));                                     \
57   if (egl.function_name == nullptr) {                                          \
58     egl.function_name = reinterpret_cast<return_type(GL_APIENTRY*) signature>( \
59         egl.eglGetProcAddress(#function_name));                                \
60   }                                                                            \
61   if (egl.function_name == nullptr) {                                          \
62     LOG(VERBOSE) << "Failed to load EGL function: " << #function_name;         \
63   } else {                                                                     \
64     LOG(VERBOSE) << "Loaded EGL function: " << #function_name;                 \
65   }
66 
67   FOR_EACH_EGL_FUNCTION(LOAD_EGL_FUNCTION_POINTER);
68 
69   egl.Init();
70 
71   return std::move(egl);
72 }
73 
Init()74 void Egl::Init() {
75   EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
76   if (display == EGL_NO_DISPLAY) {
77     LOG(FATAL) << "Failed to get default display";
78   }
79 
80   EGLint client_version_major = 0;
81   EGLint client_version_minor = 0;
82   if (eglInitialize(display, &client_version_major, &client_version_minor) !=
83       EGL_TRUE) {
84     LOG(FATAL) << "Failed to initialize display.";
85     return;
86   }
87   LOG(VERBOSE) << "Found EGL client version " << client_version_major << "."
88                << client_version_minor;
89 
90   const std::string vendor_string = eglQueryString(display, EGL_VENDOR);
91   if (vendor_string.empty()) {
92     LOG(FATAL) << "Failed to query vendor.";
93     return;
94   }
95   LOG(VERBOSE) << "Found EGL vendor: " << vendor_string;
96 
97   const std::string extensions_string = eglQueryString(display, EGL_EXTENSIONS);
98   if (extensions_string.empty()) {
99     LOG(FATAL) << "Failed to query extensions.";
100     return;
101   }
102   LOG(VERBOSE) << "Found EGL extensions: " << extensions_string;
103 
104   if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) {
105     LOG(FATAL) << "Failed to bind GLES API.";
106     return;
107   }
108 
109   const EGLint attribs[] = {
110       // clang-format off
111     EGL_SURFACE_TYPE,     EGL_PBUFFER_BIT,
112     EGL_RENDERABLE_TYPE,  EGL_OPENGL_ES3_BIT,
113     EGL_RED_SIZE,         8,
114     EGL_GREEN_SIZE,       8,
115     EGL_BLUE_SIZE,        8,
116     EGL_ALPHA_SIZE,       8,
117     EGL_NONE,
118       // clang-format on
119   };
120 
121   EGLConfig config;
122   EGLint num_configs = 0;
123   if (eglChooseConfig(display, attribs, &config, 1, &num_configs) != EGL_TRUE) {
124     LOG(FATAL) << "Failed to find matching framebuffer config.";
125     return;
126   }
127   LOG(VERBOSE) << "Found matching framebuffer config.";
128 
129   const EGLint pbuffer_attribs[] = {
130       // clang-format off
131     EGL_WIDTH,  720,
132     EGL_HEIGHT, 720,
133     EGL_NONE,
134       // clang-format on
135   };
136 
137   EGLSurface primary_surface =
138       eglCreatePbufferSurface(display, config, pbuffer_attribs);
139   if (primary_surface == EGL_NO_SURFACE) {
140     LOG(FATAL) << "Failed to create EGL surface.";
141     return;
142   }
143 
144   const EGLint context_attribs[] = {
145       // clang-format off
146     EGL_CONTEXT_CLIENT_VERSION, 3,
147     EGL_NONE
148       // clang-format on
149   };
150 
151   EGLContext primary_context =
152       eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribs);
153   if (primary_context == EGL_NO_CONTEXT) {
154     LOG(FATAL) << "Failed to create EGL context.";
155     return;
156   }
157 
158   if (eglMakeCurrent(display, primary_surface, primary_surface,
159                      primary_context) == EGL_FALSE) {
160     LOG(FATAL) << "Failed to make primary EGL context/surface current.";
161     return;
162   }
163 }
164 
165 }  // namespace cuttlefish