Skip to content

Truly independent web browser

Compositor: Upload converted bitmaps whatever the page’s unpack state

sideshowbarkerb098e42f519e
diff --git a/Services/Compositor/HostWebGLContext.cpp b/Services/Compositor/HostWebGLContext.cpp
index 116957eee35..cbff59d9c51 100644
--- a/Services/Compositor/HostWebGLContext.cpp
+++ b/Services/Compositor/HostWebGLContext.cpp
@@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <GLES3/gl3.h>
+
#include <AK/Vector.h>
#include <Compositor/BitmapExport.h>
#include <Compositor/HostWebGLContext.h>
@@ -26,6 +28,47 @@ using namespace Compositing::WebGL;
static constexpr GLsizei max_webgl_string_list_entries = 16384;
+// The pixels of a bitmap converted for upload are tightly packed, row after row and image after image. So while they're
+// uploaded, the unpack parameters that the page set for its own data are reset, and afterward restored.
+class PackedBitmapUnpackState {
+ AK_MAKE_NONCOPYABLE(PackedBitmapUnpackState);
+ AK_MAKE_NONMOVABLE(PackedBitmapUnpackState);
+
+public:
+ explicit PackedBitmapUnpackState(WebGLVersion version)
+ {
+ reset(GL_UNPACK_ALIGNMENT, 1);
+ if (version == WebGLVersion::WebGL2) {
+ reset(GL_UNPACK_ROW_LENGTH, 0);
+ reset(GL_UNPACK_IMAGE_HEIGHT, 0);
+ reset(GL_UNPACK_SKIP_PIXELS, 0);
+ reset(GL_UNPACK_SKIP_ROWS, 0);
+ reset(GL_UNPACK_SKIP_IMAGES, 0);
+ }
+ }
+
+ ~PackedBitmapUnpackState()
+ {
+ for (auto const& saved : m_saved_parameters)
+ glPixelStorei(saved.parameter, saved.value);
+ }
+
+private:
+ void reset(GLenum parameter, GLint value)
+ {
+ GLint saved_value = 0;
+ glGetIntegerv(parameter, &saved_value);
+ m_saved_parameters.append({ parameter, saved_value });
+ glPixelStorei(parameter, value);
+ }
+
+ struct SavedParameter {
+ GLenum parameter;
+ GLint value;
+ };
+ Vector<SavedParameter, 6> m_saved_parameters;
+};
+
HostWebGLContext::HostWebGLContext(NonnullOwnPtr<OpenGLContext> gl_context, RefPtr<Gfx::SkiaBackendContext> skia_backend_context)
: m_gl_context(move(gl_context))
, m_skia_backend_context(move(skia_backend_context))
@@ -261,6 +304,7 @@ ErrorOr<void> HostWebGLContext::tex_image2d_from_bitmap(Commands::TexImage2DFrom
if (auto const* video_frame = TRY(upload_source_at(sources, command.bitmap_index))->get_pointer<NonnullRefPtr<Media::VideoFrame>>(); video_frame && define_texture_image_from_video_frame(command, **video_frame))
return {};
auto converted = TRY(convert_bitmap_for_upload(sources, m_skia_backend_context, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));
+ PackedBitmapUnpackState packed_bitmap_unpack_state { m_gl_context->webgl_version() };
m_gl_context->tex_image2d_robust_angle(command.target, command.level, command.internalformat, converted.width, converted.height, 0, command.format, command.type, converted.buffer.size(), converted.buffer.data());
return {};
}
@@ -270,6 +314,7 @@ ErrorOr<void> HostWebGLContext::tex_sub_image2d_from_bitmap(Commands::TexSubImag
if (auto const* video_frame = TRY(upload_source_at(sources, command.bitmap_index))->get_pointer<NonnullRefPtr<Media::VideoFrame>>(); video_frame && update_texture_image_from_video_frame(command, **video_frame))
return {};
auto converted = TRY(convert_bitmap_for_upload(sources, m_skia_backend_context, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));
+ PackedBitmapUnpackState packed_bitmap_unpack_state { m_gl_context->webgl_version() };
m_gl_context->tex_sub_image2d_robust_angle(command.target, command.level, command.xoffset, command.yoffset, converted.width, converted.height, command.format, command.type, converted.buffer.size(), converted.buffer.data());
return {};
}
@@ -277,6 +322,7 @@ ErrorOr<void> HostWebGLContext::tex_sub_image2d_from_bitmap(Commands::TexSubImag
ErrorOr<void> HostWebGLContext::tex_image3d_from_bitmap(Commands::TexImage3DFromBitmap const& command, Vector<UploadSource> const& sources)
{
auto converted = TRY(convert_bitmap_for_upload(sources, m_skia_backend_context, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));
+ PackedBitmapUnpackState packed_bitmap_unpack_state { m_gl_context->webgl_version() };
m_gl_context->tex_image3d_robust_angle(command.target, command.level, command.internalformat, converted.width, converted.height, command.depth, 0, command.format, command.type, converted.buffer.size(), converted.buffer.data());
return {};
}
@@ -284,6 +330,7 @@ ErrorOr<void> HostWebGLContext::tex_image3d_from_bitmap(Commands::TexImage3DFrom
ErrorOr<void> HostWebGLContext::tex_sub_image3d_from_bitmap(Commands::TexSubImage3DFromBitmap const& command, Vector<UploadSource> const& sources)
{
auto converted = TRY(convert_bitmap_for_upload(sources, m_skia_backend_context, command.bitmap_index, command.format, command.type, command.has_explicit_destination_size, command.destination_width, command.destination_height, command.flip_y, command.premultiply_alpha));
+ PackedBitmapUnpackState packed_bitmap_unpack_state { m_gl_context->webgl_version() };
m_gl_context->tex_sub_image3d_robust_angle(command.target, command.level, command.xoffset, command.yoffset, command.zoffset, converted.width, converted.height, command.depth, command.format, command.type, converted.buffer.size(), converted.buffer.data());
return {};
}
diff --git a/Tests/LibWeb/Text/expected/WebGL/tex-image-2d-canvas-odd-width.txt b/Tests/LibWeb/Text/expected/WebGL/tex-image-2d-canvas-odd-width.txt
new file mode 100644
index 00000000000..abfe096b75e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/WebGL/tex-image-2d-canvas-odd-width.txt
@@ -0,0 +1,12 @@
+UNPACK_ALIGNMENT 4:
+ RGB: error 0 | first row 200,100,50,255 | second row 10,20,30,255
+ LUMINANCE: error 0 | first row 200,200,200,255 | second row 10,10,10,255
+ LUMINANCE_ALPHA: error 0 | first row 200,200,200,255 | second row 10,10,10,255
+ ALPHA: error 0 | first row 0,0,0,255 | second row 0,0,0,255
+ UNPACK_ALIGNMENT afterward: 4
+UNPACK_ALIGNMENT 8:
+ RGB: error 0 | first row 200,100,50,255 | second row 10,20,30,255
+ LUMINANCE: error 0 | first row 200,200,200,255 | second row 10,10,10,255
+ LUMINANCE_ALPHA: error 0 | first row 200,200,200,255 | second row 10,10,10,255
+ ALPHA: error 0 | first row 0,0,0,255 | second row 0,0,0,255
+ UNPACK_ALIGNMENT afterward: 8
diff --git a/Tests/LibWeb/Text/input/WebGL/tex-image-2d-canvas-odd-width.html b/Tests/LibWeb/Text/input/WebGL/tex-image-2d-canvas-odd-width.html
new file mode 100644
index 00000000000..83d48f6ebba
--- /dev/null
+++ b/Tests/LibWeb/Text/input/WebGL/tex-image-2d-canvas-odd-width.html
@@ -0,0 +1,75 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // Uploads a 3x2 canvas, whose rows aren't a multiple of 4 bytes long in these formats, and reads back what sampling
+ // the texture gives for its first and second rows.
+ function sampledTexels(gl, format, source) {
+ const texture = gl.createTexture();
+ gl.bindTexture(gl.TEXTURE_2D, texture);
+ gl.texImage2D(gl.TEXTURE_2D, 0, format, format, gl.UNSIGNED_BYTE, source);
+ const error = gl.getError();
+ 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, 0, 0, 0);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+
+ const pixels = new Uint8Array(3 * 2 * 4);
+ gl.readPixels(0, 0, 3, 2, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
+ const texel = index => Array.from(pixels.slice(index * 4, index * 4 + 4)).join(",");
+ // The texture's first row, the canvas's top row, is the framebuffer's bottom row, which readPixels() reads first.
+ return `error ${error} | first row ${texel(0)} | second row ${texel(3)}`;
+ }
+
+ test(() => {
+ const source = document.createElement("canvas");
+ source.width = 3;
+ source.height = 2;
+ const context = source.getContext("2d");
+ context.fillStyle = "rgb(200, 100, 50)";
+ context.fillRect(0, 0, 3, 1);
+ context.fillStyle = "rgb(10, 20, 30)";
+ context.fillRect(0, 1, 3, 1);
+
+ const canvas = document.createElement("canvas");
+ canvas.width = 3;
+ canvas.height = 2;
+ const gl = canvas.getContext("webgl");
+
+ const target = gl.createTexture();
+ gl.bindTexture(gl.TEXTURE_2D, target);
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 3, 2, 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, 3, 2);
+
+ 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 alignment of [4, 8]) {
+ gl.pixelStorei(gl.UNPACK_ALIGNMENT, alignment);
+ println(`UNPACK_ALIGNMENT ${alignment}:`);
+ for (const [name, format] of [["RGB", gl.RGB], ["LUMINANCE", gl.LUMINANCE], ["LUMINANCE_ALPHA", gl.LUMINANCE_ALPHA], ["ALPHA", gl.ALPHA]])
+ println(` ${name}: ${sampledTexels(gl, format, source)}`);
+ println(` UNPACK_ALIGNMENT afterward: ${gl.getParameter(gl.UNPACK_ALIGNMENT)}`);
+ }
+ });
+</script>