@symbiote-native/gl 0.0.0-stage → 0.1.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 +219 -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 +107 -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 +35 -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 +108 -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 +36 -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 +51 -0
  77. package/src/angular/index.ts +4 -0
  78. package/src/core/constants.ts +6 -0
  79. package/src/core/gl-context.ts +79 -0
  80. package/src/core/gl-errors.ts +8 -0
  81. package/src/core/gl-utils.ts +106 -0
  82. package/src/core/gl-view.ts +160 -0
  83. package/src/core/index.ts +26 -0
  84. package/src/core/native-module.ts +22 -0
  85. package/src/core/types.ts +95 -0
  86. package/src/core/worklet-context-manager.ts +52 -0
  87. package/src/react/gl-view.ts +17 -0
  88. package/src/react/index.ts +5 -0
  89. package/src/solid/gl-view.ts +8 -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,79 @@
1
+ import { CodedError } from 'expo-modules-core';
2
+ import { GL_CONTEXTS_GLOBAL } from './constants';
3
+ import { configureLogging } from './gl-utils';
4
+ import { glNativeModule } from './native-module';
5
+ import type {
6
+ IExpoWebGLRenderingContext,
7
+ IGLSnapshot,
8
+ IGLSnapshotOptions,
9
+ } from './types';
10
+ import { createWorkletContextManager } from './worklet-context-manager';
11
+
12
+ const workletContextManager = createWorkletContextManager();
13
+
14
+ function isContext(value: unknown): value is IExpoWebGLRenderingContext {
15
+ return typeof value === 'object' && value !== null;
16
+ }
17
+
18
+ /** The context of a worklet, `exgl` has to be created with worklet support */
19
+ export function getWorkletContext(
20
+ contextId: number,
21
+ ): IExpoWebGLRenderingContext | undefined {
22
+ 'worklet';
23
+ return workletContextManager.getContext(contextId);
24
+ }
25
+
26
+ export function unregisterGLContext(exglCtxId: number): void {
27
+ const contexts: unknown = Reflect.get(globalThis, GL_CONTEXTS_GLOBAL);
28
+ if (contexts) Reflect.deleteProperty(Object(contexts), String(exglCtxId));
29
+ workletContextManager.unregister?.(exglCtxId);
30
+ }
31
+
32
+ /** The GL interface of a context id the native side created */
33
+ export function getGl(exglCtxId: number): IExpoWebGLRenderingContext {
34
+ const contexts: unknown = Reflect.get(globalThis, GL_CONTEXTS_GLOBAL);
35
+ if (!contexts) {
36
+ throw new CodedError(
37
+ 'ERR_GL_NOT_AVAILABLE',
38
+ 'GL is currently not available. (Have you enabled remote debugging? GL is not available while debugging remotely.)',
39
+ );
40
+ }
41
+ const gl: unknown = Reflect.get(Object(contexts), String(exglCtxId));
42
+ if (!isContext(gl))
43
+ throw new Error(`There is no EXGLContext with id ${exglCtxId}`);
44
+ configureLogging(gl);
45
+ return gl;
46
+ }
47
+
48
+ export function getContextId(
49
+ exgl?: IExpoWebGLRenderingContext | number,
50
+ ): number {
51
+ const exglCtxId = exgl && typeof exgl === 'object' ? exgl.contextId : exgl;
52
+ if (!exglCtxId || typeof exglCtxId !== 'number') {
53
+ throw new Error(`Invalid EXGLContext id: ${String(exglCtxId)}`);
54
+ }
55
+ return exglCtxId;
56
+ }
57
+
58
+ /** A context with no view, which does not present its framebuffer, take a snapshot to see it */
59
+ export async function createContextAsync(): Promise<IExpoWebGLRenderingContext> {
60
+ const { exglCtxId } = await glNativeModule.createContextAsync();
61
+ return getGl(exglCtxId);
62
+ }
63
+
64
+ /** For a headless context made by `createContextAsync` */
65
+ export async function destroyContextAsync(
66
+ exgl?: IExpoWebGLRenderingContext | number,
67
+ ): Promise<boolean> {
68
+ const exglCtxId = getContextId(exgl);
69
+ unregisterGLContext(exglCtxId);
70
+ return glNativeModule.destroyContextAsync(exglCtxId);
71
+ }
72
+
73
+ /** Saves the framebuffer as a file in the cache directory of the app */
74
+ export async function takeSnapshotAsync(
75
+ exgl?: IExpoWebGLRenderingContext | number,
76
+ options: IGLSnapshotOptions = {},
77
+ ): Promise<IGLSnapshot> {
78
+ return glNativeModule.takeSnapshotAsync(getContextId(exgl), options);
79
+ }
@@ -0,0 +1,8 @@
1
+ export const GL_ERRORS: Readonly<Record<number, string>> = {
2
+ 1280: 'INVALID ENUM: An unacceptable value has been specified for an enumerated argument.',
3
+ 1281: 'INVALID_VALUE: A numeric argument is out of range.',
4
+ 1282: 'INVALID_OPERATION: The specified command is not allowed for the current state.',
5
+ 1285: 'OUT_OF_MEMORY: Not enough memory is left to execute the command.',
6
+ 1286: 'INVALID_FRAMEBUFFER_OPERATION: The currently bound framebuffer is not complete when trying to render to or to read from it.',
7
+ 37442: 'CONTEXT_LOST_WEBGL: The WebGL context is lost.',
8
+ };
@@ -0,0 +1,106 @@
1
+ import { GL_ERRORS } from './gl-errors';
2
+ import { GLLoggingOption } from './types';
3
+ import type { IExpoWebGLRenderingContext } from './types';
4
+
5
+ type ILoggableGL = Pick<IExpoWebGLRenderingContext, '__expoSetLogging'>;
6
+
7
+ type IGLFunction = (...args: unknown[]) => unknown;
8
+
9
+ const MAX_STRING_LENGTH = 20;
10
+
11
+ const SET_LOGGING_KEY = '__expoSetLogging';
12
+
13
+ const wrappers = new WeakSet<IGLFunction>();
14
+
15
+ function isGLFunction(value: unknown): value is IGLFunction {
16
+ return typeof value === 'function';
17
+ }
18
+
19
+ function constantNameOf(gl: object, value: number): string | undefined {
20
+ for (const name in gl) {
21
+ if (Reflect.get(gl, name) === value) return name;
22
+ }
23
+ return undefined;
24
+ }
25
+
26
+ // Numbers get the name of a constant that holds them, which can mislead for a size, so the
27
+ // number stays. Strings are cut because a shader is long and logging it blocks the bridge
28
+ function describeArg(gl: object, option: number, arg: unknown): string {
29
+ if (option & GLLoggingOption.RESOLVE_CONSTANTS && typeof arg === 'number') {
30
+ const name = constantNameOf(gl, arg);
31
+ if (name) return `${arg} (${name})`;
32
+ }
33
+ const isLong = typeof arg === 'string' && arg.length > MAX_STRING_LENGTH;
34
+ if (option & GLLoggingOption.TRUNCATE_STRINGS && isLong) {
35
+ const lastSpace = arg.lastIndexOf(' ', MAX_STRING_LENGTH);
36
+ return `${arg.slice(0, lastSpace >= 0 ? lastSpace : MAX_STRING_LENGTH)}...`;
37
+ }
38
+ return String(arg);
39
+ }
40
+
41
+ // The original `getError`, a wrapped one would log itself
42
+ function reportError(gl: object): void {
43
+ const getError: unknown = Reflect.get(
44
+ Reflect.getPrototypeOf(gl) ?? {},
45
+ 'getError',
46
+ );
47
+ if (!isGLFunction(getError)) return;
48
+ const error = getError.call(gl);
49
+ if (typeof error !== 'number' || error === Reflect.get(gl, 'NO_ERROR'))
50
+ return;
51
+ console.warn(`\x1b[31mExpoGL: Error ${GL_ERRORS[error]}\x1b[0m`);
52
+ }
53
+
54
+ function wrapMethod(
55
+ gl: object,
56
+ key: string,
57
+ original: IGLFunction,
58
+ getOption: () => number,
59
+ ): void {
60
+ const wrapper: IGLFunction = (...args) => {
61
+ const option = getOption();
62
+ if (option & GLLoggingOption.METHOD_CALLS) {
63
+ const params = args.map(arg => describeArg(gl, option, arg));
64
+ console.warn(`ExpoGL: ${key}(${params.join(', ')})`);
65
+ }
66
+ const result = original.apply(gl, args);
67
+ if (option & GLLoggingOption.METHOD_CALLS)
68
+ console.warn(`ExpoGL: = ${String(result)}`);
69
+ if (option & GLLoggingOption.GET_ERRORS && key !== 'getError')
70
+ reportError(gl);
71
+ return result;
72
+ };
73
+ wrappers.add(wrapper);
74
+ Reflect.set(gl, key, wrapper);
75
+ }
76
+
77
+ function wrapMethods(gl: object, getOption: () => number): void {
78
+ for (const [key, original] of Object.entries(
79
+ Reflect.getPrototypeOf(gl) ?? {},
80
+ )) {
81
+ if (isGLFunction(original) && key !== SET_LOGGING_KEY) {
82
+ wrapMethod(gl, key, original, getOption);
83
+ }
84
+ }
85
+ }
86
+
87
+ function unwrapMethods(gl: object): void {
88
+ for (const [key, value] of Object.entries(gl)) {
89
+ if (isGLFunction(value) && wrappers.has(value))
90
+ Reflect.deleteProperty(gl, key);
91
+ }
92
+ }
93
+
94
+ /** Adds `__expoSetLogging` to the context, for the logging options useful in debugging GL calls */
95
+ export function configureLogging(gl: ILoggableGL): void {
96
+ let loggingOption: number = GLLoggingOption.DISABLED;
97
+
98
+ gl.__expoSetLogging = (option: GLLoggingOption): void => {
99
+ // Both on or both off needs no wrapping or unwrapping, only the new option
100
+ if (!loggingOption !== !option) {
101
+ if (option) wrapMethods(gl, () => loggingOption);
102
+ else unwrapMethods(gl);
103
+ }
104
+ loggingOption = option;
105
+ };
106
+ }
@@ -0,0 +1,160 @@
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(
50
+ value: unknown,
51
+ ): value is IGLViewProps['onContextCreate'] {
52
+ return typeof value === 'function';
53
+ }
54
+
55
+ // Adapters hand over the props they hold, a missing callback is a view nobody listens to
56
+ function toViewProps(props: object): IGLViewProps {
57
+ const onContextCreate: unknown = Reflect.get(props, 'onContextCreate');
58
+ return {
59
+ ...props,
60
+ onContextCreate: isContextCallback(onContextCreate)
61
+ ? onContextCreate
62
+ : () => undefined,
63
+ };
64
+ }
65
+
66
+ function nativeTagOf(camera: IGLNodeOrTag): number {
67
+ const tag = isSymbioteNode(camera) ? getNativeTag(camera) : camera;
68
+ if (typeof tag !== 'number')
69
+ throw new Error('The camera view has no native tag yet');
70
+ return tag;
71
+ }
72
+
73
+ /** One per mounted view, `getNode` is kept for the shape the adapters share */
74
+ export function createGLView(_getNode: IGetNode): IGLView {
75
+ let exglCtxId: number | undefined;
76
+ let latestProps: IGLViewProps | undefined;
77
+ let hasWorkletSupport: boolean | undefined;
78
+
79
+ const onSurfaceCreate = (event: ISymbioteEvent): void => {
80
+ if (!isSurfaceCreated(event.nativeEvent)) return;
81
+ exglCtxId = event.nativeEvent.exglCtxId;
82
+ latestProps?.onContextCreate(getGl(exglCtxId));
83
+ };
84
+
85
+ function warnOnWorkletChange(enabled: boolean): void {
86
+ if (hasWorkletSupport !== undefined && hasWorkletSupport !== enabled) {
87
+ console.warn(
88
+ 'Updating prop enableExperimentalWorkletSupport is not supported',
89
+ );
90
+ }
91
+ hasWorkletSupport = enabled;
92
+ }
93
+
94
+ const handle: IGLViewHandle = {
95
+ get exglCtxId() {
96
+ return exglCtxId;
97
+ },
98
+ async createCameraTextureAsync(camera) {
99
+ if (!glNativeModule.createCameraTextureAsync) {
100
+ throw new UnavailabilityError(
101
+ GL_PACKAGE_NAME,
102
+ 'createCameraTextureAsync',
103
+ );
104
+ }
105
+ if (!exglCtxId) throw new Error("GLView's surface is not created yet!");
106
+ const { exglObjId } = await glNativeModule.createCameraTextureAsync(
107
+ exglCtxId,
108
+ nativeTagOf(camera),
109
+ );
110
+ const texture: WebGLTexture = { id: exglObjId };
111
+ return texture;
112
+ },
113
+ async destroyObjectAsync(glObject: IGLObject) {
114
+ if (!glNativeModule.destroyObjectAsync) {
115
+ throw new UnavailabilityError(GL_PACKAGE_NAME, 'destroyObjectAsync');
116
+ }
117
+ return glNativeModule.destroyObjectAsync(glObject.id);
118
+ },
119
+ takeSnapshotAsync: (options?: IGLSnapshotOptions) =>
120
+ takeSnapshotAsync(exglCtxId, options),
121
+ };
122
+
123
+ return {
124
+ handle,
125
+ dispose: () => {
126
+ if (exglCtxId) unregisterGLContext(exglCtxId);
127
+ },
128
+ render: props => {
129
+ if (!ensureGLViewRegistered()) {
130
+ if (isDevBuild()) console.warn("'GLView' is not available.");
131
+ return null;
132
+ }
133
+ latestProps = toViewProps(props);
134
+ const {
135
+ onContextCreate,
136
+ msaaSamples = DEFAULT_MSAA_SAMPLES,
137
+ enableExperimentalWorkletSupport = false,
138
+ ...viewProps
139
+ } = latestProps;
140
+ warnOnWorkletChange(enableExperimentalWorkletSupport);
141
+ return el('view', viewProps, [
142
+ el(glViewName(), {
143
+ style: {
144
+ flex: 1,
145
+ ...Platform.select({
146
+ ios: { backgroundColor: 'transparent' },
147
+ default: {},
148
+ }),
149
+ },
150
+ onSurfaceCreate,
151
+ enableExperimentalWorkletSupport,
152
+ msaaSamples: Platform.select({
153
+ ios: msaaSamples,
154
+ default: undefined,
155
+ }),
156
+ }),
157
+ ]);
158
+ },
159
+ };
160
+ }
@@ -0,0 +1,26 @@
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 {
11
+ createGLView,
12
+ ensureGLViewRegistered,
13
+ glViewName,
14
+ type IGLView,
15
+ } from './gl-view';
16
+ export { GLLoggingOption } from './types';
17
+ export type {
18
+ IExpoWebGLRenderingContext,
19
+ IGLNodeOrTag,
20
+ IGLObject,
21
+ IGLSnapshot,
22
+ IGLSnapshotOptions,
23
+ IGLSurfaceCreateEvent,
24
+ IGLViewHandle,
25
+ IGLViewProps,
26
+ } from './types';
@@ -0,0 +1,22 @@
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(
9
+ exglCtxId: number,
10
+ options: IGLSnapshotOptions,
11
+ ): Promise<IGLSnapshot>;
12
+ /** Absent where there is no camera texture support */
13
+ createCameraTextureAsync?(
14
+ exglCtxId: number,
15
+ cameraTag: number,
16
+ ): Promise<{ exglObjId: number }>;
17
+ /** Absent where objects cannot be destroyed one by one */
18
+ destroyObjectAsync?(exglObjId: number): Promise<boolean>;
19
+ };
20
+
21
+ export const glNativeModule =
22
+ 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,52 @@
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 = (
10
+ worklet: (contextId: number) => void,
11
+ ) => (contextId: number) => void;
12
+
13
+ function isRunOnUI(value: unknown): value is IRunOnUI {
14
+ return typeof value === 'function';
15
+ }
16
+
17
+ // Reanimated stays optional, and an import of it in a try block in the module that also holds the
18
+ // view breaks with `inlineRequires`, so the manager lives in its own file
19
+ export function createWorkletContextManager(): IWorkletContextManager {
20
+ try {
21
+ // Reanimated has to load before any workletized code is created
22
+ // eslint-disable-next-line @typescript-eslint/no-require-imports -- optional dependency, see above
23
+ const reanimated: unknown = require('react-native-reanimated');
24
+ const runOnUI = Reflect.get(Object(reanimated), 'runOnUI');
25
+ if (!isRunOnUI(runOnUI))
26
+ throw new Error('react-native-reanimated has no runOnUI');
27
+ return {
28
+ getContext: contextId => {
29
+ 'worklet';
30
+ return Reflect.get(
31
+ Object(Reflect.get(globalThis, GL_CONTEXTS_GLOBAL)),
32
+ String(contextId),
33
+ );
34
+ },
35
+ unregister: contextId => {
36
+ runOnUI(id => {
37
+ 'worklet';
38
+ Reflect.deleteProperty(
39
+ Object(Reflect.get(globalThis, GL_CONTEXTS_GLOBAL)),
40
+ String(id),
41
+ );
42
+ })(contextId);
43
+ },
44
+ };
45
+ } catch {
46
+ return {
47
+ getContext: () => {
48
+ throw new Error('Worklet runtime is not available');
49
+ },
50
+ };
51
+ }
52
+ }
@@ -0,0 +1,17 @@
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({
13
+ ref,
14
+ ...props
15
+ }: IGLViewReactProps): ReactElement | null {
16
+ return useNativeViewController(ref, createGLView, props);
17
+ }
@@ -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,8 @@
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>(
7
+ createGLView,
8
+ );
@@ -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';