src/video/kmsdrm/SDL_kmsdrmopengles.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 05 Aug 2017 22:10:36 +0200
changeset 11190 478630f6b756
parent 11181 951807bdae20
child 11342 0b924e560249
permissions -rw-r--r--
kmsdrm: Fixed crashes if allocating memory failed.
     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->next_bo != NULL) {
    77         KMSDRM_gbm_surface_release_buffer(wdata->gs, wdata->current_bo);
    78         /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Released GBM surface %p", (void *)wdata->next_bo); */
    79 
    80         wdata->current_bo = wdata->next_bo;
    81         wdata->next_bo = NULL;
    82     }
    83 
    84     if (!(_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, wdata->egl_surface))) {
    85         SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "eglSwapBuffers failed.");
    86         return 0;
    87     }
    88 
    89     if (wdata->current_bo == NULL) {
    90         wdata->current_bo = KMSDRM_gbm_surface_lock_front_buffer(wdata->gs);
    91         if (wdata->current_bo == NULL) {
    92             return 0;
    93         }
    94     }
    95 
    96     wdata->next_bo = KMSDRM_gbm_surface_lock_front_buffer(wdata->gs);
    97     if (wdata->next_bo == NULL) {
    98         SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not lock GBM surface front buffer");
    99         return 0;
   100     /* } else {
   101         SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Locked GBM surface %p", (void *)wdata->next_bo); */
   102     }
   103 
   104     fb_info = KMSDRM_FBFromBO(_this, wdata->next_bo);
   105     if (fb_info == NULL) {
   106         return 0;
   107     }
   108     if (_this->egl_data->egl_swapinterval == 0) {
   109         /* Swap buffers instantly, possible tearing */
   110         /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "drmModeSetCrtc(%d, %u, %u, 0, 0, &%u, 1, &%ux%u@%u)",
   111             vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id, vdata->saved_conn_id,
   112             displaydata->cur_mode.hdisplay, displaydata->cur_mode.vdisplay, displaydata->cur_mode.vrefresh); */
   113         ret = KMSDRM_drmModeSetCrtc(vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id,
   114                                     0, 0, &vdata->saved_conn_id, 1, &displaydata->cur_mode);
   115         if(ret != 0) {
   116             SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not pageflip with drmModeSetCrtc: %d", ret);
   117         }
   118     } else {
   119         /* Queue page flip at vsync */
   120         /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "drmModePageFlip(%d, %u, %u, DRM_MODE_PAGE_FLIP_EVENT, &wdata->waiting_for_flip)",
   121             vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id); */
   122         ret = KMSDRM_drmModePageFlip(vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id,
   123                                      DRM_MODE_PAGE_FLIP_EVENT, &wdata->waiting_for_flip);
   124         if (ret == 0) {
   125             wdata->waiting_for_flip = SDL_TRUE;
   126         } else {
   127             SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not queue pageflip: %d", ret);
   128         }
   129     }
   130 
   131     return 0;
   132 }
   133 
   134 SDL_EGL_MakeCurrent_impl(KMSDRM)
   135 
   136 #endif /* SDL_VIDEO_DRIVER_KMSDRM && SDL_VIDEO_OPENGL_EGL */
   137 
   138 /* vi: set ts=4 sw=4 expandtab: */