I'm trying to capture the contents of a Composable...
# compose-desktop
z
I'm trying to capture the contents of a Composable function as an image. What I do is define a GraphicsLayer and use it in
drawWithContent
modifier to draw the contents of the composable onto the graphics layer:
Copy code
.drawWithContent {
    graphicsLayer.record {
        this@drawWithContent.drawContent()
    }
    ...
}
when I run this code on Web target, it works as expected. But if I run it against desktop, I get a compile error:
Copy code
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00000001284a77cc, pid=21921, tid=73991
#
# JRE version: OpenJDK Runtime Environment (21.0.4) (build 21.0.4+-12422083-b607.1)
# Java VM: OpenJDK 64-Bit Server VM (21.0.4+-12422083-b607.1, mixed mode, tiered, compressed oops, compressed class ptrs, g1 gc, bsd-amd64)
# Problematic frame:
# C  [libskiko-macos-x64.dylib+0x1027cc]  SkPictureRecorder::finishRecordingAsPicture()+0x2c
#
# No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
JNI global refs:
JNI global refs: 132, weak refs: 6

JNI global refs memory usage: 2099, weak refs: 833
If I remove
this@drawWithContent.drawContent()
and replace it with draw commands like
drawRect(...)
, it works as expected. Any idea what might be wrong?
k
File a bug on Skiko. Looks like something is not quite right in one of the Skia bindings and memory / pointer management
👍 1
z
Nevermind, I just found my issue. I was applying the same Modifier that was containing
drawWithContent
to a parent and inner child composable. 🤦‍♂️ Removing it from the child composable fixed it. It was something like
Copy code
@Composable
fun MyElement(modifier: Modifier = Modifier) {
    Surface(modifier) {
        Text(modifier)    // Passing same modifier - big mistake! Read Copilot code carefully guys!!   
    }
}

val screenshotModifier = Modifier.drawWithContent {
    graphicsLayer.record {
        this@drawWithContent.drawContent()
    }
    ...
}

MyElement(screenshotModifier)
P.S. I'm quite happy to see you're still in the Android area, Kiril. I remember watching your talks about your work on Google Play Store 9-10 years ago 🙂