Atlas - SDL_qnxgl.c

Home / ext / SDL / src / video / qnx Lines: 1 | Size: 11539 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 2026 BlackBerry Limited 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21 22#include "SDL_internal.h" 23#include "SDL_qnx.h" 24 25static EGLDisplay egl_disp; 26 27struct DummyConfig 28{ 29 int red_size; 30 int green_size; 31 int blue_size; 32 int alpha_size; 33 int native_id; 34}; 35 36static struct DummyConfig getDummyConfigFromScreenSettings(int format) 37{ 38 struct DummyConfig dummyConfig= {}; 39 40 dummyConfig.native_id = format; 41 switch (format) { 42 case SCREEN_FORMAT_RGBX4444: 43 dummyConfig.red_size = 4; 44 dummyConfig.green_size = 4; 45 dummyConfig.blue_size = 4; 46 dummyConfig.alpha_size = 4; 47 break; 48 case SCREEN_FORMAT_RGBA5551: 49 dummyConfig.red_size = 5; 50 dummyConfig.green_size = 5; 51 dummyConfig.blue_size = 5; 52 dummyConfig.alpha_size = 1; 53 break; 54 case SCREEN_FORMAT_RGB565: 55 dummyConfig.red_size = 5; 56 dummyConfig.green_size = 6; 57 dummyConfig.blue_size = 5; 58 dummyConfig.alpha_size = 0; 59 break; 60 case SCREEN_FORMAT_RGB888: 61 dummyConfig.red_size = 8; 62 dummyConfig.green_size = 8; 63 dummyConfig.blue_size = 8; 64 dummyConfig.alpha_size = 0; 65 break; 66 case SCREEN_FORMAT_BGRA8888: 67 case SCREEN_FORMAT_BGRX8888: 68 case SCREEN_FORMAT_RGBA8888: 69 case SCREEN_FORMAT_RGBX8888: 70 dummyConfig.red_size = 8; 71 dummyConfig.green_size = 8; 72 dummyConfig.blue_size = 8; 73 dummyConfig.alpha_size = 8; 74 break; 75 default: 76 break; 77 } 78 return dummyConfig; 79} 80 81static EGLConfig chooseConfig(struct DummyConfig dummyConfig, EGLConfig* egl_configs, EGLint egl_num_configs) 82{ 83 EGLConfig glConfig = (EGLConfig)0; 84 85 for (size_t ii = 0; ii < egl_num_configs; ii++) { 86 EGLint val; 87 88 eglGetConfigAttrib(egl_disp, egl_configs[ii], EGL_SURFACE_TYPE, &val); 89 if (!(val & EGL_WINDOW_BIT)) { 90 continue; 91 } 92 93 eglGetConfigAttrib(egl_disp, egl_configs[ii], EGL_RENDERABLE_TYPE, &val); 94 if (!(val & EGL_OPENGL_ES2_BIT)) { 95 continue; 96 } 97 98 eglGetConfigAttrib(egl_disp, egl_configs[ii], EGL_DEPTH_SIZE, &val); 99 if (val == 0) { 100 continue; 101 } 102 103 eglGetConfigAttrib(egl_disp, egl_configs[ii], EGL_RED_SIZE, &val); 104 if (val != dummyConfig.red_size) { 105 continue; 106 } 107 108 eglGetConfigAttrib(egl_disp, egl_configs[ii], EGL_GREEN_SIZE, &val); 109 if (val != dummyConfig.green_size) { 110 continue; 111 } 112 113 eglGetConfigAttrib(egl_disp, egl_configs[ii], EGL_BLUE_SIZE, &val); 114 if (val != dummyConfig.blue_size) { 115 continue; 116 } 117 118 eglGetConfigAttrib(egl_disp, egl_configs[ii], EGL_ALPHA_SIZE, &val); 119 if (val != dummyConfig.alpha_size) { 120 continue; 121 } 122 if(!glConfig) 123 { 124 glConfig = egl_configs[ii]; 125 } 126 127 eglGetConfigAttrib(egl_disp, egl_configs[ii], EGL_NATIVE_VISUAL_ID, &val); 128 if ((val != 0) && (val == dummyConfig.native_id)) { 129 return egl_configs[ii]; 130 } 131 } 132 return glConfig; 133} 134 135/** 136 * Detertmines the pixel format to use based on the current display and EGL 137 * configuration. 138 * @param egl_conf EGL configuration to use 139 * @return A SCREEN_FORMAT* constant for the pixel format to use 140 */ 141static int chooseFormat(EGLConfig egl_conf) 142{ 143 EGLint buffer_bit_depth; 144 EGLint alpha_bit_depth; 145 146 eglGetConfigAttrib(egl_disp, egl_conf, EGL_BUFFER_SIZE, &buffer_bit_depth); 147 eglGetConfigAttrib(egl_disp, egl_conf, EGL_ALPHA_SIZE, &alpha_bit_depth); 148 149 switch (buffer_bit_depth) { 150 case 32: 151 return SCREEN_FORMAT_RGBX8888; 152 case 24: 153 return SCREEN_FORMAT_RGB888; 154 case 16: 155 switch (alpha_bit_depth) { 156 case 4: 157 return SCREEN_FORMAT_RGBX4444; 158 case 1: 159 return SCREEN_FORMAT_RGBA5551; 160 default: 161 return SCREEN_FORMAT_RGB565; 162 } 163 default: 164 return 0; 165 } 166} 167 168/** 169 * Enumerates the supported EGL configurations and chooses a suitable one. 170 * @param[out] pformat The chosen pixel format 171 * @return true if successful, false on error 172 */ 173bool glInitConfig(SDL_WindowData *impl, int *pformat) 174{ 175 EGLConfig egl_conf = (EGLConfig)0; 176 EGLConfig *egl_configs; 177 EGLint egl_num_configs; 178 EGLBoolean rc; 179 struct DummyConfig dummyconfig = {}; 180 181 // Determine the number of configurations. 182 rc = eglGetConfigs(egl_disp, NULL, 0, &egl_num_configs); 183 if (rc != EGL_TRUE) { 184 return false; 185 } 186 187 if (egl_num_configs == 0) { 188 return false; 189 } 190 191 // Allocate enough memory for all configurations. 192 egl_configs = SDL_malloc(egl_num_configs * sizeof(*egl_configs)); 193 if (!egl_configs) { 194 return false; 195 } 196 197 // Get the list of configurations. 198 rc = eglGetConfigs(egl_disp, egl_configs, egl_num_configs, 199 &egl_num_configs); 200 if (rc != EGL_TRUE) { 201 SDL_free(egl_configs); 202 return false; 203 } 204 205 dummyconfig = getDummyConfigFromScreenSettings(*pformat); 206 egl_conf = chooseConfig(dummyconfig, egl_configs, egl_num_configs); 207 *pformat = chooseFormat(egl_conf); 208 209 SDL_free(egl_configs); 210 impl->conf = egl_conf; 211 212 return true; 213} 214 215/** 216 * Initializes the EGL library. 217 * @param SDL_VideoDevice *_this 218 * @param name unused 219 * @return true if successful, false on error 220 */ 221bool glLoadLibrary(SDL_VideoDevice *_this, const char *name) 222{ 223 EGLNativeDisplayType disp_id = EGL_DEFAULT_DISPLAY; 224 225 egl_disp = eglGetDisplay(disp_id); 226 if (egl_disp == EGL_NO_DISPLAY) { 227 return false; 228 } 229 230 if (eglInitialize(egl_disp, NULL, NULL) == EGL_FALSE) { 231 return false; 232 } 233 234 return true; 235} 236 237/** 238 * Finds the address of an EGL extension function. 239 * @param proc Function name 240 * @return Function address 241 */ 242SDL_FunctionPointer glGetProcAddress(SDL_VideoDevice *_this, const char *proc) 243{ 244 return eglGetProcAddress(proc); 245} 246 247/** 248 * Associates the given window with the necessary EGL structures for drawing and 249 * displaying content. 250 * @param SDL_VideoDevice *_this 251 * @param window The SDL window to create the context for 252 * @return A pointer to the created context, if successful, NULL on error 253 */ 254SDL_GLContext glCreateContext(SDL_VideoDevice *_this, SDL_Window *window) 255{ 256 SDL_WindowData *impl = (SDL_WindowData *)window->internal; 257 EGLContext context; 258 EGLSurface surface; 259 260 struct { 261 EGLint client_version[2]; 262 EGLint none; 263 } egl_ctx_attr = { 264 .client_version = { EGL_CONTEXT_CLIENT_VERSION, 2 }, 265 .none = EGL_NONE 266 }; 267 268 struct { 269 EGLint render_buffer[2]; 270 EGLint none; 271 } egl_surf_attr = { 272 .render_buffer = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER }, 273 .none = EGL_NONE 274 }; 275 276 context = eglCreateContext(egl_disp, impl->conf, EGL_NO_CONTEXT, 277 (EGLint *)&egl_ctx_attr); 278 if (context == EGL_NO_CONTEXT) { 279 return NULL; 280 } 281 282 surface = eglCreateWindowSurface(egl_disp, impl->conf, 283 (EGLNativeWindowType)impl->window, 284 (EGLint *)&egl_surf_attr); 285 if (surface == EGL_NO_SURFACE) { 286 return NULL; 287 } 288 289 eglMakeCurrent(egl_disp, surface, surface, context); 290 291 impl->surface = surface; 292 impl->context = context; 293 294 SDL_SetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_QNX_SURFACE_POINTER, impl->surface); 295 296 return context; 297} 298 299/** 300 * Sets a new value for the number of frames to display before swapping buffers. 301 * @param SDL_VideoDevice *_this 302 * @param interval New interval value 303 * @return true if successful, false on error 304 */ 305bool glSetSwapInterval(SDL_VideoDevice *_this, int interval) 306{ 307 if (eglSwapInterval(egl_disp, interval) != EGL_TRUE) { 308 return false; 309 } 310 311 return true; 312} 313 314/** 315 * Swaps the EGL buffers associated with the given window 316 * @param SDL_VideoDevice *_this 317 * @param window Window to swap buffers for 318 * @return true if successful, false on error 319 */ 320bool glSwapWindow(SDL_VideoDevice *_this, SDL_Window *window) 321{ 322 // !!! FIXME: should we migrate this all over to use SDL_egl.c? 323 SDL_WindowData *impl = (SDL_WindowData *)window->internal; 324 { 325 if (impl->resize) { 326 EGLSurface surface; 327 struct { 328 EGLint render_buffer[2]; 329 EGLint none; 330 } egl_surf_attr = { 331 .render_buffer = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER }, 332 .none = EGL_NONE 333 }; 334 335 if (eglMakeCurrent(egl_disp, NULL, NULL, impl->context) != EGL_TRUE) { 336 return false; 337 } 338 eglDestroySurface(egl_disp, impl->surface); 339 340 surface = eglCreateWindowSurface(egl_disp, impl->conf, impl->window, 341 (EGLint *)&egl_surf_attr); 342 if (surface == EGL_NO_SURFACE) { 343 return false; 344 } 345 346 if (eglMakeCurrent(egl_disp, surface, surface, impl->context) != EGL_TRUE) { 347 return false; 348 } 349 350 impl->surface = surface; 351 impl->resize = 0; 352 } 353 } 354 355 return eglSwapBuffers(egl_disp, impl->surface) == EGL_TRUE ? true : false; 356} 357 358/** 359 * Makes the given context the current one for drawing operations. 360 * @param SDL_VideoDevice *_this 361 * @param window SDL window associated with the context (maybe NULL) 362 * @param context The context to activate 363 * @return true if successful, false on error 364 */ 365bool glMakeCurrent(SDL_VideoDevice *_this, SDL_Window *window, SDL_GLContext context) 366{ 367 SDL_WindowData *impl; 368 EGLSurface surface = NULL; 369 370 if (window) { 371 impl = (SDL_WindowData *)window->internal; 372 surface = impl->surface; 373 } 374 375 if (eglMakeCurrent(egl_disp, surface, surface, context) != EGL_TRUE) { 376 return false; 377 } 378 379 return true; 380} 381 382/** 383 * Destroys a context. 384 * @param SDL_VideoDevice *_this 385 * @param context The context to destroy 386 */ 387bool glDeleteContext(SDL_VideoDevice *_this, SDL_GLContext context) 388{ 389 eglDestroyContext(egl_disp, context); 390 return true; 391} 392 393/** 394 * Terminates access to the EGL library. 395 * @param SDL_VideoDevice *_this 396 */ 397void glUnloadLibrary(SDL_VideoDevice *_this) 398{ 399 eglTerminate(egl_disp); 400} 401
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.