src/video/raspberry/SDL_rpivideo.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 02 Sep 2017 19:35:32 -0400
changeset 11444 38f181f81476
parent 11383 6cafb5954aaf
child 11694 2ce56475ad57
permissions -rw-r--r--
video: Let video targets optionally decide their default OpenGL configs.

This is necessary because the Raspberry Pi is a strange beast, that believes
it has OpenGL support (through glX?) but generally has GLES2 support.

So when using the raspberry video target, we need to force this to default
to a GLES2 context, or by default SDL_CreateWindow() will fail, deep down
when it tries to load the proper GL library.

Fixes testsprite2 (and basically everything else that wasn't testgles2) when
run on a Raspberry Pi without a X server.

Please note that other targets might also need this filled in, the Raspberry
Pi is just the most prominent and readily-available System-On-A-Chip style
thing on my desk. :)
gabomdq@7753
     1
/*
gabomdq@7753
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
gabomdq@7753
     4
gabomdq@7753
     5
  This software is provided 'as-is', without any express or implied
gabomdq@7753
     6
  warranty.  In no event will the authors be held liable for any damages
gabomdq@7753
     7
  arising from the use of this software.
gabomdq@7753
     8
gabomdq@7753
     9
  Permission is granted to anyone to use this software for any purpose,
gabomdq@7753
    10
  including commercial applications, and to alter it and redistribute it
gabomdq@7753
    11
  freely, subject to the following restrictions:
gabomdq@7753
    12
gabomdq@7753
    13
  1. The origin of this software must not be misrepresented; you must not
gabomdq@7753
    14
     claim that you wrote the original software. If you use this software
gabomdq@7753
    15
     in a product, an acknowledgment in the product documentation would be
gabomdq@7753
    16
     appreciated but is not required.
gabomdq@7753
    17
  2. Altered source versions must be plainly marked as such, and must not be
gabomdq@7753
    18
     misrepresented as being the original software.
gabomdq@7753
    19
  3. This notice may not be removed or altered from any source distribution.
gabomdq@7753
    20
*/
gabomdq@7753
    21
icculus@8093
    22
#include "../../SDL_internal.h"
gabomdq@7753
    23
gabomdq@7753
    24
#if SDL_VIDEO_DRIVER_RPI
gabomdq@7753
    25
gabomdq@7753
    26
/* References
gabomdq@7753
    27
 * http://elinux.org/RPi_VideoCore_APIs
gabomdq@7753
    28
 * https://github.com/raspberrypi/firmware/blob/master/opt/vc/src/hello_pi/hello_triangle/triangle.c
gabomdq@7753
    29
 * http://cgit.freedesktop.org/wayland/weston/tree/src/rpi-renderer.c
gabomdq@7753
    30
 * http://cgit.freedesktop.org/wayland/weston/tree/src/compositor-rpi.c
gabomdq@7753
    31
 */
gabomdq@7753
    32
gabomdq@7753
    33
/* SDL internals */
gabomdq@7753
    34
#include "../SDL_sysvideo.h"
gabomdq@7753
    35
#include "SDL_version.h"
gabomdq@7753
    36
#include "SDL_syswm.h"
gabomdq@7753
    37
#include "SDL_loadso.h"
gabomdq@7753
    38
#include "SDL_events.h"
gabomdq@7753
    39
#include "../../events/SDL_mouse_c.h"
gabomdq@7753
    40
#include "../../events/SDL_keyboard_c.h"
slouken@10553
    41
#include "SDL_hints.h"
gabomdq@7753
    42
gabomdq@7753
    43
#ifdef SDL_INPUT_LINUXEV
slouken@8000
    44
#include "../../core/linux/SDL_evdev.h"
gabomdq@7753
    45
#endif
gabomdq@7753
    46
gabomdq@7753
    47
/* RPI declarations */
gabomdq@7753
    48
#include "SDL_rpivideo.h"
gabomdq@7753
    49
