src/video/raspberry/SDL_rpivideo.c
author Sam Lantinga
Sat, 02 Jan 2016 10:10:34 -0800
changeset 9998 f67cf37e9cd4
parent 9711 0a329b2a297f
child 10202 16c283d1dcca
permissions -rw-r--r--
Updated copyright to 2016
gabomdq@7753
     1
/*
gabomdq@7753
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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"
gabomdq@7753
    41
gabomdq@7753
    42
#ifdef SDL_INPUT_LINUXEV
slouken@8000
    43
#include "../../core/linux/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;
icculus@7799
   147
    SDL_DisplayData *data;
gabomdq@7753
   148
    uint32_t w,h;
gabomdq@7753
   149
gabomdq@7753
   150
    /* Initialize BCM Host */
gabomdq@7753
   151
    bcm_host_init();
gabomdq@7753
   152
gabomdq@7753
   153
    SDL_zero(current_mode);
gabomdq@7753
   154
gabomdq@7753
   155
    if (graphics_get_display_size( 0, &w, &h) < 0) {
gabomdq@7753
   156
        return -1;
gabomdq@7753
   157
    }
gabomdq@7753
   158
gabomdq@7753
   159
    current_mode.w = w;
gabomdq@7753
   160
    current_mode.h = h;
gabomdq@7753
   161
    /* FIXME: Is there a way to tell the actual refresh rate? */
gabomdq@7753
   162
    current_mode.refresh_rate = 60;
gabomdq@7753
   163
    /* 32 bpp for default */
gabomdq@7753
   164
    current_mode.format = SDL_PIXELFORMAT_ABGR8888;
gabomdq@7753
   165
gabomdq@7753
   166
    current_mode.driverdata = NULL;
gabomdq@7753
   167
gabomdq@7753
   168
    SDL_zero(display);
gabomdq@7753
   169
    display.desktop_mode = current_mode;
gabomdq@7753
   170
    display.current_mode = current_mode;
gabomdq@7753
   171
gabomdq@7753
   172
    /* Allocate display internal data */
gabomdq@7753
   173
    data = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData));
gabomdq@7753
   174
    if (data == NULL) {
gabomdq@7753
   175
        return SDL_OutOfMemory();
gabomdq@7753
   176
    }
gabomdq@7753
   177
gabomdq@7753
   178
    data->dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
gabomdq@7753
   179
gabomdq@7753
   180
    display.driverdata = data;
gabomdq@7753
   181
gabomdq@7753
   182
    SDL_AddVideoDisplay(&display);
gabomdq@7753
   183
gabomdq@7753
   184
#ifdef SDL_INPUT_LINUXEV    
gabomdq@7753
   185
    SDL_EVDEV_Init();
gabomdq@7753
   186
#endif    
gabomdq@7753
   187
    
gabomdq@7753
   188
    RPI_InitMouse(_this);
gabomdq@7753
   189
gabomdq@7753
   190
    return 1;
gabomdq@7753
   191
}
gabomdq@7753
   192
gabomdq@7753
   193
void
gabomdq@7753
   194
RPI_VideoQuit(_THIS)
gabomdq@7753
   195
{
gabomdq@7753
   196
#ifdef SDL_INPUT_LINUXEV    
gabomdq@7753
   197
    SDL_EVDEV_Quit();
gabomdq@7753
   198
#endif    
gabomdq@7753
   199
}
gabomdq@7753
   200
gabomdq@7753
   201
void
gabomdq@7753
   202
RPI_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
gabomdq@7753
   203
{
gabomdq@7753
   204
    /* Only one display mode available, the current one */
gabomdq@7753
   205
    SDL_AddDisplayMode(display, &display->current_mode);
gabomdq@7753
   206
}
gabomdq@7753
   207
gabomdq@7753
   208
int
gabomdq@7753
   209
RPI_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
gabomdq@7753
   210
{
gabomdq@7753
   211
    return 0;
gabomdq@7753
   212
}
gabomdq@7753
   213
gabomdq@7753
   214
int
gabomdq@7753
   215
