UnityView.mm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "UnityView.h"
  2. #include "UnityAppController.h"
  3. #include "UnityAppController+Rendering.h"
  4. #include "OrientationSupport.h"
  5. #include "Unity/DisplayManager.h"
  6. #include "Unity/UnityMetalSupport.h"
  7. #include "Unity/ObjCRuntime.h"
  8. extern bool _renderingInited;
  9. extern bool _unityAppReady;
  10. extern bool _skipPresent;
  11. @implementation UnityView
  12. {
  13. CGSize _surfaceSize;
  14. }
  15. @synthesize contentOrientation = _curOrientation;
  16. - (void)onUpdateSurfaceSize:(CGSize)size
  17. {
  18. _surfaceSize = size;
  19. CGSize systemRenderSize = CGSizeMake(size.width * self.contentScaleFactor, size.height * self.contentScaleFactor);
  20. _curOrientation = (ScreenOrientation)UnityReportResizeView((unsigned)systemRenderSize.width, (unsigned)systemRenderSize.height, _curOrientation);
  21. ReportSafeAreaChangeForView(self);
  22. }
  23. - (void)boundsUpdated
  24. {
  25. [self onUpdateSurfaceSize: self.bounds.size];
  26. }
  27. - (void)initImpl:(CGRect)frame scaleFactor:(CGFloat)scale
  28. {
  29. #if !PLATFORM_TVOS
  30. self.multipleTouchEnabled = YES;
  31. self.exclusiveTouch = YES;
  32. #endif
  33. self.contentScaleFactor = scale;
  34. self.isAccessibilityElement = TRUE;
  35. self.accessibilityTraits = UIAccessibilityTraitAllowsDirectInteraction;
  36. #if UNITY_TVOS
  37. _curOrientation = UNITY_TVOS_ORIENTATION;
  38. #elif UNITY_VISIONOS
  39. _curOrientation = UNITY_VISIONOS_ORIENTATION;
  40. #endif
  41. [self onUpdateSurfaceSize: frame.size];
  42. }
  43. - (id)initWithFrame:(CGRect)frame scaleFactor:(CGFloat)scale;
  44. {
  45. if ((self = [super initWithFrame: frame]))
  46. [self initImpl: frame scaleFactor: scale];
  47. return self;
  48. }
  49. - (id)initWithFrame:(CGRect)frame
  50. {
  51. if ((self = [super initWithFrame: frame]))
  52. [self initImpl: frame scaleFactor: 1.0f];
  53. return self;
  54. }
  55. - (id)initFromMainScreen
  56. {
  57. #if !PLATFORM_VISIONOS
  58. CGRect frame = [UIScreen mainScreen].bounds;
  59. CGFloat scale = UnityScreenScaleFactor([UIScreen mainScreen]);
  60. #else
  61. CGRect frame = CGRectMake(0.0f, 0.0f, 1920.0f, 1080.0f);
  62. CGFloat scale = 1.0f;
  63. #endif
  64. if ((self = [super initWithFrame: frame]))
  65. [self initImpl: frame scaleFactor: scale];
  66. return self;
  67. }
  68. - (void)layoutSubviews
  69. {
  70. if (_surfaceSize.width != self.bounds.size.width || _surfaceSize.height != self.bounds.size.height)
  71. _shouldRecreateView = YES;
  72. [self onUpdateSurfaceSize: self.bounds.size];
  73. for (UIView* subView in self.subviews)
  74. {
  75. if ([subView respondsToSelector: @selector(onUnityUpdateViewLayout)])
  76. [subView performSelector: @selector(onUnityUpdateViewLayout)];
  77. }
  78. [super layoutSubviews];
  79. }
  80. - (void)safeAreaInsetsDidChange
  81. {
  82. ReportSafeAreaChangeForView(self);
  83. }
  84. - (void)recreateRenderingSurfaceIfNeeded
  85. {
  86. #if !PLATFORM_VISIONOS
  87. float requestedContentScaleFactor = UnityScreenScaleFactor([UIScreen mainScreen]);
  88. #else
  89. float requestedContentScaleFactor = 1.0f;
  90. #endif
  91. if (abs(requestedContentScaleFactor - self.contentScaleFactor) > FLT_EPSILON)
  92. {
  93. self.contentScaleFactor = requestedContentScaleFactor;
  94. [self onUpdateSurfaceSize: self.bounds.size];
  95. }
  96. unsigned requestedW, requestedH; UnityGetRenderingResolution(&requestedW, &requestedH);
  97. int requestedMSAA = UnityGetDesiredMSAASampleCount(1);
  98. int requestedSRGB = UnityGetSRGBRequested();
  99. int requestedWideColor = UnityGetWideColorRequested();
  100. int requestedHDR = UnityGetHDRModeRequested();
  101. int requestedMemorylessDepth = UnityMetalMemorylessDepth();
  102. UnityDisplaySurfaceBase* surf = GetMainDisplaySurface();
  103. if (_shouldRecreateView == YES
  104. || surf->targetW != requestedW || surf->targetH != requestedH
  105. || surf->disableDepthAndStencil != UnityDisableDepthAndStencilBuffers()
  106. || surf->msaaSamples != requestedMSAA
  107. || surf->srgb != requestedSRGB
  108. || surf->wideColor != requestedWideColor
  109. || surf->hdr != requestedHDR
  110. || surf->memorylessDepth != requestedMemorylessDepth
  111. )
  112. {
  113. [self recreateRenderingSurface];
  114. }
  115. }
  116. - (void)recreateRenderingSurface
  117. {
  118. if (_renderingInited)
  119. {
  120. unsigned requestedW, requestedH;
  121. UnityGetRenderingResolution(&requestedW, &requestedH);
  122. RenderingSurfaceParams params =
  123. {
  124. .msaaSampleCount = UnityGetDesiredMSAASampleCount(1),
  125. .renderW = (int)requestedW,
  126. .renderH = (int)requestedH,
  127. .srgb = UnityGetSRGBRequested(),
  128. .wideColor = UnityGetWideColorRequested(),
  129. .hdr = UnityGetHDRModeRequested(),
  130. .metalFramebufferOnly = UnityMetalFramebufferOnly(),
  131. .metalMemorylessDepth = UnityMetalMemorylessDepth(),
  132. .disableDepthAndStencil = UnityDisableDepthAndStencilBuffers(),
  133. .useCVTextureCache = 0,
  134. };
  135. APP_CONTROLLER_RENDER_PLUGIN_METHOD_ARG(onBeforeMainDisplaySurfaceRecreate, &params);
  136. [GetMainDisplay() recreateSurface: params];
  137. // actually poke unity about updated back buffer and notify that extents were changed
  138. UnityReportBackbufferChange(GetMainDisplaySurface()->unityColorBuffer, GetMainDisplaySurface()->unityDepthBuffer);
  139. APP_CONTROLLER_RENDER_PLUGIN_METHOD(onAfterMainDisplaySurfaceRecreate);
  140. if (_unityAppReady)
  141. {
  142. // seems like ios sometimes got confused about abrupt swap chain destroy
  143. // draw 2 times to fill "both" buffers (we assume double buffering)
  144. // present only once to make sure correct image goes to CA
  145. // if we are calling this from inside repaint, second draw and present will be done automatically
  146. _skipPresent = true;
  147. // we may be asked to recreate surface while paused (in the background)
  148. // like changing device orientation while showing some system dialog
  149. // in this case we still want to redraw contents to avoid view stretching
  150. const bool wasPaused = UnityIsPaused();
  151. // please note that we still need to pretend we did come from displaylink to make sure vsync magic works
  152. // NOTE: unity does handle "draw frame with exact same timestamp" just fine
  153. UnityDisplayLinkCallback(GetAppController().unityDisplayLink.timestamp);
  154. UnityRepaint();
  155. // if we are inside actual repaint: we are done (second draw and present will be done automatically)
  156. // otherwise we need the second repaint, actualy doing present this time
  157. _skipPresent = false;
  158. if (_viewIsRotating || wasPaused)
  159. {
  160. UnityDisplayLinkCallback(GetAppController().unityDisplayLink.timestamp);
  161. UnityRepaint();
  162. }
  163. }
  164. }
  165. _shouldRecreateView = NO;
  166. }
  167. @end
  168. @implementation UnityView (Deprecated)
  169. - (void)recreateGLESSurfaceIfNeeded { [self recreateRenderingSurfaceIfNeeded]; }
  170. - (void)recreateGLESSurface { [self recreateRenderingSurface]; }
  171. @end
  172. static Class UnityRenderingView_LayerClassMTL(id self_, SEL _cmd)
  173. {
  174. return NSClassFromString(@"CAMetalLayer");
  175. }
  176. static Class UnityRenderingView_LayerClassNULL(id self_, SEL _cmd)
  177. {
  178. return NSClassFromString(@"CALayer");
  179. }
  180. @implementation UnityRenderingView
  181. + (Class)layerClass
  182. {
  183. return nil;
  184. }
  185. + (void)InitializeForAPI:(UnityRenderingAPI)api
  186. {
  187. IMP layerClassImpl = api == apiMetal ? (IMP)UnityRenderingView_LayerClassMTL : (IMP)UnityRenderingView_LayerClassNULL;
  188. class_replaceMethod(object_getClass([UnityRenderingView class]), @selector(layerClass), layerClassImpl, UIView_LayerClass_Enc);
  189. }
  190. @end
  191. void ReportSafeAreaChangeForView(UIView* view)
  192. {
  193. CGRect safeArea = ComputeSafeArea(view);
  194. UnityReportSafeAreaChange(safeArea.origin.x, safeArea.origin.y,
  195. safeArea.size.width, safeArea.size.height);
  196. #if !PLATFORM_VISIONOS
  197. if (UnityDeviceHasCutout())
  198. {
  199. CGSize cutoutSizeRatio = GetCutoutToScreenRatio();
  200. if (!CGSizeEqualToSize(CGSizeZero, cutoutSizeRatio))
  201. {
  202. const float w = ([UIScreen mainScreen].nativeBounds.size.width * cutoutSizeRatio.width);
  203. const float h = ([UIScreen mainScreen].nativeBounds.size.height * cutoutSizeRatio.height);
  204. // Apple's cutouts are currently centred on the horizontal, and stuck to the top of the vertical, hence this positioning.
  205. const float x = (([UIScreen mainScreen].nativeBounds.size.width - w) / 2);
  206. const float y = ([UIScreen mainScreen].nativeBounds.size.height - h);
  207. UnityReportDisplayCutouts(&x, &y, &w, &h, 1);
  208. return;
  209. }
  210. }
  211. #endif
  212. UnityReportDisplayCutouts(nullptr, nullptr, nullptr, nullptr, 0);
  213. }
  214. CGRect ComputeSafeArea(UIView* view)
  215. {
  216. CGSize screenSize = view.bounds.size;
  217. CGRect screenRect = CGRectMake(0, 0, screenSize.width, screenSize.height);
  218. UIEdgeInsets insets = [view safeAreaInsets];
  219. float insetLeft = insets.left, insetBottom = insets.bottom;
  220. float insetWidth = insetLeft + insets.right, insetHeight = insetBottom + insets.top;
  221. #if PLATFORM_IOS && !PLATFORM_VISIONOS
  222. // pre-iOS 15 there is a bug with safeAreaInsets when coupled with the way unity handles forced orientation
  223. // when we create/show new ViewController with fixed orientation, safeAreaInsets include status bar always
  224. // alas, we did not find a good way to work around that (this can be seen even in View Debugging: Safe Area would have status bar accounted for)
  225. // we know for sure that status bar height is 20 (at least on ios16 or older), so we can check if the safe area
  226. // includes inset of this size while status bar should be hidden, resetting vertical insets in this case
  227. if (@available(iOS 15, *))
  228. {
  229. // everything works as expected
  230. }
  231. else
  232. {
  233. bool isStatusBarHidden = false;
  234. if (@available(iOS 13, *))
  235. isStatusBarHidden = view.window.windowScene.statusBarManager.statusBarHidden;
  236. else
  237. isStatusBarHidden = [UIApplication sharedApplication].statusBarHidden;
  238. if (isStatusBarHidden && fabsf(insetHeight - 20) < 1e-6f)
  239. insetHeight = insetBottom = 0.0f;
  240. }
  241. #endif
  242. // Unity uses bottom left as the origin
  243. screenRect = CGRectOffset(screenRect, insetLeft, insetBottom);
  244. screenRect.size.width -= insetWidth;
  245. screenRect.size.height -= insetHeight;
  246. const float scale = view.contentScaleFactor;
  247. // Truncate safe area size because in some cases (for example when Display zoom is turned on)
  248. // it might become larger than Screen.width/height which are returned as ints.
  249. screenRect.origin.x = (unsigned)(screenRect.origin.x * scale);
  250. screenRect.origin.y = (unsigned)(screenRect.origin.y * scale);
  251. screenRect.size.width = (unsigned)(screenRect.size.width * scale);
  252. screenRect.size.height = (unsigned)(screenRect.size.height * scale);
  253. return screenRect;
  254. }
  255. // Apple does not provide the cutout width and height in points/pixels. They *do* however list the
  256. // size of the cutout and screen in mm for accessory makers. We can use this information to calculate the percentage of the screen is cutout.
  257. // This information can be found here - https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf
  258. CGSize GetCutoutToScreenRatio()
  259. {
  260. switch (UnityDeviceGeneration())
  261. {
  262. case deviceiPhone14:
  263. return CGSizeMake(0.415, 0.04);
  264. case deviceiPhone14Plus:
  265. return CGSizeMake(0.377, 0.036);
  266. case deviceiPhone14Pro:
  267. return CGSizeMake(0.318, 0.062);
  268. case deviceiPhone14ProMax:
  269. return CGSizeMake(0.292, 0.052);
  270. case deviceiPhone13ProMax:
  271. return CGSizeMake(0.373, 0.036);
  272. case deviceiPhone13Pro:
  273. case deviceiPhone13:
  274. return CGSizeMake(0.4148, 0.0399);
  275. case deviceiPhone13Mini:
  276. return CGSizeMake(0.4644, 0.0462);
  277. case deviceiPhone12ProMax:
  278. return CGSizeMake(0.4897, 0.0346);
  279. case deviceiPhone12Pro:
  280. case deviceiPhone12:
  281. return CGSizeMake(0.5393, 0.0379);
  282. case deviceiPhone12Mini:
  283. return CGSizeMake(0.604, 0.0424);
  284. case deviceiPhone11ProMax:
  285. return CGSizeMake(0.5057, 0.0335);
  286. case deviceiPhone11Pro:
  287. return CGSizeMake(0.5583, 0.037);
  288. case deviceiPhone11:
  289. case deviceiPhoneXR:
  290. return CGSizeMake(0.5568, 0.0398);
  291. case deviceiPhoneXSMax:
  292. return CGSizeMake(0.4884, 0.0333);
  293. case deviceiPhoneX:
  294. case deviceiPhoneXS:
  295. return CGSizeMake(0.5391, 0.0368);
  296. default:
  297. NSCAssert(!UnityDeviceHasCutout(), @"Device has a cutout, but no ratio has been added for it.");
  298. return CGSizeZero;
  299. }
  300. }