File: eglport.cpp

package info (click to toggle)
sludge 2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,820 kB
  • sloc: cpp: 31,960; sh: 1,037; xml: 874; makefile: 672
file content (417 lines) | stat: -rw-r--r-- 12,372 bytes parent folder | download | duplicates (3)
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
/**
 *
 *  EGLPORT.C
 *  Copyright (C) 2011 Scott R. Smith
 *
 *  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.
 *
 */

#include "eglport.h"

#if defined(USE_EGL_SDL)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "SDL_syswm.h"
#include "SDL.h"
#endif

#if defined(PANDORA)
/* Pandora VSync */
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>

#ifndef FBIO_WAITFORVSYNC
#define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
#endif
int fbdev = -1;
#endif

bool VSync  = false;
int8_t FSAA = 0;

EGLDisplay          g_eglDisplay    = NULL;
EGLConfig           g_eglConfig     = NULL;
EGLContext          g_eglContext    = NULL;
EGLSurface          g_eglSurface    = NULL;
#if defined(USE_EGL_SDL)
Display*            g_Display       = NULL;
#else
NativeDisplayType   g_Display       = NULL;
#endif
NativeWindowType    g_Window        = 0;

#define g_totalConfigsIn 20
EGLint      g_totalConfigsFound = 0;
EGLConfig   g_allConfigs[g_totalConfigsIn];


/*======================================================
 * Close EGL resources
  ====================================================*/
void EGL_Close()
{
    if (g_eglDisplay != NULL)
    {
        peglMakeCurrent( g_eglDisplay, NULL, NULL, EGL_NO_CONTEXT );
        if (g_eglContext != NULL) {
            peglDestroyContext( g_eglDisplay, g_eglContext );
        }
        if (g_eglSurface != NULL) {
            peglDestroySurface( g_eglDisplay, g_eglSurface );
        }
        peglTerminate( g_eglDisplay );
    }

    g_eglSurface = NULL;
    g_eglContext = NULL;
    g_eglDisplay = NULL;

#if defined(USE_EGL_RAW)
	if (g_Window != NULL) {
		free(g_Window);
	}
    g_Window = NULL;
#elif defined(USE_EGL_SDL)
	if (g_Display != NULL) {
		XCloseDisplay(g_Display);
	}
    g_Display = NULL;
#else
    #error Incorrect EGL Configuration
#endif /* defined(USE_EGL_RAW) */

    CheckEGLErrors( __FILE__, __LINE__ );

    printf( "EGL Closed\n");

    Platform_Close();
}

/*===========================================================
Setup EGL context and surface
===========================================================*/
int8_t EGL_Init( void )
{
    int configIndex = 0;

    if (FindAppropriateEGLConfigs() != 0)
    {
        printf( "EGL ERROR: Unable to configure EGL. See previous error.\n" );
        return 1;
    }

	printf( "EGL Config %d\n", configIndex );

	if ( ConfigureEGL(g_allConfigs[configIndex]) != 0)
	{
		CheckEGLErrors( __FILE__, __LINE__ );
		printf( "EGL ERROR: Unable to configure EGL. See previous error.\n" );
		return 1;
	}

    return 0;
}

/*===========================================================
Swap EGL buffers and update the display
===========================================================*/
void EGL_SwapBuffers( void )
{
    if (VSync == true) {
        Platform_VSync();
    }
	peglSwapBuffers( g_eglDisplay, g_eglSurface );
}


/*========================================================
 *  Init base EGL
 * ======================================================*/
int8_t EGL_Open( void )
{
    EGLBoolean result;
    string output;

    // Setup any platform specific bits
    Platform_Open();

#if defined(USE_EGL_SDL)
    printf( "EGL Opening X11 display\n" );
    g_Display = XOpenDisplay(NULL);

    if (g_Display == NULL)
    {
        printf( "EGL ERROR: unable to get display!\n" );
        return 1;
    }
#endif /* defined(USE_EGL_SDL) */

    printf( "EGL Getting EGL display\n" );
    g_eglDisplay = peglGetDisplay( (NativeDisplayType)g_Display );

    if (g_eglDisplay == EGL_NO_DISPLAY)
    {
        CheckEGLErrors( __FILE__, __LINE__ );
        printf( "EGL ERROR: Unable to create EGL display.\n" );
        return 1;
    }

    printf( "EGL Initializing\n" );
    result = peglInitialize( g_eglDisplay, NULL, NULL );

    if (result != EGL_TRUE )
    {
        CheckEGLErrors( __FILE__, __LINE__ );
        printf( "EGL ERROR: Unable to initialize EGL display.\n" );
        return 1;
    }

    // Get EGL Library Information
    output = peglQueryString( g_eglDisplay, EGL_VENDOR );
    printf( "EGL_VENDOR: %s\n", output.c_str() );
    output = peglQueryString( g_eglDisplay, EGL_VERSION );
    printf( "EGL_VERSION: %s\n", output.c_str() );
    output = peglQueryString( g_eglDisplay, EGL_EXTENSIONS );
    printf( "EGL_EXTENSIONS: %s\n", output.c_str() );

    CheckEGLErrors( __FILE__, __LINE__ );

    return 0;
}