RPI_CreateWindow(_THIS, SDL_Window * window)
gabomdq@7753
   216
{
gabomdq@7753
   217
    SDL_WindowData *wdata;
gabomdq@7753
   218
    SDL_VideoDisplay *display;
gabomdq@7753
   219
    SDL_DisplayData *displaydata;
gabomdq@7753
   220
    VC_RECT_T dst_rect;
gabomdq@7753
   221
    VC_RECT_T src_rect;
gabomdq@7753
   222
    VC_DISPMANX_ALPHA_T         dispman_alpha;
gabomdq@7753
   223
    DISPMANX_UPDATE_HANDLE_T dispman_update;
gabomdq@7753
   224
gabomdq@7753
   225
    /* Disable alpha, otherwise the app looks composed with whatever dispman is showing (X11, console,etc) */
gabomdq@7753
   226
    dispman_alpha.flags = DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS; 
gabomdq@7753
   227
    dispman_alpha.opacity = 0xFF; 
gabomdq@7753
   228
    dispman_alpha.mask = 0;
gabomdq@7753
   229
gabomdq@7753
   230
    /* Allocate window internal data */
gabomdq@7753
   231
    wdata = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
gabomdq@7753
   232
    if (wdata == NULL) {
gabomdq@7753
   233
        return SDL_OutOfMemory();
gabomdq@7753
   234
    }
gabomdq@7753
   235
    display = SDL_GetDisplayForWindow(window);
gabomdq@7753
   236
    displaydata = (SDL_DisplayData *) display->driverdata;
gabomdq@7753
   237
gabomdq@7753
   238
    /* Windows have one size for now */
gabomdq@7753
   239
    window->w = display->desktop_mode.w;
gabomdq@7753
   240
    window->h = display->desktop_mode.h;
gabomdq@7753
   241
gabomdq@7753
   242
    /* OpenGL ES is the law here, buddy */
gabomdq@7753
   243
    window->flags |= SDL_WINDOW_OPENGL;
gabomdq@7753
   244
gabomdq@7753
   245
    /* Create a dispman element and associate a window to it */
gabomdq@7753
   246
    dst_rect.x = 0;
gabomdq@7753
   247
    dst_rect.y = 0;
gabomdq@7753
   248
    dst_rect.width = window->w;
gabomdq@7753
   249
    dst_rect.height = window->h;
gabomdq@7753
   250
gabomdq@7753
   251
    src_rect.x = 0;
gabomdq@7753
   252
    src_rect.y = 0;
gabomdq@7753
   253
    src_rect.width = window->w << 16;
gabomdq@7753
   254
    src_rect.height = window->h << 16;
gabomdq@7753
   255
gabomdq@7753
   256
    dispman_update = vc_dispmanx_update_start( 0 );
gabomdq@7753
   257
    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
   258
    wdata->dispman_window.width = window->w;
gabomdq@7753
   259
    wdata->dispman_window.height = window->h;
gabomdq@7753
   260
    vc_dispmanx_update_submit_sync( dispman_update );
gabomdq@7753
   261
    
gabomdq@7753
   262
    if (!_this->egl_data) {
gabomdq@7753
   263
        if (SDL_GL_LoadLibrary(NULL) < 0) {
gabomdq@7753
   264
            return -1;
gabomdq@7753
   265
        }
gabomdq@7753
   266
    }
gabomdq@7753
   267
    wdata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) &wdata->dispman_window);
gabomdq@7753
   268
gabomdq@7753
   269
    if (wdata->egl_surface == EGL_NO_SURFACE) {
gabomdq@7753
   270
        return SDL_SetError("Could not create GLES window surface");
gabomdq@7753
   271
    }
gabomdq@7753
   272
gabomdq@7753
   273
    /* Setup driver data for this window */
gabomdq@7753
   274
    window->driverdata = wdata;
gabomdq@7753
   275
    
gabomdq@7753
   276
    /* One window, it always has focus */
gabomdq@7753
   277
    SDL_SetMouseFocus(window);
gabomdq@7753
   278
    SDL_SetKeyboardFocus(window);
gabomdq@7753
   279
gabomdq@7753
   280
    /* Window has been successfully created */
gabomdq@7753
   281
    return 0;
gabomdq@7753
   282
}
gabomdq@7753
   283
gabomdq@7986
   284
void
gabomdq@7986
   285
