@hxa-rn/react-native-gcanvas 6.0.25 → 6.0.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 (86) hide show
  1. package/CHANGELOG.md +2 -2
  2. package/LICENSE +203 -21
  3. package/README.md +5 -5
  4. package/harmony/gcanvas/LICENSE +203 -21
  5. package/harmony/gcanvas/NOTICE +32 -32
  6. package/harmony/gcanvas/OAT.xml +37 -37
  7. package/harmony/gcanvas/build-profile.json5 +16 -16
  8. package/harmony/gcanvas/hvigorfile.ts +1 -1
  9. package/harmony/gcanvas/index.ets +1 -1
  10. package/harmony/gcanvas/oh-package.json5 +14 -14
  11. package/harmony/gcanvas/src/main/cpp/CMakeLists.txt +126 -126
  12. package/harmony/gcanvas/src/main/cpp/GCanvasNapi.cpp +485 -485
  13. package/harmony/gcanvas/src/main/cpp/GCanvasXComponent.cpp +106 -106
  14. package/harmony/gcanvas/src/main/cpp/GcanvasPackage.h +24 -24
  15. package/harmony/gcanvas/src/main/ets/GCanvasModuleTurboModule.ts +448 -448
  16. package/harmony/gcanvas/src/main/ets/GCanvasNativeEngine.ts +206 -206
  17. package/harmony/gcanvas/src/main/ets/components/GCanvasView.ets +64 -64
  18. package/harmony/gcanvas/src/main/module.json5 +21 -21
  19. package/harmony/gcanvas/src/main/resources/base/element/string.json +12 -12
  20. package/harmony/gcanvas/src/main/resources/en_US/element/string.json +8 -8
  21. package/harmony/gcanvas/src/main/resources/zh_CN/element/string.json +12 -12
  22. package/harmony/gcanvas.har +0 -0
  23. package/package.json +4 -4
  24. package/packages/gcanvas/.babelrc +4 -4
  25. package/packages/gcanvas/.eslintrc.js +133 -133
  26. package/packages/gcanvas/package.json +19 -19
  27. package/packages/gcanvas/rollup.config.js +13 -13
  28. package/packages/gcanvas/src/bridge/react-native.js +286 -286
  29. package/packages/gcanvas/src/bridge/weex.js +210 -210
  30. package/packages/gcanvas/src/context/2d/FillStyleLinearGradient.js +16 -16
  31. package/packages/gcanvas/src/context/2d/FillStylePattern.js +7 -7
  32. package/packages/gcanvas/src/context/2d/FillStyleRadialGradient.js +16 -16
  33. package/packages/gcanvas/src/context/2d/OffScreenRenderingContext.js +48 -48
  34. package/packages/gcanvas/src/context/2d/RenderingContext.js +787 -787
  35. package/packages/gcanvas/src/context/webgl/ActiveInfo.js +10 -10
  36. package/packages/gcanvas/src/context/webgl/Buffer.js +20 -20
  37. package/packages/gcanvas/src/context/webgl/Framebuffer.js +20 -20
  38. package/packages/gcanvas/src/context/webgl/GLenum.js +300 -300
  39. package/packages/gcanvas/src/context/webgl/GLmethod.js +141 -141
  40. package/packages/gcanvas/src/context/webgl/GLtype.js +22 -22
  41. package/packages/gcanvas/src/context/webgl/Program.js +20 -20
  42. package/packages/gcanvas/src/context/webgl/Renderbuffer.js +20 -20
  43. package/packages/gcanvas/src/context/webgl/RenderingContext.js +1479 -1479
  44. package/packages/gcanvas/src/context/webgl/Shader.js +21 -21
  45. package/packages/gcanvas/src/context/webgl/ShaderPrecisionFormat.js +10 -10
  46. package/packages/gcanvas/src/context/webgl/Texture.js +21 -21
  47. package/packages/gcanvas/src/context/webgl/UniformLocation.js +21 -21
  48. package/packages/gcanvas/src/context/webgl/classUtils.js +2 -2
  49. package/packages/gcanvas/src/env/canvas.js +210 -210
  50. package/packages/gcanvas/src/env/image.js +76 -76
  51. package/packages/gcanvas/src/env/off-screen-canvas.js +74 -74
  52. package/packages/gcanvas/src/index.js +56 -56
  53. package/src/components/CanvasView.js +20 -20
  54. package/src/components/GCanvasComponent.js +408 -408
  55. package/src/index.js +69 -69
  56. package/src/packages/gcanvas/.babelrc +4 -4
  57. package/src/packages/gcanvas/.eslintrc.js +133 -133
  58. package/src/packages/gcanvas/package.json +19 -19
  59. package/src/packages/gcanvas/rollup.config.js +13 -13
  60. package/src/packages/gcanvas/src/bridge/react-native.js +288 -288
  61. package/src/packages/gcanvas/src/bridge/weex.js +210 -210
  62. package/src/packages/gcanvas/src/context/2d/FillStyleLinearGradient.js +16 -16
  63. package/src/packages/gcanvas/src/context/2d/FillStylePattern.js +7 -7
  64. package/src/packages/gcanvas/src/context/2d/FillStyleRadialGradient.js +16 -16
  65. package/src/packages/gcanvas/src/context/2d/OffScreenRenderingContext.js +48 -48
  66. package/src/packages/gcanvas/src/context/2d/RenderingContext.js +801 -801
  67. package/src/packages/gcanvas/src/context/webgl/ActiveInfo.js +10 -10
  68. package/src/packages/gcanvas/src/context/webgl/Buffer.js +20 -20
  69. package/src/packages/gcanvas/src/context/webgl/Framebuffer.js +20 -20
  70. package/src/packages/gcanvas/src/context/webgl/GLenum.js +300 -300
  71. package/src/packages/gcanvas/src/context/webgl/GLmethod.js +141 -141
  72. package/src/packages/gcanvas/src/context/webgl/GLtype.js +22 -22
  73. package/src/packages/gcanvas/src/context/webgl/Program.js +20 -20
  74. package/src/packages/gcanvas/src/context/webgl/Renderbuffer.js +20 -20
  75. package/src/packages/gcanvas/src/context/webgl/RenderingContext.js +1479 -1479
  76. package/src/packages/gcanvas/src/context/webgl/Shader.js +21 -21
  77. package/src/packages/gcanvas/src/context/webgl/ShaderPrecisionFormat.js +10 -10
  78. package/src/packages/gcanvas/src/context/webgl/Texture.js +21 -21
  79. package/src/packages/gcanvas/src/context/webgl/UniformLocation.js +21 -21
  80. package/src/packages/gcanvas/src/context/webgl/classUtils.js +2 -2
  81. package/src/packages/gcanvas/src/env/canvas.js +210 -210
  82. package/src/packages/gcanvas/src/env/image.js +76 -76
  83. package/src/packages/gcanvas/src/env/off-screen-canvas.js +74 -74
  84. package/src/packages/gcanvas/src/index.js +56 -56
  85. package/src/specs/v1/GCanvasViewNativeComponent.ts +42 -42
  86. package/src/specs/v1/NativeGCanvasModule.ts +140 -140
