@preeternal/react-native-godot 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,7 +28,6 @@
28
28
 
29
29
  #include <cmath>
30
30
 
31
- #include <libgodot/libgodot.h>
32
31
  #include <godot_cpp/classes/display_server_embedded.hpp>
33
32
  #include <godot_cpp/classes/engine.hpp>
34
33
  #include <godot_cpp/classes/godot_instance.hpp>
@@ -58,18 +57,23 @@ static NSHashTable<UIView *> *_views = [NSHashTable weakObjectsHashTable];
58
57
  static __weak UIView *_currentView = nil;
59
58
 
60
59
  @interface RTNGodotView () <RCTRTNGodotViewViewProtocol>
60
+ - (void)invalidateAttachment;
61
+ - (bool)ownsRenderingLayer;
62
+ - (void)addToGodotView;
63
+ - (void)removeFromGodotView:(bool)addAfter unregister:(bool)unregister;
61
64
  @end
62
65
  @implementation RTNGodotView {
63
66
  NSString *_windowName;
64
67
  CALayer *_renderingLayer;
65
68
  uint64_t _windowId;
66
- godot::Ref<godot::RenderingNativeSurface> _nativeSurface;
69
+ uint64_t _engineGeneration;
70
+ double _contentScaleFactor;
67
71
  std::vector<UITouch *> _touches;
68
72
  bool _propsUpdated;
69
73
  bool _instanceCallbackRegistered;
70
74
  bool _isMounted;
71
- uint64_t _attachmentGeneration;
72
- uint64_t _resizeGeneration;
75
+ std::atomic<uint64_t> _attachmentGeneration;
76
+ std::atomic<uint64_t> _resizeGeneration;
73
77
  godot::Vector2i _lastRequestedPixelSize;
74
78
  }
75
79
 
@@ -77,67 +81,60 @@ static __weak UIView *_currentView = nil;
77
81
  return NO;
78
82
  }
79
83
 
