Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 1 | /* |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 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/GCScheduler.h" |
| 26 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 27 | #include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" |
| 29 | |
| 30 | using namespace arm_compute; |
| 31 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 32 | std::once_flag GCScheduler::_initialize_symbols; |
| 33 | |
| 34 | GCScheduler::GCScheduler() |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 35 | : _display(EGL_NO_DISPLAY), _context(EGL_NO_CONTEXT), _target(GPUTarget::MIDGARD) |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | GCScheduler::~GCScheduler() |
| 40 | { |
| 41 | eglDestroyContext(_display, _context); |
| 42 | eglTerminate(_display); |
| 43 | |
| 44 | _context = EGL_NO_CONTEXT; |
| 45 | _display = EGL_NO_DISPLAY; |
| 46 | } |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 47 | |
| 48 | void GCScheduler::default_init() |
| 49 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 50 | setup_context(); |
| 51 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 52 | init(_display, _context); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void GCScheduler::init(EGLDisplay dpy, EGLContext ctx) |
| 56 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame^] | 57 | _target = get_target_from_device(); |
| 58 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 59 | GCKernelLibrary::get().init("./cs_shaders/", dpy, ctx); |
| 60 | } |
| 61 | |
| 62 | GCScheduler &GCScheduler::get() |
| 63 | { |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 64 | std::call_once(_initialize_symbols, opengles31_is_available); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 65 | static GCScheduler scheduler; |
| 66 | return scheduler; |
| 67 | } |
| 68 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 69 | void GCScheduler::dispatch(IGCKernel &kernel, bool flush) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 70 | { |
| 71 | kernel.run(kernel.window()); |
| 72 | if(flush) |
| 73 | { |
| 74 | ARM_COMPUTE_GL_CHECK(glFlush()); |
| 75 | } |
| 76 | } |
| 77 | |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 78 | void GCScheduler::memory_barrier() |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 79 | { |
| 80 | ARM_COMPUTE_GL_CHECK(glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT)); |
| 81 | } |
Anthony Barbier | f45d5a9 | 2018-01-24 16:23:15 +0000 | [diff] [blame] | 82 | |
| 83 | void GCScheduler::setup_context() |
| 84 | { |
| 85 | EGLBoolean res; |
| 86 | _display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 87 | |
| 88 | ARM_COMPUTE_ERROR_ON_MSG(_display == EGL_NO_DISPLAY, "Failed to get display: 0x%x.", eglGetError()); |
| 89 | |
| 90 | res = eglInitialize(_display, nullptr, nullptr); |
| 91 | |
| 92 | ARM_COMPUTE_ERROR_ON_MSG(res == EGL_FALSE, "Failed to initialize egl: 0x%x.", eglGetError()); |
| 93 | ARM_COMPUTE_UNUSED(res); |
| 94 | |
| 95 | const char *egl_extension_st = eglQueryString(_display, EGL_EXTENSIONS); |
| 96 | ARM_COMPUTE_ERROR_ON_MSG((strstr(egl_extension_st, "EGL_KHR_create_context") == nullptr), "Failed to query EGL_KHR_create_context"); |
| 97 | ARM_COMPUTE_ERROR_ON_MSG((strstr(egl_extension_st, "EGL_KHR_surfaceless_context") == nullptr), "Failed to query EGL_KHR_surfaceless_context"); |
| 98 | ARM_COMPUTE_UNUSED(egl_extension_st); |
| 99 | |
| 100 | const EGLint config_attribs[] = |
| 101 | { |
| 102 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR, |
| 103 | EGL_NONE |
| 104 | }; |
| 105 | EGLConfig cfg; |
| 106 | EGLint count; |
| 107 | |
| 108 | res = eglChooseConfig(_display, config_attribs, &cfg, 1, &count); |
| 109 | |
| 110 | ARM_COMPUTE_ERROR_ON_MSG(res == EGL_FALSE, "Failed to choose config: 0x%x.", eglGetError()); |
| 111 | ARM_COMPUTE_UNUSED(res); |
| 112 | |
| 113 | res = eglBindAPI(EGL_OPENGL_ES_API); |
| 114 | |
| 115 | ARM_COMPUTE_ERROR_ON_MSG(res == EGL_FALSE, "Failed to bind api: 0x%x.", eglGetError()); |
| 116 | |
| 117 | const EGLint attribs[] = |
| 118 | { |
| 119 | EGL_CONTEXT_CLIENT_VERSION, 3, |
| 120 | EGL_NONE |
| 121 | }; |
| 122 | _context = eglCreateContext(_display, |
| 123 | cfg, |
| 124 | EGL_NO_CONTEXT, |
| 125 | attribs); |
| 126 | |
| 127 | ARM_COMPUTE_ERROR_ON_MSG(_context == EGL_NO_CONTEXT, "Failed to create context: 0x%x.", eglGetError()); |
| 128 | ARM_COMPUTE_UNUSED(res); |
| 129 | |
| 130 | res = eglMakeCurrent(_display, EGL_NO_SURFACE, EGL_NO_SURFACE, _context); |
| 131 | |
| 132 | ARM_COMPUTE_ERROR_ON_MSG(res == EGL_FALSE, "Failed to make current: 0x%x.", eglGetError()); |
| 133 | ARM_COMPUTE_UNUSED(res); |
| 134 | } |