Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eframe::App::post_rendering #1591

Merged
merged 10 commits into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"examples/hello_world",
"examples/puffin_profiler",
"examples/retained_image",
"examples/screenshot",
"examples/svg",
]

Expand Down
5 changes: 5 additions & 0 deletions eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ pub trait App {
fn warm_up_enabled(&self) -> bool {
false
}

/// Called each time after the rendering the UI.
///
/// Can be used to access pixel data with `get_pixels`
fn post_rendering(&mut self, _screen_size_px: [u32; 2], _frame: &Frame) {}
}

/// Options controlling the behavior of a native window.
Expand Down
8 changes: 8 additions & 0 deletions eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ impl EpiIntegration {
full_output
}

pub fn post_rendering(&mut self, app: &mut dyn epi::App) {
let pixels_per_point = self.frame.info().native_pixels_per_point.unwrap_or(1.0);
let screen_size = self.egui_ctx.input().screen_rect.size() * pixels_per_point;
let screen_size_px = [screen_size.x as u32, screen_size.y as u32];
elwerene marked this conversation as resolved.
Show resolved Hide resolved

app.post_rendering(screen_size_px, &self.frame);
}

pub fn handle_platform_output(
&mut self,
window: &winit::window::Window,
Expand Down
2 changes: 2 additions & 0 deletions eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub fn run_glow(
&textures_delta,
);

integration.post_rendering(app.as_mut());

{
crate::profile_scope!("swap_buffers");
gl_window.swap_buffers().unwrap();
Expand Down
14 changes: 14 additions & 0 deletions examples/screenshot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "screenshot"
version = "0.1.0"
authors = ["René Rössler <[email protected]>"]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.60"
publish = false


[dependencies]
eframe = { path = "../../eframe" }
egui_extras = { path = "../../egui_extras", features = ["image"] }
itertools = "0.10.3"
3 changes: 3 additions & 0 deletions examples/screenshot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```sh
cargo run -p screenshot
```
81 changes: 81 additions & 0 deletions examples/screenshot/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release

use eframe::{
egui::{self, ColorImage},
glow::{self, HasContext},
};
use itertools::Itertools;

fn main() {
let options = eframe::NativeOptions::default();
eframe::run_native(
"Take screenshots and display with eframe/egui",
options,
Box::new(|_cc| Box::new(MyApp::default())),
);
}

#[derive(Default)]
struct MyApp {
texture: Option<egui::TextureHandle>,
screenshot: Option<ColorImage>,
}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
if let Some(screenshot) = self.screenshot.take() {
self.texture = Some(ui.ctx().load_texture("screenshot", screenshot));
}

if ui
.add(egui::Label::new("Screenshot (hover me!):").sense(egui::Sense::hover()))
.hovered()
{
ctx.set_visuals(egui::Visuals::dark());
} else {
ctx.set_visuals(egui::Visuals::light());
};
if let Some(texture) = self.texture.as_ref() {
ui.image(texture, texture.size_vec2());
elwerene marked this conversation as resolved.
Show resolved Hide resolved
} else {
ui.spinner();
}

ctx.request_repaint();
});
}

#[allow(unsafe_code)]
fn post_rendering(&mut self, screen_size_px: [u32; 2], frame: &eframe::Frame) {
if let Some(gl) = frame.gl() {
elwerene marked this conversation as resolved.
Show resolved Hide resolved
let mut buf = vec![0u8; screen_size_px[0] as usize * screen_size_px[1] as usize * 4];
let pixels = glow::PixelPackData::Slice(&mut buf[..]);
unsafe {
gl.read_pixels(
0,
0,
screen_size_px[0] as i32,
screen_size_px[1] as i32,
glow::RGBA,
glow::UNSIGNED_BYTE,
pixels,
);
}

let mut rows: Vec<Vec<u8>> = buf
.into_iter()
.chunks(screen_size_px[0] as usize * 4)
.into_iter()
.map(|chunk| chunk.collect())
.collect();
rows.reverse();
let buf: Vec<u8> = rows.into_iter().flatten().collect();

self.screenshot = Some(ColorImage::from_rgba_unmultiplied(
[screen_size_px[0] as usize, screen_size_px[1] as usize],
&buf[..],
));
}
}
}