@lightningjs/renderer 2.13.1 → 2.13.3

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 (353) hide show
  1. package/COPYING +1 -0
  2. package/LICENSE +202 -202
  3. package/NOTICE +3 -3
  4. package/README.md +147 -147
  5. package/dist/exports/canvas-shaders.d.ts +10 -0
  6. package/dist/exports/canvas-shaders.js +27 -0
  7. package/dist/exports/canvas-shaders.js.map +1 -0
  8. package/dist/exports/webgl-shaders.d.ts +11 -0
  9. package/dist/exports/webgl-shaders.js +28 -0
  10. package/dist/exports/webgl-shaders.js.map +1 -0
  11. package/dist/src/core/CoreTextureManager.js +6 -0
  12. package/dist/src/core/CoreTextureManager.js.map +1 -1
  13. package/dist/src/core/platforms/Platform.d.ts +37 -0
  14. package/dist/src/{main-api/texture-usage-trackers/TextureUsageTracker.js → core/platforms/Platform.js} +3 -9
  15. package/dist/src/core/platforms/Platform.js.map +1 -0
  16. package/dist/src/core/platforms/web/WebPlatform.d.ts +9 -0
  17. package/dist/src/core/platforms/web/WebPlatform.js +58 -0
  18. package/dist/src/core/platforms/web/WebPlatform.js.map +1 -0
  19. package/dist/src/core/renderers/CoreShaderNode.d.ts +58 -0
  20. package/dist/src/core/renderers/CoreShaderNode.js +104 -0
  21. package/dist/src/core/renderers/CoreShaderNode.js.map +1 -0
  22. package/dist/src/core/renderers/CoreShaderProgram.d.ts +4 -0
  23. package/dist/src/{main-api/ICoreDriver.js → core/renderers/CoreShaderProgram.js} +1 -1
  24. package/dist/src/core/renderers/CoreShaderProgram.js.map +1 -0
  25. package/dist/src/core/renderers/canvas/CanvasRenderer.d.ts +36 -0
  26. package/dist/src/core/renderers/canvas/CanvasRenderer.js +212 -0
  27. package/dist/src/core/renderers/canvas/CanvasRenderer.js.map +1 -0
  28. package/dist/src/core/renderers/canvas/CanvasShaderNode.d.ts +21 -0
  29. package/dist/src/core/renderers/canvas/CanvasShaderNode.js +60 -0
  30. package/dist/src/core/renderers/canvas/CanvasShaderNode.js.map +1 -0
  31. package/dist/src/core/renderers/canvas/CanvasTexture.d.ts +16 -0
  32. package/dist/src/core/renderers/canvas/CanvasTexture.js +124 -0
  33. package/dist/src/core/renderers/canvas/CanvasTexture.js.map +1 -0
  34. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.d.ts +12 -0
  35. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.js +55 -0
  36. package/dist/src/core/renderers/webgl/WebGlCtxRenderTexture.js.map +1 -0
  37. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.d.ts +9 -0
  38. package/dist/src/core/{CoreExtension.js → renderers/webgl/WebGlCtxSubTexture.js} +19 -10
  39. package/dist/src/core/renderers/webgl/WebGlCtxSubTexture.js.map +1 -0
  40. package/dist/src/core/renderers/webgl/WebGlCtxTexture.d.ts +57 -0
  41. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js +227 -0
  42. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js.map +1 -0
  43. package/dist/src/core/renderers/webgl/WebGlRenderOp.d.ts +44 -0
  44. package/dist/src/core/renderers/webgl/WebGlRenderOp.js +118 -0
  45. package/dist/src/core/renderers/webgl/WebGlRenderOp.js.map +1 -0
  46. package/dist/src/core/renderers/webgl/WebGlRenderer.d.ts +135 -0
  47. package/dist/src/core/renderers/webgl/WebGlRenderer.js +571 -0
  48. package/dist/src/core/renderers/webgl/WebGlRenderer.js.map +1 -0
  49. package/dist/src/core/renderers/webgl/WebGlShaderNode.d.ts +222 -0
  50. package/dist/src/core/renderers/webgl/WebGlShaderNode.js +334 -0
  51. package/dist/src/core/renderers/webgl/WebGlShaderNode.js.map +1 -0
  52. package/dist/src/core/renderers/webgl/WebGlShaderProgram.d.ts +35 -0
  53. package/dist/src/core/renderers/webgl/WebGlShaderProgram.js +201 -0
  54. package/dist/src/core/renderers/webgl/WebGlShaderProgram.js.map +1 -0
  55. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +45 -45
  56. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  57. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  58. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  59. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +62 -62
  60. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  61. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +6 -6
  62. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  63. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  64. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  65. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  66. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  67. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  68. package/dist/src/core/renderers/webgl/shaders/effects/HolePunchEffect.js +22 -22
  69. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +28 -28
  70. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +10 -10
  71. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  72. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  73. package/dist/src/core/shaders/canvas/Border.d.ts +9 -0
  74. package/dist/src/core/shaders/canvas/Border.js +57 -0
  75. package/dist/src/core/shaders/canvas/Border.js.map +1 -0
  76. package/dist/src/core/shaders/canvas/HolePunch.d.ts +7 -0
  77. package/dist/src/core/shaders/canvas/HolePunch.js +38 -0
  78. package/dist/src/core/shaders/canvas/HolePunch.js.map +1 -0
  79. package/dist/src/core/shaders/canvas/LinearGradient.d.ts +10 -0
  80. package/dist/src/core/shaders/canvas/LinearGradient.js +46 -0
  81. package/dist/src/core/shaders/canvas/LinearGradient.js.map +1 -0
  82. package/dist/src/core/shaders/canvas/RadialGradient.d.ts +11 -0
  83. package/dist/src/core/shaders/canvas/RadialGradient.js +68 -0
  84. package/dist/src/core/shaders/canvas/RadialGradient.js.map +1 -0
  85. package/dist/src/core/shaders/canvas/Rounded.d.ts +7 -0
  86. package/dist/src/core/shaders/canvas/Rounded.js +33 -0
  87. package/dist/src/core/shaders/canvas/Rounded.js.map +1 -0
  88. package/dist/src/core/shaders/canvas/RoundedWithBorder.d.ts +7 -0
  89. package/dist/src/core/shaders/canvas/RoundedWithBorder.js +41 -0
  90. package/dist/src/core/shaders/canvas/RoundedWithBorder.js.map +1 -0
  91. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.d.ts +8 -0
  92. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js +39 -0
  93. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js.map +1 -0
  94. package/dist/src/core/shaders/canvas/RoundedWithShadow.d.ts +7 -0
  95. package/dist/src/core/shaders/canvas/RoundedWithShadow.js +38 -0
  96. package/dist/src/core/shaders/canvas/RoundedWithShadow.js.map +1 -0
  97. package/dist/src/core/shaders/canvas/Shadow.d.ts +8 -0
  98. package/dist/src/core/shaders/canvas/Shadow.js +31 -0
  99. package/dist/src/core/shaders/canvas/Shadow.js.map +1 -0
  100. package/dist/src/core/shaders/canvas/utils/render.d.ts +5 -0
  101. package/dist/src/core/shaders/canvas/utils/render.js +84 -0
  102. package/dist/src/core/shaders/canvas/utils/render.js.map +1 -0
  103. package/dist/src/core/shaders/templates/BorderTemplate.d.ts +37 -0
  104. package/dist/src/core/shaders/templates/BorderTemplate.js +73 -0
  105. package/dist/src/core/shaders/templates/BorderTemplate.js.map +1 -0
  106. package/dist/src/core/shaders/templates/HolePunchTemplate.d.ts +46 -0
  107. package/dist/src/core/shaders/templates/HolePunchTemplate.js +35 -0
  108. package/dist/src/core/shaders/templates/HolePunchTemplate.js.map +1 -0
  109. package/dist/src/core/shaders/templates/LinearGradientTemplate.d.ts +23 -0
  110. package/dist/src/core/shaders/templates/LinearGradientTemplate.js +47 -0
  111. package/dist/src/core/shaders/templates/LinearGradientTemplate.js.map +1 -0
  112. package/dist/src/core/shaders/templates/RadialGradientTemplate.d.ts +31 -0
  113. package/dist/src/core/shaders/templates/RadialGradientTemplate.js +49 -0
  114. package/dist/src/core/shaders/templates/RadialGradientTemplate.js.map +1 -0
  115. package/dist/src/core/shaders/templates/RoundedTemplate.d.ts +29 -0
  116. package/dist/src/core/shaders/templates/RoundedTemplate.js +67 -0
  117. package/dist/src/core/shaders/templates/RoundedTemplate.js.map +1 -0
  118. package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.d.ts +7 -0
  119. package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.js +24 -0
  120. package/dist/src/core/shaders/templates/RoundedWithBorderAndShadowTemplate.js.map +1 -0
  121. package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.d.ts +6 -0
  122. package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.js +23 -0
  123. package/dist/src/core/shaders/templates/RoundedWithBorderTemplate.js.map +1 -0
  124. package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.d.ts +6 -0
  125. package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.js +23 -0
  126. package/dist/src/core/shaders/templates/RoundedWithShadowTemplate.js.map +1 -0
  127. package/dist/src/core/shaders/templates/ShadowTemplate.d.ts +34 -0
  128. package/dist/src/core/shaders/templates/ShadowTemplate.js +66 -0
  129. package/dist/src/core/shaders/templates/ShadowTemplate.js.map +1 -0
  130. package/dist/src/core/shaders/templates/shaderUtils.d.ts +5 -0
  131. package/dist/src/core/shaders/templates/shaderUtils.js +41 -0
  132. package/dist/src/core/shaders/templates/shaderUtils.js.map +1 -0
  133. package/dist/src/core/shaders/webgl/Border.d.ts +3 -0
  134. package/dist/src/core/shaders/webgl/Border.js +110 -0
  135. package/dist/src/core/shaders/webgl/Border.js.map +1 -0
  136. package/dist/src/core/shaders/webgl/Default.d.ts +2 -0
  137. package/dist/src/core/shaders/webgl/Default.js +86 -0
  138. package/dist/src/core/shaders/webgl/Default.js.map +1 -0
  139. package/dist/src/core/shaders/webgl/DefaultBatched.d.ts +2 -0
  140. package/dist/src/core/shaders/webgl/DefaultBatched.js +104 -0
  141. package/dist/src/core/shaders/webgl/DefaultBatched.js.map +1 -0
  142. package/dist/src/core/shaders/webgl/HolePunch.d.ts +3 -0
  143. package/dist/src/core/shaders/webgl/HolePunch.js +64 -0
  144. package/dist/src/core/shaders/webgl/HolePunch.js.map +1 -0
  145. package/dist/src/core/shaders/webgl/LinearGradient.d.ts +3 -0
  146. package/dist/src/core/shaders/webgl/LinearGradient.js +75 -0
  147. package/dist/src/core/shaders/webgl/LinearGradient.js.map +1 -0
  148. package/dist/src/core/shaders/webgl/RadialGradient.d.ts +3 -0
  149. package/dist/src/core/shaders/webgl/RadialGradient.js +73 -0
  150. package/dist/src/core/shaders/webgl/RadialGradient.js.map +1 -0
  151. package/dist/src/core/shaders/webgl/Rounded.d.ts +7 -0
  152. package/dist/src/core/shaders/webgl/Rounded.js +86 -0
  153. package/dist/src/core/shaders/webgl/Rounded.js.map +1 -0
  154. package/dist/src/core/shaders/webgl/RoundedWithBorder.d.ts +3 -0
  155. package/dist/src/core/shaders/webgl/RoundedWithBorder.js +129 -0
  156. package/dist/src/core/shaders/webgl/RoundedWithBorder.js.map +1 -0
  157. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.d.ts +3 -0
  158. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +149 -0
  159. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js.map +1 -0
  160. package/dist/src/core/shaders/webgl/RoundedWithShadow.d.ts +3 -0
  161. package/dist/src/core/shaders/webgl/RoundedWithShadow.js +84 -0
  162. package/dist/src/core/shaders/webgl/RoundedWithShadow.js.map +1 -0
  163. package/dist/src/core/shaders/webgl/SdfShader.d.ts +32 -0
  164. package/dist/src/core/shaders/webgl/SdfShader.js +116 -0
  165. package/dist/src/core/shaders/webgl/SdfShader.js.map +1 -0
  166. package/dist/src/core/shaders/webgl/Shadow.d.ts +3 -0
  167. package/dist/src/core/shaders/webgl/Shadow.js +110 -0
  168. package/dist/src/core/shaders/webgl/Shadow.js.map +1 -0
  169. package/dist/src/core/temp.js +77 -0
  170. package/dist/src/core/temp.js.map +1 -0
  171. package/dist/src/core/textures/Texture.d.ts +4 -2
  172. package/dist/src/core/textures/Texture.js +18 -6
  173. package/dist/src/core/textures/Texture.js.map +1 -1
  174. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  175. package/exports/canvas.ts +39 -39
  176. package/exports/index.ts +89 -89
  177. package/exports/inspector.ts +24 -24
  178. package/exports/utils.ts +44 -44
  179. package/exports/webgl.ts +38 -38
  180. package/package.json +1 -2
  181. package/scripts/please-use-pnpm.js +13 -13
  182. package/src/common/CommonTypes.ts +146 -146
  183. package/src/common/EventEmitter.ts +77 -77
  184. package/src/common/IAnimationController.ts +92 -92
  185. package/src/common/IEventEmitter.ts +28 -28
  186. package/src/core/CoreNode.test.ts +202 -202
  187. package/src/core/CoreNode.ts +2455 -2455
  188. package/src/core/CoreShaderManager.ts +292 -292
  189. package/src/core/CoreTextNode.ts +455 -455
  190. package/src/core/CoreTextureManager.ts +604 -597
  191. package/src/core/Stage.ts +743 -743
  192. package/src/core/TextureMemoryManager.ts +395 -395
  193. package/src/core/animations/AnimationManager.ts +38 -38
  194. package/src/core/animations/CoreAnimation.ts +340 -340
  195. package/src/core/animations/CoreAnimationController.ts +157 -157
  196. package/src/core/lib/ContextSpy.ts +41 -41
  197. package/src/core/lib/ImageWorker.ts +280 -280
  198. package/src/core/lib/Matrix3d.ts +244 -244
  199. package/src/core/lib/RenderCoords.ts +86 -86
  200. package/src/core/lib/WebGlContextWrapper.ts +1332 -1332
  201. package/src/core/lib/textureCompression.ts +152 -152
  202. package/src/core/lib/textureSvg.ts +78 -78
  203. package/src/core/lib/utils.ts +384 -384
  204. package/src/core/lib/validateImageBitmap.ts +76 -76
  205. package/src/core/platform.ts +63 -63
  206. package/src/core/renderers/CoreContextTexture.ts +43 -43
  207. package/src/core/renderers/CoreRenderOp.ts +22 -22
  208. package/src/core/renderers/CoreRenderer.ts +115 -115
  209. package/src/core/renderers/CoreShader.ts +41 -41
  210. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +375 -375
  211. package/src/core/renderers/canvas/CanvasCoreTexture.ts +153 -153
  212. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +231 -231
  213. package/src/core/renderers/canvas/internal/ColorUtils.ts +69 -69
  214. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -48
  215. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +86 -86
  216. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +50 -50
  217. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +301 -301
  218. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +125 -125
  219. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +815 -815
  220. package/src/core/renderers/webgl/WebGlCoreShader.ts +362 -362
  221. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  222. package/src/core/renderers/webgl/internal/RendererUtils.ts +155 -155
  223. package/src/core/renderers/webgl/internal/ShaderUtils.ts +143 -143
  224. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  225. package/src/core/renderers/webgl/shaders/DefaultShader.ts +93 -93
  226. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  227. package/src/core/renderers/webgl/shaders/DynamicShader.ts +580 -580
  228. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +167 -167
  229. package/src/core/renderers/webgl/shaders/SdfShader.ts +204 -204
  230. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  231. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +87 -87
  232. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  233. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  234. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  235. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +159 -159
  236. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +127 -127
  237. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +148 -148
  238. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  239. package/src/core/renderers/webgl/shaders/effects/HolePunchEffect.ts +157 -157
  240. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +171 -171
  241. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +168 -168
  242. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +187 -187
  243. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +110 -110
  244. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +196 -196
  245. package/src/core/text-rendering/TextRenderingUtils.ts +36 -36
  246. package/src/core/text-rendering/TextTextureRendererUtils.ts +263 -263
  247. package/src/core/text-rendering/TrFontManager.ts +183 -183
  248. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +176 -176
  249. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  250. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  251. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +171 -171
  252. package/src/core/text-rendering/font-face-types/TrFontFace.ts +187 -187
  253. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +94 -94
  254. package/src/core/text-rendering/font-face-types/utils.ts +39 -39
  255. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +509 -509
  256. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +808 -808
  257. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +853 -853
  258. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  259. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  260. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  261. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  262. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +117 -117
  263. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  264. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  265. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +408 -408
  266. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  267. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +52 -52
  268. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  269. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  270. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  271. package/src/core/text-rendering/renderers/TextRenderer.ts +557 -557
  272. package/src/core/textures/ColorTexture.ts +102 -102
  273. package/src/core/textures/ImageTexture.ts +399 -399
  274. package/src/core/textures/NoiseTexture.ts +104 -104
  275. package/src/core/textures/RenderTexture.ts +85 -85
  276. package/src/core/textures/SubTexture.ts +205 -205
  277. package/src/core/textures/Texture.ts +353 -337
  278. package/src/core/utils.ts +227 -227
  279. package/src/env.d.ts +7 -7
  280. package/src/main-api/DynamicShaderController.ts +104 -104
  281. package/src/main-api/INode.ts +101 -101
  282. package/src/main-api/Inspector.ts +522 -522
  283. package/src/main-api/Renderer.ts +751 -751
  284. package/src/main-api/ShaderController.ts +80 -80
  285. package/src/main-api/utils.ts +45 -45
  286. package/src/utils.ts +248 -248
  287. package/dist/exports/core-api.d.ts +0 -74
  288. package/dist/exports/core-api.js +0 -96
  289. package/dist/exports/core-api.js.map +0 -1
  290. package/dist/exports/main-api.d.ts +0 -30
  291. package/dist/exports/main-api.js +0 -45
  292. package/dist/exports/main-api.js.map +0 -1
  293. package/dist/src/core/CoreExtension.d.ts +0 -12
  294. package/dist/src/core/CoreExtension.js.map +0 -1
  295. package/dist/src/main-api/ICoreDriver.d.ts +0 -24
  296. package/dist/src/main-api/ICoreDriver.js.map +0 -1
  297. package/dist/src/main-api/RendererMain.d.ts +0 -378
  298. package/dist/src/main-api/RendererMain.js +0 -367
  299. package/dist/src/main-api/RendererMain.js.map +0 -1
  300. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.d.ts +0 -9
  301. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js +0 -38
  302. package/dist/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.js.map +0 -1
  303. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.d.ts +0 -56
  304. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js +0 -101
  305. package/dist/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.js.map +0 -1
  306. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.d.ts +0 -32
  307. package/dist/src/main-api/texture-usage-trackers/TextureUsageTracker.js.map +0 -1
  308. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +0 -21
  309. package/dist/src/render-drivers/main/MainCoreDriver.js +0 -115
  310. package/dist/src/render-drivers/main/MainCoreDriver.js.map +0 -1
  311. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +0 -101
  312. package/dist/src/render-drivers/main/MainOnlyNode.js +0 -425
  313. package/dist/src/render-drivers/main/MainOnlyNode.js.map +0 -1
  314. package/dist/src/render-drivers/main/MainOnlyTextNode.d.ts +0 -47
  315. package/dist/src/render-drivers/main/MainOnlyTextNode.js +0 -204
  316. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +0 -1
  317. package/dist/src/render-drivers/threadx/NodeStruct.d.ts +0 -93
  318. package/dist/src/render-drivers/threadx/NodeStruct.js +0 -290
  319. package/dist/src/render-drivers/threadx/NodeStruct.js.map +0 -1
  320. package/dist/src/render-drivers/threadx/SharedNode.d.ts +0 -40
  321. package/dist/src/render-drivers/threadx/SharedNode.js +0 -61
  322. package/dist/src/render-drivers/threadx/SharedNode.js.map +0 -1
  323. package/dist/src/render-drivers/threadx/TextNodeStruct.d.ts +0 -44
  324. package/dist/src/render-drivers/threadx/TextNodeStruct.js +0 -203
  325. package/dist/src/render-drivers/threadx/TextNodeStruct.js.map +0 -1
  326. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +0 -25
  327. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +0 -232
  328. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +0 -1
  329. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.d.ts +0 -24
  330. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js +0 -113
  331. package/dist/src/render-drivers/threadx/ThreadXMainAnimationController.js.map +0 -1
  332. package/dist/src/render-drivers/threadx/ThreadXMainNode.d.ts +0 -46
  333. package/dist/src/render-drivers/threadx/ThreadXMainNode.js +0 -160
  334. package/dist/src/render-drivers/threadx/ThreadXMainNode.js.map +0 -1
  335. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.d.ts +0 -28
  336. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js +0 -55
  337. package/dist/src/render-drivers/threadx/ThreadXMainTextNode.js.map +0 -1
  338. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +0 -70
  339. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js +0 -32
  340. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +0 -1
  341. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.d.ts +0 -19
  342. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js +0 -184
  343. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js.map +0 -1
  344. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.d.ts +0 -27
  345. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js +0 -109
  346. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js.map +0 -1
  347. package/dist/src/render-drivers/threadx/worker/renderer.js +0 -147
  348. package/dist/src/render-drivers/threadx/worker/renderer.js.map +0 -1
  349. package/dist/src/render-drivers/utils.d.ts +0 -12
  350. package/dist/src/render-drivers/utils.js +0 -74
  351. package/dist/src/render-drivers/utils.js.map +0 -1
  352. package/dist/tsconfig.tsbuildinfo +0 -1
  353. /package/dist/src/{render-drivers/threadx/worker/renderer.d.ts → core/temp.d.ts} +0 -0
