rendy_chain/schedule/
mod.rsmod family;
mod queue;
mod submission;
use std::collections::HashMap;
use std::ops::{Index, IndexMut};
pub use self::{
family::Family,
queue::{Queue, QueueId},
submission::{Submission, SubmissionId},
};
#[derive(Clone, Debug)]
pub struct Schedule<S> {
map: HashMap<rendy_core::hal::queue::QueueFamilyId, Family<S>>,
ordered: Vec<SubmissionId>,
}
impl<S> Schedule<S> {
pub fn new() -> Self {
Schedule {
map: HashMap::default(),
ordered: Vec::new(),
}
}
pub fn total(&self) -> usize {
self.ordered.len()
}
pub fn ordered(&self) -> impl Iterator<Item = &Submission<S>> {
let ref map = self.map;
self.ordered
.iter()
.map(move |&sid| map[&sid.family()].submission(sid).unwrap())
}
pub fn family_count(&self) -> usize {
self.map.len()
}
pub fn queue_count(&self) -> usize {
self.map.iter().map(|x| x.1.queue_count()).sum()
}
pub fn iter(&self) -> impl Iterator<Item = &Family<S>> {
self.map.values()
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Family<S>> {
self.map.values_mut()
}
pub fn family(&self, fid: rendy_core::hal::queue::QueueFamilyId) -> Option<&Family<S>> {
self.map.get(&fid)
}
pub fn family_mut(
&mut self,
fid: rendy_core::hal::queue::QueueFamilyId,
) -> Option<&mut Family<S>> {
self.map.get_mut(&fid)
}
pub fn queue(&self, qid: QueueId) -> Option<&Queue<S>> {
self.family(qid.family())
.and_then(|family| family.queue(qid))
}
pub fn queue_mut(&mut self, qid: QueueId) -> Option<&mut Queue<S>> {
self.family_mut(qid.family())
.and_then(|family| family.queue_mut(qid))
}
pub fn submission(&self, sid: SubmissionId) -> Option<&Submission<S>> {
self.queue(sid.queue())
.and_then(|queue| queue.submission(sid))
}
pub fn submission_mut(&mut self, sid: SubmissionId) -> Option<&mut Submission<S>> {
self.queue_mut(sid.queue())
.and_then(|queue| queue.submission_mut(sid))
}
pub fn ensure_family(&mut self, fid: rendy_core::hal::queue::QueueFamilyId) -> &mut Family<S> {
self.map.entry(fid).or_insert_with(|| Family::new(fid))
}
pub fn ensure_queue(&mut self, qid: QueueId) -> &mut Queue<S> {
self.ensure_family(qid.family()).ensure_queue(qid)
}
pub(crate) fn set_queue(&mut self, queue: Queue<S>) {
let qid = queue.id();
*self.ensure_queue(qid) = queue;
}
pub fn build_order(&mut self) {
let mut oredered: Vec<SubmissionId> = Vec::new();
{
let submissions = self
.iter()
.flat_map(|family| family.iter().flat_map(|queue| queue.iter()));
for submission in submissions {
if submission.submit_order() == !0 {
continue;
}
let len = oredered.len();
if len <= submission.submit_order() {
oredered.extend((len..submission.submit_order() + 1).map(|_| submission.id()));
} else {
oredered[submission.submit_order()] = submission.id();
}
}
}
log::debug!("Schedule order: {:#?}", oredered);
self.ordered = oredered;
}
}
impl<S> Index<QueueId> for Schedule<S> {
type Output = Queue<S>;
fn index(&self, qid: QueueId) -> &Queue<S> {
self.queue(qid).unwrap()
}
}
impl<S> IndexMut<QueueId> for Schedule<S> {
fn index_mut(&mut self, qid: QueueId) -> &mut Queue<S> {
self.queue_mut(qid).unwrap()
}
}
impl<S> Index<SubmissionId> for Schedule<S> {
type Output = Submission<S>;
fn index(&self, sid: SubmissionId) -> &Submission<S> {
self.submission(sid).unwrap()
}
}
impl<S> IndexMut<SubmissionId> for Schedule<S> {
fn index_mut(&mut self, sid: SubmissionId) -> &mut Submission<S> {
self.submission_mut(sid).unwrap()
}
}