@lightningjs/renderer 0.8.2 → 0.8.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.
- package/dist/src/core/CoreShaderManager.d.ts +2 -0
- package/dist/src/core/CoreShaderManager.js +8 -0
- package/dist/src/core/CoreShaderManager.js.map +1 -1
- package/dist/src/core/Stage.d.ts +3 -2
- package/dist/src/core/Stage.js +15 -5
- package/dist/src/core/Stage.js.map +1 -1
- package/dist/src/core/lib/ImageWorker.js +1 -1
- package/dist/src/core/renderers/CoreRenderer.d.ts +21 -3
- package/dist/src/core/renderers/CoreRenderer.js +13 -2
- package/dist/src/core/renderers/CoreRenderer.js.map +1 -1
- package/dist/src/core/renderers/canvas/CanvasCoreRenderer.d.ts +16 -0
- package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js +155 -0
- package/dist/src/core/renderers/canvas/CanvasCoreRenderer.js.map +1 -0
- package/dist/src/core/renderers/canvas/CanvasCoreTexture.d.ts +17 -0
- package/dist/src/core/renderers/canvas/CanvasCoreTexture.js +122 -0
- package/dist/src/core/renderers/canvas/CanvasCoreTexture.js.map +1 -0
- package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.d.ts +5 -0
- package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js +32 -0
- package/dist/src/core/renderers/canvas/internal/C2DShaderUtils.js.map +1 -0
- package/dist/src/core/renderers/canvas/internal/ColorUtils.d.ts +15 -0
- package/dist/src/core/renderers/canvas/internal/ColorUtils.js +45 -0
- package/dist/src/core/renderers/canvas/internal/ColorUtils.js.map +1 -0
- package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.d.ts +10 -0
- package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js +43 -0
- package/dist/src/core/renderers/canvas/shaders/UnsupportedShader.js.map +1 -0
- package/dist/src/core/renderers/webgl/WebGlCoreRenderer.d.ts +2 -20
- package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js +3 -15
- package/dist/src/core/renderers/webgl/WebGlCoreRenderer.js.map +1 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js +3 -1
- package/dist/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.js.map +1 -1
- package/dist/src/core/textures/ImageTexture.js +3 -0
- package/dist/src/core/textures/ImageTexture.js.map +1 -1
- package/dist/src/main-api/Inspector.js +7 -1
- package/dist/src/main-api/Inspector.js.map +1 -1
- package/dist/src/main-api/RendererMain.d.ts +4 -0
- package/dist/src/main-api/RendererMain.js +1 -0
- package/dist/src/main-api/RendererMain.js.map +1 -1
- package/dist/src/render-drivers/main/MainCoreDriver.js +1 -0
- package/dist/src/render-drivers/main/MainCoreDriver.js.map +1 -1
- package/dist/src/render-drivers/threadx/worker/renderer.js +1 -0
- package/dist/src/render-drivers/threadx/worker/renderer.js.map +1 -1
- package/dist/src/render-drivers/utils.js +1 -1
- package/dist/src/render-drivers/utils.js.map +1 -1
- package/dist/src/utils.d.ts +12 -6
- package/dist/src/utils.js +19 -9
- package/dist/src/utils.js.map +1 -1
- package/dist/tsconfig.dist.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/core/CoreShaderManager.ts +10 -0
- package/src/core/Stage.ts +18 -5
- package/src/core/lib/ImageWorker.ts +1 -1
- package/src/core/renderers/CoreRenderer.ts +32 -6
- package/src/core/renderers/canvas/CanvasCoreRenderer.ts +178 -0
- package/src/core/renderers/canvas/CanvasCoreTexture.ts +138 -0
- package/src/core/renderers/canvas/internal/C2DShaderUtils.ts +34 -0
- package/src/core/renderers/canvas/internal/ColorUtils.ts +55 -0
- package/src/core/renderers/canvas/shaders/UnsupportedShader.ts +49 -0
- package/src/core/renderers/webgl/WebGlCoreRenderer.ts +8 -38
- package/src/core/text-rendering/renderers/SdfTextRenderer/SdfTextRenderer.ts +3 -1
- package/src/core/textures/ImageTexture.ts +3 -0
- package/src/main-api/Inspector.ts +6 -1
- package/src/main-api/RendererMain.ts +7 -0
- package/src/render-drivers/main/MainCoreDriver.ts +1 -0
- package/src/render-drivers/threadx/worker/renderer.ts +1 -0
- package/src/render-drivers/utils.ts +1 -1
- package/src/utils.ts +21 -9
|
@@ -0,0 +1,178 @@
|
|
|
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 { CoreShaderManager } from "../../CoreShaderManager.js";
|
|
21
|
+
import { getRgbaComponents, type RGBA } from "../../lib/utils.js";
|
|
22
|
+
import { SubTexture } from "../../textures/SubTexture.js";
|
|
23
|
+
import type { Texture } from "../../textures/Texture.js";
|
|
24
|
+
import type { CoreContextTexture } from "../CoreContextTexture.js";
|
|
25
|
+
import { CoreRenderer, type CoreRendererOptions, type QuadOptions } from "../CoreRenderer.js";
|
|
26
|
+
import { CanvasCoreTexture } from "./CanvasCoreTexture.js";
|
|
27
|
+
import { getRadius } from "./internal/C2DShaderUtils.js";
|
|
28
|
+
import { formatRgba, parseColor, type IParsedColor } from "./internal/ColorUtils.js";
|
|
29
|
+
|
|
30
|
+
export class CanvasCoreRenderer extends CoreRenderer {
|
|
31
|
+
|
|
32
|
+
private context: CanvasRenderingContext2D;
|
|
33
|
+
private canvas: HTMLCanvasElement;
|
|
34
|
+
private pixelRatio: number;
|
|
35
|
+
private clearColor: RGBA | undefined;
|
|
36
|
+
|
|
37
|
+
constructor(options: CoreRendererOptions) {
|
|
38
|
+
super(options);
|
|
39
|
+
|
|
40
|
+
this.mode = 'canvas';
|
|
41
|
+
this.shManager.renderer = this;
|
|
42
|
+
|
|
43
|
+
const { canvas, pixelRatio, clearColor } = options;
|
|
44
|
+
this.canvas = canvas as HTMLCanvasElement;
|
|
45
|
+
this.context = canvas.getContext('2d') as CanvasRenderingContext2D;
|
|
46
|
+
this.pixelRatio = pixelRatio;
|
|
47
|
+
this.clearColor = clearColor ? getRgbaComponents(clearColor) : undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
reset(): void {
|
|
51
|
+
// eslint-disable-next-line no-self-assign
|
|
52
|
+
this.canvas.width = this.canvas.width; // quick reset canvas
|
|
53
|
+
|
|
54
|
+
const ctx = this.context;
|
|
55
|
+
|
|
56
|
+
if (this.clearColor) {
|
|
57
|
+
const [r, g, b, a] = this.clearColor;
|
|
58
|
+
ctx.fillStyle = `rgba(${r}, ${g}, ${b}, ${a / 255})`;
|
|
59
|
+
ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
ctx.scale(this.pixelRatio, this.pixelRatio);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
render(): void {
|
|
66
|
+
// noop
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
addQuad(quad: QuadOptions): void {
|
|
70
|
+
const ctx = this.context;
|
|
71
|
+
const {
|
|
72
|
+
tx, ty, width, height, alpha, colorTl, colorTr, colorBr, ta, tb, tc, td, clippingRect
|
|
73
|
+
} = quad;
|
|
74
|
+
let texture = quad.texture;
|
|
75
|
+
let ctxTexture: CanvasCoreTexture | undefined = undefined;
|
|
76
|
+
let frame: { x: number, y: number, width: number, height: number } | undefined;
|
|
77
|
+
|
|
78
|
+
if (texture) {
|
|
79
|
+
if (texture instanceof SubTexture) {
|
|
80
|
+
frame = texture.props;
|
|
81
|
+
texture = texture.parentTexture;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
ctxTexture = this.txManager.getCtxTexture(texture) as CanvasCoreTexture;
|
|
85
|
+
if (texture.state === 'freed') {
|
|
86
|
+
ctxTexture.load();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (texture.state !== 'loaded' || !ctxTexture.hasImage()) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const color = parseColor(colorTl);
|
|
95
|
+
const hasTransform = ta !== 1;
|
|
96
|
+
const hasClipping = clippingRect.width !== 0 && clippingRect.height !== 0;
|
|
97
|
+
const hasGradient = colorTl !== colorTr || colorTl !== colorBr;
|
|
98
|
+
const radius = quad.shader ? getRadius(quad) : 0;
|
|
99
|
+
|
|
100
|
+
if (hasTransform || hasClipping || radius) {
|
|
101
|
+
ctx.save();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (hasClipping) {
|
|
105
|
+
const path = new Path2D();
|
|
106
|
+
const { x, y, width, height } = clippingRect;
|
|
107
|
+
path.rect(x, y, width, height);
|
|
108
|
+
ctx.clip(path);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (hasTransform) {
|
|
112
|
+
// Quad transform:
|
|
113
|
+
// | ta tb tx |
|
|
114
|
+
// | tc td ty |
|
|
115
|
+
// | 0 0 1 |
|
|
116
|
+
// C2D transform:
|
|
117
|
+
// | a c e |
|
|
118
|
+
// | b d f |
|
|
119
|
+
// | 0 0 1 |
|
|
120
|
+
const scale = this.pixelRatio;
|
|
121
|
+
ctx.setTransform(ta, tc, tb, td, tx * scale, ty * scale);
|
|
122
|
+
ctx.scale(scale, scale);
|
|
123
|
+
ctx.translate(-tx, -ty);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (radius) {
|
|
127
|
+
const path = new Path2D();
|
|
128
|
+
path.roundRect(tx, ty, width, height, radius);
|
|
129
|
+
ctx.clip(path);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (ctxTexture) {
|
|
133
|
+
const image = ctxTexture.getImage(color);
|
|
134
|
+
ctx.globalAlpha = alpha;
|
|
135
|
+
if (frame) {
|
|
136
|
+
ctx.drawImage(image, frame.x, frame.y, frame.width, frame.height, tx, ty, width, height);
|
|
137
|
+
} else {
|
|
138
|
+
ctx.drawImage(image, tx, ty, width, height);
|
|
139
|
+
}
|
|
140
|
+
ctx.globalAlpha = 1;
|
|
141
|
+
} else if (hasGradient) {
|
|
142
|
+
let endX: number = tx;
|
|
143
|
+
let endY: number = ty;
|
|
144
|
+
let endColor: IParsedColor;
|
|
145
|
+
if (colorTl === colorTr) {
|
|
146
|
+
// vertical
|
|
147
|
+
endX = tx;
|
|
148
|
+
endY = ty + height;
|
|
149
|
+
endColor = parseColor(colorBr);
|
|
150
|
+
} else {
|
|
151
|
+
// horizontal
|
|
152
|
+
endX = tx + width;
|
|
153
|
+
endY = ty;
|
|
154
|
+
endColor = parseColor(colorTr);
|
|
155
|
+
}
|
|
156
|
+
const gradient = ctx.createLinearGradient(tx, ty, endX, endY);
|
|
157
|
+
gradient.addColorStop(0, formatRgba(color));
|
|
158
|
+
gradient.addColorStop(1, formatRgba(endColor));
|
|
159
|
+
ctx.fillStyle = gradient;
|
|
160
|
+
ctx.fillRect(tx, ty, width, height);
|
|
161
|
+
} else {
|
|
162
|
+
ctx.fillStyle = formatRgba(color);
|
|
163
|
+
ctx.fillRect(tx, ty, width, height);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (hasTransform || hasClipping || radius) {
|
|
167
|
+
ctx.restore();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
createCtxTexture(textureSource: Texture): CoreContextTexture {
|
|
172
|
+
return new CanvasCoreTexture(this.txMemManager, textureSource);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
getShaderManager(): CoreShaderManager {
|
|
176
|
+
return this.shManager;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
|
|
27
|
+
protected image: ImageBitmap | HTMLCanvasElement | undefined;
|
|
28
|
+
protected tintCache: {
|
|
29
|
+
key: string;
|
|
30
|
+
image: HTMLCanvasElement
|
|
31
|
+
} | undefined;
|
|
32
|
+
|
|
33
|
+
load(): void {
|
|
34
|
+
if (this.textureSource.state !== 'freed') {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
this.textureSource.setState('loading');
|
|
38
|
+
this.onLoadRequest().then((size) => {
|
|
39
|
+
this.textureSource.setState('loaded', size);
|
|
40
|
+
this.updateMemSize();
|
|
41
|
+
}).catch((err) => {
|
|
42
|
+
this.textureSource.setState('failed', err as Error);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
free(): void {
|
|
47
|
+
this.image = undefined;
|
|
48
|
+
this.tintCache = undefined;
|
|
49
|
+
this.textureSource.setState('freed');
|
|
50
|
+
this.memManager.setTextureMemUse(this, 0);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
updateMemSize(): void {
|
|
54
|
+
// Counting memory usage for:
|
|
55
|
+
// - main image
|
|
56
|
+
// - tinted image
|
|
57
|
+
const mult = this.tintCache ? 8 : 4;
|
|
58
|
+
if (this.textureSource.dimensions) {
|
|
59
|
+
const { width, height } = this.textureSource.dimensions;
|
|
60
|
+
this.memManager.setTextureMemUse(this, width * height * mult);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
hasImage(): boolean {
|
|
65
|
+
return this.image !== undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
getImage(color: IParsedColor): ImageBitmap | HTMLCanvasElement {
|
|
69
|
+
const image = this.image;
|
|
70
|
+
assertTruthy(image, 'Attempt to get unloaded image texture');
|
|
71
|
+
|
|
72
|
+
if (color.isWhite) {
|
|
73
|
+
if (this.tintCache) {
|
|
74
|
+
this.tintCache = undefined;
|
|
75
|
+
this.updateMemSize();
|
|
76
|
+
}
|
|
77
|
+
return image;
|
|
78
|
+
}
|
|
79
|
+
const key = formatRgba(color);
|
|
80
|
+
if (this.tintCache?.key === key) {
|
|
81
|
+
return this.tintCache.image;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const tintedImage = this.tintTexture(image, key);
|
|
85
|
+
this.tintCache = {
|
|
86
|
+
key,
|
|
87
|
+
image: tintedImage
|
|
88
|
+
}
|
|
89
|
+
this.updateMemSize();
|
|
90
|
+
return tintedImage;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
protected tintTexture(source: ImageBitmap | HTMLCanvasElement, color: string) {
|
|
94
|
+
const { width, height } = source;
|
|
95
|
+
const canvas = document.createElement('canvas');
|
|
96
|
+
canvas.width = width;
|
|
97
|
+
canvas.height = height;
|
|
98
|
+
const ctx = canvas.getContext('2d');
|
|
99
|
+
if (ctx) {
|
|
100
|
+
// fill with target color
|
|
101
|
+
ctx.fillStyle = color;
|
|
102
|
+
ctx.globalCompositeOperation = 'copy';
|
|
103
|
+
ctx.fillRect(0, 0, width, height);
|
|
104
|
+
|
|
105
|
+
// multiply with image, resulting in non-transparent tinted image
|
|
106
|
+
ctx.globalCompositeOperation = 'multiply';
|
|
107
|
+
ctx.drawImage(source, 0, 0, width, height, 0, 0, width, height);
|
|
108
|
+
|
|
109
|
+
// apply original image alpha
|
|
110
|
+
ctx.globalCompositeOperation = 'destination-in';
|
|
111
|
+
ctx.drawImage(source, 0, 0, width, height, 0, 0, width, height);
|
|
112
|
+
}
|
|
113
|
+
return canvas;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
get renderable(): boolean {
|
|
117
|
+
return this.textureSource.renderable;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private async onLoadRequest(): Promise<Dimensions> {
|
|
121
|
+
const { data } = await this.textureSource.getTextureData();
|
|
122
|
+
// TODO: canvas from text renderer should be able to provide the canvas directly
|
|
123
|
+
// instead of having to re-draw it into a new canvas...
|
|
124
|
+
if (data instanceof ImageData) {
|
|
125
|
+
const canvas = document.createElement('canvas');
|
|
126
|
+
canvas.width = data.width;
|
|
127
|
+
canvas.height = data.height;
|
|
128
|
+
const ctx = canvas.getContext('2d');
|
|
129
|
+
if (ctx) ctx.putImageData(data, 0, 0);
|
|
130
|
+
this.image = canvas;
|
|
131
|
+
return { width: data.width, height: data.height };
|
|
132
|
+
} else if (data instanceof ImageBitmap) {
|
|
133
|
+
this.image = data;
|
|
134
|
+
return { width: data.width, height: data.height };
|
|
135
|
+
}
|
|
136
|
+
return { width: 0, height: 0 };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
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 { ROUNDED_RECTANGLE_SHADER_TYPE, UnsupportedShader } from "../shaders/UnsupportedShader.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Extract `RoundedRectangle` shader radius to apply as a clipping
|
|
25
|
+
*/
|
|
26
|
+
export function getRadius(quad: QuadOptions): number {
|
|
27
|
+
if (quad.shader instanceof UnsupportedShader) {
|
|
28
|
+
const shType = quad.shader.shType;
|
|
29
|
+
if (shType === ROUNDED_RECTANGLE_SHADER_TYPE) {
|
|
30
|
+
return quad.shaderProps?.radius as number ?? 0;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
|
@@ -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,49 @@
|
|
|
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
|
+
|
|
26
|
+
public shType: string;
|
|
27
|
+
|
|
28
|
+
constructor(shType: string) {
|
|
29
|
+
super();
|
|
30
|
+
this.shType = shType;
|
|
31
|
+
if (shType !== ROUNDED_RECTANGLE_SHADER_TYPE) {
|
|
32
|
+
console.warn('Unsupported shader:', shType);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
bindRenderOp(): void {
|
|
37
|
+
// noop
|
|
38
|
+
}
|
|
39
|
+
protected bindProps(): void {
|
|
40
|
+
// noop
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
attach(): void {
|
|
44
|
+
// noop
|
|
45
|
+
}
|
|
46
|
+
detach(): void {
|
|
47
|
+
// noop
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -21,9 +21,8 @@ import {
|
|
|
21
21
|
assertTruthy,
|
|
22
22
|
createWebGLContext,
|
|
23
23
|
hasOwn,
|
|
24
|
-
mergeColorAlphaPremultiplied,
|
|
25
24
|
} from '../../../utils.js';
|
|
26
|
-
import { CoreRenderer, type QuadOptions } from '../CoreRenderer.js';
|
|
25
|
+
import { CoreRenderer, type CoreRendererOptions, type QuadOptions } from '../CoreRenderer.js';
|
|
27
26
|
import { WebGlCoreRenderOp } from './WebGlCoreRenderOp.js';
|
|
28
27
|
import type { CoreContextTexture } from '../CoreContextTexture.js';
|
|
29
28
|
import {
|
|
@@ -36,43 +35,23 @@ import {
|
|
|
36
35
|
import { WebGlCoreCtxTexture } from './WebGlCoreCtxTexture.js';
|
|
37
36
|
import { Texture } from '../../textures/Texture.js';
|
|
38
37
|
import { ColorTexture } from '../../textures/ColorTexture.js';
|
|
39
|
-
import type { Stage } from '../../Stage.js';
|
|
40
38
|
import { SubTexture } from '../../textures/SubTexture.js';
|
|
41
39
|
import { WebGlCoreCtxSubTexture } from './WebGlCoreCtxSubTexture.js';
|
|
42
|
-
import type {
|
|
43
|
-
CoreTextureManager,
|
|
44
|
-
TextureOptions,
|
|
45
|
-
} from '../../CoreTextureManager.js';
|
|
46
40
|
import { CoreShaderManager } from '../../CoreShaderManager.js';
|
|
47
|
-
import type { CoreShader } from '../CoreShader.js';
|
|
48
41
|
import { BufferCollection } from './internal/BufferCollection.js';
|
|
49
42
|
import {
|
|
50
43
|
compareRect,
|
|
51
44
|
getNormalizedRgbaComponents,
|
|
52
|
-
type Rect,
|
|
53
45
|
type RectWithValid,
|
|
54
46
|
} from '../../lib/utils.js';
|
|
55
47
|
import type { Dimensions } from '../../../common/CommonTypes.js';
|
|
56
48
|
import { WebGlCoreShader } from './WebGlCoreShader.js';
|
|
57
|
-
import { RoundedRectangle } from './shaders/RoundedRectangle.js';
|
|
58
|
-
import { ContextSpy } from '../../lib/ContextSpy.js';
|
|
59
49
|
import { WebGlContextWrapper } from '../../lib/WebGlContextWrapper.js';
|
|
60
|
-
import type { TextureMemoryManager } from '../../TextureMemoryManager.js';
|
|
61
50
|
|
|
62
51
|
const WORDS_PER_QUAD = 24;
|
|
63
|
-
const BYTES_PER_QUAD = WORDS_PER_QUAD * 4;
|
|
64
|
-
|
|
65
|
-
export
|
|
66
|
-
stage: Stage;
|
|
67
|
-
canvas: HTMLCanvasElement | OffscreenCanvas;
|
|
68
|
-
pixelRatio: number;
|
|
69
|
-
txManager: CoreTextureManager;
|
|
70
|
-
txMemManager: TextureMemoryManager;
|
|
71
|
-
shManager: CoreShaderManager;
|
|
72
|
-
clearColor: number;
|
|
73
|
-
bufferMemory: number;
|
|
74
|
-
contextSpy: ContextSpy | null;
|
|
75
|
-
}
|
|
52
|
+
// const BYTES_PER_QUAD = WORDS_PER_QUAD * 4;
|
|
53
|
+
|
|
54
|
+
export type WebGlCoreRendererOptions = CoreRendererOptions;
|
|
76
55
|
|
|
77
56
|
interface CoreWebGlSystem {
|
|
78
57
|
parameters: CoreWebGlParameters;
|
|
@@ -84,14 +63,6 @@ export class WebGlCoreRenderer extends CoreRenderer {
|
|
|
84
63
|
glw: WebGlContextWrapper;
|
|
85
64
|
system: CoreWebGlSystem;
|
|
86
65
|
|
|
87
|
-
//// Core Managers
|
|
88
|
-
txManager: CoreTextureManager;
|
|
89
|
-
txMemManager: TextureMemoryManager;
|
|
90
|
-
shManager: CoreShaderManager;
|
|
91
|
-
|
|
92
|
-
//// Options
|
|
93
|
-
options: Required<WebGlCoreRendererOptions>;
|
|
94
|
-
|
|
95
66
|
//// Persistent data
|
|
96
67
|
quadBuffer: ArrayBuffer = new ArrayBuffer(1024 * 1024 * 4);
|
|
97
68
|
fQuadBuffer: Float32Array = new Float32Array(this.quadBuffer);
|
|
@@ -113,12 +84,11 @@ export class WebGlCoreRenderer extends CoreRenderer {
|
|
|
113
84
|
defaultTexture: Texture;
|
|
114
85
|
|
|
115
86
|
constructor(options: WebGlCoreRendererOptions) {
|
|
116
|
-
super(options
|
|
87
|
+
super(options);
|
|
88
|
+
this.mode = 'webgl';
|
|
89
|
+
|
|
117
90
|
const { canvas, clearColor, bufferMemory } = options;
|
|
118
|
-
|
|
119
|
-
this.txManager = options.txManager;
|
|
120
|
-
this.txMemManager = options.txMemManager;
|
|
121
|
-
this.shManager = options.shManager;
|
|
91
|
+
|
|
122
92
|
this.defaultTexture = new ColorTexture(this.txManager);
|
|
123
93
|
// When the default texture is loaded, request a render in case the
|
|
124
94
|
// RAF is paused. Fixes: https://github.com/lightning-js/renderer/issues/123
|
|
@@ -56,6 +56,7 @@ import type {
|
|
|
56
56
|
import type { WebGlCoreCtxTexture } from '../../../renderers/webgl/WebGlCoreCtxTexture.js';
|
|
57
57
|
import { EventEmitter } from '../../../../common/EventEmitter.js';
|
|
58
58
|
import type { Matrix3d } from '../../../lib/Matrix3d.js';
|
|
59
|
+
import { WebGlCoreRenderer } from '../../../renderers/webgl/WebGlCoreRenderer.js';
|
|
59
60
|
|
|
60
61
|
declare module '../TextRenderer.js' {
|
|
61
62
|
interface TextRendererMap {
|
|
@@ -580,7 +581,8 @@ export class SdfTextRenderer extends TextRenderer<SdfTextRendererState> {
|
|
|
580
581
|
return;
|
|
581
582
|
}
|
|
582
583
|
|
|
583
|
-
const
|
|
584
|
+
const renderer: WebGlCoreRenderer = this.stage.renderer as WebGlCoreRenderer;
|
|
585
|
+
assertTruthy(renderer instanceof WebGlCoreRenderer);
|
|
584
586
|
|
|
585
587
|
const { fontSize, color, contain, scrollable, zIndex, debug } = state.props;
|
|
586
588
|
|
|
@@ -117,6 +117,9 @@ export class ImageTexture extends Texture {
|
|
|
117
117
|
};
|
|
118
118
|
} else {
|
|
119
119
|
const img = new Image();
|
|
120
|
+
if (!(src.substr(0, 5) === 'data:')) {
|
|
121
|
+
img.crossOrigin = 'Anonymous';
|
|
122
|
+
}
|
|
120
123
|
img.src = src;
|
|
121
124
|
await new Promise<void>((resolve, reject) => {
|
|
122
125
|
img.onload = () => resolve();
|
|
@@ -393,7 +393,12 @@ export class Inspector {
|
|
|
393
393
|
if (property === 'data') {
|
|
394
394
|
for (const key in value) {
|
|
395
395
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
396
|
-
|
|
396
|
+
const keyValue: unknown = value[key];
|
|
397
|
+
if (keyValue === undefined) {
|
|
398
|
+
div.removeAttribute(`data-${key}`);
|
|
399
|
+
} else {
|
|
400
|
+
div.setAttribute(`data-${key}`, String(keyValue));
|
|
401
|
+
}
|
|
397
402
|
}
|
|
398
403
|
return;
|
|
399
404
|
}
|
|
@@ -41,6 +41,7 @@ import { EventEmitter } from '../common/EventEmitter.js';
|
|
|
41
41
|
import { Inspector } from './Inspector.js';
|
|
42
42
|
import { santizeCustomDataMap } from '../render-drivers/utils.js';
|
|
43
43
|
import { isProductionEnvironment } from '../utils.js';
|
|
44
|
+
import type { StageOptions } from '../core/Stage.js';
|
|
44
45
|
|
|
45
46
|
/**
|
|
46
47
|
* An immutable reference to a specific Texture type
|
|
@@ -270,6 +271,11 @@ export interface RendererMainSettings {
|
|
|
270
271
|
* @defaultValue `false` (disabled)
|
|
271
272
|
*/
|
|
272
273
|
enableInspector?: boolean;
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Renderer mode
|
|
277
|
+
*/
|
|
278
|
+
renderMode?: 'webgl' | 'canvas';
|
|
273
279
|
}
|
|
274
280
|
|
|
275
281
|
/**
|
|
@@ -344,6 +350,7 @@ export class RendererMain extends EventEmitter {
|
|
|
344
350
|
settings.numImageWorkers !== undefined ? settings.numImageWorkers : 2,
|
|
345
351
|
enableContextSpy: settings.enableContextSpy ?? false,
|
|
346
352
|
enableInspector: settings.enableInspector ?? false,
|
|
353
|
+
renderMode: settings.renderMode ?? 'webgl'
|
|
347
354
|
};
|
|
348
355
|
this.settings = resolvedSettings;
|
|
349
356
|
|
|
@@ -64,6 +64,7 @@ export class MainCoreDriver implements ICoreDriver {
|
|
|
64
64
|
fpsUpdateInterval: rendererSettings.fpsUpdateInterval,
|
|
65
65
|
enableContextSpy: rendererSettings.enableContextSpy,
|
|
66
66
|
numImageWorkers: rendererSettings.numImageWorkers,
|
|
67
|
+
renderMode: rendererSettings.renderMode,
|
|
67
68
|
debug: {
|
|
68
69
|
monitorTextureCache: false,
|
|
69
70
|
},
|
|
@@ -58,7 +58,7 @@ export async function loadCoreExtension(
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
export function santizeCustomDataMap(d: CustomDataMap): CustomDataMap {
|
|
61
|
-
const validTypes = { boolean: true, string: true, number: true };
|
|
61
|
+
const validTypes = { boolean: true, string: true, number: true, undefined: true };
|
|
62
62
|
|
|
63
63
|
const keys = Object.keys(d);
|
|
64
64
|
for (let i = 0; i < keys.length; i++) {
|