Skip to content

Commit 3005cc3

Browse files
Lubrsigmta
authored andcommitted
LibWeb/WebGL2: Check if WebGLSync object belongs to the current context
1 parent 3d2874b commit 3005cc3

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

Libraries/LibWeb/WebGL/WebGL2RenderingContextImpl.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,21 +756,53 @@ GC::Root<WebGLSync> WebGL2RenderingContextImpl::fence_sync(WebIDL::UnsignedLong
756756
void WebGL2RenderingContextImpl::delete_sync(GC::Root<WebGLSync> sync)
757757
{
758758
m_context->make_current();
759-
glDeleteSync((GLsync)(sync ? sync->sync_handle() : nullptr));
759+
760+
GLsync sync_handle = nullptr;
761+
if (sync) {
762+
auto handle_or_error = sync->sync_handle(this);
763+
if (handle_or_error.is_error()) {
764+
set_error(GL_INVALID_OPERATION);
765+
return;
766+
}
767+
sync_handle = static_cast<GLsync>(handle_or_error.release_value());
768+
}
769+
770+
glDeleteSync(sync_handle);
760771
}
761772

762773
WebIDL::UnsignedLong WebGL2RenderingContextImpl::client_wait_sync(GC::Root<WebGLSync> sync, WebIDL::UnsignedLong flags, WebIDL::UnsignedLongLong timeout)
763774
{
764775
m_context->make_current();
765-
return glClientWaitSync((GLsync)(sync ? sync->sync_handle() : nullptr), flags, timeout);
776+
777+
GLsync sync_handle = nullptr;
778+
if (sync) {
779+
auto handle_or_error = sync->sync_handle(this);
780+
if (handle_or_error.is_error()) {
781+
set_error(GL_INVALID_OPERATION);
782+
return GL_WAIT_FAILED;
783+
}
784+
sync_handle = static_cast<GLsync>(handle_or_error.release_value());
785+
}
786+
787+
return glClientWaitSync(sync_handle, flags, timeout);
766788
}
767789

768790
JS::Value WebGL2RenderingContextImpl::get_sync_parameter(GC::Root<WebGLSync> sync, WebIDL::UnsignedLong pname)
769791
{
770792
m_context->make_current();
771793

794+
GLsync sync_handle = nullptr;
795+
if (sync) {
796+
auto handle_or_error = sync->sync_handle(this);
797+
if (handle_or_error.is_error()) {
798+
set_error(GL_INVALID_OPERATION);
799+
return JS::js_null();
800+
}
801+
sync_handle = static_cast<GLsync>(handle_or_error.release_value());
802+
}
803+
772804
GLint result = 0;
773-
glGetSynciv((GLsync)(sync ? sync->sync_handle() : nullptr), pname, 1, nullptr, &result);
805+
glGetSynciv(sync_handle, pname, 1, nullptr, &result);
774806
return JS::Value(result);
775807
}
776808

Libraries/LibWeb/WebGL/WebGLObject.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ class WEB_API WebGLObject : public Bindings::PlatformObject {
3434

3535
bool invalidated() const { return m_invalidated; }
3636

37-
private:
3837
// FIXME: It should be GC::Ptr instead of raw pointer, but we need to make WebGLRenderingContextBase inherit from PlatformObject first.
3938
WebGLRenderingContextBase* m_context;
39+
40+
private:
4041
GLuint m_handle { 0 };
4142

4243
bool m_invalidated { false };

Libraries/LibWeb/WebGL/WebGLSync.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <LibWeb/Bindings/WebGLSyncPrototype.h>
1010
#include <LibWeb/WebGL/WebGLSync.h>
1111

12+
#include <GLES2/gl2.h>
13+
1214
namespace Web::WebGL {
1315

1416
GC_DEFINE_ALLOCATOR(WebGLSync);
@@ -32,4 +34,11 @@ void WebGLSync::initialize(JS::Realm& realm)
3234
Base::initialize(realm);
3335
}
3436

37+
ErrorOr<GLsyncInternal> WebGLSync::sync_handle(WebGLRenderingContextBase const* context) const
38+
{
39+
if (context == m_context)
40+
return m_sync_handle;
41+
return Error::from_errno(GL_INVALID_OPERATION);
42+
}
43+
3544
}

Libraries/LibWeb/WebGL/WebGLSync.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class WebGLSync : public WebGLObject {
2020

2121
virtual ~WebGLSync() override;
2222

23-
GLsyncInternal sync_handle() const { return m_sync_handle; }
23+
ErrorOr<GLsyncInternal> sync_handle(WebGLRenderingContextBase const* context) const;
2424

2525
protected:
2626
explicit WebGLSync(JS::Realm&, WebGLRenderingContextBase&, GLsyncInternal handle);

0 commit comments

Comments
 (0)