Images display briefly before disappearing - Upgrading 0.16 to 0.17 #1424
-
Hi, all! I'm upgrading a project from an old version of egui to 0.17. My application (an image search engine) did a lot of work with tracking texture_ids to try and keep memory usage down. With retained image, I swapped out a lot of the references and my results page now has an issue where the results appear for a split second before disappearing. Here's a video of the issue: pixelbox_2022-03-25_23-02-21.mp4I have diffs of the branch (JosephCatrambone/pixelbox#5) and I think it's something to do with this: let tex_id = fetch_or_generate_thumbnail(res, thumbnail_cache, ctx);
ui.image(tex_id, [res.thumbnail_resolution.0 as f32, res.thumbnail_resolution.1 as f32]) ... Note that I'm keeping the texture ids in the hashmap and NOT retained image. /// Given the thumbnail cache and an image ID, will attempt to load the TextureID from the cache.
/// On a cache hit, will return the TextureID.
/// On a cache miss, will take the RGB enumeration and generate a new thumbnail, then return the ID.
pub fn fetch_or_generate_thumbnail(res: &IndexedImage, thumbnail_cache: &mut HashMap::<i64, egui::TextureId>, ctx: &egui::Context) -> egui::TextureId {
match thumbnail_cache.get(&res.id) {
Some(tid) => tid.clone(),
None => {
//let tid = thumbnail_to_egui_element(res, frame);
let tid = RetainedImage::from_color_image(
&res.path,
egui::ColorImage::from_rgba_unmultiplied(
[res.thumbnail_resolution.0 as usize, res.thumbnail_resolution.1 as usize],
&img_to_rgba(res, 255u8)
)
);
let tex_id = tid.texture_id(ctx);
thumbnail_cache.insert(res.id, tex_id.clone());
tex_id
}
}
} I have some borrowing and ownership issues if I use RetainedImage, but ultimately if that's the right way to do it I can make the shift to keeping references to them. I'm not 100% sure how I'd do that, but I can figure it out. Is cloning the texture_id the source of this flashing, or is it another matter entirely that I'm missing? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You create a |
Beta Was this translation helpful? Give feedback.
You create a
RetainedImage
, and then it directly goes out of scope, andDrop
s, killing the image it is containing. So the texture id is dangling. You should store the wholeRetainedImage
in thethumbnail_cache
(i.e. retain it).