[epi] Simplify TextureAllocator interface
This commit is contained in:
parent
e8e53e9500
commit
5ef9f35d1e
6 changed files with 20 additions and 27 deletions
|
@ -394,9 +394,7 @@ impl TextureManager {
|
|||
let pixels = gradient.to_pixel_row();
|
||||
let width = pixels.len();
|
||||
let height = 1;
|
||||
let id = tex_allocator.alloc();
|
||||
tex_allocator.set_srgba_premultiplied(id, (width, height), &pixels);
|
||||
id
|
||||
tex_allocator.alloc_srgba_premultiplied((width, height), &pixels)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -265,13 +265,16 @@ impl TexMngr {
|
|||
image: &Image,
|
||||
) -> Option<egui::TextureId> {
|
||||
let tex_allocator = frame.tex_allocator().as_mut()?;
|
||||
let texture_id = self.texture_id.unwrap_or_else(|| tex_allocator.alloc());
|
||||
self.texture_id = Some(texture_id);
|
||||
if self.loaded_url != url {
|
||||
if let Some(texture_id) = self.texture_id.take() {
|
||||
tex_allocator.free(texture_id);
|
||||
}
|
||||
|
||||
self.texture_id =
|
||||
Some(tex_allocator.alloc_srgba_premultiplied(image.size, &image.pixels));
|
||||
self.loaded_url = url.to_owned();
|
||||
tex_allocator.set_srgba_premultiplied(texture_id, image.size, &image.pixels);
|
||||
}
|
||||
Some(texture_id)
|
||||
self.texture_id
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -28,17 +28,14 @@ fn deserialize_memory(_: &Option<Box<dyn epi::Storage>>) -> Option<egui::Memory>
|
|||
}
|
||||
|
||||
impl epi::TextureAllocator for Painter {
|
||||
fn alloc(&mut self) -> egui::TextureId {
|
||||
self.alloc_user_texture()
|
||||
}
|
||||
|
||||
fn set_srgba_premultiplied(
|
||||
fn alloc_srgba_premultiplied(
|
||||
&mut self,
|
||||
id: egui::TextureId,
|
||||
size: (usize, usize),
|
||||
srgba_pixels: &[Color32],
|
||||
) {
|
||||
) -> egui::TextureId {
|
||||
let id = self.alloc_user_texture();
|
||||
self.set_user_texture(id, size, srgba_pixels);
|
||||
id
|
||||
}
|
||||
|
||||
fn free(&mut self, id: egui::TextureId) {
|
||||
|
|
|
@ -66,17 +66,14 @@ impl WebBackend {
|
|||
}
|
||||
|
||||
impl epi::TextureAllocator for webgl::Painter {
|
||||
fn alloc(&mut self) -> egui::TextureId {
|
||||
self.alloc_user_texture()
|
||||
}
|
||||
|
||||
fn set_srgba_premultiplied(
|
||||
fn alloc_srgba_premultiplied(
|
||||
&mut self,
|
||||
id: egui::TextureId,
|
||||
size: (usize, usize),
|
||||
srgba_pixels: &[Color32],
|
||||
) {
|
||||
) -> egui::TextureId {
|
||||
let id = self.alloc_user_texture();
|
||||
self.set_user_texture(id, size, srgba_pixels);
|
||||
id
|
||||
}
|
||||
|
||||
fn free(&mut self, id: egui::TextureId) {
|
||||
|
|
|
@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|||
|
||||
## Unreleased
|
||||
|
||||
* Simplify `TextureAllocator` interface.
|
||||
|
||||
|
||||
## 0.7.0 - 2021-01-04
|
||||
|
||||
|
|
|
@ -190,15 +190,11 @@ pub struct IntegrationInfo {
|
|||
/// How to allocate textures (images) to use in [`egui`].
|
||||
pub trait TextureAllocator {
|
||||
/// Allocate a new user texture.
|
||||
fn alloc(&mut self) -> egui::TextureId;
|
||||
|
||||
/// Set or change the pixels of a user texture.
|
||||
fn set_srgba_premultiplied(
|
||||
fn alloc_srgba_premultiplied(
|
||||
&mut self,
|
||||
id: egui::TextureId,
|
||||
size: (usize, usize),
|
||||
srgba_pixels: &[egui::Color32],
|
||||
);
|
||||
) -> egui::TextureId;
|
||||
|
||||
/// Free the given texture.
|
||||
fn free(&mut self, id: egui::TextureId);
|
||||
|
|
Loading…
Reference in a new issue