src/video/pandora/SDL_pandora.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 20 Jul 2016 21:00:36 +0200
changeset 10207 e66f6ab2b059
parent 10205 baabe483bb99
child 10208 49c460371a4a
permissions -rw-r--r--
Pandora: Fixed compile warnings in video implementation (thanks, Bombo!).

Fixes Bugzilla #2734.
slouken@3161
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@3161
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@3161
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@3161
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@3161
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@6044
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_PANDORA
slouken@3161
    24
slouken@3161
    25
/* SDL internals */
slouken@3161
    26
#include "../SDL_sysvideo.h"
slouken@3161
    27
#include "SDL_version.h"
slouken@3161
    28
#include "SDL_syswm.h"
slouken@3161
    29
#include "SDL_loadso.h"
slouken@3161
    30
#include "SDL_events.h"
slouken@3161
    31
#include "../../events/SDL_mouse_c.h"
slouken@3161
    32
#include "../../events/SDL_keyboard_c.h"
slouken@3161
    33
slouken@3161
    34
/* PND declarations */
slouken@3161
    35
#include "SDL_pandora.h"
slouken@3161
    36
#include "SDL_pandora_events.h"
slouken@3161
    37
slouken@3438
    38
/* WIZ declarations */
slouken@3438
    39
#include "GLES/gl.h"
slouken@3438
    40
#ifdef WIZ_GLES_LITE
slouken@7191
    41
static NativeWindowType hNativeWnd = 0; /* A handle to the window we will create. */
slouken@3438
    42
#endif
slouken@3438
    43
slouken@3161
    44
static int
slouken@3161
    45
PND_available(void)
slouken@3161
    46
{
slouken@3161
    47
    return 1;
slouken@3161
    48
}
slouken@3161
    49
slouken@3161
    50
static void
slouken@3161
    51
PND_destroy(SDL_VideoDevice * device)
slouken@3161
    52
{
slouken@3161
    53
    if (device->driverdata != NULL) {
slouken@3161
    54
        device->driverdata = NULL;
slouken@3161
    55
    }
slouken@3161
    56
}
slouken@3161
    57
slouken@3161
    58
static SDL_VideoDevice *
slouken@3161
    59
PND_create()
slouken@3161
    60
{
slouken@3161
    61
    SDL_VideoDevice *device;
slouken@3161
    62
    SDL_VideoData *phdata;
slouken@3161
    63
    int status;
slouken@3161
    64
slouken@3161
    65
    /* Check if pandora could be initialized */
slouken@3161
    66
    status = PND_available();
slouken@3161
    67
    if (status == 0) {
slouken@3161
    68
        /* PND could not be used */
slouken@3161
    69
        return NULL;
slouken@3161
    70
    }
slouken@3161
    71
slouken@3161
    72
    /* Initialize SDL_VideoDevice structure */
slouken@3161
    73
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
slouken@3161
    74
    if (device == NULL) {
slouken@3161
    75
        SDL_OutOfMemory();
slouken@3161
    76
        return NULL;
slouken@3161
    77
    }
slouken@3161
    78
slouken@3161
    79
    /* Initialize internal Pandora specific data */
slouken@3161
    80
    phdata = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
slouken@3161
    81
    if (phdata == NULL) {
slouken@3161
    82
        SDL_OutOfMemory();
slouken@3161
    83
        SDL_free(device);
slouken@3161
    84
        return NULL;
slouken@3161
    85
    }
slouken@3161
    86
slouken@3161
    87
    device->driverdata = phdata;
slouken@3161
    88
slouken@3161
    89
    phdata->egl_initialized = SDL_TRUE;
slouken@3161
    90
slouken@3161
    91
philipp@10202
    92
    /* Setup amount of available displays */
slouken@3238
    93
    device->num_displays = 0;
slouken@3161
    94
slouken@3161
    95
    /* Set device free function */
slouken@3161
    96
    device->free = PND_destroy;
slouken@3161
    97
slouken@3161
    98
    /* Setup all functions which we can handle */
slouken@3161
    99
    device->VideoInit = PND_videoinit;
slouken@3161
   100
    device->VideoQuit = PND_videoquit;
slouken@3161
   101
    device->GetDisplayModes = PND_getdisplaymodes;
slouken@3161
   102
    device->SetDisplayMode = PND_setdisplaymode;
slouken@3161
   103
    device->CreateWindow = PND_createwindow;
slouken@3161
   104
    device->CreateWindowFrom = PND_createwindowfrom;
slouken@3161
   105
    device->SetWindowTitle = PND_setwindowtitle;
slouken@3161
   106
    device->SetWindowIcon = PND_setwindowicon;
slouken@3161
   107
    device->SetWindowPosition = PND_setwindowposition;
slouken@3161
   108
    device->SetWindowSize = PND_setwindowsize;
slouken@3161
   109
    device->ShowWindow = PND_showwindow;
slouken@3161
   110
    device->HideWindow = PND_hidewindow;
slouken@3161
   111
    device->RaiseWindow = PND_raisewindow;
slouken@3161
   112
    device->MaximizeWindow = PND_maximizewindow;
slouken@3161
   113
    device->MinimizeWindow = PND_minimizewindow;
slouken@3161
   114
    device->RestoreWindow = PND_restorewindow;
slouken@3161
   115
    device->SetWindowGrab = PND_setwindowgrab;
slouken@3161
   116
    device->DestroyWindow = PND_destroywindow;
slouken@3161
   117
    device->GetWindowWMInfo = PND_getwindowwminfo;
slouken@3161
   118
    device->GL_LoadLibrary = PND_gl_loadlibrary;
slouken@3161
   119
    device->GL_GetProcAddress = PND_gl_getprocaddres;
slouken@3161
   120
    device->GL_UnloadLibrary = PND_gl_unloadlibrary;
slouken@3161
   121
    device->GL_CreateContext = PND_gl_createcontext;
slouken@3161
   122
    device->GL_MakeCurrent = PND_gl_makecurrent;
slouken@3161
   123
    device->GL_SetSwapInterval = PND_gl_setswapinterval;
slouken@3161
   124
    device->GL_GetSwapInterval = PND_gl_getswapinterval;
slouken@3161
   125
    device->GL_SwapWindow = PND_gl_swapwindow;
slouken@3161
   126
    device->GL_DeleteContext = PND_gl_deletecontext;
slouken@3161
   127
    device->PumpEvents = PND_PumpEvents;
slouken@3161
   128
icculus@6422
   129
    /* !!! FIXME: implement SetWindowBordered */
icculus@6422
   130
slouken@3161
   131
    return device;
slouken@3161
   132
}
slouken@3161
   133
