Skip to content

Commit

Permalink
No need to store an Arc<glow::Context>
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 13, 2023
1 parent 3a07c0c commit 5136e3a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
57 changes: 23 additions & 34 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,12 @@ mod glow_integration {
/// a Resumed event. On Android this ensures that any graphics state is only
/// initialized once the application has an associated `SurfaceView`.
struct GlowWinitRunning {
gl: Arc<glow::Context>,
painter: Rc<RefCell<egui_glow::Painter>>,
integration: epi_integration::EpiIntegration,
app: Box<dyn epi::App>,
// Conceptually this will be split out eventually so that the rest of the state
// can be persistent.

// These needs to be shared with the immediate viewport renderer, hence the Rc/Arc/RefCells:
glutin: Rc<RefCell<GlutinWindowContext>>,
painter: Rc<RefCell<egui_glow::Painter>>,
}

impl GlowWinitRunning {
Expand Down Expand Up @@ -550,7 +549,6 @@ mod glow_integration {
app,
glutin,
painter,
gl,
..
} = self;

Expand Down Expand Up @@ -595,8 +593,7 @@ mod glow_integration {

let screen_size_in_pixels: [u32; 2] = window.inner_size().into();

egui_glow::painter::clear(
gl,
painter.borrow().clear(
screen_size_in_pixels,
app.clear_color(&integration.egui_ctx.style().visuals),
);
Expand Down Expand Up @@ -1256,7 +1253,7 @@ mod glow_integration {
.unwrap_or(&self.app_name),
);

let (mut glutin_ctx, painter) = Self::create_glutin_windowed_context(
let (mut glutin, painter) = Self::create_glutin_windowed_context(
event_loop,
storage.as_deref(),
&self.app_name,
Expand All @@ -1265,17 +1262,16 @@ mod glow_integration {
let gl = painter.gl().clone();

let max_texture_side = painter.max_texture_side();
glutin_ctx.max_texture_side = Some(max_texture_side);
for viewport in glutin_ctx.viewports.values_mut() {
glutin.max_texture_side = Some(max_texture_side);
for viewport in glutin.viewports.values_mut() {
if let Some(egui_winit) = viewport.egui_winit.as_mut() {
egui_winit.set_max_texture_side(max_texture_side);
}
}

let system_theme =
system_theme(&glutin_ctx.window(ViewportId::ROOT), &self.native_options);
let system_theme = system_theme(&glutin.window(ViewportId::ROOT), &self.native_options);
let mut integration = epi_integration::EpiIntegration::new(
&glutin_ctx.window(ViewportId::ROOT),
&glutin.window(ViewportId::ROOT),
system_theme,
&self.app_name,
&self.native_options,
Expand Down Expand Up @@ -1308,7 +1304,7 @@ mod glow_integration {
#[cfg(feature = "accesskit")]
{
let event_loop_proxy = self.repaint_proxy.lock().clone();
let viewport = glutin_ctx.viewports.get_mut(&ViewportId::ROOT).unwrap();
let viewport = glutin.viewports.get_mut(&ViewportId::ROOT).unwrap();
if let Viewport {
window: Some(window),
egui_winit: Some(egui_winit),
Expand All @@ -1322,10 +1318,7 @@ mod glow_integration {
integration.egui_ctx.set_visuals(theme.egui_visuals());

if self.native_options.mouse_passthrough {
if let Err(err) = glutin_ctx
.window(ViewportId::ROOT)
.set_cursor_hittest(false)
{
if let Err(err) = glutin.window(ViewportId::ROOT).set_cursor_hittest(false) {
log::warn!("set_cursor_hittest(false) failed: {err}");
}
}
Expand All @@ -1334,20 +1327,20 @@ mod glow_integration {
.expect("Single-use AppCreator has unexpectedly already been taken");

let app = {
let window = glutin_ctx.window(ViewportId::ROOT);
let window = glutin.window(ViewportId::ROOT);
let mut app = app_creator(&epi::CreationContext {
egui_ctx: integration.egui_ctx.clone(),
integration_info: integration.frame.info().clone(),
storage: integration.frame.storage(),
gl: Some(gl.clone()),
gl: Some(gl),
#[cfg(feature = "wgpu")]
wgpu_render_state: None,
raw_display_handle: window.raw_display_handle(),
raw_window_handle: window.raw_window_handle(),
});

if app.warm_up_enabled() {
let viewport = glutin_ctx.viewport_mut(ViewportId::ROOT);
let viewport = glutin.viewport_mut(ViewportId::ROOT);
integration.warm_up(
app.as_mut(),
&window,
Expand All @@ -1358,23 +1351,21 @@ mod glow_integration {
app
};

let glutin_ctx = Rc::new(RefCell::new(glutin_ctx));
let glutin = Rc::new(RefCell::new(glutin));
let painter = Rc::new(RefCell::new(painter));

{
// Create weak pointers so that we don't keep
// state alive for too long.
let glutin = Rc::downgrade(&glutin_ctx);
let gl = Arc::downgrade(&gl);
let glutin = Rc::downgrade(&glutin);
let painter = Rc::downgrade(&painter);
let beginning = integration.beginning;

let event_loop: *const EventLoopWindowTarget<UserEvent> = event_loop;

egui::Context::set_immediate_viewport_renderer(
move |egui_ctx, viewport_builder, id_pair, viewport_ui_cb| {
if let (Some(glutin), Some(gl), Some(painter)) =
(glutin.upgrade(), gl.upgrade(), painter.upgrade())
if let (Some(glutin), Some(painter)) = (glutin.upgrade(), painter.upgrade())
{
// SAFETY: the event loop lives longer than
// the Rc:s we just upgraded above.
Expand All @@ -1388,7 +1379,6 @@ mod glow_integration {
id_pair,
viewport_ui_cb,
&glutin,
&gl,
&painter,
beginning,
);
Expand All @@ -1400,8 +1390,7 @@ mod glow_integration {
}

self.running.replace(GlowWinitRunning {
glutin: glutin_ctx,
gl,
glutin,
painter,
integration,
app,
Expand All @@ -1422,7 +1411,6 @@ mod glow_integration {
id_pair: ViewportIdPair,
viewport_ui_cb: Box<dyn FnOnce(&egui::Context) + '_>,
glutin: &RefCell<GlutinWindowContext>,
gl: &glow::Context,
painter: &RefCell<egui_glow::Painter>,
beginning: Instant,
) {
Expand Down Expand Up @@ -1533,6 +1521,7 @@ mod glow_integration {
log::error!("egui::show_viewport_immediate with title: `{:?}` is not created in main thread, try to use wgpu!", builder.title.clone().unwrap_or_default());
}

let gl = &painter.gl().clone();
egui_glow::painter::clear(gl, screen_size_in_pixels, [0.0, 0.0, 0.0, 0.0]);

let pixels_per_point = egui_ctx.input_for(id_pair.this, |i| i.pixels_per_point());
Expand Down Expand Up @@ -1582,9 +1571,9 @@ mod glow_integration {

fn window(&self, window_id: WindowId) -> Option<Rc<Window>> {
let running = self.running.as_ref()?;
let glutin_ctx = running.glutin.borrow();
let viewport_id = *glutin_ctx.viewport_maps.get(&window_id)?;
if let Some(viewport) = glutin_ctx.viewports.get(&viewport_id) {
let glutin = running.glutin.borrow();
let viewport_id = *glutin.viewport_maps.get(&window_id)?;
if let Some(viewport) = glutin.viewports.get(&viewport_id) {
viewport.window.clone()
} else {
None
Expand All @@ -1611,7 +1600,7 @@ mod glow_integration {
running.app.as_mut(),
Some(&running.glutin.borrow().window(ViewportId::ROOT)),
);
running.app.on_exit(Some(&running.gl));
running.app.on_exit(Some(running.painter.borrow().gl()));
running.painter.borrow_mut().destroy();
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/egui_glow/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ impl Painter {
(width_in_pixels, height_in_pixels)
}

pub fn clear(&self, screen_size_in_pixels: [u32; 2], clear_color: [f32; 4]) {
clear(&self.gl, screen_size_in_pixels, clear_color);
}

/// You are expected to have cleared the color buffer before calling this.
pub fn paint_and_update_textures(
&mut self,
Expand Down

0 comments on commit 5136e3a

Please sign in to comment.