src/video/raspberry/SDL_rpivideo.c
author Gabriel Jacobo <gabomdq@gmail.com>
Sat, 28 Sep 2013 13:28:19 -0300
changeset 7753 e4c38f17bfad
child 7799 a67ccdd0c03a
permissions -rw-r--r--
Raspberry Pi support (also unified UDEV and EVDEV support)
gabomdq@7753
     1
/*
gabomdq@7753
     2
  Simple DirectMedia Layer
gabomdq@7753
     3
  Copyright (C) 1997-2013 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
gabomdq@7753
    22
#include "SDL_config.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"
gabomdq@7753
    41
gabomdq@7753
    42
#ifdef SDL_INPUT_LINUXEV
gabomdq@7753
    43
#include "../../input/evdev/SDL_evdev.h"
gabomdq@7753
    44
#endif
gabomdq@7753
    45
gabomdq@7753
    46
/* RPI declarations */
gabomdq@7753
    47
#include "SDL_rpivideo.h"
gabomdq@7753
    48
#include "SDL_rpievents_c.h"
gabomdq@7753
    49
#include "SDL_rpiopengles.h"
gabomdq@7753
    50
#include "SDL_rpimouse.h"
gabomdq@7753
    51
gabomdq@7753
    52
static int
gabomdq@7753
    53
RPI_Available(void)
gabomdq@7753
    54
{
gabomdq@7753
    55
    return 1;
gabomdq@7753
    56
}
gabomdq@7753
    57
gabomdq@7753
    58
static void
gabomdq@7753
    59
RPI_Destroy(SDL_VideoDevice * device)
gabomdq@7753
    60
{
gabomdq@7753
    61
    /*    SDL_VideoData *phdata = (SDL_VideoData *) device->driverdata; */
gabomdq@7753
    62
gabomdq@7753
    63
    if (device->driverdata != NULL) {
gabomdq@7753
    64
        device->driverdata = NULL;
gabomdq@7753
    65
    }
gabomdq@7753
    66
}
gabomdq@7753
    67
gabomdq@7753
    68
static SDL_VideoDevice *
gabomdq@7753
    69
RPI_Create()
gabomdq@7753
    70
{
gabomdq@7753
    71
    SDL_VideoDevice *device;
gabomdq@7753
    72
    SDL_VideoData *phdata;
gabomdq@7753
    73
gabomdq@7753
    74
    /* Initialize SDL_VideoDevice structure */
gabomdq@7753
    75
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
gabomdq@7753
    76
    if (device == NULL) {
gabomdq@7753
    77
        SDL_OutOfMemory();
gabomdq@7753
    78
        return NULL;
gabomdq@7753
    79
    }
gabomdq@7753
    80
gabomdq@7753
    81
    /* Initialize internal data */
gabomdq@7753
    82
    phdata = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
gabomdq@7753
    83
    if (phdata == NULL) {
gabomdq@7753
    84
        SDL_OutOfMemory();
gabomdq@7753
    85
        SDL_free(device);
gabomdq@7753
    86
        return NULL;
gabomdq@7753
    87
    }
gabomdq@7753
    88
gabomdq@7753
    89
    device->driverdata = phdata;
gabomdq@7753
    90
gabomdq@7753
    91
    /* Setup amount of available displays and current display */
gabomdq@7753
    92
    device->num_displays = 0;
gabomdq@7753
    93
gabomdq@7753
    94
    /* Set device free function */
gabomdq@7753
    95
    device->free = RPI_Destroy;
gabomdq@7753
    96
gabomdq@7753
    97
    /* Setup all functions which we can handle */
gabomdq@7753
    98
    device->VideoInit = RPI_VideoInit;
gabomdq@7753
    99
    device->VideoQuit = RPI_VideoQuit;
gabomdq@7753
   100
    device->GetDisplayModes = RPI_GetDisplayModes;
gabomdq@7753
   101
    device->SetDisplayMode = RPI_SetDisplayMode;
gabomdq@7753
   102
    device->CreateWindow = RPI_CreateWindow;
gabomdq@7753
   103
    device->CreateWindowFrom = RPI_CreateWindowFrom;
gabomdq@7753
   104
    device->SetWindowTitle = RPI_SetWindowTitle;
gabomdq@7753
   105
    device->SetWindowIcon = RPI_SetWindowIcon;
gabomdq@7753
   106
    device->SetWindowPosition = RPI_SetWindowPosition;
gabomdq@7753
   107
    device->SetWindowSize = RPI_SetWindowSize;
gabomdq@7753
   108
    device->ShowWindow = RPI_ShowWindow;
gabomdq@7753
   109
    device->HideWindow = RPI_HideWindow;
gabomdq@7753
   110
    device->RaiseWindow = RPI_RaiseWindow;
gabomdq@7753
   111
    device->MaximizeWindow = RPI_MaximizeWindow;
gabomdq@7753
   112
    device->MinimizeWindow = RPI_MinimizeWindow;
gabomdq@7753
   113
    device->RestoreWindow = RPI_RestoreWindow;
gabomdq@7753
   114
    device->SetWindowGrab = RPI_SetWindowGrab;
gabomdq@7753
   115
    device->DestroyWindow = RPI_DestroyWindow;
gabomdq@7753
   116
    device->GetWindowWMInfo = RPI_GetWindowWMInfo;
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;
gabomdq@7753
   126
gabomdq@7753
   127
    device->PumpEvents = RPI_PumpEvents;
gabomdq@7753
   128
gabomdq@7753
   129
    return device;
gabomdq@7753
   130
}
gabomdq@7753
   131
