texture freeing + render updates done a bit nicer
This commit is contained in:
@@ -1,14 +1,9 @@
|
||||
use std::{
|
||||
any::TypeId,
|
||||
marker::PhantomData,
|
||||
sync::{
|
||||
Arc,
|
||||
atomic::{AtomicU32, Ordering},
|
||||
mpsc::Sender,
|
||||
},
|
||||
};
|
||||
use std::{any::TypeId, marker::PhantomData};
|
||||
|
||||
use crate::{FnTag, Ui, Widget, WidgetLike, WidgetTag, util::Id};
|
||||
use crate::{
|
||||
FnTag, Ui, UiMsg, UiMsgSender, Widget, WidgetLike, WidgetTag,
|
||||
util::{Id, RefCounter},
|
||||
};
|
||||
|
||||
pub struct AnyWidget;
|
||||
|
||||
@@ -22,8 +17,8 @@ pub struct AnyWidget;
|
||||
pub struct WidgetId<W = AnyWidget> {
|
||||
pub(super) ty: TypeId,
|
||||
pub(super) id: Id,
|
||||
pub(super) refcount: Arc<AtomicU32>,
|
||||
pub(super) send: Sender<Id>,
|
||||
counter: RefCounter,
|
||||
send: UiMsgSender,
|
||||
_pd: PhantomData<W>,
|
||||
}
|
||||
|
||||
@@ -35,11 +30,10 @@ impl<W> std::fmt::Debug for WidgetId<W> {
|
||||
|
||||
impl<W> Clone for WidgetId<W> {
|
||||
fn clone(&self) -> Self {
|
||||
self.refcount.fetch_add(1, Ordering::Release);
|
||||
Self {
|
||||
id: self.id.duplicate(),
|
||||
ty: self.ty,
|
||||
refcount: self.refcount.clone(),
|
||||
counter: self.counter.clone(),
|
||||
send: self.send.clone(),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
@@ -47,11 +41,11 @@ impl<W> Clone for WidgetId<W> {
|
||||
}
|
||||
|
||||
impl<W> WidgetId<W> {
|
||||
pub(super) fn new(id: Id, ty: TypeId, send: Sender<Id>) -> Self {
|
||||
pub(super) fn new(id: Id, ty: TypeId, send: UiMsgSender) -> Self {
|
||||
Self {
|
||||
ty,
|
||||
id,
|
||||
refcount: AtomicU32::new(0).into(),
|
||||
counter: RefCounter::new(),
|
||||
send,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
@@ -72,15 +66,14 @@ impl<W> WidgetId<W> {
|
||||
}
|
||||
|
||||
pub fn refs(&self) -> u32 {
|
||||
self.refcount.load(Ordering::Acquire)
|
||||
self.counter.refs()
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> Drop for WidgetId<W> {
|
||||
fn drop(&mut self) {
|
||||
let refs = self.refcount.fetch_sub(1, Ordering::Release);
|
||||
if refs == 0 {
|
||||
let _ = self.send.send(self.id.duplicate());
|
||||
if self.counter.drop() {
|
||||
let _ = self.send.send(UiMsg::FreeWidget(self.id.duplicate()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,48 +1,81 @@
|
||||
use image::DynamicImage;
|
||||
|
||||
use crate::render::TexturePrimitive;
|
||||
use crate::{UiMsg, UiMsgSender, render::TexturePrimitive, util::RefCounter};
|
||||
|
||||
/// TODO: proper resource management
|
||||
pub struct TextureHandle {
|
||||
pub inner: TexturePrimitive,
|
||||
counter: RefCounter,
|
||||
send: UiMsgSender,
|
||||
}
|
||||
|
||||
/// a texture manager for a ui
|
||||
/// note that this is heavily oriented towards wgpu's renderer so the primitives don't need mapped
|
||||
#[derive(Default)]
|
||||
pub struct Textures {
|
||||
/// TODO: these are images, not views rn
|
||||
views: Vec<DynamicImage>,
|
||||
changed: bool,
|
||||
free: Vec<u32>,
|
||||
images: Vec<Option<DynamicImage>>,
|
||||
updates: Vec<Update>,
|
||||
}
|
||||
|
||||
pub struct TextureUpdates<'a> {
|
||||
pub images: Option<&'a [DynamicImage]>,
|
||||
pub enum TextureUpdate<'a> {
|
||||
Push(&'a DynamicImage),
|
||||
Set(u32, &'a DynamicImage),
|
||||
Free(u32),
|
||||
}
|
||||
|
||||
enum Update {
|
||||
Push(u32),
|
||||
Set(u32),
|
||||
Free(u32),
|
||||
}
|
||||
|
||||
impl Textures {
|
||||
pub fn add(&mut self, image: DynamicImage) -> TextureHandle {
|
||||
let view_idx = self.views.len() as u32;
|
||||
self.views.push(image);
|
||||
pub fn add(&mut self, image: DynamicImage, send: UiMsgSender) -> TextureHandle {
|
||||
let view_idx = self.push(image);
|
||||
// 0 == default in renderer; TODO: actually create samplers here
|
||||
let sampler_idx = 0;
|
||||
self.changed = true;
|
||||
TextureHandle {
|
||||
inner: TexturePrimitive {
|
||||
view_idx,
|
||||
sampler_idx,
|
||||
},
|
||||
counter: RefCounter::new(),
|
||||
send,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn updates(&mut self) -> TextureUpdates<'_> {
|
||||
if self.changed {
|
||||
self.changed = false;
|
||||
TextureUpdates {
|
||||
images: Some(&self.views),
|
||||
}
|
||||
fn push(&mut self, image: DynamicImage) -> u32 {
|
||||
if let Some(i) = self.free.pop() {
|
||||
self.images[i as usize] = Some(image);
|
||||
self.updates.push(Update::Set(i));
|
||||
i
|
||||
} else {
|
||||
TextureUpdates { images: None }
|
||||
let i = self.images.len() as u32;
|
||||
self.images.push(Some(image));
|
||||
self.updates.push(Update::Push(i));
|
||||
i
|
||||
}
|
||||
}
|
||||
|
||||
pub fn free(&mut self, idx: u32) {
|
||||
self.images[idx as usize] = None;
|
||||
self.updates.push(Update::Free(idx));
|
||||
self.free.push(idx);
|
||||
}
|
||||
|
||||
pub fn updates(&mut self) -> impl Iterator<Item = TextureUpdate<'_>> {
|
||||
self.updates.drain(..).map(|u| match u {
|
||||
Update::Push(i) => TextureUpdate::Push(self.images[i as usize].as_ref().unwrap()),
|
||||
Update::Set(i) => TextureUpdate::Set(i, self.images[i as usize].as_ref().unwrap()),
|
||||
Update::Free(i) => TextureUpdate::Free(i),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TextureHandle {
|
||||
fn drop(&mut self) {
|
||||
if self.counter.drop() {
|
||||
let _ = self.send.send(UiMsg::FreeTexture(self.inner.view_idx));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use image::DynamicImage;
|
||||
|
||||
use crate::{
|
||||
ActiveSensors, HashMap, Painter, SensorMap, TextureHandle, TextureUpdates, Textures, Widget,
|
||||
WidgetId, WidgetLike,
|
||||
ActiveSensors, HashMap, Painter, SensorMap, TextureHandle, Textures, Widget, WidgetId,
|
||||
WidgetLike,
|
||||
render::Primitives,
|
||||
util::{Id, IdTracker},
|
||||
};
|
||||
@@ -16,16 +16,22 @@ pub struct Ui<Ctx> {
|
||||
base: Option<WidgetId>,
|
||||
widgets: Widgets<Ctx>,
|
||||
updates: Vec<WidgetId>,
|
||||
del_recv: Receiver<Id>,
|
||||
del_send: Sender<Id>,
|
||||
primitives: Primitives,
|
||||
textures: Textures,
|
||||
recv: Receiver<UiMsg>,
|
||||
send: UiMsgSender,
|
||||
pub(crate) primitives: Primitives,
|
||||
pub(crate) textures: Textures,
|
||||
full_redraw: bool,
|
||||
|
||||
pub(super) active_sensors: ActiveSensors,
|
||||
pub(super) sensor_map: SensorMap<Ctx>,
|
||||
}
|
||||
|
||||
pub enum UiMsg {
|
||||
FreeWidget(Id),
|
||||
FreeTexture(u32),
|
||||
}
|
||||
pub type UiMsgSender = Sender<UiMsg>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Widgets<Ctx> {
|
||||
ids: IdTracker,
|
||||
@@ -75,15 +81,11 @@ impl<Ctx> Ui<Ctx> {
|
||||
}
|
||||
|
||||
pub fn id<W: Widget<Ctx>>(&mut self) -> WidgetId<W> {
|
||||
WidgetId::new(
|
||||
self.widgets.reserve(),
|
||||
TypeId::of::<W>(),
|
||||
self.del_send.clone(),
|
||||
)
|
||||
WidgetId::new(self.widgets.reserve(), TypeId::of::<W>(), self.send.clone())
|
||||
}
|
||||
|
||||
pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle {
|
||||
self.textures.add(image)
|
||||
self.textures.add(image, self.send.clone())
|
||||
}
|
||||
|
||||
pub fn redraw_all(&mut self, ctx: &mut Ctx)
|
||||
@@ -103,33 +105,27 @@ impl<Ctx> Ui<Ctx> {
|
||||
self.primitives = painter.finish();
|
||||
}
|
||||
|
||||
pub fn update(&mut self, ctx: &mut Ctx) -> UiRenderUpdates
|
||||
pub fn update(&mut self, ctx: &mut Ctx)
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
while let Ok(id) = self.del_recv.try_recv() {
|
||||
self.widgets.delete(id);
|
||||
}
|
||||
self.recv_msgs();
|
||||
|
||||
if self.full_redraw {
|
||||
self.redraw_all(ctx);
|
||||
self.full_redraw = false;
|
||||
UiRenderUpdates {
|
||||
primitives: Some(&self.primitives),
|
||||
textures: self.textures.updates(),
|
||||
}
|
||||
} else if self.updates.is_empty() {
|
||||
UiRenderUpdates {
|
||||
primitives: None,
|
||||
textures: self.textures.updates(),
|
||||
}
|
||||
} else {
|
||||
} else if !self.updates.is_empty() {
|
||||
// TODO: partial updates
|
||||
self.redraw_all(ctx);
|
||||
self.updates.drain(..);
|
||||
UiRenderUpdates {
|
||||
primitives: Some(&self.primitives),
|
||||
textures: self.textures.updates(),
|
||||
}
|
||||
}
|
||||
|
||||
fn recv_msgs(&mut self) {
|
||||
while let Ok(msg) = self.recv.try_recv() {
|
||||
match msg {
|
||||
UiMsg::FreeWidget(id) => self.widgets.delete(id),
|
||||
UiMsg::FreeTexture(id) => self.textures.free(id),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -230,13 +226,8 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
|
||||
full_redraw: false,
|
||||
active_sensors: Default::default(),
|
||||
sensor_map: Default::default(),
|
||||
del_send,
|
||||
del_recv,
|
||||
send: del_send,
|
||||
recv: del_recv,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UiRenderUpdates<'a> {
|
||||
pub primitives: Option<&'a Primitives>,
|
||||
pub textures: TextureUpdates<'a>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user