@blotch/angular-lynx-native 22.2.24 → 22.2.26

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.
Files changed (53) hide show
  1. package/README.md +6 -4
  2. package/dist/android/project.d.ts +27 -3
  3. package/dist/android/project.d.ts.map +1 -1
  4. package/dist/android/project.js +83 -2
  5. package/dist/generate.d.ts +1 -1
  6. package/dist/generate.js +8 -2
  7. package/dist/index.d.ts +3 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +9 -0
  10. package/dist/ios/project.d.ts +20 -3
  11. package/dist/ios/project.d.ts.map +1 -1
  12. package/dist/ios/project.js +99 -2
  13. package/dist/options.d.ts +119 -0
  14. package/dist/options.d.ts.map +1 -1
  15. package/dist/options.js +143 -0
  16. package/dist/png.d.ts +5 -0
  17. package/dist/png.d.ts.map +1 -1
  18. package/dist/png.js +19 -5
  19. package/dist/splash.d.ts +70 -0
  20. package/dist/splash.d.ts.map +1 -0
  21. package/dist/splash.js +239 -0
  22. package/package.json +1 -1
  23. package/templates/android/README.md +19 -1
  24. package/templates/android/app/build.gradle.kts +4 -0
  25. package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServer.kt +6 -0
  26. package/templates/android/app/src/main/AndroidManifest.xml +27 -1
  27. package/templates/android/app/src/main/java/dev/angularlynx/shell/AssetsModule.kt +2 -1
  28. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +16 -3
  29. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +81 -4
  30. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +10 -0
  31. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShortcutActivity.kt +33 -0
  32. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShortcutsModule.kt +72 -0
  33. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +15 -4
  34. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +10 -1
  35. package/templates/android/app/src/main/res/values/colors.xml +3 -2
  36. package/templates/android/app/src/main/res/values/splash.xml +9 -0
  37. package/templates/android/app/src/main/res/values/themes.xml +14 -0
  38. package/templates/android/app/src/main/res/xml/shortcuts.xml +7 -0
  39. package/templates/android/app/src/release/java/dev/angularlynx/shell/DevServer.kt +2 -0
  40. package/templates/ios/App/AngularLynxWebGL.h +34 -6
  41. package/templates/ios/App/AngularLynxWebGL.m +58 -23
  42. package/templates/ios/App/AngularLynxWebGLContext.m +25 -21
  43. package/templates/ios/App/AnimationFrameRate.swift +214 -0
  44. package/templates/ios/App/AppDelegate.swift +2 -0
  45. package/templates/ios/App/AssetsModule.swift +3 -1
  46. package/templates/ios/App/DevServer.swift +3 -0
  47. package/templates/ios/App/Info.plist +12 -5
  48. package/templates/ios/App/LynxViewController.swift +24 -1
  49. package/templates/ios/App/ShellModule.swift +14 -1
  50. package/templates/ios/App/ShortcutsModule.swift +82 -0
  51. package/templates/ios/App/SplashScreen.swift +71 -0
  52. package/templates/ios/App/WebViewLoader.swift +95 -0
  53. package/templates/ios/App/XElementRegistry.swift +5 -1
