@lightningjs/renderer 0.7.2 → 0.7.4

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 (195) hide show
  1. package/COPYING +1 -0
  2. package/LICENSE +202 -202
  3. package/NOTICE +3 -3
  4. package/README.md +221 -221
  5. package/dist/src/common/CommonTypes.d.ts +7 -0
  6. package/dist/src/core/Stage.d.ts +2 -1
  7. package/dist/src/core/Stage.js +4 -0
  8. package/dist/src/core/Stage.js.map +1 -1
  9. package/dist/src/core/animations/CoreAnimation.js +3 -0
  10. package/dist/src/core/animations/CoreAnimation.js.map +1 -1
  11. package/dist/src/core/animations/CoreAnimationController.d.ts +8 -0
  12. package/dist/src/core/animations/CoreAnimationController.js +26 -0
  13. package/dist/src/core/animations/CoreAnimationController.js.map +1 -1
  14. package/dist/src/core/lib/ImageWorker.d.ts +1 -1
  15. package/dist/src/core/lib/ImageWorker.js +30 -22
  16. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  17. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  18. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +47 -47
  19. package/dist/src/core/renderers/webgl/shaders/DefaultShaderBatched.js +61 -61
  20. package/dist/src/core/renderers/webgl/shaders/DynamicShader.js +93 -93
  21. package/dist/src/core/renderers/webgl/shaders/RoundedRectangle.js +63 -63
  22. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +49 -49
  23. package/dist/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.js +15 -15
  24. package/dist/src/core/renderers/webgl/shaders/effects/BorderEffect.js +5 -5
  25. package/dist/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.js +15 -15
  26. package/dist/src/core/renderers/webgl/shaders/effects/BorderRightEffect.js +15 -15
  27. package/dist/src/core/renderers/webgl/shaders/effects/BorderTopEffect.js +15 -15
  28. package/dist/src/core/renderers/webgl/shaders/effects/FadeOutEffect.js +42 -42
  29. package/dist/src/core/renderers/webgl/shaders/effects/GlitchEffect.js +44 -44
  30. package/dist/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.js +3 -3
  31. package/dist/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.js +31 -31
  32. package/dist/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.js +13 -13
  33. package/dist/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.js +37 -37
  34. package/dist/src/core/renderers/webgl/shaders/effects/RadiusEffect.js +19 -19
  35. package/dist/src/core/textures/ImageTexture.d.ts +2 -1
  36. package/dist/src/core/textures/ImageTexture.js +7 -2
  37. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  38. package/dist/src/core/textures/Texture.d.ts +1 -1
  39. package/dist/src/main-api/ICoreDriver.d.ts +2 -1
  40. package/dist/src/main-api/Inspector.d.ts +6 -0
  41. package/dist/src/main-api/Inspector.js +105 -19
  42. package/dist/src/main-api/Inspector.js.map +1 -1
  43. package/dist/src/main-api/RendererMain.d.ts +12 -0
  44. package/dist/src/main-api/RendererMain.js +19 -1
  45. package/dist/src/main-api/RendererMain.js.map +1 -1
  46. package/dist/src/render-drivers/main/MainCoreDriver.d.ts +2 -1
  47. package/dist/src/render-drivers/main/MainCoreDriver.js +7 -1
  48. package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -1
  49. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.d.ts +2 -1
  50. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +7 -0
  51. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +1 -1
  52. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.d.ts +9 -1
  53. package/dist/src/render-drivers/threadx/ThreadXRendererMessage.js.map +1 -1
  54. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  55. package/exports/core-api.ts +102 -102
  56. package/exports/main-api.ts +60 -60
  57. package/exports/utils.ts +41 -41
  58. package/package.json +5 -3
  59. package/scripts/please-use-pnpm.js +13 -0
  60. package/src/common/CommonTypes.ts +113 -105
  61. package/src/common/EventEmitter.ts +77 -77
  62. package/src/common/IAnimationController.ts +29 -29
  63. package/src/core/CoreExtension.ts +32 -32
  64. package/src/core/CoreNode.ts +926 -926
  65. package/src/core/CoreShaderManager.ts +243 -243
  66. package/src/core/CoreTextNode.ts +391 -391
  67. package/src/core/CoreTextureManager.ts +326 -326
  68. package/src/core/Stage.ts +354 -342
  69. package/src/core/animations/AnimationManager.ts +38 -38
  70. package/src/core/animations/CoreAnimation.ts +181 -177
  71. package/src/core/animations/CoreAnimationController.ts +148 -117
  72. package/src/core/lib/ContextSpy.ts +41 -41
  73. package/src/core/lib/ImageWorker.ts +135 -124
  74. package/src/core/lib/Matrix3d.ts +290 -290
  75. package/src/core/lib/WebGlContextWrapper.ts +992 -992
  76. package/src/core/lib/textureCompression.ts +152 -152
  77. package/src/core/lib/utils.ts +241 -241
  78. package/src/core/platform.ts +46 -46
  79. package/src/core/renderers/CoreContextTexture.ts +30 -30
  80. package/src/core/renderers/CoreRenderOp.ts +22 -22
  81. package/src/core/renderers/CoreRenderer.ts +63 -63
  82. package/src/core/renderers/CoreShader.ts +41 -41
  83. package/src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts +37 -37
  84. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +230 -231
  85. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +107 -107
  86. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +520 -520
  87. package/src/core/renderers/webgl/WebGlCoreShader.ts +337 -337
  88. package/src/core/renderers/webgl/internal/BufferCollection.ts +54 -54
  89. package/src/core/renderers/webgl/internal/RendererUtils.ts +131 -131
  90. package/src/core/renderers/webgl/internal/ShaderUtils.ts +136 -136
  91. package/src/core/renderers/webgl/internal/WebGlUtils.ts +35 -35
  92. package/src/core/renderers/webgl/shaders/DefaultShader.ts +95 -95
  93. package/src/core/renderers/webgl/shaders/DefaultShaderBatched.ts +132 -132
  94. package/src/core/renderers/webgl/shaders/DynamicShader.ts +474 -474
  95. package/src/core/renderers/webgl/shaders/RoundedRectangle.ts +161 -161
  96. package/src/core/renderers/webgl/shaders/SdfShader.ts +174 -174
  97. package/src/core/renderers/webgl/shaders/effects/BorderBottomEffect.ts +101 -101
  98. package/src/core/renderers/webgl/shaders/effects/BorderEffect.ts +86 -86
  99. package/src/core/renderers/webgl/shaders/effects/BorderLeftEffect.ts +101 -101
  100. package/src/core/renderers/webgl/shaders/effects/BorderRightEffect.ts +101 -101
  101. package/src/core/renderers/webgl/shaders/effects/BorderTopEffect.ts +101 -101
  102. package/src/core/renderers/webgl/shaders/effects/EffectUtils.ts +33 -33
  103. package/src/core/renderers/webgl/shaders/effects/FadeOutEffect.ts +135 -135
  104. package/src/core/renderers/webgl/shaders/effects/GlitchEffect.ts +145 -145
  105. package/src/core/renderers/webgl/shaders/effects/GrayscaleEffect.ts +67 -67
  106. package/src/core/renderers/webgl/shaders/effects/LinearGradientEffect.ts +176 -176
  107. package/src/core/renderers/webgl/shaders/effects/RadialGradientEffect.ts +159 -159
  108. package/src/core/renderers/webgl/shaders/effects/RadialProgressEffect.ts +186 -186
  109. package/src/core/renderers/webgl/shaders/effects/RadiusEffect.ts +121 -121
  110. package/src/core/renderers/webgl/shaders/effects/ShaderEffect.ts +114 -114
  111. package/src/core/text-rendering/TextTextureRendererUtils.ts +189 -189
  112. package/src/core/text-rendering/TrFontManager.ts +96 -96
  113. package/src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts +141 -141
  114. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/FontShaper.ts +139 -139
  115. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.test.ts +173 -173
  116. package/src/core/text-rendering/font-face-types/SdfTrFontFace/internal/SdfFontShaper.ts +169 -169
  117. package/src/core/text-rendering/font-face-types/TrFontFace.ts +105 -105
  118. package/src/core/text-rendering/font-face-types/WebTrFontFace.ts +77 -77
  119. package/src/core/text-rendering/renderers/CanvasTextRenderer.ts +751 -751
  120. package/src/core/text-rendering/renderers/LightningTextTextureRenderer.ts +741 -741
  121. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +775 -775
  122. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.test.ts +48 -48
  123. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/PeekableGenerator.ts +66 -66
  124. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/SpecialCodepoints.ts +52 -52
  125. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/constants.ts +32 -32
  126. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getStartConditions.ts +84 -84
  127. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.test.ts +133 -133
  128. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/getUnicodeCodepoints.ts +38 -38
  129. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts +393 -393
  130. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.test.ts +49 -49
  131. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/measureText.ts +51 -51
  132. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.test.ts +205 -205
  133. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/setRenderWindow.ts +93 -93
  134. package/src/core/text-rendering/renderers/SdfTextRenderer/internal/util.ts +40 -40
  135. package/src/core/text-rendering/renderers/TextRenderer.ts +504 -504
  136. package/src/core/textures/ColorTexture.ts +86 -86
  137. package/src/core/textures/ImageTexture.ts +140 -133
  138. package/src/core/textures/NoiseTexture.ts +96 -96
  139. package/src/core/textures/SubTexture.ts +143 -143
  140. package/src/core/textures/Texture.ts +218 -218
  141. package/src/core/utils.ts +224 -224
  142. package/src/env.d.ts +7 -7
  143. package/src/main-api/ICoreDriver.ts +66 -61
  144. package/src/main-api/INode.ts +470 -470
  145. package/src/main-api/Inspector.ts +432 -0
  146. package/src/main-api/RendererMain.ts +649 -610
  147. package/src/main-api/texture-usage-trackers/FinalizationRegistryTextureUsageTracker.ts +45 -45
  148. package/src/main-api/texture-usage-trackers/ManualCountTextureUsageTracker.ts +154 -154
  149. package/src/main-api/texture-usage-trackers/TextureUsageTracker.ts +54 -54
  150. package/src/render-drivers/main/MainCoreDriver.ts +148 -133
  151. package/src/render-drivers/main/MainOnlyNode.ts +453 -453
  152. package/src/render-drivers/main/MainOnlyTextNode.ts +261 -261
  153. package/src/render-drivers/threadx/NodeStruct.ts +300 -300
  154. package/src/render-drivers/threadx/SharedNode.ts +97 -97
  155. package/src/render-drivers/threadx/TextNodeStruct.ts +211 -211
  156. package/src/render-drivers/threadx/ThreadXCoreDriver.ts +285 -273
  157. package/src/render-drivers/threadx/ThreadXMainAnimationController.ts +99 -99
  158. package/src/render-drivers/threadx/ThreadXMainNode.ts +178 -178
  159. package/src/render-drivers/threadx/ThreadXMainTextNode.ts +85 -85
  160. package/src/render-drivers/threadx/ThreadXRendererMessage.ts +110 -97
  161. package/src/render-drivers/threadx/worker/ThreadXRendererNode.ts +238 -238
  162. package/src/render-drivers/threadx/worker/ThreadXRendererTextNode.ts +149 -149
  163. package/src/render-drivers/threadx/worker/renderer.ts +151 -151
  164. package/src/render-drivers/utils.ts +57 -57
  165. package/src/utils.ts +207 -207
  166. package/dist/src/core/lib/WebGlContext.d.ts +0 -414
  167. package/dist/src/core/lib/WebGlContext.js +0 -640
  168. package/dist/src/core/lib/WebGlContext.js.map +0 -1
  169. package/dist/src/core/scene/Scene.d.ts +0 -59
  170. package/dist/src/core/scene/Scene.js +0 -106
  171. package/dist/src/core/scene/Scene.js.map +0 -1
  172. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.d.ts +0 -8
  173. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.js +0 -29
  174. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/findNearestMultiple.js.map +0 -1
  175. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.d.ts +0 -19
  176. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.js +0 -84
  177. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/SdfBufferHelper.js.map +0 -1
  178. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.d.ts +0 -8
  179. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.js +0 -40
  180. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutLine.js.map +0 -1
  181. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.d.ts +0 -2
  182. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.js +0 -41
  183. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/layoutText2.js.map +0 -1
  184. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.d.ts +0 -1
  185. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.js +0 -4
  186. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2/utils.js.map +0 -1
  187. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.d.ts +0 -1
  188. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.js +0 -2
  189. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText2.js.map +0 -1
  190. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.d.ts +0 -20
  191. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js +0 -55
  192. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/makeRenderWindow.js.map +0 -1
  193. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.d.ts +0 -9
  194. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.js +0 -32
  195. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/internal/roundUpToMultiple.js.map +0 -1
