Skip to content

Commit

Permalink
delete GL_FUNCTIONS
Browse files Browse the repository at this point in the history
Signed-off-by: sagudev <[email protected]>
  • Loading branch information
sagudev committed Jan 6, 2025
1 parent 7fc9336 commit a30817f
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 382 deletions.
13 changes: 5 additions & 8 deletions src/platform/egl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ use std::thread;

pub use crate::platform::generic::egl::context::{ContextDescriptor, NativeContext};

thread_local! {
#[doc(hidden)]
pub static GL_FUNCTIONS: Gl = unsafe {Gl::from_loader_function(context::get_proc_address)};
}

/// Represents an OpenGL rendering context.
///
/// A context allows you to issue rendering commands to a surface. When initially created, a
Expand Down Expand Up @@ -119,6 +114,7 @@ impl Device {
pbuffer,
framebuffer: Framebuffer::None,
context_is_owned: true,
gl: Gl::from_loader_function(context::get_proc_address),
};
next_context_id.0 += 1;
Ok(context)
Expand Down Expand Up @@ -149,6 +145,7 @@ impl Device {
read: native_context.egl_read_surface,
}),
context_is_owned: false,
gl: Gl::from_loader_function(get_proc_address),
};
next_context_id.0 += 1;

Expand Down Expand Up @@ -293,9 +290,9 @@ impl Device {
//
// FIXME(pcwalton): Is this necessary?
let _guard = self.temporarily_make_context_current(context)?;
GL_FUNCTIONS.with(|gl| unsafe {
gl.flush();
});
unsafe {
context.gl.flush();
};

match mem::replace(&mut context.framebuffer, Framebuffer::None) {
Framebuffer::Surface(surface) => return Ok(Some(surface)),
Expand Down
193 changes: 94 additions & 99 deletions src/platform/egl/surface/android_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::super::android_ffi::{
use super::super::android_ffi::{
AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER, AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER,
};
use super::super::context::{Context, GL_FUNCTIONS};
use super::super::context::Context;
use super::super::device::Device;
use super::{Surface, SurfaceTexture};
use crate::egl;
Expand Down Expand Up @@ -81,68 +81,65 @@ impl Device {
size: &Size2D<i32>,
) -> Result<Surface, Error> {
let _guard = self.temporarily_make_context_current(context)?;
let gl = &context.gl;
unsafe {
// Create a native hardware buffer.
let hardware_buffer_desc = AHardwareBuffer_Desc {
format: AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
height: size.height as u32,
width: size.width as u32,
layers: 1,
rfu0: 0,
rfu1: 0,
stride: 10,
usage: AHARDWAREBUFFER_USAGE_CPU_READ_NEVER
| AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER
| AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER
| AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE,
};
let mut hardware_buffer = ptr::null_mut();
let result = AHardwareBuffer_allocate(&hardware_buffer_desc, &mut hardware_buffer);
if result != 0 {
return Err(Error::SurfaceCreationFailed(WindowingApiError::Failed));
}

GL_FUNCTIONS.with(|gl| {
unsafe {
// Create a native hardware buffer.
let hardware_buffer_desc = AHardwareBuffer_Desc {
format: AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
height: size.height as u32,
width: size.width as u32,
layers: 1,
rfu0: 0,
rfu1: 0,
stride: 10,
usage: AHARDWAREBUFFER_USAGE_CPU_READ_NEVER
| AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER
| AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER
| AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE,
};
let mut hardware_buffer = ptr::null_mut();
let result = AHardwareBuffer_allocate(&hardware_buffer_desc, &mut hardware_buffer);
if result != 0 {
return Err(Error::SurfaceCreationFailed(WindowingApiError::Failed));
}
// Create an EGL image, and bind it to a texture.
let egl_image = self.create_egl_image(context, hardware_buffer);

// Create an EGL image, and bind it to a texture.
let egl_image = self.create_egl_image(context, hardware_buffer);

// Initialize and bind the image to the texture.
let texture_object =
generic::egl::surface::bind_egl_image_to_gl_texture(gl, egl_image);

// Create the framebuffer, and bind the texture to it.
let framebuffer_object = gl_utils::create_and_bind_framebuffer(
gl,
SURFACE_GL_TEXTURE_TARGET,
Some(texture_object),
);

// Bind renderbuffers as appropriate.
let context_descriptor = self.context_descriptor(context);
let context_attributes = self.context_descriptor_attributes(&context_descriptor);
let renderbuffers = Renderbuffers::new(gl, size, &context_attributes);
renderbuffers.bind_to_current_framebuffer(gl);

debug_assert_eq!(
gl.check_framebuffer_status(gl::FRAMEBUFFER),
gl::FRAMEBUFFER_COMPLETE
);

Ok(Surface {
size: *size,
context_id: context.id,
objects: SurfaceObjects::HardwareBuffer {
hardware_buffer,
egl_image,
framebuffer_object: Some(framebuffer_object),
texture_object: Some(texture_object),
renderbuffers,
},
destroyed: false,
})
}
})
// Initialize and bind the image to the texture.
let texture_object = generic::egl::surface::bind_egl_image_to_gl_texture(gl, egl_image);

// Create the framebuffer, and bind the texture to it.
let framebuffer_object = gl_utils::create_and_bind_framebuffer(
gl,
SURFACE_GL_TEXTURE_TARGET,
Some(texture_object),
);

// Bind renderbuffers as appropriate.
let context_descriptor = self.context_descriptor(context);
let context_attributes = self.context_descriptor_attributes(&context_descriptor);
let renderbuffers = Renderbuffers::new(gl, size, &context_attributes);
renderbuffers.bind_to_current_framebuffer(gl);

debug_assert_eq!(
gl.check_framebuffer_status(gl::FRAMEBUFFER),
gl::FRAMEBUFFER_COMPLETE
);

Ok(Surface {
size: *size,
context_id: context.id,
objects: SurfaceObjects::HardwareBuffer {
hardware_buffer,
egl_image,
framebuffer_object: Some(framebuffer_object),
texture_object: Some(texture_object),
renderbuffers,
},
destroyed: false,
})
}
}

unsafe fn create_window_surface(
Expand Down Expand Up @@ -191,11 +188,12 @@ impl Device {
SurfaceObjects::Window { .. } => return Err((Error::WidgetAttached, surface)),
SurfaceObjects::HardwareBuffer {
hardware_buffer, ..
} => GL_FUNCTIONS.with(|gl| {
} => {
let _guard = match self.temporarily_make_context_current(context) {
Ok(guard) => guard,
Err(err) => return Err((err, surface)),
};
let gl = &context.gl;

let local_egl_image = self.create_egl_image(context, hardware_buffer);
let texture_object =
Expand All @@ -206,7 +204,7 @@ impl Device {
texture_object: Some(texture_object),
phantom: PhantomData,
})
}),
}
}
}
}
Expand Down Expand Up @@ -304,27 +302,25 @@ impl Device {
ref mut texture_object,
ref mut renderbuffers,
} => {
GL_FUNCTIONS.with(|gl| {
gl.bind_framebuffer(gl::FRAMEBUFFER, None);
if let Some(framebuffer) = framebuffer_object.take() {
gl.delete_framebuffer(framebuffer);
}

renderbuffers.destroy(gl);

if let Some(texture) = texture_object.take() {
gl.delete_texture(texture);
}

let egl_display = self.egl_display;
let result =
(EGL_EXTENSION_FUNCTIONS.DestroyImageKHR)(egl_display, *egl_image);
assert_ne!(result, egl::FALSE);
*egl_image = EGL_NO_IMAGE_KHR;

AHardwareBuffer_release(*hardware_buffer);
*hardware_buffer = ptr::null_mut();
});
let gl = &context.gl;
gl.bind_framebuffer(gl::FRAMEBUFFER, None);
if let Some(framebuffer) = framebuffer_object.take() {
gl.delete_framebuffer(framebuffer);
}

renderbuffers.destroy(gl);

if let Some(texture) = texture_object.take() {
gl.delete_texture(texture);
}

let egl_display = self.egl_display;
let result = (EGL_EXTENSION_FUNCTIONS.DestroyImageKHR)(egl_display, *egl_image);
assert_ne!(result, egl::FALSE);
*egl_image = EGL_NO_IMAGE_KHR;

AHardwareBuffer_release(*hardware_buffer);
*hardware_buffer = ptr::null_mut();
}
SurfaceObjects::Window {
ref mut egl_surface,
Expand Down Expand Up @@ -352,23 +348,22 @@ impl Device {
mut surface_texture: SurfaceTexture,
) -> Result<Surface, (Error, SurfaceTexture)> {
let _guard = self.temporarily_make_context_current(context);
GL_FUNCTIONS.with(|gl| {
unsafe {
if let Some(texture) = surface_texture.texture_object.take() {
gl.delete_texture(texture);
}

let egl_display = self.egl_display;
let result = (EGL_EXTENSION_FUNCTIONS.DestroyImageKHR)(
egl_display,
surface_texture.local_egl_image,
);
assert_ne!(result, egl::FALSE);
surface_texture.local_egl_image = EGL_NO_IMAGE_KHR;
let gl = &context.gl;
unsafe {
if let Some(texture) = surface_texture.texture_object.take() {
gl.delete_texture(texture);
}

Ok(surface_texture.surface)
})
let egl_display = self.egl_display;
let result = (EGL_EXTENSION_FUNCTIONS.DestroyImageKHR)(
egl_display,
surface_texture.local_egl_image,
);
assert_ne!(result, egl::FALSE);
surface_texture.local_egl_image = EGL_NO_IMAGE_KHR;
}

Ok(surface_texture.surface)
}

/// Returns a pointer to the underlying surface data for reading or writing by the CPU.
Expand Down
Loading

0 comments on commit a30817f

Please sign in to comment.