Don't panic if WebGL thread can't be reached during finalization.

This commit is contained in:
Josh Matthews 2019-07-29 10:05:48 -04:00
parent eb4f2d150a
commit 8f5c37c0b5
8 changed files with 78 additions and 43 deletions

View file

@ -91,21 +91,25 @@ impl WebGLBuffer {
self.capacity.get()
}
pub fn mark_for_deletion(&self) {
pub fn mark_for_deletion(&self, fallible: bool) {
if self.marked_for_deletion.get() {
return;
}
self.marked_for_deletion.set(true);
if self.is_deleted() {
self.delete();
self.delete(fallible);
}
}
fn delete(&self) {
fn delete(&self, fallible: bool) {
assert!(self.is_deleted());
self.upcast::<WebGLObject>()
.context()
.send_command(WebGLCommand::DeleteBuffer(self.id));
let context = self.upcast::<WebGLObject>().context();
let cmd = WebGLCommand::DeleteBuffer(self.id);
if fallible {
context.send_command_ignored(cmd);
} else {
context.send_command(cmd);
}
}
pub fn is_marked_for_deletion(&self) -> bool {
@ -149,7 +153,7 @@ impl WebGLBuffer {
.expect("refcount underflowed"),
);
if self.is_deleted() {
self.delete();
self.delete(false);
}
}
@ -160,6 +164,6 @@ impl WebGLBuffer {
impl Drop for WebGLBuffer {
fn drop(&mut self) {
self.mark_for_deletion();
self.mark_for_deletion(true);
}
}

View file

@ -140,12 +140,16 @@ impl WebGLFramebuffer {
));
}
pub fn delete(&self) {
pub fn delete(&self, fallible: bool) {
if !self.is_deleted.get() {
self.is_deleted.set(true);
self.upcast::<WebGLObject>()
.context()
.send_command(WebGLCommand::DeleteFramebuffer(self.id));
let context = self.upcast::<WebGLObject>().context();
let cmd = WebGLCommand::DeleteFramebuffer(self.id);
if fallible {
context.send_command_ignored(cmd);
} else {
context.send_command(cmd);
}
}
}
@ -588,7 +592,7 @@ impl WebGLFramebuffer {
impl Drop for WebGLFramebuffer {
fn drop(&mut self) {
self.delete();
self.delete(true);
}
}

View file

@ -77,14 +77,18 @@ impl WebGLProgram {
}
/// glDeleteProgram
pub fn mark_for_deletion(&self) {
pub fn mark_for_deletion(&self, fallible: bool) {
if self.marked_for_deletion.get() {
return;
}
self.marked_for_deletion.set(true);
self.upcast::<WebGLObject>()
.context()
.send_command(WebGLCommand::DeleteProgram(self.id));
let cmd = WebGLCommand::DeleteProgram(self.id);
let context = self.upcast::<WebGLObject>().context();
if fallible {
context.send_command_ignored(cmd);
} else {
context.send_command(cmd);
}
if self.is_deleted() {
self.detach_shaders();
}
@ -443,7 +447,7 @@ impl WebGLProgram {
impl Drop for WebGLProgram {
fn drop(&mut self) {
self.in_use(false);
self.mark_for_deletion();
self.mark_for_deletion(true);
}
}

View file

@ -89,7 +89,7 @@ impl WebGLRenderbuffer {
.send_command(WebGLCommand::BindRenderbuffer(target, Some(self.id)));
}
pub fn delete(&self) {
pub fn delete(&self, fallible: bool) {
if !self.is_deleted.get() {
self.is_deleted.set(true);
@ -106,9 +106,13 @@ impl WebGLRenderbuffer {
fb.detach_renderbuffer(self);
}
self.upcast::<WebGLObject>()
.context()
.send_command(WebGLCommand::DeleteRenderbuffer(self.id));
let context = self.upcast::<WebGLObject>().context();
let cmd = WebGLCommand::DeleteRenderbuffer(self.id);
if fallible {
context.send_command_ignored(cmd);
} else {
context.send_command(cmd);
}
}
}
@ -199,6 +203,6 @@ impl WebGLRenderbuffer {
impl Drop for WebGLRenderbuffer {
fn drop(&mut self) {
self.delete();
self.delete(true);
}
}

View file

@ -339,6 +339,12 @@ impl WebGLRenderingContext {
.unwrap();
}
pub fn send_command_ignored(&self, command: WebGLCommand) {
let _ = self
.webgl_sender
.send(command, capture_webgl_backtrace(self));
}
#[inline]
pub fn send_vr_command(&self, command: WebVRCommand) {
self.webgl_sender.send_vr(command).unwrap();
@ -1039,7 +1045,7 @@ impl WebGLRenderingContext {
self.current_vao.set(None);
self.send_command(WebGLCommand::BindVertexArray(None));
}
vao.delete();
vao.delete(false);
}
}
@ -2172,7 +2178,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
self.bound_buffer_array.set(None);
buffer.decrement_attached_counter();
}
buffer.mark_for_deletion();
buffer.mark_for_deletion(false);
}
// https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.6
@ -2188,7 +2194,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
WebGLFramebufferBindingRequest::Default
))
);
framebuffer.delete()
framebuffer.delete(false)
}
}
@ -2205,7 +2211,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
None
))
);
renderbuffer.delete()
renderbuffer.delete(false)
}
}
@ -2240,7 +2246,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
));
}
texture.delete()
texture.delete(false)
}
}
@ -2248,7 +2254,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
fn DeleteProgram(&self, program: Option<&WebGLProgram>) {
if let Some(program) = program {
handle_potential_webgl_error!(self, self.validate_ownership(program), return);
program.mark_for_deletion()
program.mark_for_deletion(false)
}
}
@ -2256,7 +2262,7 @@ impl WebGLRenderingContextMethods for WebGLRenderingContext {
fn DeleteShader(&self, shader: Option<&WebGLShader>) {
if let Some(shader) = shader {
handle_potential_webgl_error!(self, self.validate_ownership(shader), return);
shader.mark_for_deletion()
shader.mark_for_deletion(false)
}
}

View file

@ -178,12 +178,16 @@ impl WebGLShader {
/// Mark this shader as deleted (if it wasn't previously)
/// and delete it as if calling glDeleteShader.
/// Currently does not check if shader is attached
pub fn mark_for_deletion(&self) {
pub fn mark_for_deletion(&self, fallible: bool) {
if !self.marked_for_deletion.get() {
self.marked_for_deletion.set(true);
self.upcast::<WebGLObject>()
.context()
.send_command(WebGLCommand::DeleteShader(self.id));
let context = self.upcast::<WebGLObject>().context();
let cmd = WebGLCommand::DeleteShader(self.id);
if fallible {
context.send_command_ignored(cmd);
} else {
context.send_command(cmd);
}
}
}
@ -230,6 +234,6 @@ impl WebGLShader {
impl Drop for WebGLShader {
fn drop(&mut self) {
self.mark_for_deletion();
self.mark_for_deletion(true);
}
}

View file

@ -180,7 +180,7 @@ impl WebGLTexture {
self.populate_mip_chain(self.base_mipmap_level, last_level)
}
pub fn delete(&self) {
pub fn delete(&self, fallible: bool) {
if !self.is_deleted.get() {
self.is_deleted.set(true);
let context = self.upcast::<WebGLObject>().context();
@ -204,7 +204,12 @@ impl WebGLTexture {
fb.detach_texture(self);
}
context.send_command(WebGLCommand::DeleteTexture(self.id));
let cmd = WebGLCommand::DeleteTexture(self.id);
if fallible {
context.send_command_ignored(cmd);
} else {
context.send_command(cmd);
}
}
}
@ -404,7 +409,7 @@ impl WebGLTexture {
impl Drop for WebGLTexture {
fn drop(&mut self) {
self.delete();
self.delete(true);
}
}

View file

@ -57,16 +57,20 @@ impl WebGLVertexArrayObjectOES {
self.is_deleted.get()
}
pub fn delete(&self) {
pub fn delete(&self, fallible: bool) {
assert!(self.id.is_some());
if self.is_deleted.get() {
return;
}
self.is_deleted.set(true);
self.upcast::<WebGLObject>()
.context()
.send_command(WebGLCommand::DeleteVertexArray(self.id.unwrap()));
let context = self.upcast::<WebGLObject>().context();
let cmd = WebGLCommand::DeleteVertexArray(self.id.unwrap());
if fallible {
context.send_command_ignored(cmd);
} else {
context.send_command(cmd);
}
for attrib_data in &**self.vertex_attribs.borrow() {
if let Some(buffer) = attrib_data.buffer() {
@ -248,7 +252,7 @@ impl WebGLVertexArrayObjectOES {
impl Drop for WebGLVertexArrayObjectOES {
fn drop(&mut self) {
if self.id.is_some() {
self.delete();
self.delete(true);
}
}
}