#include "SDL_rpievents_c.h"
gabomdq@7753
    50
#include "SDL_rpiopengles.h"
gabomdq@7753
    51
#include "SDL_rpimouse.h"
gabomdq@7753
    52
gabomdq@7753
    53
static int
gabomdq@7753
    54
RPI_Available(void)
gabomdq@7753
    55
{
gabomdq@7753
    56
    return 1;
gabomdq@7753
    57
}
gabomdq@7753
    58
gabomdq@7753
    59
static void
gabomdq@7753
    60
RPI_Destroy(SDL_VideoDevice * device)
gabomdq@7753
    61
{
icculus@10922
    62
    SDL_free(device->driverdata);
icculus@10922
    63
    SDL_free(device);
gabomdq@7753
    64
}
gabomdq@7753
    65
gabomdq@7753
    66
static SDL_VideoDevice *
gabomdq@7753
    67
RPI_Create()
gabomdq@7753
    68
{
gabomdq@7753
    69
    SDL_VideoDevice *device;
gabomdq@7753
    70
    SDL_VideoData *phdata;
gabomdq@7753
    71
gabomdq@7753
    72
    /* Initialize SDL_VideoDevice structure */
gabomdq@7753
    73
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
gabomdq@7753
    74
    if (device == NULL) {
gabomdq@7753
    75
        SDL_OutOfMemory();
gabomdq@7753
    76
        return NULL;
gabomdq@7753
    77
    }
gabomdq@7753
    78
gabomdq@7753
    79
    /* Initialize internal data */
gabomdq@7753
    80
    phdata = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
gabomdq@7753
    81
    if (phdata == NULL) {
gabomdq@7753
    82
        SDL_OutOfMemory();
gabomdq@7753
    83
        SDL_free(device);
gabomdq@7753
    84
        return NULL;
gabomdq@7753
    85
    }
gabomdq@7753
    86
gabomdq@7753
    87
    device->driverdata = phdata;
gabomdq@7753
    88
philipp@10202
    89
    /* Setup amount of available displays */
gabomdq@7753
    90
    device->num_displays = 0;
gabomdq@7753
    91
gabomdq@7753
    92
    /* Set device free function */
gabomdq@7753
    93
    device->free = RPI_Destroy;
gabomdq@7753
    94
gabomdq@7753
    95
    /* Setup all functions which we can handle */
gabomdq@7753
    96
    device->VideoInit = RPI_VideoInit;
gabomdq@7753
    97
    device->VideoQuit = RPI_VideoQuit;
gabomdq@7753
    98
    device->GetDisplayModes = RPI_GetDisplayModes;
gabomdq@7753
    99
    device->SetDisplayMode = RPI_SetDisplayMode;
slouken@11383
   100
    device->CreateSDLWindow = RPI_CreateWindow;
slouken@11383
   101
    device->CreateSDLWindowFrom = RPI_CreateWindowFrom;
gabomdq@7753
   102
    device->SetWindowTitle = RPI_SetWindowTitle;
gabomdq@7753
   103
    device->SetWindowIcon = RPI_SetWindowIcon;
gabomdq@7753
   104
    device->SetWindowPosition = RPI_SetWindowPosition;
gabomdq@7753
   105
    device->SetWindowSize = RPI_SetWindowSize;
gabomdq@7753
   106
    device->ShowWindow = RPI_ShowWindow;
gabomdq@7753
   107
    device->HideWindow = RPI_HideWindow;
gabomdq@7753
   108
    device->RaiseWindow = RPI_RaiseWindow;
gabomdq@7753
   109
    device->MaximizeWindow = RPI_MaximizeWindow;
gabomdq@7753
   110
    device->MinimizeWindow = RPI_MinimizeWindow;
gabomdq@7753
   111
    device->RestoreWindow = RPI_RestoreWindow;
gabomdq@7753
   112
    device->SetWindowGrab = RPI_SetWindowGrab;
gabomdq@7753
   113
    device->DestroyWindow = RPI_DestroyWindow;
philipp@11098
   114
#if 0
gabomdq@7753
   115
    device->GetWindowWMInfo = RPI_GetWindowWMInfo;
philipp@11098
   116
#endif
gabomdq@7753
   117
    device->GL_LoadLibrary = RPI_GLES_LoadLibrary;
gabomdq@7753
   118
    device->GL_GetProcAddress = RPI_GLES_GetProcAddress;
gabomdq@7753
   119
    device->GL_UnloadLibrary = RPI_GLES_UnloadLibrary;
gabomdq@7753
   120
    device->GL_CreateContext = RPI_GLES_CreateContext;
gabomdq@7753
   121
    device->GL_MakeCurrent = RPI_GLES_MakeCurrent;
gabomdq@7753
   122
    device->GL_SetSwapInterval = RPI_GLES_SetSwapInterval;
gabomdq@7753
   123
    device->GL_GetSwapInterval = RPI_GLES_GetSwapInterval;
gabomdq@7753
   124
    device->GL_SwapWindow = RPI_GLES_SwapWindow;
gabomdq@7753
   125
    device->GL_DeleteContext = RPI_GLES_DeleteContext;
icculus@11444
   126
    device->GL_DefaultProfileConfig = RPI_GLES_DefaultProfileConfig;
gabomdq@7753
   127
gabomdq@7753
   128
    device->PumpEvents = RPI_PumpEvents;
gabomdq@7753
   129
gabomdq@7753
   130
    return device;
gabomdq@7753
   131
}
gabomdq@7753
   132
