bridge-weex.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. const isWeex = typeof WXEnvironment !== 'undefined';
  2. const isWeexIOS = isWeex && /ios/i.test(WXEnvironment.platform);
  3. const isWeexAndroid = isWeex && !isWeexIOS;
  4. import GLmethod from '../context-webgl/GLmethod';
  5. const GCanvasModule =
  6. (typeof weex !== 'undefined' && weex.requireModule) ? (weex.requireModule('gcanvas')) :
  7. (typeof __weex_require__ !== 'undefined') ? (__weex_require__('@weex-module/gcanvas')) : {};
  8. let isDebugging = false;
  9. let isComboDisabled = false;
  10. const logCommand = (function () {
  11. const methodQuery = [];
  12. Object.keys(GLmethod).forEach(key => {
  13. methodQuery[GLmethod[key]] = key;
  14. })
  15. const queryMethod = (id) => {
  16. return methodQuery[parseInt(id)] || 'NotFoundMethod';
  17. }
  18. const logCommand = (id, cmds) => {
  19. const mId = cmds.split(',')[0];
  20. const mName = queryMethod(mId);
  21. console.log(`=== callNative - componentId:${id}; method: ${mName}; cmds: ${cmds}`);
  22. }
  23. return logCommand;
  24. })();
  25. function joinArray(arr, sep) {
  26. let res = '';
  27. for (let i = 0; i < arr.length; i++) {
  28. if (i !== 0) {
  29. res += sep;
  30. }
  31. res += arr[i];
  32. }
  33. return res;
  34. }
  35. const commandsCache = {}
  36. const GBridge = {
  37. callEnable: (ref, configArray) => {
  38. commandsCache[ref] = [];
  39. return GCanvasModule.enable({
  40. componentId: ref,
  41. config: configArray
  42. });
  43. },
  44. callEnableDebug: () => {
  45. isDebugging = true;
  46. },
  47. callEnableDisableCombo: () => {
  48. isComboDisabled = true;
  49. },
  50. callSetContextType: function (componentId, context_type) {
  51. GCanvasModule.setContextType(context_type, componentId);
  52. },
  53. callReset: function(id){
  54. GCanvasModule.resetComponent && canvasModule.resetComponent(componentId);
  55. },
  56. render: isWeexIOS ? function (componentId) {
  57. return GCanvasModule.extendCallNative({
  58. contextId: componentId,
  59. type: 0x60000001
  60. });
  61. } : function (componentId) {
  62. return callGCanvasLinkNative(componentId, 0x60000001, 'render');
  63. },
  64. render2d: isWeexIOS ? function (componentId, commands, callback) {
  65. if (isDebugging) {
  66. console.log('>>> >>> render2d ===');
  67. console.log('>>> commands: ' + commands);
  68. }
  69. GCanvasModule.render([commands, callback?true:false], componentId, callback);
  70. } : function (componentId, commands,callback) {
  71. if (isDebugging) {
  72. console.log('>>> >>> render2d ===');
  73. console.log('>>> commands: ' + commands);
  74. }
  75. callGCanvasLinkNative(componentId, 0x20000001, commands);
  76. if(callback){
  77. callback();
  78. }
  79. },
  80. callExtendCallNative: isWeexIOS ? function (componentId, cmdArgs) {
  81. throw 'should not be here anymore ' + cmdArgs;
  82. } : function (componentId, cmdArgs) {
  83. throw 'should not be here anymore ' + cmdArgs;
  84. },
  85. flushNative: isWeexIOS ? function (componentId) {
  86. const cmdArgs = joinArray(commandsCache[componentId], ';');
  87. commandsCache[componentId] = [];
  88. if (isDebugging) {
  89. console.log('>>> >>> flush native ===');
  90. console.log('>>> commands: ' + cmdArgs);
  91. }
  92. const result = GCanvasModule.extendCallNative({
  93. "contextId": componentId,
  94. "type": 0x60000000,
  95. "args": cmdArgs
  96. });
  97. const res = result && result.result;
  98. if (isDebugging) {
  99. console.log('>>> result: ' + res);
  100. }
  101. return res;
  102. } : function (componentId) {
  103. const cmdArgs = joinArray(commandsCache[componentId], ';');
  104. commandsCache[componentId] = [];
  105. if (isDebugging) {
  106. console.log('>>> >>> flush native ===');
  107. console.log('>>> commands: ' + cmdArgs);
  108. }
  109. const result = callGCanvasLinkNative(componentId, 0x60000000, cmdArgs);
  110. if (isDebugging) {
  111. console.log('>>> result: ' + result);
  112. }
  113. return result;
  114. },
  115. callNative: function (componentId, cmdArgs, cache) {
  116. if (isDebugging) {
  117. logCommand(componentId, cmdArgs);
  118. }
  119. commandsCache[componentId].push(cmdArgs);
  120. if (!cache || isComboDisabled) {
  121. return GBridge.flushNative(componentId);
  122. } else {
  123. return undefined;
  124. }
  125. },
  126. texImage2D(componentId, ...args) {
  127. if (isWeexIOS) {
  128. if (args.length === 6) {
  129. const [target, level, internalformat, format, type, image] = args;
  130. GBridge.callNative(
  131. componentId,
  132. GLmethod.texImage2D + ',' + 6 + ',' + target + ',' + level + ',' + internalformat + ',' + format + ',' + type + ',' + image.src
  133. )
  134. } else if (args.length === 9) {
  135. const [target, level, internalformat, width, height, border, format, type, image] = args;
  136. GBridge.callNative(
  137. componentId,
  138. GLmethod.texImage2D + ',' + 9 + ',' + target + ',' + level + ',' + internalformat + ',' + width + ',' + height + ',' + border + ',' +
  139. + format + ',' + type + ',' + (image ? image.src : 0)
  140. )
  141. }
  142. } else if (isWeexAndroid) {
  143. if (args.length === 6) {
  144. const [target, level, internalformat, format, type, image] = args;
  145. GCanvasModule.texImage2D(componentId, target, level, internalformat, format, type, image.src);
  146. } else if (args.length === 9) {
  147. const [target, level, internalformat, width, height, border, format, type, image] = args;
  148. GCanvasModule.texImage2D(componentId, target, level, internalformat, width, height, border, format, type, (image ? image.src : 0));
  149. }
  150. }
  151. },
  152. texSubImage2D(componentId, target, level, xoffset, yoffset, format, type, image) {
  153. if (isWeexIOS) {
  154. if (arguments.length === 8) {
  155. GBridge.callNative(
  156. componentId,
  157. GLmethod.texSubImage2D + ',' + 6 + ',' + target + ',' + level + ',' + xoffset + ',' + yoffset, + ',' + format + ',' + type + ',' + image.src
  158. )
  159. }
  160. } else if (isWeexAndroid) {
  161. GCanvasModule.texSubImage2D(componentId, target, level, xoffset, yoffset, format, type, image.src);
  162. }
  163. },
  164. bindImageTexture(componentId, src, imageId) {
  165. GCanvasModule.bindImageTexture([src, imageId], componentId);
  166. },
  167. perloadImage([url, id], callback) {
  168. GCanvasModule.preLoadImage([url, id], function (image) {
  169. image.url = url;
  170. image.id = id;
  171. callback(image);
  172. });
  173. },
  174. measureText(text, fontStyle, componentId) {
  175. return GCanvasModule.measureText([text, fontStyle], componentId);
  176. },
  177. getImageData (componentId, x, y, w, h, callback) {
  178. GCanvasModule.getImageData([x, y,w,h],componentId,callback);
  179. },
  180. putImageData (componentId, data, x, y, w, h, callback) {
  181. GCanvasModule.putImageData([x, y,w,h,data],componentId,callback);
  182. },
  183. toTempFilePath(componentId, x, y, width, height, destWidth, destHeight, fileType, quality, callback){
  184. GCanvasModule.toTempFilePath([x, y, width,height, destWidth, destHeight, fileType, quality], componentId, callback);
  185. }
  186. }
  187. export default GBridge;