src/video/kmsdrm/SDL_kmsdrmvideo.c
changeset 11175 cbc6a8a5b701
child 11176 9397a2d41d6b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c	Wed Aug 02 10:22:48 2017 -0700
     1.3 @@ -0,0 +1,634 @@
     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 +
    1.25 +#include "../../SDL_internal.h"
    1.26 +
    1.27 +#if SDL_VIDEO_DRIVER_KMSDRM
    1.28 +
    1.29 +/* SDL internals */
    1.30 +#include "../SDL_sysvideo.h"
    1.31 +#include "SDL_version.h"
    1.32 +#include "SDL_syswm.h"
    1.33 +#include "SDL_loadso.h"
    1.34 +#include "SDL_events.h"
    1.35 +#include "SDL_log.h"
    1.36 +#include "../../events/SDL_mouse_c.h"
    1.37 +#include "../../events/SDL_keyboard_c.h"
    1.38 +
    1.39 +#ifdef SDL_INPUT_LINUXEV
    1.40 +#include "../../core/linux/SDL_evdev.h"
    1.41 +#endif
    1.42 +
    1.43 +/* KMS/DRM declarations */
    1.44 +#include "SDL_kmsdrmvideo.h"
    1.45 +#include "SDL_kmsdrmevents_c.h"
    1.46 +#include "SDL_kmsdrmopengles.h"
    1.47 +#include "SDL_kmsdrmmouse.h"
    1.48 +#include "SDL_kmsdrmdyn.h"
    1.49 +
    1.50 +#define KMSDRM_DRI_CARD_0 "/dev/dri/card0"
    1.51 +
    1.52 +static int
    1.53 +KMSDRM_Available(void)
    1.54 +{
    1.55 +    int available = 0;
    1.56 +
    1.57 +    int drm_fd = open(KMSDRM_DRI_CARD_0, O_RDWR | O_CLOEXEC);
    1.58 +    if (drm_fd >= 0) {
    1.59 +        if (SDL_KMSDRM_LoadSymbols()) {
    1.60 +            drmModeRes *resources = KMSDRM_drmModeGetResources(drm_fd);
    1.61 +            if (resources != NULL) {
    1.62 +                available = 1;
    1.63 +                KMSDRM_drmModeFreeResources(resources);
    1.64 +            }
    1.65 +            SDL_KMSDRM_UnloadSymbols();
    1.66 +        }
    1.67 +        close(drm_fd);
    1.68 +    }
    1.69 +
    1.70 +    return available;
    1.71 +}
    1.72 +
    1.73 +static void
    1.74 +KMSDRM_Destroy(SDL_VideoDevice * device)
    1.75 +{
    1.76 +    if (device->driverdata != NULL) {
    1.77 +        SDL_free(device->driverdata);
    1.78 +        device->driverdata = NULL;
    1.79 +    }
    1.80 +    SDL_KMSDRM_UnloadSymbols();
    1.81 +}
    1.82 +
    1.83 +static SDL_VideoDevice *
    1.84 +KMSDRM_Create(int devindex)
    1.85 +{
    1.86 +    SDL_VideoDevice *device;
    1.87 +    SDL_VideoData *vdata;
    1.88 +
    1.89 +    if (devindex < 0 || devindex > 99) {
    1.90 +        SDL_SetError("devindex (%d) must be between 0 and 99.\n", devindex);
    1.91 +        return NULL;
    1.92 +    }
    1.93 +
    1.94 +    if (!SDL_KMSDRM_LoadSymbols()) {
    1.95 +        return NULL;
    1.96 +    }
    1.97 +
    1.98 +    /* Initialize SDL_VideoDevice structure */
    1.99 +    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
   1.100 +    if (device == NULL) {
   1.101 +        SDL_OutOfMemory();
   1.102 +        goto cleanup;
   1.103 +    }
   1.104 +
   1.105 +    /* Initialize internal data */
   1.106 +    vdata = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
   1.107 +    if (vdata == NULL) {
   1.108 +        SDL_OutOfMemory();
   1.109 +        goto cleanup;
   1.110 +    }
   1.111 +    vdata->devindex = devindex;
   1.112 +    vdata->drm_fd = -1;
   1.113 +
   1.114 +    device->driverdata = vdata;
   1.115 +
   1.116 +    /* Setup amount of available displays and current display */
   1.117 +    device->num_displays = 0;
   1.118 +
   1.119 +    /* Set device free function */
   1.120 +    device->free = KMSDRM_Destroy;
   1.121 +
   1.122 +    /* Setup all functions which we can handle */
   1.123 +    device->VideoInit = KMSDRM_VideoInit;
   1.124 +    device->VideoQuit = KMSDRM_VideoQuit;
   1.125 +    device->GetDisplayModes = KMSDRM_GetDisplayModes;
   1.126 +    device->SetDisplayMode = KMSDRM_SetDisplayMode;
   1.127 +    device->CreateWindow = KMSDRM_CreateWindow;
   1.128 +    device->CreateWindowFrom = KMSDRM_CreateWindowFrom;
   1.129 +    device->SetWindowTitle = KMSDRM_SetWindowTitle;
   1.130 +    device->SetWindowIcon = KMSDRM_SetWindowIcon;
   1.131 +    device->SetWindowPosition = KMSDRM_SetWindowPosition;
   1.132 +    device->SetWindowSize = KMSDRM_SetWindowSize;
   1.133 +    device->ShowWindow = KMSDRM_ShowWindow;
   1.134 +    device->HideWindow = KMSDRM_HideWindow;
   1.135 +    device->RaiseWindow = KMSDRM_RaiseWindow;
   1.136 +    device->MaximizeWindow = KMSDRM_MaximizeWindow;
   1.137 +    device->MinimizeWindow = KMSDRM_MinimizeWindow;
   1.138 +    device->RestoreWindow = KMSDRM_RestoreWindow;
   1.139 +    device->SetWindowGrab = KMSDRM_SetWindowGrab;
   1.140 +    device->DestroyWindow = KMSDRM_DestroyWindow;
   1.141 +    device->GetWindowWMInfo = KMSDRM_GetWindowWMInfo;
   1.142 +    device->GL_LoadLibrary = KMSDRM_GLES_LoadLibrary;
   1.143 +    device->GL_GetProcAddress = KMSDRM_GLES_GetProcAddress;
   1.144 +    device->GL_UnloadLibrary = KMSDRM_GLES_UnloadLibrary;
   1.145 +    device->GL_CreateContext = KMSDRM_GLES_CreateContext;
   1.146 +    device->GL_MakeCurrent = KMSDRM_GLES_MakeCurrent;
   1.147 +    device->GL_SetSwapInterval = KMSDRM_GLES_SetSwapInterval;
   1.148 +    device->GL_GetSwapInterval = KMSDRM_GLES_GetSwapInterval;
   1.149 +    device->GL_SwapWindow = KMSDRM_GLES_SwapWindow;
   1.150 +    device->GL_DeleteContext = KMSDRM_GLES_DeleteContext;
   1.151 +
   1.152 +    device->PumpEvents = KMSDRM_PumpEvents;
   1.153 +
   1.154 +    return device;
   1.155 +
   1.156 +cleanup:
   1.157 +    if (device != NULL)
   1.158 +        SDL_free(device);
   1.159 +    if (vdata != NULL)
   1.160 +        SDL_free(vdata);
   1.161 +    return NULL;
   1.162 +}
   1.163 +
   1.164 +VideoBootStrap KMSDRM_bootstrap = {
   1.165 +    "KMSDRM",
   1.166 +    "KMS/DRM Video Driver",
   1.167 +    KMSDRM_Available,
   1.168 +    KMSDRM_Create
   1.169 +};
   1.170 +
   1.171 +
   1.172 +static void
   1.173 +KMSDRM_FBDestroyCallback(struct gbm_bo *bo, void *data)
   1.174 +{
   1.175 +    KMSDRM_FBInfo *fb_info = (KMSDRM_FBInfo *)data;
   1.176 +
   1.177 +    if (fb_info && fb_info->drm_fd > 0 && fb_info->fb_id != 0) {
   1.178 +        KMSDRM_drmModeRmFB(fb_info->drm_fd, fb_info->fb_id);
   1.179 +        SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Delete DRM FB %u", fb_info->fb_id);
   1.180 +    }
   1.181 +
   1.182 +    free(fb_info);
   1.183 +}
   1.184 +
   1.185 +KMSDRM_FBInfo *
   1.186 +KMSDRM_FBFromBO(_THIS, struct gbm_bo *bo)
   1.187 +{
   1.188 +    uint32_t w, h, stride, handle;
   1.189 +    int ret;
   1.190 +    SDL_VideoData *vdata = ((SDL_VideoData *)_this->driverdata);
   1.191 +    KMSDRM_FBInfo *fb_info;
   1.192 +
   1.193 +    fb_info = (KMSDRM_FBInfo *)KMSDRM_gbm_bo_get_user_data(bo);
   1.194 +    if (fb_info != NULL) {
   1.195 +        /* Have a previously used framebuffer, return it */
   1.196 +        return fb_info;
   1.197 +    }
   1.198 +
   1.199 +    /* Here a new DRM FB must be created */
   1.200 +    fb_info = (KMSDRM_FBInfo *)SDL_calloc(1, sizeof(KMSDRM_FBInfo));
   1.201 +    fb_info->drm_fd = vdata->drm_fd;
   1.202 +
   1.203 +    w  = KMSDRM_gbm_bo_get_width(bo);
   1.204 +    h = KMSDRM_gbm_bo_get_height(bo);
   1.205 +    stride = KMSDRM_gbm_bo_get_stride(bo);
   1.206 +    handle = KMSDRM_gbm_bo_get_handle(bo).u32;
   1.207 +
   1.208 +    ret = KMSDRM_drmModeAddFB(vdata->drm_fd, w, h, 24, 32, stride, handle, &fb_info->fb_id);
   1.209 +    if (ret < 0) {
   1.210 +       free(fb_info);
   1.211 +       return NULL;
   1.212 +    }
   1.213 +    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "New DRM FB (%u): %ux%u, stride %u from BO %p", fb_info->fb_id, w, h, stride, (void *)bo);
   1.214 +
   1.215 +    /* Associate our DRM framebuffer with this buffer object */
   1.216 +    KMSDRM_gbm_bo_set_user_data(bo, fb_info, KMSDRM_FBDestroyCallback);
   1.217 +    return fb_info;
   1.218 +}
   1.219 +
   1.220 +SDL_bool
   1.221 +KMSDRM_WaitPageFlip(_THIS, SDL_WindowData *wdata, int timeout) {
   1.222 +    SDL_VideoData *vdata = ((SDL_VideoData *)_this->driverdata);
   1.223 +
   1.224 +    while (wdata->waiting_for_flip) {
   1.225 +        vdata->drm_pollfd.revents = 0;
   1.226 +        if (poll(&vdata->drm_pollfd, 1, timeout) < 0) {
   1.227 +            SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "DRM poll error");
   1.228 +            return SDL_FALSE;
   1.229 +        }
   1.230 +
   1.231 +        if (vdata->drm_pollfd.revents & (POLLHUP | POLLERR)) {
   1.232 +            SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "DRM poll hup or error");
   1.233 +            return SDL_FALSE;
   1.234 +        }
   1.235 +
   1.236 +        if (vdata->drm_pollfd.revents & POLLIN) {
   1.237 +            /* Page flip? If so, drmHandleEvent will unset wdata->waiting_for_flip */
   1.238 +            KMSDRM_drmHandleEvent(vdata->drm_fd, &vdata->drm_evctx);
   1.239 +        } else {
   1.240 +            /* Timed out and page flip didn't happen */
   1.241 +            SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Dropping frame while waiting_for_flip");
   1.242 +            return SDL_FALSE;
   1.243 +        }
   1.244 +    }
   1.245 +    return SDL_TRUE;
   1.246 +}
   1.247 +
   1.248 +static void
   1.249 +KMSDRM_FlipHandler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void *data)
   1.250 +{
   1.251 +    *((SDL_bool *) data) = SDL_FALSE;
   1.252 +}
   1.253 +
   1.254 +
   1.255 +/*****************************************************************************/
   1.256 +/* SDL Video and Display initialization/handling functions                   */
   1.257 +/* _this is a SDL_VideoDevice *                                              */
   1.258 +/*****************************************************************************/
   1.259 +int
   1.260 +KMSDRM_VideoInit(_THIS)
   1.261 +{
   1.262 +    int i;
   1.263 +    int ret = 0;
   1.264 +    char *devname;
   1.265 +    SDL_VideoData *vdata = ((SDL_VideoData *)_this->driverdata);
   1.266 +    drmModeRes *resources = NULL;
   1.267 +    drmModeConnector *connector = NULL;
   1.268 +    drmModeEncoder *encoder = NULL;
   1.269 +    SDL_DisplayMode current_mode;
   1.270 +    SDL_VideoDisplay display;
   1.271 +
   1.272 +    /* Allocate display internal data */
   1.273 +    SDL_DisplayData *data = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData));
   1.274 +    if (data == NULL) {
   1.275 +        return SDL_OutOfMemory();
   1.276 +    }
   1.277 +
   1.278 +    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "KMSDRM_VideoInit()");
   1.279 +
   1.280 +    /* Open /dev/dri/cardNN */
   1.281 +    devname = (char *) SDL_calloc(1, 16);
   1.282 +    SDL_snprintf(devname, 16, "/dev/dri/card%d", vdata->devindex);
   1.283 +    vdata->drm_fd = open(devname, O_RDWR | O_CLOEXEC);
   1.284 +    SDL_free(devname);
   1.285 +
   1.286 +    if (vdata->drm_fd < 0) {
   1.287 +        ret = SDL_SetError("Could not open /dev/dri/card%d.", vdata->devindex);
   1.288 +        goto cleanup;
   1.289 +    }
   1.290 +    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Opened DRM FD (%d)", vdata->drm_fd);
   1.291 +
   1.292 +    vdata->gbm = KMSDRM_gbm_create_device(vdata->drm_fd);
   1.293 +    if (vdata->gbm == NULL) {
   1.294 +        ret = SDL_SetError("Couldn't create gbm device.");
   1.295 +        goto cleanup;
   1.296 +    }
   1.297 +
   1.298 +    /* Find the first available connector with modes */
   1.299 +    resources = KMSDRM_drmModeGetResources(vdata->drm_fd);
   1.300 +    if (!resources) {
   1.301 +        ret = SDL_SetError("drmModeGetResources(%d) failed", vdata->drm_fd);
   1.302 +        goto cleanup;
   1.303 +    }
   1.304 +
   1.305 +    for (i = 0; i < resources->count_connectors; i++) {
   1.306 +        connector = KMSDRM_drmModeGetConnector(vdata->drm_fd, resources->connectors[i]);
   1.307 +        if (connector == NULL)
   1.308 +            continue;
   1.309 +
   1.310 +        if (connector->connection == DRM_MODE_CONNECTED &&
   1.311 +            connector->count_modes > 0) {
   1.312 +            SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found connector %d with %d modes.",
   1.313 +                         connector->connector_id, connector->count_modes);
   1.314 +            vdata->saved_conn_id = connector->connector_id;
   1.315 +            break;
   1.316 +        }
   1.317 +
   1.318 +        KMSDRM_drmModeFreeConnector(connector);
   1.319 +    }
   1.320 +
   1.321 +    if (i == resources->count_connectors) {
   1.322 +        ret = SDL_SetError("No currently active connector found.");
   1.323 +        goto cleanup;
   1.324 +    }
   1.325 +
   1.326 +    for (i = 0; i < resources->count_encoders; i++) {
   1.327 +        encoder = KMSDRM_drmModeGetEncoder(vdata->drm_fd, resources->encoders[i]);
   1.328 +
   1.329 +        if (encoder == NULL)
   1.330 +            continue;
   1.331 +
   1.332 +        if (encoder->encoder_id == connector->encoder_id) {
   1.333 +            SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id);
   1.334 +            data->encoder_id = encoder->encoder_id;
   1.335 +            break;
   1.336 +        }
   1.337 +
   1.338 +        KMSDRM_drmModeFreeEncoder(encoder);
   1.339 +    }
   1.340 +
   1.341 +    if (i == resources->count_encoders) {
   1.342 +        ret = SDL_SetError("No connected encoder found.");
   1.343 +        goto cleanup;
   1.344 +    }
   1.345 +
   1.346 +    vdata->saved_crtc = KMSDRM_drmModeGetCrtc(vdata->drm_fd, encoder->crtc_id);
   1.347 +    if (vdata->saved_crtc == NULL) {
   1.348 +        ret = SDL_SetError("No CRTC found.");
   1.349 +        goto cleanup;
   1.350 +    }
   1.351 +    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Saved crtc_id %u, fb_id %u, (%u,%u), %ux%u",
   1.352 +                 vdata->saved_crtc->crtc_id, vdata->saved_crtc->buffer_id, vdata->saved_crtc->x,
   1.353 +                 vdata->saved_crtc->y, vdata->saved_crtc->width, vdata->saved_crtc->height);
   1.354 +    data->crtc_id = encoder->crtc_id;
   1.355 +    data->cur_mode = vdata->saved_crtc->mode;
   1.356 +
   1.357 +    SDL_zero(current_mode);
   1.358 +
   1.359 +    current_mode.w = vdata->saved_crtc->mode.hdisplay;
   1.360 +    current_mode.h = vdata->saved_crtc->mode.vdisplay;
   1.361 +    current_mode.refresh_rate = vdata->saved_crtc->mode.vrefresh;
   1.362 +
   1.363 +    /* FIXME ?
   1.364 +    drmModeFB *fb = drmModeGetFB(vdata->drm_fd, vdata->saved_crtc->buffer_id);
   1.365 +    current_mode.format = drmToSDLPixelFormat(fb->bpp, fb->depth);
   1.366 +    drmModeFreeFB(fb);
   1.367 +    */
   1.368 +    current_mode.format = SDL_PIXELFORMAT_ARGB8888;
   1.369 +
   1.370 +    current_mode.driverdata = NULL;
   1.371 +
   1.372 +    SDL_zero(display);
   1.373 +    display.desktop_mode = current_mode;
   1.374 +    display.current_mode = current_mode;
   1.375 +
   1.376 +    display.driverdata = data;
   1.377 +    /* SDL_VideoQuit will later SDL_free(display.driverdata) */
   1.378 +    SDL_AddVideoDisplay(&display);
   1.379 +
   1.380 +    /* Setup page flip handler */
   1.381 +    vdata->drm_pollfd.fd = vdata->drm_fd;
   1.382 +    vdata->drm_pollfd.events = POLLIN;
   1.383 +    vdata->drm_evctx.version = DRM_EVENT_CONTEXT_VERSION;
   1.384 +    vdata->drm_evctx.page_flip_handler = KMSDRM_FlipHandler;
   1.385 +
   1.386 +#ifdef SDL_INPUT_LINUXEV
   1.387 +    SDL_EVDEV_Init();
   1.388 +#endif
   1.389 +
   1.390 +    KMSDRM_InitMouse(_this);
   1.391 +
   1.392 +cleanup:
   1.393 +    if (encoder != NULL)
   1.394 +        KMSDRM_drmModeFreeEncoder(encoder);
   1.395 +    if (connector != NULL)
   1.396 +        KMSDRM_drmModeFreeConnector(connector);
   1.397 +    if (resources != NULL)
   1.398 +        KMSDRM_drmModeFreeResources(resources);
   1.399 +
   1.400 +    if (ret != 0) {
   1.401 +        /* Error (complete) cleanup */
   1.402 +        SDL_free(data);
   1.403 +        if(vdata->saved_crtc != NULL) {
   1.404 +            KMSDRM_drmModeFreeCrtc(vdata->saved_crtc);
   1.405 +            vdata->saved_crtc = NULL;
   1.406 +        }
   1.407 +        if (vdata->gbm != NULL) {
   1.408 +            KMSDRM_gbm_device_destroy(vdata->gbm);
   1.409 +            vdata->gbm = NULL;
   1.410 +        }
   1.411 +        if (vdata->drm_fd >= 0) {
   1.412 +            close(vdata->drm_fd);
   1.413 +            vdata->drm_fd = -1;
   1.414 +        }
   1.415 +    }
   1.416 +    return ret;
   1.417 +}
   1.418 +
   1.419 +void
   1.420 +KMSDRM_VideoQuit(_THIS)
   1.421 +{
   1.422 +    SDL_VideoData *vdata = ((SDL_VideoData *)_this->driverdata);
   1.423 +
   1.424 +    SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "KMSDRM_VideoQuit()");
   1.425 +
   1.426 +    if (_this->gl_config.driver_loaded) {
   1.427 +        SDL_GL_UnloadLibrary();
   1.428 +    }
   1.429 +
   1.430 +    if(vdata->saved_crtc != NULL) {
   1.431 +        if(vdata->drm_fd > 0 && vdata->saved_conn_id > 0) {
   1.432 +            /* Restore saved CRTC settings */
   1.433 +            drmModeCrtc *crtc = vdata->saved_crtc;
   1.434 +            if(KMSDRM_drmModeSetCrtc(vdata->drm_fd, crtc->crtc_id, crtc->buffer_id,
   1.435 +                                     crtc->x, crtc->y, &vdata->saved_conn_id, 1,
   1.436 +                                     &crtc->mode) != 0) {
   1.437 +                SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not restore original CRTC mode");
   1.438 +            }
   1.439 +        }
   1.440 +        KMSDRM_drmModeFreeCrtc(vdata->saved_crtc);
   1.441 +        vdata->saved_crtc = NULL;
   1.442 +    }
   1.443 +    if (vdata->gbm != NULL) {
   1.444 +        KMSDRM_gbm_device_destroy(vdata->gbm);
   1.445 +        vdata->gbm = NULL;
   1.446 +    }
   1.447 +    if (vdata->drm_fd >= 0) {
   1.448 +        close(vdata->drm_fd);
   1.449 +        SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Closed DRM FD %d", vdata->drm_fd);
   1.450 +        vdata->drm_fd = -1;
   1.451 +    }
   1.452 +#ifdef SDL_INPUT_LINUXEV
   1.453 +    SDL_EVDEV_Quit();
   1.454 +#endif
   1.455 +}
   1.456 +
   1.457 +void
   1.458 +KMSDRM_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   1.459 +{
   1.460 +    /* Only one display mode available, the current one */
   1.461 +    SDL_AddDisplayMode(display, &display->current_mode);
   1.462 +}
   1.463 +
   1.464 +int
   1.465 +KMSDRM_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   1.466 +{
   1.467 +    return 0;
   1.468 +}
   1.469 +
   1.470 +int
   1.471 +KMSDRM_CreateWindow(_THIS, SDL_Window * window)
   1.472 +{
   1.473 +    SDL_WindowData *wdata;
   1.474 +    SDL_VideoDisplay *display;
   1.475 +    SDL_VideoData *vdata = ((SDL_VideoData *)_this->driverdata);
   1.476 +    Uint32 surface_fmt, surface_flags;
   1.477 +
   1.478 +    /* Allocate window internal data */
   1.479 +    wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
   1.480 +    if (wdata == NULL) {
   1.481 +        return SDL_OutOfMemory();
   1.482 +    }
   1.483 +
   1.484 +    wdata->waiting_for_flip = SDL_FALSE;
   1.485 +    display = SDL_GetDisplayForWindow(window);
   1.486 +
   1.487 +    /* Windows have one size for now */
   1.488 +    window->w = display->desktop_mode.w;
   1.489 +    window->h = display->desktop_mode.h;
   1.490 +
   1.491 +    /* Maybe you didn't ask for a fullscreen OpenGL window, but that's what you get */
   1.492 +    window->flags |= (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL);
   1.493 +
   1.494 +    surface_fmt = GBM_BO_FORMAT_XRGB8888;
   1.495 +    surface_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
   1.496 +
   1.497 +    if (!KMSDRM_gbm_device_is_format_supported(vdata->gbm, surface_fmt, surface_flags)) {
   1.498 +        SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "GBM surface format not supported. Trying anyway.");
   1.499 +    }
   1.500 +    wdata->gs = KMSDRM_gbm_surface_create(vdata->gbm, window->w, window->h, surface_fmt, surface_flags);
   1.501 +
   1.502 +#if SDL_VIDEO_OPENGL_EGL
   1.503 +    if (!_this->egl_data) {
   1.504 +        if (SDL_GL_LoadLibrary(NULL) < 0) {
   1.505 +            goto error;
   1.506 +        }
   1.507 +    }
   1.508 +    wdata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) wdata->gs);
   1.509 +
   1.510 +    if (wdata->egl_surface == EGL_NO_SURFACE) {
   1.511 +        SDL_SetError("Could not create EGL window surface");
   1.512 +        goto error;
   1.513 +    }
   1.514 +#endif /* SDL_VIDEO_OPENGL_EGL */
   1.515 +
   1.516 +    /* Setup driver data for this window */
   1.517 +    window->driverdata = wdata;
   1.518 +
   1.519 +    /* One window, it always has focus */
   1.520 +    SDL_SetMouseFocus(window);
   1.521 +    SDL_SetKeyboardFocus(window);
   1.522 +
   1.523 +    /* Window has been successfully created */
   1.524 +    return 0;
   1.525 +
   1.526 +error:
   1.527 +    if (wdata != NULL) {
   1.528 +#if SDL_VIDEO_OPENGL_EGL
   1.529 +        if (wdata->egl_surface != EGL_NO_SURFACE)
   1.530 +            SDL_EGL_DestroySurface(_this, wdata->egl_surface);
   1.531 +#endif /* SDL_VIDEO_OPENGL_EGL */
   1.532 +        if (wdata->gs != NULL)
   1.533 +            KMSDRM_gbm_surface_destroy(wdata->gs);
   1.534 +        SDL_free(wdata);
   1.535 +    }
   1.536 +    return -1;
   1.537 +}
   1.538 +
   1.539 +void
   1.540 +KMSDRM_DestroyWindow(_THIS, SDL_Window * window)
   1.541 +{
   1.542 +    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   1.543 +    if(data) {
   1.544 +        /* Wait for any pending page flips and unlock buffer */
   1.545 +        KMSDRM_WaitPageFlip(_this, data, -1);
   1.546 +        if (data->locked_bo != NULL) {
   1.547 +            KMSDRM_gbm_surface_release_buffer(data->gs, data->locked_bo);
   1.548 +            data->locked_bo = NULL;
   1.549 +        }
   1.550 +#if SDL_VIDEO_OPENGL_EGL
   1.551 +        SDL_EGL_MakeCurrent(_this, EGL_NO_SURFACE, EGL_NO_CONTEXT);
   1.552 +        if (data->egl_surface != EGL_NO_SURFACE) {
   1.553 +            SDL_EGL_DestroySurface(_this, data->egl_surface);
   1.554 +        }
   1.555 +#endif /* SDL_VIDEO_OPENGL_EGL */
   1.556 +        if (data->gs != NULL) {
   1.557 +            KMSDRM_gbm_surface_destroy(data->gs);
   1.558 +            data->gs = NULL;
   1.559 +        }
   1.560 +        SDL_free(data);
   1.561 +        window->driverdata = NULL;
   1.562 +    }
   1.563 +}
   1.564 +
   1.565 +int
   1.566 +KMSDRM_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   1.567 +{
   1.568 +    return -1;
   1.569 +}
   1.570 +
   1.571 +void
   1.572 +KMSDRM_SetWindowTitle(_THIS, SDL_Window * window)
   1.573 +{
   1.574 +}
   1.575 +void
   1.576 +KMSDRM_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
   1.577 +{
   1.578 +}
   1.579 +void
   1.580 +KMSDRM_SetWindowPosition(_THIS, SDL_Window * window)
   1.581 +{
   1.582 +}
   1.583 +void
   1.584 +KMSDRM_SetWindowSize(_THIS, SDL_Window * window)
   1.585 +{
   1.586 +}
   1.587 +void
   1.588 +KMSDRM_ShowWindow(_THIS, SDL_Window * window)
   1.589 +{
   1.590 +}
   1.591 +void
   1.592 +KMSDRM_HideWindow(_THIS, SDL_Window * window)
   1.593 +{
   1.594 +}
   1.595 +void
   1.596 +KMSDRM_RaiseWindow(_THIS, SDL_Window * window)
   1.597 +{
   1.598 +}
   1.599 +void
   1.600 +KMSDRM_MaximizeWindow(_THIS, SDL_Window * window)
   1.601 +{
   1.602 +}
   1.603 +void
   1.604 +KMSDRM_MinimizeWindow(_THIS, SDL_Window * window)
   1.605 +{
   1.606 +}
   1.607 +void
   1.608 +KMSDRM_RestoreWindow(_THIS, SDL_Window * window)
   1.609 +{
   1.610 +}
   1.611 +void
   1.612 +KMSDRM_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
   1.613 +{
   1.614 +
   1.615 +}
   1.616 +
   1.617 +/*****************************************************************************/
   1.618 +/* SDL Window Manager function                                               */
   1.619 +/*****************************************************************************/
   1.620 +SDL_bool
   1.621 +KMSDRM_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info)
   1.622 +{
   1.623 +    if (info->version.major <= SDL_MAJOR_VERSION) {
   1.624 +        return SDL_TRUE;
   1.625 +    } else {
   1.626 +        SDL_SetError("application not compiled with SDL %d.%d\n",
   1.627 +                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   1.628 +        return SDL_FALSE;
   1.629 +    }
   1.630 +
   1.631 +    /* Failed to get window manager information */
   1.632 +    return SDL_FALSE;
   1.633 +}
   1.634 +
   1.635 +#endif /* SDL_VIDEO_DRIVER_KMSDRM */
   1.636 +
   1.637 +/* vi: set ts=4 sw=4 expandtab: */