gabomdq@7753
   133
VideoBootStrap RPI_bootstrap = {
gabomdq@7753
   134
    "RPI",
gabomdq@7753
   135
    "RPI Video Driver",
gabomdq@7753
   136
    RPI_Available,
gabomdq@7753
   137
    RPI_Create
gabomdq@7753
   138
};
gabomdq@7753
   139
gabomdq@7753
   140
/*****************************************************************************/
gabomdq@7753
   141
/* SDL Video and Display initialization/handling functions                   */
gabomdq@7753
   142
/*****************************************************************************/
gabomdq@7753
   143
int
gabomdq@7753
   144
RPI_VideoInit(_THIS)
gabomdq@7753
   145
{
gabomdq@7753
   146
    SDL_VideoDisplay display;
gabomdq@7753
   147
    SDL_DisplayMode current_mode;
icculus@7799
   148
    SDL_DisplayData *data;
gabomdq@7753
   149
    uint32_t w,h;
gabomdq@7753
   150
gabomdq@7753
   151
    /* Initialize BCM Host */
gabomdq@7753
   152
    bcm_host_init();
gabomdq@7753
   153
gabomdq@7753
   154
    SDL_zero(current_mode);
gabomdq@7753
   155
gabomdq@7753
   156
    if (graphics_get_display_size( 0, &w, &h) < 0) {
gabomdq@7753
   157
        return -1;
gabomdq@7753
   158
    }
gabomdq@7753
   159
gabomdq@7753
   160
    current_mode.w = w;
gabomdq@7753
   161
    current_mode.h = h;
gabomdq@7753
   162
    /* FIXME: Is there a way to tell the actual refresh rate? */
gabomdq@7753
   163
    current_mode.refresh_rate = 60;
gabomdq@7753
   164
    /* 32 bpp for default */
gabomdq@7753
   165
    current_mode.format = SDL_PIXELFORMAT_ABGR8888;
gabomdq@7753
   166
gabomdq@7753
   167
    current_mode.driverdata = NULL;
gabomdq@7753
   168
gabomdq@7753
   169
    SDL_zero(display);
gabomdq@7753
   170
    display.desktop_mode = current_mode;
gabomdq@7753
   171
    display.current_mode = current_mode;
gabomdq@7753
   172
gabomdq@7753
   173
    /* Allocate display internal data */
gabomdq@7753
   174
    data = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData));
