• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include "arm_compute/runtime/GLES_COMPUTE/GCHelpers.h"
26 
27 #include "arm_compute/core/Error.h"
28 
29 namespace arm_compute
30 {
create_opengl_display_and_context()31 std::tuple<EGLDisplay, EGLContext, EGLBoolean> create_opengl_display_and_context()
32 {
33     EGLBoolean res;
34     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
35 
36     ARM_COMPUTE_ERROR_ON_MSG_VAR(display == EGL_NO_DISPLAY, "Failed to get display: 0x%x.", eglGetError());
37 
38     res = eglInitialize(display, nullptr, nullptr);
39 
40     ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to initialize egl: 0x%x.", eglGetError());
41     ARM_COMPUTE_UNUSED(res);
42 
43     const char *egl_extension_st = eglQueryString(display, EGL_EXTENSIONS);
44     ARM_COMPUTE_ERROR_ON_MSG((strstr(egl_extension_st, "EGL_KHR_create_context") == nullptr), "Failed to query EGL_KHR_create_context");
45     ARM_COMPUTE_ERROR_ON_MSG((strstr(egl_extension_st, "EGL_KHR_surfaceless_context") == nullptr), "Failed to query EGL_KHR_surfaceless_context");
46     ARM_COMPUTE_UNUSED(egl_extension_st);
47 
48     const std::array<EGLint, 3> config_attribs =
49     {
50         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR,
51         EGL_NONE
52     };
53     EGLConfig cfg;
54     EGLint    count;
55 
56     res = eglChooseConfig(display, config_attribs.data(), &cfg, 1, &count);
57 
58     ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to choose config: 0x%x.", eglGetError());
59     ARM_COMPUTE_UNUSED(res);
60 
61     res = eglBindAPI(EGL_OPENGL_ES_API);
62 
63     ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to bind api: 0x%x.", eglGetError());
64 
65     const std::array<EGLint, 3> attribs =
66     {
67         EGL_CONTEXT_CLIENT_VERSION, 3,
68         EGL_NONE
69     };
70     EGLContext context = eglCreateContext(display,
71                                           cfg,
72                                           EGL_NO_CONTEXT,
73                                           attribs.data());
74 
75     ARM_COMPUTE_ERROR_ON_MSG_VAR(context == EGL_NO_CONTEXT, "Failed to create context: 0x%x.", eglGetError());
76     ARM_COMPUTE_UNUSED(res);
77 
78     res = eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, context);
79 
80     ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to make current: 0x%x.", eglGetError());
81     ARM_COMPUTE_UNUSED(res);
82 
83     return std::make_tuple(display, context, res);
84 }
85 } // namespace arm_compute
86