80
- + (void)removeMainLayerFromGodotView:(UIView *)view {
81
- if (![_views containsObject:view]) {
84
+ + (void)removeMainLayerFromGodotView:(RTNGodotView *)view promote:(bool)promote {
85
+ // The module has already cleared its layer on Stop. The view owns this reference.
86
+ CALayer *mainLayer = view->_renderingLayer;
87
+ if (promote) {
88
+ [_views removeObject:view];
89
+ }
90
+ if (_currentView != view) {
82
91
  return;
83
92
  }
84
-
85
- CALayer *mainLayer = (__bridge CALayer *)GodotModule::get_singleton()->get_main_rendering_layer();
86
-
87
- if (view == _currentView) {
88
- if (mainLayer) {
89
- [mainLayer removeFromSuperlayer];
90
- }
93
+ if (mainLayer.superlayer == view.layer) {
94
+ [mainLayer removeFromSuperlayer];
91
95
  }
92
- [_views removeObject:view];
93
-
94
- if (_views.count > 0) {
95
- RTNGodotView *nextView = (RTNGodotView *)_views.allObjects.lastObject;
96
- _currentView = nextView;
97
- if (mainLayer) {
98
- [CATransaction begin];
99
- [CATransaction setDisableActions:YES];
100
- mainLayer.hidden = YES;
101
- [nextView.layer addSublayer:mainLayer];
102
- [CATransaction commit];
103
- nextView->_lastRequestedPixelSize = godot::Vector2i();
104
- [nextView setNeedsLayout];
96
+ // Keep the preferred host across Stop/Start, regardless of callback order.
97
+ if (!promote) {
98
+ return;
99
+ }
100
+ _currentView = nil;
101
+ const uint64_t generation = GodotModule::get_singleton()->get_instance_generation();
102
+ for (RTNGodotView *nextView in _views.allObjects) {
103
+ if (!nextView->_isMounted || nextView->_engineGeneration != generation ||
104
+ generation == 0 || !nextView->_renderingLayer) {
105
+ continue;
105
106
  }
106
- } else {
107
- _currentView = nil;
107
+ [nextView invalidateAttachment];
108
+ _currentView = nextView;
109
+ // The departing host may not have received the new engine's layer yet.
110
+ CALayer *nextLayer = nextView->_renderingLayer;
111
+ [CATransaction begin];
112
+ [CATransaction setDisableActions:YES];
113
+ nextLayer.hidden = YES;
114
+ [nextView.layer addSublayer:nextLayer];
115
+ [CATransaction commit];
116
+ nextView->_lastRequestedPixelSize = godot::Vector2i();
117
+ [nextView setNeedsLayout];
118
+ break;
108
119
  }
109
120
  }
110
121
 
111
- + (CALayer *)addMainLayerToGodotView:(UIView *)view {
112
- godot::Ref<godot::RenderingNativeSurface> nativeSurface = GodotModule::get_singleton()->get_main_rendering_surface();
113
- if (nativeSurface.is_null()) {
114
- return nil;
115
- }
116
- godot::Ref<godot::RenderingNativeSurfaceApple> appleSurface = godot::Object::cast_to<godot::RenderingNativeSurfaceApple>(*nativeSurface);
117
- if (appleSurface.is_null()) {
118
- return nil;
119
- }
120
- CALayer *mainLayer = (__bridge CALayer *)(void *)appleSurface->get_layer();
121
- if (!mainLayer) {
122
- return nil;
123
- }
124
- if (![_views containsObject:view]) {
125
- [_views addObject:view];
122
+ + (void)addMainLayer:(CALayer *)mainLayer toGodotView:(RTNGodotView *)view {
123
+ const bool newHost = ![_views containsObject:view];
124
+ [_views addObject:view];
125
+ if (_currentView && _currentView != view && !newHost) {
126
+ return;
126
127
  }
127
- if (mainLayer.superlayer != nil) {
128
- [mainLayer removeFromSuperlayer];
128
+ if (_currentView && _currentView != view) {
129
+ [(RTNGodotView *)_currentView invalidateAttachment];
129
130
  }
130
-
131
- [view.layer addSublayer:mainLayer];
132
131
  [CATransaction begin];
133
132
  [CATransaction setDisableActions:YES];
133
+ [mainLayer removeFromSuperlayer];
134
+ [view.layer addSublayer:mainLayer];
134
135
  mainLayer.hidden = YES;
135
- mainLayer.frame = view.bounds;
136
136
  [CATransaction commit];
137
- [view setNeedsLayout];
138
-
139
137
  _currentView = view;
140
- return mainLayer;
141
138
  }
142
139
 
143
140
  - (instancetype)init {
@@ -158,6 +155,8 @@ static __weak UIView *_currentView = nil;
158
155
 
159
156
  _renderingLayer = nil;
160
157
  _windowId = 0;
158
+ _engineGeneration = 0;
159
+ _contentScaleFactor = 1.0;
161
160
  _touches.reserve(MAX_TOUCH_COUNT);
162
161
  for (int i = 0; i < MAX_TOUCH_COUNT; ++i) {
163
162
  _touches.push_back(nil);
@@ -181,10 +180,12 @@ static __weak UIView *_currentView = nil;
181
180
  }
182
181
 
183
182
  NSLog(@"Setting windowName to: %@", n);
183
+ if (_isMounted) {
184
+ [self removeFromGodotView:false unregister:true];
185
+ }
184
186
  _windowName = n;
185
- if (self.superview) {
186
- // Remove and add again
187
- [self removeFromGodotView:true unregister:true];
187
+ if (_isMounted) {
188
+ [self addToGodotView];
188
189
  }
189
190
  }
190
191
 
@@ -208,211 +209,171 @@ static __weak UIView *_currentView = nil;
208
209
  GodotModule::get_singleton()->unregisterWindowUpdateCallback((__bridge void *)self);
209
210
  }
210
211
  if ([NSThread isMainThread]) {
211
- [RTNGodotView removeMainLayerFromGodotView:self];
212
+ [RTNGodotView removeMainLayerFromGodotView:self promote:true];
212
213
  }
213
214
  }
214
215
 
215
- - (void)addToGodotView {
216
- NSLog(@"RTNGodotView: Adding Godot View: %@, windowName: %@", self, _windowName);
217
- if (!_isMounted) {
216
+ - (void)invalidateAttachment {
217
+ ++_attachmentGeneration;
218
+ ++_resizeGeneration;
219
+ std::vector<int> ids;
220
+ for (int i = 0; i < MAX_TOUCH_COUNT; ++i) {
221
+ if (_touches[i]) {
222
+ ids.push_back(i);
223
+ }
224
+ _touches[i] = nil;
225
+ }
226
+ const uint64_t windowId = _windowId;
227
+ if (ids.empty() || !windowId) {
218
228
  return;
219
229
  }
230
+ GodotModule::get_singleton()->runWithInstance(_engineGeneration, [windowId, ids]() {
231
+ godot::Window *window = godot::Object::cast_to<godot::Window>(godot::UtilityFunctions::instance_from_id(windowId));
232
+ auto *dse = godot::DisplayServerEmbedded::get_singleton();
233
+ if (window && dse) {
234
+ for (int id : ids) {
235
+ dse->touches_canceled(id, window->get_window_id());
236
+ }
237
+ }
238
+ });
239
+ }
220
240
 
221
- godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
241
+ - (bool)ownsRenderingLayer {
242
+ return _isMounted && _windowId && _renderingLayer &&
243
+ _engineGeneration != 0 &&
244
+ _engineGeneration == GodotModule::get_singleton()->get_instance_generation() &&
245
+ _renderingLayer.superlayer == self.layer &&
246
+ (![@"" isEqualToString:_windowName] || _currentView == self);
247
+ }
222
248
 
249
+ - (void)addToGodotView {
250
+ if (!_isMounted) {
251
+ return;
252
+ }
253
+ auto *module = GodotModule::get_singleton();
223
254
  if (!_instanceCallbackRegistered) {
224
- GodotModule::get_singleton()->unregisterWindowUpdateCallback((__bridge void *)self);
225
- std::string newWinName = [_windowName UTF8String];
226
255
  __weak RTNGodotView *weakSelf = self;
227
- GodotModule::get_singleton()->registerWindowUpdateCallback(newWinName, (__bridge void *)self, [weakSelf](bool adding) {
228
- RTNGodotView *strongSelf = weakSelf;
229
- if (!strongSelf) {
230
- return;
231
- }
232
- if (adding) {
233
- dispatch_async(dispatch_get_main_queue(), ^{
234
- RTNGodotView *view = weakSelf;
235
- if (!view) {
236
- return;
237
- }
238
- NSLog(@"RTNGodotView: Adding Godot View from Window Update Callback: %@", view);
239
- [view addToGodotView];
240
- });
241
- } else {
242
- dispatch_async(dispatch_get_main_queue(), ^{
243
- RTNGodotView *view = weakSelf;
244
- if (!view) {
245
- return;
246
- }
247
- NSLog(@"RTNGodotView: Removing Godot View from Window Update Callback: %@", view);
256
+ module->registerWindowUpdateCallback([_windowName UTF8String], (__bridge void *)self, [weakSelf](bool adding) {
257
+ const uint64_t generation = GodotModule::get_singleton()->get_instance_generation();
258
+ dispatch_async(dispatch_get_main_queue(), ^{
259
+ RTNGodotView *view = weakSelf;
260
+ if (!view){ return;
261
+ }
262
+ if (adding) {
263
+ if (generation == GodotModule::get_singleton()->get_instance_generation()){ [view addToGodotView];
264
+ }
265
+ } else if (view->_engineGeneration == generation) {
248
266
  [view removeFromGodotView:false unregister:false];
249
- });
250
- } }, nullptr);
267
+ }
268
+ }); }, nullptr);
251
269
  _instanceCallbackRegistered = true;
252
270
  }
253
-
254
- if (!instance || !instance->is_started()) {
255
- // Cannot continue without a Godot instance
256
- NSLog(@"RTNGodotView: Godot instance not started yet.");
257
- return;
271
+ const uint64_t engineGeneration = module->get_instance_generation();
272
+ if (_engineGeneration && _engineGeneration != engineGeneration) {
273
+ [self removeFromGodotView:false unregister:false];
258
274
  }
259
-
260
- if (_windowId > 0 && godot::UtilityFunctions::is_instance_id_valid(_windowId)) {
261
- NSLog(@"RTNGodotView: Window already configured");
275
+ if (!engineGeneration || (_windowId && _engineGeneration == engineGeneration)) {
262
276
  return;
263
277
  }
264
-
265
278
  const uint64_t attachmentGeneration = ++_attachmentGeneration;
266
- if ([@"" isEqualToString:_windowName]) {
267
- // Set up the main window
268
-
269
- GodotModule::get_singleton()->runOnGodotThread([=]() {
270
- godot::MainLoop *mainLoop = godot::Engine::get_singleton()->get_main_loop();
271
- godot::SceneTree *sceneTree = godot::Object::cast_to<godot::SceneTree>(mainLoop);
272
- if (!sceneTree) {
273
- NSLog(@"RTNGodotView: Unable to get SceneTree from Godot!");
274
- return;
275
- }
276
- godot::Window *newWindow = sceneTree->get_root();
277
- const uint64_t windowId = newWindow->get_instance_id();
278
- dispatch_async(dispatch_get_main_queue(), ^{
279
- if (!self->_isMounted || self->_attachmentGeneration != attachmentGeneration) {
280
- return;
281
- }
282
- CALayer *renderingLayer = [RTNGodotView addMainLayerToGodotView:self];
283
- if (!renderingLayer) {
279
+ const std::string windowName = [_windowName UTF8String];
280
+ const CGSize initialSize = self.bounds.size;
281
+ module->runWithInstance(engineGeneration, [=]() {
282
+ if (self->_attachmentGeneration.load() != attachmentGeneration) {
283
+ return;
284
+ }
285
+ auto *tree = godot::Object::cast_to<godot::SceneTree>(godot::Engine::get_singleton()->get_main_loop());
286
+ if (!tree) {
287
+ return;
288
+ }
289
+ auto *window = windowName.empty() ? tree->get_root() : godot::Object::cast_to<godot::Window>(tree->get_root()->find_child(godot::String::utf8(windowName.c_str()), true, false));
290
+ if (!window) {
291
+ return;
292
+ }
293
+ const uint64_t windowId = window->get_instance_id();
294
+ const double scale = module->get_content_scale_factor();
295
+ CALayer *layer = nil;
296
+ if (windowName.empty()) {
297
+ layer = (__bridge CALayer *)module->get_main_rendering_layer();
298
+ } else {
299
+ godot::Ref<godot::RenderingNativeSurfaceApple> surface = window->get_native_surface();
300
+ if (surface.is_null()) {
301
+ surface = godot::RenderingNativeSurfaceApple::create(0);
302
+ if (surface.is_null()) {
284
303
  return;
285
304
  }
286
- self->_windowId = windowId;
287
- self->_renderingLayer = renderingLayer;
288
- self->_lastRequestedPixelSize = godot::Vector2i();
289
- [self setNeedsLayout];
290
- });
291
- });
292
-
293
- } else {
294
- // Subwindow case
295
- GodotModule::get_singleton()->runOnGodotThread([=]() {
296
- godot::MainLoop *mainLoop = godot::Engine::get_singleton()->get_main_loop();
297
- godot::SceneTree *sceneTree = godot::Object::cast_to<godot::SceneTree>(mainLoop);
298
- if (!sceneTree) {
299
- NSLog(@"RTNGodotView: Unable to get SceneTree from Godot!");
300
- return;
301
- }
302
-
303
- std::string newWinName = [_windowName UTF8String];
304
- godot::Node *node = sceneTree->get_root()->find_child(godot::String::utf8(newWinName.c_str()), true, false);
305
- godot::Window *newWindow = godot::Object::cast_to<godot::Window>(node);
306
-
307
- if (!newWindow) {
308
- NSLog(@"RTNGodotView: Godot Window not valid: 0x%p", newWindow);
309
- return;
305
+ layer = (__bridge CALayer *)(void *)surface->get_layer();
306
+ layer.bounds = CGRectMake(0, 0, godot::MAX(1, initialSize.width), godot::MAX(1, initialSize.height));
307
+ layer.anchorPoint = CGPointZero;
308
+ layer.position = CGPointZero;
309
+ layer.contentsScale = scale;
310
+ window->set_native_surface(surface);
311
+ } else {
312
+ layer = (__bridge CALayer *)(void *)surface->get_layer();
310
313
  }
311
- const uint64_t windowId = newWindow->get_instance_id();
312
-
313
- CGRect screen = [[UIScreen mainScreen] bounds];
314
- CALayer *newRenderingLayer = nil;
315
-
316
- godot::Ref<godot::RenderingNativeSurfaceApple> appleSurface = godot::RenderingNativeSurfaceApple::create(0);
317
- newRenderingLayer = (__bridge CALayer *)(void *)appleSurface->get_layer();
318
- newRenderingLayer.bounds = CGRectMake(0, 0, screen.size.width, screen.size.height);
319
- newRenderingLayer.position = CGPointMake(0, 0);
320
- newRenderingLayer.anchorPoint = CGPointMake(0, 0);
321
- newRenderingLayer.contentsScale = GodotModule::get_singleton()->get_content_scale_factor();
322
-
323
- godot::RenderingNativeSurface *ptr = godot::Object::cast_to<godot::RenderingNativeSurface>(appleSurface.ptr());
324
- godot::Ref<godot::RenderingNativeSurface> nativeSurface(ptr);
325
-
326
- newWindow->set_visible(true);
327
- newWindow->set_native_surface(nativeSurface);
328
- godot::Callable exited_cb = GodotModule::get_singleton()->create_callable([=](const godot::Variant **p_arguments, int p_argcount, godot::Variant &r_return_value, GDExtensionCallError &r_call_error) {
329
- // Window is now removed, needs relayout
314
+ window->set_visible(true);
315
+ __weak RTNGodotView *weakSelf = self;
316
+ godot::Callable exited = module->create_callable([=](const godot::Variant **, int, godot::Variant &, GDExtensionCallError &) {
330
317
  dispatch_async(dispatch_get_main_queue(), ^{
331
- if (self->_renderingLayer == newRenderingLayer) {
332
- [newRenderingLayer removeFromSuperlayer];
333
- self->_renderingLayer = nil;
318
+ RTNGodotView *view = weakSelf;
319
+ if (view && view->_engineGeneration == engineGeneration &&
320
+ view->_attachmentGeneration.load() == attachmentGeneration && view->_windowId == windowId) {
321
+ [view removeFromGodotView:false unregister:false];
334
322
  }
335
- self->_windowId = 0;
336
- [self setNeedsLayout];
337
323
  });
338
324
  });
339
- newWindow->connect("tree_exited", exited_cb);
340
-
341
- dispatch_async(dispatch_get_main_queue(), ^{
342
- if (!self->_isMounted || self->_attachmentGeneration != attachmentGeneration) {
343
- return;
344
- }
345
- self->_windowId = windowId;
346
- self->_renderingLayer = newRenderingLayer;
347
- self->_lastRequestedPixelSize = godot::Vector2i();
348
- [CATransaction begin];
349
- [CATransaction setDisableActions:YES];
350
- self->_renderingLayer.hidden = YES;
351
- [self.layer addSublayer:self->_renderingLayer];
352
- [CATransaction commit];
353
- [self setNeedsLayout];
354
- });
325
+ window->connect("tree_exited", exited);
326
+ }
327
+ if (!layer) {
328
+ return;
329
+ }
330
+ // Only retain a CALayer across the UI handoff, never a Godot binding/Ref.
331
+ dispatch_async(dispatch_get_main_queue(), ^{
332
+ if (!self->_isMounted || self->_attachmentGeneration.load() != attachmentGeneration ||
333
+ engineGeneration != module->get_instance_generation()) {
334
+ return;
335
+ }
336
+ self->_windowId = windowId;
337
+ self->_engineGeneration = engineGeneration;
338
+ self->_contentScaleFactor = scale;
339
+ self->_renderingLayer = layer;
340
+ self->_lastRequestedPixelSize = godot::Vector2i();
341
+ if (windowName.empty()) {
342
+ [RTNGodotView addMainLayer:layer toGodotView:self];
343
+ } else {
344
+ [self.layer addSublayer:layer];
345
+ layer.hidden = YES;
346
+ }
347
+ [self setNeedsLayout];
355
348
  });
356
- }
349
+ });
357
350
  }
358
351
 
359
352
  - (void)removeFromGodotView:(bool)addAfter unregister:(bool)unregister {
360
- NSLog(@"RTNGodotView: Removing Godot View: %@, windowName: %@, addAfter: %d", self, _windowName, addAfter);
361
- ++_attachmentGeneration;
353
+ // All callers are on the main queue; teardown callbacks carry their old engine generation.
354
+ [self invalidateAttachment];
362
355
  if (unregister) {
363
356
  GodotModule::get_singleton()->unregisterWindowUpdateCallback((__bridge void *)self);
364
- self->_instanceCallbackRegistered = false;
365
- }
366
- void (^removeBlock)() = ^() {
367
- if (![@"" isEqualToString:self->_windowName]) {
368
- // Only remove if it is not the main window
369
- if (self->_renderingLayer) {
370
- if (self->_renderingLayer.superlayer == self.layer) {
371
- // Only remove, if it is our sublayer
372
- [self->_renderingLayer removeFromSuperlayer];
373
- }
374
- }
375
- } else {
376
- // If it is the main window, then request removal from the manager
377
- [RTNGodotView removeMainLayerFromGodotView:self];
378
- }
379
- self->_windowId = 0;
380
- self->_renderingLayer = nullptr;
381
- self->_lastRequestedPixelSize = godot::Vector2i();
382
- ++self->_resizeGeneration;
383
- if (addAfter) {
384
- [self addToGodotView];
385
- }
386
- };
387
- if ([NSThread isMainThread]) {
388
- removeBlock();
389
- } else {
390
- if (!unregister) {
391
- // This should only happen when the Godot instance is being stopped
392
- dispatch_sync(dispatch_get_main_queue(), ^{
393
- removeBlock();
394
- });
395
- } else {
396
- dispatch_async(dispatch_get_main_queue(), ^{
397
- removeBlock();
398
- });
399
- }
357
+ _instanceCallbackRegistered = false;
358
+ }
359
+ if ([@"" isEqualToString:_windowName]) {
360
+ [RTNGodotView removeMainLayerFromGodotView:self promote:unregister];
361
+ } else if (_renderingLayer.superlayer == self.layer) {
362
+ [_renderingLayer removeFromSuperlayer];
363
+ }
364
+ _windowId = 0;
365
+ _engineGeneration = 0;
366
+ _renderingLayer = nil;
367
+ _lastRequestedPixelSize = godot::Vector2i();
368
+ if (addAfter) {
369
+ [self addToGodotView];
400
370
  }
401
371
  }
402
372
 
403
373
  - (void)layoutSubviews {
404
374
  [super layoutSubviews];
405
375
 
406
- godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
407
-
408
- if (!instance || !instance->is_started()) {
409
- // Cannot continue without a Godot instance
410
- NSLog(@"RTNGodotView: layoutSubviews: No Godot Instance Running");
411
- return;
412
- }
413
-
414
- if (!_windowId || !_renderingLayer) {
415
- NSLog(@"RTNGodotView: layoutSubviews: No window configured");
376
+ if (![self ownsRenderingLayer]) {
416
377
  return;
417
378
  }
418
379
 
@@ -421,7 +382,7 @@ static __weak UIView *_currentView = nil;
421
382
  return;
422
383
  }
423
384
 
424
- double contentScaleFactor = GodotModule::get_singleton()->get_content_scale_factor();
385
+ double contentScaleFactor = _contentScaleFactor;
425
386
  CGRect renderingFrame = CGRectMake(0, 0, renderingSize.width, renderingSize.height);
426
387
  uint64_t windowId = _windowId;
427
388
  godot::Vector2i pixelSize(
@@ -438,7 +399,7 @@ static __weak UIView *_currentView = nil;
438
399
  }
439
400
 
440
401
  _lastRequestedPixelSize = pixelSize;
441
- const uint64_t attachmentGeneration = _attachmentGeneration;
402
+ const uint64_t attachmentGeneration = _attachmentGeneration.load();
442
403
  const uint64_t resizeGeneration = ++_resizeGeneration;
443
404
  CALayer *renderingLayer = _renderingLayer;
444
405
 
@@ -447,21 +408,24 @@ static __weak UIView *_currentView = nil;
447
408
  renderingLayer.hidden = YES;
448
409
  [CATransaction commit];
449
410
 
450
- GodotModule::get_singleton()->runOnGodotThread([=]() {
411
+ const uint64_t engineGeneration = _engineGeneration;
412
+ GodotModule::get_singleton()->runWithInstance(engineGeneration, [=]() {
413
+ if (self->_attachmentGeneration.load() != attachmentGeneration || self->_resizeGeneration.load() != resizeGeneration) {
414
+ return;
415
+ }
451
416
  godot::DisplayServerEmbedded *dse = godot::DisplayServerEmbedded::get_singleton();
452
417
  if (dse && godot::UtilityFunctions::is_instance_id_valid(windowId)) {
453
418
  godot::Object *obj = godot::UtilityFunctions::instance_from_id(windowId);
454
419
  godot::Window *window = godot::Object::cast_to<godot::Window>(obj);
455
420
  if (window) {
456
421
  dse->resize_window(pixelSize, window->get_window_id());
457
- // Render one frame at the new size before exposing the layer. Without
458
- // this barrier iOS briefly composites the previous framebuffer into
459
- // the new host bounds, which appears as a frozen bottom-left image.
422
+ // Submit a frame at the new size before exposing the layer. This
423
+ // orders CPU work; it is not a GPU presentation-completion signal.
460
424
  GodotModule::get_singleton()->iterate();
461
425
  dispatch_async(dispatch_get_main_queue(), ^{
462
- if (!self->_isMounted ||
463
- self->_attachmentGeneration != attachmentGeneration ||
464
- self->_resizeGeneration != resizeGeneration ||
426
+ if (![self ownsRenderingLayer] ||
427
+ self->_attachmentGeneration.load() != attachmentGeneration ||
428
+ self->_resizeGeneration.load() != resizeGeneration ||
465
429
  self->_windowId != windowId ||
466
430
  self->_renderingLayer != renderingLayer) {
467
431
  return;
@@ -507,21 +471,13 @@ static __weak UIView *_currentView = nil;
507
471
 
508
472
  - (void)touchesBegan:(NSSet<UITouch *> *)touches
509
473
  withEvent:(UIEvent *)event {
510
- godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
511
-
512
- if (!instance || !instance->is_started()) {
513
- NSLog(@"RTNGodotView: touchesBegan: No Godot instance started");
514
- return;
515
- }
516
- if (!_windowId) {
517
- NSLog(@"RTNGodotView: touchesBegan: No window configured");
518
- return;
519
- }
520
- godot::DisplayServerEmbedded *dse = godot::DisplayServerEmbedded::get_singleton();
521
- if (!dse) {
522
- NSLog(@"RTNGodotView: touchesBegan: No Godot DisplayServer instance");
474
+ if (![self ownsRenderingLayer]) {
523
475
  return;
524
476
  }
477
+ const uint64_t windowId = _windowId;
478
+ const uint64_t engineGeneration = _engineGeneration;
479
+ const uint64_t attachmentGeneration = _attachmentGeneration.load();
480
+
525
481
  for (UITouch *touch in touches) {
526
482
  int touchId = [self getTouchId:touch];
527
483
  if (touchId == -1) {
@@ -534,11 +490,18 @@ static __weak UIView *_currentView = nil;
534
490
  location.x -= _renderingLayer.frame.origin.x;
535
491
  location.y -= _renderingLayer.frame.origin.y;
536
492
  NSUInteger tapCount = touch.tapCount;
537
- double contentScaleFactor = GodotModule::get_singleton()->get_content_scale_factor();
538
- GodotModule::get_singleton()->runOnGodotThread([=]() {
539
- if (godot::UtilityFunctions::is_instance_id_valid(_windowId)) {
540
- godot::Object *obj = godot::UtilityFunctions::instance_from_id(_windowId);
493
+ double contentScaleFactor = _contentScaleFactor;
494
+ GodotModule::get_singleton()->runWithInstance(engineGeneration, [=]() {
495
+ if (self->_attachmentGeneration.load() != attachmentGeneration) {
496
+ return;
497
+ }
498
+ auto *dse = godot::DisplayServerEmbedded::get_singleton();
499
+ if (dse && godot::UtilityFunctions::is_instance_id_valid(windowId)) {
500
+ godot::Object *obj = godot::UtilityFunctions::instance_from_id(windowId);
541
501
  godot::Window *window = godot::Object::cast_to<godot::Window>(obj);
502
+ if (!window) {
503
+ return;
504
+ }
542
505
  dse->touch_press(touchId, location.x * contentScaleFactor, location.y * contentScaleFactor, true, tapCount > 1, window->get_window_id());
543
506
  }
544
507
  });
@@ -547,21 +510,13 @@ static __weak UIView *_currentView = nil;
547
510
 
548
511
  - (void)touchesMoved:(NSSet<UITouch *> *)touches
549
512
  withEvent:(UIEvent *)event {
550
- godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
551
-
552
- if (!instance || !instance->is_started()) {
553
- NSLog(@"RTNGodotView: touchesMoved: No Godot instance started");
554
- return;
555
- }
556
- if (!_windowId) {
557
- NSLog(@"RTNGodotView: touchesMoved: No window configured");
558
- return;
559
- }
560
- godot::DisplayServerEmbedded *dse = godot::DisplayServerEmbedded::get_singleton();
561
- if (!dse) {
562
- NSLog(@"RTNGodotView: touchesMoved: No Godot DisplayServer instance");
513
+ if (![self ownsRenderingLayer]) {
563
514
  return;
564
515
  }
516
+ const uint64_t windowId = _windowId;
517
+ const uint64_t engineGeneration = _engineGeneration;
518
+ const uint64_t attachmentGeneration = _attachmentGeneration.load();
519
+
565
520
  for (UITouch *touch in touches) {
566
521
  int touchId = [self getTouchId:touch];
567
522
  if (touchId == -1) {
@@ -583,11 +538,18 @@ static __weak UIView *_currentView = nil;
583
538
  CGVector azim = [touch azimuthUnitVectorInView:self];
584
539
  CGFloat force = touch.force;
585
540
  CGFloat maximumPossibleForce = touch.maximumPossibleForce;
586
- double contentScaleFactor = GodotModule::get_singleton()->get_content_scale_factor();
587
- GodotModule::get_singleton()->runOnGodotThread([=]() {
588
- if (godot::UtilityFunctions::is_instance_id_valid(_windowId)) {
589
- godot::Object *obj = godot::UtilityFunctions::instance_from_id(_windowId);
541
+ double contentScaleFactor = _contentScaleFactor;
542
+ GodotModule::get_singleton()->runWithInstance(engineGeneration, [=]() {
543
+ if (self->_attachmentGeneration.load() != attachmentGeneration) {
544
+ return;
545
+ }
546
+ auto *dse = godot::DisplayServerEmbedded::get_singleton();
547
+ if (dse && godot::UtilityFunctions::is_instance_id_valid(windowId)) {
548
+ godot::Object *obj = godot::UtilityFunctions::instance_from_id(windowId);
590
549
  godot::Window *window = godot::Object::cast_to<godot::Window>(obj);
550
+ if (!window) {
551
+ return;
552
+ }
591
553
  dse->touch_drag(
592
554
  touchId,
593
555
  prevLocation.x * contentScaleFactor,
@@ -604,21 +566,13 @@ static __weak UIView *_currentView = nil;
604
566
 
605
567
  - (void)touchesEnded:(NSSet<UITouch *> *)touches
606
568
  withEvent:(UIEvent *)event {
607
- godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
608
-
609
- if (!instance || !instance->is_started()) {
610
- NSLog(@"RTNGodotView: touchesEnded: No Godot instance started");
611
- return;
612
- }
613
- if (!_windowId) {
614
- NSLog(@"RTNGodotView: touchesEnded: No window configured");
615
- return;
616
- }
617
- godot::DisplayServerEmbedded *dse = godot::DisplayServerEmbedded::get_singleton();
618
- if (!dse) {
619
- NSLog(@"RTNGodotView: touchesEnded: No Godot DisplayServer instance");
569
+ if (![self ownsRenderingLayer]) {
620
570
  return;
621
571
  }
572
+ const uint64_t windowId = _windowId;
573
+ const uint64_t engineGeneration = _engineGeneration;
574
+ const uint64_t attachmentGeneration = _attachmentGeneration.load();
575
+
622
576
  for (UITouch *touch in touches) {
623
577
  int touchId = [self getTouchId:touch];
624
578
  if (touchId == -1) {
@@ -631,11 +585,18 @@ static __weak UIView *_currentView = nil;
631
585
  }
632
586
  location.x -= _renderingLayer.frame.origin.x;
633
587
  location.y -= _renderingLayer.frame.origin.y;
634
- double contentScaleFactor = GodotModule::get_singleton()->get_content_scale_factor();
635
- GodotModule::get_singleton()->runOnGodotThread([=]() {
636
- if (godot::UtilityFunctions::is_instance_id_valid(_windowId)) {
637
- godot::Object *obj = godot::UtilityFunctions::instance_from_id(_windowId);
588
+ double contentScaleFactor = _contentScaleFactor;
589
+ GodotModule::get_singleton()->runWithInstance(engineGeneration, [=]() {
590
+ if (self->_attachmentGeneration.load() != attachmentGeneration) {
591
+ return;
592
+ }
593
+ auto *dse = godot::DisplayServerEmbedded::get_singleton();
594
+ if (dse && godot::UtilityFunctions::is_instance_id_valid(windowId)) {
595
+ godot::Object *obj = godot::UtilityFunctions::instance_from_id(windowId);
638
596
  godot::Window *window = godot::Object::cast_to<godot::Window>(obj);
597
+ if (!window) {
598
+ return;
599
+ }
639
600
  dse->touch_press(touchId, location.x * contentScaleFactor, location.y * contentScaleFactor, false, false, window->get_window_id());
640
601
  }
641
602
  });
@@ -644,31 +605,30 @@ static __weak UIView *_currentView = nil;
644
605
 
645
606
  - (void)touchesCancelled:(NSSet<UITouch *> *)touches
646
607
  withEvent:(UIEvent *)event {
647
- godot::GodotInstance *instance = GodotModule::get_singleton()->get_instance();
648
-
649
- if (!instance || !instance->is_started()) {
650
- NSLog(@"RTNGodotView: touchesCancelled: No Godot instance started");
651
- return;
652
- }
653
- if (!_windowId) {
654
- NSLog(@"RTNGodotView: touchesCancelled: No window configured");
655
- return;
656
- }
657
- godot::DisplayServerEmbedded *dse = godot::DisplayServerEmbedded::get_singleton();
658
- if (!dse) {
659
- NSLog(@"RTNGodotView: touchesCancelled: No Godot DisplayServer instance");
608
+ if (![self ownsRenderingLayer]) {
660
609
  return;
661
610
  }
611
+ const uint64_t windowId = _windowId;
612
+ const uint64_t engineGeneration = _engineGeneration;
613
+ const uint64_t attachmentGeneration = _attachmentGeneration.load();
614
+
662
615
  for (UITouch *touch in touches) {
663
616
  int touchId = [self getTouchId:touch];
664
617
  if (touchId == -1) {
665
618
  continue;
666
619
  }
667
620
  [self removeTouchId:touchId];
668
- GodotModule::get_singleton()->runOnGodotThread([=]() {
669
- if (godot::UtilityFunctions::is_instance_id_valid(_windowId)) {
670
- godot::Object *obj = godot::UtilityFunctions::instance_from_id(_windowId);
621
+ GodotModule::get_singleton()->runWithInstance(engineGeneration, [=]() {
622
+ if (self->_attachmentGeneration.load() != attachmentGeneration) {
623
+ return;
624
+ }
625
+ auto *dse = godot::DisplayServerEmbedded::get_singleton();
626
+ if (dse && godot::UtilityFunctions::is_instance_id_valid(windowId)) {
627
+ godot::Object *obj = godot::UtilityFunctions::instance_from_id(windowId);
671
628
  godot::Window *window = godot::Object::cast_to<godot::Window>(obj);
629
+ if (!window) {
630
+ return;
631
+ }
672
632
  dse->touches_canceled(touchId, window->get_window_id());
673
633
  }
674
634
  });