I have an AndroidView because I need to display a ...
# compose-android
c
I have an AndroidView because I need to display a video feed from a webcam (idk how I got myself in this mess either 😂 ) everything seems to work fine but my camera feed is upside down. My code
Copy code
AndroidView(
    factory = { ctx ->
      AspectRatioSurfaceView(ctx).apply {
how can I rotate/mirror it?
Things I've tried. Adding amodifier directly on the AndroidView
Copy code
AndroidView(
      modifier = Modifier.graphicsLayer(rotationX = 180f),
Doesn't work. No change in the result.
Another thing I tried is doing it in view land
Copy code
AndroidView(
    factory = { ctx ->
      AspectRatioSurfaceView(ctx).apply {
          rotationX = 180F
still doesn't work
i also tried wrapping it in a box and flipping the box. same thing. no change. I'm out of ideas...
I think because its a surface view it just can't be rotated?
Maybe I can do something with the holder? 🤷
switched to AspectRatioTextureView instead of surface view. and setting the rotationX worked there. phew. interesting why surface view didn't want to work. lol
r
Compose now has
AndroidExternalSurface
to use in place of
SurfaceView
And the rotation/mirroring shouldn’t be done at the view level, but with the Camera APIs
(you have to do this on phones as the camera sensor might be mounted rotated relative to the screen for instance)
c
ooh. ill take a look at AndroidExternalSurface. TIL I did try to flip with the camera apis, but the only usb camera library I could find isn't really well supported unfortunately and the rotation doesn't work: https://github.com/jiangdongguo/AndroidUSBCamera
@romainguy, any chance to get a TLDR of AndroidExternalSurface from your POV? I tried using it and read the docs, but still don't understand why AndroidExternalSurface had to be a new addition/replacing SurfaceView
r
It's not replacing SurfaceView, it's a Compose-friendly API for SurfaceView. The use of coroutines makes managing the surface's lifecycle much easier for instance
c
Thank you!
r
Also note that AndroidExternal/EmbeddedExternalSurface have the same API to manage the surface
It makes it much easier to switch from one to the other compared to texture/SurfaceView
c
Cool cool. i wish there was a built in way to show usb webcams in android (like camerax or something) but for now, that random library will have to do
s
would be the same available for GLSurfaceView?
To make OpenGL embeddings easier.
r
You can use SurfaceView for this
s
Hm, but how to set rendering callback with SurfaceView? https://developer.android.com/develop/ui/views/graphics/opengl/environment and the documentation still suggest to use GLSurfaceView
r
GLSurfaceView is just a SurfaceView
s
so what it is purpose than?
r
It's a small convenience that creates the GL context and a thread for you
s
can we have the same a small convenient widget that creates gl context android for compose 😄
r
No, this shouldn't be a specific widget, it should be a separate API that takes a Surface as an input. GLSurfaceView was not a good design choice
And for this you can use the androidx graphics-core library
that is indeed a nicer approach
Whenever I return to working with OpenGL on Android, I've been using Grafika for examples, but it's quite outdated now, being 11 years old. Could you recommend any more recent resources or best practices for OpenGL on Android?"
r
Not really. The latest version of OpenGL is itself 10 years old now, things haven't changed
s
I see. OpenGL is solid as a rock 😄 I came across a new library, androidx.graphics, with really useful helpers – appreciate the tip. It's surprising that it's not mentioned anywhere in the OpenGL section of the official documentation.
May I ask one more question? Is it possible for me to render a bitmap that uses the 'HARDWARE' configuration into my FBO, or is the Bitmap.Config#HARDWARE configuration entirely controlled by the system, making it impossible for me to access and bind its data to my frame buffer?