@@ -0,0 +1,72 @@
1
+ package dev.angularlynx.shell
2
+
3
+ import android.content.Context
4
+ import android.content.Intent
5
+ import com.lynx.jsbridge.LynxMethod
6
+ import com.lynx.jsbridge.LynxModule
7
+ import com.lynx.react.bridge.Callback
8
+ import com.lynx.react.bridge.JavaOnlyArray
9
+ import com.lynx.react.bridge.JavaOnlyMap
10
+
11
+ /**
12
+ * The native module behind the runtime's `LynxShortcuts`, which calls it as
13
+ * `NativeModules.AngularLynxShortcuts` (from the background thread, where
14
+ * NativeModules lives). iOS's shell has one of the same name.
15
+ *
16
+ * angular.json's `shortcuts` become the app's static shortcuts
17
+ * (`res/xml/shortcuts.xml`, which the generator writes), the ones in the menu
18
+ * a long press on the app's icon opens. One may start the app, so the
19
+ * activity can't hand it straight to the Angular app: it leaves the shortcut
20
+ * here, and the runtime takes it when it's ready, at startup and again each
21
+ * time it hears the `angularLynxShortcut` global event. A shortcut waits for
22
+ * as long as that takes, so the one that started the app is never lost, and
23
+ * none is handled twice.
24
+ *
25
+ * `take` calls back with `{ shortcuts }`: each shortcut the person ran since
26
+ * the last call, as `{ id, route? }`.
27
+ */
28
+ class ShortcutsModule(context: Context, param: Any?) : LynxModule(context, param) {
29
+ @LynxMethod
30
+ fun take(callback: Callback) {
31
+ val shortcuts = JavaOnlyArray()
32
+ for (shortcut in takeWaiting()) {
33
+ shortcuts.pushMap(JavaOnlyMap.from(shortcut))
34
+ }
35
+ callback.invoke(JavaOnlyMap.from(mapOf("shortcuts" to shortcuts)))
36
+ }
37
+
38
+ companion object {
39
+ const val NAME = "AngularLynxShortcuts"
40
+
41
+ // The global event that says a shortcut is waiting. It carries
42
+ // nothing: the listener answers by taking them.
43
+ const val EVENT = "angularLynxShortcut"
44
+
45
+ // What a shortcut's intent carries (res/xml/shortcuts.xml).
46
+ const val EXTRA_ID = "angularLynxShortcutId"
47
+ const val EXTRA_ROUTE = "angularLynxShortcutRoute"
48
+
49
+ // The activity leaves them on the main thread, and Lynx calls
50
+ // modules off it.
51
+ private val waiting = mutableListOf<Map<String, String>>()
52
+
53
+ /**
54
+ * Leaves the shortcut `intent` carries for the runtime to take.
55
+ * Returns whether it carried one.
56
+ */
57
+ fun run(intent: Intent): Boolean {
58
+ val id = intent.getStringExtra(EXTRA_ID) ?: return false
59
+ val route = intent.getStringExtra(EXTRA_ROUTE)
60
+ val shortcut = if (route == null) mapOf("id" to id) else mapOf("id" to id, "route" to route)
61
+ synchronized(waiting) { waiting.add(shortcut) }
62
+ return true
63
+ }
64
+
65
+ private fun takeWaiting(): List<Map<String, String>> =
66
+ synchronized(waiting) {
67
+ val taken = waiting.toList()
68
+ waiting.clear()
69
+ taken
70
+ }
71
+ }
72
+ }
@@ -33,8 +33,13 @@ private const val EGL_OPENGL_ES3_BIT = 0x40
33
33
  * default framebuffer the size of the canvas's `width` and `height`, which
34
34
  * each finished frame is copied from into the canvas's TextureView.
35
35
  *
36
- * The runtime's `run` calls arrive on the UI thread, which makes the context
37
- * current for each and puts back whatever was current before.
36
+ * The runtime's `run` calls arrive on Lynx's engine thread, which the shell
37
+ * runs off the UI thread (MainActivity's thread strategy), while the canvas's
38
+ * TextureView reports its surface on the UI thread. Each call makes the
39
+ * context current for itself and puts back whatever was current before, so
40
+ * either thread can make the next one; the context's lock keeps them from
41
+ * making two at once. The UI thread waits for a batch only when the surface
42
+ * comes, goes or resizes.
38
43
  */
39
44
  class WebGLContext(val canvasId: String, attributes: ReadableMap?) {
40
45
  private val display: EGLDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY)
@@ -125,6 +130,7 @@ class WebGLContext(val canvasId: String, attributes: ReadableMap?) {
125
130
  }
126
131
  }
127
132
 
