@lightningjs/renderer 0.8.2 → 0.8.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 (153) hide show
  1. package/dist/src/core/CoreNode.d.ts +27 -1
  2. package/dist/src/core/CoreNode.js +130 -5
  3. package/dist/src/core/CoreNode.js.map +1 -1
  4. package/dist/src/core/CoreShaderManager.d.ts +2 -0
  5. package/dist/src/core/CoreShaderManager.js +9 -0
  6. package/dist/src/core/CoreShaderManager.js.map +1 -1
  7. package/dist/src/core/CoreTextNode.js +20 -1
  8. package/dist/src/core/CoreTextNode.js.map +1 -1
  9. package/dist/src/core/CoreTextureManager.d.ts +2 -0
  10. package/dist/src/core/CoreTextureManager.js +2 -0
  11. package/dist/src/core/CoreTextureManager.js.map +1 -1
  12. package/dist/src/core/Stage.d.ts +3 -2
  13. package/dist/src/core/Stage.js +36 -10
  14. package/dist/src/core/Stage.js.map +1 -1
  15. package/dist/src/core/TextureMemoryManager.d.ts +1 -0
  16. package/dist/src/core/TextureMemoryManager.js +3 -1
  17. package/dist/src/core/TextureMemoryManager.js.map +1 -1
  18. package/dist/src/core/lib/ImageWorker.d.ts +2 -1
  19. package/dist/src/core/lib/ImageWorker.js +11 -7
  20. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  21. package/dist/src/core/lib/WebGlContextWrapper.d.ts +26 -1
  22. package/dist/src/core/lib/WebGlContextWrapper.js +37 -1
  23. package/dist/src/core/lib/WebGlContextWrapper.js.map +1 -1
  24. package/dist/src/core/renderers/CoreRenderer.d.ts +32 -3
  25. package/dist/src/core/renderers/CoreRenderer.js +14 -2
  26. package/dist/src/core/renderers/CoreRenderer.js.map +1 -1
  27. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +22 -0
  28. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +166 -0
  29. package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -0
  30. package/dist/src/core/renderers/canvas/CanvasCoreTexture.d.ts +17 -0
  31. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +124 -0
  32. package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -0
  33. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.d.ts +5 -0
  34. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js +32 -0
  35. package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js.map +1 -0
  36. package/dist/src/core/renderers/canvas/internal/ColorUtils.d.ts +15 -0
  37. package/dist/src/core/renderers/canvas/internal/ColorUtils.js +45 -0
  38. package/dist/src/core/renderers/canvas/internal/ColorUtils.js.map +1 -0
  39. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.d.ts +10 -0
  40. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js +43 -0
  41. package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js.map +1 -0
  42. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.d.ts +11 -0
  43. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js +51 -0
  44. package/dist/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.js.map +1 -0
  45. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.d.ts +11 -1
  46. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +22 -11
  47. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  48. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.d.ts +4 -1
  49. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.js +7 -2
  50. package/dist/src/core/renderers/webgl/WebGlCoreRenderOp.js.map +1 -1
  51. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +18 -21
  52. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +121 -41
  53. package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -1
  54. package/dist/src/core/renderers/webgl/WebGlCoreShader.js +19 -4
  55. package/dist/src/core/renderers/webgl/WebGlCoreShader.js.map +1 -1
  56. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js +4 -6
  57. package/dist/src/core/renderers/webgl/shaders/DefaultShader.js.map +1 -1
  58. package/dist/src/core/renderers/webgl/shaders/SdfShader.js +6 -1
  59. package/dist/src/core/renderers/webgl/shaders/SdfShader.js.map +1 -1
  60. package/dist/src/core/text-rendering/TextRenderingUtils.d.ts +12 -0
  61. package/dist/src/core/text-rendering/TextRenderingUtils.js +14 -0
  62. package/dist/src/core/text-rendering/TextRenderingUtils.js.map +1 -0
  63. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.d.ts +2 -1
  64. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js +6 -3
  65. package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
  66. package/dist/src/core/text-rendering/renderers/TextRenderer.d.ts +2 -1
  67. package/dist/src/core/text-rendering/renderers/TextRenderer.js.map +1 -1
  68. package/dist/src/core/textures/ImageTexture.js +3 -0
  69. package/dist/src/core/textures/ImageTexture.js.map +1 -1
  70. package/dist/src/core/textures/RenderTexture.d.ts +28 -0
  71. package/dist/src/core/textures/RenderTexture.js +52 -0
  72. package/dist/src/core/textures/RenderTexture.js.map +1 -0
  73. package/dist/src/core/utils.d.ts +6 -1
  74. package/dist/src/core/utils.js +74 -82
  75. package/dist/src/core/utils.js.map +1 -1
  76. package/dist/src/main-api/INode.d.ts +9 -0
  77. package/dist/src/main-api/Inspector.js +7 -1
  78. package/dist/src/main-api/Inspector.js.map +1 -1
  79. package/dist/src/main-api/RendererMain.d.ts +4 -0
  80. package/dist/src/main-api/RendererMain.js +2 -0
  81. package/dist/src/main-api/RendererMain.js.map +1 -1
  82. package/dist/src/render-drivers/main/MainCoreDriver.js +1 -0
  83. package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -1
  84. package/dist/src/render-drivers/main/MainOnlyNode.d.ts +3 -0
  85. package/dist/src/render-drivers/main/MainOnlyNode.js +29 -0
  86. package/dist/src/render-drivers/main/MainOnlyNode.js.map +1 -1
  87. package/dist/src/render-drivers/main/MainOnlyTextNode.js +1 -0
  88. package/dist/src/render-drivers/main/MainOnlyTextNode.js.map +1 -1
  89. package/dist/src/render-drivers/threadx/NodeStruct.d.ts +3 -0
  90. package/dist/src/render-drivers/threadx/NodeStruct.js +9 -0
  91. package/dist/src/render-drivers/threadx/NodeStruct.js.map +1 -1
  92. package/dist/src/render-drivers/threadx/SharedNode.d.ts +1 -0
  93. package/dist/src/render-drivers/threadx/SharedNode.js +1 -0
  94. package/dist/src/render-drivers/threadx/SharedNode.js.map +1 -1
  95. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js +2 -0
  96. package/dist/src/render-drivers/threadx/ThreadXCoreDriver.js.map +1 -1
  97. package/dist/src/render-drivers/threadx/ThreadXMainNode.d.ts +3 -0
  98. package/dist/src/render-drivers/threadx/ThreadXMainNode.js +7 -0
  99. package/dist/src/render-drivers/threadx/ThreadXMainNode.js.map +1 -1
  100. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js +1 -0
  101. package/dist/src/render-drivers/threadx/worker/ThreadXRendererNode.js.map +1 -1
  102. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js +1 -0
  103. package/dist/src/render-drivers/threadx/worker/ThreadXRendererTextNode.js.map +1 -1
  104. package/dist/src/render-drivers/threadx/worker/renderer.js +2 -0
  105. package/dist/src/render-drivers/threadx/worker/renderer.js.map +1 -1
  106. package/dist/src/render-drivers/utils.js +1 -1
  107. package/dist/src/render-drivers/utils.js.map +1 -1
  108. package/dist/src/utils.d.ts +12 -6
  109. package/dist/src/utils.js +19 -9
  110. package/dist/src/utils.js.map +1 -1
  111. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  112. package/package.json +1 -1
  113. package/src/core/CoreNode.ts +164 -8
  114. package/src/core/CoreShaderManager.ts +13 -0
  115. package/src/core/CoreTextNode.ts +25 -0
  116. package/src/core/CoreTextureManager.ts +3 -0
  117. package/src/core/Stage.ts +44 -10
  118. package/src/core/TextureMemoryManager.ts +3 -1
  119. package/src/core/lib/ImageWorker.ts +13 -8
  120. package/src/core/lib/WebGlContextWrapper.ts +51 -1
  121. package/src/core/renderers/CoreRenderer.ts +46 -6
  122. package/src/core/renderers/canvas/CanvasCoreRenderer.ts +223 -0
  123. package/src/core/renderers/canvas/CanvasCoreTexture.ts +144 -0
  124. package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +37 -0
  125. package/src/core/renderers/canvas/internal/ColorUtils.ts +55 -0
  126. package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +48 -0
  127. package/src/core/renderers/webgl/WebGlCoreCtxRenderTexture.ts +79 -0
  128. package/src/core/renderers/webgl/WebGlCoreCtxTexture.ts +26 -24
  129. package/src/core/renderers/webgl/WebGlCoreRenderOp.ts +4 -2
  130. package/src/core/renderers/webgl/WebGlCoreRenderer.ts +173 -70
  131. package/src/core/renderers/webgl/WebGlCoreShader.ts +29 -4
  132. package/src/core/renderers/webgl/shaders/DefaultShader.ts +4 -6
  133. package/src/core/renderers/webgl/shaders/SdfShader.ts +6 -1
  134. package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +10 -1
  135. package/src/core/text-rendering/renderers/TextRenderer.ts +3 -0
  136. package/src/core/textures/ImageTexture.ts +3 -0
  137. package/src/core/textures/RenderTexture.ts +81 -0
  138. package/src/core/utils.ts +101 -93
  139. package/src/main-api/INode.ts +11 -0
  140. package/src/main-api/Inspector.ts +6 -1
  141. package/src/main-api/RendererMain.ts +8 -0
  142. package/src/render-drivers/main/MainCoreDriver.ts +1 -0
  143. package/src/render-drivers/main/MainOnlyNode.ts +44 -0
  144. package/src/render-drivers/main/MainOnlyTextNode.ts +1 -0
  145. package/src/render-drivers/threadx/NodeStruct.ts +10 -0
  146. package/src/render-drivers/threadx/SharedNode.ts +2 -0
  147. package/src/render-drivers/threadx/ThreadXCoreDriver.ts +2 -0
  148. package/src/render-drivers/threadx/ThreadXMainNode.ts +9 -0
  149. package/src/render-drivers/threadx/worker/ThreadXRendererNode.ts +1 -0
  150. package/src/render-drivers/threadx/worker/ThreadXRendererTextNode.ts +1 -0
  151. package/src/render-drivers/threadx/worker/renderer.ts +2 -0
  152. package/src/render-drivers/utils.ts +1 -1
  153. package/src/utils.ts +21 -9
