src/video/vivante/SDL_vivantevideo.c
changeset 9164 494876610c49
child 9169 70e5da67cf9d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/vivante/SDL_vivantevideo.c	Wed Oct 15 09:18:17 2014 -0700
     1.3 @@ -0,0 +1,397 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2014 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_VIVANTE
    1.27 +
    1.28 +/* SDL internals */
    1.29 +#include "../SDL_sysvideo.h"
    1.30 +#include "SDL_version.h"
    1.31 +#include "SDL_syswm.h"
    1.32 +#include "SDL_loadso.h"
    1.33 +#include "SDL_events.h"
    1.34 +#include "../../events/SDL_events_c.h"
    1.35 +
    1.36 +#ifdef SDL_INPUT_LINUXEV
    1.37 +#include "../../core/linux/SDL_evdev.h"
    1.38 +#endif
    1.39 +
    1.40 +#include "SDL_vivantevideo.h"
    1.41 +#include "SDL_vivanteplatform.h"
    1.42 +#include "SDL_vivanteopengles.h"
    1.43 +
    1.44 +
    1.45 +static int
    1.46 +VIVANTE_Available(void)
    1.47 +{
    1.48 +    return 1;
    1.49 +}
    1.50 +
    1.51 +static void
    1.52 +VIVANTE_Destroy(SDL_VideoDevice * device)
    1.53 +{
    1.54 +    if (device->driverdata != NULL) {
    1.55 +        SDL_free(device->driverdata);
    1.56 +        device->driverdata = NULL;
    1.57 +    }
    1.58 +}
    1.59 +
    1.60 +static SDL_VideoDevice *
    1.61 +VIVANTE_Create()
    1.62 +{
    1.63 +    SDL_VideoDevice *device;
    1.64 +    SDL_VideoData *data;
    1.65 +
    1.66 +    /* Initialize SDL_VideoDevice structure */
    1.67 +    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    1.68 +    if (device == NULL) {
    1.69 +        SDL_OutOfMemory();
    1.70 +        return NULL;
    1.71 +    }
    1.72 +
    1.73 +    /* Initialize internal data */
    1.74 +    data = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
    1.75 +    if (data == NULL) {
    1.76 +        SDL_OutOfMemory();
    1.77 +        SDL_free(device);
    1.78 +        return NULL;
    1.79 +    }
    1.80 +
    1.81 +    device->driverdata = data;
    1.82 +
    1.83 +    /* Setup amount of available displays and current display */
    1.84 +    device->num_displays = 0;
    1.85 +
    1.86 +    /* Set device free function */
    1.87 +    device->free = VIVANTE_Destroy;
    1.88 +
    1.89 +    /* Setup all functions which we can handle */
    1.90 +    device->VideoInit = VIVANTE_VideoInit;
    1.91 +    device->VideoQuit = VIVANTE_VideoQuit;
    1.92 +    device->GetDisplayModes = VIVANTE_GetDisplayModes;
    1.93 +    device->SetDisplayMode = VIVANTE_SetDisplayMode;
    1.94 +    device->CreateWindow = VIVANTE_CreateWindow;
    1.95 +    device->SetWindowTitle = VIVANTE_SetWindowTitle;
    1.96 +    device->SetWindowPosition = VIVANTE_SetWindowPosition;
    1.97 +    device->SetWindowSize = VIVANTE_SetWindowSize;
    1.98 +    device->ShowWindow = VIVANTE_ShowWindow;
    1.99 +    device->HideWindow = VIVANTE_HideWindow;
   1.100 +    device->DestroyWindow = VIVANTE_DestroyWindow;
   1.101 +    device->GetWindowWMInfo = VIVANTE_GetWindowWMInfo;
   1.102 +
   1.103 +    device->GL_LoadLibrary = VIVANTE_GLES_LoadLibrary;
   1.104 +    device->GL_GetProcAddress = VIVANTE_GLES_GetProcAddress;
   1.105 +    device->GL_UnloadLibrary = VIVANTE_GLES_UnloadLibrary;
   1.106 +    device->GL_CreateContext = VIVANTE_GLES_CreateContext;
   1.107 +    device->GL_MakeCurrent = VIVANTE_GLES_MakeCurrent;
   1.108 +    device->GL_SetSwapInterval = VIVANTE_GLES_SetSwapInterval;
   1.109 +    device->GL_GetSwapInterval = VIVANTE_GLES_GetSwapInterval;
   1.110 +    device->GL_SwapWindow = VIVANTE_GLES_SwapWindow;
   1.111 +    device->GL_DeleteContext = VIVANTE_GLES_DeleteContext;
   1.112 +
   1.113 +    device->PumpEvents = VIVANTE_PumpEvents;
   1.114 +
   1.115 +    return device;
   1.116 +}
   1.117 +
   1.118 +VideoBootStrap VIVANTE_bootstrap = {
   1.119 +    "vivante",
   1.120 +    "Vivante EGL Video Driver",
   1.121 +    VIVANTE_Available,
   1.122 +    VIVANTE_Create
   1.123 +};
   1.124 +
   1.125 +/*****************************************************************************/
   1.126 +/* SDL Video and Display initialization/handling functions                   */
   1.127 +/*****************************************************************************/
   1.128 +
   1.129 +static int
   1.130 +VIVANTE_AddVideoDisplays(_THIS)
   1.131 +{
   1.132 +    SDL_VideoData *videodata = _this->driverdata;
   1.133 +    SDL_VideoDisplay display;
   1.134 +    SDL_DisplayMode current_mode;
   1.135 +    SDL_DisplayData *data;
   1.136 +    int pitch = 0, bpp = 0;
   1.137 +    unsigned long pixels = 0;
   1.138 +
   1.139 +    data = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData));
   1.140 +    if (data == NULL) {
   1.141 +        return SDL_OutOfMemory();
   1.142 +    }
   1.143 +
   1.144 +    SDL_zero(current_mode);
   1.145 +#if SDL_VIDEO_DRIVER_VIVANTE_VDK
   1.146 +    data->native_display = vdkGetDisplay(videodata->vdk_private);
   1.147 +
   1.148 +    vdkGetDisplayInfo(data->native_display, &current_mode.w, &current_mode.h, &pixels, &pitch, &bpp);
   1.149 +#else
   1.150 +    data->native_display = videodata->fbGetDisplayByIndex(0);
   1.151 +
   1.152 +    videodata->fbGetDisplayInfo(data->native_display, &current_mode.w, &current_mode.h, &pixels, &pitch, &bpp);
   1.153 +#endif /* SDL_VIDEO_DRIVER_VIVANTE_VDK */
   1.154 +
   1.155 +    switch (bpp)
   1.156 +    {
   1.157 +    default: /* Is another format used? */
   1.158 +    case 16:
   1.159 +        current_mode.format = SDL_PIXELFORMAT_RGB565;
   1.160 +        break;
   1.161 +    }
   1.162 +    /* FIXME: How do we query refresh rate? */
   1.163 +    current_mode.refresh_rate = 60;
   1.164 +
   1.165 +    SDL_zero(display);
   1.166 +    display.desktop_mode = current_mode;
   1.167 +    display.current_mode = current_mode;
   1.168 +    display.driverdata = data;
   1.169 +    SDL_AddVideoDisplay(&display);
   1.170 +    return 0;
   1.171 +}
   1.172 +
   1.173 +int
   1.174 +VIVANTE_VideoInit(_THIS)
   1.175 +{
   1.176 +    SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   1.177 +
   1.178 +#if SDL_VIDEO_DRIVER_VIVANTE_VDK
   1.179 +    videodata->vdk_private = vdkInitialize();
   1.180 +    if (!videodata->vdk_private) {
   1.181 +        return SDL_SetError("vdkInitialize() failed");
   1.182 +    }
   1.183 +#else
   1.184 +    videodata->egl_handle = SDL_LoadObject("libEGL.so.1");
   1.185 +    if (!videodata->egl_handle) {
   1.186 +        videodata->egl_handle = SDL_LoadObject("libEGL.so");
   1.187 +        if (!videodata->egl_handle) {
   1.188 +            return -1;
   1.189 +        }
   1.190 +    }
   1.191 +#define LOAD_FUNC(NAME) \
   1.192 +    videodata->NAME = SDL_LoadFunction(videodata->egl_handle, #NAME); \
   1.193 +    if (!videodata->NAME) return -1;
   1.194 +
   1.195 +    LOAD_FUNC(fbGetDisplay);
   1.196 +    LOAD_FUNC(fbGetDisplayByIndex);
   1.197 +    LOAD_FUNC(fbGetDisplayGeometry);
   1.198 +    LOAD_FUNC(fbGetDisplayInfo);
   1.199 +    LOAD_FUNC(fbDestroyDisplay);
   1.200 +    LOAD_FUNC(fbCreateWindow);
   1.201 +    LOAD_FUNC(fbGetWindowGeometry);
   1.202 +    LOAD_FUNC(fbGetWindowInfo);
   1.203 +    LOAD_FUNC(fbDestroyWindow);
   1.204 +#endif
   1.205 +
   1.206 +    if (VIVANTE_SetupPlatform(_this) < 0) {
   1.207 +        return -1;
   1.208 +    }
   1.209 +
   1.210 +    if (VIVANTE_AddVideoDisplays(_this) < 0) {
   1.211 +        return -1;
   1.212 +    }
   1.213 +
   1.214 +#ifdef SDL_INPUT_LINUXEV
   1.215 +    if (SDL_EVDEV_Init() < 0) {
   1.216 +        return -1;
   1.217 +    }
   1.218 +#endif
   1.219 +
   1.220 +    return 0;
   1.221 +}
   1.222 +
   1.223 +void
   1.224 +VIVANTE_VideoQuit(_THIS)
   1.225 +{
   1.226 +    SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   1.227 +
   1.228 +#ifdef SDL_INPUT_LINUXEV
   1.229 +    SDL_EVDEV_Quit();
   1.230 +#endif
   1.231 +
   1.232 +#if SDL_VIDEO_DRIVER_VIVANTE_VDK
   1.233 +    if (videodata->vdk_private) {
   1.234 +        vdkExit(videodata->vdk_private);
   1.235 +        videodata->vdk_private = NULL;
   1.236 +    }
   1.237 +#else
   1.238 +    if (videodata->egl_handle) {
   1.239 +        SDL_UnloadObject(videodata->egl_handle);
   1.240 +        videodata->egl_handle = NULL;
   1.241 +    }
   1.242 +#endif
   1.243 +}
   1.244 +
   1.245 +void
   1.246 +VIVANTE_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   1.247 +{
   1.248 +    /* Only one display mode available, the current one */
   1.249 +    SDL_AddDisplayMode(display, &display->current_mode);
   1.250 +}
   1.251 +
   1.252 +int
   1.253 +VIVANTE_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   1.254 +{
   1.255 +    return 0;
   1.256 +}
   1.257 +
   1.258 +int
   1.259 +VIVANTE_CreateWindow(_THIS, SDL_Window * window)
   1.260 +{
   1.261 +    SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   1.262 +    SDL_DisplayData *displaydata;
   1.263 +    SDL_WindowData *data;
   1.264 +
   1.265 +    displaydata = SDL_GetDisplayDriverData(0);
   1.266 +
   1.267 +    /* Allocate window internal data */
   1.268 +    data = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
   1.269 +    if (data == NULL) {
   1.270 +        return SDL_OutOfMemory();
   1.271 +    }
   1.272 +
   1.273 +    /* Setup driver data for this window */
   1.274 +    window->driverdata = data;
   1.275 +
   1.276 +#if SDL_VIDEO_DRIVER_VIVANTE_VDK
   1.277 +    data->native_window = vdkCreateWindow(displaydata->native_display, window->x, window->y, window->w, window->h);
   1.278 +#else
   1.279 +    data->native_window = videodata->fbCreateWindow(displaydata->native_display, window->x, window->y, window->w, window->h);
   1.280 +#endif
   1.281 +    if (!data->native_window) {
   1.282 +        return SDL_SetError("VIVANTE: Can't create native window");
   1.283 +    }
   1.284 +
   1.285 +    if (window->flags & SDL_WINDOW_OPENGL) {
   1.286 +        data->egl_surface = SDL_EGL_CreateSurface(_this, data->native_window);
   1.287 +        if (data->egl_surface == EGL_NO_SURFACE) {
   1.288 +            return SDL_SetError("VIVANTE: Can't create EGL surface");
   1.289 +        }
   1.290 +    } else {
   1.291 +        data->egl_surface = EGL_NO_SURFACE;
   1.292 +    }
   1.293 +
   1.294 +    /* Window has been successfully created */
   1.295 +    return 0;
   1.296 +}
   1.297 +
   1.298 +void
   1.299 +VIVANTE_DestroyWindow(_THIS, SDL_Window * window)
   1.300 +{
   1.301 +    SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   1.302 +    SDL_WindowData *data;
   1.303 +
   1.304 +    data = window->driverdata;
   1.305 +    if (data) {
   1.306 +        if (data->egl_surface != EGL_NO_SURFACE) {
   1.307 +            SDL_EGL_DestroySurface(_this, data->egl_surface);
   1.308 +        }
   1.309 +
   1.310 +        if (data->native_window) {
   1.311 +#if SDL_VIDEO_DRIVER_VIVANTE_VDK
   1.312 +            vdkDestroyWindow(data->native_window);
   1.313 +#else
   1.314 +            videodata->fbDestroyWindow(data->native_window);
   1.315 +#endif
   1.316 +        }
   1.317 +
   1.318 +        SDL_free(data);
   1.319 +    }
   1.320 +    window->driverdata = NULL;
   1.321 +}
   1.322 +
   1.323 +void
   1.324 +VIVANTE_SetWindowTitle(_THIS, SDL_Window * window)
   1.325 +{
   1.326 +#if SDL_VIDEO_DRIVER_VIVANTE_VDK
   1.327 +    SDL_WindowData *data = window->driverdata;
   1.328 +    vdkSetWindowTitle(data->native_window, window->title);
   1.329 +#endif
   1.330 +}
   1.331 +
   1.332 +void
   1.333 +VIVANTE_SetWindowPosition(_THIS, SDL_Window * window)
   1.334 +{
   1.335 +    /* FIXME */
   1.336 +}
   1.337 +
   1.338 +void
   1.339 +VIVANTE_SetWindowSize(_THIS, SDL_Window * window)
   1.340 +{
   1.341 +    /* FIXME */
   1.342 +}
   1.343 +
   1.344 +void
   1.345 +VIVANTE_ShowWindow(_THIS, SDL_Window * window)
   1.346 +{
   1.347 +#if SDL_VIDEO_DRIVER_VIVANTE_VDK
   1.348 +    SDL_WindowData *data = window->driverdata;
   1.349 +    vdkShowWindow(data->native_window);
   1.350 +#endif
   1.351 +    SDL_SetMouseFocus(window);
   1.352 +    SDL_SetKeyboardFocus(window);
   1.353 +}
   1.354 +
   1.355 +void
   1.356 +VIVANTE_HideWindow(_THIS, SDL_Window * window)
   1.357 +{
   1.358 +#if SDL_VIDEO_DRIVER_VIVANTE_VDK
   1.359 +    SDL_WindowData *data = window->driverdata;
   1.360 +    vdkHideWindow(data->native_window);
   1.361 +#endif
   1.362 +}
   1.363 +
   1.364 +/*****************************************************************************/
   1.365 +/* SDL Window Manager function                                               */
   1.366 +/*****************************************************************************/
   1.367 +SDL_bool
   1.368 +VIVANTE_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info)
   1.369 +{
   1.370 +/*
   1.371 +    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   1.372 +
   1.373 +    if (info->version.major == SDL_MAJOR_VERSION &&
   1.374 +        info->version.minor == SDL_MINOR_VERSION) {
   1.375 +        info->subsystem = SDL_SYSWM_VIVANTE;
   1.376 +        info->info.vivante.window = data->native_window;
   1.377 +        return SDL_TRUE;
   1.378 +    } else {
   1.379 +        SDL_SetError("Application not compiled with SDL %d.%d\n",
   1.380 +                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   1.381 +        return SDL_FALSE;
   1.382 +    }
   1.383 +*/
   1.384 +    SDL_Unsupported();
   1.385 +    return SDL_FALSE;
   1.386 +}
   1.387 +
   1.388 +/*****************************************************************************/
   1.389 +/* SDL event functions                                                       */
   1.390 +/*****************************************************************************/
   1.391 +void VIVANTE_PumpEvents(_THIS)
   1.392 +{
   1.393 +#ifdef SDL_INPUT_LINUXEV
   1.394 +    SDL_EVDEV_Poll();
   1.395 +#endif
   1.396 +}
   1.397 +
   1.398 +#endif /* SDL_VIDEO_DRIVER_VIVANTE */
   1.399 +
   1.400 +/* vi: set ts=4 sw=4 expandtab: */