gabomdq@7753
   132
VideoBootStrap RPI_bootstrap = {
gabomdq@7753
   133
    "RPI",
gabomdq@7753
   134
    "RPI Video Driver",
gabomdq@7753
   135
    RPI_Available,
gabomdq@7753
   136
    RPI_Create
gabomdq@7753
   137
};
gabomdq@7753
   138
gabomdq@7753
   139
/*****************************************************************************/
gabomdq@7753
   140
/* SDL Video and Display initialization/handling functions                   */
gabomdq@7753
   141
/*****************************************************************************/
gabomdq@7753
   142
int
gabomdq@7753
   143
RPI_VideoInit(_THIS)
gabomdq@7753
   144
{
gabomdq@7753
   145
    SDL_VideoDisplay display;
gabomdq@7753
   146
    SDL_DisplayMode current_mode;
gabomdq@7753
   147
    uint32_t w,h;
gabomdq@7753
   148
gabomdq@7753
   149
    /* Initialize BCM Host */
gabomdq@7753
   150
    bcm_host_init();
gabomdq@7753
   151
gabomdq@7753
   152
    SDL_zero(current_mode);
gabomdq@7753
   153
gabomdq@7753
   154
    if (graphics_get_display_size( 0, &w, &h) < 0) {
gabomdq@7753
   155
        return -1;
gabomdq@7753
   156
    }
gabomdq@7753
   157
gabomdq@7753
   158
    current_mode.w = w;
gabomdq@7753
   159
    current_mode.h = h;
gabomdq@7753
   160
    /* FIXME: Is there a way to tell the actual refresh rate? */
gabomdq@7753
   161
    current_mode.refresh_rate = 60;
gabomdq@7753
   162
    /* 32 bpp for default */
gabomdq@7753
   163
    current_mode.format = SDL_PIXELFORMAT_ABGR8888;
gabomdq@7753
   164
gabomdq@7753
   165
    current_mode.driverdata = NULL;
gabomdq@7753
   166
gabomdq@7753
   167
    SDL_zero(display);
gabomdq@7753
   168
    display.desktop_mode = current_mode;
gabomdq@7753
   169
    display.current_mode = current_mode;
gabomdq@7753
   170
gabomdq@7753
   171
    SDL_DisplayData *data;
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    
gabomdq@7753
   186
    SDL_EVDEV_Init();
gabomdq@7753
   187
#endif    
gabomdq@7753
   188
    
gabomdq@7753
   189
    RPI_InitMouse(_this);
gabomdq@7753
   190
gabomdq@7753
   191
    return 1;
gabomdq@7753
   192
}
gabomdq@7753
   193
gabomdq@7753
   194
void
gabomdq@7753
   195
RPI_VideoQuit(_THIS)
gabomdq@7753
   196
{
gabomdq@7753
   197
#ifdef SDL_INPUT_LINUXEV    
gabomdq@7753
   198
    SDL_EVDEV_Quit();
gabomdq@7753
   199
#endif    
gabomdq@7753
   200
}
gabomdq@7753
   201
gabomdq@7753
   202
void
gabomdq@7753
   203
RPI_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
gabomdq@7753
   204
{
gabomdq@7753
   205
    /* Only one display mode available, the current one */
gabomdq@7753
   206
    SDL_AddDisplayMode(display, &display->current_mode);
gabomdq@7753
   207
}
gabomdq@7753
   208
