@symbiote-native/gl 0.0.0-stage → 0.0.0

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 (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +38 -2
  3. package/build/angular/gl-view/index.d.ts +18 -0
  4. package/build/angular/gl-view/index.js +87 -0
  5. package/build/angular/index.d.ts +2 -0
  6. package/build/angular/index.js +3 -0
  7. package/build/core/constants.d.ts +4 -0
  8. package/build/core/constants.js +4 -0
  9. package/build/core/gl-context.d.ts +13 -0
  10. package/build/core/gl-context.js +54 -0
  11. package/build/core/gl-errors.d.ts +1 -0
  12. package/build/core/gl-errors.js +8 -0
  13. package/build/core/gl-utils.d.ts +5 -0
  14. package/build/core/gl-utils.js +84 -0
  15. package/build/core/gl-view.d.ts +16 -0
  16. package/build/core/gl-view.js +99 -0
  17. package/build/core/index.d.ts +6 -0
  18. package/build/core/index.js +5 -0
  19. package/build/core/native-module.d.ts +15 -0
  20. package/build/core/native-module.js +3 -0
  21. package/build/core/types.d.ts +75 -0
  22. package/build/core/types.js +13 -0
  23. package/build/core/worklet-context-manager.d.ts +6 -0
  24. package/build/core/worklet-context-manager.js +34 -0
  25. package/build/react/gl-view.d.ts +8 -0
  26. package/build/react/gl-view.js +6 -0
  27. package/build/react/index.d.ts +3 -0
  28. package/build/react/index.js +3 -0
  29. package/build/solid/gl-view.d.ts +13 -0
  30. package/build/solid/gl-view.js +4 -0
  31. package/build/solid/index.d.ts +2 -0
  32. package/build/solid/index.js +3 -0
  33. package/build/svelte/gl-view.svelte +19 -0
  34. package/build/svelte/gl-view.svelte.d.ts +9 -0
  35. package/build/svelte/index.d.ts +2 -0
  36. package/build/svelte/index.js +3 -0
  37. package/build/vue/gl-view.d.ts +2 -0
  38. package/build/vue/gl-view.js +4 -0
  39. package/build/vue/index.d.ts +2 -0
  40. package/build/vue/index.js +3 -0
  41. package/build-ngc/angular/gl-view/index.d.ts +21 -0
  42. package/build-ngc/angular/gl-view/index.js +46 -0
  43. package/build-ngc/angular/gl-view/index.js.map +1 -0
  44. package/build-ngc/angular/index.d.ts +2 -0
  45. package/build-ngc/angular/index.js +4 -0
  46. package/build-ngc/angular/index.js.map +1 -0
  47. package/build-ngc/core/constants.d.ts +4 -0
  48. package/build-ngc/core/constants.js +5 -0
  49. package/build-ngc/core/constants.js.map +1 -0
  50. package/build-ngc/core/gl-context.d.ts +13 -0
  51. package/build-ngc/core/gl-context.js +55 -0
  52. package/build-ngc/core/gl-context.js.map +1 -0
  53. package/build-ngc/core/gl-errors.d.ts +1 -0
  54. package/build-ngc/core/gl-errors.js +9 -0
  55. package/build-ngc/core/gl-errors.js.map +1 -0
  56. package/build-ngc/core/gl-utils.d.ts +5 -0
  57. package/build-ngc/core/gl-utils.js +85 -0
  58. package/build-ngc/core/gl-utils.js.map +1 -0
  59. package/build-ngc/core/gl-view.d.ts +16 -0
  60. package/build-ngc/core/gl-view.js +100 -0
  61. package/build-ngc/core/gl-view.js.map +1 -0
  62. package/build-ngc/core/index.d.ts +6 -0
  63. package/build-ngc/core/index.js +6 -0
  64. package/build-ngc/core/index.js.map +1 -0
  65. package/build-ngc/core/native-module.d.ts +15 -0
  66. package/build-ngc/core/native-module.js +4 -0
  67. package/build-ngc/core/native-module.js.map +1 -0
  68. package/build-ngc/core/types.d.ts +75 -0
  69. package/build-ngc/core/types.js +14 -0
  70. package/build-ngc/core/types.js.map +1 -0
  71. package/build-ngc/core/worklet-context-manager.d.ts +6 -0
  72. package/build-ngc/core/worklet-context-manager.js +35 -0
  73. package/build-ngc/core/worklet-context-manager.js.map +1 -0
  74. package/native-link.json +12 -0
  75. package/package.json +145 -3
  76. package/src/angular/gl-view/index.ts +50 -0
  77. package/src/angular/index.ts +4 -0
  78. package/src/core/constants.ts +6 -0
  79. package/src/core/gl-context.ts +74 -0
  80. package/src/core/gl-errors.ts +8 -0
  81. package/src/core/gl-utils.ts +97 -0
  82. package/src/core/gl-view.ts +143 -0
  83. package/src/core/index.ts +21 -0
  84. package/src/core/native-module.ts +18 -0
  85. package/src/core/types.ts +95 -0
  86. package/src/core/worklet-context-manager.ts +42 -0
  87. package/src/react/gl-view.ts +14 -0
  88. package/src/react/index.ts +5 -0
  89. package/src/solid/gl-view.ts +6 -0
  90. package/src/solid/index.ts +4 -0
  91. package/src/svelte/gl-view.svelte +19 -0
  92. package/src/svelte/index.ts +4 -0
  93. package/src/svelte/svelte-ambient.d.ts +3 -0
  94. package/src/svelte/svelte-compile.test-helper.ts +6 -0
  95. package/src/vue/gl-view.ts +5 -0
  96. package/src/vue/index.ts +4 -0
@@ -0,0 +1,143 @@
1
+ import {
2
+ Platform,
3
+ UnavailabilityError,
4
+ requireNativeViewManager,
5
+ } from 'expo-modules-core';
6
+ import { el } from '@symbiote-native/components';
7
+ import type { IDescriptor } from '@symbiote-native/components';
8
+ import {
9
+ defineExpoNativeView,
10
+ getNativeTag,
11
+ isDevBuild,
12
+ isSymbioteNode,
13
+ } from '@symbiote-native/engine';
14
+ import type { ISymbioteEvent, ISymbioteNode } from '@symbiote-native/engine';
15
+ import { GL_MODULE_NAME, GL_PACKAGE_NAME } from './constants';
16
+ import { getGl, takeSnapshotAsync, unregisterGLContext } from './gl-context';
17
+ import { glNativeModule } from './native-module';
18
+ import type {
19
+ IGLNodeOrTag,
20
+ IGLObject,
21
+ IGLSnapshotOptions,
22
+ IGLViewHandle,
23
+ IGLViewProps,
24
+ } from './types';
25
+
26
+ // Registered on render: a barrel side effect is lost in a release build
27
+ const glView = defineExpoNativeView(requireNativeViewManager, GL_MODULE_NAME);
28
+
29
+ export const glViewName = glView.name;
30
+
31
+ export const ensureGLViewRegistered = glView.ensureRegistered;
32
+
33
+ const DEFAULT_MSAA_SAMPLES = 4;
34
+
35
+ export type IGLView = {
36
+ handle: IGLViewHandle;
37
+ /** `null` means the view cannot register and the caller renders nothing */
38
+ render(props: object): IDescriptor | null;
39
+ /** Forgets the context of the surface, called when the view unmounts */
40
+ dispose(): void;
41
+ };
42
+
43
+ type IGetNode = () => ISymbioteNode | null | undefined;
44
+
45
+ function isSurfaceCreated(value: unknown): value is { exglCtxId: number } {
46
+ return typeof Reflect.get(Object(value), 'exglCtxId') === 'number';
47
+ }
48
+
49
+ function isContextCallback(value: unknown): value is IGLViewProps['onContextCreate'] {
50
+ return typeof value === 'function';
51
+ }
52
+
53
+ // Adapters hand over the props they hold, a missing callback is a view nobody listens to
54
+ function toViewProps(props: object): IGLViewProps {
55
+ const onContextCreate: unknown = Reflect.get(props, 'onContextCreate');
56
+ return {
57
+ ...props,
58
+ onContextCreate: isContextCallback(onContextCreate) ? onContextCreate : () => undefined,
59
+ };
60
+ }
61
+
62
+ function nativeTagOf(camera: IGLNodeOrTag): number {
63
+ const tag = isSymbioteNode(camera) ? getNativeTag(camera) : camera;
64
+ if (typeof tag !== 'number') throw new Error('The camera view has no native tag yet');
65
+ return tag;
66
+ }
67
+
68
+ /** One per mounted view, `getNode` is kept for the shape the adapters share */
69
+ export function createGLView(_getNode: IGetNode): IGLView {
70
+ let exglCtxId: number | undefined;
71
+ let latestProps: IGLViewProps | undefined;
72
+ let hasWorkletSupport: boolean | undefined;
73
+
74
+ const onSurfaceCreate = (event: ISymbioteEvent): void => {
75
+ if (!isSurfaceCreated(event.nativeEvent)) return;
76
+ exglCtxId = event.nativeEvent.exglCtxId;
77
+ latestProps?.onContextCreate(getGl(exglCtxId));
78
+ };
79
+
80
+ function warnOnWorkletChange(enabled: boolean): void {
81
+ if (hasWorkletSupport !== undefined && hasWorkletSupport !== enabled) {
82
+ console.warn('Updating prop enableExperimentalWorkletSupport is not supported');
83
+ }
84
+ hasWorkletSupport = enabled;
85
+ }
86
+
87
+ const handle: IGLViewHandle = {
88
+ get exglCtxId() {
89
+ return exglCtxId;
90
+ },
91
+ async createCameraTextureAsync(camera) {
92
+ if (!glNativeModule.createCameraTextureAsync) {
93
+ throw new UnavailabilityError(GL_PACKAGE_NAME, 'createCameraTextureAsync');
94
+ }
95
+ if (!exglCtxId) throw new Error("GLView's surface is not created yet!");
96
+ const { exglObjId } = await glNativeModule.createCameraTextureAsync(
97
+ exglCtxId,
98
+ nativeTagOf(camera),
99
+ );
100
+ const texture: WebGLTexture = { id: exglObjId };
101
+ return texture;
102
+ },
103
+ async destroyObjectAsync(glObject: IGLObject) {
104
+ if (!glNativeModule.destroyObjectAsync) {
105
+ throw new UnavailabilityError(GL_PACKAGE_NAME, 'destroyObjectAsync');
106
+ }
107
+ return glNativeModule.destroyObjectAsync(glObject.id);
108
+ },
109
+ takeSnapshotAsync: (options?: IGLSnapshotOptions) => takeSnapshotAsync(exglCtxId, options),
110
+ };
111
+
112
+ return {
113
+ handle,
114
+ dispose: () => {
115
+ if (exglCtxId) unregisterGLContext(exglCtxId);
116
+ },
117
+ render: props => {
118
+ if (!ensureGLViewRegistered()) {
119
+ if (isDevBuild()) console.warn("'GLView' is not available.");
120
+ return null;
121
+ }
122
+ latestProps = toViewProps(props);
123
+ const {
124
+ onContextCreate,
125
+ msaaSamples = DEFAULT_MSAA_SAMPLES,
126
+ enableExperimentalWorkletSupport = false,
127
+ ...viewProps
128
+ } = latestProps;
129
+ warnOnWorkletChange(enableExperimentalWorkletSupport);
130
+ return el('view', viewProps, [
131
+ el(glViewName(), {
132
+ style: {
133
+ flex: 1,
134
+ ...Platform.select({ ios: { backgroundColor: 'transparent' }, default: {} }),
135
+ },
136
+ onSurfaceCreate,
137
+ enableExperimentalWorkletSupport,
138
+ msaaSamples: Platform.select({ ios: msaaSamples, default: undefined }),
139
+ }),
140
+ ]);
141
+ },
142
+ };
143
+ }
@@ -0,0 +1,21 @@
1
+ export { GL_MODULE_NAME } from './constants';
2
+ export {
3
+ createContextAsync,
4
+ destroyContextAsync,
5
+ getGl,
6
+ getWorkletContext,
7
+ takeSnapshotAsync,
8
+ } from './gl-context';
9
+ export { configureLogging } from './gl-utils';
10
+ export { createGLView, ensureGLViewRegistered, glViewName, type IGLView } from './gl-view';
11
+ export { GLLoggingOption } from './types';
12
+ export type {
13
+ IExpoWebGLRenderingContext,
14
+ IGLNodeOrTag,
15
+ IGLObject,
16
+ IGLSnapshot,
17
+ IGLSnapshotOptions,
18
+ IGLSurfaceCreateEvent,
19
+ IGLViewHandle,
20
+ IGLViewProps,
21
+ } from './types';
@@ -0,0 +1,18 @@
1
+ import { requireNativeModule } from 'expo-modules-core';
2
+ import { GL_MODULE_NAME } from './constants';
3
+ import type { IGLSnapshot, IGLSnapshotOptions } from './types';
4
+
5
+ export type INativeGLModule = {
6
+ createContextAsync(): Promise<{ exglCtxId: number }>;
7
+ destroyContextAsync(exglCtxId: number): Promise<boolean>;
8
+ takeSnapshotAsync(exglCtxId: number, options: IGLSnapshotOptions): Promise<IGLSnapshot>;
9
+ /** Absent where there is no camera texture support */
10
+ createCameraTextureAsync?(
11
+ exglCtxId: number,
12
+ cameraTag: number,
13
+ ): Promise<{ exglObjId: number }>;
14
+ /** Absent where objects cannot be destroyed one by one */
15
+ destroyObjectAsync?(exglObjId: number): Promise<boolean>;
16
+ };
17
+
18
+ export const glNativeModule = requireNativeModule<INativeGLModule>(GL_MODULE_NAME);
@@ -0,0 +1,95 @@
1
+ import type {
2
+ IAccessibilityProps,
3
+ IAriaProps,
4
+ IResponderProps,
5
+ } from '@symbiote-native/components';
6
+ import type {
7
+ IStyleProp,
8
+ ISymbioteEvent,
9
+ ISymbioteNode,
10
+ IViewStyle,
11
+ } from '@symbiote-native/engine';
12
+
13
+ export type IGLObject = {
14
+ id: number;
15
+ };
16
+
17
+ export type IGLSurfaceCreateEvent = {
18
+ nativeEvent: {
19
+ exglCtxId: number;
20
+ };
21
+ };
22
+
23
+ export type IGLSnapshotOptions = {
24
+ /** @default false */
25
+ flip?: boolean;
26
+ /** Defaults to the framebuffer shown in the view, or the current one of a headless context */
27
+ framebuffer?: WebGLFramebuffer;
28
+ /** Passed to `glReadPixels` */
29
+ rect?: {
30
+ x: number;
31
+ y: number;
32
+ width: number;
33
+ height: number;
34
+ };
35
+ /** PNG is lossless but slower, iOS writes a PNG instead of WebP. @default 'jpeg' */
36
+ format?: 'jpeg' | 'png' | 'webp';
37
+ /** From `0` (most compressed) to `1.0` (none). @default 1.0 */
38
+ compress?: number;
39
+ };
40
+
41
+ export type IGLSnapshot = {
42
+ uri: string | Blob | null;
43
+ /** Same as `uri`, which makes a snapshot usable in `texImage2D` */
44
+ localUri: string;
45
+ width: number;
46
+ height: number;
47
+ };
48
+
49
+ export type IExpoWebGLRenderingContext = WebGL2RenderingContext & {
50
+ contextId: number;
51
+ endFrameEXP(): void;
52
+ flushEXP(): void;
53
+ __expoSetLogging(option: GLLoggingOption): void;
54
+ };
55
+
56
+ /** A native tag, or the host node of a view such as a camera */
57
+ export type IGLNodeOrTag = number | ISymbioteNode | null;
58
+
59
+ export type IGLViewProps = IAccessibilityProps &
60
+ IAriaProps &
61
+ IResponderProps & {
62
+ style?: IStyleProp<IViewStyle>;
63
+ testID?: string;
64
+ nativeID?: string;
65
+ onLayout?: (event: ISymbioteEvent) => void;
66
+ /** Called with the context once the surface exists */
67
+ onContextCreate(gl: IExpoWebGLRenderingContext): void;
68
+ /** Multisampling, `0` turns it off. @default 4 @platform ios */
69
+ msaaSamples?: number;
70
+ /** Lets the Reanimated worklet thread use the `gl` object. @default false */
71
+ enableExperimentalWorkletSupport?: boolean;
72
+ };
73
+
74
+ export enum GLLoggingOption {
75
+ DISABLED = 0,
76
+ /** Logs calls, their parameters and results */
77
+ METHOD_CALLS = 1,
78
+ /** Calls `getError` after each call, which blocks and slows drawing down */
79
+ GET_ERRORS = 2,
80
+ /** Prints the name of the constant a numeric parameter holds */
81
+ RESOLVE_CONSTANTS = 4,
82
+ /** Cuts long strings such as shaders */
83
+ TRUNCATE_STRINGS = 8,
84
+ ALL = METHOD_CALLS | GET_ERRORS | RESOLVE_CONSTANTS | TRUNCATE_STRINGS,
85
+ }
86
+
87
+ /** What a ref to the view hands out */
88
+ export type IGLViewHandle = {
89
+ /** The id of the context of the view, `undefined` until the surface exists */
90
+ readonly exglCtxId: number | undefined;
91
+ createCameraTextureAsync(camera: IGLNodeOrTag): Promise<WebGLTexture>;
92
+ destroyObjectAsync(glObject: IGLObject): Promise<boolean>;
93
+ /** A snapshot of the context of this view */
94
+ takeSnapshotAsync(options?: IGLSnapshotOptions): Promise<IGLSnapshot>;
95
+ };
@@ -0,0 +1,42 @@
1
+ import { GL_CONTEXTS_GLOBAL } from './constants';
2
+ import type { IExpoWebGLRenderingContext } from './types';
3
+
4
+ export type IWorkletContextManager = {
5
+ getContext(contextId: number): IExpoWebGLRenderingContext | undefined;
6
+ unregister?(contextId: number): void;
7
+ };
8
+
9
+ type IRunOnUI = (worklet: (contextId: number) => void) => (contextId: number) => void;
10
+
11
+ function isRunOnUI(value: unknown): value is IRunOnUI {
12
+ return typeof value === 'function';
13
+ }
14
+
15
+ // Reanimated stays optional, and an import of it in a try block in the module that also holds the
16
+ // view breaks with `inlineRequires`, so the manager lives in its own file
17
+ export function createWorkletContextManager(): IWorkletContextManager {
18
+ try {
19
+ // Reanimated has to load before any workletized code is created
20
+ const reanimated: unknown = require('react-native-reanimated');
21
+ const runOnUI = Reflect.get(Object(reanimated), 'runOnUI');
22
+ if (!isRunOnUI(runOnUI)) throw new Error('react-native-reanimated has no runOnUI');
23
+ return {
24
+ getContext: contextId => {
25
+ 'worklet';
26
+ return Reflect.get(Object(Reflect.get(globalThis, GL_CONTEXTS_GLOBAL)), String(contextId));
27
+ },
28
+ unregister: contextId => {
29
+ runOnUI(id => {
30
+ 'worklet';
31
+ Reflect.deleteProperty(Object(Reflect.get(globalThis, GL_CONTEXTS_GLOBAL)), String(id));
32
+ })(contextId);
33
+ },
34
+ };
35
+ } catch {
36
+ return {
37
+ getContext: () => {
38
+ throw new Error('Worklet runtime is not available');
39
+ },
40
+ };
41
+ }
42
+ }
@@ -0,0 +1,14 @@
1
+ import type { ReactElement, Ref } from 'react';
2
+ import { useNativeViewController } from '@symbiote-native/react';
3
+ import { createGLView } from '../core';
4
+ import type { IGLViewHandle, IGLViewProps } from '../core';
5
+
6
+ export type IGLViewReactProps = IGLViewProps & {
7
+ ref?: Ref<IGLViewHandle>;
8
+ className?: string;
9
+ };
10
+
11
+ /** React twin of `expo-gl`'s `GLView` */
12
+ export function GLView({ ref, ...props }: IGLViewReactProps): ReactElement | null {
13
+ return useNativeViewController(ref, createGLView, props);
14
+ }
@@ -0,0 +1,5 @@
1
+ // @symbiote-native/gl/react: a GLView that gives a WebGL context to draw into, on a shared core
2
+
3
+ export { GLView } from './gl-view';
4
+ export type { IGLViewReactProps } from './gl-view';
5
+ export * from '../core';
@@ -0,0 +1,6 @@
1
+ import { defineNativeViewComponent } from '@symbiote-native/solid';
2
+ import { createGLView } from '../core';
3
+ import type { IGLViewHandle, IGLViewProps } from '../core';
4
+
5
+ /** Solid twin of `expo-gl`'s `GLView`, `ref` gets the handle of the view */
6
+ export const GLView = defineNativeViewComponent<IGLViewHandle, IGLViewProps>(createGLView);
@@ -0,0 +1,4 @@
1
+ // @symbiote-native/gl/solid: a GLView that gives a WebGL context to draw into, on a shared core
2
+
3
+ export { GLView } from './gl-view';
4
+ export * from '../core';
@@ -0,0 +1,19 @@
1
+ <script lang="ts">
2
+ import DescriptorHost from '@symbiote-native/svelte/descriptor-host';
3
+ import { useNativeViewController } from '@symbiote-native/svelte/runes/use-native-view-controller';
4
+ import { createGLView } from '../core';
5
+ import type { IGLViewProps } from '../core';
6
+
7
+ const props: IGLViewProps = $props();
8
+ const view = useNativeViewController(createGLView, () => ({ ...props }));
9
+
10
+ // The functions of the view are instance members, `bind:this` reaches them
11
+ export const createCameraTextureAsync = view.handle.createCameraTextureAsync;
12
+ export const destroyObjectAsync = view.handle.destroyObjectAsync;
13
+ export const takeSnapshotAsync = view.handle.takeSnapshotAsync;
14
+ export const getContextId = (): number | undefined => view.handle.exglCtxId;
15
+ </script>
16
+
17
+ {#if view.descriptor}
18
+ <DescriptorHost descriptor={view.descriptor} />
19
+ {/if}
@@ -0,0 +1,4 @@
1
+ // @symbiote-native/gl/svelte: a GLView that gives a WebGL context to draw into, on a shared core
2
+
3
+ export { default as GLView } from './gl-view.svelte';
4
+ export * from '../core';
@@ -0,0 +1,3 @@
1
+ // Pulls in svelte's ambient globals ($state/$effect/...) for this package's tsc program
2
+ // This package has no value import from 'svelte', so a triple-slash reference does it explicitly
3
+ /// <reference types="svelte" />
@@ -0,0 +1,6 @@
1
+ // Re-exports the existing harness instead of another copy
2
+ export {
3
+ createSvelteHarness,
4
+ loadComponent,
5
+ type ISvelteHarness,
6
+ } from '../../../sqlite/src/svelte/svelte-compile.test-helper';
@@ -0,0 +1,5 @@
1
+ import { defineNativeViewComponent } from '@symbiote-native/vue';
2
+ import { createGLView } from '../core';
3
+
4
+ /** Vue twin of `expo-gl`'s `GLView`, `ref` gets the handle of the view */
5
+ export const GLView = defineNativeViewComponent('GLView', createGLView);
@@ -0,0 +1,4 @@
1
+ // @symbiote-native/gl/vue: a GLView that gives a WebGL context to draw into, on a shared core
2
+
3
+ export { GLView } from './gl-view';
4
+ export * from '../core';