slouken@3161
   134
VideoBootStrap PND_bootstrap = {
slouken@3438
   135
#ifdef WIZ_GLES_LITE
slouken@3438
   136
    "wiz",
slouken@3438
   137
    "SDL Wiz Video Driver",
slouken@3438
   138
#else
slouken@3161
   139
    "pandora",
slouken@3161
   140
    "SDL Pandora Video Driver",
slouken@3438
   141
#endif
slouken@3161
   142
    PND_available,
slouken@3161
   143
    PND_create
slouken@3161
   144
};
slouken@3161
   145
slouken@3161
   146
/*****************************************************************************/
slouken@3161
   147
/* SDL Video and Display initialization/handling functions                   */
slouken@3161
   148
/*****************************************************************************/
slouken@3161
   149
int
slouken@3161
   150
PND_videoinit(_THIS)
slouken@3161
   151
{
slouken@3161
   152
    SDL_VideoDisplay display;
slouken@3161
   153
    SDL_DisplayMode current_mode;
slouken@3161
   154
slouken@3161
   155
    SDL_zero(current_mode);
slouken@3438
   156
#ifdef WIZ_GLES_LITE
slouken@3438
   157
    current_mode.w = 320;
slouken@3438
   158
    current_mode.h = 240;
slouken@3438
   159
#else
slouken@3161
   160
    current_mode.w = 800;
slouken@3161
   161
    current_mode.h = 480;
slouken@3438
   162
#endif
slouken@3161
   163
    current_mode.refresh_rate = 60;
slouken@3161
   164
    current_mode.format = SDL_PIXELFORMAT_RGB565;
slouken@3161
   165
    current_mode.driverdata = NULL;
slouken@3161
   166
slouken@3161
   167
    SDL_zero(display);
slouken@3161
   168
    display.desktop_mode = current_mode;
slouken@3161
   169
    display.current_mode = current_mode;
slouken@3161
   170
    display.driverdata = NULL;
slouken@3161
   171
slouken@3161
   172
    SDL_AddVideoDisplay(&display);
slouken@3161
   173
slouken@3161
   174
    return 1;
slouken@3161
   175
}
slouken@3161
   176
slouken@3161
   177
void
slouken@3161
   178
PND_videoquit(_THIS)
slouken@3161
   179
{
slouken@3161
   180
slouken@3161
   181
}
slouken@3161
   182
slouken@3161
   183
void
slouken@3500
   184
PND_getdisplaymodes(_THIS, SDL_VideoDisplay * display)
slouken@3161
   185
{
slouken@3161
   186
slouken@3161
   187
}
slouken@3161
   188
slouken@3161
   189
int
slouken@3500
   190
PND_setdisplaymode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
slouken@3161
   191
{
slouken@3161
   192
    return 0;
slouken@3161
   193
}
slouken@3161
   194
slouken@3161
   195
int
slouken@3161
   196
PND_createwindow(_THIS, SDL_Window * window)
slouken@3161
   197
{
slouken@3161
   198
    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
slouken@3161
   199
slouken@3161
   200
    SDL_WindowData *wdata;
slouken@3161
   201
slouken@3161
   202
    /* Allocate window internal data */
slouken@3161
   203
    wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
slouken@3161
   204
    if (wdata == NULL) {
icculus@7037
   205
        return SDL_OutOfMemory();
slouken@3161
   206
    }
slouken@3161
   207
slouken@3161
   208
    /* Setup driver data for this window */
slouken@3161
   209
    window->driverdata = wdata;
slouken@3161
   210
slouken@3161
   211
    /* Check if window must support OpenGL ES rendering */
slouken@3161
   212
    if ((window->flags & SDL_WINDOW_OPENGL) == SDL_WINDOW_OPENGL) {
slouken@3161
   213
slouken@3161
   214
        EGLBoolean initstatus;
slouken@3161
   215
slouken@3161
   216
        /* Mark this window as OpenGL ES compatible */
slouken@3161
   217
        wdata->uses_gles = SDL_TRUE;
slouken@3161
   218
slouken@3161
   219
        /* Create connection to OpenGL ES */
slouken@3161
   220
        if (phdata->egl_display == EGL_NO_DISPLAY) {
slouken@3161
   221
            phdata->egl_display = eglGetDisplay((NativeDisplayType) 0);
slouken@3161
   222
            if (phdata->egl_display == EGL_NO_DISPLAY) {
icculus@7037
   223
                return SDL_SetError("PND: Can't get connection to OpenGL ES");
slouken@3161
   224
            }
slouken@3161
   225
slouken@3161
   226
            initstatus = eglInitialize(phdata->egl_display, NULL, NULL);
slouken@3161
   227
            if (initstatus != EGL_TRUE) {
icculus@7037
   228
                return SDL_SetError("PND: Can't init OpenGL ES library");
slouken@3161
   229
            }
slouken@3161
   230
        }
slouken@3161
   231
slouken@3161
   232
        phdata->egl_refcount++;
slouken@3161
   233
    }
slouken@3161
   234
slouken@3161
   235
    /* Window has been successfully created */
slouken@3161
   236
    return 0;
slouken@3161
   237
}
slouken@3161
   238
