diff --git a/libs/ux-api/src/minigfx/clip.rs b/libs/ux-api/src/minigfx/clip.rs index 8a57cb714..3faa3a9c7 100644 --- a/libs/ux-api/src/minigfx/clip.rs +++ b/libs/ux-api/src/minigfx/clip.rs @@ -59,15 +59,36 @@ impl ObjectList { /// bounds that the system can handle. Thus, this method can fail returning the pushed object, /// at which point one should send the draw list to the graphics engine, and retry the push. pub fn push(&mut self, item: ClipObjectType) -> Result<(), ClipObjectType> { + let serialized_size = + self.list.capacity() * size_of::() + size_of::>(); + log::debug!("sersize {}", serialized_size); // TODO: export the capacity limit of a buffer. The origin of the capacity limit is equal to // the size of a page of memory, plus 256 bytes for "scratch" area for rkyv to work in. I did // try to use the .replace() method with an allocation of a large enough buffer to hold the whole // Vec, but it seems to fail. I think it could be that the scratch space hard-coded into the IPC // library is not big enough... - if self.list.capacity() * size_of::() + size_of::>() < 4096 - 256 - { - self.list.push(item); - Ok(()) + // + // Update: in a later version of Rust, this is even worse - the margin seems to need to be + // fairly large for serialization to work. Setting it to 1k short of 4096 now. + if serialized_size < 3072 { + if serialized_size > 2048 { + // pushing can cause a re-allocation that breaks sending the vec. Allocs happen on roughly + // power-of-two boundaries in this implementation, so as a heuristic flag a problem only after + // we've exceeded half the page size + if self.list.capacity() > self.list.len() { + // we're almost out of space, but we still have reserved capacity + self.list.push(item); + Ok(()) + } else { + // pushing it would cause a capacity bump to over our acceptable size + Err(item) + } + } else { + // if the size is less than half a page, go ahead and do the push; a re-alloc won't bring + // us over the page size + self.list.push(item); + Ok(()) + } } else { Err(item) } diff --git a/libs/ux-api/src/service/gfx.rs b/libs/ux-api/src/service/gfx.rs index 7487877fe..15ec5a122 100644 --- a/libs/ux-api/src/service/gfx.rs +++ b/libs/ux-api/src/service/gfx.rs @@ -715,10 +715,13 @@ impl Gfx { } /// V2-only API - pub fn draw_object_list(&self, list: ObjectList) -> Result<(), xous::Error> { + pub fn draw_object_list(&self, mut list: ObjectList) -> Result<(), xous::Error> { + list.list.shrink_to_fit(); + let size = list.list.capacity() * size_of::() + size_of::>(); let buf = match Buffer::into_buf(list) { Ok(b) => b, Err(e) => { + log::info!("size {}", size); log::error!("err: {:?}", e); panic!("error") }