summaryrefslogtreecommitdiff
path: root/src/glut_egl/glut_egl.c
blob: 6347d9d962796f82d4ed6672d105ad5e116e2c5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Copyright (C) 2010 LunarG Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Chia-I Wu <olv@lunarg.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/time.h>

#include "EGL/egl.h"
#include "EGL/eglext.h"

#include "glut_eglint.h"

static struct glut_state _glut_state = {
   .api_mask = GLUT_OPENGL_ES1_BIT,
   .display_mode = GLUT_RGB,
   .window_width = 300,
   .window_height = 300,
   .verbose = 0,
   .num_windows = 0,
};

struct glut_state *_glut = &_glut_state;

void
_glutFatal(char *format, ...)
{
  va_list args;

  va_start(args, format);

  fprintf(stderr, "GLUT_EGL: ");
  vfprintf(stderr, format, args);
  va_end(args);
  putc('\n', stderr);

  exit(1);
}

/* return current time (in milliseconds) */
int
_glutNow(void)
{
   struct timeval tv;
#ifdef __VMS
   (void) gettimeofday(&tv, NULL );
#else
   struct timezone tz;
   (void) gettimeofday(&tv, &tz);
#endif
   return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

static void
_glutDestroyWindow(struct glut_window *win)
{
   if (_glut->surface_type != EGL_PBUFFER_BIT &&
       _glut->surface_type != EGL_SCREEN_BIT_MESA)
      eglDestroySurface(_glut->dpy, win->surface);

   _glutNativeFiniWindow(win);

   eglDestroyContext(_glut->dpy, win->context);
}

static EGLConfig
_glutChooseConfig(void)
{
   EGLConfig config;
   EGLint config_attribs[32];
   EGLint renderable_type, num_configs, i;

   i = 0;
   config_attribs[i++] = EGL_RED_SIZE;
   config_attribs[i++] = 1;
   config_attribs[i++] = EGL_GREEN_SIZE;
   config_attribs[i++] = 1;
   config_attribs[i++] = EGL_BLUE_SIZE;
   config_attribs[i++] = 1;

   config_attribs[i++] = EGL_ALPHA_SIZE;
   if (_glut->display_mode & GLUT_ALPHA)
      config_attribs[i++] = 1;
   else
      config_attribs[i++] = 0;

   config_attribs[i++] = EGL_DEPTH_SIZE;
   if (_glut->display_mode & GLUT_DEPTH)
      config_attribs[i++] = 1;
   else
      config_attribs[i++] = 0;

   config_attribs[i++] = EGL_STENCIL_SIZE;
   if (_glut->display_mode & GLUT_STENCIL)
      config_attribs[i++] = 1;
   else
      config_attribs[i++] = 0;

   config_attribs[i++] = EGL_SURFACE_TYPE;
   config_attribs[i++] = _glut->surface_type;

   config_attribs[i++] = EGL_RENDERABLE_TYPE;
   renderable_type = 0x0;
   if (_glut->api_mask & GLUT_OPENGL_BIT)
      renderable_type |= EGL_OPENGL_BIT;
   if (_glut->api_mask & GLUT_OPENGL_ES1_BIT)
      renderable_type |= EGL_OPENGL_ES_BIT;
   if (_glut->api_mask & GLUT_OPENGL_ES2_BIT)
      renderable_type |= EGL_OPENGL_ES2_BIT;
   if (_glut->api_mask & GLUT_OPENVG_BIT)
      renderable_type |= EGL_OPENVG_BIT;
   config_attribs[i++] = renderable_type;

   config_attribs[i] = EGL_NONE;

   if (!eglChooseConfig(_glut->dpy,
            config_attribs, &config, 1, &num_configs) || !num_configs)
      _glutFatal("failed to choose a config");

   return config;
}

static struct glut_window *
_glutCreateWindow(const char *title, int x, int y, int w, int h)
{
   struct glut_window *win;
   EGLint context_attribs[4];
   EGLint api, i;

   win = calloc(1, sizeof(*win));
   if (!win)
      _glutFatal("failed to allocate window");

   win->config = _glutChooseConfig();

   i = 0;
   context_attribs[i] = EGL_NONE;

   /* multiple APIs? */

   api = EGL_OPENGL_ES_API;
   if (_glut->api_mask & GLUT_OPENGL_BIT) {
      api = EGL_OPENGL_API;
   }
   else if (_glut->api_mask & GLUT_OPENVG_BIT) {
      api = EGL_OPENVG_API;
   }
   else if (_glut->api_mask & GLUT_OPENGL_ES2_BIT) {
      context_attribs[i++] = EGL_CONTEXT_CLIENT_VERSION;
      context_attribs[i++] = 2;
   }

   context_attribs[i] = EGL_NONE;

   eglBindAPI(api);
   win->context = eglCreateContext(_glut->dpy,
         win->config, EGL_NO_CONTEXT, context_attribs);
   if (!win->context)
      _glutFatal("failed to create context");

   _glutNativeInitWindow(win, title, x, y, w, h);
   switch (_glut->surface_type) {
   case EGL_WINDOW_BIT:
      win->surface = eglCreateWindowSurface(_glut->dpy,
            win->config, win->native.u.window, NULL);
      break;
   case EGL_PIXMAP_BIT:
      win->surface = eglCreatePixmapSurface(_glut->dpy,
            win->config, win->native.u.pixmap, NULL);
      break;
   case EGL_PBUFFER_BIT:
   case EGL_SCREEN_BIT_MESA:
      win->surface = win->native.u.surface;
      break;
   default:
      break;
   }
   if (win->surface == EGL_NO_SURFACE)
      _glutFatal("failed to create surface");

   return win;
}

void
glutInitAPIMask(int mask)
{
   _glut->api_mask = mask;
}

void
glutInitDisplayMode(unsigned int mode)
{
   _glut->display_mode = mode;
}

void
glutInitWindowPosition(int x, int y)
{
}

void
glutInitWindowSize(int width, int height)
{
   _glut->window_width = width;
   _glut->window_height = height;
}

void
glutInit(int *argcp, char **argv)
{
   int i;

   for (i = 1; i < *argcp; i++) {
      if (strcmp(argv[i], "-display") == 0)
         _glut->display_name = argv[++i];
      else if (strcmp(argv[i], "-info") == 0) {
         _glut->verbose = 1;
      }
   }

   _glutNativeInitDisplay();
   _glut->dpy = eglGetDisplay(_glut->native_dpy);

   if (!eglInitialize(_glut->dpy, &_glut->major, &_glut->minor))
      _glutFatal("failed to initialize EGL display");

   _glut->init_time = _glutNow();

   printf("EGL_VERSION = %s\n", eglQueryString(_glut->dpy, EGL_VERSION));
   if (_glut->verbose) {
      printf("EGL_VENDOR = %s\n", eglQueryString(_glut->dpy, EGL_VENDOR));
      printf("EGL_EXTENSIONS = %s\n",
            eglQueryString(_glut->dpy, EGL_EXTENSIONS));
      printf("EGL_CLIENT_APIS = %s\n",
            eglQueryString(_glut->dpy, EGL_CLIENT_APIS));
   }
}

int
glutGet(int state)
{
   int val;

   switch (state) {
   case GLUT_ELAPSED_TIME:
      val = _glutNow() - _glut->init_time;
      break;
   default:
      val = -1;
      break;
   }

   return val;
}

void
glutIdleFunc(GLUT_EGLidleCB func)
{
   _glut->idle_cb = func;
}

void
glutPostRedisplay(void)
{
   _glut->redisplay = 1;
}

void
glutMainLoop(void)
{
   struct glut_window *win = _glut->current;

   if (!win)
      _glutFatal("no window is created\n");

   if (win->reshape_cb)
      win->reshape_cb(win->native.width, win->native.height);

   _glutNativeEventLoop();
}

static void
_glutFini(void)
{
   eglTerminate(_glut->dpy);
   _glutNativeFiniDisplay();
}

void
glutDestroyWindow(int win)
{
   struct glut_window *window = _glut->current;

   if (window->index != win)
      return;

   /* XXX it causes some bug in st/egl KMS backend */
   if ( _glut->surface_type != EGL_SCREEN_BIT_MESA)
      eglMakeCurrent(_glut->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);

   _glutDestroyWindow(_glut->current);
}

static void
_glutDefaultKeyboard(unsigned char key, int x, int y)
{
   if (key == 27) {
      if (_glut->current)
         glutDestroyWindow(_glut->current->index);
      _glutFini();

      exit(0);
   }
}

int
glutCreateWindow(const char *title)
{
   struct glut_window *win;

   win = _glutCreateWindow(title, 0, 0,
         _glut->window_width, _glut->window_height);

   win->index = _glut->num_windows++;
   win->reshape_cb = NULL;
   win->display_cb = NULL;
   win->keyboard_cb = _glutDefaultKeyboard;
   win->special_cb = NULL;

   if (!eglMakeCurrent(_glut->dpy, win->surface, win->surface, win->context))
      _glutFatal("failed to make window current");
   _glut->current = win;

   return win->index;
}

int
glutGetWindowWidth(void)
{
   struct glut_window *win = _glut->current;
   return win->native.width;
}

int
glutGetWindowHeight(void)
{
   struct glut_window *win = _glut->current;
   return win->native.height;
}

void
glutDisplayFunc(GLUT_EGLdisplayCB func)
{
   struct glut_window *win = _glut->current;
   win->display_cb = func;

}

void
glutReshapeFunc(GLUT_EGLreshapeCB func)
{
   struct glut_window *win = _glut->current;
   win->reshape_cb = func;
}

void
glutKeyboardFunc(GLUT_EGLkeyboardCB func)
{
   struct glut_window *win = _glut->current;
   win->keyboard_cb = func;
}

void
glutSpecialFunc(GLUT_EGLspecialCB func)
{
   struct glut_window *win = _glut->current;
   win->special_cb = func;
}

void
glutSwapBuffers(void)
{
}