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