linebender / piet

An abstraction for 2D graphics.
Apache License 2.0
1.25k stars 94 forks source link

More flexible `RenderContext::make_image` #553

Closed LucaCiucci closed 1 year ago

LucaCiucci commented 1 year ago

I am trying to create a druid widget that renders a 3d scene using WGPU. To do this, I render the scene into a texture, get the data as a &[u8] and paint it using piet's draw_image.
Unfortunately, each row in the texture has a stride with an alignment of COPY_BYTES_PER_ROW_ALIGNMENT bytes so, in order to use it with make_image, I have to convert it first. This is quite annoying and feels unnecessary.

I think this situation could be common in other situations such as showing images/videos from raw data. Maybe it could be useful to create another function, for example RenderContext::make_image_flat that accepts a row pitch, just like image::FlatSamples that stores the layout information in SampleLayout

xStrom commented 1 year ago

Yes I think this is a reasonable method to add.

On the topic of naming it, is flat a common way to describe such a method? It's definitely shorter than an explicit make_image_with_stride.

LucaCiucci commented 1 year ago

I'm not sure if that's the best name to give this function. This was the first name that came to my mind because I was reading the FlatSamples doc.

I don't know many other libraries, afaik Qt does not provide a similar function for images and pixmaps, Skia has MakeRasterData that is undocumented but an example usage is shown here.

One idea might be to implement make_image_with_stride and make the function to accept a stride parameter so that the name make_image_flat can be used in the future for more complex layouts. What do you think?

xStrom commented 1 year ago

Yeah that sounds like a decent plan. Let's go with make_image_with_stride for now. It's possible to deprecate it later if someone comes up with a better name.

LucaCiucci commented 1 year ago

I will try to implement this in a fork if you are ok with that

xStrom commented 1 year ago

Yes that sounds good. Then open a pull request here and I can review.