slouken@3161
   239
int
slouken@3161
   240
PND_createwindowfrom(_THIS, SDL_Window * window, const void *data)
slouken@3161
   241
{
slouken@3161
   242
    return -1;
slouken@3161
   243
}
slouken@3161
   244
slouken@3161
   245
void
slouken@3161
   246
PND_setwindowtitle(_THIS, SDL_Window * window)
slouken@3161
   247
{
slouken@3161
   248
}
slouken@3161
   249
void
slouken@3161
   250
PND_setwindowicon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@3161
   251
{
slouken@3161
   252
}
slouken@3161
   253
void
slouken@3161
   254
PND_setwindowposition(_THIS, SDL_Window * window)
slouken@3161
   255
{
slouken@3161
   256
}
slouken@3161
   257
void
slouken@3161
   258
PND_setwindowsize(_THIS, SDL_Window * window)
slouken@3161
   259
{
slouken@3161
   260
}
slouken@3161
   261
void
slouken@3161
   262
PND_showwindow(_THIS, SDL_Window * window)
slouken@3161
   263
{
slouken@3161
   264
}
slouken@3161
   265
void
slouken@3161
   266
PND_hidewindow(_THIS, SDL_Window * window)
slouken@3161
   267
{
slouken@3161
   268
}
slouken@3161
   269
void
slouken@3161
   270
PND_raisewindow(_THIS, SDL_Window * window)
slouken@3161
   271
{
slouken@3161
   272
}
slouken@3161
   273
void
slouken@3161
   274
PND_maximizewindow(_THIS, SDL_Window * window)
slouken@3161
   275
{
slouken@3161
   276
}
slouken@3161
   277
void
slouken@3161
   278
PND_minimizewindow(_THIS, SDL_Window * window)
slouken@3161
   279
{
slouken@3161
   280
}
slouken@3161
   281
void
slouken@3161
   282
PND_restorewindow(_THIS, SDL_Window * window)
slouken@3161
   283
{
slouken@3161
   284
}
slouken@3161
   285
void
philipp@10207
   286
PND_setwindowgrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@3161
   287
{
slouken@3161
   288
}
slouken@3161
   289
void
slouken@3161
   290
PND_destroywindow(_THIS, SDL_Window * window)
slouken@3161
   291
{
slouken@3235
   292
    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
slouken@3235
   293
    eglTerminate(phdata->egl_display);
slouken@3161
   294
}
slouken@3161
   295
slouken@3161
   296
/*****************************************************************************/
slouken@3161
   297
/* SDL Window Manager function                                               */
slouken@3161
   298
/*****************************************************************************/
slouken@3161
   299
SDL_bool
slouken@3161
   300
PND_getwindowwminfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info)
slouken@3161
   301
{
slouken@3161
   302
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@3161
   303
        return SDL_TRUE;
slouken@3161
   304
    } else {
slouken@3161
   305
        SDL_SetError("application not compiled with SDL %d.%d\n",
slouken@3161
   306
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@3161
   307
        return SDL_FALSE;
slouken@3161
   308
    }
slouken@3161
   309
slouken@3161
   310
    /* Failed to get window manager information */
slouken@3161
   311
    return SDL_FALSE;
slouken@3161
   312
}
slouken@3161
   313
slouken@3161
   314
/*****************************************************************************/
slouken@3161
   315
/* SDL OpenGL/OpenGL ES functions                                            */
slouken@3161
   316
/*****************************************************************************/
slouken@3161
   317
int
slouken@3161
   318
PND_gl_loadlibrary(_THIS, const char *path)
slouken@3161
   319
{
slouken@3161
   320
    /* Check if OpenGL ES library is specified for GF driver */
slouken@3161
   321
    if (path == NULL) {
slouken@3161
   322
        path = SDL_getenv("SDL_OPENGL_LIBRARY");
slouken@3161
   323
        if (path == NULL) {
slouken@3161
   324
            path = SDL_getenv("SDL_OPENGLES_LIBRARY");
slouken@3161
   325
        }
slouken@3161
   326
    }
slouken@3161
   327
slouken@3161
   328
    /* Check if default library loading requested */
slouken@3161
   329
    if (path == NULL) {
slouken@3161
   330
        /* Already linked with GF library which provides egl* subset of  */
slouken@3161
   331
        /* functions, use Common profile of OpenGL ES library by default */
slouken@3438
   332
#ifdef WIZ_GLES_LITE
slouken@7191
   333
    path = "/lib/libopengles_lite.so";
slouken@3438
   334
#else
slouken@3161
   335
        path = "/usr/lib/libGLES_CM.so";
slouken@3438
   336
#endif
slouken@3161
   337
    }
slouken@3161
   338
slouken@3161
   339
    /* Load dynamic library */
slouken@3161
   340
    _this->gl_config.dll_handle = SDL_LoadObject(path);
slouken@3161
   341
    if (!_this->gl_config.dll_handle) {
slouken@3161
   342
        /* Failed to load new GL ES library */
icculus@7037
   343
        return SDL_SetError("PND: Failed to locate OpenGL ES library");
slouken@3161
   344
    }
slouken@3161
   345
slouken@3161
   346
    /* Store OpenGL ES library path and name */
slouken@3161
   347
    SDL_strlcpy(_this->gl_config.driver_path, path,
slouken@3161
   348
                SDL_arraysize(_this->gl_config.driver_path));
slouken@3161
   349
slouken@3161
   350
    /* New OpenGL ES library is loaded */
slouken@3161
   351
    return 0;
slouken@3161
   352
}
slouken@3161
   353
