[refactor] rename 'PaintBatches' to 'PaintJobs'

This commit is contained in:
Emil Ernerfeldt 2020-07-19 00:01:13 +02:00
parent 7565210b2d
commit fccd135254
7 changed files with 45 additions and 43 deletions

View file

@ -6,7 +6,7 @@ use crate::{layout::align_rect, paint::*, *};
#[derive(Clone, Copy, Default)] #[derive(Clone, Copy, Default)]
struct PaintStats { struct PaintStats {
num_batches: usize, num_jobs: usize,
num_primitives: usize, num_primitives: usize,
num_vertices: usize, num_vertices: usize,
num_triangles: usize, num_triangles: usize,
@ -168,15 +168,15 @@ impl Context {
/// Call at the end of each frame. /// Call at the end of each frame.
/// Returns what has happened this frame (`Output`) as well as what you need to paint. /// Returns what has happened this frame (`Output`) as well as what you need to paint.
#[must_use] #[must_use]
pub fn end_frame(&self) -> (Output, PaintBatches) { pub fn end_frame(&self) -> (Output, PaintJobs) {
if self.input.wants_repaint() { if self.input.wants_repaint() {
self.request_repaint(); self.request_repaint();
} }
self.memory().end_frame(); self.memory().end_frame();
let output: Output = std::mem::take(&mut self.output()); let output: Output = std::mem::take(&mut self.output());
let paint_batches = self.paint(); let paint_jobs = self.paint();
(output, paint_batches) (output, paint_jobs)
} }
fn drain_paint_lists(&self) -> Vec<(Rect, PaintCmd)> { fn drain_paint_lists(&self) -> Vec<(Rect, PaintCmd)> {
@ -184,27 +184,27 @@ impl Context {
self.graphics().drain(memory.areas.order()).collect() self.graphics().drain(memory.areas.order()).collect()
} }
fn paint(&self) -> PaintBatches { fn paint(&self) -> PaintJobs {
let mut paint_options = *self.paint_options.lock(); let mut paint_options = *self.paint_options.lock();
paint_options.aa_size = 1.0 / self.pixels_per_point(); paint_options.aa_size = 1.0 / self.pixels_per_point();
paint_options.aa_size *= 1.5; // Looks better, but TODO: should not be needed paint_options.aa_size *= 1.5; // Looks better, but TODO: should not be needed
let paint_commands = self.drain_paint_lists(); let paint_commands = self.drain_paint_lists();
let num_primitives = paint_commands.len(); let num_primitives = paint_commands.len();
let batches = let paint_jobs =
mesher::paint_commands_into_triangles(paint_options, self.fonts(), paint_commands); mesher::paint_commands_into_triangles(paint_options, self.fonts(), paint_commands);
{ {
let mut stats = PaintStats::default(); let mut stats = PaintStats::default();
stats.num_batches = batches.len(); stats.num_jobs = paint_jobs.len();
stats.num_primitives = num_primitives; stats.num_primitives = num_primitives;
for (_, triangles) in &batches { for (_, triangles) in &paint_jobs {
stats.num_vertices += triangles.vertices.len(); stats.num_vertices += triangles.vertices.len();
stats.num_triangles += triangles.indices.len() / 3; stats.num_triangles += triangles.indices.len() / 3;
} }
*self.paint_stats.lock() = stats; *self.paint_stats.lock() = stats;
} }
batches paint_jobs
} }
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
@ -639,7 +639,7 @@ impl paint::PaintOptions {
impl PaintStats { impl PaintStats {
pub fn ui(&self, ui: &mut Ui) { pub fn ui(&self, ui: &mut Ui) {
ui.add(label!("Batches: {}", self.num_batches)) ui.add(label!("Jobs: {}", self.num_jobs))
.tooltip_text("Number of separate clip rectanlges"); .tooltip_text("Number of separate clip rectanlges");
ui.add(label!("Primitives: {}", self.num_primitives)) ui.add(label!("Primitives: {}", self.num_primitives))
.tooltip_text("Boxes, circles, text areas etc"); .tooltip_text("Boxes, circles, text areas etc");

View file

@ -9,6 +9,6 @@ pub use {
color::Color, color::Color,
command::{LineStyle, PaintCmd}, command::{LineStyle, PaintCmd},
fonts::{FontDefinitions, Fonts, TextStyle}, fonts::{FontDefinitions, Fonts, TextStyle},
mesher::{PaintBatches, PaintOptions, Path, Triangles, Vertex}, mesher::{PaintJobs, PaintOptions, Path, Triangles, Vertex},
texture_atlas::Texture, texture_atlas::Texture,
}; };

View file

@ -33,8 +33,10 @@ pub struct Triangles {
pub vertices: Vec<Vertex>, pub vertices: Vec<Vertex>,
} }
pub type PaintJob = (Rect, Triangles);
/// Grouped by clip rectangles, in pixel coordinates /// Grouped by clip rectangles, in pixel coordinates
pub type PaintBatches = Vec<(Rect, Triangles)>; pub type PaintJobs = Vec<PaintJob>;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -680,20 +682,20 @@ pub fn paint_commands_into_triangles(
) -> Vec<(Rect, Triangles)> { ) -> Vec<(Rect, Triangles)> {
let mut reused_path = Path::default(); let mut reused_path = Path::default();
let mut batches = PaintBatches::default(); let mut jobs = PaintJobs::default();
for (clip_rect, cmd) in commands { for (clip_rect, cmd) in commands {
// TODO: cull(clip_rect, cmd) // TODO: cull(clip_rect, cmd)
if batches.is_empty() || batches.last().unwrap().0 != clip_rect { if jobs.is_empty() || jobs.last().unwrap().0 != clip_rect {
batches.push((clip_rect, Triangles::default())); jobs.push((clip_rect, Triangles::default()));
} }
let out = &mut batches.last_mut().unwrap().1; let out = &mut jobs.last_mut().unwrap().1;
paint_command_into_triangles(&mut reused_path, options, fonts, cmd, out); paint_command_into_triangles(&mut reused_path, options, fonts, cmd, out);
} }
if options.debug_paint_clip_rects { if options.debug_paint_clip_rects {
for (clip_rect, triangles) in &mut batches { for (clip_rect, triangles) in &mut jobs {
paint_command_into_triangles( paint_command_into_triangles(
&mut reused_path, &mut reused_path,
options, options,
@ -709,5 +711,5 @@ pub fn paint_commands_into_triangles(
} }
} }
batches jobs
} }

View file

@ -3,7 +3,7 @@
use { use {
egui::{ egui::{
math::clamp, math::clamp,
paint::{PaintBatches, Triangles}, paint::{PaintJobs, Triangles},
Rect, Rect,
}, },
glium::{implement_vertex, index::PrimitiveType, program, texture, uniform, Frame, Surface}, glium::{implement_vertex, index::PrimitiveType, program, texture, uniform, Frame, Surface},
@ -181,24 +181,24 @@ impl Painter {
self.current_texture_id = Some(texture.id); self.current_texture_id = Some(texture.id);
} }
pub fn paint_batches( pub fn paint_jobs(
&mut self, &mut self,
display: &glium::Display, display: &glium::Display,
batches: PaintBatches, jobs: PaintJobs,
texture: &egui::Texture, texture: &egui::Texture,
) { ) {
self.upload_texture(display, texture); self.upload_texture(display, texture);
let mut target = display.draw(); let mut target = display.draw();
target.clear_color(0.0, 0.0, 0.0, 0.0); target.clear_color(0.0, 0.0, 0.0, 0.0);
for (clip_rect, triangles) in batches { for (clip_rect, triangles) in jobs {
self.paint_batch(&mut target, display, clip_rect, &triangles, texture) self.paint_job(&mut target, display, clip_rect, &triangles, texture)
} }
target.finish().unwrap(); target.finish().unwrap();
} }
#[inline(never)] // Easier profiling #[inline(never)] // Easier profiling
fn paint_batch( fn paint_job(
&mut self, &mut self,
target: &mut Frame, target: &mut Frame,
display: &glium::Display, display: &glium::Display,

View file

@ -74,27 +74,27 @@ impl Backend {
self.ctx.begin_frame(raw_input) self.ctx.begin_frame(raw_input)
} }
pub fn end_frame(&mut self) -> Result<(egui::Output, egui::PaintBatches), JsValue> { pub fn end_frame(&mut self) -> Result<(egui::Output, egui::PaintJobs), JsValue> {
let frame_start = self let frame_start = self
.frame_start .frame_start
.take() .take()
.expect("unmatched calls to begin_frame/end_frame"); .expect("unmatched calls to begin_frame/end_frame");
let (output, batches) = self.ctx.end_frame(); let (output, paint_jobs) = self.ctx.end_frame();
self.auto_save(); self.auto_save();
let now = now_sec(); let now = now_sec();
self.frame_times.add(now, (now - frame_start) as f32); self.frame_times.add(now, (now - frame_start) as f32);
Ok((output, batches)) Ok((output, paint_jobs))
} }
pub fn paint(&mut self, batches: egui::PaintBatches) -> Result<(), JsValue> { pub fn paint(&mut self, paint_jobs: egui::PaintJobs) -> Result<(), JsValue> {
let bg_color = egui::color::TRANSPARENT; // Use background css color. let bg_color = egui::color::TRANSPARENT; // Use background css color.
self.painter.paint_batches( self.painter.paint_jobs(
bg_color, bg_color,
batches, paint_jobs,
self.ctx.texture(), self.ctx.texture(),
self.ctx.pixels_per_point(), self.ctx.pixels_per_point(),
) )
@ -175,7 +175,7 @@ impl AppRunner {
self.backend.canvas_id() self.backend.canvas_id()
} }
pub fn logic(&mut self) -> Result<(egui::Output, egui::PaintBatches), JsValue> { pub fn logic(&mut self) -> Result<(egui::Output, egui::PaintJobs), JsValue> {
resize_to_screen_size(self.backend.canvas_id()); resize_to_screen_size(self.backend.canvas_id());
let raw_input = self.web_input.new_frame(); let raw_input = self.web_input.new_frame();
@ -186,13 +186,13 @@ impl AppRunner {
let mut ui = self.backend.begin_frame(raw_input); let mut ui = self.backend.begin_frame(raw_input);
self.app.ui(&mut ui, &mut self.backend, &info); self.app.ui(&mut ui, &mut self.backend, &info);
let (output, batches) = self.backend.end_frame()?; let (output, paint_jobs) = self.backend.end_frame()?;
handle_output(&output); handle_output(&output);
Ok((output, batches)) Ok((output, paint_jobs))
} }
pub fn paint(&mut self, batches: egui::PaintBatches) -> Result<(), JsValue> { pub fn paint(&mut self, paint_jobs: egui::PaintJobs) -> Result<(), JsValue> {
self.backend.paint(batches) self.backend.paint(paint_jobs)
} }
} }
@ -414,8 +414,8 @@ fn paint_and_schedule(runner_ref: AppRunnerRef) -> Result<(), JsValue> {
let mut runner_lock = runner_ref.0.lock(); let mut runner_lock = runner_ref.0.lock();
if runner_lock.backend.run_mode() == RunMode::Continuous || runner_lock.needs_repaint { if runner_lock.backend.run_mode() == RunMode::Continuous || runner_lock.needs_repaint {
runner_lock.needs_repaint = false; runner_lock.needs_repaint = false;
let (output, batches) = runner_lock.logic()?; let (output, paint_jobs) = runner_lock.logic()?;
runner_lock.paint(batches)?; runner_lock.paint(paint_jobs)?;
runner_lock.needs_repaint = output.needs_repaint; runner_lock.needs_repaint = output.needs_repaint;
} }
Ok(()) Ok(())

View file

@ -6,7 +6,7 @@ use {
use egui::{ use egui::{
math::clamp, math::clamp,
paint::{Color, PaintBatches, Texture, Triangles}, paint::{Color, PaintJobs, Texture, Triangles},
vec2, vec2,
}; };
@ -149,10 +149,10 @@ impl Painter {
self.current_texture_id = Some(texture.id); self.current_texture_id = Some(texture.id);
} }
pub fn paint_batches( pub fn paint_jobs(
&mut self, &mut self,
bg_color: Color, bg_color: Color,
batches: PaintBatches, jobs: PaintJobs,
texture: &Texture, texture: &Texture,
pixels_per_point: f32, pixels_per_point: f32,
) -> Result<(), JsValue> { ) -> Result<(), JsValue> {
@ -205,7 +205,7 @@ impl Painter {
); );
gl.clear(Gl::COLOR_BUFFER_BIT); gl.clear(Gl::COLOR_BUFFER_BIT);
for (clip_rect, triangles) in batches { for (clip_rect, triangles) in jobs {
let clip_min_x = pixels_per_point * clip_rect.min.x; let clip_min_x = pixels_per_point * clip_rect.min.x;
let clip_min_y = pixels_per_point * clip_rect.min.y; let clip_min_y = pixels_per_point * clip_rect.min.y;
let clip_max_x = pixels_per_point * clip_rect.max.x; let clip_max_x = pixels_per_point * clip_rect.max.x;

View file

@ -135,14 +135,14 @@ fn main() {
.text_style(TextStyle::Monospace), .text_style(TextStyle::Monospace),
); );
let (output, paint_batches) = ctx.end_frame(); let (output, paint_jobs) = ctx.end_frame();
frame_times.add( frame_times.add(
raw_input.time, raw_input.time,
(Instant::now() - egui_start).as_secs_f64() as f32, (Instant::now() - egui_start).as_secs_f64() as f32,
); );
painter.paint_batches(&display, paint_batches, ctx.texture()); painter.paint_jobs(&display, paint_jobs, ctx.texture());
egui_glium::handle_output(output, &display, clipboard.as_mut()); egui_glium::handle_output(output, &display, clipboard.as_mut());
} }