133
+ @Synchronized
128
134
  fun destroy() {
129
135
  if (destroyed) return
130
136
  withCurrent {
@@ -167,6 +173,7 @@ class WebGLContext(val canvasId: String, attributes: ReadableMap?) {
167
173
 
168
174
  // --- The view ------------------------------------------------------------
169
175
 
176
+ @Synchronized
170
177
  fun attachView(canvas: CanvasUI?) {
171
178
  if (view === canvas) return
172
179
  view = canvas
@@ -175,6 +182,7 @@ class WebGLContext(val canvasId: String, attributes: ReadableMap?) {
175
182
  }
176
183
 
177
184
  /** The view's surface came, went or changed size. */
185
+ @Synchronized
178
186
  fun surfaceChanged() {
179
187
  if (destroyed) return
180
188
  val texture = view?.surface
@@ -187,8 +195,9 @@ class WebGLContext(val canvasId: String, attributes: ReadableMap?) {
187
195
  } else {
188
196
  windowTexture = texture
189
197
  withCurrent {
190
- // Presenting never waits for the display: it runs on
191
- // the UI thread.
198
+ // Presenting never waits for the display: the frame
199
+ // is copied once into the TextureView, which the UI
200
+ // thread composites at its own pace.
192
201
  EGL14.eglSwapInterval(display, 0)
193
202
  }
194
203
  }
@@ -332,6 +341,7 @@ class WebGLContext(val canvasId: String, attributes: ReadableMap?) {
332
341
  * app has drawn since the last frame was shown, or, once a frame has been
333
342
  * shown without preserveDrawingBuffer, the cleared buffer.
334
343
  */
344
+ @Synchronized
335
345
  fun readDrawingBuffer(): DrawingBufferPixels? {
336
346
  if (destroyed) return null
337
347
  val pixels = ByteBuffer.allocateDirect(width * height * 4).order(ByteOrder.nativeOrder())
@@ -640,6 +650,7 @@ class WebGLContext(val canvasId: String, attributes: ReadableMap?) {
640
650
  fun skip() { index++ }
641
651
  }
642
652
 
653
+ @Synchronized
643
654
  fun run(commands: ReadableArray): JavaOnlyArray {
644
655
  val results = JavaOnlyArray()
645
656
  if (destroyed) return results
@@ -28,7 +28,8 @@ import kotlin.math.roundToInt
28
28
  * only come from a main-thread module: MainActivity turns those on
29
29
  * (`setEnableMTSModule`). The runtime batches the calls into one `run` per
30
30
  * frame; see packages/runtime/src/lib/canvas/webgl-bridge.ts for the
31
- * protocol. Lynx calls these methods on the UI thread, where GL runs.
31
+ * protocol. Lynx calls these methods on its engine thread, where the main
32
+ * thread's JavaScript runs, which the shell runs off the UI thread.
32
33
  */
33
34
  class WebGLModule(context: Context, param: Any?) : LynxModule(context, param) {
34
35
  private val contexts = HashMap<Int, WebGLContext>()
@@ -232,18 +233,25 @@ internal object DecodedImages {
232
233
  /**
233
234
  * Pairs canvases with contexts by canvas id: either can come first, as the
234
235
  * runtime may make the context before Lynx has made the element's view.
236
+ *
237
+ * Views come and go on the UI thread, contexts on Lynx's engine thread, so
238
+ * it's locked. It takes a context's lock inside its own, and a context never
239
+ * calls it, so the two can't wait on each other.
235
240
  */
236
241
  internal object CanvasRegistry {
237
242
  private val views = HashMap<String, WeakReference<CanvasUI>>()
238
243
  private val contexts = HashMap<String, WeakReference<WebGLContext>>()
239
244
 
245
+ @Synchronized
240
246
  fun setView(canvasId: String, view: CanvasUI?) {
241
247
  if (view == null) views.remove(canvasId) else views[canvasId] = WeakReference(view)
242
248
  contexts[canvasId]?.get()?.attachView(view)
243
249
  }
244
250
 
251
+ @Synchronized
245
252
  fun view(canvasId: String): CanvasUI? = views[canvasId]?.get()
246
253
 
254
+ @Synchronized
247
255
  fun setContext(canvasId: String, context: WebGLContext?) {
248
256
  if (context == null) {
249
257
  contexts.remove(canvasId)
@@ -253,6 +261,7 @@ internal object CanvasRegistry {
253
261
  }
254
262
  }
255
263
 
264
+ @Synchronized
256
265
  fun context(canvasId: String): WebGLContext? = contexts[canvasId]?.get()
257
266
  }
258
267
 
@@ -1,8 +1,9 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
2
  <resources>
3
3
  <!--
4
- The window background, which shows before the first Lynx frame and
5
- behind the system bars. MainActivity sets it again when dark mode
4
+ The window background, which shows behind the splash screen's image
5
+ and the system bars, and behind the page: angular.json's
6
+ splash.backgroundColor. MainActivity sets it again when dark mode
6
7
  switches while the app is open, from values-night's.
7
8
  -->
8
9
  <color name="window_background">#FFFFFFFF</color>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <resources>
3
+ <!--
4
+ The splash screen's image (themes.xml): the launcher icon, as Android 12
5
+ shows by default. With angular.json's splash.image, the generator
6
+ deletes this file and writes drawable-*/splash_icon.png instead.
7
+ -->
8
+ <drawable name="splash_icon">@mipmap/ic_launcher</drawable>
9
+ </resources>
@@ -10,4 +10,18 @@
10
10
  <item name="android:windowLightStatusBar">true</item>
11
11
  <item name="android:windowLightNavigationBar">true</item>
12
12
  </style>
13
+
14
+ <!--
15
+ The splash screen, from the tap on the icon until the app's first screen
16
+ is ready (MainActivity keeps it up until the runtime says so). AndroidX's
17
+ core-splashscreen draws it as Android 12 and later do on every version,
18
+ and switches to the theme above once it's installed. Its color is the
19
+ window's, and its image splash_icon: the launcher icon, or angular.json's
20
+ splash.image, which the generator writes in its place.
21
+ -->
22
+ <style name="Theme.AngularLynxShell.Starting" parent="Theme.SplashScreen">
23
+ <item name="windowSplashScreenBackground">@color/window_background</item>
24
+ <item name="windowSplashScreenAnimatedIcon">@drawable/splash_icon</item>
25
+ <item name="postSplashScreenTheme">@style/Theme.AngularLynxShell</item>
26
+ </style>
13
27
  </resources>
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <!--
3
+ The app's static shortcuts, in the menu a long press on its icon opens.
4
+ The generator writes this file from angular.json's shortcuts; an app
5
+ without any keeps this empty list.
6
+ -->
7
+ <shortcuts xmlns:android="http://schemas.android.com/apk/res/android" />
@@ -21,4 +21,6 @@ object DevServer {
21
21
  fun remoteUrl(url: String): String? = null
22
22
 
23
23
  fun loaded(url: String, error: Throwable?) = Unit
24
+
25
+ fun isShowingMessage(): Boolean = false
24
26
  }
@@ -9,6 +9,14 @@
9
9
  // (`enableMTSModule`). The runtime batches the calls into one `run` per frame;
10
10
  // see packages/runtime/src/lib/canvas/webgl-bridge.ts for the protocol.
11
11
  //
12
+ // Lynx's main thread is its engine thread, which the shell runs off the UI
13
+ // thread (LynxViewController's thread strategy), and the module's calls
14
+ // arrive there. So every GL call is made there: an EGL context is current on
15
+ // one thread at a time, and the context keeps it current between batches.
16
+ // The canvas's view lives on the UI thread, and hands the context what it
17
+ // draws into (AngularLynxCanvasTarget) through Lynx's runOnTasmThread:, so
18
+ // the registry and the contexts are the engine thread's alone.
19
+ //
12
20
  // The OpenGL ES 3.0 is ANGLE's, on Metal (libEGL and libGLESv2, prebuilt
13
21
  // with Lynx's frameworks by scripts/build-angle-ios-frameworks.sh), rather
14
22
  // than iOS's own OpenGL ES. Apple deprecated that in iOS 12, and the
@@ -18,18 +26,37 @@
18
26
  // 3.00, is WebGL2's, so three.js's shaders compile as they are.
19
27
 
20
28
  #import <Foundation/Foundation.h>
29
+ #import <Lynx/LynxContext.h>
21
30
  #import <Lynx/LynxModule.h>
22
31
  #import <Lynx/LynxUI.h>
32
+ #import <QuartzCore/QuartzCore.h>
23
33
  #import <UIKit/UIKit.h>
24
34
 
25
35
  NS_ASSUME_NONNULL_BEGIN
26
36
 
27
37
  @class AngularLynxCanvasView;
28
38
 
39
+ /// What a canvas's view gives its context to show frames in: the view's
40
+ /// CAMetalLayer, which the context makes its window surface on, and the
41
+ /// layer's size in pixels. The context reads it on the engine thread, where
42
+ /// a UIView mustn't be touched, nor released for the last time.
43
+ @interface AngularLynxCanvasTarget : NSObject
44
+ @property(nonatomic, readonly) CAMetalLayer *layer;
45
+ /// The layer's size in pixels as the view was last laid out: written on the
46
+ /// UI thread, read on the engine thread.
47
+ @property(atomic) CGSize pixelSize;
48
+ /// The view, for the UI thread alone (the context sets its opacity there).
49
+ @property(nonatomic, weak, readonly, nullable) AngularLynxCanvasView *view;
50
+ @end
51
+
29
52
  /// The drawing surface: a view backed by a CAMetalLayer, which the context
30
53
  /// copies each finished frame into (an EGL window surface on the layer).
31
54
  @interface AngularLynxCanvasView : UIView
32
55
  @property(nonatomic, copy, nullable) NSString *canvasId;
56
+ /// The LynxView's context, whose engine thread the view hands its target to
57
+ /// (runOnTasmThread:). The canvas element sets it before the canvas id.
58
+ @property(nonatomic, weak, nullable) LynxContext *lynxContext;
59
+ @property(nonatomic, readonly) AngularLynxCanvasTarget *target;
33
60
  @end
34
61
 
35
62
  /// The `canvas` element. Its `canvas-id` attribute, which the runtime sets,
@@ -63,24 +90,25 @@ NS_ASSUME_NONNULL_BEGIN
63
90
  - (NSDictionary *)encodeImageOfType:(NSString *)type
64
91
  quality:(double)quality
65
92
  premultiplied:(BOOL)premultiplied;
66
- /// Shows frames in this view, or nowhere while it's nil.
67
- - (void)attachView:(nullable AngularLynxCanvasView *)view;
93
+ /// Shows frames in this canvas's target, or nowhere while it's nil.
94
+ - (void)attachTarget:(nullable AngularLynxCanvasTarget *)target;
68
95
  /// The view was laid out: a frame that waited for it to have a size is shown.
69
- - (void)viewDidLayOut:(AngularLynxCanvasView *)view;
96
+ - (void)targetDidLayOut:(AngularLynxCanvasTarget *)target;
70
97
  - (void)destroy;
71
98
  @end
72
99
 
73
100
  /// Pairs canvases with contexts by canvas id: either can come first, as the
74
101
  /// runtime may make the context before Lynx has made the element's view.
102
+ /// Engine thread only, as the contexts are.
75
103
  @interface AngularLynxCanvasRegistry : NSObject
76
- + (void)setView:(nullable AngularLynxCanvasView *)view forCanvasId:(NSString *)canvasId;
77
- + (nullable AngularLynxCanvasView *)viewForCanvasId:(NSString *)canvasId;
104
+ + (void)setTarget:(nullable AngularLynxCanvasTarget *)target forCanvasId:(NSString *)canvasId;
105
+ + (nullable AngularLynxCanvasTarget *)targetForCanvasId:(NSString *)canvasId;
78
106
  + (void)setContext:(nullable AngularLynxWebGLContext *)context forCanvasId:(NSString *)canvasId;
79
107
  + (nullable AngularLynxWebGLContext *)contextForCanvasId:(NSString *)canvasId;
80
108
  @end
81
109
 
82
110
  /// The `AngularLynxWebGL` module. Registered on the LynxView's config, and
83
- /// called from the main thread.
111
+ /// called from Lynx's main thread, on its engine thread.
84
112
  @interface AngularLynxWebGLModule : NSObject <LynxModule>
85
113
  @end
86
114
 
@@ -8,8 +8,8 @@
8
8
 
9
9
  @implementation AngularLynxCanvasRegistry
10
10
 
11
- // Weak both ways: the view belongs to Lynx, the context to the module.
12
- static NSMapTable<NSString *, AngularLynxCanvasView *> *views(void) {
11
+ // Weak both ways: the target belongs to its view, the context to the module.
12
+ static NSMapTable<NSString *, AngularLynxCanvasTarget *> *targets(void) {
13
13
  static NSMapTable *table;
14
14
  static dispatch_once_t once;
15
15
  dispatch_once(&once, ^{
@@ -27,23 +27,23 @@ static NSMapTable<NSString *, AngularLynxWebGLContext *> *contexts(void) {
27
27
  return table;
28
28
  }
29
29
 
30
- + (void)setView:(AngularLynxCanvasView *)view forCanvasId:(NSString *)canvasId {
31
- if (view) {
32
- [views() setObject:view forKey:canvasId];
30
+ + (void)setTarget:(AngularLynxCanvasTarget *)target forCanvasId:(NSString *)canvasId {
31
+ if (target) {
32
+ [targets() setObject:target forKey:canvasId];
33
33
  } else {
34
- [views() removeObjectForKey:canvasId];
34
+ [targets() removeObjectForKey:canvasId];
35
35
  }
36
- [[contexts() objectForKey:canvasId] attachView:view];
36
+ [[contexts() objectForKey:canvasId] attachTarget:target];
37
37
  }
38
38
 
39
- + (AngularLynxCanvasView *)viewForCanvasId:(NSString *)canvasId {
40
- return [views() objectForKey:canvasId];
39
+ + (AngularLynxCanvasTarget *)targetForCanvasId:(NSString *)canvasId {
40
+ return [targets() objectForKey:canvasId];
41
41
  }
42
42
 
43
43
  + (void)setContext:(AngularLynxWebGLContext *)context forCanvasId:(NSString *)canvasId {
44
44
  if (context) {
45
45
  [contexts() setObject:context forKey:canvasId];
46
- [context attachView:[views() objectForKey:canvasId]];
46
+ [context attachTarget:[targets() objectForKey:canvasId]];
47
47
  } else {
48
48
  [contexts() removeObjectForKey:canvasId];
49
49
  }
@@ -289,6 +289,18 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
289
289
 
290
290
  #pragma mark - Canvas
291
291
 
292
+ @implementation AngularLynxCanvasTarget
293
+
294
+ - (instancetype)initWithView:(AngularLynxCanvasView *)view {
295
+ if ((self = [super init])) {
296
+ _layer = (CAMetalLayer *)view.layer;
297
+ _view = view;
298
+ }
299
+ return self;
300
+ }
301
+
302
+ @end
303
+
292
304
  @implementation AngularLynxCanvasView
293
305
 
294
306
  + (Class)layerClass {
@@ -305,40 +317,62 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
305
317
  self.contentScaleFactor = UIScreen.mainScreen.scale;
306
318
  // Lynx dispatches touches itself, from the LynxView.
307
319
  self.userInteractionEnabled = NO;
320
+ _target = [[AngularLynxCanvasTarget alloc] initWithView:self];
308
321
  }
309
322
  return self;
310
323
  }
311
324
 
325
+ /// Runs `block` on Lynx's engine thread, where the module's calls arrive and
326
+ /// every GL call is made: next on it, or at once when this is it (it's the UI
327
+ /// thread under Lynx's default thread strategy). Nothing runs once the
328
+ /// LynxView has gone, nor do its contexts.
329
+ - (void)onEngineThread:(dispatch_block_t)block {
330
+ [_lynxContext runOnTasmThread:block];
331
+ }
332
+
312
333
  // A frame drawn before Lynx gave the view a size waits for one (see
313
334
  // present). An app that draws only when something changes, such as a photo
314
335
  // editor drawing its photo once, would otherwise show nothing until it drew
315
336
  // again; one drawing every frame never noticed.
316
337
  - (void)layoutSubviews {
317
338
  [super layoutSubviews];
318
- if (_canvasId) {
319
- [[AngularLynxCanvasRegistry contextForCanvasId:_canvasId] viewDidLayOut:self];
320
- }
339
+ // ANGLE sizes the layer's next drawable from its bounds and scale, as here.
340
+ CGSize size = self.bounds.size;
341
+ CGFloat scale = self.layer.contentsScale;
342
+ _target.pixelSize = CGSizeMake(floor(size.width * scale), floor(size.height * scale));
343
+ NSString *canvasId = _canvasId;
344
+ AngularLynxCanvasTarget *target = _target;
345
+ if (!canvasId) return;
346
+ [self onEngineThread:^{
347
+ [[AngularLynxCanvasRegistry contextForCanvasId:canvasId] targetDidLayOut:target];
348
+ }];
321
349
  }
322
350
 
323
351
  - (void)setCanvasId:(NSString *)canvasId {
324
352
  if ([_canvasId isEqualToString:canvasId]) return;
325
- if (_canvasId && [AngularLynxCanvasRegistry viewForCanvasId:_canvasId] == self) {
326
- [AngularLynxCanvasRegistry setView:nil forCanvasId:_canvasId];
327
- }
353
+ NSString *old = _canvasId;
328
354
  _canvasId = [canvasId copy];
329
- if (_canvasId) [AngularLynxCanvasRegistry setView:self forCanvasId:_canvasId];
355
+ NSString *new = _canvasId;
356
+ AngularLynxCanvasTarget *target = _target;
357
+ [self onEngineThread:^{
358
+ if (old && [AngularLynxCanvasRegistry targetForCanvasId:old] == target) {
359
+ [AngularLynxCanvasRegistry setTarget:nil forCanvasId:old];
360
+ }
361
+ if (new) [AngularLynxCanvasRegistry setTarget:target forCanvasId:new];
362
+ }];
330
363
  }
331
364
 
332
365
  - (void)dealloc {
333
- // The registry holds the view weakly; a context still showing frames in it
334
- // stops.
366
+ // The context holds the target, and its layer, while it shows frames in
367
+ // it; it lets go here.
335
368
  NSString *canvasId = _canvasId;
369
+ AngularLynxCanvasTarget *target = _target;
336
370
  if (canvasId) {
337
- dispatch_async(dispatch_get_main_queue(), ^{
338
- if (![AngularLynxCanvasRegistry viewForCanvasId:canvasId]) {
339
- [[AngularLynxCanvasRegistry contextForCanvasId:canvasId] attachView:nil];
371
+ [_lynxContext runOnTasmThread:^{
372
+ if ([AngularLynxCanvasRegistry targetForCanvasId:canvasId] == target) {
373
+ [AngularLynxCanvasRegistry setTarget:nil forCanvasId:canvasId];
340
374
  }
341
- });
375
+ }];
342
376
  }
343
377
  }
344
378
 
@@ -351,6 +385,7 @@ static AngularLynxDecodedImage *decodeImage(NSData *bytes, NSDictionary *options
351
385
  }
352
386
 
353
387
  LYNX_PROP_SETTER("canvas-id", setCanvasId, NSString *) {
388
+ self.view.lynxContext = self.context.lynxContext;
354
389
  self.view.canvasId = requestReset || ![value isKindOfClass:NSString.class] ? nil : value;
355
390
  }
356
391
 
@@ -185,7 +185,7 @@ static NSUInteger componentsOf(GLenum format) {
185
185
 
186
186
  // The view's layer, as a window surface each frame is copied into. Made
187
187
  // when a frame is first shown in the view, as the surface's framebuffer 0.
188
- __weak AngularLynxCanvasView *_view;
188
+ AngularLynxCanvasTarget *_target;
189
189
  EGLSurface _surface;
190
190
  /// A frame was finished before there was a view to show it in.
191
191
  BOOL _framePending;
@@ -420,41 +420,45 @@ static NSUInteger componentsOf(GLenum format) {
420
420
  glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _drawFramebuffer);
421
421
  }
422
422
 
423
- - (void)attachView:(AngularLynxCanvasView *)view {
424
- // A view that went away has already turned _view (weak) to nil, but its
425
- // layer lives on in the surface until the surface is let go of here.
426
- if (_view == view && (view || _surface == EGL_NO_SURFACE)) return;
423
+ - (void)attachTarget:(AngularLynxCanvasTarget *)target {
424
+ if (_target == target) return;
425
+ // The surface holds the old target's layer until it's let go of here.
427
426
  [self releaseSurface];
428
- _view = view;
429
- if (view) {
430
- view.layer.opaque = !_alpha;
431
- view.opaque = !_alpha;
427
+ _target = target;
428
+ if (target) {
429
+ // The view's opacity is the UI thread's to set.
430
+ BOOL opaque = !_alpha;
431
+ dispatch_async(dispatch_get_main_queue(), ^{
432
+ AngularLynxCanvasView *view = target.view;
433
+ view.layer.opaque = opaque;
434
+ view.opaque = opaque;
435
+ });
432
436
  if (_framePending) [self present];
433
437
  }
434
438
  }
435
439
 
436
- - (void)viewDidLayOut:(AngularLynxCanvasView *)view {
437
- if (view == _view && _framePending) [self present];
440
+ - (void)targetDidLayOut:(AngularLynxCanvasTarget *)target {
441
+ if (target == _target && _framePending) [self present];
438
442
  }
439
443
 
440
444
  /// Shows the finished frame: copies the default framebuffer (resolving it
441
445
  /// when it's multisampled) into the view's layer, scaled to its size, and
442
446
  /// presents it. Without a laid-out view, the frame waits for one.
443
447
  - (void)present {
444
- AngularLynxCanvasView *view = _view;
445
- CGSize size = view.bounds.size;
446
- // ANGLE sizes the layer's next drawable from its bounds and scale, as
447
- // here, when the blit below first draws to it.
448
- CGFloat scale = view.layer.contentsScale;
449
- GLint layerWidth = (GLint)(size.width * scale);
450
- GLint layerHeight = (GLint)(size.height * scale);
451
- if (!view || layerWidth < 1 || layerHeight < 1) {
448
+ AngularLynxCanvasTarget *target = _target;
449
+ // The layer's size as the view was last laid out: ANGLE sizes the layer's
450
+ // next drawable from its bounds and scale, as the view measured it, when
451
+ // the blit below first draws to it.
452
+ CGSize size = target.pixelSize;
453
+ GLint layerWidth = (GLint)size.width;
454
+ GLint layerHeight = (GLint)size.height;
455
+ if (!target || layerWidth < 1 || layerHeight < 1) {
452
456
  _framePending = YES;
453
457
  return;
454
458
  }
455
459
  if (_surface == EGL_NO_SURFACE) {
456
- _surface = eglCreateWindowSurface(_display, _config, (__bridge EGLNativeWindowType)view.layer,
457
- NULL);
460
+ _surface = eglCreateWindowSurface(_display, _config,
461
+ (__bridge EGLNativeWindowType)target.layer, NULL);
458
462
  if (_surface == EGL_NO_SURFACE) {
459
463
  NSLog(@"[AngularLynxWebGL] No window surface for the canvas (EGL error 0x%x)", eglGetError());
460
464
  _framePending = YES;