slouken@3161
   354
void *
slouken@3161
   355
PND_gl_getprocaddres(_THIS, const char *proc)
slouken@3161
   356
{
slouken@3161
   357
    void *function_address;
slouken@3161
   358
slouken@3161
   359
    /* Try to get function address through the egl interface */
slouken@3161
   360
    function_address = eglGetProcAddress(proc);
slouken@3161
   361
    if (function_address != NULL) {
slouken@3161
   362
        return function_address;
slouken@3161
   363
    }
slouken@3161
   364
slouken@3161
   365
    /* Then try to get function in the OpenGL ES library */
slouken@3161
   366
    if (_this->gl_config.dll_handle) {
slouken@3161
   367
        function_address =
slouken@3161
   368
            SDL_LoadFunction(_this->gl_config.dll_handle, proc);
slouken@3161
   369
        if (function_address != NULL) {
slouken@3161
   370
            return function_address;
slouken@3161
   371
        }
slouken@3161
   372
    }
slouken@3161
   373
slouken@3161
   374
    /* Failed to get GL ES function address pointer */
slouken@3161
   375
    SDL_SetError("PND: Cannot locate OpenGL ES function name");
slouken@3161
   376
    return NULL;
slouken@3161
   377
}
slouken@3161
   378
slouken@3161
   379
void
slouken@3161
   380
PND_gl_unloadlibrary(_THIS)
slouken@3161
   381
{
slouken@3161
   382
    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
slouken@3161
   383
slouken@3161
   384
    if (phdata->egl_initialized == SDL_TRUE) {
slouken@3161
   385
        /* Unload OpenGL ES library */
slouken@3161
   386
        if (_this->gl_config.dll_handle) {
slouken@3161
   387
            SDL_UnloadObject(_this->gl_config.dll_handle);
slouken@3161
   388
            _this->gl_config.dll_handle = NULL;
slouken@3161
   389
        }
slouken@3161
   390
    } else {
slouken@3161
   391
        SDL_SetError("PND: GF initialization failed, no OpenGL ES support");
slouken@3161
   392
    }
slouken@3161
   393
}
slouken@3161
   394
slouken@3161
   395
SDL_GLContext
slouken@3161
   396