@@ -1,326 +1,326 @@
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 { assertTruthy } from '../utils.js';
21
- import { ImageWorkerManager } from './lib/ImageWorker.js';
22
- import type { CoreContextTexture } from './renderers/CoreContextTexture.js';
23
- import type { CoreRenderer } from './renderers/CoreRenderer.js';
24
- import { ColorTexture } from './textures/ColorTexture.js';
25
- import { ImageTexture } from './textures/ImageTexture.js';
26
- import { NoiseTexture } from './textures/NoiseTexture.js';
27
- import { SubTexture } from './textures/SubTexture.js';
28
- import type { Texture } from './textures/Texture.js';
29
-
30
- /**
31
- * Augmentable map of texture types
32
- *
33
- * @remarks
34
- * This interface can be augmented by other modules/apps to add additional
35
- * texture types. The ones included directly here are the ones that are
36
- * included in the core library.
37
- */
38
- export interface TextureMap {
39
- ColorTexture: typeof ColorTexture;
40
- ImageTexture: typeof ImageTexture;
41
- NoiseTexture: typeof NoiseTexture;
42
- SubTexture: typeof SubTexture;
43
- }
44
-
45
- export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
46
- ? Props
47
- : never;
48
-
49
- /**
50
- * Contains information about the texture manager's internal state
51
- * for debugging purposes.
52
- */
53
- export interface TextureManagerDebugInfo {
54
- keyCacheSize: number;
55
- idCacheSize: number;
56
- }
57
-
58
- /**
59
- * Universal options for all texture types
60
- *
61
- * @remarks
62
- * Texture Options provide a way to specify options that are relevant to the
63
- * texture loading process (including caching) and specifically for how a
64
- * texture is rendered within a specific Node (or set of Nodes).
65
- *
66
- * They are not used in determining the cache key for a texture (except if
67
- * the `cacheKey` option is provided explicitly to oveerride the default
68
- * cache key for the texture instance) nor are they stored/referenced within
69
- * the texture instance itself. Instead, the options are stored/referenced
70
- * within individual Nodes. So a single texture instance can be used in
71
- * multiple Nodes each using a different set of options.
72
- */
73
- export interface TextureOptions {
74
- /**
75
- * Preload the texture immediately even if it's not being rendered to the
76
- * screen.
77
- *
78
- * @remarks
79
- * This allows the texture to be used immediately without any delay when it
80
- * is first needed for rendering. Otherwise the loading process will start
81
- * when the texture is first rendered, which may cause a delay in that texture
82
- * being shown properly.
83
- *
84
- * @defaultValue `false`
85
- */
86
- preload?: boolean;
87
-
88
- /**
89
- * ID to use for this texture.
90
- *
91
- * @remarks
92
- * This is for internal use only as an optimization.
93
- *
94
- * @privateRemarks
95
- * This is used to avoid having to look up the texture in the texture cache
96
- * by its cache key. Theoretically this should be faster.
97
- *
98
- * @defaultValue Automatically generated
99
- */
100
- id?: number;
101
-
102
- /**
103
- * Cache key to use for this texture
104
- *
105
- * @remarks
106
- * If this is set, the texture will be cached using this key. If a texture
107
- * with the same key is already cached, it will be returned instead of
108
- * creating a new texture.
109
- *
110
- * If this is not set (undefined), it will be automatically generated via
111
- * the specified `Texture`'s `makeCacheKey()` method.
112
- *
113
- * @defaultValue Automatically generated via `Texture.makeCacheKey()`
114
- */
115
- cacheKey?: string | false;
116
-
117
- /**
118
- * Flip the texture horizontally when rendering
119
- *
120
- * @defaultValue `false`
121
- */
122
- flipX?: boolean;
123
-
124
- /**
125
- * Flip the texture vertically when rendering
126
- *
127
- * @defaultValue `false`
128
- */
129
- flipY?: boolean;
130
- }
131
-
132
- export class CoreTextureManager {
133
- /**
134
- * Amount of used memory defined in pixels
135
- */
136
- usedMemory = 0;
137
-
138
- txConstructors: Partial<TextureMap> = {};
139
-
140
- textureKeyCache: Map<string, Texture> = new Map();
141
- textureIdCache: Map<number, Texture> = new Map();
142
-
143
- ctxTextureCache: WeakMap<Texture, CoreContextTexture> = new WeakMap();
144
- textureRefCountMap: WeakMap<
145
- Texture,
146
- { cacheKey: string | false; count: number }
147
- > = new WeakMap();
148
- imageWorkerManager: ImageWorkerManager;
149
- /**
150
- * Renderer that this texture manager is associated with
151
- *
152
- * @remarks
153
- * This MUST be set before the texture manager is used. Otherwise errors
154
- * will occur when using the texture manager.
155
- */
156
- renderer!: CoreRenderer;
157
-
158
- constructor(numImageWorkers: number) {
159
- // Register default known texture types
160
- this.imageWorkerManager = new ImageWorkerManager(numImageWorkers);
161
- this.registerTextureType('ImageTexture', ImageTexture);
162
- this.registerTextureType('ColorTexture', ColorTexture);
163
- this.registerTextureType('NoiseTexture', NoiseTexture);
164
- this.registerTextureType('SubTexture', SubTexture);
165
- }
166
-
167
- registerTextureType<Type extends keyof TextureMap>(
168
- textureType: Type,
169
- textureClass: TextureMap[Type],
170
- ): void {
171
- this.txConstructors[textureType] = textureClass;
172
- }
173
-
174
- loadTexture<Type extends keyof TextureMap>(
175
- textureType: Type,
176
- props: ExtractProps<TextureMap[Type]>,
177
- options: TextureOptions | null = null,
178
- ): InstanceType<TextureMap[Type]> {
179
- const TextureClass = this.txConstructors[textureType];
180
- if (!TextureClass) {
181
- throw new Error(`Texture type "${textureType}" is not registered`);
182
- }
183
- let texture: Texture | undefined;
184
- // If an ID is specified, try to get the texture from the ID cache first
185
- if (options?.id !== undefined && this.textureIdCache.has(options.id)) {
186
- // console.log('Getting texture by texture desc ID', options.id);
187
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
188
- texture = this.textureIdCache.get(options.id)!;
189
- }
190
- // If the texture is not found in the ID cache, try to get it from the key cache
191
- if (!texture) {
192
- const descId = options?.id;
193
- const cacheKey =
194
- options?.cacheKey ?? TextureClass.makeCacheKey(props as any);
195
- if (cacheKey && this.textureKeyCache.has(cacheKey)) {
196
- // console.log('Getting texture by cache key', cacheKey);
197
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
198
- texture = this.textureKeyCache.get(cacheKey)!;
199
- } else {
200
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
201
- texture = new TextureClass(this, props as any);
202
- }
203
- if (descId) {
204
- this.addTextureIdToCache(descId, cacheKey, texture);
205
- }
206
- }
207
- if (options?.preload) {
208
- const ctxTx = this.getCtxTexture(texture);
209
- ctxTx.load();
210
- }
211
- return texture as InstanceType<TextureMap[Type]>;
212
- }
213
-
214
- /**
215
- * Add a `Texture` to the texture cache by its texture desc ID and cache key
216
- *
217
- * @remarks
218
- * This is used internally by the `CoreTextureManager` to cache textures
219
- * when they are created.
220
- *
221
- * It handles updating the texture ID cache, texture key cache, and texture
222
- * reference count map.
223
- *
224
- * @param textureDescId
225
- * @param cacheKey
226
- * @param texture
227
- */
228
- private addTextureIdToCache(
229
- textureDescId: number,
230
- cacheKey: string | false,
231
- texture: Texture,
232
- ): void {
233
- const { textureIdCache, textureRefCountMap } = this;
234
- textureIdCache.set(textureDescId, texture);
235
- if (textureRefCountMap.has(texture)) {
236
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
237
- textureRefCountMap.get(texture)!.count++;
238
- } else {
239
- textureRefCountMap.set(texture, { cacheKey, count: 1 });
240
- if (cacheKey) {
241
- this.textureKeyCache.set(cacheKey, texture);
242
- }
243
- }
244
- }
245
-
246
- /**
247
- * Remove a `Texture` from the texture cache by its texture desc ID
248
- *
249
- * @remarks
250
- * This is called externally by when we know (at least reasonably well) that
251
- * the `TextureRef` in the Main API space has been is no longer used. This
252
- * allows us to remove the `Texture` from the Usage Cache so that it can be
253
- * garbage collected as well.
254
- *
255
- * @param textureDescId
256
- */
257
- removeTextureIdFromCache(textureDescId: number): void {
258
- const { textureIdCache, textureRefCountMap } = this;
259
- const texture = textureIdCache.get(textureDescId);
260
- if (!texture) {
261
- // Sometimes a texture is removed from the cache before it ever gets
262
- // added to the cache. This is fine and not an error.
263
- return;
264
- }
265
- textureIdCache.delete(textureDescId);
266
- if (textureRefCountMap.has(texture)) {
267
- const refCountObj = textureRefCountMap.get(texture);
268
- assertTruthy(refCountObj);
269
- refCountObj.count--;
270
- if (refCountObj.count === 0) {
271
- textureRefCountMap.delete(texture);
272
- // If the texture is not referenced anywhere else, remove it from the key cache
273
- // as well.
274
- // This should allow the `Texture` instance to be garbage collected.
275
- if (refCountObj.cacheKey) {
276
- this.textureKeyCache.delete(refCountObj.cacheKey);
277
- }
278
- }
279
- }
280
- }
281
-
282
- /**
283
- * Get an object containing debug information about the texture manager.
284
- *
285
- * @returns
286
- */
287
- getDebugInfo(): TextureManagerDebugInfo {
288
- // const textureSet = new Set<Texture>();
289
- // for (const texture of this.textureIdCache.values()) {
290
- // textureSet.add(texture);
291
- // }
292
- // for (const texture of this.textureKeyCache.values()) {
293
- // textureSet.add(texture);
294
- // }
295
- // TODO: Output number of bytes used by textures
296
- return {
297
- keyCacheSize: this.textureKeyCache.size,
298
- idCacheSize: this.textureIdCache.size,
299
- };
300
- }
301
-
302
- /**
303
- * Get a CoreContextTexture for the given Texture source.
304
- *
305
- * @remarks
306
- * If the texture source already has an allocated CoreContextTexture, it will be
307
- * returned from the cache. Otherwise, a new CoreContextTexture will be created
308
- * and cached.
309
- *
310
- * ContextTextures are stored in a WeakMap, so they will be garbage collected
311
- * when the Texture source is no longer referenced.
312
- *
313
- * @param textureSource
314
- * @returns
315
- */
316
- getCtxTexture(textureSource: Texture): CoreContextTexture {
317
- if (this.ctxTextureCache.has(textureSource)) {
318
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
319
- return this.ctxTextureCache.get(textureSource)!;
320
- }
321
- const texture = this.renderer.createCtxTexture(textureSource);
322
-
323
- this.ctxTextureCache.set(textureSource, texture);
324
- return texture;
325
- }
326
- }
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 { assertTruthy } from '../utils.js';
21
+ import { ImageWorkerManager } from './lib/ImageWorker.js';
22
+ import type { CoreContextTexture } from './renderers/CoreContextTexture.js';
23
+ import type { CoreRenderer } from './renderers/CoreRenderer.js';
24
+ import { ColorTexture } from './textures/ColorTexture.js';
25
+ import { ImageTexture } from './textures/ImageTexture.js';
26
+ import { NoiseTexture } from './textures/NoiseTexture.js';
27
+ import { SubTexture } from './textures/SubTexture.js';
28
+ import type { Texture } from './textures/Texture.js';
29
+
30
+ /**
31
+ * Augmentable map of texture types
32
+ *
33
+ * @remarks
34
+ * This interface can be augmented by other modules/apps to add additional
35
+ * texture types. The ones included directly here are the ones that are
36
+ * included in the core library.
37
+ */
38
+ export interface TextureMap {
39
+ ColorTexture: typeof ColorTexture;
40
+ ImageTexture: typeof ImageTexture;
41
+ NoiseTexture: typeof NoiseTexture;
42
+ SubTexture: typeof SubTexture;
43
+ }
44
+
45
+ export type ExtractProps<Type> = Type extends { z$__type__Props: infer Props }
46
+ ? Props
47
+ : never;
48
+
49
+ /**
50
+ * Contains information about the texture manager's internal state
51
+ * for debugging purposes.
52
+ */
53
+ export interface TextureManagerDebugInfo {
54
+ keyCacheSize: number;
55
+ idCacheSize: number;
56
+ }
57
+
58
+ /**
59
+ * Universal options for all texture types
60
+ *
61
+ * @remarks
62
+ * Texture Options provide a way to specify options that are relevant to the
63
+ * texture loading process (including caching) and specifically for how a
64
+ * texture is rendered within a specific Node (or set of Nodes).
65
+ *
66
+ * They are not used in determining the cache key for a texture (except if
67
+ * the `cacheKey` option is provided explicitly to oveerride the default
68
+ * cache key for the texture instance) nor are they stored/referenced within
69
+ * the texture instance itself. Instead, the options are stored/referenced
70
+ * within individual Nodes. So a single texture instance can be used in
71
+ * multiple Nodes each using a different set of options.
72
+ */
73
+ export interface TextureOptions {
74
+ /**
75
+ * Preload the texture immediately even if it's not being rendered to the
76
+ * screen.
77
+ *
78
+ * @remarks
79
+ * This allows the texture to be used immediately without any delay when it
80
+ * is first needed for rendering. Otherwise the loading process will start
81
+ * when the texture is first rendered, which may cause a delay in that texture
82
+ * being shown properly.
83
+ *
84
+ * @defaultValue `false`
85
+ */
86
+ preload?: boolean;
87
+
88
+ /**
89
+ * ID to use for this texture.
90
+ *
91
+ * @remarks
92
+ * This is for internal use only as an optimization.
93
+ *
94
+ * @privateRemarks
95
+ * This is used to avoid having to look up the texture in the texture cache
96
+ * by its cache key. Theoretically this should be faster.
97
+ *
98
+ * @defaultValue Automatically generated
99
+ */
100
+ id?: number;
101
+
102
+ /**
103
+ * Cache key to use for this texture
104
+ *
105
+ * @remarks
106
+ * If this is set, the texture will be cached using this key. If a texture
107
+ * with the same key is already cached, it will be returned instead of
108
+ * creating a new texture.
109
+ *
110
+ * If this is not set (undefined), it will be automatically generated via
111
+ * the specified `Texture`'s `makeCacheKey()` method.
112
+ *
113
+ * @defaultValue Automatically generated via `Texture.makeCacheKey()`
114
+ */
115
+ cacheKey?: string | false;
116
+
117
+ /**
118
+ * Flip the texture horizontally when rendering
119
+ *
120
+ * @defaultValue `false`
121
+ */
122
+ flipX?: boolean;
123
+
124
+ /**
125
+ * Flip the texture vertically when rendering
126
+ *
127
+ * @defaultValue `false`
128
+ */
129
+ flipY?: boolean;
130
+ }
131
+
132
+ export class CoreTextureManager {
133
+ /**
134
+ * Amount of used memory defined in pixels
135
+ */
136
+ usedMemory = 0;
137
+
138
+ txConstructors: Partial<TextureMap> = {};
139
+
140
+ textureKeyCache: Map<string, Texture> = new Map();
141
+ textureIdCache: Map<number, Texture> = new Map();
142
+
143
+ ctxTextureCache: WeakMap<Texture, CoreContextTexture> = new WeakMap();
144
+ textureRefCountMap: WeakMap<
145
+ Texture,
146
+ { cacheKey: string | false; count: number }
147
+ > = new WeakMap();
148
+ imageWorkerManager: ImageWorkerManager;
149
+ /**
150
+ * Renderer that this texture manager is associated with
151
+ *
152
+ * @remarks
153
+ * This MUST be set before the texture manager is used. Otherwise errors
154
+ * will occur when using the texture manager.
155
+ */
156
+ renderer!: CoreRenderer;
157
+
158
+ constructor(numImageWorkers: number) {
159
+ // Register default known texture types
160
+ this.imageWorkerManager = new ImageWorkerManager(numImageWorkers);
161
+ this.registerTextureType('ImageTexture', ImageTexture);
162
+ this.registerTextureType('ColorTexture', ColorTexture);
163
+ this.registerTextureType('NoiseTexture', NoiseTexture);
164
+ this.registerTextureType('SubTexture', SubTexture);
165
+ }
166
+
167
+ registerTextureType<Type extends keyof TextureMap>(
168
+ textureType: Type,
169
+ textureClass: TextureMap[Type],
170
+ ): void {
171
+ this.txConstructors[textureType] = textureClass;
172
+ }
173
+
174
+ loadTexture<Type extends keyof TextureMap>(
175
+ textureType: Type,
176
+ props: ExtractProps<TextureMap[Type]>,
177
+ options: TextureOptions | null = null,
178
+ ): InstanceType<TextureMap[Type]> {
179
+ const TextureClass = this.txConstructors[textureType];
180
+ if (!TextureClass) {
181
+ throw new Error(`Texture type "${textureType}" is not registered`);
182
+ }
183
+ let texture: Texture | undefined;
184
+ // If an ID is specified, try to get the texture from the ID cache first
185
+ if (options?.id !== undefined && this.textureIdCache.has(options.id)) {
186
+ // console.log('Getting texture by texture desc ID', options.id);
187
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
188
+ texture = this.textureIdCache.get(options.id)!;
189
+ }
190
+ // If the texture is not found in the ID cache, try to get it from the key cache
191
+ if (!texture) {
192
+ const descId = options?.id;
193
+ const cacheKey =
194
+ options?.cacheKey ?? TextureClass.makeCacheKey(props as any);
195
+ if (cacheKey && this.textureKeyCache.has(cacheKey)) {
196
+ // console.log('Getting texture by cache key', cacheKey);
197
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
198
+ texture = this.textureKeyCache.get(cacheKey)!;
199
+ } else {
200
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
201
+ texture = new TextureClass(this, props as any);
202
+ }
203
+ if (descId) {
204
+ this.addTextureIdToCache(descId, cacheKey, texture);
205
+ }
206
+ }
207
+ if (options?.preload) {
208
+ const ctxTx = this.getCtxTexture(texture);
209
+ ctxTx.load();
210
+ }
211
+ return texture as InstanceType<TextureMap[Type]>;
212
+ }
213
+
214
+ /**
215
+ * Add a `Texture` to the texture cache by its texture desc ID and cache key
216
+ *
217
+ * @remarks
218
+ * This is used internally by the `CoreTextureManager` to cache textures
219
+ * when they are created.
220
+ *
221
+ * It handles updating the texture ID cache, texture key cache, and texture
222
+ * reference count map.
223
+ *
224
+ * @param textureDescId
225
+ * @param cacheKey
226
+ * @param texture
227
+ */
228
+ private addTextureIdToCache(
229
+ textureDescId: number,
230
+ cacheKey: string | false,
231
+ texture: Texture,
232
+ ): void {
233
+ const { textureIdCache, textureRefCountMap } = this;
234
+ textureIdCache.set(textureDescId, texture);
235
+ if (textureRefCountMap.has(texture)) {
236
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
237
+ textureRefCountMap.get(texture)!.count++;
238
+ } else {
239
+ textureRefCountMap.set(texture, { cacheKey, count: 1 });
240
+ if (cacheKey) {
241
+ this.textureKeyCache.set(cacheKey, texture);
242
+ }
243
+ }
244
+ }
245
+
246
+ /**
247
+ * Remove a `Texture` from the texture cache by its texture desc ID
248
+ *
249
+ * @remarks
250
+ * This is called externally by when we know (at least reasonably well) that
251
+ * the `TextureRef` in the Main API space has been is no longer used. This
252
+ * allows us to remove the `Texture` from the Usage Cache so that it can be
253
+ * garbage collected as well.
254
+ *
255
+ * @param textureDescId
256
+ */
257
+ removeTextureIdFromCache(textureDescId: number): void {
258
+ const { textureIdCache, textureRefCountMap } = this;
259
+ const texture = textureIdCache.get(textureDescId);
260
+ if (!texture) {
261
+ // Sometimes a texture is removed from the cache before it ever gets
262
+ // added to the cache. This is fine and not an error.
263
+ return;
264
+ }
265
+ textureIdCache.delete(textureDescId);
266
+ if (textureRefCountMap.has(texture)) {
267
+ const refCountObj = textureRefCountMap.get(texture);
268
+ assertTruthy(refCountObj);
269
+ refCountObj.count--;
270
+ if (refCountObj.count === 0) {
271
+ textureRefCountMap.delete(texture);
272
+ // If the texture is not referenced anywhere else, remove it from the key cache
273
+ // as well.
274
+ // This should allow the `Texture` instance to be garbage collected.
275
+ if (refCountObj.cacheKey) {
276
+ this.textureKeyCache.delete(refCountObj.cacheKey);
277
+ }
278
+ }
279
+ }
280
+ }
281
+
282
+ /**
283
+ * Get an object containing debug information about the texture manager.
284
+ *
285
+ * @returns
286
+ */
287
+ getDebugInfo(): TextureManagerDebugInfo {
288
+ // const textureSet = new Set<Texture>();
289
+ // for (const texture of this.textureIdCache.values()) {
290
+ // textureSet.add(texture);
291
+ // }
292
+ // for (const texture of this.textureKeyCache.values()) {
293
+ // textureSet.add(texture);
294
+ // }
295
+ // TODO: Output number of bytes used by textures
296
+ return {
297
+ keyCacheSize: this.textureKeyCache.size,
298
+ idCacheSize: this.textureIdCache.size,
299
+ };
300
+ }
301
+
302
+ /**
303
+ * Get a CoreContextTexture for the given Texture source.
304
+ *
305
+ * @remarks
306
+ * If the texture source already has an allocated CoreContextTexture, it will be
307
+ * returned from the cache. Otherwise, a new CoreContextTexture will be created
308
+ * and cached.
309
+ *
310
+ * ContextTextures are stored in a WeakMap, so they will be garbage collected
311
+ * when the Texture source is no longer referenced.
312
+ *
313
+ * @param textureSource
314
+ * @returns
315
+ */
316
+ getCtxTexture(textureSource: Texture): CoreContextTexture {
317
+ if (this.ctxTextureCache.has(textureSource)) {
318
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
319
+ return this.ctxTextureCache.get(textureSource)!;
320
+ }
321
+ const texture = this.renderer.createCtxTexture(textureSource);
322
+
323
+ this.ctxTextureCache.set(textureSource, texture);
324
+ return texture;
325
+ }
326
+ }