Truly independent web browser
Compositor: Upload WebGL luminance textures from images and canvases
sideshowbarker247dc240e672
diff --git a/Libraries/LibCompositing/WebGL/TextureUpload.cpp b/Libraries/LibCompositing/WebGL/TextureUpload.cppindex 6c12f639d1f..26d32041d8a 100644--- a/Libraries/LibCompositing/WebGL/TextureUpload.cpp+++ b/Libraries/LibCompositing/WebGL/TextureUpload.cpp@@ -49,7 +49,15 @@ Optional<Gfx::ExportFormat> texture_export_format(GLenum format, GLenum type)case GL_LUMINANCE:switch (type) {case GL_UNSIGNED_BYTE:- return Gfx::ExportFormat::Gray8;+ return Gfx::ExportFormat::Luminance8;+ default:+ break;+ }+ break;+ case GL_LUMINANCE_ALPHA:+ switch (type) {+ case GL_UNSIGNED_BYTE:+ return Gfx::ExportFormat::LuminanceAlpha88;default:break;}diff --git a/Libraries/LibGfx/ExportFormat.h b/Libraries/LibGfx/ExportFormat.hindex be3653c0f6d..946b230200b 100644--- a/Libraries/LibGfx/ExportFormat.h+++ b/Libraries/LibGfx/ExportFormat.h@@ -11,7 +11,8 @@namespace Gfx {#define ENUMERATE_EXPORT_FORMATS(X) \- X(Gray8) \+ X(Luminance8) \+ X(LuminanceAlpha88) \X(Alpha8) \X(RGB565) \X(RGBA5551) \diff --git a/Services/Compositor/BitmapExport.cpp b/Services/Compositor/BitmapExport.cppindex f783b1c72f5..3e07459e39d 100644--- a/Services/Compositor/BitmapExport.cpp+++ b/Services/Compositor/BitmapExport.cpp@@ -32,9 +32,10 @@ StringView export_format_name(ExportFormat format)static int bytes_per_pixel_for_export_format(ExportFormat format){switch (format) {- case ExportFormat::Gray8:+ case ExportFormat::Luminance8:case ExportFormat::Alpha8:return 1;+ case ExportFormat::LuminanceAlpha88:case ExportFormat::RGB565:case ExportFormat::RGBA5551:case ExportFormat::RGBA4444:@@ -51,14 +52,14 @@ static int bytes_per_pixel_for_export_format(ExportFormat format)static SkColorType export_format_to_skia_color_type(ExportFormat format){switch (format) {- case ExportFormat::Gray8:- return SkColorType::kGray_8_SkColorType;case ExportFormat::Alpha8:return SkColorType::kAlpha_8_SkColorType;case ExportFormat::RGB565:return SkColorType::kRGB_565_SkColorType;case ExportFormat::RGBA4444:return SkColorType::kARGB_4444_SkColorType;+ case ExportFormat::Luminance8:+ case ExportFormat::LuminanceAlpha88:case ExportFormat::RGBA5551:case ExportFormat::RGB888:// These need to be converted manually because Skia has no color type for them.@@ -142,8 +143,9 @@ ErrorOr<BitmapExportResult> export_bitmap_to_byte_buffer(raw_buffer[buffer_offset + 2] = pixel.blue();}}- } else if (format == ExportFormat::RGBA5551) {- // 16 bit RGBA with 1 bit of alpha is not supported by Skia, so we draw into 32 bit RGBA and pack that.+ } else if (format == ExportFormat::RGBA5551 || format == ExportFormat::Luminance8 || format == ExportFormat::LuminanceAlpha88) {+ // Skia has no color type for 16 bit RGBA with 1 bit of alpha, or for a luminance that's the red channel (its+ // gray color type weighs all three channels), so we draw into 32 bit RGBA and pack that.Checked<size_t> rgba_pitch = width;rgba_pitch *= 4;if (rgba_pitch.has_overflow() || Checked<size_t>::multiplication_would_overflow(rgba_pitch.value(), height))@@ -156,8 +158,22 @@ ErrorOr<BitmapExportResult> export_bitmap_to_byte_buffer(auto* destination_row = buffer.data() + (y * buffer_pitch.value());for (auto x = 0; x < width; x++) {auto const* source = source_row + (x * 4ull);- u16 pixel = ((source[0] >> 3) << 11) | ((source[1] >> 3) << 6) | ((source[2] >> 3) << 1) | (source[3] >> 7);- ByteReader::store(destination_row + (x * 2ull), pixel);+ switch (format) {+ case ExportFormat::RGBA5551: {+ u16 pixel = ((source[0] >> 3) << 11) | ((source[1] >> 3) << 6) | ((source[2] >> 3) << 1) | (source[3] >> 7);+ ByteReader::store(destination_row + (x * 2ull), pixel);+ break;+ }+ case ExportFormat::Luminance8:+ destination_row[x] = source[0];+ break;+ case ExportFormat::LuminanceAlpha88:+ destination_row[(x * 2ull) + 0] = source[0];+ destination_row[(x * 2ull) + 1] = source[3];+ break;+ default:+ VERIFY_NOT_REACHED();+ }}}} else {diff --git a/Tests/Compositor/TestBitmapExport.cpp b/Tests/Compositor/TestBitmapExport.cppindex 518b71a124e..9f6cc6b1621 100644--- a/Tests/Compositor/TestBitmapExport.cpp+++ b/Tests/Compositor/TestBitmapExport.cpp@@ -50,25 +50,41 @@ TEST_CASE(export_bitmap_to_byte_buffer)alpha_bitmap_formats,{ Premultiplied::No },{ 0x00FFFFFF, 0x55FFFFFF, 0xAAFFFFFF, 0xFFFFFFFF },- Gfx::ExportFormat::Gray8,- { Premultiplied::No, Premultiplied::Yes },+ Gfx::ExportFormat::Luminance8,+ { Premultiplied::No },+ { 0x00, 0xFF, 0xFF, 0xFF },+ },+ {+ alpha_bitmap_formats,+ { Premultiplied::No },+ { 0x00FFFFFF, 0x55FFFFFF, 0xAAFFFFFF, 0xFFFFFFFF },+ Gfx::ExportFormat::Luminance8,+ { Premultiplied::Yes },{ 0x00, 0x55, 0xAA, 0xFF },},{non_alpha_bitmap_formats,- { Premultiplied::No },- { 0x00000000, 0x55555555, 0xAAAAAAAA, 0xFFFFFFFF },- Gfx::ExportFormat::Gray8,{ Premultiplied::No, Premultiplied::Yes },- { 0x00, 0x55, 0xAA, 0xFF },+ { 0x00112233, 0x44556677, 0x8899AABB, 0xCCDDEEFF },+ Gfx::ExportFormat::Luminance8,+ { Premultiplied::No, Premultiplied::Yes },+ { 0x11, 0x55, 0x99, 0xDD },},{- all_bitmap_formats,- { Premultiplied::Yes },- { 0x00000000, 0x55555555, 0xAAAAAAAA, 0xFFFFFFFF },- Gfx::ExportFormat::Gray8,+ non_alpha_bitmap_formats,{ Premultiplied::No, Premultiplied::Yes },- { 0x00, 0x55, 0xAA, 0xFF },+ { 0x00112233, 0x44556677, 0x8899AABB, 0xCCDDEEFF },+ Gfx::ExportFormat::LuminanceAlpha88,+ { Premultiplied::No, Premultiplied::Yes },+ { 0x11, 0xFF, 0x55, 0xFF, 0x99, 0xFF, 0xDD, 0xFF },+ },+ {+ alpha_bitmap_formats,+ { Premultiplied::No, Premultiplied::Yes },+ { 0x00000000, 0xFF112233, 0x00000000, 0xFFDDEEFF },+ Gfx::ExportFormat::LuminanceAlpha88,+ { Premultiplied::No, Premultiplied::Yes },+ { 0x00, 0x00, 0x11, 0xFF, 0x00, 0x00, 0xDD, 0xFF },},{alpha_bitmap_formats,diff --git a/Tests/LibWeb/Text/expected/WebGL/tex-image-2d-canvas-luminance-formats.txt b/Tests/LibWeb/Text/expected/WebGL/tex-image-2d-canvas-luminance-formats.txtnew file mode 100644index 00000000000..ee7724d05ad--- /dev/null+++ b/Tests/LibWeb/Text/expected/WebGL/tex-image-2d-canvas-luminance-formats.txt@@ -0,0 +1,3 @@+LUMINANCE: white 255,255,255,255 | orange 200,200,200,255 | half-transparent white 255,255,255,255 | transparent 0,0,0,255 | error 0+LUMINANCE_ALPHA: white 255,255,255,255 | orange 200,200,200,255 | half-transparent white 255,255,255,128 | transparent 0,0,0,0 | error 0+ALPHA: white 0,0,0,255 | orange 0,0,0,255 | half-transparent white 0,0,0,128 | transparent 0,0,0,0 | error 0diff --git a/Tests/LibWeb/Text/input/WebGL/tex-image-2d-canvas-luminance-formats.html b/Tests/LibWeb/Text/input/WebGL/tex-image-2d-canvas-luminance-formats.htmlnew file mode 100644index 00000000000..f67017beb74--- /dev/null+++ b/Tests/LibWeb/Text/input/WebGL/tex-image-2d-canvas-luminance-formats.html@@ -0,0 +1,70 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // Uploads a 2D canvas with an opaque white, an opaque orange, a half-transparent white and a transparent pixel as a+ // texture of the format, and reads back what sampling the texture gives for each of them.+ function sampledTexels(gl, format) {+ const source = document.createElement("canvas");+ source.width = 4;+ source.height = 1;+ const context = source.getContext("2d");+ context.fillStyle = "rgb(255, 255, 255)";+ context.fillRect(0, 0, 1, 1);+ context.fillStyle = "rgb(200, 100, 50)";+ context.fillRect(1, 0, 1, 1);+ context.fillStyle = "rgba(255, 255, 255, 0.5)";+ context.fillRect(2, 0, 1, 1);++ const texture = gl.createTexture();+ gl.bindTexture(gl.TEXTURE_2D, texture);+ gl.texImage2D(gl.TEXTURE_2D, 0, format, format, gl.UNSIGNED_BYTE, source);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);++ gl.clearColor(0.2, 0.4, 0.6, 0.8);+ gl.clear(gl.COLOR_BUFFER_BIT);+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);++ const pixels = new Uint8Array(4 * 4);+ gl.readPixels(0, 0, 4, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);+ const texel = index => Array.from(pixels.slice(index * 4, index * 4 + 4)).join(",");+ return `white ${texel(0)} | orange ${texel(1)} | half-transparent white ${texel(2)} | transparent ${texel(3)} | error ${gl.getError()}`;+ }++ test(() => {+ const canvas = document.createElement("canvas");+ canvas.width = 4;+ canvas.height = 1;+ const gl = canvas.getContext("webgl");++ const target = gl.createTexture();+ gl.bindTexture(gl.TEXTURE_2D, target);+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 4, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);+ gl.bindFramebuffer(gl.FRAMEBUFFER, gl.createFramebuffer());+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, target, 0);+ gl.viewport(0, 0, 4, 1);++ const vertexShader = gl.createShader(gl.VERTEX_SHADER);+ gl.shaderSource(vertexShader, "attribute vec2 position; varying vec2 coordinate; void main() { coordinate = (position + 1.0) / 2.0; gl_Position = vec4(position, 0, 1); }");+ gl.compileShader(vertexShader);+ const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);+ gl.shaderSource(fragmentShader, "precision mediump float; varying vec2 coordinate; uniform sampler2D source; void main() { gl_FragColor = texture2D(source, coordinate); }");+ gl.compileShader(fragmentShader);+ const program = gl.createProgram();+ gl.attachShader(program, vertexShader);+ gl.attachShader(program, fragmentShader);+ gl.linkProgram(program);+ gl.useProgram(program);++ gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), gl.STATIC_DRAW);+ const position = gl.getAttribLocation(program, "position");+ gl.enableVertexAttribArray(position);+ gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);++ for (const [name, format] of [["LUMINANCE", gl.LUMINANCE], ["LUMINANCE_ALPHA", gl.LUMINANCE_ALPHA], ["ALPHA", gl.ALPHA]])+ println(`${name}: ${sampledTexels(gl, format)}`);+ });+</script>