@@ -1,597 +1,604 @@
1
- /*
2
- * If not stated otherwise in this file or this component's LICENSE file the
3
- * following copyright and licenses apply:
4
- *
5
- * Copyright 2023 Comcast Cable Communications Management, LLC.
6
- *
7
- * Licensed under the Apache License, Version 2.0 (the License);
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- */
19
-
20
- import { ImageWorkerManager } from './lib/ImageWorker.js';
21
- import type { CoreRenderer } from './renderers/CoreRenderer.js';
22
- import { ColorTexture } from './textures/ColorTexture.js';
23
- import { ImageTexture } from './textures/ImageTexture.js';
24
- import { NoiseTexture } from './textures/NoiseTexture.js';
25
- import { SubTexture } from './textures/SubTexture.js';
26
- import { RenderTexture } from './textures/RenderTexture.js';
27
- import { TextureType, type Texture } from './textures/Texture.js';
28
- import { EventEmitter } from '../common/EventEmitter.js';
29
- import { getTimeStamp } from './platform.js';
30
- import type { Stage } from './Stage.js';
31
- import {
32
- validateCreateImageBitmap,
33
- type CreateImageBitmapSupport,
34
- } from './lib/validateImageBitmap.js';
35
-
36
- /**
37
- * Augmentable map of texture class types
38
- *
39
- * @remarks
40
- * This interface can be augmented by other modules/apps to add additional
41
- * texture types. The ones included directly here are the ones that are
42
- * included in the core library.
43
- */
44
- export interface TextureMap {
45
- ColorTexture: typeof ColorTexture;
46
- ImageTexture: typeof ImageTexture;
47
- NoiseTexture: typeof NoiseTexture;
48
- SubTexture: typeof SubTexture;
49
- RenderTexture: typeof RenderTexture;
50
- }
51
-
52
- export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
53
- ? Props
54
- : never;
55
-
56
- /**
57
- * Contains information about the texture manager's internal state
58
- * for debugging purposes.
59
- */
60
- export interface TextureManagerDebugInfo {
61
- keyCacheSize: number;
62
- }
63
-
64
- export interface TextureManagerSettings {
65
- numImageWorkers: number;
66
- createImageBitmapSupport: 'auto' | 'basic' | 'options' | 'full';
67
- }
68
-
69
- export type ResizeModeOptions =
70
- | {
71
- /**
72
- * Specifies that the image should be resized to cover the specified dimensions.
73
- */
74
- type: 'cover';
75
- /**
76
- * The horizontal clipping position
77
- * To clip the left, set clipX to 0. To clip the right, set clipX to 1.
78
- * clipX 0.5 will clip a equal amount from left and right
79
- *
80
- * @defaultValue 0.5
81
- */
82
- clipX?: number;
83
- /**
84
- * The vertical clipping position
85
- * To clip the top, set clipY to 0. To clip the bottom, set clipY to 1.
86
- * clipY 0.5 will clip a equal amount from top and bottom
87
- *
88
- * @defaultValue 0.5
89
- */
90
- clipY?: number;
91
- }
92
- | {
93
- /**
94
- * Specifies that the image should be resized to fit within the specified dimensions.
95
- */
96
- type: 'contain';
97
- };
98
-
99
- /**
100
- * Universal options for all texture types
101
- *
102
- * @remarks
103
- * Texture Options provide a way to specify options that are relevant to the
104
- * texture loading process (including caching) and specifically for how a
105
- * texture is rendered within a specific Node (or set of Nodes).
106
- *
107
- * They are not used in determining the cache key for a texture (except if
108
- * the `cacheKey` option is provided explicitly to oveerride the default
109
- * cache key for the texture instance) nor are they stored/referenced within
110
- * the texture instance itself. Instead, the options are stored/referenced
111
- * within individual Nodes. So a single texture instance can be used in
112
- * multiple Nodes each using a different set of options.
113
- */
114
- export interface TextureOptions {
115
- /**
116
- * Preload the texture immediately even if it's not being rendered to the
117
- * screen.
118
- *
119
- * @remarks
120
- * This allows the texture to be used immediately without any delay when it
121
- * is first needed for rendering. Otherwise the loading process will start
122
- * when the texture is first rendered, which may cause a delay in that texture
123
- * being shown properly.
124
- *
125
- * @defaultValue `false`
126
- */
127
- preload?: boolean;
128
-
129
- /**
130
- * Prevent clean up of the texture when it is no longer being used.
131
- *
132
- * @remarks
133
- * This is useful when you want to keep the texture in memory for later use.
134
- * Regardless of whether the texture is being used or not, it will not be
135
- * cleaned up.
136
- *
137
- * @defaultValue `false`
138
- */
139
- preventCleanup?: boolean;
140
-
141
- /**
142
- * Flip the texture horizontally when rendering
143
- *
144
- * @defaultValue `false`
145
- */
146
- flipX?: boolean;
147
-
148
- /**
149
- * Flip the texture vertically when rendering
150
- *
151
- * @defaultValue `false`
152
- */
153
- flipY?: boolean;
154
-
155
- /**
156
- * You can use resizeMode to determine the clipping automatically from the width
157
- * and height of the source texture. This can be convenient if you are unsure about
158
- * the exact image sizes but want the image to cover a specific area.
159
- *
160
- * The resize modes cover and contain are supported
161
- */
162
- resizeMode?: ResizeModeOptions;
163
- }
164
-
165
- export class CoreTextureManager extends EventEmitter {
166
- /**
167
- * Map of textures by cache key
168
- */
169
- keyCache: Map<string, Texture> = new Map();
170
-
171
- /**
172
- * Map of cache keys by texture
173
- */
174
- inverseKeyCache: WeakMap<Texture, string> = new WeakMap();
175
-
176
- /**
177
- * Map of texture constructors by their type name
178
- */
179
- txConstructors: Partial<TextureMap> = {};
180
-
181
- private downloadTextureSourceQueue: Array<Texture> = [];
182
- private priorityQueue: Array<Texture> = [];
183
- private uploadTextureQueue: Array<Texture> = [];
184
- private initialized = false;
185
- private stage: Stage;
186
- private numImageWorkers: number;
187
-
188
- imageWorkerManager: ImageWorkerManager | null = null;
189
- hasCreateImageBitmap = !!self.createImageBitmap;
190
- imageBitmapSupported = {
191
- basic: false,
192
- options: false,
193
- full: false,
194
- };
195
-
196
- hasWorker = !!self.Worker;
197
- /**
198
- * Renderer that this texture manager is associated with
199
- *
200
- * @remarks
201
- * This MUST be set before the texture manager is used. Otherwise errors
202
- * will occur when using the texture manager.
203
- */
204
- renderer!: CoreRenderer;
205
-
206
- /**
207
- * The current frame time in milliseconds
208
- *
209
- * @remarks
210
- * This is used to populate the `lastRenderableChangeTime` property of
211
- * {@link Texture} instances when their renderable state changes.
212
- *
213
- * Set by stage via `updateFrameTime` method.
214
- */
215
- frameTime = 0;
216
-
217
- constructor(stage: Stage, settings: TextureManagerSettings) {
218
- super();
219
-
220
- const { numImageWorkers, createImageBitmapSupport } = settings;
221
- this.stage = stage;
222
- this.numImageWorkers = numImageWorkers;
223
-
224
- if (createImageBitmapSupport === 'auto') {
225
- validateCreateImageBitmap()
226
- .then((result) => {
227
- this.initialize(result);
228
- })
229
- .catch((e) => {
230
- console.warn(
231
- '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
232
- );
233
-
234
- // initialized without image worker manager and createImageBitmap
235
- this.initialized = true;
236
- this.emit('initialized');
237
- });
238
- } else {
239
- this.initialize({
240
- basic: createImageBitmapSupport === 'basic',
241
- options: createImageBitmapSupport === 'options',
242
- full: createImageBitmapSupport === 'full',
243
- });
244
- }
245
-
246
- this.registerTextureType('ImageTexture', ImageTexture);
247
- this.registerTextureType('ColorTexture', ColorTexture);
248
- this.registerTextureType('NoiseTexture', NoiseTexture);
249
- this.registerTextureType('SubTexture', SubTexture);
250
- this.registerTextureType('RenderTexture', RenderTexture);
251
- }
252
-
253
- registerTextureType<Type extends keyof TextureMap>(
254
- textureType: Type,
255
- textureClass: TextureMap[Type],
256
- ): void {
257
- this.txConstructors[textureType] = textureClass;
258
- }
259
-
260
- private initialize(support: CreateImageBitmapSupport) {
261
- this.hasCreateImageBitmap =
262
- support.basic || support.options || support.full;
263
- this.imageBitmapSupported = support;
264
-
265
- if (!this.hasCreateImageBitmap) {
266
- console.warn(
267
- '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
268
- );
269
- }
270
-
271
- if (
272
- this.hasCreateImageBitmap &&
273
- this.hasWorker &&
274
- this.numImageWorkers > 0
275
- ) {
276
- this.imageWorkerManager = new ImageWorkerManager(
277
- this.numImageWorkers,
278
- support,
279
- );
280
- } else {
281
- console.warn(
282
- '[Lightning] Imageworker is 0 or not supported on this browser. Image loading will be slower.',
283
- );
284
- }
285
-
286
- this.initialized = true;
287
- this.emit('initialized');
288
- }
289
-
290
- /**
291
- * Enqueue a texture for downloading its source image.
292
- */
293
- enqueueDownloadTextureSource(texture: Texture): void {
294
- if (!this.downloadTextureSourceQueue.includes(texture)) {
295
- this.downloadTextureSourceQueue.push(texture);
296
- }
297
- }
298
-
299
- /**
300
- * Enqueue a texture for uploading to the GPU.
301
- *
302
- * @param texture - The texture to upload
303
- */
304
- enqueueUploadTexture(texture: Texture): void {
305
- if (this.uploadTextureQueue.includes(texture) === false) {
306
- this.uploadTextureQueue.push(texture);
307
- }
308
- }
309
-
310
- /**
311
- * Create a texture
312
- *
313
- * @param textureType - The type of texture to create
314
- * @param props - The properties to use for the texture
315
- */
316
- createTexture<Type extends keyof TextureMap>(
317
- textureType: Type,
318
- props: ExtractProps<TextureMap[Type]>,
319
- ): InstanceType<TextureMap[Type]> {
320
- let texture: Texture | undefined;
321
- const TextureClass = this.txConstructors[textureType];
322
- if (!TextureClass) {
323
- throw new Error(`Texture type "${textureType}" is not registered`);
324
- }
325
-
326
- const cacheKey = TextureClass.makeCacheKey(props as any);
327
- if (cacheKey && this.keyCache.has(cacheKey)) {
328
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
329
- texture = this.keyCache.get(cacheKey)!;
330
- } else {
331
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
332
- texture = new TextureClass(this, props as any);
333
-
334
- if (cacheKey) {
335
- this.initTextureToCache(texture, cacheKey);
336
- }
337
- }
338
-
339
- return texture as InstanceType<TextureMap[Type]>;
340
- }
341
-
342
- orphanTexture(texture: Texture): void {
343
- // if it is part of the download or upload queue, remove it
344
- this.removeTextureFromQueue(texture);
345
-
346
- if (texture.type === TextureType.subTexture) {
347
- // ignore subtextures
348
- return;
349
- }
350
-
351
- this.stage.txMemManager.addToOrphanedTextures(texture);
352
- }
353
-
354
- /**
355
- * Override loadTexture to use the batched approach.
356
- *
357
- * @param texture - The texture to load
358
- * @param immediate - Whether to prioritize the texture for immediate loading
359
- */
360
- loadTexture(texture: Texture, priority?: boolean): void {
361
- this.stage.txMemManager.removeFromOrphanedTextures(texture);
362
-
363
- if (texture.type === TextureType.subTexture) {
364
- // ignore subtextures - they get loaded through their parent
365
- return;
366
- }
367
-
368
- // if the texture is already loaded, don't load it again
369
- if (
370
- texture.ctxTexture !== undefined &&
371
- texture.ctxTexture.state === 'loaded'
372
- ) {
373
- texture.setState('loaded');
374
- return;
375
- }
376
-
377
- // if the texture is already being processed, don't load it again
378
- if (
379
- this.downloadTextureSourceQueue.includes(texture) === true ||
380
- this.uploadTextureQueue.includes(texture) === true
381
- ) {
382
- return;
383
- }
384
-
385
- // if the texture is already loading, free it, this can happen if the texture is
386
- // orphaned and then reloaded
387
- if (
388
- texture.ctxTexture !== undefined &&
389
- texture.ctxTexture.state === 'loading'
390
- ) {
391
- // if the texture has texture data, queue it for upload
392
- if (texture.textureData !== null) {
393
- this.enqueueUploadTexture(texture);
394
- }
395
-
396
- // else we will have to re-download the texture
397
- texture.free();
398
- }
399
-
400
- // if we're not initialized, just queue the texture into the priority queue
401
- if (this.initialized === false) {
402
- this.priorityQueue.push(texture);
403
- return;
404
- }
405
-
406
- // these types of textures don't need to be downloaded
407
- // Technically the noise texture shouldn't either, but it's a special case
408
- // and not really used in production so who cares ¯\_(ツ)_/¯
409
- if (
410
- (texture.type === TextureType.color ||
411
- texture.type === TextureType.renderToTexture) &&
412
- texture.state !== 'initial'
413
- ) {
414
- texture.setState('fetched');
415
- this.enqueueUploadTexture(texture);
416
- return;
417
- }
418
-
419
- texture.setState('loading');
420
-
421
- // prioritize the texture for immediate loading
422
- if (priority === true) {
423
- texture
424
- .getTextureData()
425
- .then(() => {
426
- this.uploadTexture(texture);
427
- })
428
- .catch((err) => {
429
- console.error(err);
430
- });
431
- }
432
-
433
- // enqueue the texture for download and upload
434
- this.enqueueDownloadTextureSource(texture);
435
- }
436
-
437
- /**
438
- * Upload a texture to the GPU
439
- *
440
- * @param texture Texture to upload
441
- */
442
- uploadTexture(texture: Texture): void {
443
- if (
444
- this.stage.txMemManager.doNotExceedCriticalThreshold === true &&
445
- this.stage.txMemManager.criticalCleanupRequested === true
446
- ) {
447
- // we're at a critical memory threshold, don't upload textures
448
- this.enqueueUploadTexture(texture);
449
- return;
450
- }
451
-
452
- const coreContext = texture.loadCtxTexture();
453
- if (coreContext !== null && coreContext.state === 'loaded') {
454
- texture.setState('loaded');
455
- return;
456
- }
457
-
458
- coreContext.load();
459
- }
460
-
461
- /**
462
- * Check if a texture is being processed
463
- */
464
- isProcessingTexture(texture: Texture): boolean {
465
- return (
466
- this.downloadTextureSourceQueue.includes(texture) === true ||
467
- this.uploadTextureQueue.includes(texture) === true
468
- );
469
- }
470
-
471
- /**
472
- * Process a limited number of downloads and uploads.
473
- *
474
- * @param maxItems - The maximum number of items to process
475
- */
476
- processSome(maxProcessingTime: number): void {
477
- if (this.initialized === false) {
478
- return;
479
- }
480
-
481
- const startTime = getTimeStamp();
482
-
483
- // Process priority queue
484
- while (
485
- this.priorityQueue.length > 0 &&
486
- getTimeStamp() - startTime < maxProcessingTime
487
- ) {
488
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
489
- const texture = this.priorityQueue.pop()!;
490
- texture.getTextureData().then(() => {
491
- this.uploadTexture(texture);
492
- });
493
- }
494
-
495
- // Process uploads
496
- while (
497
- this.uploadTextureQueue.length > 0 &&
498
- getTimeStamp() - startTime < maxProcessingTime
499
- ) {
500
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
501
- this.uploadTexture(this.uploadTextureQueue.pop()!);
502
- }
503
-
504
- // Process downloads
505
- while (
506
- this.downloadTextureSourceQueue.length > 0 &&
507
- getTimeStamp() - startTime < maxProcessingTime
508
- ) {
509
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
510
- const texture = this.downloadTextureSourceQueue.shift()!;
511
- texture.getTextureData().then(() => {
512
- if (texture.state === 'fetched') {
513
- this.enqueueUploadTexture(texture);
514
- }
515
- });
516
- }
517
- }
518
-
519
- public hasUpdates(): boolean {
520
- return (
521
- this.downloadTextureSourceQueue.length > 0 ||
522
- this.uploadTextureQueue.length > 0
523
- );
524
- }
525
-
526
- /**
527
- * Initialize a texture to the cache
528
- *
529
- * @param texture Texture to cache
530
- * @param cacheKey Cache key for the texture
531
- */
532
- initTextureToCache(texture: Texture, cacheKey: string) {
533
- const { keyCache, inverseKeyCache } = this;
534
- keyCache.set(cacheKey, texture);
535
- inverseKeyCache.set(texture, cacheKey);
536
- }
537
-
538
- /**
539
- * Get a texture from the cache
540
- *
541
- * @param cacheKey
542
- */
543
- getTextureFromCache(cacheKey: string): Texture | undefined {
544
- return this.keyCache.get(cacheKey);
545
- }
546
-
547
- /**
548
- * Remove a texture from the cache
549
- *
550
- * @remarks
551
- * Called by Texture Cleanup when a texture is freed.
552
- *
553
- * @param texture
554
- */
555
- removeTextureFromCache(texture: Texture) {
556
- const { inverseKeyCache, keyCache } = this;
557
- const cacheKey = inverseKeyCache.get(texture);
558
- if (cacheKey) {
559
- keyCache.delete(cacheKey);
560
- }
561
- }
562
-
563
- /**
564
- * Remove texture from the queue's
565
- *
566
- * @param texture - The texture to remove
567
- */
568
- removeTextureFromQueue(texture: Texture): void {
569
- const downloadIndex = this.downloadTextureSourceQueue.indexOf(texture);
570
- if (downloadIndex !== -1) {
571
- this.downloadTextureSourceQueue.splice(downloadIndex, 1);
572
- }
573
-
574
- const uploadIndex = this.uploadTextureQueue.indexOf(texture);
575
- if (uploadIndex !== -1) {
576
- this.uploadTextureQueue.splice(uploadIndex, 1);
577
- }
578
- }
579
-
580
- /**
581
- * Resolve a parent texture from the cache or fallback to the provided texture.
582
- *
583
- * @param texture - The provided texture to resolve.
584
- * @returns The cached or provided texture.
585
- */
586
- resolveParentTexture(texture: ImageTexture): Texture {
587
- if (!texture?.props) {
588
- return texture;
589
- }
590
-
591
- const cacheKey = ImageTexture.makeCacheKey(texture.props);
592
- const cachedTexture = cacheKey
593
- ? this.getTextureFromCache(cacheKey)
594
- : undefined;
595
- return cachedTexture ?? texture;
596
- }
597
- }
1
+ /*
2
+ * If not stated otherwise in this file or this component's LICENSE file the
3
+ * following copyright and licenses apply:
4
+ *
5
+ * Copyright 2023 Comcast Cable Communications Management, LLC.
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the License);
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ import { ImageWorkerManager } from './lib/ImageWorker.js';
21
+ import type { CoreRenderer } from './renderers/CoreRenderer.js';
22
+ import { ColorTexture } from './textures/ColorTexture.js';
23
+ import { ImageTexture } from './textures/ImageTexture.js';
24
+ import { NoiseTexture } from './textures/NoiseTexture.js';
25
+ import { SubTexture } from './textures/SubTexture.js';
26
+ import { RenderTexture } from './textures/RenderTexture.js';
27
+ import { TextureType, type Texture } from './textures/Texture.js';
28
+ import { EventEmitter } from '../common/EventEmitter.js';
29
+ import { getTimeStamp } from './platform.js';
30
+ import type { Stage } from './Stage.js';
31
+ import {
32
+ validateCreateImageBitmap,
33
+ type CreateImageBitmapSupport,
34
+ } from './lib/validateImageBitmap.js';
35
+
36
+ /**
37
+ * Augmentable map of texture class types
38
+ *
39
+ * @remarks
40
+ * This interface can be augmented by other modules/apps to add additional
41
+ * texture types. The ones included directly here are the ones that are
42
+ * included in the core library.
43
+ */
44
+ export interface TextureMap {
45
+ ColorTexture: typeof ColorTexture;
46
+ ImageTexture: typeof ImageTexture;
47
+ NoiseTexture: typeof NoiseTexture;
48
+ SubTexture: typeof SubTexture;
49
+ RenderTexture: typeof RenderTexture;
50
+ }
51
+
52
+ export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
53
+ ? Props
54
+ : never;
55
+
56
+ /**
57
+ * Contains information about the texture manager's internal state
58
+ * for debugging purposes.
59
+ */
60
+ export interface TextureManagerDebugInfo {
61
+ keyCacheSize: number;
62
+ }
63
+
64
+ export interface TextureManagerSettings {
65
+ numImageWorkers: number;
66
+ createImageBitmapSupport: 'auto' | 'basic' | 'options' | 'full';
67
+ }
68
+
69
+ export type ResizeModeOptions =
70
+ | {
71
+ /**
72
+ * Specifies that the image should be resized to cover the specified dimensions.
73
+ */
74
+ type: 'cover';
75
+ /**
76
+ * The horizontal clipping position
77
+ * To clip the left, set clipX to 0. To clip the right, set clipX to 1.
78
+ * clipX 0.5 will clip a equal amount from left and right
79
+ *
80
+ * @defaultValue 0.5
81
+ */
82
+ clipX?: number;
83
+ /**
84
+ * The vertical clipping position
85
+ * To clip the top, set clipY to 0. To clip the bottom, set clipY to 1.
86
+ * clipY 0.5 will clip a equal amount from top and bottom
87
+ *
88
+ * @defaultValue 0.5
89
+ */
90
+ clipY?: number;
91
+ }
92
+ | {
93
+ /**
94
+ * Specifies that the image should be resized to fit within the specified dimensions.
95
+ */
96
+ type: 'contain';
97
+ };
98
+
99
+ /**
100
+ * Universal options for all texture types
101
+ *
102
+ * @remarks
103
+ * Texture Options provide a way to specify options that are relevant to the
104
+ * texture loading process (including caching) and specifically for how a
105
+ * texture is rendered within a specific Node (or set of Nodes).
106
+ *
107
+ * They are not used in determining the cache key for a texture (except if
108
+ * the `cacheKey` option is provided explicitly to oveerride the default
109
+ * cache key for the texture instance) nor are they stored/referenced within
110
+ * the texture instance itself. Instead, the options are stored/referenced
111
+ * within individual Nodes. So a single texture instance can be used in
112
+ * multiple Nodes each using a different set of options.
113
+ */
114
+ export interface TextureOptions {
115
+ /**
116
+ * Preload the texture immediately even if it's not being rendered to the
117
+ * screen.
118
+ *
119
+ * @remarks
120
+ * This allows the texture to be used immediately without any delay when it
121
+ * is first needed for rendering. Otherwise the loading process will start
122
+ * when the texture is first rendered, which may cause a delay in that texture
123
+ * being shown properly.
124
+ *
125
+ * @defaultValue `false`
126
+ */
127
+ preload?: boolean;
128
+
129
+ /**
130
+ * Prevent clean up of the texture when it is no longer being used.
131
+ *
132
+ * @remarks
133
+ * This is useful when you want to keep the texture in memory for later use.
134
+ * Regardless of whether the texture is being used or not, it will not be
135
+ * cleaned up.
136
+ *
137
+ * @defaultValue `false`
138
+ */
139
+ preventCleanup?: boolean;
140
+
141
+ /**
142
+ * Flip the texture horizontally when rendering
143
+ *
144
+ * @defaultValue `false`
145
+ */
146
+ flipX?: boolean;
147
+
148
+ /**
149
+ * Flip the texture vertically when rendering
150
+ *
151
+ * @defaultValue `false`
152
+ */
153
+ flipY?: boolean;
154
+
155
+ /**
156
+ * You can use resizeMode to determine the clipping automatically from the width
157
+ * and height of the source texture. This can be convenient if you are unsure about
158
+ * the exact image sizes but want the image to cover a specific area.
159
+ *
160
+ * The resize modes cover and contain are supported
161
+ */
162
+ resizeMode?: ResizeModeOptions;
163
+ }
164
+
165
+ export class CoreTextureManager extends EventEmitter {
166
+ /**
167
+ * Map of textures by cache key
168
+ */
169
+ keyCache: Map<string, Texture> = new Map();
170
+
171
+ /**
172
+ * Map of cache keys by texture
173
+ */
174
+ inverseKeyCache: WeakMap<Texture, string> = new WeakMap();
175
+
176
+ /**
177
+ * Map of texture constructors by their type name
178
+ */
179
+ txConstructors: Partial<TextureMap> = {};
180
+
181
+ private downloadTextureSourceQueue: Array<Texture> = [];
182
+ private priorityQueue: Array<Texture> = [];
183
+ private uploadTextureQueue: Array<Texture> = [];
184
+ private initialized = false;
185
+ private stage: Stage;
186
+ private numImageWorkers: number;
187
+
188
+ imageWorkerManager: ImageWorkerManager | null = null;
189
+ hasCreateImageBitmap = !!self.createImageBitmap;
190
+ imageBitmapSupported = {
191
+ basic: false,
192
+ options: false,
193
+ full: false,
194
+ };
195
+
196
+ hasWorker = !!self.Worker;
197
+ /**
198
+ * Renderer that this texture manager is associated with
199
+ *
200
+ * @remarks
201
+ * This MUST be set before the texture manager is used. Otherwise errors
202
+ * will occur when using the texture manager.
203
+ */
204
+ renderer!: CoreRenderer;
205
+
206
+ /**
207
+ * The current frame time in milliseconds
208
+ *
209
+ * @remarks
210
+ * This is used to populate the `lastRenderableChangeTime` property of
211
+ * {@link Texture} instances when their renderable state changes.
212
+ *
213
+ * Set by stage via `updateFrameTime` method.
214
+ */
215
+ frameTime = 0;
216
+
217
+ constructor(stage: Stage, settings: TextureManagerSettings) {
218
+ super();
219
+
220
+ const { numImageWorkers, createImageBitmapSupport } = settings;
221
+ this.stage = stage;
222
+ this.numImageWorkers = numImageWorkers;
223
+
224
+ if (createImageBitmapSupport === 'auto') {
225
+ validateCreateImageBitmap()
226
+ .then((result) => {
227
+ this.initialize(result);
228
+ })
229
+ .catch((e) => {
230
+ console.warn(
231
+ '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
232
+ );
233
+
234
+ // initialized without image worker manager and createImageBitmap
235
+ this.initialized = true;
236
+ this.emit('initialized');
237
+ });
238
+ } else {
239
+ this.initialize({
240
+ basic: createImageBitmapSupport === 'basic',
241
+ options: createImageBitmapSupport === 'options',
242
+ full: createImageBitmapSupport === 'full',
243
+ });
244
+ }
245
+
246
+ this.registerTextureType('ImageTexture', ImageTexture);
247
+ this.registerTextureType('ColorTexture', ColorTexture);
248
+ this.registerTextureType('NoiseTexture', NoiseTexture);
249
+ this.registerTextureType('SubTexture', SubTexture);
250
+ this.registerTextureType('RenderTexture', RenderTexture);
251
+ }
252
+
253
+ registerTextureType<Type extends keyof TextureMap>(
254
+ textureType: Type,
255
+ textureClass: TextureMap[Type],
256
+ ): void {
257
+ this.txConstructors[textureType] = textureClass;
258
+ }
259
+
260
+ private initialize(support: CreateImageBitmapSupport) {
261
+ this.hasCreateImageBitmap =
262
+ support.basic || support.options || support.full;
263
+ this.imageBitmapSupported = support;
264
+
265
+ if (!this.hasCreateImageBitmap) {
266
+ console.warn(
267
+ '[Lightning] createImageBitmap is not supported on this browser. ImageTexture will be slower.',
268
+ );
269
+ }
270
+
271
+ if (
272
+ this.hasCreateImageBitmap &&
273
+ this.hasWorker &&
274
+ this.numImageWorkers > 0
275
+ ) {
276
+ this.imageWorkerManager = new ImageWorkerManager(
277
+ this.numImageWorkers,
278
+ support,
279
+ );
280
+ } else {
281
+ console.warn(
282
+ '[Lightning] Imageworker is 0 or not supported on this browser. Image loading will be slower.',
283
+ );
284
+ }
285
+
286
+ this.initialized = true;
287
+ this.emit('initialized');
288
+ }
289
+
290
+ /**
291
+ * Enqueue a texture for downloading its source image.
292
+ */
293
+ enqueueDownloadTextureSource(texture: Texture): void {
294
+ if (!this.downloadTextureSourceQueue.includes(texture)) {
295
+ this.downloadTextureSourceQueue.push(texture);
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Enqueue a texture for uploading to the GPU.
301
+ *
302
+ * @param texture - The texture to upload
303
+ */
304
+ enqueueUploadTexture(texture: Texture): void {
305
+ if (this.uploadTextureQueue.includes(texture) === false) {
306
+ this.uploadTextureQueue.push(texture);
307
+ }
308
+ }
309
+
310
+ /**
311
+ * Create a texture
312
+ *
313
+ * @param textureType - The type of texture to create
314
+ * @param props - The properties to use for the texture
315
+ */
316
+ createTexture<Type extends keyof TextureMap>(
317
+ textureType: Type,
318
+ props: ExtractProps<TextureMap[Type]>,
319
+ ): InstanceType<TextureMap[Type]> {
320
+ let texture: Texture | undefined;
321
+ const TextureClass = this.txConstructors[textureType];
322
+ if (!TextureClass) {
323
+ throw new Error(`Texture type "${textureType}" is not registered`);
324
+ }
325
+
326
+ const cacheKey = TextureClass.makeCacheKey(props as any);
327
+ if (cacheKey && this.keyCache.has(cacheKey)) {
328
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
329
+ texture = this.keyCache.get(cacheKey)!;
330
+ } else {
331
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
332
+ texture = new TextureClass(this, props as any);
333
+
334
+ if (cacheKey) {
335
+ this.initTextureToCache(texture, cacheKey);
336
+ }
337
+ }
338
+
339
+ return texture as InstanceType<TextureMap[Type]>;
340
+ }
341
+
342
+ orphanTexture(texture: Texture): void {
343
+ // if it is part of the download or upload queue, remove it
344
+ this.removeTextureFromQueue(texture);
345
+
346
+ if (texture.type === TextureType.subTexture) {
347
+ // ignore subtextures
348
+ return;
349
+ }
350
+
351
+ this.stage.txMemManager.addToOrphanedTextures(texture);
352
+ }
353
+
354
+ /**
355
+ * Override loadTexture to use the batched approach.
356
+ *
357
+ * @param texture - The texture to load
358
+ * @param immediate - Whether to prioritize the texture for immediate loading
359
+ */
360
+ loadTexture(texture: Texture, priority?: boolean): void {
361
+ this.stage.txMemManager.removeFromOrphanedTextures(texture);
362
+
363
+ if (texture.type === TextureType.subTexture) {
364
+ // ignore subtextures - they get loaded through their parent
365
+ return;
366
+ }
367
+
368
+ // if the texture is already loaded, don't load it again
369
+ if (
370
+ texture.ctxTexture !== undefined &&
371
+ texture.ctxTexture.state === 'loaded'
372
+ ) {
373
+ texture.setState('loaded');
374
+ return;
375
+ }
376
+
377
+ // if the texture is already being processed, don't load it again
378
+ if (
379
+ this.downloadTextureSourceQueue.includes(texture) === true ||
380
+ this.uploadTextureQueue.includes(texture) === true
381
+ ) {
382
+ return;
383
+ }
384
+
385
+ // if the texture is already loading, free it, this can happen if the texture is
386
+ // orphaned and then reloaded
387
+ if (
388
+ texture.ctxTexture !== undefined &&
389
+ texture.ctxTexture.state === 'loading'
390
+ ) {
391
+ // if the texture has texture data, queue it for upload
392
+ if (texture.textureData !== null) {
393
+ this.enqueueUploadTexture(texture);
394
+ return;
395
+ }
396
+
397
+ // else we will have to re-download the texture
398
+ texture.free();
399
+ }
400
+
401
+ // if we're not initialized, just queue the texture into the priority queue
402
+ if (this.initialized === false) {
403
+ this.priorityQueue.push(texture);
404
+ return;
405
+ }
406
+
407
+ // If the texture failed to load, we need to re-download it.
408
+ if (texture.state === 'failed') {
409
+ texture.free();
410
+ texture.freeTextureData();
411
+ }
412
+
413
+ // these types of textures don't need to be downloaded
414
+ // Technically the noise texture shouldn't either, but it's a special case
415
+ // and not really used in production so who cares ¯\_()_/¯
416
+ if (
417
+ (texture.type === TextureType.color ||
418
+ texture.type === TextureType.renderToTexture) &&
419
+ texture.state !== 'initial'
420
+ ) {
421
+ texture.setState('fetched');
422
+ this.enqueueUploadTexture(texture);
423
+ return;
424
+ }
425
+
426
+ texture.setState('loading');
427
+
428
+ // prioritize the texture for immediate loading
429
+ if (priority === true) {
430
+ texture
431
+ .getTextureData()
432
+ .then(() => {
433
+ this.uploadTexture(texture);
434
+ })
435
+ .catch((err) => {
436
+ console.error(err);
437
+ });
438
+ }
439
+
440
+ // enqueue the texture for download and upload
441
+ this.enqueueDownloadTextureSource(texture);
442
+ }
443
+
444
+ /**
445
+ * Upload a texture to the GPU
446
+ *
447
+ * @param texture Texture to upload
448
+ */
449
+ uploadTexture(texture: Texture): void {
450
+ if (
451
+ this.stage.txMemManager.doNotExceedCriticalThreshold === true &&
452
+ this.stage.txMemManager.criticalCleanupRequested === true
453
+ ) {
454
+ // we're at a critical memory threshold, don't upload textures
455
+ this.enqueueUploadTexture(texture);
456
+ return;
457
+ }
458
+
459
+ const coreContext = texture.loadCtxTexture();
460
+ if (coreContext !== null && coreContext.state === 'loaded') {
461
+ texture.setState('loaded');
462
+ return;
463
+ }
464
+
465
+ coreContext.load();
466
+ }
467
+
468
+ /**
469
+ * Check if a texture is being processed
470
+ */
471
+ isProcessingTexture(texture: Texture): boolean {
472
+ return (
473
+ this.downloadTextureSourceQueue.includes(texture) === true ||
474
+ this.uploadTextureQueue.includes(texture) === true
475
+ );
476
+ }
477
+
478
+ /**
479
+ * Process a limited number of downloads and uploads.
480
+ *
481
+ * @param maxItems - The maximum number of items to process
482
+ */
483
+ processSome(maxProcessingTime: number): void {
484
+ if (this.initialized === false) {
485
+ return;
486
+ }
487
+
488
+ const startTime = getTimeStamp();
489
+
490
+ // Process priority queue
491
+ while (
492
+ this.priorityQueue.length > 0 &&
493
+ getTimeStamp() - startTime < maxProcessingTime
494
+ ) {
495
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
496
+ const texture = this.priorityQueue.pop()!;
497
+ texture.getTextureData().then(() => {
498
+ this.uploadTexture(texture);
499
+ });
500
+ }
501
+
502
+ // Process uploads
503
+ while (
504
+ this.uploadTextureQueue.length > 0 &&
505
+ getTimeStamp() - startTime < maxProcessingTime
506
+ ) {
507
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
508
+ this.uploadTexture(this.uploadTextureQueue.pop()!);
509
+ }
510
+
511
+ // Process downloads
512
+ while (
513
+ this.downloadTextureSourceQueue.length > 0 &&
514
+ getTimeStamp() - startTime < maxProcessingTime
515
+ ) {
516
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
517
+ const texture = this.downloadTextureSourceQueue.shift()!;
518
+ texture.getTextureData().then(() => {
519
+ if (texture.state === 'fetched') {
520
+ this.enqueueUploadTexture(texture);
521
+ }
522
+ });
523
+ }
524
+ }
525
+
526
+ public hasUpdates(): boolean {
527
+ return (
528
+ this.downloadTextureSourceQueue.length > 0 ||
529
+ this.uploadTextureQueue.length > 0
530
+ );
531
+ }
532
+
533
+ /**
534
+ * Initialize a texture to the cache
535
+ *
536
+ * @param texture Texture to cache
537
+ * @param cacheKey Cache key for the texture
538
+ */
539
+ initTextureToCache(texture: Texture, cacheKey: string) {
540
+ const { keyCache, inverseKeyCache } = this;
541
+ keyCache.set(cacheKey, texture);
542
+ inverseKeyCache.set(texture, cacheKey);
543
+ }
544
+
545
+ /**
546
+ * Get a texture from the cache
547
+ *
548
+ * @param cacheKey
549
+ */
550
+ getTextureFromCache(cacheKey: string): Texture | undefined {
551
+ return this.keyCache.get(cacheKey);
552
+ }
553
+
554
+ /**
555
+ * Remove a texture from the cache
556
+ *
557
+ * @remarks
558
+ * Called by Texture Cleanup when a texture is freed.
559
+ *
560
+ * @param texture
561
+ */
562
+ removeTextureFromCache(texture: Texture) {
563
+ const { inverseKeyCache, keyCache } = this;
564
+ const cacheKey = inverseKeyCache.get(texture);
565
+ if (cacheKey) {
566
+ keyCache.delete(cacheKey);
567
+ }
568
+ }
569
+
570
+ /**
571
+ * Remove texture from the queue's
572
+ *
573
+ * @param texture - The texture to remove
574
+ */
575
+ removeTextureFromQueue(texture: Texture): void {
576
+ const downloadIndex = this.downloadTextureSourceQueue.indexOf(texture);
577
+ if (downloadIndex !== -1) {
578
+ this.downloadTextureSourceQueue.splice(downloadIndex, 1);
579
+ }
580
+
581
+ const uploadIndex = this.uploadTextureQueue.indexOf(texture);
582
+ if (uploadIndex !== -1) {
583
+ this.uploadTextureQueue.splice(uploadIndex, 1);
584
+ }
585
+ }
586
+
587
+ /**
588
+ * Resolve a parent texture from the cache or fallback to the provided texture.
589
+ *
590
+ * @param texture - The provided texture to resolve.
591
+ * @returns The cached or provided texture.
592
+ */
593
+ resolveParentTexture(texture: ImageTexture): Texture {
594
+ if (!texture?.props) {
595
+ return texture;
596
+ }
597
+
598
+ const cacheKey = ImageTexture.makeCacheKey(texture.props);
599
+ const cachedTexture = cacheKey
600
+ ? this.getTextureFromCache(cacheKey)
601
+ : undefined;
602
+ return cachedTexture ?? texture;
603
+ }
604
+ }