PND_gl_createcontext(_THIS, SDL_Window * window)
slouken@3161
   397
{
slouken@3161
   398
    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
slouken@3161
   399
    SDL_WindowData *wdata = (SDL_WindowData *) window->driverdata;
slouken@3161
   400
    EGLBoolean status;
slouken@3161
   401
    EGLint configs;
slouken@3161
   402
    uint32_t attr_pos;
slouken@3161
   403
    EGLint attr_value;
slouken@3161
   404
    EGLint cit;
slouken@3161
   405
slouken@3161
   406
    /* Check if EGL was initialized */
slouken@3161
   407
    if (phdata->egl_initialized != SDL_TRUE) {
slouken@3161
   408
        SDL_SetError("PND: EGL initialization failed, no OpenGL ES support");
slouken@3161
   409
        return NULL;
slouken@3161
   410
    }
slouken@3161
   411
slouken@3161
   412
    /* Prepare attributes list to pass them to OpenGL ES */
slouken@3161
   413
    attr_pos = 0;
slouken@3161
   414
    wdata->gles_attributes[attr_pos++] = EGL_SURFACE_TYPE;
slouken@3161
   415
    wdata->gles_attributes[attr_pos++] = EGL_WINDOW_BIT;
slouken@3161
   416
    wdata->gles_attributes[attr_pos++] = EGL_RED_SIZE;
slouken@3161
   417
    wdata->gles_attributes[attr_pos++] = _this->gl_config.red_size;
slouken@3161
   418
    wdata->gles_attributes[attr_pos++] = EGL_GREEN_SIZE;
slouken@3161
   419
    wdata->gles_attributes[attr_pos++] = _this->gl_config.green_size;
slouken@3161
   420
    wdata->gles_attributes[attr_pos++] = EGL_BLUE_SIZE;
slouken@3161
   421
    wdata->gles_attributes[attr_pos++] = _this->gl_config.blue_size;
slouken@3161
   422
    wdata->gles_attributes[attr_pos++] = EGL_ALPHA_SIZE;
slouken@3161
   423
slouken@3161
   424
    /* Setup alpha size in bits */
slouken@3161
   425
    if (_this->gl_config.alpha_size) {
slouken@3161
   426
        wdata->gles_attributes[attr_pos++] = _this->gl_config.alpha_size;
slouken@3161
   427
    } else {
slouken@3161
   428
        wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   429
    }
slouken@3161
   430
slouken@3161
   431
    /* Setup color buffer size */
slouken@3161
   432
    if (_this->gl_config.buffer_size) {
slouken@3161
   433
        wdata->gles_attributes[attr_pos++] = EGL_BUFFER_SIZE;
slouken@3161
   434
        wdata->gles_attributes[attr_pos++] = _this->gl_config.buffer_size;
slouken@3161
   435
    } else {
slouken@3161
   436
        wdata->gles_attributes[attr_pos++] = EGL_BUFFER_SIZE;
slouken@3161
   437
        wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   438
    }
slouken@3161
   439
slouken@3161
   440
    /* Setup depth buffer bits */
slouken@3161
   441
    wdata->gles_attributes[attr_pos++] = EGL_DEPTH_SIZE;
slouken@3161
   442
    wdata->gles_attributes[attr_pos++] = _this->gl_config.depth_size;
slouken@3161
   443
slouken@3161
   444
    /* Setup stencil bits */
slouken@3161
   445
    if (_this->gl_config.stencil_size) {
slouken@3161
   446
        wdata->gles_attributes[attr_pos++] = EGL_STENCIL_SIZE;
slouken@3161
   447
        wdata->gles_attributes[attr_pos++] = _this->gl_config.buffer_size;
slouken@3161
   448
    } else {
slouken@3161
   449
        wdata->gles_attributes[attr_pos++] = EGL_STENCIL_SIZE;
slouken@3161
   450
        wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   451
    }
slouken@3161
   452
slouken@3161
   453
    /* Set number of samples in multisampling */
slouken@3161
   454
    if (_this->gl_config.multisamplesamples) {
slouken@3161
   455
        wdata->gles_attributes[attr_pos++] = EGL_SAMPLES;
slouken@3161
   456
        wdata->gles_attributes[attr_pos++] =
slouken@3161
   457
            _this->gl_config.multisamplesamples;
slouken@3161
   458
    }
slouken@3161
   459
slouken@3161
   460
    /* Multisample buffers, OpenGL ES 1.0 spec defines 0 or 1 buffer */
slouken@3161
   461
    if (_this->gl_config.multisamplebuffers) {
slouken@3161
   462
        wdata->gles_attributes[attr_pos++] = EGL_SAMPLE_BUFFERS;
slouken@3161
   463
        wdata->gles_attributes[attr_pos++] =
slouken@3161
   464
            _this->gl_config.multisamplebuffers;
slouken@3161
   465
    }
slouken@3161
   466
slouken@3161
   467
    /* Finish attributes list */
slouken@3161
   468
    wdata->gles_attributes[attr_pos] = EGL_NONE;
slouken@3161
   469
slouken@3161
   470
    /* Request first suitable framebuffer configuration */
slouken@3161
   471
    status = eglChooseConfig(phdata->egl_display, wdata->gles_attributes,
slouken@3161
   472
                             wdata->gles_configs, 1, &configs);
slouken@3161
   473
    if (status != EGL_TRUE) {
slouken@3161
   474
        SDL_SetError("PND: Can't find closest configuration for OpenGL ES");
slouken@3161
   475
        return NULL;
slouken@3161
   476
    }
slouken@3161
   477
slouken@3161
   478
    /* Check if nothing has been found, try "don't care" settings */
slouken@3161
   479
    if (configs == 0) {
slouken@3161
   480
        int32_t it;
slouken@3161
   481
        int32_t jt;
slouken@3161
   482
        GLint depthbits[4] = { 32, 24, 16, EGL_DONT_CARE };
slouken@3161
   483
slouken@3161
   484
        for (it = 0; it < 4; it++) {
slouken@3161
   485
            for (jt = 16; jt >= 0; jt--) {
slouken@3161
   486
                /* Don't care about color buffer bits, use what exist */
slouken@3161
   487
                /* Replace previous set data with EGL_DONT_CARE       */
slouken@3161
   488
                attr_pos = 0;
slouken@3161
   489
                wdata->gles_attributes[attr_pos++] = EGL_SURFACE_TYPE;
slouken@3161
   490
                wdata->gles_attributes[attr_pos++] = EGL_WINDOW_BIT;
slouken@3161
   491
                wdata->gles_attributes[attr_pos++] = EGL_RED_SIZE;
slouken@3161
   492
                wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   493
                wdata->gles_attributes[attr_pos++] = EGL_GREEN_SIZE;
slouken@3161
   494
                wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   495
                wdata->gles_attributes[attr_pos++] = EGL_BLUE_SIZE;
slouken@3161
   496
                wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   497
                wdata->gles_attributes[attr_pos++] = EGL_ALPHA_SIZE;
slouken@3161
   498
                wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   499
                wdata->gles_attributes[attr_pos++] = EGL_BUFFER_SIZE;
slouken@3161
   500
                wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   501
slouken@3161
   502
                /* Try to find requested or smallest depth */
slouken@3161
   503
                if (_this->gl_config.depth_size) {
slouken@3161
   504
                    wdata->gles_attributes[attr_pos++] = EGL_DEPTH_SIZE;
slouken@3161
   505
                    wdata->gles_attributes[attr_pos++] = depthbits[it];
slouken@3161
   506
                } else {
slouken@3161
   507
                    wdata->gles_attributes[attr_pos++] = EGL_DEPTH_SIZE;
slouken@3161
   508
                    wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   509
                }
slouken@3161
   510
slouken@3161
   511
                if (_this->gl_config.stencil_size) {
slouken@3161
   512
                    wdata->gles_attributes[attr_pos++] = EGL_STENCIL_SIZE;
slouken@3161
   513
                    wdata->gles_attributes[attr_pos++] = jt;
slouken@3161
   514
                } else {
slouken@3161
   515
                    wdata->gles_attributes[attr_pos++] = EGL_STENCIL_SIZE;
slouken@3161
   516
                    wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   517
                }
slouken@3161
   518
slouken@3161
   519
                wdata->gles_attributes[attr_pos++] = EGL_SAMPLES;
slouken@3161
   520
                wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   521
                wdata->gles_attributes[attr_pos++] = EGL_SAMPLE_BUFFERS;
slouken@3161
   522
                wdata->gles_attributes[attr_pos++] = EGL_DONT_CARE;
slouken@3161
   523
                wdata->gles_attributes[attr_pos] = EGL_NONE;
slouken@3161
   524
slouken@3161
   525
                /* Request first suitable framebuffer configuration */
slouken@3161
   526
                status =
slouken@3161
   527
                    eglChooseConfig(phdata->egl_display,
slouken@3161
   528
                                    wdata->gles_attributes,
slouken@3161
   529
                                    wdata->gles_configs, 1, &configs);
slouken@3161
   530
slouken@3161
   531
                if (status != EGL_TRUE) {
slouken@3161
   532
                    SDL_SetError
slouken@3161
   533
                        ("PND: Can't find closest configuration for OpenGL ES");
slouken@3161
   534
                    return NULL;
slouken@3161
   535
                }
slouken@3161
   536
                if (configs != 0) {
slouken@3161
   537
                    break;
slouken@3161
   538
                }
slouken@3161
   539
            }
slouken@3161
   540
            if (configs != 0) {
slouken@3161
   541
                break;
slouken@3161
   542
            }
slouken@3161
   543
        }
slouken@3161
   544
slouken@3161
   545
        /* No available configs */
slouken@3161
   546
        if (configs == 0) {
slouken@3161
   547
            SDL_SetError("PND: Can't find any configuration for OpenGL ES");
slouken@3161
   548
            return NULL;
slouken@3161
   549
        }
slouken@3161
   550
    }
slouken@3161
   551
slouken@3161
   552
    /* Initialize config index */
slouken@3161
   553
    wdata->gles_config = 0;
slouken@3161
   554
slouken@3161
   555
    /* Now check each configuration to find out the best */
slouken@3161
   556
    for (cit = 0; cit < configs; cit++) {
slouken@3161
   557
        uint32_t stencil_found;
slouken@3161
   558
        uint32_t depth_found;
slouken@3161
   559
slouken@3161
   560
        stencil_found = 0;
slouken@3161
   561
        depth_found = 0;
slouken@3161
   562
slouken@3161
   563
        if (_this->gl_config.stencil_size) {
slouken@3161
   564
            status =
slouken@3161
   565
                eglGetConfigAttrib(phdata->egl_display,
slouken@3161
   566
                                   wdata->gles_configs[cit], EGL_STENCIL_SIZE,
slouken@3161
   567
                                   &attr_value);
slouken@3161
   568
            if (status == EGL_TRUE) {
slouken@3161
   569
                if (attr_value != 0) {
slouken@3161
   570
                    stencil_found = 1;
slouken@3161
   571
                }
slouken@3161
   572
            }
slouken@3161
   573
        } else {
slouken@3161
   574
            stencil_found = 1;
slouken@3161
   575
        }
slouken@3161
   576
slouken@3161
   577
        if (_this->gl_config.depth_size) {
slouken@3161
   578
            status =
slouken@3161
   579
                eglGetConfigAttrib(phdata->egl_display,
slouken@3161
   580
                                   wdata->gles_configs[cit], EGL_DEPTH_SIZE,
slouken@3161
   581
                                   &attr_value);
slouken@3161
   582
            if (status == EGL_TRUE) {
slouken@3161
   583
                if (attr_value != 0) {
slouken@3161
   584
                    depth_found = 1;
slouken@3161
   585
                }
slouken@3161
   586
            }
slouken@3161
   587
        } else {
slouken@3161
   588
            depth_found = 1;
slouken@3161
   589
        }
slouken@3161
   590
slouken@3161
   591
        /* Exit from loop if found appropriate configuration */
slouken@3161
   592
        if ((depth_found != 0) && (stencil_found != 0)) {
slouken@3161
   593
            break;
slouken@3161
   594
        }
slouken@3161
   595
    }
slouken@3161
   596
slouken@3161
   597
    /* If best could not be found, use first */
slouken@3161
   598
    if (cit == configs) {
slouken@3161
   599
        cit = 0;
slouken@3161
   600
    }
slouken@3161
   601
    wdata->gles_config = cit;
slouken@3161
   602
slouken@3161
   603
    /* Create OpenGL ES context */
slouken@3161
   604
    wdata->gles_context =
slouken@3161
   605
        eglCreateContext(phdata->egl_display,
slouken@3161
   606
                         wdata->gles_configs[wdata->gles_config], NULL, NULL);
slouken@3161
   607
    if (wdata->gles_context == EGL_NO_CONTEXT) {
slouken@3161
   608
        SDL_SetError("PND: OpenGL ES context creation has been failed");
slouken@3161
   609
        return NULL;
slouken@3161
   610
    }
slouken@3161
   611
slouken@3438
   612
#ifdef WIZ_GLES_LITE
slouken@3438
   613
    if( !hNativeWnd ) {
slouken@7191
   614
    hNativeWnd = (NativeWindowType)malloc(16*1024);
slouken@3438
   615
slouken@7191
   616
    if(!hNativeWnd)
philipp@10205
   617
        printf( "Error: Wiz framebuffer allocatation failed\n" );
slouken@7191
   618
    else
philipp@10205
   619
        printf( "SDL: Wiz framebuffer allocated: %X\n", hNativeWnd );
slouken@3438
   620
    }
slouken@3438
   621
    else {
philipp@10205
   622
        printf( "SDL: Wiz framebuffer already allocated: %X\n", hNativeWnd );
slouken@3438
   623
    }
slouken@3438
   624
slouken@3438
   625
    wdata->gles_surface =
slouken@7191
   626
    eglCreateWindowSurface(phdata->egl_display,
slouken@7191
   627
                   wdata->gles_configs[wdata->gles_config],
slouken@7191
   628
                   hNativeWnd, NULL );
slouken@3438
   629
#else
slouken@3161
   630
    wdata->gles_surface =
slouken@3161
   631
        eglCreateWindowSurface(phdata->egl_display,
slouken@3161
   632
                               wdata->gles_configs[wdata->gles_config],
slouken@3161
   633
                               (NativeWindowType) 0, NULL);
slouken@3438
   634
#endif
slouken@3438
   635
slouken@3438
   636
slouken@3161
   637
    if (wdata->gles_surface == 0) {
slouken@3161
   638
        SDL_SetError("Error : eglCreateWindowSurface failed;\n");
slouken@3161
   639
        return NULL;
slouken@3161
   640
    }
slouken@3161
   641
slouken@3161
   642
    /* Make just created context current */
slouken@3161
   643
    status =
slouken@3161
   644
        eglMakeCurrent(phdata->egl_display, wdata->gles_surface,
slouken@3161
   645
                       wdata->gles_surface, wdata->gles_context);
slouken@3161
   646
    if (status != EGL_TRUE) {
slouken@3161
   647
        /* Destroy OpenGL ES surface */
slouken@3161
   648
        eglDestroySurface(phdata->egl_display, wdata->gles_surface);
slouken@3161
   649
        eglDestroyContext(phdata->egl_display, wdata->gles_context);
slouken@3161
   650
        wdata->gles_context = EGL_NO_CONTEXT;
slouken@3161
   651
        SDL_SetError("PND: Can't set OpenGL ES context on creation");
slouken@3161
   652
        return NULL;
slouken@3161
   653
    }
slouken@3161
   654
slouken@3161
   655
    _this->gl_config.accelerated = 1;
slouken@3161
   656
slouken@3161
   657
    /* Always clear stereo enable, since OpenGL ES do not supports stereo */
slouken@3161
   658
    _this->gl_config.stereo = 0;
slouken@3161
   659
slouken@3161
   660
    /* Get back samples and samplebuffers configurations. Rest framebuffer */
slouken@3161
   661
    /* parameters could be obtained through the OpenGL ES API              */
slouken@3161
   662
    status =
slouken@3161
   663
        eglGetConfigAttrib(phdata->egl_display,
slouken@3161
   664
                           wdata->gles_configs[wdata->gles_config],
slouken@3161
   665
                           EGL_SAMPLES, &attr_value);
slouken@3161
   666
    if (status == EGL_TRUE) {
slouken@3161
   667
        _this->gl_config.multisamplesamples = attr_value;
slouken@3161
   668
    }
slouken@3161
   669
    status =
slouken@3161
   670
        eglGetConfigAttrib(phdata->egl_display,
slouken@3161
   671
                           wdata->gles_configs[wdata->gles_config],
slouken@3161
   672
                           EGL_SAMPLE_BUFFERS, &attr_value);
slouken@3161
   673
    if (status == EGL_TRUE) {
slouken@3161
   674
        _this->gl_config.multisamplebuffers = attr_value;
slouken@3161
   675
    }
slouken@3161
   676
slouken@3161
   677
    /* Get back stencil and depth buffer sizes */
slouken@3161
   678
    status =
slouken@3161
   679
        eglGetConfigAttrib(phdata->egl_display,
slouken@3161
   680
                           wdata->gles_configs[wdata->gles_config],
slouken@3161
   681
                           EGL_DEPTH_SIZE, &attr_value);
slouken@3161
   682
    if (status == EGL_TRUE) {
slouken@3161
   683
        _this->gl_config.depth_size = attr_value;
slouken@3161
   684
    }
slouken@3161
   685
    status =
slouken@3161
   686
        eglGetConfigAttrib(phdata->egl_display,
slouken@3161
   687
                           wdata->gles_configs[wdata->gles_config],
slouken@3161
   688
                           EGL_STENCIL_SIZE, &attr_value);
slouken@3161
   689
    if (status == EGL_TRUE) {
slouken@3161
   690
        _this->gl_config.stencil_size = attr_value;
slouken@3161
   691
    }
slouken@3161
   692
slouken@3161
   693
    /* Under PND OpenGL ES output can't be double buffered */
slouken@3161
   694
    _this->gl_config.double_buffer = 0;
slouken@3161
   695
slouken@3161
   696
    /* GL ES context was successfully created */
slouken@3161
   697
    return wdata->gles_context;
slouken@3161
   698
}
slouken@3161
   699
