Hi, in a multiplatform project is there an efficie...
# compose
p
Hi, in a multiplatform project is there an efficient way to draw a composition to a native texture and then sample the texture in my preexisting, custom C++ graphics/shader library? I.e. on Android draw to a GL texture, and on iOS draw to a Metal texture (my graphics lib already has abstractions and helpers to work with either GL or Metal backends).
(CC: @romainguy @Ivan Matkov)
I guess I'm wondering if there's a modern update to this experimental/lwjgl-integration
i
Too many cases/fallbacks to do it reliably. Especially on Desktop. To avoid copying it into CPU you need to share the same GPU context with skia, and only that is complicated
thank you color 2
s
Regarding the Android part, while I can't speak for iOS or Desktop platforms, you can directly render Compose UI into an OpenGL texture on Android, specifically
GLES11Ext.GL_TEXTURE_EXTERNAL_OES
. This is done through the
GraphicsLayer
API from the latest Compose version. You create an android.graphics.SurfaceTexture, attach it to an android.view.Surface, and get a hardware-accelerated canvas. Then, using
CanvasDrawScope
, you can draw the Compose UI layer onto the canvas. In my current project, I use this approach extensively. This writes the UI to the external texture in OpenGL memory. Then, in the layerExternalTexture.setOnFrameAvailableListener, you receive a callback when the frame is ready, and you can sample this texture in your shader as samplerExternalOES type. Hopefully, my code isn’t too convoluted, but it might help give you some ideas 😄. Feel free to explore my project and ask questions. I'll try to answer them.
p
Thanks @Sergey Y., this is a good idea! In the past I have used
SurfaceTexture
, and sampling with
samplerExternalOES
, but in the pre-Compose days of Android. I wonder if there is a similar pattern on iOS to configure a Metal-based canvas.
s
Yeah, iOS is a different story. Compose for iOS relies on Skia, which is bundled with it, similar to how Flutter works. So, I guess you would need to bridge with Skia and extract textures from it to achieve the same result.