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