gabomdq@7753
   175
    if (data == NULL) {
gabomdq@7753
   176
        return SDL_OutOfMemory();
gabomdq@7753
   177
    }
gabomdq@7753
   178
gabomdq@7753
   179
    data->dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
gabomdq@7753
   180
gabomdq@7753
   181
    display.driverdata = data;
gabomdq@7753
   182
gabomdq@7753
   183
    SDL_AddVideoDisplay(&display);
gabomdq@7753
   184
gabomdq@7753
   185
#ifdef SDL_INPUT_LINUXEV    
slouken@10794
   186
    if (SDL_EVDEV_Init() < 0) {
slouken@10794
   187
        return -1;
slouken@10794
   188
    }
gabomdq@7753
   189
#endif    
gabomdq@7753
   190
    
gabomdq@7753
   191
    RPI_InitMouse(_this);
gabomdq@7753
   192
gabomdq@7753
   193
    return 1;
gabomdq@7753
   194
}
gabomdq@7753
   195
gabomdq@7753
   196
void
gabomdq@7753
   197
RPI_VideoQuit(_THIS)
gabomdq@7753
   198
{
gabomdq@7753
   199
#ifdef SDL_INPUT_LINUXEV    
gabomdq@7753
   200
    SDL_EVDEV_Quit();
gabomdq@7753
   201
#endif    
gabomdq@7753
   202
}
gabomdq@7753
   203
gabomdq@7753
   204
void
gabomdq@7753
   205
RPI_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
gabomdq@7753
   206
{
gabomdq@7753
   207
    /* Only one display mode available, the current one */
gabomdq@7753
   208
    SDL_AddDisplayMode(display, &display->current_mode);
gabomdq@7753
   209
}
gabomdq@7753
   210
gabomdq@7753
   211
int
gabomdq@7753
   212
RPI_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
gabomdq@7753
   213
{
gabomdq@7753
   214
    return 0;
gabomdq@7753
   215
}
gabomdq@7753
   216
gabomdq@7753
   217
int
gabomdq@7753
   218
