jrmuizel / raqote

Rust 2D graphics library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using external buffer for DrawTarget

l4l opened this issue · comments

commented

It seems for now raqote can only draw into a self-allocated buffer (here). It would be nice, if it can support passing a buffer as an argument and draw into it. That's particularly useful for mmap-ed memory provided by some other entity, e.g. on Wayland.

commented

I've taken a look at current implementation and I believe it should be possible to wrap buffer in Cow-like structure, which simply have a mutable access to the inner value (without cloning comparing to std::Cow). Although, this change breaks compatibility due to a lifetime parameter at DrawTarget.

Yeah, I've wanted something like this, but was discouraged by having to leak the lifetime parameter into the DrawTarget api.

commented

Another possible solution is an additional type with a lifetime, but that probably make some duplicated code.

UPD: If anyone else interested, as a temporary solution, I forked the crate with a proposed solution patch (the one with lifetime). Though, I hope we end up with an approach in the upstream which suits everyone.

A method that may be better for upstreaming than adding a lifetime parameter is to just abstract out the buffer provider as a generic type parameter that defaults to Vec. That means all existing users continue to work as-is, and anyone with a buffer can create DrawTarget<&[u32]> instead with a different constructor.

I might make a fork with this implemented sometime soon.