@@ -1,286 +1,286 @@
1
- import GLmethod from '../context/webgl/GLmethod';
2
-
3
- const isReactNativeIOS = () => {
4
- return GBridge.Platform.OS === 'ios';
5
- };
6
-
7
- const isReactNativeAndroid = () => {
8
- return GBridge.Platform.OS === 'android';
9
- };
10
-
11
- const isReactNativeHarmony = () => {
12
- return GBridge.Platform.OS === 'harmony';
13
- };
14
-
15
- let isDebugging = false;
16
-
17
- let isComboDisabled = false;
18
-
19
- const logCommand = (function() {
20
- const methodQuery = [];
21
- Object.keys(GLmethod).forEach(key => {
22
- methodQuery[GLmethod[key]] = key;
23
- });
24
- const queryMethod = (id) => {
25
- return methodQuery[parseInt(id)] || 'NotFoundMethod';
26
- };
27
- const logCommand = (id, cmds) => {
28
- const mId = cmds.split(',')[0];
29
- const mName = queryMethod(mId);
30
- console.log(`=== callNative - componentId:${id}; method: ${mName}; cmds: ${cmds}`);
31
- };
32
- return logCommand;
33
- })();
34
-
35
- function joinArray(arr, sep) {
36
- let res = '';
37
- for (let i = 0; i < arr.length; i++) {
38
- if (i !== 0) {
39
- res += sep;
40
- }
41
- res += arr[i];
42
- }
43
- return res;
44
- }
45
-
46
- const commandsCache = {};
47
- const viewport2commands = {};
48
-
49
- const GBridge = {
50
-
51
- GCanvasModule: null,
52
-
53
- Platform: null,
54
-
55
- callEnable: (ref, configArray) => {
56
- commandsCache[ref] = [];
57
-
58
- return GBridge.GCanvasModule.enable({
59
- componentId: ref,
60
- config: configArray
61
- });
62
- },
63
-
64
- callEnableDebug: () => {
65
- isDebugging = true;
66
- },
67
-
68
- callEnableDisableCombo: () => {
69
- isComboDisabled = true;
70
- },
71
-
72
- callDisable: (ref) => {
73
- GBridge.GCanvasModule.disable(ref);
74
- },
75
-
76
- callSetContextType: function(componentId, context_type) {
77
- GBridge.GCanvasModule.setContextType(context_type, componentId);
78
- },
79
-
80
- callSetDevicePixelRatio: function(componentId, ratio) {
81
- GBridge.GCanvasModule.setDevicePixelRatio(componentId, ratio);
82
- },
83
-
84
- callResetGlViewport: function(componentId) {
85
- GBridge.GCanvasModule.resetGlViewport(componentId);
86
- },
87
-
88
- callToDataURL: function(componentId, mimeType, quality) {
89
- return GBridge.GCanvasModule.toDataURL(componentId, mimeType, quality);
90
- },
91
-
92
- callReset: function(componentId) {
93
- GBridge.GCanvasModule.resetComponent && GBridge.GCanvasModule.resetComponent(componentId);
94
- },
95
-
96
- callViewport: (componentId, viewportArgs) => {
97
- viewport2commands[componentId] = viewportArgs;
98
- },
99
-
100
- callNative: function(
101
- componentId,
102
- cmdArgs,
103
- isCacheCmd = false,
104
- contextType = 'webgl',
105
- methodType = 'sync',
106
- optionType = 'execWithoutDisplay',
107
- ) {
108
- if (contextType === '2d') {
109
- if (isDebugging) {
110
- console.log('>>> exec commands: ' + cmdArgs);
111
- }
112
-
113
- if (methodType === 'sync') {
114
- /* call native type description
115
- +---------------------------------------------------+
116
- | 32 bit integer |
117
- +---------------------------------------------------+
118
- | 31~30 | 29 | (28~0) |
119
- | ContextType | MethodType | OptionType |
120
- +---------------------------------------------------+
121
- | 1-webgl | 0-async RN | 0-execWithoutDisplay |
122
- | 0-2d | 1-sync RN | 1-execWithDisplay |
123
- +---------------------------------------------------+
124
- */
125
- // MethodType async RN above means call native async and resume JS
126
- //
127
- // MethodType sync RN above means call native untill native return then resume JS:
128
- // on Android, will waitUtilTimeout() cmd exec indirectly by QueueProc(), then resume JS
129
- // on iOS, if with ContextType 2d and OptionType execWithDisplay, will waitUtilTimeout() cmd exec indirectly
130
- // by drawInRect(), then resume JS, but actually, since cause low JS FPS as described in getImageData()
131
- // of `context/2d/RenderingContext.js`, seems `2d + sync RN + execWithDisplay` situation will not happen;
132
- // if with ContextType webgl, ref to below
133
- //
134
- // OptionType execWithDisplay in 2d above means:
135
- // on Android, indirectly exec cmd to generate new graphics then eglSwapBuffers() to display new graphics
136
- // on screen by QueueProc()
137
- // on iOS, setNeedsDisplay() to display old graphics on screen, and thus will impliedly invoke drawInRect()
138
- // to indirectly exec cmd to generate new graphics (can then be getImageData() even not be displayed)
139
- //
140
- // OptionType execWithDisplay in webgl above means:
141
- // on Android, indirectly exec cmd to generate new graphics then eglSwapBuffers() to display new graphics
142
- // on screen by QueueProc()
143
- // on iOS, directly exec cmd to generate new graphics then setNeedsDisplay() to display new graphics on
144
- // screen, and will not impliedly invoke drawInRect() since `component.glkview.delegate = nil;`
145
- //
146
- // OptionType execWithoutDisplay in 2d or webgl above means:
147
- // on Android, just indirectly exec cmd by QueueProc() to generate new graphics
148
- // on iOS, just directly exec cmd to generate new graphics
149
-
150
- let type = optionType === 'execWithDisplay' ? 0x20000001 : 0x20000000;
151
- // extendCallNative() is a sync RN method, if want get exec cmd result, must use it
152
- const result = GBridge.GCanvasModule.extendCallNative({
153
- contextId: componentId,
154
- args: cmdArgs,
155
- type,
156
- });
157
-
158
- const res = result && result.result;
159
-
160
- if (isDebugging) {
161
- console.log('>>> result: ' + res);
162
- }
163
-
164
- return res;
165
- } else {
166
- let type = optionType === 'execWithDisplay' ? 0x00000001 : 0x00000000;
167
- // render() is an async RN method
168
- GBridge.GCanvasModule.render(componentId, cmdArgs, type);
169
- }
170
- } else { // 'webgl'
171
- if (isDebugging) {
172
- logCommand(componentId, cmdArgs);
173
- }
174
-
175
- if (cmdArgs) {
176
- commandsCache[componentId].push(cmdArgs);
177
- }
178
-
179
- if (!isCacheCmd || isComboDisabled) {
180
- let commands = joinArray(commandsCache[componentId], ';');
181
- commandsCache[componentId] = [];
182
-
183
- if (isReactNativeIOS()) {
184
- // if no hack additional viewportArgs beyond the commands, glViewport() in C++ will has no
185
- // effect in:
186
- // 1. `webgl_demo/cube.js` which invoke gl.viewport() every 16ms , and _disableAutoSwap is true
187
- // 2. https://github.com/flyskywhy/snakeRN which invoke gl.viewport() twice within pixi.js
188
- // damn iOS
189
- const viewportArgs = viewport2commands[componentId];
190
- if (viewportArgs) {
191
- commands = viewportArgs + ';' + commands;
192
- }
193
- }
194
-
195
- if (isDebugging) {
196
- console.log('>>> exec commands: ' + commands);
197
- }
198
-
199
- if (methodType === 'sync') {
200
- // most gl ops should be sync, otherwise may cause issue like
201
- // https://stackoverflow.com/questions/9008291/webgls-getattriblocation-oddly-returns-1
202
- let type = optionType === 'execWithDisplay' ? 0x60000001 : 0x60000000;
203
- const result = GBridge.GCanvasModule.extendCallNative({
204
- contextId: componentId,
205
- args: commands,
206
- type,
207
- });
208
-
209
- const res = result && result.result;
210
-
211
- if (isDebugging) {
212
- console.log('>>> result: ' + res);
213
- }
214
-
215
- return res;
216
- } else {
217
- let type = optionType === 'execWithDisplay' ? 0x40000001 : 0x40000000;
218
- GBridge.GCanvasModule.render(componentId, commands, type);
219
- }
220
- }
221
- }
222
- },
223
-
224
-
225
- texImage2D(componentId, ...args) {
226
- if (isDebugging) {
227
- console.log('>>> texImage2D: ' + componentId, ...args);
228
- }
229
-
230
- if (isReactNativeAndroid() || isReactNativeHarmony()) {
231
- const [target, level, internalformat, format, type, image] = args;
232
- GBridge.GCanvasModule.texImage2D(componentId, target, level, internalformat, format, type, image.src);
233
- }
234
- },
235
-
236
- texSubImage2D(componentId, ...args) {
237
- if (isDebugging) {
238
- console.log('>>> texSubImage2D: ' + componentId, ...args);
239
- }
240
-
241
- if (isReactNativeAndroid() || isReactNativeHarmony()) {
242
- const [target, level, xoffset, yoffset, format, type, image] = args;
243
- GBridge.GCanvasModule.texSubImage2D(componentId, target, level, xoffset, yoffset, format, type, image.src);
244
- }
245
- },
246
-
247
- drawCanvas2Canvas(map) {
248
- if (isDebugging) {
249
- console.log('>>> drawCanvas2Canvas: ', map);
250
- }
251
-
252
- GBridge.GCanvasModule.drawCanvas2Canvas(map);
253
- },
254
-
255
- bindImageTexture(componentId, src, imageId) {
256
- if (isDebugging) {
257
- console.log('>>> bindImageTexture: ' + componentId, src, imageId);
258
- }
259
-
260
- // TurboModule 异步方法返回 Promise(原旧架构 NativeModules 为回调)
261
- const ret = GBridge.GCanvasModule.bindImageTexture({src: src, imageId: imageId}, componentId);
262
- if (ret && typeof ret.then === 'function') {
263
- ret.catch(() => {});
264
- }
265
- },
266
-
267
- preloadImage([url, id], callback) {
268
- const p = GBridge.GCanvasModule.preLoadImage({url: url, id: id});
269
- if (p && typeof p.then === 'function') {
270
- p.then((image) => {
271
- image.url = url;
272
- image.id = id;
273
- callback(image);
274
- }).catch(() => {
275
- callback({error: 'preLoadImage failed'});
276
- });
277
- } else if (p) {
278
- // 兼容:旧架构回调式返回
279
- p.url = url;
280
- p.id = id;
281
- callback(p);
282
- }
283
- }
284
- };
285
-
286
- export default GBridge;
1
+ import GLmethod from '../context/webgl/GLmethod';
2
+
3
+ const isReactNativeIOS = () => {
4
+ return GBridge.Platform.OS === 'ios';
5
+ };
6
+
7
+ const isReactNativeAndroid = () => {
8
+ return GBridge.Platform.OS === 'android';
9
+ };
10
+
11
+ const isReactNativeHarmony = () => {
12
+ return GBridge.Platform.OS === 'harmony';
13
+ };
14
+
15
+ let isDebugging = false;
16
+
17
+ let isComboDisabled = false;
18
+
19
+ const logCommand = (function() {
20
+ const methodQuery = [];
21
+ Object.keys(GLmethod).forEach(key => {
22
+ methodQuery[GLmethod[key]] = key;
23
+ });
24
+ const queryMethod = (id) => {
25
+ return methodQuery[parseInt(id)] || 'NotFoundMethod';
26
+ };
27
+ const logCommand = (id, cmds) => {
28
+ const mId = cmds.split(',')[0];
29
+ const mName = queryMethod(mId);
30
+ console.log(`=== callNative - componentId:${id}; method: ${mName}; cmds: ${cmds}`);
31
+ };
32
+ return logCommand;
33
+ })();
34
+
35
+ function joinArray(arr, sep) {
36
+ let res = '';
37
+ for (let i = 0; i < arr.length; i++) {
38
+ if (i !== 0) {
39
+ res += sep;
40
+ }
41
+ res += arr[i];
42
+ }
43
+ return res;
44
+ }
45
+
46
+ const commandsCache = {};
47
+ const viewport2commands = {};
48
+
49
+ const GBridge = {
50
+
51
+ GCanvasModule: null,
52
+
53
+ Platform: null,
54
+
55
+ callEnable: (ref, configArray) => {
56
+ commandsCache[ref] = [];
57
+
58
+ return GBridge.GCanvasModule.enable({
59
+ componentId: ref,
60
+ config: configArray
61
+ });
62
+ },
63
+
64
+ callEnableDebug: () => {
65
+ isDebugging = true;
66
+ },
67
+
68
+ callEnableDisableCombo: () => {
69
+ isComboDisabled = true;
70
+ },
71
+
72
+ callDisable: (ref) => {
73
+ GBridge.GCanvasModule.disable(ref);
74
+ },
75
+
76
+ callSetContextType: function(componentId, context_type) {
77
+ GBridge.GCanvasModule.setContextType(context_type, componentId);
78
+ },
79
+
80
+ callSetDevicePixelRatio: function(componentId, ratio) {
81
+ GBridge.GCanvasModule.setDevicePixelRatio(componentId, ratio);
82
+ },
83
+
84
+ callResetGlViewport: function(componentId) {
85
+ GBridge.GCanvasModule.resetGlViewport(componentId);
86
+ },
87
+
88
+ callToDataURL: function(componentId, mimeType, quality) {
89
+ return GBridge.GCanvasModule.toDataURL(componentId, mimeType, quality);
90
+ },
91
+
92
+ callReset: function(componentId) {
93
+ GBridge.GCanvasModule.resetComponent && GBridge.GCanvasModule.resetComponent(componentId);
94
+ },
95
+
96
+ callViewport: (componentId, viewportArgs) => {
97
+ viewport2commands[componentId] = viewportArgs;
98
+ },
99
+
100
+ callNative: function(
101
+ componentId,
102
+ cmdArgs,
103
+ isCacheCmd = false,
104
+ contextType = 'webgl',
105
+ methodType = 'sync',
106
+ optionType = 'execWithoutDisplay',
107
+ ) {
108
+ if (contextType === '2d') {
109
+ if (isDebugging) {
110
+ console.log('>>> exec commands: ' + cmdArgs);
111
+ }
112
+
113
+ if (methodType === 'sync') {
114
+ /* call native type description
115
+ +---------------------------------------------------+
116
+ | 32 bit integer |
117
+ +---------------------------------------------------+
118
+ | 31~30 | 29 | (28~0) |
119
+ | ContextType | MethodType | OptionType |
120
+ +---------------------------------------------------+
121
+ | 1-webgl | 0-async RN | 0-execWithoutDisplay |
122
+ | 0-2d | 1-sync RN | 1-execWithDisplay |
123
+ +---------------------------------------------------+
124
+ */
125
+ // MethodType async RN above means call native async and resume JS
126
+ //
127
+ // MethodType sync RN above means call native untill native return then resume JS:
128
+ // on Android, will waitUtilTimeout() cmd exec indirectly by QueueProc(), then resume JS
129
+ // on iOS, if with ContextType 2d and OptionType execWithDisplay, will waitUtilTimeout() cmd exec indirectly
130
+ // by drawInRect(), then resume JS, but actually, since cause low JS FPS as described in getImageData()
131
+ // of `context/2d/RenderingContext.js`, seems `2d + sync RN + execWithDisplay` situation will not happen;
132
+ // if with ContextType webgl, ref to below
133
+ //
134
+ // OptionType execWithDisplay in 2d above means:
135
+ // on Android, indirectly exec cmd to generate new graphics then eglSwapBuffers() to display new graphics
136
+ // on screen by QueueProc()
137
+ // on iOS, setNeedsDisplay() to display old graphics on screen, and thus will impliedly invoke drawInRect()
138
+ // to indirectly exec cmd to generate new graphics (can then be getImageData() even not be displayed)
139
+ //
140
+ // OptionType execWithDisplay in webgl above means:
141
+ // on Android, indirectly exec cmd to generate new graphics then eglSwapBuffers() to display new graphics
142
+ // on screen by QueueProc()
143
+ // on iOS, directly exec cmd to generate new graphics then setNeedsDisplay() to display new graphics on
144
+ // screen, and will not impliedly invoke drawInRect() since `component.glkview.delegate = nil;`
145
+ //
146
+ // OptionType execWithoutDisplay in 2d or webgl above means:
147
+ // on Android, just indirectly exec cmd by QueueProc() to generate new graphics
148
+ // on iOS, just directly exec cmd to generate new graphics
149
+
150
+ let type = optionType === 'execWithDisplay' ? 0x20000001 : 0x20000000;
151
+ // extendCallNative() is a sync RN method, if want get exec cmd result, must use it
152
+ const result = GBridge.GCanvasModule.extendCallNative({
153
+ contextId: componentId,
154
+ args: cmdArgs,
155
+ type,
156
+ });
157
+
158
+ const res = result && result.result;
159
+
160
+ if (isDebugging) {
161
+ console.log('>>> result: ' + res);
162
+ }
163
+
164
+ return res;
165
+ } else {
166
+ let type = optionType === 'execWithDisplay' ? 0x00000001 : 0x00000000;
167
+ // render() is an async RN method
168
+ GBridge.GCanvasModule.render(componentId, cmdArgs, type);
169
+ }
170
+ } else { // 'webgl'
171
+ if (isDebugging) {
172
+ logCommand(componentId, cmdArgs);
173
+ }
174
+
175
+ if (cmdArgs) {
176
+ commandsCache[componentId].push(cmdArgs);
177
+ }
178
+
179
+ if (!isCacheCmd || isComboDisabled) {
180
+ let commands = joinArray(commandsCache[componentId], ';');
181
+ commandsCache[componentId] = [];
182
+
183
+ if (isReactNativeIOS()) {
184
+ // if no hack additional viewportArgs beyond the commands, glViewport() in C++ will has no
185
+ // effect in:
186
+ // 1. `webgl_demo/cube.js` which invoke gl.viewport() every 16ms , and _disableAutoSwap is true
187
+ // 2. https://github.com/flyskywhy/snakeRN which invoke gl.viewport() twice within pixi.js
188
+ // damn iOS
189
+ const viewportArgs = viewport2commands[componentId];
190
+ if (viewportArgs) {
191
+ commands = viewportArgs + ';' + commands;
192
+ }
193
+ }
194
+
195
+ if (isDebugging) {
196
+ console.log('>>> exec commands: ' + commands);
197
+ }
198
+
199
+ if (methodType === 'sync') {
200
+ // most gl ops should be sync, otherwise may cause issue like
201
+ // https://stackoverflow.com/questions/9008291/webgls-getattriblocation-oddly-returns-1
202
+ let type = optionType === 'execWithDisplay' ? 0x60000001 : 0x60000000;
203
+ const result = GBridge.GCanvasModule.extendCallNative({
204
+ contextId: componentId,
205
+ args: commands,
206
+ type,
207
+ });
208
+
209
+ const res = result && result.result;
210
+
211
+ if (isDebugging) {
212
+ console.log('>>> result: ' + res);
213
+ }
214
+
215
+ return res;
216
+ } else {
217
+ let type = optionType === 'execWithDisplay' ? 0x40000001 : 0x40000000;
218
+ GBridge.GCanvasModule.render(componentId, commands, type);
219
+ }
220
+ }
221
+ }
222
+ },
223
+
224
+
225
+ texImage2D(componentId, ...args) {
226
+ if (isDebugging) {
227
+ console.log('>>> texImage2D: ' + componentId, ...args);
228
+ }
229
+
230
+ if (isReactNativeAndroid() || isReactNativeHarmony()) {
231
+ const [target, level, internalformat, format, type, image] = args;
232
+ GBridge.GCanvasModule.texImage2D(componentId, target, level, internalformat, format, type, image.src);
233
+ }
234
+ },
235
+
236
+ texSubImage2D(componentId, ...args) {
237
+ if (isDebugging) {
238
+ console.log('>>> texSubImage2D: ' + componentId, ...args);
239
+ }
240
+
241
+ if (isReactNativeAndroid() || isReactNativeHarmony()) {
242
+ const [target, level, xoffset, yoffset, format, type, image] = args;
243
+ GBridge.GCanvasModule.texSubImage2D(componentId, target, level, xoffset, yoffset, format, type, image.src);
244
+ }
245
+ },
246
+
247
+ drawCanvas2Canvas(map) {
248
+ if (isDebugging) {
249
+ console.log('>>> drawCanvas2Canvas: ', map);
250
+ }
251
+
252
+ GBridge.GCanvasModule.drawCanvas2Canvas(map);
253
+ },
254
+
255
+ bindImageTexture(componentId, src, imageId) {
256
+ if (isDebugging) {
257
+ console.log('>>> bindImageTexture: ' + componentId, src, imageId);
258
+ }
259
+
260
+ // TurboModule 异步方法返回 Promise(原旧架构 NativeModules 为回调)
261
+ const ret = GBridge.GCanvasModule.bindImageTexture({src: src, imageId: imageId}, componentId);
262
+ if (ret && typeof ret.then === 'function') {
263
+ ret.catch(() => {});
264
+ }
265
+ },
266
+
267
+ preloadImage([url, id], callback) {
268
+ const p = GBridge.GCanvasModule.preLoadImage({url: url, id: id});
269
+ if (p && typeof p.then === 'function') {
270
+ p.then((image) => {
271
+ image.url = url;
272
+ image.id = id;
273
+ callback(image);
274
+ }).catch(() => {
275
+ callback({error: 'preLoadImage failed'});
276
+ });
277
+ } else if (p) {
278
+ // 兼容:旧架构回调式返回
279
+ p.url = url;
280
+ p.id = id;
281
+ callback(p);
282
+ }
283
+ }
284
+ };
285
+
286
+ export default GBridge;