-
Notifications
You must be signed in to change notification settings - Fork 0
/
piglut.c
545 lines (462 loc) · 12.4 KB
/
piglut.c
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <alloca.h>
#include <termios.h>
#include <string.h>
#include "bcm_host.h"
#include <EGL/egl.h>
#include "piglut.h"
typedef struct
{
bool widthFromCmdLine;
bool heightFromCmdLine;
bool bppFromCmdLine;
unsigned int width;
unsigned int height;
unsigned int panelWidth;
unsigned int panelHeight;
unsigned int bpp;
/* callbacks */
displayCallback displayCb;
keyboardCallback keyboardCb;
initCallback initCb;
/* set by anything to terminate the main loop */
bool terminate;
/* EGL */
EGLDisplay display;
EGLSurface surface;
EGLContext context;
/* dispmax stuff */
EGL_DISPMANX_WINDOW_T nativeWindow;
DISPMANX_ELEMENT_HANDLE_T dispmanElement;
DISPMANX_DISPLAY_HANDLE_T dispmanDisplay;
DISPMANX_UPDATE_HANDLE_T dispmanUpdate;
/* keyboard input */
struct termios oldTerminalConfig;
int peekCharacter;
/* user data */
void * userData;
} piglut_t;
#define MAX(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#define MIN(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
#define MAX_WIDTH 1920
#define MAX_HEIGHT 1080
void * piglutInit(int argc, char **argv)
{
piglut_t * p = (piglut_t *)malloc(sizeof(piglut_t));
if (p)
{
memset(p, 0, sizeof(piglut_t));
/* TODO : command line parsing */
}
/* NULL on error */
return (void *)p;
}
void piglutTerm(void *pg)
{
piglut_t * p = (piglut_t *)pg;
if (p)
{
/* TODO : find out what's required to terminate */
p->dispmanUpdate = vc_dispmanx_update_start(0);
vc_dispmanx_element_remove(p->dispmanDisplay, p->dispmanElement);
vc_dispmanx_update_submit_sync(p->dispmanUpdate);
vc_dispmanx_display_close(p->dispmanDisplay);
bcm_host_deinit();
/* makes sure that if anyone kept a reference, it's gone */
memset(p, 0, sizeof(piglut_t));
free(p);
}
}
/* more extensible, allows adding of additional parameters to the
API without having to change all your software */
void piglutInitWindowConfig(piglutWindowConfig_t * wc)
{
if (wc)
{
wc->width = 0xFFFFFFFFU;
wc->height = 0xFFFFFFFFU;
wc->bpp = 32;
}
}
/* allows you to override options not specified at the command line */
int piglutInitWindowSize(void *pg,
piglutWindowConfig_t * wc)
{
piglut_t * p = (piglut_t *)pg;
unsigned int width = wc->width;
unsigned int height = wc->height;
unsigned int bpp = wc->bpp;
if (p)
{
/* verify the new values prior to saving them */
if (!p->widthFromCmdLine)
width = MIN(width, MAX_WIDTH);
if (!p->heightFromCmdLine)
height = MIN(height, MAX_HEIGHT);
if ((!p->bppFromCmdLine) && (bpp == 32))
p->bpp = 32;
else if ((!p->bppFromCmdLine) && (bpp == 24))
p->bpp = 24;
else if ((!p->bppFromCmdLine) && (bpp == 16))
p->bpp = 16;
else
{
errno = EINVAL;
return -1;
}
p->width = width;
p->height = height;
return 0;
}
else
{
errno = EINVAL;
return -1;
}
}
int piglutDisplayFunc(void *pg,
displayCallback display)
{
piglut_t * p = (piglut_t *)pg;
if (p)
{
p->displayCb = display;
return 0;
}
else
{
errno = EINVAL;
return -1;
}
}
int piglutKeyboardFunc(void *pg,
keyboardCallback keyboard)
{
piglut_t * p = (piglut_t *)pg;
if (p)
{
p->keyboardCb = keyboard;
return 0;
}
else
{
errno = EINVAL;
return -1;
}
}
int piglutInitFunc(void *pg,
initCallback init)
{
piglut_t * p = (piglut_t *)pg;
if (p)
{
p->initCb = init;
return 0;
}
else
{
errno = EINVAL;
return -1;
}
}
static void populateConfig(EGLint *p,
unsigned int bpp,
unsigned int depthSize,
unsigned int stencilSize,
unsigned int multisample)
{
int i = 0;
p[i++] = EGL_RED_SIZE;
if (bpp == 16)
p[i++] = 5;
else if (bpp > 16)
p[i++] = 8;
else
p[i++] = 0;
p[i++] = EGL_GREEN_SIZE;
if (bpp == 16)
p[i++] = 6;
else if (bpp > 16)
p[i++] = 8;
else
p[i++] = 0;
p[i++] = EGL_BLUE_SIZE;
if (bpp == 16)
p[i++] = 5;
else if (bpp > 16)
p[i++] = 8;
else
p[i++] = 0;
p[i++] = EGL_ALPHA_SIZE;
if (bpp == 16)
p[i++] = 0;
else if (bpp == 24)
p[i++] = 0;
else if (bpp == 32)
p[i++] = 8;
else
p[i++] = 0;
p[i++] = EGL_DEPTH_SIZE;
p[i++] = depthSize;
p[i++] = EGL_STENCIL_SIZE;
p[i++] = stencilSize;
if (multisample)
{
p[i++] = EGL_SAMPLE_BUFFERS;
p[i++] = 1;
p[i++] = EGL_SAMPLES;
p[i++] = 4;
}
p[i++] = EGL_SURFACE_TYPE;
p[i++] = EGL_WINDOW_BIT;
p[i++] = EGL_RENDERABLE_TYPE;
p[i++] = EGL_OPENGL_ES2_BIT;
p[i++] = EGL_NONE;
}
static int kbhit(piglut_t * p)
{
unsigned char ch;
int nRead;
struct termios term;
/* character pending */
if (p->peekCharacter != -1)
return 1;
tcgetattr(STDIN_FILENO, &term);
term.c_cc[VMIN] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &term);
nRead = read(STDIN_FILENO, &ch, 1);
term.c_cc[VMIN] = 1;
tcsetattr(STDIN_FILENO, TCSANOW, &term);
if (nRead == 1)
{
p->peekCharacter = ch;
return 1;
}
return 0;
}
static int readch(piglut_t * p)
{
char ch;
/* already had a char from earlier, just return it */
if (p->peekCharacter != -1)
{
ch = p->peekCharacter;
p->peekCharacter = -1;
return ch;
}
/* nothing pending, just read */
read(0, &ch, 1);
return ch;
}
int piglutMainLoop(void *pg)
{
piglut_t * p = (piglut_t *)pg;
if (p)
{
VC_RECT_T dstRect;
VC_RECT_T srcRect;
VC_DISPMANX_ALPHA_T layerAlpha;
EGLint configAttributes[32];
EGLint numberConfigs, selectedConfig;
EGLConfig * eglConfigs;
int i;
struct termios newTerminalConfig;
static const EGLint contextAttributes[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
bcm_host_init();
/* setup dispmax */
if (graphics_get_display_size(0 /* LCD */, &p->panelWidth, &p->panelHeight))
{
errno = ECONNREFUSED;
return -1;
}
/* reclamp the state width and height to that of the display */
p->width = MIN(p->width, p->panelWidth);
p->height = MIN(p->height, p->panelHeight);
dstRect.x = 0;
dstRect.y = 0;
dstRect.width = p->panelWidth;
dstRect.height = p->panelHeight;
srcRect.x = 0;
srcRect.y = 0;
srcRect.width = p->width << 16;
srcRect.height = p->height << 16;
/* this is nothing to do with the EGL window having alpha, but how its
blended to the console underneath */
layerAlpha.flags = DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS;
layerAlpha.opacity = 255;
layerAlpha.mask = 0;
p->dispmanDisplay = vc_dispmanx_display_open(0 /* LCD */);
/* this pairs with the vc_dispmanx_update_submit_sync() below,
which applies the changes inbetween */
p->dispmanUpdate = vc_dispmanx_update_start(0);
p->dispmanElement = vc_dispmanx_element_add(p->dispmanUpdate,
p->dispmanDisplay,
0/*layer*/,
&dstRect,
0/*src*/,
&srcRect,
DISPMANX_PROTECTION_NONE,
&layerAlpha,
0/*clamp*/,
0/*transform*/);
p->nativeWindow.element = p->dispmanElement;
p->nativeWindow.width = p->width;
p->nativeWindow.height = p->height;
vc_dispmanx_update_submit_sync(p->dispmanUpdate);
/* TODO : Add cleanup if failure */
/* create an EGL context */
p->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (p->display == EGL_NO_DISPLAY)
{
errno = ECONNREFUSED;
return -1;
}
/* initialize the EGL display connection */
if (eglInitialize(p->display, NULL, NULL) == EGL_FALSE)
{
errno = ECONNREFUSED;
return -1;
}
/* TODO : add depth stencil config to the API */
populateConfig(configAttributes, p->bpp, 15, 1, false);
if (!eglGetConfigs(p->display, NULL, 0, &numberConfigs))
{
errno = ECONNREFUSED;
return -1;
}
eglConfigs = (EGLConfig *)alloca(numberConfigs * sizeof(EGLConfig));
if (!eglChooseConfig(p->display, configAttributes, eglConfigs, numberConfigs, &numberConfigs) || (numberConfigs == 0))
{
errno = ECONNREFUSED;
return -1;
}
for (i = 0; i < numberConfigs; i++)
{
EGLint redSize, greenSize, blueSize, alphaSize, depthSize;
eglGetConfigAttrib(p->display, eglConfigs[i], EGL_RED_SIZE, &redSize);
eglGetConfigAttrib(p->display, eglConfigs[i], EGL_GREEN_SIZE, &greenSize);
eglGetConfigAttrib(p->display, eglConfigs[i], EGL_BLUE_SIZE, &blueSize);
eglGetConfigAttrib(p->display, eglConfigs[i], EGL_ALPHA_SIZE, &alphaSize);
eglGetConfigAttrib(p->display, eglConfigs[i], EGL_DEPTH_SIZE, &depthSize);
if (p->bpp == (redSize + greenSize + blueSize + alphaSize))
break;
}
selectedConfig = i;
/* get an appropriate EGL frame buffer configuration */
if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE)
{
errno = ECONNREFUSED;
return -1;
}
/* create an EGL rendering context */
p->context = eglCreateContext(p->display, eglConfigs[selectedConfig], EGL_NO_CONTEXT, contextAttributes);
if (p->context == EGL_NO_CONTEXT)
{
errno = ECONNREFUSED;
return -1;
}
p->surface = eglCreateWindowSurface(p->display, eglConfigs[selectedConfig], &p->nativeWindow, NULL);
if (p->surface == EGL_NO_SURFACE)
{
errno = ECONNREFUSED;
return -1;
}
/* connect the context to the surface */
if (eglMakeCurrent(p->display, p->surface, p->surface, p->context) == EGL_FALSE)
{
errno = ECONNREFUSED;
return -1;
}
/* this is called when GL is up, so suits texture loading, one time init, etc */
if (p->initCb)
p->initCb(pg);
/* set the initial condition for kbhit & readch */
p->peekCharacter = -1;
tcgetattr(STDIN_FILENO, &p->oldTerminalConfig);
newTerminalConfig = p->oldTerminalConfig;
/* remove buffering and echo */
newTerminalConfig.c_lflag &= ~(ICANON | ECHO | ISIG);
newTerminalConfig.c_cc[VMIN] = 1;
newTerminalConfig.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &newTerminalConfig);
while (!p->terminate)
{
if (p->keyboardCb)
{
if (kbhit(p))
{
do
{
/* returning true from the keyboard function will quit */
p->terminate = p->keyboardCb(pg, readch(p));
} while (kbhit(p));
}
}
if (p->displayCb)
p->displayCb(pg);
}
/* return the keyboard to default handler state */
tcsetattr(STDIN_FILENO, TCSANOW, &p->oldTerminalConfig);
return 0;
}
else
{
errno = EINVAL;
return -1;
}
}
int piglutSetUserData(void *pg, void * userData)
{
piglut_t * p = (piglut_t *)pg;
if (p)
{
p->userData = userData;
return 0;
}
else
{
errno = EINVAL;
return -1;
}
}
void * piglutGetUserData(void *pg)
{
piglut_t * p = (piglut_t *)pg;
if (p)
return p->userData;
else
{
errno = EINVAL;
return 0;
}
}
int piglutGetDisplayConfig(void *pg,
piglutDisplayConfig_t * dc)
{
piglut_t * p = (piglut_t *)pg;
if (p && dc)
{
dc->width = p->width;
dc->height = p->height;
dc->panelWidth = p->panelWidth;
dc->panelHeight = p->panelHeight;
return 0;
}
else
{
errno = EINVAL;
return -1;
}
}