Simplify backends by adding fn paint_and_update_textures helper

This commit is contained in:
Emil Ernerfeldt 2022-02-21 21:49:52 +01:00
parent 8f887e2ebd
commit 0a46634c13
7 changed files with 73 additions and 42 deletions

View file

@ -67,14 +67,10 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
std::thread::sleep(std::time::Duration::from_millis(10));
}
let (needs_repaint, mut textures_delta, shapes) =
let (needs_repaint, textures_delta, shapes) =
integration.update(display.gl_window().window());
let clipped_meshes = integration.egui_ctx.tessellate(shapes);
for (id, image_delta) in textures_delta.set {
painter.set_texture(&display, id, &image_delta);
}
// paint:
{
use glium::Surface as _;
@ -82,20 +78,17 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
let color = integration.app.clear_color();
target.clear_color(color[0], color[1], color[2], color[3]);
painter.paint_meshes(
painter.paint_and_update_textures(
&display,
&mut target,
integration.egui_ctx.pixels_per_point(),
clipped_meshes,
&textures_delta,
);
target.finish().unwrap();
}
for id in textures_delta.free.drain(..) {
painter.free_texture(id);
}
{
*control_flow = if integration.should_quit() {
glutin::event_loop::ControlFlow::Exit

View file

@ -157,22 +157,14 @@ impl EguiGlium {
/// Paint the results of the last call to [`Self::run`].
pub fn paint<T: glium::Surface>(&mut self, display: &glium::Display, target: &mut T) {
let shapes = std::mem::take(&mut self.shapes);
let mut textures_delta = std::mem::take(&mut self.textures_delta);
for (id, image_delta) in textures_delta.set {
self.painter.set_texture(display, id, &image_delta);
}
let textures_delta = std::mem::take(&mut self.textures_delta);
let clipped_meshes = self.egui_ctx.tessellate(shapes);
self.painter.paint_meshes(
self.painter.paint_and_update_textures(
display,
target,
self.egui_ctx.pixels_per_point(),
clipped_meshes,
&textures_delta,
);
for id in textures_delta.free.drain(..) {
self.painter.free_texture(id);
}
}
}

View file

@ -65,6 +65,25 @@ impl Painter {
self.max_texture_side
}
pub fn paint_and_update_textures<T: glium::Surface>(
&mut self,
display: &glium::Display,
target: &mut T,
pixels_per_point: f32,
clipped_meshes: Vec<egui::ClippedMesh>,
textures_delta: &egui::TexturesDelta,
) {
for (id, image_delta) in &textures_delta.set {
self.set_texture(display, *id, image_delta);
}
self.paint_meshes(display, target, pixels_per_point, clipped_meshes);
for &id in &textures_delta.free {
self.free_texture(id);
}
}
/// Main entry-point for painting a frame.
/// You should call `target.clear_color(..)` before
/// and `target.finish()` after this.
@ -73,9 +92,9 @@ impl Painter {
display: &glium::Display,
target: &mut T,
pixels_per_point: f32,
cipped_meshes: Vec<egui::ClippedMesh>,
clipped_meshes: Vec<egui::ClippedMesh>,
) {
for egui::ClippedMesh(clip_rect, mesh) in cipped_meshes {
for egui::ClippedMesh(clip_rect, mesh) in clipped_meshes {
self.paint_mesh(target, display, pixels_per_point, clip_rect, &mesh);
}
}

View file

@ -83,14 +83,9 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
std::thread::sleep(std::time::Duration::from_millis(10));
}
let (needs_repaint, mut textures_delta, shapes) =
integration.update(gl_window.window());
let (needs_repaint, textures_delta, shapes) = integration.update(gl_window.window());
let clipped_meshes = integration.egui_ctx.tessellate(shapes);
for (id, image_delta) in textures_delta.set {
painter.set_texture(&gl, id, &image_delta);
}
// paint:
{
let color = integration.app.clear_color();
@ -100,20 +95,17 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
gl.clear_color(color[0], color[1], color[2], color[3]);
gl.clear(glow::COLOR_BUFFER_BIT);
}
painter.paint_meshes(
painter.paint_and_update_textures(
&gl,
gl_window.window().inner_size().into(),
integration.egui_ctx.pixels_per_point(),
clipped_meshes,
&textures_delta,
);
gl_window.swap_buffers().unwrap();
}
for id in textures_delta.free.drain(..) {
painter.free_texture(&gl, id);
}
{
*control_flow = if integration.should_quit() {
winit::event_loop::ControlFlow::Exit

View file

@ -270,6 +270,25 @@ impl Painter {
(width_in_pixels, height_in_pixels)
}
pub fn paint_and_update_textures(
&mut self,
gl: &glow::Context,
inner_size: [u32; 2],
pixels_per_point: f32,
clipped_meshes: Vec<egui::ClippedMesh>,
textures_delta: &egui::TexturesDelta,
) {
for (id, image_delta) in &textures_delta.set {
self.set_texture(gl, *id, image_delta);
}
self.paint_meshes(gl, inner_size, pixels_per_point, clipped_meshes);
for &id in &textures_delta.free {
self.free_texture(gl, id);
}
}
/// Main entry-point for painting a frame.
/// You should call `target.clear_color(..)` before
/// and `target.finish()` after this.

View file

@ -294,17 +294,14 @@ impl AppRunner {
/// Paint the results of the last call to [`Self::logic`].
pub fn paint(&mut self, clipped_meshes: Vec<egui::ClippedMesh>) -> Result<(), JsValue> {
let textures_delta = std::mem::take(&mut self.textures_delta);
for (id, image_delta) in textures_delta.set {
self.painter.set_texture(id, &image_delta);
}
self.painter.clear(self.app.clear_color());
self.painter
.paint_meshes(clipped_meshes, self.egui_ctx.pixels_per_point())?;
for id in textures_delta.free {
self.painter.free_texture(id);
}
self.painter.paint_and_update_textures(
clipped_meshes,
self.egui_ctx.pixels_per_point(),
&textures_delta,
)?;
Ok(())
}

View file

@ -22,4 +22,23 @@ pub trait Painter {
) -> Result<(), JsValue>;
fn name(&self) -> &'static str;
fn paint_and_update_textures(
&mut self,
clipped_meshes: Vec<egui::ClippedMesh>,
pixels_per_point: f32,
textures_delta: &egui::TexturesDelta,
) -> Result<(), JsValue> {
for (id, image_delta) in &textures_delta.set {
self.set_texture(*id, image_delta);
}
self.paint_meshes(clipped_meshes, pixels_per_point)?;
for &id in &textures_delta.free {
self.free_texture(id);
}
Ok(())
}
}