/*===========================================================
Initialise OpenGL settings
===========================================================*/
int8_t ConfigureEGL(EGLConfig config)
{
    EGLBoolean result;

#if defined(USE_GLES1)
    static const EGLint s_contextAttribs = 0;
#elif defined(USE_GLES2)
    static const EGLint s_contextAttribs[] =
    {
          EGL_CONTEXT_CLIENT_VERSION,     2,
          EGL_NONE
    };
#else
    #error Incorrect Opengl-ES Configuration for s_contextAttribs
#endif /* defined(USE_GLES1) */

    // Cleanup in case of a reset
    if (g_eglDisplay != NULL)
    {
        peglMakeCurrent( g_eglDisplay, NULL, NULL, EGL_NO_CONTEXT );
        if (g_eglContext != NULL) {
            peglDestroyContext( g_eglDisplay, g_eglContext );
        }
        if (g_eglSurface != NULL) {
            peglDestroySurface( g_eglDisplay, g_eglSurface );
        }
    }

#if defined(EGL_VERSION_1_2)
    // Bind GLES and create the context
    printf( "EGL Binding API\n" );
    peglBindAPI( EGL_OPENGL_ES_API );

	if ( CheckEGLErrors( __FILE__, __LINE__ ) !=  0 )
    {
        printf( "EGL ERROR: Could not bind EGL API.\n" );
		return 1;
	}
#endif /* defined(USE_EGL_SDL) */

    printf( "EGL Creating Context\n" );
    g_eglContext = peglCreateContext( g_eglDisplay, config, NULL, s_contextAttribs );

    if (g_eglContext == EGL_NO_CONTEXT)
    {
        CheckEGLErrors( __FILE__, __LINE__ );
        printf( "EGL ERROR: Unable to create GLES context!\n");
        return 1;
    }

#if defined(USE_EGL_RAW)
    if (g_Window == NULL) {
        g_Window = (NativeWindowType)malloc(16*1024);

        if(g_Window == NULL) {
            printf( "EGL ERROR: Memory for window Failed\n" );
            return 1;
        }
    }
    else
    {
        printf( "EGL Info: Memory for window already allocated\n" );
    }
#elif defined(USE_EGL_SDL)
    // Get the SDL window handle
    SDL_SysWMinfo sysInfo; //Will hold our Window information
    SDL_VERSION(&sysInfo.version); //Set SDL version

    if(SDL_GetWMInfo(&sysInfo) <= 0)
    {
        printf( "EGL ERROR: Unable to get SDL window handle: %s\n", SDL_GetError() );
        return 1;
    }
    g_Window = (NativeWindowType)sysInfo.info.x11.window;
#else
    #error Incorrect EGL Configuration for g_Window
#endif /* defined(USE_EGL_RAW) */

    printf( "EGL Creating window surface\n" );
    g_eglSurface = peglCreateWindowSurface( g_eglDisplay, config, g_Window, 0 );

    if (g_eglSurface == EGL_NO_SURFACE)
    {
        CheckEGLErrors( __FILE__, __LINE__ );
        printf( "EGL ERROR: Unable to create EGL surface!\n" );
        return 1;
    }

    printf( "EGL Making Current\n" );
    result = peglMakeCurrent( g_eglDisplay,  g_eglSurface,  g_eglSurface, g_eglContext );

    if (result != EGL_TRUE)
    {
        CheckEGLErrors( __FILE__, __LINE__ );
        printf( "EGL ERROR: Unable to make GLES context current\n" );
        return 1;
    }

    CheckEGLErrors( __FILE__, __LINE__ );
    printf( "EGL Complete\n" );
    return 0;
}