RPI_DestroyWindow(_THIS, SDL_Window * window)
gabomdq@7986
   286
{
icculus@9711
   287
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@9711
   288
    if(data) {
icculus@9711
   289
#if SDL_VIDEO_OPENGL_EGL
gabomdq@7986
   290
        if (data->egl_surface != EGL_NO_SURFACE) {
gabomdq@7986
   291
            SDL_EGL_DestroySurface(_this, data->egl_surface);
gabomdq@7986
   292
        }
icculus@9711
   293
#endif
icculus@9711
   294
        SDL_free(data);
gabomdq@7986
   295
        window->driverdata = NULL;
gabomdq@7986
   296
    }
gabomdq@7986
   297
}
gabomdq@7986
   298
gabomdq@7753
   299
int
gabomdq@7753
   300
RPI_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
gabomdq@7753
   301
{
gabomdq@7753
   302
    return -1;
gabomdq@7753
   303
}
gabomdq@7753
   304
gabomdq@7753
   305
void
gabomdq@7753
   306
RPI_SetWindowTitle(_THIS, SDL_Window * window)
gabomdq@7753
   307
{
gabomdq@7753
   308
}
gabomdq@7753
   309
void
gabomdq@7753
   310
RPI_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
gabomdq@7753
   311
{
gabomdq@7753
   312
}
gabomdq@7753
   313
void
gabomdq@7753
   314
RPI_SetWindowPosition(_THIS, SDL_Window * window)
gabomdq@7753
   315
{
gabomdq@7753
   316
}
gabomdq@7753
   317
void
gabomdq@7753
   318
RPI_SetWindowSize(_THIS, SDL_Window * window)
gabomdq@7753
   319
{
gabomdq@7753
   320
}
gabomdq@7753
   321
void
gabomdq@7753
   322
RPI_ShowWindow(_THIS, SDL_Window * window)
gabomdq@7753
   323
{
gabomdq@7753
   324
}
gabomdq@7753
   325
void
gabomdq@7753
   326
RPI_HideWindow(_THIS, SDL_Window * window)
gabomdq@7753
   327
{
gabomdq@7753
   328
}
gabomdq@7753
   329
void
gabomdq@7753
   330
RPI_RaiseWindow(_THIS, SDL_Window * window)
gabomdq@7753
   331
{
gabomdq@7753
   332
}
gabomdq@7753
   333
void
gabomdq@7753
   334
RPI_MaximizeWindow(_THIS, SDL_Window * window)
gabomdq@7753
   335
{
gabomdq@7753
   336
}
gabomdq@7753
   337
void
gabomdq@7753
   338
RPI_MinimizeWindow(_THIS, SDL_Window * window)
gabomdq@7753
   339
{
gabomdq@7753
   340
}
gabomdq@7753
   341
void
gabomdq@7753
   342
RPI_RestoreWindow(_THIS, SDL_Window * window)
gabomdq@7753
   343
{
gabomdq@7753
   344
}
gabomdq@7753
   345
void
gabomdq@7753
   346
RPI_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
gabomdq@7753
   347
{
gabomdq@7753
   348
gabomdq@7753
   349
}
gabomdq@7753
   350
gabomdq@7753
   351
/*****************************************************************************/
gabomdq@7753
   352
/* SDL Window Manager function                                               */
gabomdq@7753
   353
/*****************************************************************************/
gabomdq@7753
   354
SDL_bool
gabomdq@7753
   355
RPI_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info)
gabomdq@7753
   356
{
gabomdq@7753
   357
    if (info->version.major <= SDL_MAJOR_VERSION) {
gabomdq@7753
   358
        return SDL_TRUE;
gabomdq@7753
   359
    } else {
gabomdq@7753
   360
        SDL_SetError("application not compiled with SDL %d.%d\n",
gabomdq@7753
   361
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
gabomdq@7753
   362
        return SDL_FALSE;
gabomdq@7753
   363
    }
gabomdq@7753
   364
gabomdq@7753
   365
    /* Failed to get window manager information */
gabomdq@7753
   366
    return SDL_FALSE;
gabomdq@7753
   367
}
gabomdq@7753
   368
gabomdq@7753
   369
#endif /* SDL_VIDEO_DRIVER_RPI */
gabomdq@7753
   370
gabomdq@7753
   371
/* vi: set ts=4 sw=4 expandtab: */