| 1 | // SPDX-FileCopyrightText: 2016 Gaetan Allaert |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <iostream> |
| 6 | #include <string.h> |
| 7 | #include <sys/time.h> |
| 8 | |
| 9 | #ifdef _RPI |
| 10 | #include <bcm_host.h> |
| 11 | #else |
| 12 | #include <X11/Xlib.h> |
| 13 | #include <X11/Xatom.h> |
| 14 | #include <X11/Xutil.h> |
| 15 | #endif |
| 16 | |
| 17 | #include "GLES_utils.hxx" |
| 18 | |
| 19 | /// esCreateWindow flag - RGB color buffer |
| 20 | #define GLES_UTILS_WINDOW_RGB 0 |
| 21 | /// esCreateWindow flag - ALPHA color buffer |
| 22 | #define GLES_UTILS_WINDOW_ALPHA 1 |
| 23 | /// esCreateWindow flag - depth buffer |
| 24 | #define GLES_UTILS_WINDOW_DEPTH 2 |
| 25 | /// esCreateWindow flag - stencil buffer |
| 26 | #define GLES_UTILS_WINDOW_STENCIL 4 |
| 27 | /// esCreateWindow flat - multi-sample buffer |
| 28 | #define GLES_UTILS_WINDOW_MULTISAMPLE 8 |
| 29 | |
| 30 | GLES_utils::GLES_utils () : |
| 31 | m_State (), |
| 32 | display_func (NULL), |
| 33 | idle_func (NULL), |
| 34 | keyboard_func (NULL), |
| 35 | reshape_func (NULL) {} |
| 36 | |
| 37 | GLES_utils::~GLES_utils () {} |
| 38 | |
| 39 | GLES_utils& |
| 40 | GLES_utils::instance () { |
| 41 | static GLES_utils* const The_Instance (new GLES_utils); |
| 42 | return *The_Instance; |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | GLES_utils::init (const string &title) { |
| 47 | #ifdef _RPI |
| 48 | bcm_host_init (); |
| 49 | init_dispmanx (m_State.native_window); |
| 50 | #else |
| 51 | init_display (m_State, title); |
| 52 | #endif |
| 53 | init_egl (m_State, GLES_UTILS_WINDOW_RGB); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | GLES_utils::print_config_info (const int n, const EGLDisplay &display, EGLConfig &config) { |
| 58 | int size; |
| 59 | |
| 60 | cout << "EGL Configuration " << n << " is" << endl; |
| 61 | |
| 62 | eglGetConfigAttrib (display, config, EGL_RED_SIZE, &size); |
| 63 | cout << "EGL Red size is " << size << endl; |
| 64 | |
| 65 | eglGetConfigAttrib (display, config, EGL_BLUE_SIZE, &size); |
| 66 | cout << "EGL Blue size is " << size << endl; |
| 67 | |
| 68 | eglGetConfigAttrib (display, config, EGL_GREEN_SIZE, &size); |
| 69 | cout << "EGL Green size is " << size << endl; |
| 70 | |
| 71 | eglGetConfigAttrib (display, config, EGL_BUFFER_SIZE, &size); |
| 72 | cout << "EGL Buffer size is " << size << endl; |
| 73 | |
| 74 | eglGetConfigAttrib (display, config, EGL_BIND_TO_TEXTURE_RGB , &size); |
| 75 | if (size == EGL_TRUE) { |
| 76 | cout << "EGL Can be bound to RGB texture" << endl; |
| 77 | } else { |
| 78 | cout << "EGL Can't be bound to RGB texture" << endl; |
| 79 | } |
| 80 | |
| 81 | eglGetConfigAttrib (display, config, EGL_BIND_TO_TEXTURE_RGBA , &size); |
| 82 | if (size == EGL_TRUE) { |
| 83 | cout << "EGL Can be bound to RGBA texture" << endl; |
| 84 | } else { |
| 85 | cout << "EGL Can't be bound to RGBA texture" << endl; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | GLES_utils::init_egl (EGL_STATE_T &state, const GLuint flags) { |
| 91 | EGLBoolean result; |
| 92 | |
| 93 | static const EGLint attribute_list[] = { |
| 94 | EGL_RED_SIZE, 5, |
| 95 | EGL_GREEN_SIZE, 6, |
| 96 | EGL_BLUE_SIZE, 5, |
| 97 | EGL_ALPHA_SIZE, (flags & GLES_UTILS_WINDOW_ALPHA) ? 8 : EGL_DONT_CARE, |
| 98 | EGL_DEPTH_SIZE, (flags & GLES_UTILS_WINDOW_DEPTH) ? 8 : EGL_DONT_CARE, |
| 99 | EGL_STENCIL_SIZE, (flags & GLES_UTILS_WINDOW_STENCIL) ? 8 : EGL_DONT_CARE, |
| 100 | EGL_SAMPLE_BUFFERS, (flags & GLES_UTILS_WINDOW_MULTISAMPLE) ? 1 : 0, |
| 101 | EGL_NONE |
| 102 | }; |
| 103 | |
| 104 | static const EGLint context_attributes[] = { |
| 105 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 106 | EGL_NONE |
| 107 | #ifndef _RPI |
| 108 | , EGL_NONE |
| 109 | #endif |
| 110 | }; |
| 111 | |
| 112 | EGLConfig *configs; |
| 113 | |
| 114 | // get an EGL display connection |
| 115 | #ifdef _RPI |
| 116 | state.display = eglGetDisplay (EGL_DEFAULT_DISPLAY); |
| 117 | #else |
| 118 | state.display = eglGetDisplay ((EGLNativeDisplayType) state.x_display); |
| 119 | #endif |
| 120 | assert (state.display != EGL_NO_DISPLAY); |
| 121 | |
| 122 | // initialize the EGL display connection |
| 123 | result = eglInitialize (state.display, &(state.major_version), &(state.minor_version)); |
| 124 | assert (result != EGL_FALSE); |
| 125 | |
| 126 | result = eglGetConfigs (state.display, NULL, 0, &(state.num_configs)); |
| 127 | assert (result != EGL_FALSE); |
| 128 | |
| 129 | configs = (EGLConfig *) calloc (state.num_configs, sizeof (*configs)); |
| 130 | result = eglGetConfigs (state.display, configs, state.num_configs, &(state.num_configs)); |
| 131 | assert (result != EGL_FALSE); |
| 132 | |
| 133 | cout << "EGL version = " << state.major_version << "." << state.minor_version << endl; |
| 134 | cout << "EGL has " << state.num_configs << " configs" << endl; |
| 135 | for (int i = 0; i < state.num_configs; ++i) { |
| 136 | print_config_info (i, state.display, configs[i]); |
| 137 | } |
| 138 | |
| 139 | // get an appropriate EGL frame buffer configuration |
| 140 | result = eglChooseConfig (state.display, attribute_list, &(state.config), 1, &(state.num_configs)); |
| 141 | assert (result != EGL_FALSE); |
| 142 | |
| 143 | // Choose the OpenGL ES API |
| 144 | result = eglBindAPI (EGL_OPENGL_ES_API); |
| 145 | assert (result != EGL_FALSE); |
| 146 | |
| 147 | #ifdef _RPI |
| 148 | state.surface = eglCreateWindowSurface (state.display, |
| 149 | state.config, |
| 150 | (EGLNativeWindowType) &(state.native_window), |
| 151 | NULL); |
| 152 | #else |
| 153 | state.surface = eglCreateWindowSurface (state.display, |
| 154 | state.config, |
| 155 | state.native_window, |
| 156 | NULL); |
| 157 | #endif |
| 158 | assert (state.surface != EGL_NO_SURFACE); |
| 159 | |
| 160 | // create an EGL rendering context |
| 161 | state.context = eglCreateContext (state.display, state.config, EGL_NO_CONTEXT, context_attributes); |
| 162 | assert (state.context != EGL_NO_CONTEXT); |
| 163 | |
| 164 | // connect the context to the surface |
| 165 | result = eglMakeCurrent (state.display, state.surface, state.surface, state.context); |
| 166 | assert (result != EGL_FALSE); |
| 167 | } |
| 168 | |
| 169 | #ifdef _RPI |
| 170 | void |
| 171 | GLES_utils::init_dispmanx (EGL_DISPMANX_WINDOW_T &native_window) { |
| 172 | int32_t success = 0; |
| 173 | uint32_t screen_width; |
| 174 | uint32_t screen_height; |
| 175 | |
| 176 | DISPMANX_ELEMENT_HANDLE_T dispman_element; |
| 177 | DISPMANX_DISPLAY_HANDLE_T dispman_display; |
| 178 | DISPMANX_UPDATE_HANDLE_T dispman_update; |
| 179 | VC_RECT_T dst_rect; |
| 180 | VC_RECT_T src_rect; |
| 181 | |
| 182 | // create an EGL window surface |
| 183 | success = graphics_get_display_size (0 /* LCD */, |
| 184 | &screen_width, |
| 185 | &screen_height); |
| 186 | assert (success >= 0); |
| 187 | |
| 188 | dst_rect.x = 0; |
| 189 | dst_rect.y = 0; |
| 190 | dst_rect.width = screen_width; |
| 191 | dst_rect.height = screen_height; |
| 192 | |
| 193 | src_rect.x = 0; |
| 194 | src_rect.y = 0; |
| 195 | src_rect.width = screen_width << 16; |
| 196 | src_rect.height = screen_height << 16; |
| 197 | |
| 198 | dispman_display = vc_dispmanx_display_open (0 /* LCD */); |
| 199 | dispman_update = vc_dispmanx_update_start (0); |
| 200 | |
| 201 | dispman_element = vc_dispmanx_element_add (dispman_update, |
| 202 | dispman_display, |
| 203 | 0 /*layer*/, |
| 204 | &dst_rect, |
| 205 | 0 /*src*/, |
| 206 | &src_rect, |
| 207 | DISPMANX_PROTECTION_NONE, |
| 208 | 0 /*alpha*/, |
| 209 | 0 /*clamp*/, |
| 210 | DISPMANX_TRANSFORM_T (0) /*transform*/); |
| 211 | |
| 212 | // Build an EGL_DISPMANX_WINDOW_T from the Dispmanx window |
| 213 | native_window.element = dispman_element; |
| 214 | native_window.width = screen_width; |
| 215 | native_window.height = screen_height; |
| 216 | vc_dispmanx_update_submit_sync (dispman_update); |
| 217 | |
| 218 | cout << "Got a Dispmanx window" << endl; |
| 219 | } |
| 220 | |
| 221 | GLboolean |
| 222 | GLES_utils::user_interrupt () { |
| 223 | return GL_FALSE; |
| 224 | } |
| 225 | #else |
| 226 | void |
| 227 | GLES_utils::init_display (EGL_STATE_T &state, const string &title) { |
| 228 | Window root; |
| 229 | XSetWindowAttributes swa; |
| 230 | XSetWindowAttributes xattr; |
| 231 | Atom wm_state; |
| 232 | XWMHints hints; |
| 233 | XEvent xev; |
| 234 | Window win; |
| 235 | |
| 236 | state.width = 1024; |
| 237 | state.height = 768; |
| 238 | |
| 239 | /* |
| 240 | * X11 native display initialization |
| 241 | */ |
| 242 | |
| 243 | state.x_display = XOpenDisplay (NULL); |
| 244 | assert (state.x_display != NULL); |
| 245 | |
| 246 | root = DefaultRootWindow (state.x_display); |
| 247 | |
| 248 | swa.event_mask = ExposureMask | PointerMotionMask | KeyPressMask; |
| 249 | win = XCreateWindow (state.x_display, |
| 250 | root, |
| 251 | 0, |
| 252 | 0, |
| 253 | state.width, |
| 254 | state.height, |
| 255 | 0, |
| 256 | CopyFromParent, |
| 257 | InputOutput, |
| 258 | CopyFromParent, |
| 259 | CWEventMask, |
| 260 | &swa); |
| 261 | |
| 262 | xattr.override_redirect = false; |
| 263 | XChangeWindowAttributes (state.x_display, win, CWOverrideRedirect, &xattr); |
| 264 | |
| 265 | hints.input = true; |
| 266 | hints.flags = InputHint; |
| 267 | XSetWMHints (state.x_display, win, &hints); |
| 268 | |
| 269 | // make the window visible on the screen |
| 270 | XMapWindow (state.x_display, win); |
| 271 | XStoreName (state.x_display, win, title.c_str ()); |
| 272 | |
| 273 | // get identifiers for the provided atom name strings |
| 274 | wm_state = XInternAtom (state.x_display, "_NET_WM_STATE", false); |
| 275 | |
| 276 | memset (&xev, 0, sizeof (xev)); |
| 277 | xev.type = ClientMessage; |
| 278 | xev.xclient.window = win; |
| 279 | xev.xclient.message_type = wm_state; |
| 280 | xev.xclient.format = 32; |
| 281 | xev.xclient.data.l[0] = 1; |
| 282 | xev.xclient.data.l[1] = false; |
| 283 | XSendEvent (state.x_display, DefaultRootWindow (state.x_display), false, SubstructureNotifyMask, &xev); |
| 284 | |
| 285 | state.native_window = (EGLNativeWindowType) win; |
| 286 | } |
| 287 | |
| 288 | GLboolean |
| 289 | GLES_utils::user_interrupt () { |
| 290 | XEvent xev; |
| 291 | KeySym key; |
| 292 | GLboolean user_interrupt = GL_FALSE; |
| 293 | char text; |
| 294 | |
| 295 | // Pump all messages from X server. Keypresses are directed to keyfunc (if defined) |
| 296 | while (XPending (m_State.x_display)) { |
| 297 | XNextEvent (m_State.x_display, &xev); |
| 298 | if (xev.type == KeyPress) { |
| 299 | if (XLookupString (&xev.xkey, &text, 1, &key, 0) == 1) { |
| 300 | if (keyboard_func != NULL) { |
| 301 | keyboard_func (text, 0, 0); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | if (xev.type == DestroyNotify) { |
| 306 | user_interrupt = GL_TRUE; |
| 307 | } |
| 308 | } |
| 309 | return user_interrupt; |
| 310 | } |
| 311 | #endif |
| 312 | |
| 313 | void |
| 314 | GLES_utils::register_display_func (void (*display_func) ()) { |
| 315 | this->display_func = display_func; |
| 316 | } |
| 317 | |
| 318 | void |
| 319 | GLES_utils::register_idle_func (void (*idle_func) ()) { |
| 320 | this->idle_func = idle_func; |
| 321 | } |
| 322 | |
| 323 | void |
| 324 | GLES_utils::register_keyboard_func (void (*keyboard_func) (unsigned char, int, int)) { |
| 325 | this->keyboard_func = keyboard_func; |
| 326 | } |
| 327 | |
| 328 | void |
| 329 | GLES_utils::register_reshape_func (void (*reshape_func) (int, int)) { |
| 330 | this->reshape_func = reshape_func; |
| 331 | } |
| 332 | |
| 333 | void |
| 334 | GLES_utils::main_loop () { |
| 335 | struct timeval t1, t2; |
| 336 | struct timezone tz; |
| 337 | float delta_time; |
| 338 | float total_time = 0.0f; |
| 339 | unsigned int frames = 0; |
| 340 | |
| 341 | gettimeofday (&t1 , &tz); |
| 342 | |
| 343 | while (user_interrupt () == GL_FALSE) { |
| 344 | gettimeofday (&t2, &tz); |
| 345 | delta_time = (float) (t2.tv_sec - t1.tv_sec + (t2.tv_usec - t1.tv_usec) * 1e-6); |
| 346 | t1 = t2; |
| 347 | |
| 348 | if (reshape_func != NULL) { |
| 349 | #ifdef _RPI |
| 350 | reshape_func (m_State.native_window.width, m_State.native_window.height); |
| 351 | #else |
| 352 | reshape_func (m_State.width, m_State.height); |
| 353 | #endif |
| 354 | } |
| 355 | if (idle_func != NULL) { |
| 356 | idle_func (); |
| 357 | } |
| 358 | if (display_func != NULL) { |
| 359 | display_func (); |
| 360 | } |
| 361 | |
| 362 | eglSwapBuffers (m_State.display, m_State.surface); |
| 363 | |
| 364 | total_time += delta_time; |
| 365 | ++frames; |
| 366 | if (total_time > 2.0f) { |
| 367 | cout << frames << " frames rendered in " << total_time << " seconds -> FPS = " << (frames / total_time) << endl; |
| 368 | total_time -= 2.0f; |
| 369 | frames = 0; |
| 370 | } |
| 371 | } |
| 372 | } |