display_server_ios.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**************************************************************************/
  2. /* display_server_ios.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #import "display_server_ios.h"
  31. #import "device_metrics.h"
  32. #import <UIKit/UIKit.h>
  33. #import <sys/utsname.h>
  34. DisplayServerIOS *DisplayServerIOS::get_singleton() {
  35. return (DisplayServerIOS *)DisplayServerAppleEmbedded::get_singleton();
  36. }
  37. DisplayServerIOS::DisplayServerIOS(const String &p_rendering_driver, WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) :
  38. DisplayServerAppleEmbedded(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, p_parent_window, r_error) {
  39. }
  40. DisplayServerIOS::~DisplayServerIOS() {
  41. }
  42. DisplayServer *DisplayServerIOS::create_func(const String &p_rendering_driver, WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error) {
  43. return memnew(DisplayServerIOS(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, p_parent_window, r_error));
  44. }
  45. void DisplayServerIOS::register_ios_driver() {
  46. register_create_function("iOS", create_func, get_rendering_drivers_func);
  47. }
  48. String DisplayServerIOS::get_name() const {
  49. return "iOS";
  50. }
  51. int DisplayServerIOS::screen_get_dpi(int p_screen) const {
  52. p_screen = _get_screen_index(p_screen);
  53. int screen_count = get_screen_count();
  54. ERR_FAIL_INDEX_V(p_screen, screen_count, 72);
  55. struct utsname systemInfo;
  56. uname(&systemInfo);
  57. NSString *string = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
  58. NSDictionary *iOSModelToDPI = [GDTDeviceMetrics dpiList];
  59. for (NSArray *keyArray in iOSModelToDPI) {
  60. if ([keyArray containsObject:string]) {
  61. NSNumber *value = iOSModelToDPI[keyArray];
  62. return [value intValue];
  63. }
  64. }
  65. // If device wasn't found in dictionary
  66. // make a best guess from device metrics.
  67. CGFloat scale = [UIScreen mainScreen].scale;
  68. UIUserInterfaceIdiom idiom = [UIDevice currentDevice].userInterfaceIdiom;
  69. switch (idiom) {
  70. case UIUserInterfaceIdiomPad:
  71. return scale == 2 ? 264 : 132;
  72. case UIUserInterfaceIdiomPhone: {
  73. if (scale == 3) {
  74. CGFloat nativeScale = [UIScreen mainScreen].nativeScale;
  75. return nativeScale == 3 ? 458 : 401;
  76. }
  77. return 326;
  78. }
  79. default:
  80. return 72;
  81. }
  82. }
  83. float DisplayServerIOS::screen_get_refresh_rate(int p_screen) const {
  84. p_screen = _get_screen_index(p_screen);
  85. int screen_count = get_screen_count();
  86. ERR_FAIL_INDEX_V(p_screen, screen_count, SCREEN_REFRESH_RATE_FALLBACK);
  87. float fps = [UIScreen mainScreen].maximumFramesPerSecond;
  88. if ([NSProcessInfo processInfo].lowPowerModeEnabled) {
  89. fps = 60;
  90. }
  91. return fps;
  92. }
  93. float DisplayServerIOS::screen_get_scale(int p_screen) const {
  94. p_screen = _get_screen_index(p_screen);
  95. int screen_count = get_screen_count();
  96. ERR_FAIL_INDEX_V(p_screen, screen_count, 1.0f);
  97. return [UIScreen mainScreen].scale;
  98. }