RPI_CreateWindow(_THIS, SDL_Window * window)
gabomdq@7753
   219
{
gabomdq@7753
   220
    SDL_WindowData *wdata;
gabomdq@7753
   221
    SDL_VideoDisplay *display;
gabomdq@7753
   222
    SDL_DisplayData *displaydata;
gabomdq@7753
   223
    VC_RECT_T dst_rect;
gabomdq@7753
   224
    VC_RECT_T src_rect;
gabomdq@7753
   225
    VC_DISPMANX_ALPHA_T         dispman_alpha;
gabomdq@7753
   226
    DISPMANX_UPDATE_HANDLE_T dispman_update;
slouken@10553
   227
    uint32_t layer = SDL_RPI_VIDEOLAYER;
slouken@10553
   228
    const char *env;
gabomdq@7753
   229
gabomdq@7753
   230
    /* Disable alpha, otherwise the app looks composed with whatever dispman is showing (X11, console,etc) */
gabomdq@7753
   231
    dispman_alpha.flags = DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS; 
gabomdq@7753
   232
    dispman_alpha.opacity = 0xFF; 
gabomdq@7753
   233
    dispman_alpha.mask = 0;
gabomdq@7753
   234
gabomdq@7753
   235
    /* Allocate window internal data */
gabomdq@7753
   236
    wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
gabomdq@7753
   237
    if (wdata == NULL) {
gabomdq@7753
   238
        return SDL_OutOfMemory();
gabomdq@7753
   239
    }
gabomdq@7753
   240
    display = SDL_GetDisplayForWindow(window);
gabomdq@7753
   241
    displaydata = (SDL_DisplayData *) display->driverdata;
gabomdq@7753
   242
gabomdq@7753
   243
    /* Windows have one size for now */
gabomdq@7753
   244
    window->w = display->desktop_mode.w;
gabomdq@7753
   245
    window->h = display->desktop_mode.h;
gabomdq@7753
   246
gabomdq@7753
   247
    /* OpenGL ES is the law here, buddy */
gabomdq@7753
   248
    window->flags |= SDL_WINDOW_OPENGL;
gabomdq@7753
   249
gabomdq@7753
   250
    /* Create a dispman element and associate a window to it */
gabomdq@7753
   251
    dst_rect.x = 0;
gabomdq@7753
   252
    dst_rect.y = 0;
gabomdq@7753
   253
    dst_rect.width = window->w;
gabomdq@7753
   254
    dst_rect.height = window->h;
gabomdq@7753
   255
gabomdq@7753
   256
    src_rect.x = 0;
gabomdq@7753
   257
    src_rect.y = 0;
gabomdq@7753
   258
    src_rect.width = window->w << 16;
gabomdq@7753
   259
    src_rect.height = window->h << 16;
gabomdq@7753
   260
slouken@10553
   261
    env = SDL_GetHint(SDL_HINT_RPI_VIDEO_LAYER);
slouken@10553
   262
    if (env) {
slouken@10553
   263
        layer = SDL_atoi(env);
slouken@10553
   264
    }
slouken@10553
   265
gabomdq@7753
   266
    dispman_update = vc_dispmanx_update_start( 0 );
slouken@10553
   267
    wdata->dispman_window.element = vc_dispmanx_element_add (dispman_update,
slouken@10553
   268
                                                             displaydata->dispman_display,
slouken@10553
   269
                                                             layer /* layer */,
slouken@10553
   270
                                                             &dst_rect,
slouken@10553
   271
                                                             0 /*src*/,
slouken@10553
   272
                                                             &src_rect,
slouken@10553
   273
                                                             DISPMANX_PROTECTION_NONE,
slouken@10553
   274
                                                             &dispman_alpha /*alpha*/,
slouken@10553
   275
                                                             0 /*clamp*/,
slouken@10553
   276
                                                             0 /*transform*/);
gabomdq@7753
   277
    wdata->dispman_window.width = window->w;
gabomdq@7753
   278
    wdata->dispman_window.height = window->h;
slouken@10553
   279
    vc_dispmanx_update_submit_sync(dispman_update);
gabomdq@7753
   280
    
gabomdq@7753
   281
    if (!_this->egl_data) {
gabomdq@7753
   282
        if (SDL_GL_LoadLibrary(NULL) < 0) {
gabomdq@7753
   283
            return -1;
gabomdq@7753
   284
        }
gabomdq@7753
   285
    }
gabomdq@7753
   286
    wdata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) &wdata->dispman_window);
gabomdq@7753
   287
gabomdq@7753
   288
    if (wdata->egl_surface == EGL_NO_SURFACE) {
gabomdq@7753
   289
        return SDL_SetError("Could not create GLES window surface");
gabomdq@7753
   290
    }
gabomdq@7753
   291
gabomdq@7753
   292
    /* Setup driver data for this window */
gabomdq@7753
   293
    window->driverdata = wdata;
gabomdq@7753
   294
    
gabomdq@7753
   295
    /* One window, it always has focus */
gabomdq@7753
   296
    SDL_SetMouseFocus(window);
gabomdq@7753
   297
    SDL_SetKeyboardFocus(window);
gabomdq@7753
   298
gabomdq@7753
   299
    /* Window has been successfully created */
gabomdq@7753
   300
    return 0;
gabomdq@7753
   301
}
gabomdq@7753
   302
gabomdq@7986
   303
void
gabomdq@7986
   304
RPI_DestroyWindow(_THIS, SDL_Window * window)
gabomdq@7986
   305
{
icculus@9711
   306
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@9711
   307
    if(data) {
icculus@9711
   308
#if SDL_VIDEO_OPENGL_EGL
gabomdq@7986
   309
        if (data->egl_surface != EGL_NO_SURFACE) {
gabomdq@7986
   310
            SDL_EGL_DestroySurface(_this, data->egl_surface);
gabomdq@7986
   311
        }
icculus@9711
   312
#endif
icculus@9711
   313
        SDL_free(data);
gabomdq@7986
   314
        window->driverdata = NULL;
gabomdq@7986
   315
    }
gabomdq@7986
   316
}
gabomdq@7986
   317