gabomdq@7753
   209
int
gabomdq@7753
   210
RPI_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
gabomdq@7753
   211
{
gabomdq@7753
   212
    return 0;
gabomdq@7753
   213
}
gabomdq@7753
   214
gabomdq@7753
   215
int
gabomdq@7753
   216
RPI_CreateWindow(_THIS, SDL_Window * window)
gabomdq@7753
   217
{
gabomdq@7753
   218
    SDL_WindowData *wdata;
gabomdq@7753
   219
    SDL_VideoDisplay *display;
gabomdq@7753
   220
    SDL_DisplayData *displaydata;
gabomdq@7753
   221
    VC_RECT_T dst_rect;
gabomdq@7753
   222
    VC_RECT_T src_rect;
gabomdq@7753
   223
    VC_DISPMANX_ALPHA_T         dispman_alpha;
gabomdq@7753
   224
    DISPMANX_UPDATE_HANDLE_T dispman_update;
gabomdq@7753
   225
gabomdq@7753
   226
    /* Disable alpha, otherwise the app looks composed with whatever dispman is showing (X11, console,etc) */
gabomdq@7753
   227
    dispman_alpha.flags = DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS; 
gabomdq@7753
   228
    dispman_alpha.opacity = 0xFF; 
gabomdq@7753
   229
    dispman_alpha.mask = 0;
gabomdq@7753
   230
gabomdq@7753
   231
    /* Allocate window internal data */
gabomdq@7753
   232
    wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
gabomdq@7753
   233
    if (wdata == NULL) {
gabomdq@7753
   234
        return SDL_OutOfMemory();
gabomdq@7753
   235
    }
gabomdq@7753
   236
    display = SDL_GetDisplayForWindow(window);
gabomdq@7753
   237
    displaydata = (SDL_DisplayData *) display->driverdata;
gabomdq@7753
   238
gabomdq@7753
   239
    /* Windows have one size for now */
gabomdq@7753
   240
    window->w = display->desktop_mode.w;
gabomdq@7753
   241
    window->h = display->desktop_mode.h;
gabomdq@7753
   242
gabomdq@7753
   243
    /* OpenGL ES is the law here, buddy */
gabomdq@7753
   244
    window->flags |= SDL_WINDOW_OPENGL;
gabomdq@7753
   245
gabomdq@7753
   246
    /* Create a dispman element and associate a window to it */
gabomdq@7753
   247
    dst_rect.x = 0;
gabomdq@7753
   248
    dst_rect.y = 0;
gabomdq@7753
   249
    dst_rect.width = window->w;
gabomdq@7753
   250
    dst_rect.height = window->h;
gabomdq@7753
   251
gabomdq@7753
   252
    src_rect.x = 0;
gabomdq@7753
   253
    src_rect.y = 0;
gabomdq@7753
   254
    src_rect.width = window->w << 16;
gabomdq@7753
   255
    src_rect.height = window->h << 16;
gabomdq@7753
   256
gabomdq@7753
   257
    dispman_update = vc_dispmanx_update_start( 0 );
gabomdq@7753
   258
    wdata->dispman_window.element = vc_dispmanx_element_add ( dispman_update, displaydata->dispman_display, SDL_RPI_VIDEOLAYER /* layer */, &dst_rect, 0/*src*/, &src_rect, DISPMANX_PROTECTION_NONE, &dispman_alpha /*alpha*/, 0/*clamp*/, 0/*transform*/);
gabomdq@7753
   259
    wdata->dispman_window.width = window->w;
gabomdq@7753
   260
    wdata->dispman_window.height = window->h;
gabomdq@7753
   261
    vc_dispmanx_update_submit_sync( dispman_update );
gabomdq@7753
   262
    
gabomdq@7753
   263
    if (!_this->egl_data) {
gabomdq@7753
   264
        if (SDL_GL_LoadLibrary(NULL) < 0) {
gabomdq@7753
   265
            return -1;
gabomdq@7753
   266
        }
gabomdq@7753
   267
    }
gabomdq@7753
   268
    wdata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) &wdata->dispman_window);
gabomdq@7753
   269
gabomdq@7753
   270
    if (wdata->egl_surface == EGL_NO_SURFACE) {
gabomdq@7753
   271
        return SDL_SetError("Could not create GLES window surface");
gabomdq@7753
   272
    }
