SDL_androidvideo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_ANDROID
  20. /* Android SDL video driver implementation */
  21. #include "../SDL_sysvideo.h"
  22. #include "../SDL_pixels_c.h"
  23. #include "../../events/SDL_events_c.h"
  24. #include "../../events/SDL_windowevents_c.h"
  25. #include "SDL_androidvideo.h"
  26. #include "SDL_androidgl.h"
  27. #include "SDL_androidclipboard.h"
  28. #include "SDL_androidevents.h"
  29. #include "SDL_androidkeyboard.h"
  30. #include "SDL_androidmouse.h"
  31. #include "SDL_androidtouch.h"
  32. #include "SDL_androidwindow.h"
  33. #include "SDL_androidvulkan.h"
  34. #define ANDROID_VID_DRIVER_NAME "Android"
  35. /* Initialization/Query functions */
  36. static int Android_VideoInit(_THIS);
  37. static void Android_VideoQuit(_THIS);
  38. int Android_GetDisplayPhysicalDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi);
  39. #include "../SDL_egl_c.h"
  40. #define Android_GLES_GetProcAddress SDL_EGL_GetProcAddressInternal
  41. #define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
  42. #define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
  43. #define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
  44. #define Android_GLES_DeleteContext SDL_EGL_DeleteContext
  45. /* Android driver bootstrap functions */
  46. /* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
  47. int Android_SurfaceWidth = 0;
  48. int Android_SurfaceHeight = 0;
  49. static int Android_DeviceWidth = 0;
  50. static int Android_DeviceHeight = 0;
  51. static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_RGB565; /* Default SurfaceView format, in case this is queried before being filled */
  52. float Android_ScreenDensity = 1.0f;
  53. static float Android_ScreenRate = 0.0f;
  54. SDL_sem *Android_PauseSem = NULL;
  55. SDL_sem *Android_ResumeSem = NULL;
  56. SDL_mutex *Android_ActivityMutex = NULL;
  57. static void Android_SuspendScreenSaver(_THIS)
  58. {
  59. Android_JNI_SuspendScreenSaver(_this->suspend_screensaver);
  60. }
  61. static void Android_DeleteDevice(SDL_VideoDevice *device)
  62. {
  63. SDL_free(device->driverdata);
  64. SDL_free(device);
  65. }
  66. static SDL_VideoDevice *Android_CreateDevice(void)
  67. {
  68. SDL_VideoDevice *device;
  69. SDL_VideoData *data;
  70. SDL_bool block_on_pause;
  71. /* Initialize all variables that we clean on shutdown */
  72. device = (SDL_VideoDevice *)SDL_calloc(1, sizeof(SDL_VideoDevice));
  73. if (device == NULL) {
  74. SDL_OutOfMemory();
  75. return NULL;
  76. }
  77. data = (SDL_VideoData *)SDL_calloc(1, sizeof(SDL_VideoData));
  78. if (data == NULL) {
  79. SDL_OutOfMemory();
  80. SDL_free(device);
  81. return NULL;
  82. }
  83. device->driverdata = data;
  84. /* Set the function pointers */
  85. device->VideoInit = Android_VideoInit;
  86. device->VideoQuit = Android_VideoQuit;
  87. block_on_pause = SDL_GetHintBoolean(SDL_HINT_ANDROID_BLOCK_ON_PAUSE, SDL_TRUE);
  88. if (block_on_pause) {
  89. device->PumpEvents = Android_PumpEvents_Blocking;
  90. } else {
  91. device->PumpEvents = Android_PumpEvents_NonBlocking;
  92. }
  93. device->GetDisplayPhysicalDPI = Android_GetDisplayPhysicalDPI;
  94. device->CreateSDLWindow = Android_CreateWindow;
  95. device->SetWindowTitle = Android_SetWindowTitle;
  96. device->SetWindowFullscreen = Android_SetWindowFullscreen;
  97. device->MinimizeWindow = Android_MinimizeWindow;
  98. device->SetWindowResizable = Android_SetWindowResizable;
  99. device->DestroyWindow = Android_DestroyWindow;
  100. device->GetWindowWMInfo = Android_GetWindowWMInfo;
  101. device->free = Android_DeleteDevice;
  102. /* GL pointers */
  103. #if SDL_VIDEO_OPENGL_EGL
  104. device->GL_LoadLibrary = Android_GLES_LoadLibrary;
  105. device->GL_GetProcAddress = Android_GLES_GetProcAddress;
  106. device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
  107. device->GL_CreateContext = Android_GLES_CreateContext;
  108. device->GL_MakeCurrent = Android_GLES_MakeCurrent;
  109. device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
  110. device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
  111. device->GL_SwapWindow = Android_GLES_SwapWindow;
  112. device->GL_DeleteContext = Android_GLES_DeleteContext;
  113. #endif
  114. #if SDL_VIDEO_VULKAN
  115. device->Vulkan_LoadLibrary = Android_Vulkan_LoadLibrary;
  116. device->Vulkan_UnloadLibrary = Android_Vulkan_UnloadLibrary;
  117. device->Vulkan_GetInstanceExtensions = Android_Vulkan_GetInstanceExtensions;
  118. device->Vulkan_CreateSurface = Android_Vulkan_CreateSurface;
  119. #endif
  120. /* Screensaver */
  121. device->SuspendScreenSaver = Android_SuspendScreenSaver;
  122. /* Text input */
  123. device->StartTextInput = Android_StartTextInput;
  124. device->StopTextInput = Android_StopTextInput;
  125. device->SetTextInputRect = Android_SetTextInputRect;
  126. /* Screen keyboard */
  127. device->HasScreenKeyboardSupport = Android_HasScreenKeyboardSupport;
  128. device->IsScreenKeyboardShown = Android_IsScreenKeyboardShown;
  129. /* Clipboard */
  130. device->SetClipboardText = Android_SetClipboardText;
  131. device->GetClipboardText = Android_GetClipboardText;
  132. device->HasClipboardText = Android_HasClipboardText;
  133. return device;
  134. }
  135. VideoBootStrap Android_bootstrap = {
  136. ANDROID_VID_DRIVER_NAME, "SDL Android video driver",
  137. Android_CreateDevice
  138. };
  139. int Android_VideoInit(_THIS)
  140. {
  141. SDL_VideoData *videodata = _this->driverdata;
  142. SDL_DisplayID displayID;
  143. SDL_VideoDisplay *display;
  144. SDL_DisplayMode mode;
  145. videodata->isPaused = SDL_FALSE;
  146. videodata->isPausing = SDL_FALSE;
  147. videodata->pauseAudio = SDL_GetHintBoolean(SDL_HINT_ANDROID_BLOCK_ON_PAUSE_PAUSEAUDIO, SDL_TRUE);
  148. SDL_zero(mode);
  149. mode.format = Android_ScreenFormat;
  150. mode.pixel_w = Android_DeviceWidth;
  151. mode.pixel_h = Android_DeviceHeight;
  152. mode.display_scale = Android_ScreenDensity;
  153. mode.refresh_rate = Android_ScreenRate;
  154. mode.driverdata = NULL;
  155. displayID = SDL_AddBasicVideoDisplay(&mode);
  156. if (displayID == 0) {
  157. return -1;
  158. }
  159. display = SDL_GetVideoDisplay(displayID);
  160. display->orientation = Android_JNI_GetDisplayOrientation();
  161. SDL_AddDisplayMode(&_this->displays[0], &mode);
  162. Android_InitTouch();
  163. Android_InitMouse();
  164. /* We're done! */
  165. return 0;
  166. }
  167. void Android_VideoQuit(_THIS)
  168. {
  169. Android_QuitMouse();
  170. Android_QuitTouch();
  171. }
  172. int Android_GetDisplayPhysicalDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi)
  173. {
  174. return Android_JNI_GetDisplayPhysicalDPI(ddpi, hdpi, vdpi);
  175. }
  176. void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float density, float rate)
  177. {
  178. Android_SurfaceWidth = surfaceWidth;
  179. Android_SurfaceHeight = surfaceHeight;
  180. Android_DeviceWidth = deviceWidth;
  181. Android_DeviceHeight = deviceHeight;
  182. Android_ScreenDensity = (density > 0.0f) ? density : 1.0f;
  183. Android_ScreenRate = rate;
  184. }
  185. static Uint32 format_to_pixelFormat(int format)
  186. {
  187. Uint32 pf;
  188. if (format == AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM) { /* 1 */
  189. pf = SDL_PIXELFORMAT_RGBA8888;
  190. } else if (format == AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM) { /* 2 */
  191. pf = SDL_PIXELFORMAT_RGBX8888;
  192. } else if (format == AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM) { /* 3 */
  193. pf = SDL_PIXELFORMAT_RGB24;
  194. } else if (format == AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM) { /* 4*/
  195. pf = SDL_PIXELFORMAT_RGB565;
  196. } else if (format == 5) {
  197. pf = SDL_PIXELFORMAT_BGRA8888;
  198. } else if (format == 6) {
  199. pf = SDL_PIXELFORMAT_RGBA5551;
  200. } else if (format == 7) {
  201. pf = SDL_PIXELFORMAT_RGBA4444;
  202. } else if (format == 0x115) {
  203. /* HAL_PIXEL_FORMAT_BGR_565 */
  204. pf = SDL_PIXELFORMAT_RGB565;
  205. } else {
  206. pf = SDL_PIXELFORMAT_UNKNOWN;
  207. }
  208. return pf;
  209. }
  210. void Android_SetFormat(int format_wanted, int format_got)
  211. {
  212. Uint32 pf_wanted;
  213. Uint32 pf_got;
  214. pf_wanted = format_to_pixelFormat(format_wanted);
  215. pf_got = format_to_pixelFormat(format_got);
  216. Android_ScreenFormat = pf_got;
  217. SDL_Log("pixel format wanted %s (%d), got %s (%d)",
  218. SDL_GetPixelFormatName(pf_wanted), format_wanted,
  219. SDL_GetPixelFormatName(pf_got), format_got);
  220. }
  221. void Android_SendResize(SDL_Window *window)
  222. {
  223. /*
  224. Update the resolution of the desktop mode, so that the window
  225. can be properly resized. The screen resolution change can for
  226. example happen when the Activity enters or exits immersive mode,
  227. which can happen after VideoInit().
  228. */
  229. SDL_VideoDevice *device = SDL_GetVideoDevice();
  230. if (device && device->num_displays > 0) {
  231. SDL_VideoDisplay *display = &device->displays[0];
  232. SDL_DisplayMode desktop_mode;
  233. SDL_zero(desktop_mode);
  234. desktop_mode.format = Android_ScreenFormat;
  235. desktop_mode.pixel_w = Android_DeviceWidth;
  236. desktop_mode.pixel_h = Android_DeviceHeight;
  237. desktop_mode.display_scale = Android_ScreenDensity;
  238. desktop_mode.refresh_rate = Android_ScreenRate;
  239. SDL_SetDesktopDisplayMode(display, &desktop_mode);
  240. }
  241. if (window) {
  242. /* Force the current mode to match the resize otherwise the SDL_EVENT_WINDOW_RESTORED event
  243. * will fall back to the old mode */
  244. int w, h;
  245. SDL_VideoDisplay *display = SDL_GetVideoDisplayForWindow(window);
  246. SDL_DisplayMode current_mode;
  247. current_mode.format = Android_ScreenFormat;
  248. current_mode.pixel_w = Android_DeviceWidth;
  249. current_mode.pixel_h = Android_DeviceHeight;
  250. current_mode.display_scale = Android_ScreenDensity;
  251. current_mode.refresh_rate = Android_ScreenRate;
  252. SDL_SetCurrentDisplayMode(display, &current_mode);
  253. w = (int)SDL_floorf(Android_SurfaceWidth / Android_ScreenDensity);
  254. h = (int)SDL_floorf(Android_SurfaceHeight / Android_ScreenDensity);
  255. SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, w, h);
  256. }
  257. }
  258. #endif /* SDL_VIDEO_DRIVER_ANDROID */