slouken@3161
   700
int
slouken@3161
   701
PND_gl_makecurrent(_THIS, SDL_Window * window, SDL_GLContext context)
slouken@3161
   702
{
slouken@3161
   703
    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
slouken@3161
   704
    SDL_WindowData *wdata;
slouken@3161
   705
    EGLBoolean status;
slouken@3161
   706
slouken@3161
   707
    if (phdata->egl_initialized != SDL_TRUE) {
icculus@7037
   708
        return SDL_SetError("PND: GF initialization failed, no OpenGL ES support");
slouken@3161
   709
    }
slouken@3161
   710
slouken@3161
   711
    if ((window == NULL) && (context == NULL)) {
slouken@3161
   712
        status =
slouken@3161
   713
            eglMakeCurrent(phdata->egl_display, EGL_NO_SURFACE,
slouken@3161
   714
                           EGL_NO_SURFACE, EGL_NO_CONTEXT);
slouken@3161
   715
        if (status != EGL_TRUE) {
slouken@3161
   716
            /* Failed to set current GL ES context */
icculus@7037
   717
            return SDL_SetError("PND: Can't set OpenGL ES context");
slouken@3161
   718
        }
slouken@3161
   719
    } else {
slouken@3161
   720
        wdata = (SDL_WindowData *) window->driverdata;
slouken@3161
   721
        if (wdata->gles_surface == EGL_NO_SURFACE) {
icculus@7037
   722
            return SDL_SetError
slouken@3161
   723
                ("PND: OpenGL ES surface is not initialized for this window");
slouken@3161
   724
        }
slouken@3161
   725
        if (wdata->gles_context == EGL_NO_CONTEXT) {
icculus@7037
   726
            return SDL_SetError
slouken@3161
   727
                ("PND: OpenGL ES context is not initialized for this window");
slouken@3161
   728
        }
slouken@3161
   729
        if (wdata->gles_context != context) {
icculus@7037
   730
            return SDL_SetError
slouken@3161
   731
                ("PND: OpenGL ES context is not belong to this window");
slouken@3161
   732
        }
slouken@3161
   733
        status =
slouken@3161
   734
            eglMakeCurrent(phdata->egl_display, wdata->gles_surface,
slouken@3161
   735
                           wdata->gles_surface, wdata->gles_context);
slouken@3161
   736
        if (status != EGL_TRUE) {
slouken@3161
   737
            /* Failed to set current GL ES context */
icculus@7037
   738
            return SDL_SetError("PND: Can't set OpenGL ES context");
slouken@3161
   739
        }
slouken@3161
   740
    }
slouken@3161
   741
    return 0;
slouken@3161
   742
}
slouken@3161
   743