gabomdq@7753
   273
gabomdq@7753
   274
    /* Setup driver data for this window */
gabomdq@7753
   275
    window->driverdata = wdata;
gabomdq@7753
   276
    
gabomdq@7753
   277
    /* One window, it always has focus */
gabomdq@7753
   278
    SDL_SetMouseFocus(window);
gabomdq@7753
   279
    SDL_SetKeyboardFocus(window);
gabomdq@7753
   280
gabomdq@7753
   281
    /* Window has been successfully created */
gabomdq@7753
   282
    return 0;
gabomdq@7753
   283
}
gabomdq@7753
   284
gabomdq@7753
   285
int
gabomdq@7753
   286
RPI_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
gabomdq@7753
   287
{
gabomdq@7753
   288
    return -1;
gabomdq@7753
   289
}
gabomdq@7753
   290
gabomdq@7753
   291
void
gabomdq@7753
   292
RPI_SetWindowTitle(_THIS, SDL_Window * window)
gabomdq@7753
   293
{
gabomdq@7753
   294
}
gabomdq@7753
   295
void
gabomdq@7753
   296
RPI_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
gabomdq@7753
   297
{
gabomdq@7753
   298
}
gabomdq@7753
   299
void
gabomdq@7753
   300
RPI_SetWindowPosition(_THIS, SDL_Window * window)
gabomdq@7753
   301
{
gabomdq@7753
   302
}
gabomdq@7753
   303
void
gabomdq@7753
   304
RPI_SetWindowSize(_THIS, SDL_Window * window)
gabomdq@7753
   305
{
gabomdq@7753
   306
}
gabomdq@7753
   307
void
gabomdq@7753
   308
RPI_ShowWindow(_THIS, SDL_Window * window)
gabomdq@7753
   309
{
gabomdq@7753
   310
}
gabomdq@7753
   311
void
gabomdq@7753
   312
RPI_HideWindow(_THIS, SDL_Window * window)
gabomdq@7753
   313
{
gabomdq@7753
   314
}
gabomdq@7753
   315
void
gabomdq@7753
   316
RPI_RaiseWindow(_THIS, SDL_Window * window)
gabomdq@7753
   317
{
gabomdq@7753
   318
}
gabomdq@7753
   319
void
gabomdq@7753
   320
RPI_MaximizeWindow(_THIS, SDL_Window * window)
gabomdq@7753
   321
{
gabomdq@7753
   322
}
gabomdq@7753
   323
void
gabomdq@7753
   324
RPI_MinimizeWindow(_THIS, SDL_Window * window)
gabomdq@7753
   325
{
gabomdq@7753
   326
}
gabomdq@7753
   327
void
gabomdq@7753
   328
RPI_RestoreWindow(_THIS, SDL_Window * window)
gabomdq@7753
   329
{
gabomdq@7753
   330
}
gabomdq@7753
   331
void
gabomdq@7753
   332
RPI_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
gabomdq@7753
   333
{
gabomdq@7753
   334
gabomdq@7753
   335
}
gabomdq@7753
   336
void
gabomdq@7753
   337
RPI_DestroyWindow(_THIS, SDL_Window * window)
gabomdq@7753
   338
{
gabomdq@7753
   339
}
gabomdq@7753
   340
gabomdq@7753
   341
/*****************************************************************************/
gabomdq@7753
   342
/* SDL Window Manager function                                               */
gabomdq@7753
   343
/*****************************************************************************/
gabomdq@7753
   344
SDL_bool
gabomdq@7753
   345
RPI_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info)
gabomdq@7753
   346
{
gabomdq@7753
   347
    if (info->version.major <= SDL_MAJOR_VERSION) {
gabomdq@7753
   348
        return SDL_TRUE;
gabomdq@7753
   349
    } else {
gabomdq@7753
   350
        SDL_SetError("application not compiled with SDL %d.%d\n",
gabomdq@7753
   351
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
gabomdq@7753
   352
        return SDL_FALSE;
gabomdq@7753
   353
    }
gabomdq@7753
   354
gabomdq@7753
   355
    /* Failed to get window manager information */
gabomdq@7753
   356
    return SDL_FALSE;
gabomdq@7753
   357
}
gabomdq@7753
   358
gabomdq@7753
   359
#endif /* SDL_VIDEO_DRIVER_RPI */
gabomdq@7753
   360
gabomdq@7753
   361
/* vi: set ts=4 sw=4 expandtab: */