display_server_android.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /*************************************************************************/
  2. /* display_server_android.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "display_server_android.h"
  31. #include "core/config/project_settings.h"
  32. #include "java_godot_io_wrapper.h"
  33. #include "java_godot_wrapper.h"
  34. #include "os_android.h"
  35. #include "tts_android.h"
  36. #if defined(VULKAN_ENABLED)
  37. #include "drivers/vulkan/rendering_device_vulkan.h"
  38. #include "platform/android/vulkan/vulkan_context_android.h"
  39. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  40. #endif
  41. #ifdef GLES3_ENABLED
  42. #include "drivers/gles3/rasterizer_gles3.h"
  43. #include <EGL/egl.h>
  44. #endif
  45. DisplayServerAndroid *DisplayServerAndroid::get_singleton() {
  46. return static_cast<DisplayServerAndroid *>(DisplayServer::get_singleton());
  47. }
  48. bool DisplayServerAndroid::has_feature(Feature p_feature) const {
  49. switch (p_feature) {
  50. case FEATURE_CURSOR_SHAPE:
  51. //case FEATURE_CUSTOM_CURSOR_SHAPE:
  52. //case FEATURE_GLOBAL_MENU:
  53. //case FEATURE_HIDPI:
  54. //case FEATURE_ICON:
  55. //case FEATURE_IME:
  56. case FEATURE_MOUSE:
  57. //case FEATURE_MOUSE_WARP:
  58. //case FEATURE_NATIVE_DIALOG:
  59. //case FEATURE_NATIVE_ICON:
  60. //case FEATURE_WINDOW_TRANSPARENCY:
  61. case FEATURE_CLIPBOARD:
  62. case FEATURE_KEEP_SCREEN_ON:
  63. case FEATURE_ORIENTATION:
  64. case FEATURE_TOUCHSCREEN:
  65. case FEATURE_VIRTUAL_KEYBOARD:
  66. case FEATURE_TEXT_TO_SPEECH:
  67. return true;
  68. default:
  69. return false;
  70. }
  71. }
  72. String DisplayServerAndroid::get_name() const {
  73. return "Android";
  74. }
  75. bool DisplayServerAndroid::tts_is_speaking() const {
  76. return TTS_Android::is_speaking();
  77. }
  78. bool DisplayServerAndroid::tts_is_paused() const {
  79. return TTS_Android::is_paused();
  80. }
  81. TypedArray<Dictionary> DisplayServerAndroid::tts_get_voices() const {
  82. return TTS_Android::get_voices();
  83. }
  84. void DisplayServerAndroid::tts_speak(const String &p_text, const String &p_voice, int p_volume, float p_pitch, float p_rate, int p_utterance_id, bool p_interrupt) {
  85. TTS_Android::speak(p_text, p_voice, p_volume, p_pitch, p_rate, p_utterance_id, p_interrupt);
  86. }
  87. void DisplayServerAndroid::tts_pause() {
  88. TTS_Android::pause();
  89. }
  90. void DisplayServerAndroid::tts_resume() {
  91. TTS_Android::resume();
  92. }
  93. void DisplayServerAndroid::tts_stop() {
  94. TTS_Android::stop();
  95. }
  96. void DisplayServerAndroid::clipboard_set(const String &p_text) {
  97. GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
  98. ERR_FAIL_NULL(godot_java);
  99. if (godot_java->has_set_clipboard()) {
  100. godot_java->set_clipboard(p_text);
  101. } else {
  102. DisplayServer::clipboard_set(p_text);
  103. }
  104. }
  105. String DisplayServerAndroid::clipboard_get() const {
  106. GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
  107. ERR_FAIL_NULL_V(godot_java, String());
  108. if (godot_java->has_get_clipboard()) {
  109. return godot_java->get_clipboard();
  110. } else {
  111. return DisplayServer::clipboard_get();
  112. }
  113. }
  114. bool DisplayServerAndroid::clipboard_has() const {
  115. GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
  116. ERR_FAIL_NULL_V(godot_java, false);
  117. if (godot_java->has_has_clipboard()) {
  118. return godot_java->has_clipboard();
  119. } else {
  120. return DisplayServer::clipboard_has();
  121. }
  122. }
  123. TypedArray<Rect2> DisplayServerAndroid::get_display_cutouts() const {
  124. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  125. ERR_FAIL_NULL_V(godot_io_java, Array());
  126. return godot_io_java->get_display_cutouts();
  127. }
  128. Rect2i DisplayServerAndroid::get_display_safe_area() const {
  129. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  130. ERR_FAIL_NULL_V(godot_io_java, Rect2i());
  131. return godot_io_java->get_display_safe_area();
  132. }
  133. void DisplayServerAndroid::screen_set_keep_on(bool p_enable) {
  134. GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
  135. ERR_FAIL_NULL(godot_java);
  136. godot_java->set_keep_screen_on(p_enable);
  137. keep_screen_on = p_enable;
  138. }
  139. bool DisplayServerAndroid::screen_is_kept_on() const {
  140. return keep_screen_on;
  141. }
  142. void DisplayServerAndroid::screen_set_orientation(DisplayServer::ScreenOrientation p_orientation, int p_screen) {
  143. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  144. ERR_FAIL_NULL(godot_io_java);
  145. godot_io_java->set_screen_orientation(p_orientation);
  146. }
  147. DisplayServer::ScreenOrientation DisplayServerAndroid::screen_get_orientation(int p_screen) const {
  148. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  149. ERR_FAIL_NULL_V(godot_io_java, SCREEN_LANDSCAPE);
  150. const int orientation = godot_io_java->get_screen_orientation();
  151. ERR_FAIL_INDEX_V_MSG(orientation, 7, SCREEN_LANDSCAPE, "Unrecognized screen orientation");
  152. return (ScreenOrientation)orientation;
  153. }
  154. int DisplayServerAndroid::get_screen_count() const {
  155. return 1;
  156. }
  157. Point2i DisplayServerAndroid::screen_get_position(int p_screen) const {
  158. return Point2i(0, 0);
  159. }
  160. Size2i DisplayServerAndroid::screen_get_size(int p_screen) const {
  161. return OS_Android::get_singleton()->get_display_size();
  162. }
  163. Rect2i DisplayServerAndroid::screen_get_usable_rect(int p_screen) const {
  164. Size2i display_size = OS_Android::get_singleton()->get_display_size();
  165. return Rect2i(0, 0, display_size.width, display_size.height);
  166. }
  167. int DisplayServerAndroid::screen_get_dpi(int p_screen) const {
  168. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  169. ERR_FAIL_NULL_V(godot_io_java, 0);
  170. return godot_io_java->get_screen_dpi();
  171. }
  172. float DisplayServerAndroid::screen_get_scale(int p_screen) const {
  173. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  174. ERR_FAIL_NULL_V(godot_io_java, 1.0f);
  175. return godot_io_java->get_scaled_density();
  176. }
  177. float DisplayServerAndroid::screen_get_refresh_rate(int p_screen) const {
  178. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  179. if (!godot_io_java) {
  180. ERR_PRINT("An error occurred while trying to get the screen refresh rate.");
  181. return SCREEN_REFRESH_RATE_FALLBACK;
  182. }
  183. return godot_io_java->get_screen_refresh_rate(SCREEN_REFRESH_RATE_FALLBACK);
  184. }
  185. bool DisplayServerAndroid::screen_is_touchscreen(int p_screen) const {
  186. return true;
  187. }
  188. void DisplayServerAndroid::virtual_keyboard_show(const String &p_existing_text, const Rect2 &p_screen_rect, VirtualKeyboardType p_type, int p_max_length, int p_cursor_start, int p_cursor_end) {
  189. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  190. ERR_FAIL_NULL(godot_io_java);
  191. if (godot_io_java->has_vk()) {
  192. godot_io_java->show_vk(p_existing_text, (int)p_type, p_max_length, p_cursor_start, p_cursor_end);
  193. } else {
  194. ERR_PRINT("Virtual keyboard not available");
  195. }
  196. }
  197. void DisplayServerAndroid::virtual_keyboard_hide() {
  198. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  199. ERR_FAIL_NULL(godot_io_java);
  200. if (godot_io_java->has_vk()) {
  201. godot_io_java->hide_vk();
  202. } else {
  203. ERR_PRINT("Virtual keyboard not available");
  204. }
  205. }
  206. int DisplayServerAndroid::virtual_keyboard_get_height() const {
  207. GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
  208. ERR_FAIL_NULL_V(godot_io_java, 0);
  209. return godot_io_java->get_vk_height();
  210. }
  211. void DisplayServerAndroid::window_set_window_event_callback(const Callable &p_callable, DisplayServer::WindowID p_window) {
  212. window_event_callback = p_callable;
  213. }
  214. void DisplayServerAndroid::window_set_input_event_callback(const Callable &p_callable, DisplayServer::WindowID p_window) {
  215. input_event_callback = p_callable;
  216. }
  217. void DisplayServerAndroid::window_set_input_text_callback(const Callable &p_callable, DisplayServer::WindowID p_window) {
  218. input_text_callback = p_callable;
  219. }
  220. void DisplayServerAndroid::window_set_rect_changed_callback(const Callable &p_callable, DisplayServer::WindowID p_window) {
  221. rect_changed_callback = p_callable;
  222. }
  223. void DisplayServerAndroid::window_set_drop_files_callback(const Callable &p_callable, DisplayServer::WindowID p_window) {
  224. // Not supported on Android.
  225. }
  226. void DisplayServerAndroid::_window_callback(const Callable &p_callable, const Variant &p_arg, bool p_deferred) const {
  227. if (!p_callable.is_null()) {
  228. const Variant *argp = &p_arg;
  229. Variant ret;
  230. Callable::CallError ce;
  231. if (p_deferred) {
  232. p_callable.callp((const Variant **)&argp, 1, ret, ce);
  233. } else {
  234. p_callable.call_deferredp((const Variant **)&argp, 1);
  235. }
  236. }
  237. }
  238. void DisplayServerAndroid::send_window_event(DisplayServer::WindowEvent p_event, bool p_deferred) const {
  239. _window_callback(window_event_callback, int(p_event), p_deferred);
  240. }
  241. void DisplayServerAndroid::send_input_event(const Ref<InputEvent> &p_event) const {
  242. _window_callback(input_event_callback, p_event);
  243. }
  244. void DisplayServerAndroid::send_input_text(const String &p_text) const {
  245. _window_callback(input_text_callback, p_text);
  246. }
  247. void DisplayServerAndroid::_dispatch_input_events(const Ref<InputEvent> &p_event) {
  248. DisplayServerAndroid::get_singleton()->send_input_event(p_event);
  249. }
  250. Vector<DisplayServer::WindowID> DisplayServerAndroid::get_window_list() const {
  251. Vector<WindowID> ret;
  252. ret.push_back(MAIN_WINDOW_ID);
  253. return ret;
  254. }
  255. DisplayServer::WindowID DisplayServerAndroid::get_window_at_screen_position(const Point2i &p_position) const {
  256. return MAIN_WINDOW_ID;
  257. }
  258. int64_t DisplayServerAndroid::window_get_native_handle(HandleType p_handle_type, WindowID p_window) const {
  259. ERR_FAIL_COND_V(p_window != MAIN_WINDOW_ID, 0);
  260. switch (p_handle_type) {
  261. case WINDOW_HANDLE: {
  262. return reinterpret_cast<int64_t>(static_cast<OS_Android *>(OS::get_singleton())->get_godot_java()->get_activity());
  263. }
  264. case WINDOW_VIEW: {
  265. return 0; // Not supported.
  266. }
  267. #ifdef GLES3_ENABLED
  268. case DISPLAY_HANDLE: {
  269. if (rendering_driver == "opengl3") {
  270. return reinterpret_cast<int64_t>(eglGetCurrentDisplay());
  271. }
  272. return 0;
  273. }
  274. case OPENGL_CONTEXT: {
  275. if (rendering_driver == "opengl3") {
  276. return reinterpret_cast<int64_t>(eglGetCurrentContext());
  277. }
  278. return 0;
  279. }
  280. #endif
  281. default: {
  282. return 0;
  283. }
  284. }
  285. }
  286. void DisplayServerAndroid::window_attach_instance_id(ObjectID p_instance, DisplayServer::WindowID p_window) {
  287. window_attached_instance_id = p_instance;
  288. }
  289. ObjectID DisplayServerAndroid::window_get_attached_instance_id(DisplayServer::WindowID p_window) const {
  290. return window_attached_instance_id;
  291. }
  292. void DisplayServerAndroid::window_set_title(const String &p_title, DisplayServer::WindowID p_window) {
  293. // Not supported on Android.
  294. }
  295. int DisplayServerAndroid::window_get_current_screen(DisplayServer::WindowID p_window) const {
  296. return SCREEN_OF_MAIN_WINDOW;
  297. }
  298. void DisplayServerAndroid::window_set_current_screen(int p_screen, DisplayServer::WindowID p_window) {
  299. // Not supported on Android.
  300. }
  301. Point2i DisplayServerAndroid::window_get_position(DisplayServer::WindowID p_window) const {
  302. return Point2i();
  303. }
  304. void DisplayServerAndroid::window_set_position(const Point2i &p_position, DisplayServer::WindowID p_window) {
  305. // Not supported on Android.
  306. }
  307. void DisplayServerAndroid::window_set_transient(DisplayServer::WindowID p_window, DisplayServer::WindowID p_parent) {
  308. // Not supported on Android.
  309. }
  310. void DisplayServerAndroid::window_set_max_size(const Size2i p_size, DisplayServer::WindowID p_window) {
  311. // Not supported on Android.
  312. }
  313. Size2i DisplayServerAndroid::window_get_max_size(DisplayServer::WindowID p_window) const {
  314. return Size2i();
  315. }
  316. void DisplayServerAndroid::window_set_min_size(const Size2i p_size, DisplayServer::WindowID p_window) {
  317. // Not supported on Android.
  318. }
  319. Size2i DisplayServerAndroid::window_get_min_size(DisplayServer::WindowID p_window) const {
  320. return Size2i();
  321. }
  322. void DisplayServerAndroid::window_set_size(const Size2i p_size, DisplayServer::WindowID p_window) {
  323. // Not supported on Android.
  324. }
  325. Size2i DisplayServerAndroid::window_get_size(DisplayServer::WindowID p_window) const {
  326. return OS_Android::get_singleton()->get_display_size();
  327. }
  328. Size2i DisplayServerAndroid::window_get_real_size(DisplayServer::WindowID p_window) const {
  329. return OS_Android::get_singleton()->get_display_size();
  330. }
  331. void DisplayServerAndroid::window_set_mode(DisplayServer::WindowMode p_mode, DisplayServer::WindowID p_window) {
  332. // Not supported on Android.
  333. }
  334. DisplayServer::WindowMode DisplayServerAndroid::window_get_mode(DisplayServer::WindowID p_window) const {
  335. return WINDOW_MODE_FULLSCREEN;
  336. }
  337. bool DisplayServerAndroid::window_is_maximize_allowed(DisplayServer::WindowID p_window) const {
  338. return false;
  339. }
  340. void DisplayServerAndroid::window_set_flag(DisplayServer::WindowFlags p_flag, bool p_enabled, DisplayServer::WindowID p_window) {
  341. // Not supported on Android.
  342. }
  343. bool DisplayServerAndroid::window_get_flag(DisplayServer::WindowFlags p_flag, DisplayServer::WindowID p_window) const {
  344. return false;
  345. }
  346. void DisplayServerAndroid::window_request_attention(DisplayServer::WindowID p_window) {
  347. // Not supported on Android.
  348. }
  349. void DisplayServerAndroid::window_move_to_foreground(DisplayServer::WindowID p_window) {
  350. // Not supported on Android.
  351. }
  352. bool DisplayServerAndroid::window_can_draw(DisplayServer::WindowID p_window) const {
  353. return true;
  354. }
  355. bool DisplayServerAndroid::can_any_window_draw() const {
  356. return true;
  357. }
  358. void DisplayServerAndroid::process_events() {
  359. Input::get_singleton()->flush_buffered_events();
  360. }
  361. Vector<String> DisplayServerAndroid::get_rendering_drivers_func() {
  362. Vector<String> drivers;
  363. #ifdef GLES3_ENABLED
  364. drivers.push_back("opengl3");
  365. #endif
  366. #ifdef VULKAN_ENABLED
  367. drivers.push_back("vulkan");
  368. #endif
  369. return drivers;
  370. }
  371. DisplayServer *DisplayServerAndroid::create_func(const String &p_rendering_driver, DisplayServer::WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, Error &r_error) {
  372. DisplayServer *ds = memnew(DisplayServerAndroid(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, r_error));
  373. if (r_error != OK) {
  374. OS::get_singleton()->alert("Your video card driver does not support any of the supported Vulkan versions.", "Unable to initialize Video driver");
  375. if (p_rendering_driver == "vulkan") {
  376. OS::get_singleton()->alert("Your video card driver does not support the selected Vulkan version.\n"
  377. "Please try exporting your game using the gl_compatibility renderer.",
  378. "Unable to initialize Video driver");
  379. } else {
  380. OS::get_singleton()->alert("Your video card driver does not support OpenGL ES 3.0.",
  381. "Unable to initialize Video driver");
  382. }
  383. }
  384. return ds;
  385. }
  386. void DisplayServerAndroid::register_android_driver() {
  387. register_create_function("android", create_func, get_rendering_drivers_func);
  388. }
  389. void DisplayServerAndroid::reset_window() {
  390. #if defined(VULKAN_ENABLED)
  391. if (rendering_driver == "vulkan") {
  392. ANativeWindow *native_window = OS_Android::get_singleton()->get_native_window();
  393. ERR_FAIL_NULL(native_window);
  394. ERR_FAIL_NULL(context_vulkan);
  395. VSyncMode last_vsync_mode = context_vulkan->get_vsync_mode(MAIN_WINDOW_ID);
  396. context_vulkan->window_destroy(MAIN_WINDOW_ID);
  397. Size2i display_size = OS_Android::get_singleton()->get_display_size();
  398. if (context_vulkan->window_create(native_window, last_vsync_mode, display_size.width, display_size.height) != OK) {
  399. memdelete(context_vulkan);
  400. context_vulkan = nullptr;
  401. ERR_FAIL_MSG("Failed to reset Vulkan window.");
  402. }
  403. }
  404. #endif
  405. }
  406. void DisplayServerAndroid::notify_surface_changed(int p_width, int p_height) {
  407. if (rect_changed_callback.is_null()) {
  408. return;
  409. }
  410. const Variant size = Rect2i(0, 0, p_width, p_height);
  411. const Variant *sizep = &size;
  412. Variant ret;
  413. Callable::CallError ce;
  414. rect_changed_callback.callp(reinterpret_cast<const Variant **>(&sizep), 1, ret, ce);
  415. }
  416. DisplayServerAndroid::DisplayServerAndroid(const String &p_rendering_driver, DisplayServer::WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, Error &r_error) {
  417. rendering_driver = p_rendering_driver;
  418. keep_screen_on = GLOBAL_GET("display/window/energy_saving/keep_screen_on");
  419. #if defined(GLES3_ENABLED)
  420. if (rendering_driver == "opengl3") {
  421. RasterizerGLES3::make_current();
  422. }
  423. #endif
  424. #if defined(VULKAN_ENABLED)
  425. context_vulkan = nullptr;
  426. rendering_device_vulkan = nullptr;
  427. if (rendering_driver == "vulkan") {
  428. ANativeWindow *native_window = OS_Android::get_singleton()->get_native_window();
  429. ERR_FAIL_NULL(native_window);
  430. context_vulkan = memnew(VulkanContextAndroid);
  431. if (context_vulkan->initialize() != OK) {
  432. memdelete(context_vulkan);
  433. context_vulkan = nullptr;
  434. ERR_FAIL_MSG("Failed to initialize Vulkan context");
  435. }
  436. Size2i display_size = OS_Android::get_singleton()->get_display_size();
  437. if (context_vulkan->window_create(native_window, p_vsync_mode, display_size.width, display_size.height) != OK) {
  438. memdelete(context_vulkan);
  439. context_vulkan = nullptr;
  440. ERR_FAIL_MSG("Failed to create Vulkan window.");
  441. }
  442. rendering_device_vulkan = memnew(RenderingDeviceVulkan);
  443. rendering_device_vulkan->initialize(context_vulkan);
  444. RendererCompositorRD::make_current();
  445. }
  446. #endif
  447. Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events);
  448. Input::get_singleton()->set_use_input_buffering(true); // Needed because events will come directly from the UI thread
  449. r_error = OK;
  450. }
  451. DisplayServerAndroid::~DisplayServerAndroid() {
  452. #if defined(VULKAN_ENABLED)
  453. if (rendering_driver == "vulkan") {
  454. if (rendering_device_vulkan) {
  455. rendering_device_vulkan->finalize();
  456. memdelete(rendering_device_vulkan);
  457. }
  458. if (context_vulkan) {
  459. memdelete(context_vulkan);
  460. }
  461. }
  462. #endif
  463. }
  464. void DisplayServerAndroid::process_accelerometer(const Vector3 &p_accelerometer) {
  465. Input::get_singleton()->set_accelerometer(p_accelerometer);
  466. }
  467. void DisplayServerAndroid::process_gravity(const Vector3 &p_gravity) {
  468. Input::get_singleton()->set_gravity(p_gravity);
  469. }
  470. void DisplayServerAndroid::process_magnetometer(const Vector3 &p_magnetometer) {
  471. Input::get_singleton()->set_magnetometer(p_magnetometer);
  472. }
  473. void DisplayServerAndroid::process_gyroscope(const Vector3 &p_gyroscope) {
  474. Input::get_singleton()->set_gyroscope(p_gyroscope);
  475. }
  476. void DisplayServerAndroid::mouse_set_mode(MouseMode p_mode) {
  477. if (!OS_Android::get_singleton()->get_godot_java()->get_godot_view()->can_update_pointer_icon() || !OS_Android::get_singleton()->get_godot_java()->get_godot_view()->can_capture_pointer()) {
  478. return;
  479. }
  480. if (mouse_mode == p_mode) {
  481. return;
  482. }
  483. if (p_mode == MouseMode::MOUSE_MODE_HIDDEN) {
  484. OS_Android::get_singleton()->get_godot_java()->get_godot_view()->set_pointer_icon(CURSOR_TYPE_NULL);
  485. } else {
  486. cursor_set_shape(cursor_shape);
  487. }
  488. if (p_mode == MouseMode::MOUSE_MODE_CAPTURED) {
  489. OS_Android::get_singleton()->get_godot_java()->get_godot_view()->request_pointer_capture();
  490. } else {
  491. OS_Android::get_singleton()->get_godot_java()->get_godot_view()->release_pointer_capture();
  492. }
  493. mouse_mode = p_mode;
  494. }
  495. DisplayServer::MouseMode DisplayServerAndroid::mouse_get_mode() const {
  496. return mouse_mode;
  497. }
  498. Point2i DisplayServerAndroid::mouse_get_position() const {
  499. return Input::get_singleton()->get_mouse_position();
  500. }
  501. MouseButton DisplayServerAndroid::mouse_get_button_state() const {
  502. return (MouseButton)Input::get_singleton()->get_mouse_button_mask();
  503. }
  504. void DisplayServerAndroid::_cursor_set_shape_helper(CursorShape p_shape, bool force) {
  505. if (!OS_Android::get_singleton()->get_godot_java()->get_godot_view()->can_update_pointer_icon()) {
  506. return;
  507. }
  508. if (cursor_shape == p_shape && !force) {
  509. return;
  510. }
  511. cursor_shape = p_shape;
  512. if (mouse_mode == MouseMode::MOUSE_MODE_VISIBLE || mouse_mode == MouseMode::MOUSE_MODE_CONFINED) {
  513. OS_Android::get_singleton()->get_godot_java()->get_godot_view()->set_pointer_icon(android_cursors[cursor_shape]);
  514. }
  515. }
  516. void DisplayServerAndroid::cursor_set_shape(DisplayServer::CursorShape p_shape) {
  517. _cursor_set_shape_helper(p_shape);
  518. }
  519. DisplayServer::CursorShape DisplayServerAndroid::cursor_get_shape() const {
  520. return cursor_shape;
  521. }
  522. void DisplayServerAndroid::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
  523. String cursor_path = p_cursor.is_valid() ? p_cursor->get_path() : "";
  524. if (!cursor_path.is_empty()) {
  525. cursor_path = ProjectSettings::get_singleton()->globalize_path(cursor_path);
  526. }
  527. OS_Android::get_singleton()->get_godot_java()->get_godot_view()->configure_pointer_icon(android_cursors[cursor_shape], cursor_path, p_hotspot);
  528. _cursor_set_shape_helper(p_shape, true);
  529. }
  530. void DisplayServerAndroid::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) {
  531. #if defined(VULKAN_ENABLED)
  532. context_vulkan->set_vsync_mode(p_window, p_vsync_mode);
  533. #endif
  534. }
  535. DisplayServer::VSyncMode DisplayServerAndroid::window_get_vsync_mode(WindowID p_window) const {
  536. #if defined(VULKAN_ENABLED)
  537. return context_vulkan->get_vsync_mode(p_window);
  538. #else
  539. return DisplayServer::VSYNC_ENABLED;
  540. #endif
  541. }
  542. void DisplayServerAndroid::reset_swap_buffers_flag() {
  543. swap_buffers_flag = false;
  544. }
  545. bool DisplayServerAndroid::should_swap_buffers() const {
  546. return swap_buffers_flag;
  547. }
  548. void DisplayServerAndroid::swap_buffers() {
  549. swap_buffers_flag = true;
  550. }
  551. void DisplayServerAndroid::set_native_icon(const String &p_filename) {
  552. // NOT SUPPORTED
  553. }
  554. void DisplayServerAndroid::set_icon(const Ref<Image> &p_icon) {
  555. // NOT SUPPORTED
  556. }