Compute Library
 21.02
GCHelpers.cpp
Go to the documentation of this file.
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 
26 
27 #include "arm_compute/core/Error.h"
28 
29 namespace arm_compute
30 {
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
EGLBoolean EGLAPIENTRY eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
Definition: OpenGLES.cpp:256
EGLBoolean EGLAPIENTRY eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config)
Definition: OpenGLES.cpp:191
EGLContext EGLAPIENTRY eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list)
Definition: OpenGLES.cpp:204
EGLint EGLAPIENTRY eglGetError()
Definition: OpenGLES.cpp:282
EGLBoolean EGLAPIENTRY eglBindAPI(EGLenum api)
Definition: OpenGLES.cpp:178
char const *EGLAPIENTRY eglQueryString(EGLDisplay dpy, EGLint name)
Definition: OpenGLES.cpp:295
#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg,...)
Definition: Error.h:457
Copyright (c) 2017-2021 Arm Limited.
EGLBoolean EGLAPIENTRY eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
Definition: OpenGLES.cpp:243
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
EGLDisplay EGLAPIENTRY eglGetDisplay(EGLNativeDisplayType display_id)
Definition: OpenGLES.cpp:230
std::tuple< EGLDisplay, EGLContext, EGLBoolean > create_opengl_display_and_context()
This function creates an OpenGL-ES context and a display.
Definition: GCHelpers.cpp:31