src/video/android/SDL_androidvideo.c
author Sylvain Becker <sylvain.becker@gmail.com>
Thu, 03 Jan 2019 16:22:33 +0100
changeset 12497 9e13f3286831
parent 12496 93771c30420b
child 12498 c2e223dc5da9
permissions -rw-r--r--
Android: prevent the error message from SDL_EGL_CreateSurface() to be masked.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2018 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_ANDROID
    24 
    25 /* Android SDL video driver implementation */
    26 
    27 #include "SDL_video.h"
    28 #include "SDL_mouse.h"
    29 #include "../SDL_sysvideo.h"
    30 #include "../SDL_pixels_c.h"
    31 #include "../../events/SDL_events_c.h"
    32 #include "../../events/SDL_windowevents_c.h"
    33 
    34 #include "SDL_androidvideo.h"
    35 #include "SDL_androidgl.h"
    36 #include "SDL_androidclipboard.h"
    37 #include "SDL_androidevents.h"
    38 #include "SDL_androidkeyboard.h"
    39 #include "SDL_androidmouse.h"
    40 #include "SDL_androidtouch.h"
    41 #include "SDL_androidwindow.h"
    42 #include "SDL_androidvulkan.h"
    43 
    44 #define ANDROID_VID_DRIVER_NAME "Android"
    45 
    46 /* Initialization/Query functions */
    47 static int Android_VideoInit(_THIS);
    48 static void Android_VideoQuit(_THIS);
    49 int Android_GetDisplayDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi);
    50 
    51 #include "../SDL_egl_c.h"
    52 #define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
    53 #define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
    54 #define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
    55 #define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
    56 #define Android_GLES_DeleteContext SDL_EGL_DeleteContext
    57 
    58 /* Android driver bootstrap functions */
    59 
    60 
    61 /* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
    62 int Android_SurfaceWidth = 0;
    63 int Android_SurfaceHeight = 0;
    64 int Android_DeviceWidth = 0;
    65 int Android_DeviceHeight = 0;
    66 static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    67 static int Android_ScreenRate = 0;
    68 
    69 SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL, *Android_ActivitySem = NULL;
    70 
    71 static int
    72 Android_Available(void)
    73 {
    74     return 1;
    75 }
    76 
    77 static void
    78 Android_SuspendScreenSaver(_THIS)
    79 {
    80     Android_JNI_SuspendScreenSaver(_this->suspend_screensaver);
    81 }
    82 
    83 static void
    84 Android_DeleteDevice(SDL_VideoDevice *device)
    85 {
    86     SDL_free(device->driverdata);
    87     SDL_free(device);
    88 }
    89 
    90 static SDL_VideoDevice *
    91 Android_CreateDevice(int devindex)
    92 {
    93     SDL_VideoDevice *device;
    94     SDL_VideoData *data;
    95 
    96     /* Initialize all variables that we clean on shutdown */
    97     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    98     if (!device) {
    99         SDL_OutOfMemory();
   100         return NULL;
   101     }
   102 
   103     data = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
   104     if (!data) {
   105         SDL_OutOfMemory();
   106         SDL_free(device);
   107         return NULL;
   108     }
   109 
   110     device->driverdata = data;
   111 
   112     /* Set the function pointers */
   113     device->VideoInit = Android_VideoInit;
   114     device->VideoQuit = Android_VideoQuit;
   115     device->PumpEvents = Android_PumpEvents;
   116 
   117     device->GetDisplayDPI = Android_GetDisplayDPI;
   118 
   119     device->CreateSDLWindow = Android_CreateWindow;
   120     device->SetWindowTitle = Android_SetWindowTitle;
   121     device->SetWindowFullscreen = Android_SetWindowFullscreen;
   122     device->DestroyWindow = Android_DestroyWindow;
   123     device->GetWindowWMInfo = Android_GetWindowWMInfo;
   124 
   125     device->free = Android_DeleteDevice;
   126 
   127     /* GL pointers */
   128     device->GL_LoadLibrary = Android_GLES_LoadLibrary;
   129     device->GL_GetProcAddress = Android_GLES_GetProcAddress;
   130     device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
   131     device->GL_CreateContext = Android_GLES_CreateContext;
   132     device->GL_MakeCurrent = Android_GLES_MakeCurrent;
   133     device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
   134     device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
   135     device->GL_SwapWindow = Android_GLES_SwapWindow;
   136     device->GL_DeleteContext = Android_GLES_DeleteContext;
   137 
   138 #if SDL_VIDEO_VULKAN
   139     device->Vulkan_LoadLibrary = Android_Vulkan_LoadLibrary;
   140     device->Vulkan_UnloadLibrary = Android_Vulkan_UnloadLibrary;
   141     device->Vulkan_GetInstanceExtensions = Android_Vulkan_GetInstanceExtensions;
   142     device->Vulkan_CreateSurface = Android_Vulkan_CreateSurface;
   143 #endif
   144 
   145     /* Screensaver */
   146     device->SuspendScreenSaver = Android_SuspendScreenSaver;
   147 
   148     /* Text input */
   149     device->StartTextInput = Android_StartTextInput;
   150     device->StopTextInput = Android_StopTextInput;
   151     device->SetTextInputRect = Android_SetTextInputRect;
   152 
   153     /* Screen keyboard */
   154     device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
   155     device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
   156 
   157     /* Clipboard */
   158     device->SetClipboardText = Android_SetClipboardText;
   159     device->GetClipboardText = Android_GetClipboardText;
   160     device->HasClipboardText = Android_HasClipboardText;
   161 
   162     return device;
   163 }
   164 
   165 VideoBootStrap Android_bootstrap = {
   166     ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
   167     Android_Available, Android_CreateDevice
   168 };
   169 
   170 
   171 int
   172 Android_VideoInit(_THIS)
   173 {
   174     SDL_DisplayMode mode;
   175 
   176     mode.format = Android_ScreenFormat;
   177     mode.w = Android_DeviceWidth;
   178     mode.h = Android_DeviceHeight;
   179     mode.refresh_rate = Android_ScreenRate;
   180     mode.driverdata = NULL;
   181     if (SDL_AddBasicVideoDisplay(&mode) < 0) {
   182         return -1;
   183     }
   184 
   185     SDL_AddDisplayMode(&_this->displays[0], &mode);
   186 
   187     Android_InitKeyboard();
   188 
   189     Android_InitTouch();
   190 
   191     Android_InitMouse();
   192 
   193     /* We're done! */
   194     return 0;
   195 }
   196 
   197 void
   198 Android_VideoQuit(_THIS)
   199 {
   200     Android_QuitMouse();
   201     Android_QuitTouch();
   202 }
   203 
   204 int
   205 Android_GetDisplayDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi)
   206 {
   207     return Android_JNI_GetDisplayDPI(ddpi, hdpi, vdpi);
   208 }
   209 
   210 void
   211 Android_SetScreenResolution(SDL_Window *window, int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
   212 {
   213     SDL_VideoDevice *device;
   214     SDL_VideoDisplay *display;
   215     Android_SurfaceWidth = surfaceWidth;
   216     Android_SurfaceHeight = surfaceHeight;
   217     Android_DeviceWidth = deviceWidth;
   218     Android_DeviceHeight = deviceHeight;
   219     Android_ScreenFormat = format;
   220     Android_ScreenRate = (int)rate;
   221 
   222     /*
   223       Update the resolution of the desktop mode, so that the window
   224       can be properly resized. The screen resolution change can for
   225       example happen when the Activity enters or exits immersive mode,
   226       which can happen after VideoInit().
   227     */
   228     device = SDL_GetVideoDevice();
   229     if (device && device->num_displays > 0)
   230     {
   231         display = &device->displays[0];
   232         display->desktop_mode.format = Android_ScreenFormat;
   233         display->desktop_mode.w = Android_DeviceWidth;
   234         display->desktop_mode.h = Android_DeviceHeight;
   235         display->desktop_mode.refresh_rate  = Android_ScreenRate;
   236     }
   237 
   238     if (window) {
   239         /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
   240          * will fall back to the old mode */
   241         display = SDL_GetDisplayForWindow(window);
   242 
   243         display->display_modes[0].format = format;
   244         display->display_modes[0].w = Android_DeviceWidth;
   245         display->display_modes[0].h = Android_DeviceHeight;
   246         display->display_modes[0].refresh_rate = (int)rate;
   247         display->current_mode = display->display_modes[0];
   248 
   249         SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, surfaceWidth, surfaceHeight);
   250     }
   251 }
   252 
   253 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   254 
   255 /* vi: set ts=4 sw=4 expandtab: */