src/video/kmsdrm/SDL_kmsdrmopengles.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 02 Aug 2017 10:22:48 -0700
changeset 11175 cbc6a8a5b701
child 11181 951807bdae20
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 && SDL_VIDEO_OPENGL_EGL
    24 
    25 #include "SDL_log.h"
    26 
    27 #include "SDL_kmsdrmvideo.h"
    28 #include "SDL_kmsdrmopengles.h"
    29 #include "SDL_kmsdrmdyn.h"
    30 
    31 #ifndef EGL_PLATFORM_GBM_MESA
    32 #define EGL_PLATFORM_GBM_MESA 0x31D7
    33 #endif
    34 
    35 /* EGL implementation of SDL OpenGL support */
    36 
    37 int
    38 KMSDRM_GLES_LoadLibrary(_THIS, const char *path) {
    39     return SDL_EGL_LoadLibrary(_this, path, ((SDL_VideoData *)_this->driverdata)->gbm, EGL_PLATFORM_GBM_MESA);
    40 }
    41 
    42 SDL_EGL_CreateContext_impl(KMSDRM)
    43 
    44 int KMSDRM_GLES_SetSwapInterval(_THIS, int interval) {
    45     if (!_this->egl_data) {
    46         return SDL_SetError("EGL not initialized");
    47     }
    48 
    49     if (interval == 0 || interval == 1) {
    50         _this->egl_data->egl_swapinterval = interval;
    51     } else {
    52         return SDL_SetError("Only swap intervals of 0 or 1 are supported");
    53     }
    54 
    55     return 0;
    56 }
    57 
    58 int
    59 KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) {
    60     SDL_WindowData *wdata = ((SDL_WindowData *) window->driverdata);
    61     SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
    62     SDL_VideoData *vdata = ((SDL_VideoData *)_this->driverdata);
    63     KMSDRM_FBInfo *fb_info;
    64     int ret;
    65 
    66     /* Do we still need to wait for a flip? */
    67     int timeout = 0;
    68     if (_this->egl_data->egl_swapinterval == 1) {
    69         timeout = -1;
    70     }
    71     if (!KMSDRM_WaitPageFlip(_this, wdata, timeout)) {
    72         return 0;
    73     }
    74 
    75     /* Release previously displayed buffer (which is now the backbuffer) and lock a new one */
    76     if (wdata->locked_bo != NULL) {
    77         KMSDRM_gbm_surface_release_buffer(wdata->gs, wdata->locked_bo);
    78         /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Released GBM surface %p", (void *)wdata->locked_bo); */
    79         wdata->locked_bo = NULL;
    80     }
    81 
    82     if (!(_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, wdata->egl_surface))) {
    83         SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "eglSwapBuffers failed.");
    84         return 0;
    85     }
    86 
    87     wdata->locked_bo = KMSDRM_gbm_surface_lock_front_buffer(wdata->gs);
    88     if (wdata->locked_bo == NULL) {
    89         SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not lock GBM surface front buffer");
    90         return 0;
    91     /* } else {
    92         SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Locked GBM surface %p", (void *)wdata->locked_bo); */
    93     }
    94 
    95     fb_info = KMSDRM_FBFromBO(_this, wdata->locked_bo);
    96     if (_this->egl_data->egl_swapinterval == 0) {
    97         /* Swap buffers instantly, possible tearing */
    98         /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "drmModeSetCrtc(%d, %u, %u, 0, 0, &%u, 1, &%ux%u@%u)",
    99             vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id, vdata->saved_conn_id,
   100             displaydata->cur_mode.hdisplay, displaydata->cur_mode.vdisplay, displaydata->cur_mode.vrefresh); */
   101         ret = KMSDRM_drmModeSetCrtc(vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id,
   102                                     0, 0, &vdata->saved_conn_id, 1, &displaydata->cur_mode);
   103         if(ret != 0) {
   104             SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not pageflip with drmModeSetCrtc: %d", ret);
   105         }
   106     } else {
   107         /* Queue page flip at vsync */
   108         /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "drmModePageFlip(%d, %u, %u, DRM_MODE_PAGE_FLIP_EVENT, &wdata->waiting_for_flip)",
   109             vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id); */
   110         ret = KMSDRM_drmModePageFlip(vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id,
   111                                      DRM_MODE_PAGE_FLIP_EVENT, &wdata->waiting_for_flip);
   112         if (ret == 0) {
   113             wdata->waiting_for_flip = SDL_TRUE;
   114         } else {
   115             SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not queue pageflip: %d", ret);
   116         }
   117     }
   118 
   119     return 0;
   120 }
   121 
   122 SDL_EGL_MakeCurrent_impl(KMSDRM)
   123 
   124 #endif /* SDL_VIDEO_DRIVER_KMSDRM && SDL_VIDEO_OPENGL_EGL */
   125 
   126 /* vi: set ts=4 sw=4 expandtab: */