slouken@3161
   744
int
slouken@3161
   745
PND_gl_setswapinterval(_THIS, int interval)
slouken@3161
   746
{
slouken@3161
   747
    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
slouken@3161
   748
    EGLBoolean status;
slouken@3161
   749
slouken@3161
   750
    if (phdata->egl_initialized != SDL_TRUE) {
icculus@7037
   751
        return SDL_SetError("PND: EGL initialization failed, no OpenGL ES support");
slouken@3161
   752
    }
slouken@3161
   753
slouken@3161
   754
    /* Check if OpenGL ES connection has been initialized */
slouken@3161
   755
    if (phdata->egl_display != EGL_NO_DISPLAY) {
slouken@3161
   756
        /* Set swap OpenGL ES interval */
slouken@3161
   757
        status = eglSwapInterval(phdata->egl_display, interval);
slouken@3161
   758
        if (status == EGL_TRUE) {
slouken@3161
   759
            /* Return success to upper level */
slouken@3161
   760
            phdata->swapinterval = interval;
slouken@3161
   761
            return 0;
slouken@3161
   762
        }
slouken@3161
   763
    }
slouken@3161
   764
slouken@3161
   765
    /* Failed to set swap interval */
icculus@7037
   766
    return SDL_SetError("PND: Cannot set swap interval");
slouken@3161
   767
}
slouken@3161
   768
