src/video/kmsdrm/SDL_kmsdrmopengles.c
author Sam Lantinga
Thu, 27 Feb 2020 08:20:34 -0800
changeset 13555 936b1869a34a
parent 13524 9761858bd6a3
child 13696 ea20a7434b98
permissions -rw-r--r--
Fixed bug 5005 - warnings in kmsdrm

Malte Kie├čling

At the moment i get following warnings from kmsdrm:

* in SDL_kmsdrmvideo.c KMSDRM_DestroySurfaces is return type int, but thats never returned or checked against
* in SDL_kmsdrmvideo.c KMSDRM_DestroySurfaces the variable viddata is not used
* in SDL_kmsdrmopengles.c KMSDRM_GLES_LoadLibrary a cast to NativeDisplayType is missing

I attached a patch for them :)
slouken@11175
     1
/*
slouken@11175
     2
  Simple DirectMedia Layer
slouken@13422
     3
  Copyright (C) 1997-2020 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
*/
brandon@11342
    21
slouken@11175
    22
#include "../../SDL_internal.h"
slouken@11175
    23
slouken@11175
    24
#if SDL_VIDEO_DRIVER_KMSDRM && SDL_VIDEO_OPENGL_EGL
slouken@11175
    25
slouken@11175
    26
#include "SDL_log.h"
slouken@11175
    27
slouken@11175
    28
#include "SDL_kmsdrmvideo.h"
slouken@11175
    29
#include "SDL_kmsdrmopengles.h"
slouken@11175
    30
#include "SDL_kmsdrmdyn.h"
slouken@11175
    31
slouken@11175
    32
#ifndef EGL_PLATFORM_GBM_MESA
slouken@11175
    33
#define EGL_PLATFORM_GBM_MESA 0x31D7
slouken@11175
    34
#endif
slouken@11175
    35
slouken@11175
    36
/* EGL implementation of SDL OpenGL support */
slouken@11175
    37
slouken@11175
    38
int
slouken@11175
    39
KMSDRM_GLES_LoadLibrary(_THIS, const char *path) {
slouken@13555
    40
    NativeDisplayType display = (NativeDisplayType)((SDL_VideoData *)_this->driverdata)->gbm;
slouken@13555
    41
    return SDL_EGL_LoadLibrary(_this, path, display, EGL_PLATFORM_GBM_MESA);
slouken@11175
    42
}
slouken@11175
    43
slouken@11175
    44
SDL_EGL_CreateContext_impl(KMSDRM)
slouken@11175
    45
slouken@11175
    46
int KMSDRM_GLES_SetSwapInterval(_THIS, int interval) {
slouken@11175
    47
    if (!_this->egl_data) {
slouken@11175
    48
        return SDL_SetError("EGL not initialized");
slouken@11175
    49
    }
slouken@11175
    50
slouken@11175
    51
    if (interval == 0 || interval == 1) {
slouken@11175
    52
        _this->egl_data->egl_swapinterval = interval;
slouken@11175
    53
    } else {
slouken@11175
    54
        return SDL_SetError("Only swap intervals of 0 or 1 are supported");
slouken@11175
    55
    }
slouken@11175
    56
slouken@11175
    57
    return 0;
slouken@11175
    58
}
slouken@11175
    59
slouken@11175
    60
int
slouken@11175
    61
KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) {
slouken@13496
    62
    SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata);
slouken@13496
    63
    SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@13496
    64
    SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
slouken@11175
    65
    KMSDRM_FBInfo *fb_info;
slouken@13524
    66
    int ret, timeout;
slouken@11175
    67
slouken@13496
    68
    /* Recreate the GBM / EGL surfaces if the display mode has changed */
slouken@13496
    69
    if (windata->egl_surface_dirty) {
slouken@13496
    70
        KMSDRM_CreateSurfaces(_this, window);
slouken@13496
    71
    }
slouken@13496
    72
slouken@13496
    73
    /* Wait for confirmation that the next front buffer has been flipped, at which
slouken@13496
    74
       point the previous front buffer can be released */
slouken@13524
    75
    timeout = 0;
slouken@11175
    76
    if (_this->egl_data->egl_swapinterval == 1) {
slouken@11175
    77
        timeout = -1;
slouken@11175
    78
    }
slouken@13496
    79
    if (!KMSDRM_WaitPageFlip(_this, windata, timeout)) {
slouken@11175
    80
        return 0;
slouken@11175
    81
    }
slouken@11175
    82
slouken@13496
    83
    /* Release the previous front buffer */
