src/video/kmsdrm/SDL_kmsdrmdyn.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 02 Aug 2017 10:22:48 -0700
changeset 11175 cbc6a8a5b701
child 11342 0b924e560249
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@11175
     1
/*
slouken@11175
     2
  Simple DirectMedia Layer
slouken@11175
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@11175
     4
slouken@11175
     5
  This software is provided 'as-is', without any express or implied
slouken@11175
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@11175
     7
  arising from the use of this software.
slouken@11175
     8
slouken@11175
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@11175
    10
  including commercial applications, and to alter it and redistribute it
slouken@11175
    11
  freely, subject to the following restrictions:
slouken@11175
    12
slouken@11175
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@11175
    14
     claim that you wrote the original software. If you use this software
slouken@11175
    15
     in a product, an acknowledgment in the product documentation would be
slouken@11175
    16
     appreciated but is not required.
slouken@11175
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@11175
    18
     misrepresented as being the original software.
slouken@11175
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@11175
    20
*/
slouken@11175
    21
#include "../../SDL_internal.h"
slouken@11175
    22
slouken@11175
    23
#if SDL_VIDEO_DRIVER_KMSDRM
slouken@11175
    24
slouken@11175
    25
#define DEBUG_DYNAMIC_KMSDRM 0
slouken@11175
    26
slouken@11175
    27
#include "SDL_kmsdrmdyn.h"
slouken@11175
    28
slouken@11175
    29
#if DEBUG_DYNAMIC_KMSDRM
slouken@11175
    30
#include "SDL_log.h"
slouken@11175
    31
#endif
slouken@11175
    32
slouken@11175
    33
#ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
slouken@11175
    34
slouken@11175
    35
#include "SDL_name.h"
slouken@11175
    36
#include "SDL_loadso.h"
slouken@11175
    37
slouken@11175
    38
typedef struct
slouken@11175
    39
{
slouken@11175
    40
    void *lib;
slouken@11175
    41
    const char *libname;
slouken@11175
    42
} kmsdrmdynlib;
slouken@11175
    43
slouken@11175
    44
#ifndef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
slouken@11175
    45
#define SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC NULL
slouken@11175
    46
#endif
slouken@11175
    47
#ifndef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM
slouken@11175
    48
#define SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM NULL
slouken@11175
    49
#endif
slouken@11175
    50
slouken@11175
    51
static kmsdrmdynlib kmsdrmlibs[] = {
slouken@11175
    52
    {NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC},
slouken@11175
    53
    {NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM}
slouken@11175
    54
};
slouken@11175
    55
slouken@11175
    56
static void *
slouken@11175
    57
KMSDRM_GetSym(const char *fnname, int *pHasModule)
slouken@11175
    58
{
slouken@11175
    59
    int i;
slouken@11175
    60
    void *fn = NULL;
slouken@11175
    61
    for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
slouken@11175
    62
        if (kmsdrmlibs[i].lib != NULL) {
slouken@11175
    63
            fn = SDL_LoadFunction(kmsdrmlibs[i].lib, fnname);
slouken@11175
    64
            if (fn != NULL)
slouken@11175
    65
                break;
slouken@11175
    66
        }
slouken@11175
    67
    }
slouken@11175
    68
slouken@11175
    69
#if DEBUG_DYNAMIC_KMSDRM
slouken@11175
    70
    if (fn != NULL)
slouken@11175
    71
        SDL_Log("KMSDRM: Found '%s' in %s (%p)\n", fnname, kmsdrmlibs[i].libname, fn);
slouken@11175
    72
    else
slouken@11175
    73
        SDL_Log("KMSDRM: Symbol '%s' NOT FOUND!\n", fnname);
slouken@11175
    74
#endif
slouken@11175
    75
slouken@11175
    76
    if (fn == NULL)
slouken@11175
    77
        *pHasModule = 0;  /* kill this module. */
slouken@11175
    78
slouken@11175
    79
    return fn;
slouken@11175
    80
}
slouken@11175
    81
slouken@11175
    82
#endif /* SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC */
slouken@11175
    83
slouken@11175
    84
/* Define all the function pointers and wrappers... */
slouken@11175
    85
#define SDL_KMSDRM_MODULE(modname) int SDL_KMSDRM_HAVE_##modname = 0;
slouken@11175
    86
#define SDL_KMSDRM_SYM(rc,fn,params) SDL_DYNKMSDRMFN_##fn KMSDRM_##fn = NULL;
slouken@11175
    87
#define SDL_KMSDRM_SYM_CONST(type,name) SDL_DYNKMSDRMCONST_##name KMSDRM_##name = NULL;
slouken@11175
    88
