BoostUtils.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* *
  2. *
  3. * Copyright (c) 2019-2021 Highsoft AS
  4. *
  5. * Boost module: stripped-down renderer for higher performance
  6. *
  7. * License: highcharts.com/license
  8. *
  9. * This files contains generic utility functions used by the boost module.
  10. *
  11. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  12. *
  13. * */
  14. 'use strict';
  15. import H from '../../Core/Globals.js';
  16. var win = H.win, doc = H.doc;
  17. import boostableMap from './BoostableMap.js';
  18. import createAndAttachRenderer from './BoostAttach.js';
  19. import U from '../../Core/Utilities.js';
  20. var pick = U.pick;
  21. // This should be a const.
  22. var CHUNK_SIZE = 3000;
  23. /**
  24. * Tolerant max() function.
  25. *
  26. * @private
  27. * @function patientMax
  28. *
  29. * @param {...Array<Array<unknown>>} args
  30. * Max arguments
  31. *
  32. * @return {number}
  33. * Max value
  34. */
  35. function patientMax() {
  36. var args = [];
  37. for (var _i = 0; _i < arguments.length; _i++) {
  38. args[_i] = arguments[_i];
  39. }
  40. var r = -Number.MAX_VALUE;
  41. args.forEach(function (t) {
  42. if (typeof t !== 'undefined' &&
  43. t !== null &&
  44. typeof t.length !== 'undefined') {
  45. // r = r < t.length ? t.length : r;
  46. if (t.length > 0) {
  47. r = t.length;
  48. return true;
  49. }
  50. }
  51. });
  52. return r;
  53. }
  54. /**
  55. * Return true if ths boost.enabled option is true
  56. *
  57. * @private
  58. * @function boostEnabled
  59. *
  60. * @param {Highcharts.Chart} chart
  61. * The chart
  62. *
  63. * @return {boolean}
  64. * True, if boost is enabled.
  65. */
  66. function boostEnabled(chart) {
  67. return pick((chart &&
  68. chart.options &&
  69. chart.options.boost &&
  70. chart.options.boost.enabled), true);
  71. }
  72. /**
  73. * Returns true if we should force boosting the chart
  74. * @private
  75. * @function shouldForceChartSeriesBoosting
  76. *
  77. * @param {Highcharts.Chart} chart
  78. * The chart to check for forcing on
  79. *
  80. * @return {boolean}
  81. * True, if boosting should be forced.
  82. */
  83. function shouldForceChartSeriesBoosting(chart) {
  84. // If there are more than five series currently boosting,
  85. // we should boost the whole chart to avoid running out of webgl contexts.
  86. var sboostCount = 0, canBoostCount = 0, allowBoostForce = pick(chart.options.boost && chart.options.boost.allowForce, true), series;
  87. if (typeof chart.boostForceChartBoost !== 'undefined') {
  88. return chart.boostForceChartBoost;
  89. }
  90. if (chart.series.length > 1) {
  91. for (var i = 0; i < chart.series.length; i++) {
  92. series = chart.series[i];
  93. // Don't count series with boostThreshold set to 0
  94. // See #8950
  95. // Also don't count if the series is hidden.
  96. // See #9046
  97. if (series.options.boostThreshold === 0 ||
  98. series.visible === false) {
  99. continue;
  100. }
  101. // Don't count heatmap series as they are handled differently.
  102. // In the future we should make the heatmap/treemap path compatible
  103. // with forcing. See #9636.
  104. if (series.type === 'heatmap') {
  105. continue;
  106. }
  107. if (boostableMap[series.type]) {
  108. ++canBoostCount;
  109. }
  110. if (patientMax(series.processedXData, series.options.data,
  111. // series.xData,
  112. series.points) >= (series.options.boostThreshold || Number.MAX_VALUE)) {
  113. ++sboostCount;
  114. }
  115. }
  116. }
  117. chart.boostForceChartBoost = allowBoostForce && ((canBoostCount === chart.series.length &&
  118. sboostCount > 0) ||
  119. sboostCount > 5);
  120. return chart.boostForceChartBoost;
  121. }
  122. /* eslint-disable valid-jsdoc */
  123. /**
  124. * Performs the actual render if the renderer is
  125. * attached to the series.
  126. * @private
  127. * @param renderer {OGLRenderer} - the renderer
  128. * @param series {Highcharts.Series} - the series
  129. */
  130. function renderIfNotSeriesBoosting(renderer, series, chart) {
  131. if (renderer &&
  132. series.renderTarget &&
  133. series.canvas &&
  134. !(chart || series.chart).isChartSeriesBoosting()) {
  135. renderer.render(chart || series.chart);
  136. }
  137. }
  138. /**
  139. * @private
  140. */
  141. function allocateIfNotSeriesBoosting(renderer, series) {
  142. if (renderer &&
  143. series.renderTarget &&
  144. series.canvas &&
  145. !series.chart.isChartSeriesBoosting()) {
  146. renderer.allocateBufferForSingleSeries(series);
  147. }
  148. }
  149. /**
  150. * An "async" foreach loop. Uses a setTimeout to keep the loop from blocking the
  151. * UI thread.
  152. *
  153. * @private
  154. *
  155. * @param arr {Array} - the array to loop through
  156. * @param fn {Function} - the callback to call for each item
  157. * @param finalFunc {Function} - the callback to call when done
  158. * @param chunkSize {Number} - the number of iterations per timeout
  159. * @param i {Number} - the current index
  160. * @param noTimeout {Boolean} - set to true to skip timeouts
  161. */
  162. function eachAsync(arr, fn, finalFunc, chunkSize, i, noTimeout) {
  163. i = i || 0;
  164. chunkSize = chunkSize || CHUNK_SIZE;
  165. var threshold = i + chunkSize, proceed = true;
  166. while (proceed && i < threshold && i < arr.length) {
  167. proceed = fn(arr[i], i);
  168. ++i;
  169. }
  170. if (proceed) {
  171. if (i < arr.length) {
  172. if (noTimeout) {
  173. eachAsync(arr, fn, finalFunc, chunkSize, i, noTimeout);
  174. }
  175. else if (win.requestAnimationFrame) {
  176. // If available, do requestAnimationFrame - shaves off a few ms
  177. win.requestAnimationFrame(function () {
  178. eachAsync(arr, fn, finalFunc, chunkSize, i);
  179. });
  180. }
  181. else {
  182. setTimeout(function () {
  183. eachAsync(arr, fn, finalFunc, chunkSize, i);
  184. });
  185. }
  186. }
  187. else if (finalFunc) {
  188. finalFunc();
  189. }
  190. }
  191. }
  192. /**
  193. * Returns true if the current browser supports webgl
  194. *
  195. * @private
  196. * @function hasWebGLSupport
  197. *
  198. * @return {boolean}
  199. */
  200. function hasWebGLSupport() {
  201. var i = 0, canvas, contexts = ['webgl', 'experimental-webgl', 'moz-webgl', 'webkit-3d'], context = false;
  202. if (typeof win.WebGLRenderingContext !== 'undefined') {
  203. canvas = doc.createElement('canvas');
  204. for (; i < contexts.length; i++) {
  205. try {
  206. context = canvas.getContext(contexts[i]);
  207. if (typeof context !== 'undefined' && context !== null) {
  208. return true;
  209. }
  210. }
  211. catch (e) {
  212. // silent error
  213. }
  214. }
  215. }
  216. return false;
  217. }
  218. /* eslint-disable no-invalid-this */
  219. /**
  220. * Used for treemap|heatmap.drawPoints
  221. *
  222. * @private
  223. * @function pointDrawHandler
  224. *
  225. * @param {Function} proceed
  226. *
  227. * @return {*}
  228. */
  229. function pointDrawHandler(proceed) {
  230. var enabled = true, renderer;
  231. if (this.chart.options && this.chart.options.boost) {
  232. enabled = typeof this.chart.options.boost.enabled === 'undefined' ?
  233. true :
  234. this.chart.options.boost.enabled;
  235. }
  236. if (!enabled || !this.isSeriesBoosting) {
  237. return proceed.call(this);
  238. }
  239. this.chart.isBoosting = true;
  240. // Make sure we have a valid OGL context
  241. renderer = createAndAttachRenderer(this.chart, this);
  242. if (renderer) {
  243. allocateIfNotSeriesBoosting(renderer, this);
  244. renderer.pushSeries(this);
  245. }
  246. renderIfNotSeriesBoosting(renderer, this);
  247. }
  248. /* eslint-enable no-invalid-this, valid-jsdoc */
  249. var funs = {
  250. patientMax: patientMax,
  251. boostEnabled: boostEnabled,
  252. shouldForceChartSeriesBoosting: shouldForceChartSeriesBoosting,
  253. renderIfNotSeriesBoosting: renderIfNotSeriesBoosting,
  254. allocateIfNotSeriesBoosting: allocateIfNotSeriesBoosting,
  255. eachAsync: eachAsync,
  256. hasWebGLSupport: hasWebGLSupport,
  257. pointDrawHandler: pointDrawHandler
  258. };
  259. // This needs to be fixed.
  260. H.hasWebGLSupport = hasWebGLSupport;
  261. export default funs;