src/video/mir/SDL_miropengl.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 03 Feb 2014 11:52:54 -0500
changeset 8159 427e8fdf6f69
parent 8157 f5d262613ed2
child 8168 76d51b45342e
permissions -rw-r--r--
Hooked up dynamic loading for Mir.
icculus@8153
     1
/*
icculus@8153
     2
  Simple DirectMedia Layer
icculus@8157
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
icculus@8153
     4
icculus@8153
     5
  This software is provided 'as-is', without any express or implied
icculus@8153
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@8153
     7
  arising from the use of this software.
icculus@8153
     8
icculus@8153
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@8153
    10
  including commercial applications, and to alter it and redistribute it
icculus@8153
    11
  freely, subject to the following restrictions:
icculus@8153
    12
icculus@8153
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@8153
    14
     claim that you wrote the original software. If you use this software
icculus@8153
    15
     in a product, an acknowledgment in the product documentation would be
icculus@8153
    16
     appreciated but is not required.
icculus@8153
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@8153
    18
     misrepresented as being the original software.
icculus@8153
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@8153
    20
*/
icculus@8153
    21
icculus@8153
    22
/*
icculus@8153
    23
  Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
icculus@8153
    24
*/
icculus@8153
    25
icculus@8154
    26
#include "../../SDL_internal.h"
icculus@8154
    27
icculus@8154
    28
#if SDL_VIDEO_DRIVER_MIR
icculus@8154
    29
icculus@8153
    30
#include "SDL_miropengl.h"
icculus@8153
    31
icculus@8153
    32
#include <dlfcn.h>
icculus@8153
    33
icculus@8153
    34
#define DEFAULT_OGL_ES2 "libGLESv2.so"
icculus@8153
    35
icculus@8159
    36
#include "SDL_mirdyn.h"
icculus@8159
    37
icculus@8153
    38
void* MIR_GLHandle = NULL;
icculus@8153
    39
icculus@8153
    40
void
icculus@8153
    41
MIR_GL_SwapWindow(_THIS, SDL_Window* window)
icculus@8153
    42
{
icculus@8153
    43
    MIR_Window* mir_wind = window->driverdata;
icculus@8153
    44
icculus@8153
    45
    SDL_EGL_SwapBuffers(_this, mir_wind->egl_surface);
icculus@8153
    46
}
icculus@8153
    47
icculus@8153
    48
int
icculus@8153
    49
MIR_GL_MakeCurrent(_THIS, SDL_Window* window, SDL_GLContext context)
icculus@8153
    50
{
icculus@8153
    51
  if (window) {
icculus@8153
    52
      EGLSurface egl_surface = ((MIR_Window*)window->driverdata)->egl_surface;
icculus@8153
    53
      return SDL_EGL_MakeCurrent(_this, egl_surface, context);
icculus@8153
    54
  }
icculus@8153
    55
icculus@8153
    56
  return SDL_EGL_MakeCurrent(_this, NULL, NULL);
icculus@8153
    57
}
icculus@8153
    58
icculus@8153
    59
SDL_GLContext
icculus@8153
    60
MIR_GL_CreateContext(_THIS, SDL_Window* window)
icculus@8153
    61
{
icculus@8153
    62
    MIR_Window* mir_window = window->driverdata;
icculus@8153
    63
icculus@8153
    64
    SDL_GLContext context;
icculus@8153
    65
    context = SDL_EGL_CreateContext(_this, mir_window->egl_surface);
icculus@8153
    66
icculus@8153
    67
    return context;
icculus@8153
    68
}
icculus@8153
    69
icculus@8153
    70
int
icculus@8153
    71
MIR_GL_LoadLibrary(_THIS, const char* path)
icculus@8153
    72
{
icculus@8153
    73
    MIR_Data* mir_data = _this->driverdata;
icculus@8153
    74
icculus@8159
    75
    SDL_EGL_LoadLibrary(_this, path, MIR_mir_connection_get_egl_native_display(mir_data->connection));
icculus@8153
    76
icculus@8153
    77
    SDL_EGL_ChooseConfig(_this);
icculus@8153
    78
icculus@8153
    79
    return 0;
icculus@8153
    80
}
icculus@8153
    81
icculus@8153
    82
void
icculus@8153
    83
MIR_GL_UnloadLibrary(_THIS)
icculus@8153
    84
{
icculus@8153
    85
    SDL_EGL_UnloadLibrary(_this);
icculus@8153
    86
icculus@8153
    87
    if (MIR_GLHandle) {
icculus@8153
    88
        dlclose(MIR_GLHandle);
icculus@8153
    89
        MIR_GLHandle = NULL;
icculus@8153
    90
    }
icculus@8153
    91
}
icculus@8153
    92
icculus@8153
    93
void
icculus@8153
    94
Ensure_GL_HandleOpen()
icculus@8153
    95
{
icculus@8153
    96
    if (!MIR_GLHandle) {
icculus@8153
    97
        MIR_GLHandle = dlopen(DEFAULT_OGL_ES2, RTLD_GLOBAL);
icculus@8153
    98
        if (!MIR_GLHandle) {
icculus@8153
    99
            SDL_SetError("Failed to dlopen library.");
icculus@8153
   100
        }
icculus@8153
   101
    }
icculus@8153
   102
}
icculus@8153
   103
icculus@8153
   104
void*
icculus@8153
   105
MIR_GL_GetProcAddress(_THIS, const char* proc)
icculus@8153
   106
{
icculus@8159
   107
    void* proc_addr = SDL_EGL_GetProcAddress(_this, proc);
icculus@8153
   108
icculus@8153
   109
    /* FIXME when on the phone/tablet eglGetProcAddress returns NULL through libhybris,
icculus@8153
   110
       seems to be a problem in android. Also looks like a problem in the android video driver:
icculus@8153
   111
       src/video/android/SDL_androidgl.c
icculus@8153
   112
icculus@8153
   113
       Workaround, to just get the address ourself
icculus@8153
   114
    */
icculus@8153
   115
icculus@8153
   116
    if (!proc_addr) {
icculus@8153
   117
        Ensure_GL_HandleOpen();
icculus@8153
   118
        proc_addr = dlsym(MIR_GLHandle, proc);
icculus@8153
   119
    }
icculus@8153
   120
icculus@8153
   121
    if (!proc_addr) {
icculus@8153
   122
        SDL_SetError("Failed to find proc address!");
icculus@8153
   123
    }
icculus@8153
   124
icculus@8153
   125
    return proc_addr;
icculus@8153
   126
}
icculus@8154
   127
icculus@8154
   128
#endif /* SDL_VIDEO_DRIVER_MIR */
icculus@8154
   129
icculus@8154
   130
/* vi: set ts=4 sw=4 expandtab: */