#include "SDL_kmsdrmsym.h"
slouken@11175
    89
slouken@11175
    90
static int kmsdrm_load_refcount = 0;
slouken@11175
    91
slouken@11175
    92
void
slouken@11175
    93
SDL_KMSDRM_UnloadSymbols(void)
slouken@11175
    94
{
slouken@11175
    95
    /* Don't actually unload if more than one module is using the libs... */
slouken@11175
    96
    if (kmsdrm_load_refcount > 0) {
slouken@11175
    97
        if (--kmsdrm_load_refcount == 0) {
slouken@11175
    98
#ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
slouken@11175
    99
            int i;
slouken@11175
   100
#endif
slouken@11175
   101
slouken@11175
   102
            /* set all the function pointers to NULL. */
slouken@11175
   103
#define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 0;
slouken@11175
   104
#define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = NULL;
slouken@11175
   105
#define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = NULL;
slouken@11175
   106
#include "SDL_kmsdrmsym.h"
slouken@11175
   107
slouken@11175
   108
slouken@11175
   109
#ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
slouken@11175
   110
            for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
slouken@11175
   111
                if (kmsdrmlibs[i].lib != NULL) {
slouken@11175
   112
                    SDL_UnloadObject(kmsdrmlibs[i].lib);
slouken@11175
   113
                    kmsdrmlibs[i].lib = NULL;
slouken@11175
   114
                }
slouken@11175
   115
            }
slouken@11175
   116
#endif
slouken@11175
   117
        }
slouken@11175
   118
    }
slouken@11175
   119
}
slouken@11175
   120
slouken@11175
   121
/* returns non-zero if all needed symbols were loaded. */
slouken@11175
   122
int
slouken@11175
   123
SDL_KMSDRM_LoadSymbols(void)
slouken@11175
   124
{
slouken@11175
   125
    int rc = 1;                 /* always succeed if not using Dynamic KMSDRM stuff. */
slouken@11175
   126
slouken@11175
   127
    /* deal with multiple modules needing these symbols... */
slouken@11175
   128
    if (kmsdrm_load_refcount++ == 0) {
slouken@11175
   129
#ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
slouken@11175
   130
        int i;
slouken@11175
   131
        int *thismod = NULL;
slouken@11175
   132
        for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
slouken@11175
   133
            if (kmsdrmlibs[i].libname != NULL) {
slouken@11175
   134
                kmsdrmlibs[i].lib = SDL_LoadObject(kmsdrmlibs[i].libname);
slouken@11175
   135
            }
slouken@11175
   136
        }
slouken@11175
   137
slouken@11175
   138
#define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; /* default yes */
slouken@11175
   139
#include "SDL_kmsdrmsym.h"
slouken@11175
   140
slouken@11175
   141
#define SDL_KMSDRM_MODULE(modname) thismod = &SDL_KMSDRM_HAVE_##modname;
slouken@11175
   142
#define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = (SDL_DYNKMSDRMFN_##fn) KMSDRM_GetSym(#fn,thismod);
slouken@11175
   143
#define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = *(SDL_DYNKMSDRMCONST_##name*) KMSDRM_GetSym(#name,thismod);
slouken@11175
   144
#include "SDL_kmsdrmsym.h"
slouken@11175
   145
slouken@11175
   146
        if ((SDL_KMSDRM_HAVE_LIBDRM) && (SDL_KMSDRM_HAVE_GBM)) {
slouken@11175
   147
            /* all required symbols loaded. */
slouken@11175
   148
            SDL_ClearError();
slouken@11175
   149
        } else {
slouken@11175
   150
            /* in case something got loaded... */
slouken@11175
   151
            SDL_KMSDRM_UnloadSymbols();
slouken@11175
   152
            rc = 0;
slouken@11175
   153
        }
slouken@11175
   154
slouken@11175
   155
#else  /* no dynamic KMSDRM */
slouken@11175
   156
slouken@11175
   157
#define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; /* default yes */
slouken@11175
   158
#define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = fn;
slouken@11175
   159
#define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = name;
slouken@11175
   160
#include "SDL_kmsdrmsym.h"
slouken@11175
   161
slouken@11175
   162
#endif
slouken@11175
   163
    }
slouken@11175
   164
slouken@11175
   165
    return rc;
slouken@11175
   166
}
slouken@11175
   167
slouken@11175
   168
#endif /* SDL_VIDEO_DRIVER_KMSDRM */
slouken@11175
   169
slouken@11175
   170
/* vi: set ts=4 sw=4 expandtab: */