BoostUtils.js 7.7 KB

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