slouken@3161
   769
int
slouken@3161
   770
PND_gl_getswapinterval(_THIS)
slouken@3161
   771
{
icculus@6382
   772
    return ((SDL_VideoData *) _this->driverdata)->swapinterval;
slouken@3161
   773
}
slouken@3161
   774
slouken@3161
   775
void
slouken@3161
   776
PND_gl_swapwindow(_THIS, SDL_Window * window)
slouken@3161
   777
{
slouken@3161
   778
    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
slouken@3161
   779
    SDL_WindowData *wdata = (SDL_WindowData *) window->driverdata;
slouken@3161
   780
slouken@3161
   781
    if (phdata->egl_initialized != SDL_TRUE) {
slouken@3161
   782
        SDL_SetError("PND: GLES initialization failed, no OpenGL ES support");
slouken@3161
   783
        return;
slouken@3161
   784
    }
slouken@3161
   785
slouken@3161
   786
    /* Many applications do not uses glFinish(), so we call it for them */
slouken@3161
   787
    glFinish();
slouken@3161
   788
slouken@3161
   789
    /* Wait until OpenGL ES rendering is completed */
slouken@3161
   790
    eglWaitGL();
slouken@3161
   791
slouken@3161
   792
    eglSwapBuffers(phdata->egl_display, wdata->gles_surface);
slouken@3161
   793
}
slouken@3161
   794
slouken@3161
   795
void
slouken@3161
   796
PND_gl_deletecontext(_THIS, SDL_GLContext context)
slouken@3161
   797
{
slouken@3161
   798
    SDL_VideoData *phdata = (SDL_VideoData *) _this->driverdata;
slouken@3161
   799
    EGLBoolean status;
slouken@3161
   800
slouken@3161
   801
    if (phdata->egl_initialized != SDL_TRUE) {
slouken@3161
   802
        SDL_SetError("PND: GLES initialization failed, no OpenGL ES support");
slouken@3161
   803
        return;
slouken@3161
   804
    }
slouken@3161
   805
slouken@3161
   806
    /* Check if OpenGL ES connection has been initialized */
slouken@3161
   807
    if (phdata->egl_display != EGL_NO_DISPLAY) {
slouken@3161
   808
        if (context != EGL_NO_CONTEXT) {
slouken@3161
   809
            status = eglDestroyContext(phdata->egl_display, context);
slouken@3161
   810
            if (status != EGL_TRUE) {
slouken@3161
   811
                /* Error during OpenGL ES context destroying */
slouken@3161
   812
                SDL_SetError("PND: OpenGL ES context destroy error");
slouken@3161
   813
                return;
slouken@3161
   814
            }
slouken@3161
   815
        }
slouken@3161
   816
    }
slouken@3161
   817
slouken@3438
   818
#ifdef WIZ_GLES_LITE
slouken@3438
   819
    if( hNativeWnd != 0 )
slouken@3438
   820
    {
slouken@7191
   821
      free(hNativeWnd);
slouken@7191
   822
      hNativeWnd = 0;
philipp@10205
   823
      printf( "SDL: Wiz framebuffer released\n" );
slouken@3438
   824
    }
slouken@3438
   825
#endif
slouken@3438
   826
slouken@3161
   827
    return;
slouken@3161
   828
}
slouken@3161
   829
slouken@6044
   830
#endif /* SDL_VIDEO_DRIVER_PANDORA */
slouken@6044
   831
slouken@3161
   832
/* vi: set ts=4 sw=4 expandtab: */