slouken@13496
    84
    if (windata->curr_bo) {
slouken@13496
    85
        KMSDRM_gbm_surface_release_buffer(windata->gs, windata->curr_bo);
slouken@13496
    86
        /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Released GBM surface %p", (void *)windata->curr_bo); */
slouken@13496
    87
        windata->curr_bo = NULL;
slouken@11175
    88
    }
slouken@11175
    89
slouken@13496
    90
    windata->curr_bo = windata->next_bo;
slouken@13496
    91
slouken@13496
    92
    /* Make the current back buffer the next front buffer */
slouken@13496
    93
    if (!(_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, windata->egl_surface))) {
slouken@11175
    94
        SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "eglSwapBuffers failed.");
slouken@11175
    95
        return 0;
slouken@11175
    96
    }
slouken@11175
    97
slouken@13496
    98
    /* Lock the next front buffer so it can't be allocated as a back buffer */
slouken@13496
    99
    windata->next_bo = KMSDRM_gbm_surface_lock_front_buffer(windata->gs);
slouken@13496
   100
    if (!windata->next_bo) {
slouken@11175
   101
        SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not lock GBM surface front buffer");
slouken@11175
   102
        return 0;
slouken@11175
   103
    /* } else {
slouken@13496
   104
        SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Locked GBM surface %p", (void *)windata->next_bo); */
slouken@11175
   105
    }
slouken@11175
   106
slouken@13496
   107
    fb_info = KMSDRM_FBFromBO(_this, windata->next_bo);
slouken@13496
   108
    if (!fb_info) {
philipp@11190
   109
        return 0;
philipp@11190
   110
    }
connogriofa@13063
   111
slouken@13496
   112
    if (!windata->curr_bo) {
slouken@13496
   113
        /* On the first swap, immediately present the new front buffer. Before
slouken@13496
   114
           drmModePageFlip can be used the CRTC has to be configured to use
slouken@13496
   115
           the current connector and mode with drmModeSetCrtc */
slouken@13496
   116
        ret = KMSDRM_drmModeSetCrtc(viddata->drm_fd, dispdata->crtc_id, fb_info->fb_id, 0,
slouken@13496
   117
                                    0, &dispdata->conn->connector_id, 1, &dispdata->mode);
connogriofa@13063
   118
slouken@13496
   119
        if (ret) {
slouken@13496
   120
          SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not configure CRTC");
slouken@13496
   121
        }
slouken@13496
   122
    } else {
slouken@13496
   123
        /* On subsequent swaps, queue the new front buffer to be flipped during
slouken@13496
   124
           the next vertical blank */
slouken@13496
   125
        ret = KMSDRM_drmModePageFlip(viddata->drm_fd, dispdata->crtc_id, fb_info->fb_id,
slouken@13496
   126
                                     DRM_MODE_PAGE_FLIP_EVENT, &windata->waiting_for_flip);
slouken@13496
   127
        /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "drmModePageFlip(%d, %u, %u, DRM_MODE_PAGE_FLIP_EVENT, &windata->waiting_for_flip)",
slouken@13496
   128
            viddata->drm_fd, displaydata->crtc_id, fb_info->fb_id); */
connogriofa@13063
   129
slouken@13496
   130
        if (_this->egl_data->egl_swapinterval == 1) {
slouken@13496
   131
            if (ret == 0) {
slouken@13496
   132
                windata->waiting_for_flip = SDL_TRUE;
slouken@13496
   133
            } else {
slouken@13496
   134
                SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not queue pageflip: %d", ret);
slouken@13496
   135
            }
slouken@11175
   136
        }
brandon@11658
   137
brandon@11658
   138
        /* Wait immediately for vsync (as if we only had two buffers), for low input-lag scenarios.
brandon@11658
   139
           Run your SDL2 program with "SDL_KMSDRM_DOUBLE_BUFFER=1 <program_name>" to enable this. */
slouken@13496
   140
        if (_this->egl_data->egl_swapinterval == 1 && windata->double_buffer) {
slouken@13496
   141
            KMSDRM_WaitPageFlip(_this, windata, -1);
brandon@11658
   142
        }
slouken@11175
   143
    }
slouken@11175
   144
slouken@11175
   145
    return 0;
slouken@11175
   146
}
slouken@11175
   147
slouken@11175
   148
SDL_EGL_MakeCurrent_impl(KMSDRM)
slouken@11175
   149
slouken@11175
   150
#endif /* SDL_VIDEO_DRIVER_KMSDRM && SDL_VIDEO_OPENGL_EGL */
slouken@11175
   151
slouken@11175
   152
/* vi: set ts=4 sw=4 expandtab: */