Compute Library
 20.02.1
GCScheduler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 
29 
30 using namespace arm_compute;
31 
32 std::once_flag GCScheduler::_initialize_symbols;
33 
35  : _display(EGL_NO_DISPLAY), _context(EGL_NO_CONTEXT), _target(GPUTarget::MIDGARD)
36 {
37 }
38 
40 {
41  eglDestroyContext(_display, _context);
42  eglTerminate(_display);
43 
44  _context = EGL_NO_CONTEXT;
45  _display = EGL_NO_DISPLAY;
46 }
47 
49 {
50  setup_context();
51 
52  init(_display, _context);
53 }
54 
55 void GCScheduler::default_init_with_context(EGLDisplay display, EGLContext ctx)
56 {
57  _context = ctx;
58  _display = display;
59 
60  _target = get_target_from_device();
61 }
62 
63 void GCScheduler::init(EGLDisplay dpy, EGLContext ctx)
64 {
65  _target = get_target_from_device();
66 
67  GCKernelLibrary::get().init("./cs_shaders/", dpy, ctx);
68 }
69 
71 {
72  std::call_once(_initialize_symbols, opengles31_is_available);
73  static GCScheduler scheduler;
74  return scheduler;
75 }
76 
77 void GCScheduler::dispatch(IGCKernel &kernel, bool flush)
78 {
79  kernel.run(kernel.window());
80  if(flush)
81  {
83  }
84 }
85 
87 {
88  ARM_COMPUTE_GL_CHECK(glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT));
89 }
90 
91 void GCScheduler::setup_context()
92 {
93  EGLBoolean res;
94  _display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
95 
96  ARM_COMPUTE_ERROR_ON_MSG_VAR(_display == EGL_NO_DISPLAY, "Failed to get display: 0x%x.", eglGetError());
97 
98  res = eglInitialize(_display, nullptr, nullptr);
99 
100  ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to initialize egl: 0x%x.", eglGetError());
101  ARM_COMPUTE_UNUSED(res);
102 
103  const char *egl_extension_st = eglQueryString(_display, EGL_EXTENSIONS);
104  ARM_COMPUTE_ERROR_ON_MSG((strstr(egl_extension_st, "EGL_KHR_create_context") == nullptr), "Failed to query EGL_KHR_create_context");
105  ARM_COMPUTE_ERROR_ON_MSG((strstr(egl_extension_st, "EGL_KHR_surfaceless_context") == nullptr), "Failed to query EGL_KHR_surfaceless_context");
106  ARM_COMPUTE_UNUSED(egl_extension_st);
107 
108  const std::array<EGLint, 3> config_attribs =
109  {
110  EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR,
111  EGL_NONE
112  };
113  EGLConfig cfg;
114  EGLint count;
115 
116  res = eglChooseConfig(_display, config_attribs.data(), &cfg, 1, &count);
117 
118  ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to choose config: 0x%x.", eglGetError());
119  ARM_COMPUTE_UNUSED(res);
120 
121  res = eglBindAPI(EGL_OPENGL_ES_API);
122 
123  ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to bind api: 0x%x.", eglGetError());
124 
125  const std::array<EGLint, 3> attribs =
126  {
127  EGL_CONTEXT_CLIENT_VERSION, 3,
128  EGL_NONE
129  };
130  _context = eglCreateContext(_display,
131  cfg,
132  EGL_NO_CONTEXT,
133  attribs.data());
134 
135  ARM_COMPUTE_ERROR_ON_MSG_VAR(_context == EGL_NO_CONTEXT, "Failed to create context: 0x%x.", eglGetError());
136  ARM_COMPUTE_UNUSED(res);
137 
138  res = eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, _context);
139 
140  ARM_COMPUTE_ERROR_ON_MSG_VAR(res == EGL_FALSE, "Failed to make current: 0x%x.", eglGetError());
141  ARM_COMPUTE_UNUSED(res);
142 }
#define ARM_COMPUTE_GL_CHECK(x)
Definition: OpenGLES.h:45
EGLBoolean EGLAPIENTRY eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
Definition: OpenGLES.cpp:256
virtual void run(const Window &window)=0
Enqueue the OpenGL ES shader to process the given window.
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
EGLBoolean EGLAPIENTRY eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config)
Definition: OpenGLES.cpp:191
void dispatch(IGCKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Definition: GCScheduler.cpp:77
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
void GL_APIENTRY glFlush(void)
Definition: OpenGLES.cpp:555
EGLBoolean EGLAPIENTRY eglBindAPI(EGLenum api)
Definition: OpenGLES.cpp:178
char const *EGLAPIENTRY eglQueryString(EGLDisplay dpy, EGLint name)
Definition: OpenGLES.cpp:295
void memory_barrier()
Defines a barrier ordering memory transactions.
Definition: GCScheduler.cpp:86
#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg,...)
Definition: Error.h:457
bool opengles31_is_available()
Check if the OpenGL ES 3.1 API is available at runtime.
Definition: OpenGLES.cpp:160
Copyright (c) 2017-2020 ARM Limited.
void default_init_with_context(EGLDisplay display, EGLContext ctx)
Initializes the context and display used by the Scheduler.
Definition: GCScheduler.cpp:55
EGLBoolean EGLAPIENTRY eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
Definition: OpenGLES.cpp:217
static GCScheduler & get()
Access the scheduler singleton.
Definition: GCScheduler.cpp:70
void init(EGLDisplay dpy, EGLContext ctx)
Initialises the display and context to be used by the scheduler.
Definition: GCScheduler.cpp:63
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
GPUTarget get_target_from_device(const cl::Device &device)
Helper function to get the GPU target from CL device.
Definition: CLHelpers.cpp:226
GCScheduler()
Constructor.
Definition: GCScheduler.cpp:34
Provides global access to a OpenGL ES context and command queue.
Definition: GCScheduler.h:37
void GL_APIENTRY glMemoryBarrier(GLbitfield barriers)
Definition: OpenGLES.cpp:620
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
void init(std::string shader_path="./", EGLDisplay dpy=EGL_NO_DISPLAY, EGLContext ctx=EGL_NO_CONTEXT)
Initialises the kernel library.
GPUTarget
Available GPU Targets.
Definition: GPUTarget.h:34
Common interface for all the GLES kernels.
Definition: IGCKernel.h:39
EGLDisplay EGLAPIENTRY eglGetDisplay(EGLNativeDisplayType display_id)
Definition: OpenGLES.cpp:230
EGLBoolean EGLAPIENTRY eglTerminate(EGLDisplay dpy)
Definition: OpenGLES.cpp:269
void default_init()
Initialises the context and command queue used by the scheduler to default values and sets a default ...
Definition: GCScheduler.cpp:48