SDL  2.0
gl.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 2017 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 
26 
27 /**
28  * Detertmines the pixel format to use based on the current display and EGL
29  * configuration.
30  * @param egl_conf EGL configuration to use
31  * @return A SCREEN_FORMAT* constant for the pixel format to use
32  */
33 static int
35 {
36  EGLint buffer_bit_depth;
37  EGLint alpha_bit_depth;
38 
39  eglGetConfigAttrib(egl_disp, egl_conf, EGL_BUFFER_SIZE, &buffer_bit_depth);
40  eglGetConfigAttrib(egl_disp, egl_conf, EGL_ALPHA_SIZE, &alpha_bit_depth);
41 
42  switch (buffer_bit_depth) {
43  case 32:
44  return SCREEN_FORMAT_RGBX8888;
45  case 24:
46  return SCREEN_FORMAT_RGB888;
47  case 16:
48  switch (alpha_bit_depth) {
49  case 4:
50  return SCREEN_FORMAT_RGBX4444;
51  case 1:
52  return SCREEN_FORMAT_RGBA5551;
53  default:
54  return SCREEN_FORMAT_RGB565;
55  }
56  default:
57  return 0;
58  }
59 }
60 
61 /**
62  * Enumerates the supported EGL configurations and chooses a suitable one.
63  * @param[out] pconf The chosen configuration
64  * @param[out] pformat The chosen pixel format
65  * @return 0 if successful, -1 on error
66  */
67 int
68 glGetConfig(EGLConfig *pconf, int *pformat)
69 {
70  EGLConfig egl_conf = (EGLConfig)0;
71  EGLConfig *egl_configs;
72  EGLint egl_num_configs;
73  EGLint val;
74  EGLBoolean rc;
75  EGLint i;
76 
77  // Determine the numbfer of configurations.
78  rc = eglGetConfigs(egl_disp, NULL, 0, &egl_num_configs);
79  if (rc != EGL_TRUE) {
80  return -1;
81  }
82 
83  if (egl_num_configs == 0) {
84  return -1;
85  }
86 
87  // Allocate enough memory for all configurations.
88  egl_configs = malloc(egl_num_configs * sizeof(*egl_configs));
89  if (egl_configs == NULL) {
90  return -1;
91  }
92 
93  // Get the list of configurations.
94  rc = eglGetConfigs(egl_disp, egl_configs, egl_num_configs,
95  &egl_num_configs);
96  if (rc != EGL_TRUE) {
97  free(egl_configs);
98  return -1;
99  }
100 
101  // Find a good configuration.
102  for (i = 0; i < egl_num_configs; i++) {
103  eglGetConfigAttrib(egl_disp, egl_configs[i], EGL_SURFACE_TYPE, &val);
104  if (!(val & EGL_WINDOW_BIT)) {
105  continue;
106  }
107 
108  eglGetConfigAttrib(egl_disp, egl_configs[i], EGL_RENDERABLE_TYPE, &val);
109  if (!(val & EGL_OPENGL_ES2_BIT)) {
110  continue;
111  }
112 
113  eglGetConfigAttrib(egl_disp, egl_configs[i], EGL_DEPTH_SIZE, &val);
114  if (val == 0) {
115  continue;
116  }
117 
118  egl_conf = egl_configs[i];
119  break;
120  }
121 
122  free(egl_configs);
123  *pconf = egl_conf;
124  *pformat = chooseFormat(egl_conf);
125 
126  return 0;
127 }
128 
129 /**
130  * Initializes the EGL library.
131  * @param _THIS
132  * @param name unused
133  * @return 0 if successful, -1 on error
134  */
135 int
136 glLoadLibrary(_THIS, const char *name)
137 {
139 
140  egl_disp = eglGetDisplay(disp_id);
141  if (egl_disp == EGL_NO_DISPLAY) {
142  return -1;
143  }
144 
146  return -1;
147  }
148 
149  return 0;
150 }
151 
152 /**
153  * Finds the address of an EGL extension function.
154  * @param proc Function name
155  * @return Function address
156  */
157 void *
158 glGetProcAddress(_THIS, const char *proc)
159 {
160  return eglGetProcAddress(proc);
161 }
162 
163 /**
164  * Associates the given window with the necessary EGL structures for drawing and
165  * displaying content.
166  * @param _THIS
167  * @param window The SDL window to create the context for
168  * @return A pointer to the created context, if successful, NULL on error
169  */
172 {
173  window_impl_t *impl = (window_impl_t *)window->driverdata;
176 
177  struct {
178  EGLint client_version[2];
179  EGLint none;
180  } egl_ctx_attr = {
181  .client_version = { EGL_CONTEXT_CLIENT_VERSION, 2 },
182  .none = EGL_NONE
183  };
184 
185  struct {
186  EGLint render_buffer[2];
187  EGLint none;
188  } egl_surf_attr = {
189  .render_buffer = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER },
190  .none = EGL_NONE
191  };
192 
194  (EGLint *)&egl_ctx_attr);
195  if (context == EGL_NO_CONTEXT) {
196  return NULL;
197  }
198 
199  surface = eglCreateWindowSurface(egl_disp, impl->conf, impl->window,
200  (EGLint *)&egl_surf_attr);
201  if (surface == EGL_NO_SURFACE) {
202  return NULL;
203  }
204 
206 
207  impl->surface = surface;
208  return context;
209 }
210 
211 /**
212  * Sets a new value for the number of frames to display before swapping buffers.
213  * @param _THIS
214  * @param interval New interval value
215  * @return 0 if successful, -1 on error
216  */
217 int
218 glSetSwapInterval(_THIS, int interval)
219 {
220  if (eglSwapInterval(egl_disp, interval) != EGL_TRUE) {
221  return -1;
222  }
223 
224  return 0;
225 }
226 
227 /**
228  * Swaps the EGL buffers associated with the given window
229  * @param _THIS
230  * @param window Window to swap buffers for
231  * @return 0 if successful, -1 on error
232  */
233 int
235 {
236  /* !!! FIXME: should we migrate this all over to use SDL_egl.c? */
237  window_impl_t *impl = (window_impl_t *)window->driverdata;
238  return eglSwapBuffers(egl_disp, impl->surface) == EGL_TRUE ? 0 : -1;
239 }
240 
241 /**
242  * Makes the given context the current one for drawing operations.
243  * @param _THIS
244  * @param window SDL window associated with the context (maybe NULL)
245  * @param context The context to activate
246  * @return 0 if successful, -1 on error
247  */
248 int
250 {
251  window_impl_t *impl;
253 
254  if (window) {
255  impl = (window_impl_t *)window->driverdata;
256  surface = impl->surface;
257  }
258 
259  if (eglMakeCurrent(egl_disp, surface, surface, context) != EGL_TRUE) {
260  return -1;
261  }
262 
263  return 0;
264 }
265 
266 /**
267  * Destroys a context.
268  * @param _THIS
269  * @param context The context to destroy
270  */
271 void
273 {
274  eglDestroyContext(egl_disp, context);
275 }
276 
277 /**
278  * Terminates access to the EGL library.
279  * @param _THIS
280  */
281 void
283 {
285 }
EGLAPI EGLBoolean EGLAPIENTRY eglTerminate(EGLDisplay dpy)
void * EGLSurface
Definition: egl.h:59
int glGetConfig(EGLConfig *pconf, int *pformat)
Definition: gl.c:68
#define EGL_WINDOW_BIT
Definition: egl.h:120
EGLAPI EGLSurface EGLAPIENTRY eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list)
void * EGLConfig
Definition: egl.h:58
EGLAPI EGLBoolean EGLAPIENTRY eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
#define EGL_NO_SURFACE
Definition: egl.h:100
SDL_GLContext glCreateContext(_THIS, SDL_Window *window)
Definition: gl.c:171
#define EGL_SURFACE_TYPE
Definition: egl.h:110
HDC EGLNativeDisplayType
Definition: eglplatform.h:76
void glUnloadLibrary(_THIS)
Definition: gl.c:282
SDL_EventEntry * free
Definition: SDL_events.c:84
EGLSurface surface
Definition: eglext.h:248
static int chooseFormat(EGLConfig egl_conf)
Definition: gl.c:34
#define EGL_ALPHA_SIZE
Definition: egl.h:62
static screen_context_t context
Definition: video.c:25
#define EGL_NONE
Definition: egl.h:95
int glLoadLibrary(_THIS, const char *name)
Definition: gl.c:136
EGLAPI __eglMustCastToProperFunctionPointerType EGLAPIENTRY eglGetProcAddress(const char *procname)
void * EGLDisplay
Definition: egl.h:55
#define EGL_RENDER_BUFFER
Definition: egl.h:196
EGLAPI EGLBoolean EGLAPIENTRY eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value)
#define EGL_TRUE
Definition: egl.h:116
void glDeleteContext(_THIS, SDL_GLContext context)
Definition: gl.c:272
#define EGL_NO_DISPLAY
Definition: egl.h:99
GLuint const GLchar * name
int glSetSwapInterval(_THIS, int interval)
Definition: gl.c:218
unsigned int EGLBoolean
Definition: egl.h:54
#define EGL_DEFAULT_DISPLAY
Definition: egl.h:227
EGLAPI EGLBoolean EGLAPIENTRY eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
void * EGLContext
Definition: egl.h:60
#define EGL_BUFFER_SIZE
Definition: egl.h:76
void * SDL_GLContext
An opaque handle to an OpenGL context.
Definition: SDL_video.h:175
GLuint GLfloat * val
EGLSurface surface
Definition: sdl_qnx.h:32
#define EGL_NO_CONTEXT
Definition: egl.h:98
#define EGL_CONTEXT_CLIENT_VERSION
Definition: egl.h:212
#define _THIS
khronos_int32_t EGLint
Definition: eglplatform.h:122
EGLAPI EGLBoolean EGLAPIENTRY eglGetConfigs(EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config)
#define EGL_DEPTH_SIZE
Definition: egl.h:80
#define EGL_FALSE
Definition: egl.h:84
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164
EGLAPI EGLDisplay EGLAPIENTRY eglGetDisplay(EGLNativeDisplayType display_id)
EGLAPI EGLBoolean EGLAPIENTRY eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
The type used to identify a window.
Definition: SDL_sysvideo.h:73
static EGLDisplay egl_disp
Definition: gl.c:25
EGLAPI EGLBoolean EGLAPIENTRY eglSwapInterval(EGLDisplay dpy, EGLint interval)
void * glGetProcAddress(_THIS, const char *proc)
Definition: gl.c:158
void * driverdata
Definition: SDL_sysvideo.h:111
int glMakeCurrent(_THIS, SDL_Window *window, SDL_GLContext context)
Definition: gl.c:249
#define EGL_BACK_BUFFER
Definition: egl.h:149
int glSwapWindow(_THIS, SDL_Window *window)
Definition: gl.c:234
EGLAPI EGLContext EGLAPIENTRY eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list)
#define EGL_RENDERABLE_TYPE
Definition: egl.h:195
#define malloc
Definition: SDL_qsort.c:47
#define EGL_OPENGL_ES2_BIT
Definition: egl.h:214
EGLAPI EGLBoolean EGLAPIENTRY eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)