src/video/kmsdrm/SDL_kmsdrmopengles.c
changeset 11175 cbc6a8a5b701
child 11181 951807bdae20
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/kmsdrm/SDL_kmsdrmopengles.c	Wed Aug 02 10:22:48 2017 -0700
     1.3 @@ -0,0 +1,126 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "../../SDL_internal.h"
    1.25 +
    1.26 +#if SDL_VIDEO_DRIVER_KMSDRM && SDL_VIDEO_OPENGL_EGL
    1.27 +
    1.28 +#include "SDL_log.h"
    1.29 +
    1.30 +#include "SDL_kmsdrmvideo.h"
    1.31 +#include "SDL_kmsdrmopengles.h"
    1.32 +#include "SDL_kmsdrmdyn.h"
    1.33 +
    1.34 +#ifndef EGL_PLATFORM_GBM_MESA
    1.35 +#define EGL_PLATFORM_GBM_MESA 0x31D7
    1.36 +#endif
    1.37 +
    1.38 +/* EGL implementation of SDL OpenGL support */
    1.39 +
    1.40 +int
    1.41 +KMSDRM_GLES_LoadLibrary(_THIS, const char *path) {
    1.42 +    return SDL_EGL_LoadLibrary(_this, path, ((SDL_VideoData *)_this->driverdata)->gbm, EGL_PLATFORM_GBM_MESA);
    1.43 +}
    1.44 +
    1.45 +SDL_EGL_CreateContext_impl(KMSDRM)
    1.46 +
    1.47 +int KMSDRM_GLES_SetSwapInterval(_THIS, int interval) {
    1.48 +    if (!_this->egl_data) {
    1.49 +        return SDL_SetError("EGL not initialized");
    1.50 +    }
    1.51 +
    1.52 +    if (interval == 0 || interval == 1) {
    1.53 +        _this->egl_data->egl_swapinterval = interval;
    1.54 +    } else {
    1.55 +        return SDL_SetError("Only swap intervals of 0 or 1 are supported");
    1.56 +    }
    1.57 +
    1.58 +    return 0;
    1.59 +}
    1.60 +
    1.61 +int
    1.62 +KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) {
    1.63 +    SDL_WindowData *wdata = ((SDL_WindowData *) window->driverdata);
    1.64 +    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
    1.65 +    SDL_VideoData *vdata = ((SDL_VideoData *)_this->driverdata);
    1.66 +    KMSDRM_FBInfo *fb_info;
    1.67 +    int ret;
    1.68 +
    1.69 +    /* Do we still need to wait for a flip? */
    1.70 +    int timeout = 0;
    1.71 +    if (_this->egl_data->egl_swapinterval == 1) {
    1.72 +        timeout = -1;
    1.73 +    }
    1.74 +    if (!KMSDRM_WaitPageFlip(_this, wdata, timeout)) {
    1.75 +        return 0;
    1.76 +    }
    1.77 +
    1.78 +    /* Release previously displayed buffer (which is now the backbuffer) and lock a new one */
    1.79 +    if (wdata->locked_bo != NULL) {
    1.80 +        KMSDRM_gbm_surface_release_buffer(wdata->gs, wdata->locked_bo);
    1.81 +        /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Released GBM surface %p", (void *)wdata->locked_bo); */
    1.82 +        wdata->locked_bo = NULL;
    1.83 +    }
    1.84 +
    1.85 +    if (!(_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, wdata->egl_surface))) {
    1.86 +        SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "eglSwapBuffers failed.");
    1.87 +        return 0;
    1.88 +    }
    1.89 +
    1.90 +    wdata->locked_bo = KMSDRM_gbm_surface_lock_front_buffer(wdata->gs);
    1.91 +    if (wdata->locked_bo == NULL) {
    1.92 +        SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not lock GBM surface front buffer");
    1.93 +        return 0;
    1.94 +    /* } else {
    1.95 +        SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Locked GBM surface %p", (void *)wdata->locked_bo); */
    1.96 +    }
    1.97 +
    1.98 +    fb_info = KMSDRM_FBFromBO(_this, wdata->locked_bo);
    1.99 +    if (_this->egl_data->egl_swapinterval == 0) {
   1.100 +        /* Swap buffers instantly, possible tearing */
   1.101 +        /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "drmModeSetCrtc(%d, %u, %u, 0, 0, &%u, 1, &%ux%u@%u)",
   1.102 +            vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id, vdata->saved_conn_id,
   1.103 +            displaydata->cur_mode.hdisplay, displaydata->cur_mode.vdisplay, displaydata->cur_mode.vrefresh); */
   1.104 +        ret = KMSDRM_drmModeSetCrtc(vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id,
   1.105 +                                    0, 0, &vdata->saved_conn_id, 1, &displaydata->cur_mode);
   1.106 +        if(ret != 0) {
   1.107 +            SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not pageflip with drmModeSetCrtc: %d", ret);
   1.108 +        }
   1.109 +    } else {
   1.110 +        /* Queue page flip at vsync */
   1.111 +        /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "drmModePageFlip(%d, %u, %u, DRM_MODE_PAGE_FLIP_EVENT, &wdata->waiting_for_flip)",
   1.112 +            vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id); */
   1.113 +        ret = KMSDRM_drmModePageFlip(vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id,
   1.114 +                                     DRM_MODE_PAGE_FLIP_EVENT, &wdata->waiting_for_flip);
   1.115 +        if (ret == 0) {
   1.116 +            wdata->waiting_for_flip = SDL_TRUE;
   1.117 +        } else {
   1.118 +            SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not queue pageflip: %d", ret);
   1.119 +        }
   1.120 +    }
   1.121 +
   1.122 +    return 0;
   1.123 +}
   1.124 +
   1.125 +SDL_EGL_MakeCurrent_impl(KMSDRM)
   1.126 +
   1.127 +#endif /* SDL_VIDEO_DRIVER_KMSDRM && SDL_VIDEO_OPENGL_EGL */
   1.128 +
   1.129 +/* vi: set ts=4 sw=4 expandtab: */