src/video/x11/SDL_x11opengles.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 02 Aug 2017 10:22:48 -0700
changeset 11175 cbc6a8a5b701
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Fixed bug 3690 - SDL2 KMS/DRM render context support

Manuel

The attached patch adds support for KMS/DRM context graphics.

It builds with no problem on X86_64 GNU/Linux systems, provided the needed libraries are present, and on ARM GNU/Linux systems that have KMS/DRM support and a GLES2 implementation.
Tested on Raspberry Pi: KMS/DRM is what the Raspberry Pi will use as default in the near future, once the propietary DispmanX API by Broadcom is overtaken by open graphics stack, it's possible to boot current Raspbian system in KMS mode by adding "dtoverlay=vc4-kms-v3d" to config.txt on Raspbian's boot partition.
X86 systems use KMS right away in every current GNU/Linux system.

Simple build instructions:

$./autogen.sh
$./configure --enable-video-kmsdrm
$make
slouken@3190
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@3190
     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@3190
     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@3190
    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@3190
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@3190
    22
gabomdq@7659
    23
#if SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_OPENGL_EGL
slouken@3190
    24
slouken@3190
    25
#include "SDL_x11video.h"
slouken@3190
    26
#include "SDL_x11opengles.h"
slouken@6370
    27
#include "SDL_x11opengl.h"
slouken@3190
    28
gabomdq@7659
    29
/* EGL implementation of SDL OpenGL support */
slouken@3190
    30
slouken@3190
    31
int
slouken@9156
    32
X11_GLES_LoadLibrary(_THIS, const char *path)
slouken@9156
    33
{
slouken@3190
    34
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3190
    35
gabomdq@7723
    36
    /* If the profile requested is not GL ES, switch over to X11_GL functions  */
gabomdq@7723
    37
    if (_this->gl_config.profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
gabomdq@7659
    38
        #if SDL_VIDEO_OPENGL_GLX
gabomdq@8018
    39
        X11_GLES_UnloadLibrary(_this);
slouken@6370
    40
        _this->GL_LoadLibrary = X11_GL_LoadLibrary;
slouken@6370
    41
        _this->GL_GetProcAddress = X11_GL_GetProcAddress;
slouken@6370
    42
        _this->GL_UnloadLibrary = X11_GL_UnloadLibrary;
slouken@6370
    43
        _this->GL_CreateContext = X11_GL_CreateContext;
slouken@6370
    44
        _this->GL_MakeCurrent = X11_GL_MakeCurrent;
slouken@6370
    45
        _this->GL_SetSwapInterval = X11_GL_SetSwapInterval;
slouken@6370
    46
        _this->GL_GetSwapInterval = X11_GL_GetSwapInterval;
slouken@6370
    47
        _this->GL_SwapWindow = X11_GL_SwapWindow;
slouken@6370
    48
        _this->GL_DeleteContext = X11_GL_DeleteContext;
slouken@6370
    49
        return X11_GL_LoadLibrary(_this, path);
gabomdq@7659
    50
        #else
icculus@7037
    51
        return SDL_SetError("SDL not configured with OpenGL/GLX support");
gabomdq@7659
    52
        #endif
slouken@6370
    53
    }
gabomdq@7659
    54
    
slouken@11175
    55
    return SDL_EGL_LoadLibrary(_this, path, (NativeDisplayType) data->display, 0);
slouken@3190
    56
}
slouken@3190
    57
slouken@3190
    58
XVisualInfo *
slouken@3190
    59
X11_GLES_GetVisual(_THIS, Display * display, int screen)
slouken@3190
    60
{
gabomdq@7659
    61
   
gabomdq@7659
    62
    XVisualInfo *egl_visualinfo = NULL;
icculus@7471
    63
    EGLint visual_id;
gabomdq@7659
    64
    XVisualInfo vi_in;
gabomdq@7659
    65
    int out_count;
slouken@3190
    66
gabomdq@7659
    67
    if (!_this->egl_data) {
slouken@6370
    68
        /* The EGL library wasn't loaded, SDL_GetError() should have info */
slouken@6370
    69
        return NULL;
slouken@3190
    70
    }
slouken@3190
    71
gabomdq@7659
    72
    if (_this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
gabomdq@7659
    73
                                            _this->egl_data->egl_config,
gabomdq@7659
    74
                                            EGL_NATIVE_VISUAL_ID,
gabomdq@7659
    75
                                            &visual_id) == EGL_FALSE || !visual_id) {
gabomdq@7659
    76
        /* Use the default visual when all else fails */
gabomdq@7659
    77
        vi_in.screen = screen;
icculus@7827
    78
        egl_visualinfo = X11_XGetVisualInfo(display,
gabomdq@7659
    79
                                        VisualScreenMask,
gabomdq@7659
    80
                                        &vi_in, &out_count);
gabomdq@7659
    81
    } else {
gabomdq@7659
    82
        vi_in.screen = screen;
gabomdq@7659
    83
        vi_in.visualid = visual_id;
icculus@7827
    84
        egl_visualinfo = X11_XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &vi_in, &out_count);
slouken@3190
    85
    }
slouken@3190
    86
gabomdq@7659
    87
    return egl_visualinfo;
slouken@3190
    88
}
slouken@3190
    89
slouken@3190
    90
SDL_GLContext
slouken@3190
    91
X11_GLES_CreateContext(_THIS, SDL_Window * window)
slouken@3190
    92
{
gabomdq@7659
    93
    SDL_GLContext context;
slouken@3190
    94
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@3190
    95
    Display *display = data->videodata->display;
slouken@3190
    96
icculus@7827
    97
    X11_XSync(display, False);
gabomdq@7659
    98
    context = SDL_EGL_CreateContext(_this, data->egl_surface);
icculus@7827
    99
    X11_XSync(display, False);
slouken@3190
   100
slouken@6190
   101
    return context;
slouken@3190
   102
}
slouken@3190
   103
gabomdq@7659
   104
SDL_EGL_SwapWindow_impl(X11)
gabomdq@7659
   105
SDL_EGL_MakeCurrent_impl(X11)
slouken@3190
   106
gabomdq@7659
   107
#endif /* SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_OPENGL_EGL */
slouken@3190
   108
slouken@3190
   109
/* vi: set ts=4 sw=4 expandtab: */