Skip to content

Commit

Permalink
Merge pull request #2692 from Azorlogh/make-engine-send
Browse files Browse the repository at this point in the history
Make `iced_wgpu::Renderer` `Send` by using `Arc` in text/triangle caches
  • Loading branch information
hecrj authored Dec 10, 2024
2 parents d618229 + 917feb9 commit ce13c22
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
25 changes: 12 additions & 13 deletions wgpu/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use crate::graphics::text::{font_system, to_color, Editor, Paragraph};

use rustc_hash::FxHashMap;
use std::collections::hash_map;
use std::rc::{self, Rc};
use std::sync::atomic::{self, AtomicU64};
use std::sync::Arc;
use std::sync::{self, Arc};

pub use crate::graphics::Text;

Expand Down Expand Up @@ -37,7 +36,7 @@ pub enum Item {
pub struct Cache {
id: Id,
group: cache::Group,
text: Rc<[Text]>,
text: Arc<[Text]>,
version: usize,
}

Expand All @@ -55,7 +54,7 @@ impl Cache {
Some(Self {
id: Id(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed)),
group,
text: Rc::from(text),
text: Arc::from(text),
version: 0,
})
}
Expand All @@ -65,7 +64,7 @@ impl Cache {
return;
}

self.text = Rc::from(text);
self.text = Arc::from(text);
self.version += 1;
}
}
Expand All @@ -76,8 +75,8 @@ struct Upload {
transformation: Transformation,
version: usize,
group_version: usize,
text: rc::Weak<[Text]>,
_atlas: rc::Weak<()>,
text: sync::Weak<[Text]>,
_atlas: sync::Weak<()>,
}

#[derive(Default)]
Expand All @@ -90,7 +89,7 @@ struct Group {
atlas: glyphon::TextAtlas,
version: usize,
should_trim: bool,
handle: Rc<()>, // Keeps track of active uploads
handle: Arc<()>, // Keeps track of active uploads
}

impl Storage {
Expand Down Expand Up @@ -136,7 +135,7 @@ impl Storage {
),
version: 0,
should_trim: false,
handle: Rc::new(()),
handle: Arc::new(()),
}
});

Expand Down Expand Up @@ -167,7 +166,7 @@ impl Storage {
group.should_trim =
group.should_trim || upload.version != cache.version;

upload.text = Rc::downgrade(&cache.text);
upload.text = Arc::downgrade(&cache.text);
upload.version = cache.version;
upload.group_version = group.version;
upload.transformation = new_transformation;
Expand Down Expand Up @@ -206,8 +205,8 @@ impl Storage {
transformation: new_transformation,
version: 0,
group_version: group.version,
text: Rc::downgrade(&cache.text),
_atlas: Rc::downgrade(&group.handle),
text: Arc::downgrade(&cache.text),
_atlas: Arc::downgrade(&group.handle),
});

group.should_trim = cache.group.is_singleton();
Expand All @@ -226,7 +225,7 @@ impl Storage {
.retain(|_id, upload| upload.text.strong_count() > 0);

self.groups.retain(|id, group| {
let active_uploads = Rc::weak_count(&group.handle);
let active_uploads = Arc::weak_count(&group.handle);

if active_uploads == 0 {
log::debug!("Dropping text atlas: {id:?}");
Expand Down
14 changes: 7 additions & 7 deletions wgpu/src/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::Buffer;

use rustc_hash::FxHashMap;
use std::collections::hash_map;
use std::rc::{self, Rc};
use std::sync::atomic::{self, AtomicU64};
use std::sync::{self, Arc};

const INITIAL_INDEX_COUNT: usize = 1_000;
const INITIAL_VERTEX_COUNT: usize = 1_000;
Expand All @@ -31,7 +31,7 @@ pub enum Item {
#[derive(Debug, Clone)]
pub struct Cache {
id: Id,
batch: Rc<[Mesh]>,
batch: Arc<[Mesh]>,
version: usize,
}

Expand All @@ -48,13 +48,13 @@ impl Cache {

Some(Self {
id: Id(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed)),
batch: Rc::from(meshes),
batch: Arc::from(meshes),
version: 0,
})
}

pub fn update(&mut self, meshes: Vec<Mesh>) {
self.batch = Rc::from(meshes);
self.batch = Arc::from(meshes);
self.version += 1;
}
}
Expand All @@ -64,7 +64,7 @@ struct Upload {
layer: Layer,
transformation: Transformation,
version: usize,
batch: rc::Weak<[Mesh]>,
batch: sync::Weak<[Mesh]>,
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -113,7 +113,7 @@ impl Storage {
new_transformation,
);

upload.batch = Rc::downgrade(&cache.batch);
upload.batch = Arc::downgrade(&cache.batch);
upload.version = cache.version;
upload.transformation = new_transformation;
}
Expand All @@ -135,7 +135,7 @@ impl Storage {
layer,
transformation: new_transformation,
version: 0,
batch: Rc::downgrade(&cache.batch),
batch: Arc::downgrade(&cache.batch),
});

log::debug!(
Expand Down

0 comments on commit ce13c22

Please sign in to comment.