src/video/vivante/SDL_vivantevideo.c
author Sam Lantinga
Wed, 15 Oct 2014 09:18:17 -0700
changeset 9164 494876610c49
child 9169 70e5da67cf9d
permissions -rw-r--r--
Expanded the iMX6 video driver into a general Vivante video driver that works across multiple SoCs
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../../SDL_internal.h"
    22 
    23 #if SDL_VIDEO_DRIVER_VIVANTE
    24 
    25 /* SDL internals */
    26 #include "../SDL_sysvideo.h"
    27 #include "SDL_version.h"
    28 #include "SDL_syswm.h"
    29 #include "SDL_loadso.h"
    30 #include "SDL_events.h"
    31 #include "../../events/SDL_events_c.h"
    32 
    33 #ifdef SDL_INPUT_LINUXEV
    34 #include "../../core/linux/SDL_evdev.h"
    35 #endif
    36 
    37 #include "SDL_vivantevideo.h"
    38 #include "SDL_vivanteplatform.h"
    39 #include "SDL_vivanteopengles.h"
    40 
    41 
    42 static int
    43 VIVANTE_Available(void)
    44 {
    45     return 1;
    46 }
    47 
    48 static void
    49 VIVANTE_Destroy(SDL_VideoDevice * device)
    50 {
    51     if (device->driverdata != NULL) {
    52         SDL_free(device->driverdata);
    53         device->driverdata = NULL;
    54     }
    55 }
    56 
    57 static SDL_VideoDevice *
    58 VIVANTE_Create()
    59 {
    60     SDL_VideoDevice *device;
    61     SDL_VideoData *data;
    62 
    63     /* Initialize SDL_VideoDevice structure */
    64     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    65     if (device == NULL) {
    66         SDL_OutOfMemory();
    67         return NULL;
    68     }
    69 
    70     /* Initialize internal data */
    71     data = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
    72     if (data == NULL) {
    73         SDL_OutOfMemory();
    74         SDL_free(device);
    75         return NULL;
    76     }
    77 
    78     device->driverdata = data;
    79 
    80     /* Setup amount of available displays and current display */
    81     device->num_displays = 0;
    82 
    83     /* Set device free function */
    84     device->free = VIVANTE_Destroy;
    85 
    86     /* Setup all functions which we can handle */
    87     device->VideoInit = VIVANTE_VideoInit;
    88     device->VideoQuit = VIVANTE_VideoQuit;
    89     device->GetDisplayModes = VIVANTE_GetDisplayModes;
    90     device->SetDisplayMode = VIVANTE_SetDisplayMode;
    91     device->CreateWindow = VIVANTE_CreateWindow;
    92     device->SetWindowTitle = VIVANTE_SetWindowTitle;
    93     device->SetWindowPosition = VIVANTE_SetWindowPosition;
    94     device->SetWindowSize = VIVANTE_SetWindowSize;
    95     device->ShowWindow = VIVANTE_ShowWindow;
    96     device->HideWindow = VIVANTE_HideWindow;
    97     device->DestroyWindow = VIVANTE_DestroyWindow;
    98     device->GetWindowWMInfo = VIVANTE_GetWindowWMInfo;
    99 
   100     device->GL_LoadLibrary = VIVANTE_GLES_LoadLibrary;
   101     device->GL_GetProcAddress = VIVANTE_GLES_GetProcAddress;
   102     device->GL_UnloadLibrary = VIVANTE_GLES_UnloadLibrary;
   103     device->GL_CreateContext = VIVANTE_GLES_CreateContext;
   104     device->GL_MakeCurrent = VIVANTE_GLES_MakeCurrent;
   105     device->GL_SetSwapInterval = VIVANTE_GLES_SetSwapInterval;
   106     device->GL_GetSwapInterval = VIVANTE_GLES_GetSwapInterval;
   107     device->GL_SwapWindow = VIVANTE_GLES_SwapWindow;
   108     device->GL_DeleteContext = VIVANTE_GLES_DeleteContext;
   109 
   110     device->PumpEvents = VIVANTE_PumpEvents;
   111 
   112     return device;
   113 }
   114 
   115 VideoBootStrap VIVANTE_bootstrap = {
   116     "vivante",
   117     "Vivante EGL Video Driver",
   118     VIVANTE_Available,
   119     VIVANTE_Create
   120 };
   121 
   122 /*****************************************************************************/
   123 /* SDL Video and Display initialization/handling functions                   */
   124 /*****************************************************************************/
   125 
   126 static int
   127 VIVANTE_AddVideoDisplays(_THIS)
   128 {
   129     SDL_VideoData *videodata = _this->driverdata;
   130     SDL_VideoDisplay display;
   131     SDL_DisplayMode current_mode;
   132     SDL_DisplayData *data;
   133     int pitch = 0, bpp = 0;
   134     unsigned long pixels = 0;
   135 
   136     data = (SDL_DisplayData *) SDL_calloc(1, sizeof(SDL_DisplayData));
   137     if (data == NULL) {
   138         return SDL_OutOfMemory();
   139     }
   140 
   141     SDL_zero(current_mode);
   142 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   143     data->native_display = vdkGetDisplay(videodata->vdk_private);
   144 
   145     vdkGetDisplayInfo(data->native_display, &current_mode.w, &current_mode.h, &pixels, &pitch, &bpp);
   146 #else
   147     data->native_display = videodata->fbGetDisplayByIndex(0);
   148 
   149     videodata->fbGetDisplayInfo(data->native_display, &current_mode.w, &current_mode.h, &pixels, &pitch, &bpp);
   150 #endif /* SDL_VIDEO_DRIVER_VIVANTE_VDK */
   151 
   152     switch (bpp)
   153     {
   154     default: /* Is another format used? */
   155     case 16:
   156         current_mode.format = SDL_PIXELFORMAT_RGB565;
   157         break;
   158     }
   159     /* FIXME: How do we query refresh rate? */
   160     current_mode.refresh_rate = 60;
   161 
   162     SDL_zero(display);
   163     display.desktop_mode = current_mode;
   164     display.current_mode = current_mode;
   165     display.driverdata = data;
   166     SDL_AddVideoDisplay(&display);
   167     return 0;
   168 }
   169 
   170 int
   171 VIVANTE_VideoInit(_THIS)
   172 {
   173     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   174 
   175 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   176     videodata->vdk_private = vdkInitialize();
   177     if (!videodata->vdk_private) {
   178         return SDL_SetError("vdkInitialize() failed");
   179     }
   180 #else
   181     videodata->egl_handle = SDL_LoadObject("libEGL.so.1");
   182     if (!videodata->egl_handle) {
   183         videodata->egl_handle = SDL_LoadObject("libEGL.so");
   184         if (!videodata->egl_handle) {
   185             return -1;
   186         }
   187     }
   188 #define LOAD_FUNC(NAME) \
   189     videodata->NAME = SDL_LoadFunction(videodata->egl_handle, #NAME); \
   190     if (!videodata->NAME) return -1;
   191 
   192     LOAD_FUNC(fbGetDisplay);
   193     LOAD_FUNC(fbGetDisplayByIndex);
   194     LOAD_FUNC(fbGetDisplayGeometry);
   195     LOAD_FUNC(fbGetDisplayInfo);
   196     LOAD_FUNC(fbDestroyDisplay);
   197     LOAD_FUNC(fbCreateWindow);
   198     LOAD_FUNC(fbGetWindowGeometry);
   199     LOAD_FUNC(fbGetWindowInfo);
   200     LOAD_FUNC(fbDestroyWindow);
   201 #endif
   202 
   203     if (VIVANTE_SetupPlatform(_this) < 0) {
   204         return -1;
   205     }
   206 
   207     if (VIVANTE_AddVideoDisplays(_this) < 0) {
   208         return -1;
   209     }
   210 
   211 #ifdef SDL_INPUT_LINUXEV
   212     if (SDL_EVDEV_Init() < 0) {
   213         return -1;
   214     }
   215 #endif
   216 
   217     return 0;
   218 }
   219 
   220 void
   221 VIVANTE_VideoQuit(_THIS)
   222 {
   223     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   224 
   225 #ifdef SDL_INPUT_LINUXEV
   226     SDL_EVDEV_Quit();
   227 #endif
   228 
   229 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   230     if (videodata->vdk_private) {
   231         vdkExit(videodata->vdk_private);
   232         videodata->vdk_private = NULL;
   233     }
   234 #else
   235     if (videodata->egl_handle) {
   236         SDL_UnloadObject(videodata->egl_handle);
   237         videodata->egl_handle = NULL;
   238     }
   239 #endif
   240 }
   241 
   242 void
   243 VIVANTE_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   244 {
   245     /* Only one display mode available, the current one */
   246     SDL_AddDisplayMode(display, &display->current_mode);
   247 }
   248 
   249 int
   250 VIVANTE_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   251 {
   252     return 0;
   253 }
   254 
   255 int
   256 VIVANTE_CreateWindow(_THIS, SDL_Window * window)
   257 {
   258     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   259     SDL_DisplayData *displaydata;
   260     SDL_WindowData *data;
   261 
   262     displaydata = SDL_GetDisplayDriverData(0);
   263 
   264     /* Allocate window internal data */
   265     data = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
   266     if (data == NULL) {
   267         return SDL_OutOfMemory();
   268     }
   269 
   270     /* Setup driver data for this window */
   271     window->driverdata = data;
   272 
   273 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   274     data->native_window = vdkCreateWindow(displaydata->native_display, window->x, window->y, window->w, window->h);
   275 #else
   276     data->native_window = videodata->fbCreateWindow(displaydata->native_display, window->x, window->y, window->w, window->h);
   277 #endif
   278     if (!data->native_window) {
   279         return SDL_SetError("VIVANTE: Can't create native window");
   280     }
   281 
   282     if (window->flags & SDL_WINDOW_OPENGL) {
   283         data->egl_surface = SDL_EGL_CreateSurface(_this, data->native_window);
   284         if (data->egl_surface == EGL_NO_SURFACE) {
   285             return SDL_SetError("VIVANTE: Can't create EGL surface");
   286         }
   287     } else {
   288         data->egl_surface = EGL_NO_SURFACE;
   289     }
   290 
   291     /* Window has been successfully created */
   292     return 0;
   293 }
   294 
   295 void
   296 VIVANTE_DestroyWindow(_THIS, SDL_Window * window)
   297 {
   298     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   299     SDL_WindowData *data;
   300 
   301     data = window->driverdata;
   302     if (data) {
   303         if (data->egl_surface != EGL_NO_SURFACE) {
   304             SDL_EGL_DestroySurface(_this, data->egl_surface);
   305         }
   306 
   307         if (data->native_window) {
   308 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   309             vdkDestroyWindow(data->native_window);
   310 #else
   311             videodata->fbDestroyWindow(data->native_window);
   312 #endif
   313         }
   314 
   315         SDL_free(data);
   316     }
   317     window->driverdata = NULL;
   318 }
   319 
   320 void
   321 VIVANTE_SetWindowTitle(_THIS, SDL_Window * window)
   322 {
   323 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   324     SDL_WindowData *data = window->driverdata;
   325     vdkSetWindowTitle(data->native_window, window->title);
   326 #endif
   327 }
   328 
   329 void
   330 VIVANTE_SetWindowPosition(_THIS, SDL_Window * window)
   331 {
   332     /* FIXME */
   333 }
   334 
   335 void
   336 VIVANTE_SetWindowSize(_THIS, SDL_Window * window)
   337 {
   338     /* FIXME */
   339 }
   340 
   341 void
   342 VIVANTE_ShowWindow(_THIS, SDL_Window * window)
   343 {
   344 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   345     SDL_WindowData *data = window->driverdata;
   346     vdkShowWindow(data->native_window);
   347 #endif
   348     SDL_SetMouseFocus(window);
   349     SDL_SetKeyboardFocus(window);
   350 }
   351 
   352 void
   353 VIVANTE_HideWindow(_THIS, SDL_Window * window)
   354 {
   355 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   356     SDL_WindowData *data = window->driverdata;
   357     vdkHideWindow(data->native_window);
   358 #endif
   359 }
   360 
   361 /*****************************************************************************/
   362 /* SDL Window Manager function                                               */
   363 /*****************************************************************************/
   364 SDL_bool
   365 VIVANTE_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info)
   366 {
   367 /*
   368     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   369 
   370     if (info->version.major == SDL_MAJOR_VERSION &&
   371         info->version.minor == SDL_MINOR_VERSION) {
   372         info->subsystem = SDL_SYSWM_VIVANTE;
   373         info->info.vivante.window = data->native_window;
   374         return SDL_TRUE;
   375     } else {
   376         SDL_SetError("Application not compiled with SDL %d.%d\n",
   377                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   378         return SDL_FALSE;
   379     }
   380 */
   381     SDL_Unsupported();
   382     return SDL_FALSE;
   383 }
   384 
   385 /*****************************************************************************/
   386 /* SDL event functions                                                       */
   387 /*****************************************************************************/
   388 void VIVANTE_PumpEvents(_THIS)
   389 {
   390 #ifdef SDL_INPUT_LINUXEV
   391     SDL_EVDEV_Poll();
   392 #endif
   393 }
   394 
   395 #endif /* SDL_VIDEO_DRIVER_VIVANTE */
   396 
   397 /* vi: set ts=4 sw=4 expandtab: */