/*=======================================================
* Detect available video resolutions
=======================================================*/
int8_t FindAppropriateEGLConfigs( void )
{
    EGLBoolean result;
    int attrib = 0;
    EGLint ConfigAttribs[19];

    ConfigAttribs[attrib++] = EGL_RED_SIZE;
    ConfigAttribs[attrib++] = 4;
    ConfigAttribs[attrib++] = EGL_GREEN_SIZE;
    ConfigAttribs[attrib++] = 4;
    ConfigAttribs[attrib++] = EGL_BLUE_SIZE;
    ConfigAttribs[attrib++] = 4;
    ConfigAttribs[attrib++] = EGL_ALPHA_SIZE;
    ConfigAttribs[attrib++] = 4;
    ConfigAttribs[attrib++] = EGL_DEPTH_SIZE;
    ConfigAttribs[attrib++] = 16;
    ConfigAttribs[attrib++] = EGL_SURFACE_TYPE;
    ConfigAttribs[attrib++] = EGL_WINDOW_BIT;
#if defined(EGL_VERSION_1_2)
    ConfigAttribs[attrib++] = EGL_RENDERABLE_TYPE;
#if defined(USE_GLES1)
    ConfigAttribs[attrib++] = EGL_OPENGL_ES_BIT;
#elif defined(USE_GLES2)
    ConfigAttribs[attrib++] = EGL_OPENGL_ES2_BIT;
#endif /* defined(USE_GLES1) */
#endif /* defined(EGL_VERSION_1_2) */
    ConfigAttribs[attrib++] = EGL_SAMPLE_BUFFERS;
    ConfigAttribs[attrib++] = (FSAA > 0) ? 1 : 0;
    ConfigAttribs[attrib++] = EGL_SAMPLES;
    ConfigAttribs[attrib++] = FSAA;
    ConfigAttribs[attrib++] = EGL_NONE;

    result = peglChooseConfig( g_eglDisplay, ConfigAttribs, g_allConfigs, g_totalConfigsIn, &g_totalConfigsFound );
    if (result != EGL_TRUE || g_totalConfigsFound == 0)
    {
        CheckEGLErrors( __FILE__, __LINE__ );
        printf( "EGL ERROR: Unable to query for available configs, found %d.\n", g_totalConfigsFound );
        return 1;
    }
    printf( "EGL Found %d available configs\n", g_totalConfigsFound );

    return 0;
}

int8_t CheckEGLErrors( const string& file, uint16_t line )
{
    EGLenum error;
    string errortext;

    error = eglGetError();

    if (error != EGL_SUCCESS && error != 0)
    {
        switch (error)
        {
            case EGL_NOT_INITIALIZED:           errortext = "EGL_NOT_INITIALIZED"; break;
            case EGL_BAD_ACCESS:                errortext = "EGL_BAD_ACCESS"; break;
            case EGL_BAD_ALLOC:                 errortext = "EGL_BAD_ALLOC"; break;
            case EGL_BAD_ATTRIBUTE:             errortext = "EGL_BAD_ATTRIBUTE"; break;
            case EGL_BAD_CONTEXT:               errortext = "EGL_BAD_CONTEXT"; break;
            case EGL_BAD_CONFIG:                errortext = "EGL_BAD_CONFIG"; break;
            case EGL_BAD_CURRENT_SURFACE:       errortext = "EGL_BAD_CURRENT_SURFACE"; break;
            case EGL_BAD_DISPLAY:               errortext = "EGL_BAD_DISPLAY"; break;
            case EGL_BAD_SURFACE:               errortext = "EGL_BAD_SURFACE"; break;
            case EGL_BAD_MATCH:                 errortext = "EGL_BAD_MATCH"; break;
            case EGL_BAD_PARAMETER:             errortext = "EGL_BAD_PARAMETER"; break;
            case EGL_BAD_NATIVE_PIXMAP:         errortext = "EGL_BAD_NATIVE_PIXMAP"; break;
            case EGL_BAD_NATIVE_WINDOW:         errortext = "EGL_BAD_NATIVE_WINDOW"; break;
            default:                            errortext = "unknown"; break;
        }

        printf( "ERROR: EGL Error detected in file %s at line %d: %s (0x%X)\n", file.c_str(), line, errortext.c_str(), error );
        return 1;
    }

    return 0;
}

void Platform_Open( void )
{
#if defined(PANDORA)
    /* Pandora VSync */
    fbdev = open ("/dev/fb0", O_RDONLY /* O_RDWR */ );
    if ( fbdev < 0 ) {
      printf( "EGL Couldn't open /dev/fb0 for vsync\n" );
    }
#endif /* defined(PANDORA) */
}

void Platform_Close( void )
{
#if defined(PANDORA)
    /* Pandora VSync */
    close(fbdev);
    fbdev = -1;
#endif /* defined(PANDORA) */
}

void Platform_VSync( void )
{
#if defined(PANDORA)
    /* Pandora VSync */
    if ( fbdev >= 0 ) {
        int arg = 0;
        ioctl( fbdev, FBIO_WAITFORVSYNC, &arg );
    }
#endif /* defined(PANDORA) */
}