bridge-weex.js 7.4 KB

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