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
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 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 #include "../../SDL_internal.h"
    22 
    23 #if SDL_VIDEO_DRIVER_KMSDRM
    24 
    25 #define DEBUG_DYNAMIC_KMSDRM 0
    26 
    27 #include "SDL_kmsdrmdyn.h"
    28 
    29 #if DEBUG_DYNAMIC_KMSDRM
    30 #include "SDL_log.h"
    31 #endif
    32 
    33 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
    34 
    35 #include "SDL_name.h"
    36 #include "SDL_loadso.h"
    37 
    38 typedef struct
    39 {
    40     void *lib;
    41     const char *libname;
    42 } kmsdrmdynlib;
    43 
    44 #ifndef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
    45 #define SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC NULL
    46 #endif
    47 #ifndef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM
    48 #define SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM NULL
    49 #endif
    50 
    51 static kmsdrmdynlib kmsdrmlibs[] = {
    52     {NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC},
    53     {NULL, SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM}
    54 };
    55 
    56 static void *
    57 KMSDRM_GetSym(const char *fnname, int *pHasModule)
    58 {
    59     int i;
    60     void *fn = NULL;
    61     for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
    62         if (kmsdrmlibs[i].lib != NULL) {
    63             fn = SDL_LoadFunction(kmsdrmlibs[i].lib, fnname);
    64             if (fn != NULL)
    65                 break;
    66         }
    67     }
    68 
    69 #if DEBUG_DYNAMIC_KMSDRM
    70     if (fn != NULL)
    71         SDL_Log("KMSDRM: Found '%s' in %s (%p)\n", fnname, kmsdrmlibs[i].libname, fn);
    72     else
    73         SDL_Log("KMSDRM: Symbol '%s' NOT FOUND!\n", fnname);
    74 #endif
    75 
    76     if (fn == NULL)
    77         *pHasModule = 0;  /* kill this module. */
    78 
    79     return fn;
    80 }
    81 
    82 #endif /* SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC */
    83 
    84 /* Define all the function pointers and wrappers... */
    85 #define SDL_KMSDRM_MODULE(modname) int SDL_KMSDRM_HAVE_##modname = 0;
    86 #define SDL_KMSDRM_SYM(rc,fn,params) SDL_DYNKMSDRMFN_##fn KMSDRM_##fn = NULL;
    87 #define SDL_KMSDRM_SYM_CONST(type,name) SDL_DYNKMSDRMCONST_##name KMSDRM_##name = NULL;
    88 #include "SDL_kmsdrmsym.h"
    89 
    90 static int kmsdrm_load_refcount = 0;
    91 
    92 void
    93 SDL_KMSDRM_UnloadSymbols(void)
    94 {
    95     /* Don't actually unload if more than one module is using the libs... */
    96     if (kmsdrm_load_refcount > 0) {
    97         if (--kmsdrm_load_refcount == 0) {
    98 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
    99             int i;
   100 #endif
   101 
   102             /* set all the function pointers to NULL. */
   103 #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 0;
   104 #define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = NULL;
   105 #define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = NULL;
   106 #include "SDL_kmsdrmsym.h"
   107 
   108 
   109 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
   110             for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
   111                 if (kmsdrmlibs[i].lib != NULL) {
   112                     SDL_UnloadObject(kmsdrmlibs[i].lib);
   113                     kmsdrmlibs[i].lib = NULL;
   114                 }
   115             }
   116 #endif
   117         }
   118     }
   119 }
   120 
   121 /* returns non-zero if all needed symbols were loaded. */
   122 int
   123 SDL_KMSDRM_LoadSymbols(void)
   124 {
   125     int rc = 1;                 /* always succeed if not using Dynamic KMSDRM stuff. */
   126 
   127     /* deal with multiple modules needing these symbols... */
   128     if (kmsdrm_load_refcount++ == 0) {
   129 #ifdef SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC
   130         int i;
   131         int *thismod = NULL;
   132         for (i = 0; i < SDL_TABLESIZE(kmsdrmlibs); i++) {
   133             if (kmsdrmlibs[i].libname != NULL) {
   134                 kmsdrmlibs[i].lib = SDL_LoadObject(kmsdrmlibs[i].libname);
   135             }
   136         }
   137 
   138 #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; /* default yes */
   139 #include "SDL_kmsdrmsym.h"
   140 
   141 #define SDL_KMSDRM_MODULE(modname) thismod = &SDL_KMSDRM_HAVE_##modname;
   142 #define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = (SDL_DYNKMSDRMFN_##fn) KMSDRM_GetSym(#fn,thismod);
   143 #define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = *(SDL_DYNKMSDRMCONST_##name*) KMSDRM_GetSym(#name,thismod);
   144 #include "SDL_kmsdrmsym.h"
   145 
   146         if ((SDL_KMSDRM_HAVE_LIBDRM) && (SDL_KMSDRM_HAVE_GBM)) {
   147             /* all required symbols loaded. */
   148             SDL_ClearError();
   149         } else {
   150             /* in case something got loaded... */
   151             SDL_KMSDRM_UnloadSymbols();
   152             rc = 0;
   153         }
   154 
   155 #else  /* no dynamic KMSDRM */
   156 
   157 #define SDL_KMSDRM_MODULE(modname) SDL_KMSDRM_HAVE_##modname = 1; /* default yes */
   158 #define SDL_KMSDRM_SYM(rc,fn,params) KMSDRM_##fn = fn;
   159 #define SDL_KMSDRM_SYM_CONST(type,name) KMSDRM_##name = name;
   160 #include "SDL_kmsdrmsym.h"
   161 
   162 #endif
   163     }
   164 
   165     return rc;
   166 }
   167 
   168 #endif /* SDL_VIDEO_DRIVER_KMSDRM */
   169 
   170 /* vi: set ts=4 sw=4 expandtab: */