From 9875e7ea3f9dbb11e8bd47ee77e90777096ccccd Mon Sep 17 00:00:00 2001 From: Lee Salzman Date: Tue, 11 Aug 2020 12:53:43 +0000 Subject: [PATCH] Bug 1652894 - allow SWGL contexts to have multiple strong references. r=jimb Differential Revision: https://phabricator.services.mozilla.com/D86337 --- gfx/webrender_bindings/src/swgl_bindings.rs | 5 ++++ gfx/wr/swgl/src/gl.cc | 31 ++++++++++++++++----- gfx/wr/swgl/src/swgl_fns.rs | 7 +++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/gfx/webrender_bindings/src/swgl_bindings.rs b/gfx/webrender_bindings/src/swgl_bindings.rs index 04ec39c835a5..eb02b72e1a99 100644 --- a/gfx/webrender_bindings/src/swgl_bindings.rs +++ b/gfx/webrender_bindings/src/swgl_bindings.rs @@ -21,6 +21,11 @@ pub extern "C" fn wr_swgl_create_context() -> *mut c_void { swgl::Context::create().into() } +#[no_mangle] +pub extern "C" fn wr_swgl_reference_context(ctx: *mut c_void) { + swgl::Context::from(ctx).reference(); +} + #[no_mangle] pub extern "C" fn wr_swgl_destroy_context(ctx: *mut c_void) { swgl::Context::from(ctx).destroy(); diff --git a/gfx/wr/swgl/src/gl.cc b/gfx/wr/swgl/src/gl.cc index ef95a337b17f..030cc15a24db 100644 --- a/gfx/wr/swgl/src/gl.cc +++ b/gfx/wr/swgl/src/gl.cc @@ -512,6 +512,8 @@ struct ObjectStore { }; struct Context { + int32_t references = 1; + ObjectStore queries; ObjectStore buffers; ObjectStore textures; @@ -4093,8 +4095,11 @@ void BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, void Finish() {} -void MakeCurrent(void* ctx_ptr) { - ctx = (Context*)ctx_ptr; +void MakeCurrent(Context* c) { + if (ctx == c) { + return; + } + ctx = c; if (ctx) { setup_program(ctx->current_program); blend_key = ctx->blend ? ctx->blend_key : BLEND_KEY_NONE; @@ -4104,16 +4109,28 @@ void MakeCurrent(void* ctx_ptr) { } } -void* CreateContext() { return new Context; } +Context* CreateContext() { return new Context; } -void DestroyContext(void* ctx_ptr) { - if (!ctx_ptr) { +void ReferenceContext(Context* c) { + if (!c) { return; } - if (ctx == ctx_ptr) { + ++c->references; +} + +void DestroyContext(Context* c) { + if (!c) { + return; + } + assert(c->references > 0); + --c->references; + if (c->references > 0) { + return; + } + if (ctx == c) { MakeCurrent(nullptr); } - delete (Context*)ctx_ptr; + delete c; } typedef Texture LockedTexture; diff --git a/gfx/wr/swgl/src/swgl_fns.rs b/gfx/wr/swgl/src/swgl_fns.rs index 6baa2a5e9cbb..b43b3df928b7 100644 --- a/gfx/wr/swgl/src/swgl_fns.rs +++ b/gfx/wr/swgl/src/swgl_fns.rs @@ -300,6 +300,7 @@ extern "C" { flip: GLboolean, ); fn CreateContext() -> *mut c_void; + fn ReferenceContext(ctx: *mut c_void); fn DestroyContext(ctx: *mut c_void); fn MakeCurrent(ctx: *mut c_void); } @@ -312,6 +313,12 @@ impl Context { Context(unsafe { CreateContext() }) } + pub fn reference(&self) { + unsafe { + ReferenceContext(self.0); + } + } + pub fn destroy(&self) { unsafe { DestroyContext(self.0);