src/video/vivante/SDL_vivantevideo.c
author Sam Lantinga
Fri, 27 Jan 2017 21:23:27 -0800
changeset 10861 71d8f9afb690
parent 10737 3406a0f8b041
child 10945 1300a3135d61
permissions -rw-r--r--
Fixed bug 3569 - GL_UpdateViewport leaves PROJECTION matrix selected

Tom Seddon

GL_ActivateRenderer may call GL_UpdateViewport, which leaves the GL_PROJECTION matrix selected. But after GL_ResetState, the GL_MODELVIEW matrix is selected, suggesting that's the intended default state.

It seems at least like these should be consistent. Presumably GL_UpdateViewport should be doing a glMatrixMode(GL_MODELVIEW) before it finishes.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 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 */
    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     VIVANTE_CleanupPlatform(_this);
   230 
   231 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   232     if (videodata->vdk_private) {
   233         vdkExit(videodata->vdk_private);
   234         videodata->vdk_private = NULL;
   235     }
   236 #else
   237     if (videodata->egl_handle) {
   238         SDL_UnloadObject(videodata->egl_handle);
   239         videodata->egl_handle = NULL;
   240     }
   241 #endif
   242 }
   243 
   244 void
   245 VIVANTE_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
   246 {
   247     /* Only one display mode available, the current one */
   248     SDL_AddDisplayMode(display, &display->current_mode);
   249 }
   250 
   251 int
   252 VIVANTE_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
   253 {
   254     return 0;
   255 }
   256 
   257 int
   258 VIVANTE_CreateWindow(_THIS, SDL_Window * window)
   259 {
   260     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   261     SDL_DisplayData *displaydata;
   262     SDL_WindowData *data;
   263 
   264     displaydata = SDL_GetDisplayDriverData(0);
   265 
   266     /* Allocate window internal data */
   267     data = (SDL_WindowData *) SDL_calloc(1, sizeof(SDL_WindowData));
   268     if (data == NULL) {
   269         return SDL_OutOfMemory();
   270     }
   271 
   272     /* Setup driver data for this window */
   273     window->driverdata = data;
   274 
   275 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   276     data->native_window = vdkCreateWindow(displaydata->native_display, window->x, window->y, window->w, window->h);
   277 #else
   278     data->native_window = videodata->fbCreateWindow(displaydata->native_display, window->x, window->y, window->w, window->h);
   279 #endif
   280     if (!data->native_window) {
   281         return SDL_SetError("VIVANTE: Can't create native window");
   282     }
   283 
   284     if (window->flags & SDL_WINDOW_OPENGL) {
   285         data->egl_surface = SDL_EGL_CreateSurface(_this, data->native_window);
   286         if (data->egl_surface == EGL_NO_SURFACE) {
   287             return SDL_SetError("VIVANTE: Can't create EGL surface");
   288         }
   289     } else {
   290         data->egl_surface = EGL_NO_SURFACE;
   291     }
   292 
   293     /* Window has been successfully created */
   294     return 0;
   295 }
   296 
   297 void
   298 VIVANTE_DestroyWindow(_THIS, SDL_Window * window)
   299 {
   300     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
   301     SDL_WindowData *data;
   302 
   303     data = window->driverdata;
   304     if (data) {
   305         if (data->egl_surface != EGL_NO_SURFACE) {
   306             SDL_EGL_DestroySurface(_this, data->egl_surface);
   307         }
   308 
   309         if (data->native_window) {
   310 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   311             vdkDestroyWindow(data->native_window);
   312 #else
   313             videodata->fbDestroyWindow(data->native_window);
   314 #endif
   315         }
   316 
   317         SDL_free(data);
   318     }
   319     window->driverdata = NULL;
   320 }
   321 
   322 void
   323 VIVANTE_SetWindowTitle(_THIS, SDL_Window * window)
   324 {
   325 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   326     SDL_WindowData *data = window->driverdata;
   327     vdkSetWindowTitle(data->native_window, window->title);
   328 #endif
   329 }
   330 
   331 void
   332 VIVANTE_SetWindowPosition(_THIS, SDL_Window * window)
   333 {
   334     /* FIXME */
   335 }
   336 
   337 void
   338 VIVANTE_SetWindowSize(_THIS, SDL_Window * window)
   339 {
   340     /* FIXME */
   341 }
   342 
   343 void
   344 VIVANTE_ShowWindow(_THIS, SDL_Window * window)
   345 {
   346 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   347     SDL_WindowData *data = window->driverdata;
   348     vdkShowWindow(data->native_window);
   349 #endif
   350     SDL_SetMouseFocus(window);
   351     SDL_SetKeyboardFocus(window);
   352 }
   353 
   354 void
   355 VIVANTE_HideWindow(_THIS, SDL_Window * window)
   356 {
   357 #if SDL_VIDEO_DRIVER_VIVANTE_VDK
   358     SDL_WindowData *data = window->driverdata;
   359     vdkHideWindow(data->native_window);
   360 #endif
   361 }
   362 
   363 /*****************************************************************************/
   364 /* SDL Window Manager function                                               */
   365 /*****************************************************************************/
   366 SDL_bool
   367 VIVANTE_GetWindowWMInfo(_THIS, SDL_Window * window, struct SDL_SysWMinfo *info)
   368 {
   369     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   370     SDL_DisplayData *displaydata = SDL_GetDisplayDriverData(0);
   371 
   372     if (info->version.major == SDL_MAJOR_VERSION &&
   373         info->version.minor == SDL_MINOR_VERSION) {
   374         info->subsystem = SDL_SYSWM_VIVANTE;
   375         info->info.vivante.display = displaydata->native_display;
   376         info->info.vivante.window = data->native_window;
   377         return SDL_TRUE;
   378     } else {
   379         SDL_SetError("Application not compiled with SDL %d.%d\n",
   380                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   381         return SDL_FALSE;
   382     }
   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: */