gabomdq@7753
   318
int
gabomdq@7753
   319
RPI_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
gabomdq@7753
   320
{
gabomdq@7753
   321
    return -1;
gabomdq@7753
   322
}
gabomdq@7753
   323
gabomdq@7753
   324
void
gabomdq@7753
   325
RPI_SetWindowTitle(_THIS, SDL_Window * window)
gabomdq@7753
   326
{
gabomdq@7753
   327
}
gabomdq@7753
   328
void
gabomdq@7753
   329
RPI_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
gabomdq@7753
   330
{
gabomdq@7753
   331
}
gabomdq@7753
   332
void
gabomdq@7753
   333
RPI_SetWindowPosition(_THIS, SDL_Window * window)
gabomdq@7753
   334
{
gabomdq@7753
   335
}
gabomdq@7753
   336
void
gabomdq@7753
   337
RPI_SetWindowSize(_THIS, SDL_Window * window)
gabomdq@7753
   338
{
gabomdq@7753
   339
}
gabomdq@7753
   340
void
gabomdq@7753
   341
RPI_ShowWindow(_THIS, SDL_Window * window)
gabomdq@7753
   342
{
gabomdq@7753
   343
}
gabomdq@7753
   344
void
gabomdq@7753
   345
RPI_HideWindow(_THIS, SDL_Window * window)
gabomdq@7753
   346
{
gabomdq@7753
   347
}
gabomdq@7753
   348
void
gabomdq@7753
   349
RPI_RaiseWindow(_THIS, SDL_Window * window)
gabomdq@7753
   350
{
gabomdq@7753
   351
}
gabomdq@7753
   352
void
gabomdq@7753
   353
RPI_MaximizeWindow(_THIS, SDL_Window * window)
gabomdq@7753
   354
{
gabomdq@7753
   355
}
gabomdq@7753
   356
void
gabomdq@7753
   357
RPI_MinimizeWindow(_THIS, SDL_Window * window)
gabomdq@7753
   358
{
gabomdq@7753
   359
}
gabomdq@7753
   360
void
gabomdq@7753
   361
RPI_RestoreWindow(_THIS, SDL_Window * window)
gabomdq@7753
   362
{
gabomdq@7753
   363
}
gabomdq@7753
   364
void
gabomdq@7753
   365
RPI_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
gabomdq@7753
   366
{
gabomdq@7753
   367
gabomdq@7753
   368
}
gabomdq@7753
   369
gabomdq@7753
   370
/*****************************************************************************/
gabomdq@7753
   371
/* SDL Window Manager function                                               */
gabomdq@7753
   372
/*****************************************************************************/
philipp@11098
   373
#if 0
gabomdq@7753
   374
SDL_bool
gabomdq@7753
   375
RPI_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info)
gabomdq@7753
   376
{
gabomdq@7753
   377
    if (info->version.major <= SDL_MAJOR_VERSION) {
gabomdq@7753
   378
        return SDL_TRUE;
gabomdq@7753
   379
    } else {
philipp@10945
   380
        SDL_SetError("application not compiled with SDL %d.%d",
gabomdq@7753
   381
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
gabomdq@7753
   382
        return SDL_FALSE;
gabomdq@7753
   383
    }
gabomdq@7753
   384
gabomdq@7753
   385
    /* Failed to get window manager information */
gabomdq@7753
   386
    return SDL_FALSE;
gabomdq@7753
   387
}
philipp@11098
   388
#endif
gabomdq@7753
   389
gabomdq@7753
   390
#endif /* SDL_VIDEO_DRIVER_RPI */
gabomdq@7753
   391
gabomdq@7753
   392
/* vi: set ts=4 sw=4 expandtab: */