-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Send
for Image
#18
Conversation
/// The pointer is expected to be valid and have a ref-count of at least one. | ||
/// The pointer is expected to be valid and have a ref-count of at *exactly* one, | ||
/// with an alpha map (if any) that has a ref count of one. | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps also adding a comment that increasing the ref-count by by-passing pixman-rs is breaking Send
?
This changes the assumptions of the crate to assert that a valid `pixmap::Image` has a ref-count of 1, with an alpha map that has a ref-count of 1. Consequently, `set_alpha_map` must take ownership of its argument. My understanding, looking at the pixmap code, is that this should ensure it is safe to `Send` the `Image` to another thread. Of course this does limit the API of this crate if a consumer wants to use multiple references (though `pixman_image_ref` wasn't wrapper already), or wants to use `set_alpha_map` while still keeping a reference to the alpha map. The first issue can just be solved by wrapping in `Rc`, though. Smithay/smithay#1500
Okay, I also had to re-read some stuff in the pixman sources. Do we really have to change the signature to take ownership? Not sure this does indeed solve anything. So maybe just marking the (iirc pixman has some tls based cache, so possible that this might interfere somehow with |
Ah, I guess I wasn't paying enough attention to the lifetimes there. That should also work.
I wasn't thinking of scoped threads. We can't send I think you could:
And this would be a thread safety issue, since the spawned thread might be be reading (in a non-thread-safe way) the same properties of the alpha map image as are being modified? So maybe it needs to take ownership in the |
So I guess at a minimum, |
But at the cost of not allowing the same alpha map to be used with different images. I would really like to avoid this limitation if possible.
I don't think this would be possible, the scope guarantees that all threads are finished. Moving the alpha image to a second thread in the same scope should be prevented by the borrow checker. So imo this should be safe. |
Here's an example of what I mean: fn main() {
let alpha_map_image = pixman::Image::new(pixman::FormatCode::A1, 1920, 1080, true).unwrap();
std::thread::scope(|s| {
let mut image1 =
pixman::Image::new(pixman::FormatCode::R8G8B8A8, 1920, 1080, true).unwrap();
image1 = image1.set_alpha_map(&alpha_map_image, 0, 0);
let mut image2 =
pixman::Image::new(pixman::FormatCode::R8G8B8A8, 1920, 1080, true).unwrap();
image2 = image2.set_alpha_map(&alpha_map_image, 0, 0);
s.spawn(move || {
dbg!(image1);
});
s.spawn(move || {
dbg!(image2);
});
alpha_map_image.add_triangles((0, 0), &[pixman::Triangle::new((0, 0), (0, 1), (1, 0))]);
});
} A couple things:
|
Yeah, right. Changing all functions that mutate the image to take
I also fail to see how this could be encoded in rust today other then to limit the api by taking ownership of the alpha image :( |
I guess if there was a single global mutex that is locked any time an image is unrefed (I guess including setting / clearing the alpha map) that would would. |
Closing in favor of #19. |
This changes the assumptions of the crate to assert that a valid
pixmap::Image
has a ref-count of 1, with an alpha map that has a ref-count of 1. Consequently,set_alpha_map
must take ownership of its argument.My understanding, looking at the pixmap code, is that this should ensure it is safe to
Send
theImage
to another thread. Of course this does limit the API of this crate if a consumer wants to use multiple references (thoughpixman_image_ref
wasn't wrapper already), or wants to useset_alpha_map
while still keeping a reference to the alpha map. The first issue can just be solved by wrapping inRc
, though.Smithay/smithay#1500