Can I use a `ComposeView` to draw a composition in...
# compose
c
Can I use a
ComposeView
to draw a composition into a Canvas, off-screen? More or less this:
Copy code
val canvas = Canvas()
val composeView = ComposeView(context)
composeView.setContent {
  // Call composables here

  SideEffect {
    composeView.draw(canvas)
  }
}
Important to note that the ComposeView is never attached to a window / drawn in the UI. The view is created programmatically, as above. I am ultimately trying to get the composition into a Bitmap for the purpose of drawing into an OpenGL texture. (If there is a better approach, I’m all ears.) Thanks for any help!
👀 1
r
There are better ways, by using a SurfaceTexture and getting a Canvas for the SurfaceTexture's Surface
This bypasses the need to upload the Bitmap as a texture
On recent API levels you can go further by using a HardwareRenderer
c
Thanks for responding. Sorry I should have provided more detail. In my use case, we are capturing video from the camera and sending it out over webrtc. We allow the user to draw into the webrtc video frames.
So I am already using SurfaceTexture with WebRTC, and I can draw into the frames using a Canvas. But I need to draw a structured hierarchy of views (i.e.
View
or
@Composable
), not just plain unstructured canvas drawing.
And I would like to reuse some Composables I already have. Altogether I would like to do as much with Compose as possible
r
Right my point was just that you should not get through a Bitmap ^^
I'm not sure what a ComposeView needs exactly but you at least need to measure and layout it
In addition views often rely on m being attached to a window to work properly.
@Adam Powell probably knows more about the requirements to drive a ComposeView
a
Yeah there are a couple
ViewTreeFoo
things ComposeView looks for
c
@romainguy thanks for the feedback about the bitmap. I’ll take a look at that! Putting aside Compose for a moment, basically what I’m trying to do is define a hierarchy of UI elements (whether View or Composable) off-screen, so that I can draw it into a GL texture.
Just following up here, I was able to do roughly what I want by using a
Presentation
targeting a
VirtualDisplay
backed by a
Surface(SurfaceTexture)
r
👍