@@ -17,13 +17,20 @@
17
17
  * limitations under the License.
18
18
  */
19
19
 
20
+ import type { Dimensions } from '../../common/CommonTypes.js';
21
+ import type { CoreNode } from '../CoreNode.js';
20
22
  import type { CoreShaderManager } from '../CoreShaderManager.js';
21
- import type { TextureOptions } from '../CoreTextureManager.js';
23
+ import type {
24
+ CoreTextureManager,
25
+ TextureOptions,
26
+ } from '../CoreTextureManager.js';
22
27
  import type { Stage } from '../Stage.js';
23
- import type { Rect, RectWithValid } from '../lib/utils.js';
28
+ import type { TextureMemoryManager } from '../TextureMemoryManager.js';
29
+ import type { ContextSpy } from '../lib/ContextSpy.js';
30
+ import type { RectWithValid } from '../lib/utils.js';
31
+ import { ColorTexture } from '../textures/ColorTexture.js';
24
32
  import type { Texture } from '../textures/Texture.js';
25
33
  import { CoreContextTexture } from './CoreContextTexture.js';
26
- import type { CoreRenderOp } from './CoreRenderOp.js';
27
34
  import type { CoreShader } from './CoreShader.js';
28
35
 
29
36
  export interface QuadOptions {
@@ -46,18 +53,51 @@ export interface QuadOptions {
46
53
  tb: number;
47
54
  tc: number;
48
55
  td: number;
56
+ rtt?: boolean;
57
+ parentHasRenderTexture?: boolean;
58
+ framebufferDimensions?: Dimensions;
59
+ }
60
+
61
+ export interface CoreRendererOptions {
62
+ stage: Stage;
63
+ canvas: HTMLCanvasElement | OffscreenCanvas;
64
+ pixelRatio: number;
65
+ txManager: CoreTextureManager;
66
+ txMemManager: TextureMemoryManager;
67
+ shManager: CoreShaderManager;
68
+ clearColor: number;
69
+ bufferMemory: number;
70
+ contextSpy: ContextSpy | null;
49
71
  }
50
72
 
51
73
  export abstract class CoreRenderer {
74
+ public options: CoreRendererOptions;
75
+ public mode: 'webgl' | 'canvas' | undefined;
76
+
52
77
  protected stage: Stage;
53
78
 
54
- constructor(stage: Stage) {
55
- this.stage = stage;
79
+ //// Core Managers
80
+ txManager: CoreTextureManager;
81
+ txMemManager: TextureMemoryManager;
82
+ shManager: CoreShaderManager;
83
+ rttNodes: CoreNode[] = [];
84
+
85
+ constructor(options: CoreRendererOptions) {
86
+ this.options = options;
87
+ this.stage = options.stage;
88
+ this.txManager = options.txManager;
89
+ this.txMemManager = options.txMemManager;
90
+ this.shManager = options.shManager;
56
91
  }
57
92
 
58
93
  abstract reset(): void;
59
- abstract render(surface: 'screen' | CoreContextTexture): void;
94
+ abstract render(surface?: 'screen' | CoreContextTexture): void;
60
95
  abstract addQuad(quad: QuadOptions): void;
61
96
  abstract createCtxTexture(textureSource: Texture): CoreContextTexture;
62
97
  abstract getShaderManager(): CoreShaderManager;
98
+ abstract get renderToTextureActive(): boolean;
99
+ abstract get activeRttNode(): CoreNode | null;
100
+ abstract renderRTTNodes(): void;
101
+ abstract removeRTTNode(node: CoreNode): void;
102
+ abstract renderToTexture(node: CoreNode): void;
63
103
  }
@@ -0,0 +1,223 @@
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 type { CoreNode } from '../../CoreNode.js';
21
+ import type { CoreShaderManager } from '../../CoreShaderManager.js';
22
+ import { getRgbaComponents, type RGBA } from '../../lib/utils.js';
23
+ import { SubTexture } from '../../textures/SubTexture.js';
24
+ import type { Texture } from '../../textures/Texture.js';
25
+ import type { CoreContextTexture } from '../CoreContextTexture.js';
26
+ import {
27
+ CoreRenderer,
28
+ type CoreRendererOptions,
29
+ type QuadOptions,
30
+ } from '../CoreRenderer.js';
31
+ import { CanvasCoreTexture } from './CanvasCoreTexture.js';
32
+ import { getRadius } from './internal/C2DShaderUtils.js';
33
+ import {
34
+ formatRgba,
35
+ parseColor,
36
+ type IParsedColor,
37
+ } from './internal/ColorUtils.js';
38
+
39
+ export class CanvasCoreRenderer extends CoreRenderer {
40
+ private context: CanvasRenderingContext2D;
41
+ private canvas: HTMLCanvasElement;
42
+ private pixelRatio: number;
43
+ private clearColor: RGBA | undefined;
44
+ public renderToTextureActive = false;
45
+ activeRttNode: CoreNode | null = null;
46
+ constructor(options: CoreRendererOptions) {
47
+ super(options);
48
+
49
+ this.mode = 'canvas';
50
+ this.shManager.renderer = this;
51
+
52
+ const { canvas, pixelRatio, clearColor } = options;
53
+ this.canvas = canvas as HTMLCanvasElement;
54
+ this.context = canvas.getContext('2d') as CanvasRenderingContext2D;
55
+ this.pixelRatio = pixelRatio;
56
+ this.clearColor = clearColor ? getRgbaComponents(clearColor) : undefined;
57
+ }
58
+
59
+ reset(): void {
60
+ // eslint-disable-next-line no-self-assign
61
+ this.canvas.width = this.canvas.width; // quick reset canvas
62
+
63
+ const ctx = this.context;
64
+
65
+ if (this.clearColor) {
66
+ const [r, g, b, a] = this.clearColor;
67
+ ctx.fillStyle = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
68
+ ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
69
+ }
70
+
71
+ ctx.scale(this.pixelRatio, this.pixelRatio);
72
+ }
73
+
74
+ render(): void {
75
+ // noop
76
+ }
77
+
78
+ addQuad(quad: QuadOptions): void {
79
+ const ctx = this.context;
80
+ const {
81
+ tx,
82
+ ty,
83
+ width,
84
+ height,
85
+ alpha,
86
+ colorTl,
87
+ colorTr,
88
+ colorBr,
89
+ ta,
90
+ tb,
91
+ tc,
92
+ td,
93
+ clippingRect,
94
+ } = quad;
95
+ let texture = quad.texture;
96
+ let ctxTexture: CanvasCoreTexture | undefined = undefined;
97
+ let frame:
98
+ | { x: number; y: number; width: number; height: number }
99
+ | undefined;
100
+
101
+ if (texture) {
102
+ if (texture instanceof SubTexture) {
103
+ frame = texture.props;
104
+ texture = texture.parentTexture;
105
+ }
106
+
107
+ ctxTexture = this.txManager.getCtxTexture(texture) as CanvasCoreTexture;
108
+ if (texture.state === 'freed') {
109
+ ctxTexture.load();
110
+ return;
111
+ }
112
+ if (texture.state !== 'loaded' || !ctxTexture.hasImage()) {
113
+ return;
114
+ }
115
+ }
116
+
117
+ const color = parseColor(colorTl);
118
+ const hasTransform = ta !== 1;
119
+ const hasClipping = clippingRect.width !== 0 && clippingRect.height !== 0;
120
+ const hasGradient = colorTl !== colorTr || colorTl !== colorBr;
121
+ const radius = quad.shader ? getRadius(quad) : 0;
122
+
123
+ if (hasTransform || hasClipping || radius) {
124
+ ctx.save();
125
+ }
126
+
127
+ if (hasClipping) {
128
+ const path = new Path2D();
129
+ const { x, y, width, height } = clippingRect;
130
+ path.rect(x, y, width, height);
131
+ ctx.clip(path);
132
+ }
133
+
134
+ if (hasTransform) {
135
+ // Quad transform:
136
+ // | ta tb tx |
137
+ // | tc td ty |
138
+ // | 0 0 1 |
139
+ // C2D transform:
140
+ // | a c e |
141
+ // | b d f |
142
+ // | 0 0 1 |
143
+ const scale = this.pixelRatio;
144
+ ctx.setTransform(ta, tc, tb, td, tx * scale, ty * scale);
145
+ ctx.scale(scale, scale);
146
+ ctx.translate(-tx, -ty);
147
+ }
148
+
149
+ if (radius) {
150
+ const path = new Path2D();
151
+ path.roundRect(tx, ty, width, height, radius);
152
+ ctx.clip(path);
153
+ }
154
+
155
+ if (ctxTexture) {
156
+ const image = ctxTexture.getImage(color);
157
+ ctx.globalAlpha = alpha;
158
+ if (frame) {
159
+ ctx.drawImage(
160
+ image,
161
+ frame.x,
162
+ frame.y,
163
+ frame.width,
164
+ frame.height,
165
+ tx,
166
+ ty,
167
+ width,
168
+ height,
169
+ );
170
+ } else {
171
+ ctx.drawImage(image, tx, ty, width, height);
172
+ }
173
+ ctx.globalAlpha = 1;
174
+ } else if (hasGradient) {
175
+ let endX: number = tx;
176
+ let endY: number = ty;
177
+ let endColor: IParsedColor;
178
+ if (colorTl === colorTr) {
179
+ // vertical
180
+ endX = tx;
181
+ endY = ty + height;
182
+ endColor = parseColor(colorBr);
183
+ } else {
184
+ // horizontal
185
+ endX = tx + width;
186
+ endY = ty;
187
+ endColor = parseColor(colorTr);
188
+ }
189
+ const gradient = ctx.createLinearGradient(tx, ty, endX, endY);
190
+ gradient.addColorStop(0, formatRgba(color));
191
+ gradient.addColorStop(1, formatRgba(endColor));
192
+ ctx.fillStyle = gradient;
193
+ ctx.fillRect(tx, ty, width, height);
194
+ } else {
195
+ ctx.fillStyle = formatRgba(color);
196
+ ctx.fillRect(tx, ty, width, height);
197
+ }
198
+
199
+ if (hasTransform || hasClipping || radius) {
200
+ ctx.restore();
201
+ }
202
+ }
203
+
204
+ createCtxTexture(textureSource: Texture): CoreContextTexture {
205
+ return new CanvasCoreTexture(this.txMemManager, textureSource);
206
+ }
207
+
208
+ getShaderManager(): CoreShaderManager {
209
+ return this.shManager;
210
+ }
211
+
212
+ renderRTTNodes(): void {
213
+ // noop
214
+ }
215
+
216
+ removeRTTNode(node: CoreNode): void {
217
+ // noop
218
+ }
219
+
220
+ renderToTexture(node: CoreNode): void {
221
+ // noop
222
+ }
223
+ }
@@ -0,0 +1,144 @@
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 type { Dimensions } from '../../../common/CommonTypes.js';
21
+ import { assertTruthy } from '../../../utils.js';
22
+ import { CoreContextTexture } from '../CoreContextTexture.js';
23
+ import { formatRgba, type IParsedColor } from './internal/ColorUtils.js';
24
+
25
+ export class CanvasCoreTexture extends CoreContextTexture {
26
+ protected image: ImageBitmap | HTMLCanvasElement | undefined;
27
+ protected tintCache:
28
+ | {
29
+ key: string;
30
+ image: HTMLCanvasElement;
31
+ }
32
+ | undefined;
33
+
34
+ load(): void {
35
+ if (this.textureSource.state !== 'freed') {
36
+ return;
37
+ }
38
+ this.textureSource.setState('loading');
39
+ this.onLoadRequest()
40
+ .then((size) => {
41
+ this.textureSource.setState('loaded', size);
42
+ this.updateMemSize();
43
+ })
44
+ .catch((err) => {
45
+ this.textureSource.setState('failed', err as Error);
46
+ });
47
+ }
48
+
49
+ free(): void {
50
+ this.image = undefined;
51
+ this.tintCache = undefined;
52
+ this.textureSource.setState('freed');
53
+ this.memManager.setTextureMemUse(this, 0);
54
+ }
55
+
56
+ updateMemSize(): void {
57
+ // Counting memory usage for:
58
+ // - main image
59
+ // - tinted image
60
+ const mult = this.tintCache ? 8 : 4;
61
+ if (this.textureSource.dimensions) {
62
+ const { width, height } = this.textureSource.dimensions;
63
+ this.memManager.setTextureMemUse(this, width * height * mult);
64
+ }
65
+ }
66
+
67
+ hasImage(): boolean {
68
+ return this.image !== undefined;
69
+ }
70
+
71
+ getImage(color: IParsedColor): ImageBitmap | HTMLCanvasElement {
72
+ const image = this.image;
73
+ assertTruthy(image, 'Attempt to get unloaded image texture');
74
+
75
+ if (color.isWhite) {
76
+ if (this.tintCache) {
77
+ this.tintCache = undefined;
78
+ this.updateMemSize();
79
+ }
80
+ return image;
81
+ }
82
+ const key = formatRgba(color);
83
+ if (this.tintCache?.key === key) {
84
+ return this.tintCache.image;
85
+ }
86
+
87
+ const tintedImage = this.tintTexture(image, key);
88
+ this.tintCache = {
89
+ key,
90
+ image: tintedImage,
91
+ };
92
+ this.updateMemSize();
93
+ return tintedImage;
94
+ }
95
+
96
+ protected tintTexture(
97
+ source: ImageBitmap | HTMLCanvasElement,
98
+ color: string,
99
+ ) {
100
+ const { width, height } = source;
101
+ const canvas = document.createElement('canvas');
102
+ canvas.width = width;
103
+ canvas.height = height;
104
+ const ctx = canvas.getContext('2d');
105
+ if (ctx) {
106
+ // fill with target color
107
+ ctx.fillStyle = color;
108
+ ctx.globalCompositeOperation = 'copy';
109
+ ctx.fillRect(0, 0, width, height);
110
+
111
+ // multiply with image, resulting in non-transparent tinted image
112
+ ctx.globalCompositeOperation = 'multiply';
113
+ ctx.drawImage(source, 0, 0, width, height, 0, 0, width, height);
114
+
115
+ // apply original image alpha
116
+ ctx.globalCompositeOperation = 'destination-in';
117
+ ctx.drawImage(source, 0, 0, width, height, 0, 0, width, height);
118
+ }
119
+ return canvas;
120
+ }
121
+
122
+ get renderable(): boolean {
123
+ return this.textureSource.renderable;
124
+ }
125
+
126
+ private async onLoadRequest(): Promise<Dimensions> {
127
+ const { data } = await this.textureSource.getTextureData();
128
+ // TODO: canvas from text renderer should be able to provide the canvas directly
129
+ // instead of having to re-draw it into a new canvas...
130
+ if (data instanceof ImageData) {
131
+ const canvas = document.createElement('canvas');
132
+ canvas.width = data.width;
133
+ canvas.height = data.height;
134
+ const ctx = canvas.getContext('2d');
135
+ if (ctx) ctx.putImageData(data, 0, 0);
136
+ this.image = canvas;
137
+ return { width: data.width, height: data.height };
138
+ } else if (data instanceof ImageBitmap) {
139
+ this.image = data;
140
+ return { width: data.width, height: data.height };
141
+ }
142
+ return { width: 0, height: 0 };
143
+ }
144
+ }
@@ -0,0 +1,37 @@
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 type { QuadOptions } from '../../CoreRenderer.js';
21
+ import {
22
+ ROUNDED_RECTANGLE_SHADER_TYPE,
23
+ UnsupportedShader,
24
+ } from '../shaders/UnsupportedShader.js';
25
+
26
+ /**
27
+ * Extract `RoundedRectangle` shader radius to apply as a clipping
28
+ */
29
+ export function getRadius(quad: QuadOptions): number {
30
+ if (quad.shader instanceof UnsupportedShader) {
31
+ const shType = quad.shader.shType;
32
+ if (shType === ROUNDED_RECTANGLE_SHADER_TYPE) {
33
+ return (quad.shaderProps?.radius as number) ?? 0;
34
+ }
35
+ }
36
+ return 0;
37
+ }
@@ -0,0 +1,55 @@
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
+ export interface IParsedColor {
21
+ isWhite: boolean;
22
+ a: number;
23
+ r: number;
24
+ g: number;
25
+ b: number;
26
+ }
27
+
28
+ const WHITE: IParsedColor = {
29
+ isWhite: true,
30
+ a: 1,
31
+ r: 0xff,
32
+ g: 0xff,
33
+ b: 0xff,
34
+ };
35
+
36
+ /**
37
+ * Extract color components
38
+ */
39
+ export function parseColor(abgr: number): IParsedColor {
40
+ if (abgr === 0xffffffff) {
41
+ return WHITE;
42
+ }
43
+ const a = ((abgr >>> 24) & 0xff) / 255;
44
+ const b = (abgr >>> 16) & 0xff & 0xff;
45
+ const g = (abgr >>> 8) & 0xff & 0xff;
46
+ const r = abgr & 0xff & 0xff;
47
+ return { isWhite: false, a, r, g, b };
48
+ }
49
+
50
+ /**
51
+ * Format a parsed color into a rgba CSS color
52
+ */
53
+ export function formatRgba({ a, r, g, b }: IParsedColor): string {
54
+ return `rgba(${r},${g},${b},${a})`;
55
+ }
@@ -0,0 +1,48 @@
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 { CoreShader } from '../../CoreShader.js';
21
+
22
+ export const ROUNDED_RECTANGLE_SHADER_TYPE = 'RoundedRectangle';
23
+
24
+ export class UnsupportedShader extends CoreShader {
25
+ public shType: string;
26
+
27
+ constructor(shType: string) {
28
+ super();
29
+ this.shType = shType;
30
+ if (shType !== ROUNDED_RECTANGLE_SHADER_TYPE) {
31
+ console.warn('Unsupported shader:', shType);
32
+ }
33
+ }
34
+
35
+ bindRenderOp(): void {
36
+ // noop
37
+ }
38
+ protected bindProps(): void {
39
+ // noop
40
+ }
41
+
42
+ attach(): void {
43
+ // noop
44
+ }
45
+ detach(): void {
46
+ // noop
47
+ }
48
+ }
@@ -0,0 +1,79 @@
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 2024 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 type { Dimensions } from '../../../common/CommonTypes.js';
21
+ import { assertTruthy } from '../../../utils.js';
22
+ import type { TextureMemoryManager } from '../../TextureMemoryManager.js';
23
+ import type { WebGlContextWrapper } from '../../lib/WebGlContextWrapper.js';
24
+ import type { RenderTexture } from '../../textures/RenderTexture.js';
25
+ import { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js';
26
+
27
+ export class WebGlCoreCtxRenderTexture extends WebGlCoreCtxTexture {
28
+ declare textureSource: RenderTexture;
29
+
30
+ readonly framebuffer: WebGLFramebuffer;
31
+
32
+ constructor(
33
+ glw: WebGlContextWrapper,
34
+ memManager: TextureMemoryManager,
35
+ textureSource: RenderTexture,
36
+ ) {
37
+ super(glw, memManager, textureSource);
38
+ // Create Framebuffer object
39
+ const framebuffer = glw.createFramebuffer();
40
+ assertTruthy(framebuffer, 'Unable to create framebuffer');
41
+ this.framebuffer = framebuffer;
42
+ }
43
+
44
+ override async onLoadRequest(): Promise<Dimensions> {
45
+ const { glw, memManager } = this;
46
+ const nativeTexture = (this._nativeCtxTexture =
47
+ this.createNativeCtxTexture());
48
+ const { width, height } = this.textureSource;
49
+
50
+ // Set the dimensions of the render texture
51
+ glw.texImage2D(
52
+ 0,
53
+ glw.RGBA,
54
+ width,
55
+ height,
56
+ 0,
57
+ glw.RGBA,
58
+ glw.UNSIGNED_BYTE,
59
+ null,
60
+ );
61
+
62
+ // Update the texture memory manager
63
+ memManager.setTextureMemUse(this, width * height * 4);
64
+
65
+ // Bind the framebuffer
66
+ glw.bindFramebuffer(this.framebuffer);
67
+
68
+ // Attach the texture to the framebuffer
69
+ glw.framebufferTexture2D(glw.COLOR_ATTACHMENT0, nativeTexture, 0);
70
+
71
+ // Unbind the framebuffer
72
+ glw.bindFramebuffer(null);
73
+
74
+ return {
75
+ width,
76
+ height,
77
+ };
78
+ }
79
+ }