Skip to content

Truly independent web browser

Compositor: Keep the page's texture binding across WebGL resizes

Aliaksandr Kalenike0668f2cb9b7
diff --git a/Services/Compositor/OpenGLContext.cpp b/Services/Compositor/OpenGLContext.cpp
index 325022f97dc..137e5fce950 100644
--- a/Services/Compositor/OpenGLContext.cpp
+++ b/Services/Compositor/OpenGLContext.cpp
@@ -330,6 +330,20 @@ void OpenGLContext::clear_buffer_to_default_values()
#endif
}
+#ifdef ENABLE_WEBGL
+// The drawing buffer is also allocated when the canvas is resized, between the page's commands, so setting it up must
+// leave the page's texture binding alone.
+template<typename Callback>
+static void with_texture_bound(GLenum target, GLuint texture, Callback callback)
+{
+ GLint original_texture = 0;
+ glGetIntegerv(target == GL_TEXTURE_RECTANGLE_ANGLE ? GL_TEXTURE_BINDING_RECTANGLE_ANGLE : GL_TEXTURE_BINDING_2D, &original_texture);
+ glBindTexture(target, texture);
+ callback();
+ glBindTexture(target, original_texture);
+}
+#endif
+
#ifdef AK_OS_MACOS
void OpenGLContext::allocate_iosurface_painting_surface()
{
@@ -359,9 +373,10 @@ void OpenGLContext::allocate_iosurface_painting_surface()
eglMakeCurrent(m_impl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, m_impl->context);
glGenTextures(1, &m_impl->color_buffer);
- glBindTexture(m_impl->texture_target == EGL_TEXTURE_RECTANGLE_ANGLE ? GL_TEXTURE_RECTANGLE_ANGLE : GL_TEXTURE_2D, m_impl->color_buffer);
- auto result = eglBindTexImage(m_impl->display, m_impl->surface, EGL_BACK_BUFFER);
- VERIFY(result == EGL_TRUE);
+ with_texture_bound(m_impl->texture_target == EGL_TEXTURE_RECTANGLE_ANGLE ? GL_TEXTURE_RECTANGLE_ANGLE : GL_TEXTURE_2D, m_impl->color_buffer, [&] {
+ auto result = eglBindTexImage(m_impl->display, m_impl->surface, EGL_BACK_BUFFER);
+ VERIFY(result == EGL_TRUE);
+ });
glViewport(0, 0, m_size.width(), m_size.height());
}
@@ -438,8 +453,9 @@ bool OpenGLContext::allocate_vkimage_painting_surface()
eglMakeCurrent(m_impl->display, m_impl->surface, m_impl->surface, m_impl->context);
glGenTextures(1, &m_impl->color_buffer);
- glBindTexture(GL_TEXTURE_2D, m_impl->color_buffer);
- glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, m_impl->egl_image);
+ with_texture_bound(GL_TEXTURE_2D, m_impl->color_buffer, [&] {
+ glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, m_impl->egl_image);
+ });
glViewport(0, 0, m_size.width(), m_size.height());
@@ -459,20 +475,21 @@ void OpenGLContext::allocate_cpu_painting_surface()
eglMakeCurrent(m_impl->display, m_impl->surface, m_impl->surface, m_impl->context);
glGenTextures(1, &m_impl->color_buffer);
- glBindTexture(GL_TEXTURE_2D, m_impl->color_buffer);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-
- GLint original_pixel_unpack_buffer = 0;
- if (m_webgl_version == WebGLVersion::WebGL2) {
- glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &original_pixel_unpack_buffer);
- glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
- }
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.width(), m_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
- if (m_webgl_version == WebGLVersion::WebGL2)
- glBindBuffer(GL_PIXEL_UNPACK_BUFFER, original_pixel_unpack_buffer);
+ with_texture_bound(GL_TEXTURE_2D, m_impl->color_buffer, [&] {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+ GLint original_pixel_unpack_buffer = 0;
+ if (m_webgl_version == WebGLVersion::WebGL2) {
+ glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &original_pixel_unpack_buffer);
+ glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
+ }
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.width(), m_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
+ if (m_webgl_version == WebGLVersion::WebGL2)
+ glBindBuffer(GL_PIXEL_UNPACK_BUFFER, original_pixel_unpack_buffer);
+ });
glViewport(0, 0, m_size.width(), m_size.height());
}
diff --git a/Tests/LibWeb/Text/expected/canvas/webgl-resize-keeps-texture-binding.txt b/Tests/LibWeb/Text/expected/canvas/webgl-resize-keeps-texture-binding.txt
new file mode 100644
index 00000000000..a4f8108796f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/webgl-resize-keeps-texture-binding.txt
@@ -0,0 +1,4 @@
+webgl: pixel after resize: 255,0,255,255
+webgl: error: NO_ERROR
+webgl2: pixel after resize: 255,0,255,255
+webgl2: error: NO_ERROR
diff --git a/Tests/LibWeb/Text/input/canvas/webgl-resize-keeps-texture-binding.html b/Tests/LibWeb/Text/input/canvas/webgl-resize-keeps-texture-binding.html
new file mode 100644
index 00000000000..d83bf91a28d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/webgl-resize-keeps-texture-binding.html
@@ -0,0 +1,50 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script src="webgl-helpers.js"></script>
+<script>
+ function drawWithBoundTextureAcrossResize(contextType) {
+ const canvas = document.createElement("canvas");
+ canvas.width = 4;
+ canvas.height = 4;
+ const gl = canvas.getContext(contextType);
+
+ const compile = (type, source) => {
+ const shader = gl.createShader(type);
+ gl.shaderSource(shader, source);
+ gl.compileShader(shader);
+ return shader;
+ };
+ const program = gl.createProgram();
+ gl.attachShader(program, compile(gl.VERTEX_SHADER, "attribute vec2 position; void main() { gl_Position = vec4(position, 0.0, 1.0); }"));
+ gl.attachShader(program, compile(gl.FRAGMENT_SHADER, "precision mediump float; uniform sampler2D sampler; void main() { gl_FragColor = texture2D(sampler, vec2(0.5)); }"));
+ 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);
+
+ gl.bindTexture(gl.TEXTURE_2D, gl.createTexture());
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([255, 0, 255, 255]));
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+
+ // Resizing gives the canvas a new drawing buffer, and the texture bound above must still be what draws sample.
+ canvas.width = 8;
+ canvas.height = 8;
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+
+ const pixel = new Uint8Array(4);
+ gl.readPixels(4, 4, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
+ println(`${contextType}: pixel after resize: ${pixel}`);
+ printError(`${contextType}: error`, gl);
+ }
+
+ test(() => {
+ drawWithBoundTextureAcrossResize("webgl");
+ drawWithBoundTextureAcrossResize("webgl2");
+ });
+</script>