louiscad
05/31/2024, 10:36 PMGraphicsLayer
API from Compose UI 1.7.0-beta02, and on API 28, I get this crash quite often when I call drawLayer
from a DrawScope
provided by the record
function of another `GraphicsLayer`:
java.lang.IllegalArgumentException: Only add dependencies during a tracking
at androidx.compose.ui.graphics.InlineClassHelperKt.throwIllegalArgumentException(InlineClassHelper.kt:26)
at androidx.compose.ui.graphics.layer.ChildLayerDependenciesTracker.onDependencyAdded(ChildLayerDependenciesTracker.kt:149)
at androidx.compose.ui.graphics.layer.GraphicsLayer.addSubLayer(AndroidGraphicsLayer.android.kt:437)
at androidx.compose.ui.graphics.layer.GraphicsLayer.draw$ui_graphics_release(AndroidGraphicsLayer.android.kt:551)
at androidx.compose.ui.graphics.layer.GraphicsLayerKt.drawLayer(GraphicsLayer.kt:52)
Any clue at what could be the cause?Nader Jawad
05/31/2024, 11:00 PMlouiscad
05/31/2024, 11:17 PMlouiscad
05/31/2024, 11:20 PMNader Jawad
05/31/2024, 11:20 PMlouiscad
05/31/2024, 11:22 PMlouiscad
05/31/2024, 11:22 PMGraphicsLayer
conditionnally, and it seems to crash just after that condition switcheslouiscad
05/31/2024, 11:23 PMif
in front of my call to drawLayer
Nader Jawad
05/31/2024, 11:23 PMNader Jawad
05/31/2024, 11:24 PMlouiscad
05/31/2024, 11:25 PMtrue
, a derivedStateOf
is accessed, which then leads to the 2 `GraphicsLayer`s being recorded if needed, with one being drawn into the otherlouiscad
05/31/2024, 11:25 PMlouiscad
05/31/2024, 11:26 PMNader Jawad
05/31/2024, 11:27 PMlouiscad
05/31/2024, 11:29 PMlouiscad
05/31/2024, 11:29 PMlouiscad
05/31/2024, 11:30 PMNader Jawad
05/31/2024, 11:30 PMlouiscad
05/31/2024, 11:30 PMlouiscad
05/31/2024, 11:35 PMval firstLayer = rememberGraphicsLayerAsState { layer ->
layer.record {
drawCoolStuff()
}
}
val secondLayer = rememberGraphicsLayerAsState { layer ->
layer.compositingStrategy = CompositingStrategy.Offscreen
val l = firstLayer.get()
layer.record {
drawLayer(l)
}
}
Canvas(…) {
drawLayer(secondLayer)
}
louiscad
05/31/2024, 11:37 PMrememberGraphicsLayerAsState
is some custom code I wrote to get a SizeDependentState<GraphicsLayer>
, on which I can call get()
when I'm in another SizeDependentState.Scope
like the lambda passed to rememberGraphicsLayerAsState
, or a DrawScope
.
It's using derivedStateOf
+ states for the size and density/fontScale under the hood.louiscad
05/31/2024, 11:39 PMrememberGraphicsLayer()
without that.
If you struggle to, I can share a Kotlin file that contains all that's needed to have rememberGraphicsLayerAsState
work.louiscad
05/31/2024, 11:40 PMTolriq
06/01/2024, 7:26 AMZoltan Demant
06/02/2024, 4:24 AMTolriq
06/02/2024, 6:22 AMZoltan Demant
06/02/2024, 6:58 AMTolriq
06/02/2024, 7:01 AMZoltan Demant
06/02/2024, 7:05 AMAndrey Kulikov
06/03/2024, 12:19 PMAndrey Kulikov
06/03/2024, 12:23 PMAndrey Kulikov
06/03/2024, 12:26 PMDrawScope.draw(..., graphicsLayer = ...)
somewhere directly? or do you save the DrawScope object you get from layer.record { ... }
lamba somewhere in order to use it later?louiscad
06/03/2024, 12:28 PMNader Jawad
06/03/2024, 5:46 PMlouiscad
06/03/2024, 8:38 PM