rust-windowing / softbuffer

Easily write an image to a window
Apache License 2.0
333 stars 48 forks source link

Android platform #44

Open i509VCB opened 1 year ago

i509VCB commented 1 year ago

EDIT: A bit of discussion on this happened in https://github.com/rust-windowing/swbuf/pull/8#issuecomment-1362056178

MarijnS95 commented 1 year ago

The most likely way to implement this would be via ANativeWindow_lock(), whose API has been wrapped for the ndk crate at:

https://github.com/MarijnS95/ndk/compare/window-lock

We can merge this into the ndk crate after some slight cleanup.

ids1024 commented 1 year ago

Using ANativeWindow_lock, is there a way to request that the window uses an BGRX buffer like SoftBuffer currently assumes? Or would we have to deal with a different pixel format? (And if that could vary, things become even more complicated...)

MarijnS95 commented 1 year ago

@ids1024 There is a way to change the format, see how the example has a (commented out) way to do this:

https://github.com/rust-mobile/ndk/compare/master...MarijnS95:ndk:window-lock#diff-f302a34380ff35caef13c0c344aede7c4c0bf4ae4b957e066ad0bc73eb4cc418R10

Unfortunately there's no support for BGRX, only RGBX:

https://docs.rs/ndk/latest/ndk/hardware_buffer_format/enum.HardwareBufferFormat.html https://developer.android.com/reference/android/hardware/HardwareBuffer

ids1024 commented 1 year ago

Right. Hopefully we can at least expect RGBX to always work?

In comparison Wayland's wl_shm has a huge list of formats but only requires a compositor to support argb8888 and xrgb8888 (which a little endian, so the same as formats called BGRA/BGRX elsewhere).

It seems we can't expect to use the same pixel format everywhere while also getting performant no-copy presentation.

MarijnS95 commented 1 year ago

First draft pushed at https://github.com/MarijnS95/softbuffer/compare/android, untested but at least letting you folks know that this is being worked on :)

MarijnS95 commented 1 year ago

Updated the branch, it is fully working now (but will need some cleanup before it is ready for a PR, though let me know if I should open it as draft already)!

A couple important notes:

ids1024 commented 1 year ago

Currently, I think https://github.com/rust-windowing/softbuffer/issues/98 is the most important missing feature/API in softbuffer. It's awkward if some platforms only support RGBX/RGBA but others only support BGRA/BGRX. We'll want an API that helps with conversion, but for maximum performance an application would have to support both...

though let me know if I should open it as draft already

I generally think it's good to have a draft PR. That makes it a bit more visible that some work has been done, and easy to comment on. Relative to just linking a branch on the issue.

Exposing a Buffer::stride hopefully doesn't make the API too confusing. It isn't really any harder to deal with, it's just something users of the library have to deal with, but may be unfamiliar with if they haven't seen how this works at a low level before.

MarijnS95 commented 1 year ago

@ids1024 done in #130.

Depends on whether you want to go for performance, convenient/consistent/simple API across all platforms, or both.

In the NDK API I've added a helper function that returns an iterator yielding one mutable scanline/slice at a time without padding:

https://github.com/MarijnS95/ndk/blob/b520751834fc7d5a6c9759fa9c9ea8ce3d0f3213/ndk/src/native_window.rs#L214-L230 And when put to use: https://github.com/rust-mobile/rust-android-examples/pull/7

Maybe this could help users? Or x/y getters/setters? The possibilities are (unfortunately) endless.