I currently have a custom modifier built using `dr...
# compose-android
j
I currently have a custom modifier built using
drawBehind { ... }
to draw a custom shadow directly on a Canvas. I’m currently in the process of optimizing code and I wondered if I would get any performance benefits using a
Modifier.graphicLayer { ... }
or maybe even using the DrawModifierNode? I’m curious to know if the
Modifier.graphicLayer {}
would potentially gain performance improvements because out-the-box
Modifier.shadow(...)
uses a graphicLayer to achieve a shadow. Any thoughts, recommendations or links to articles that would help me make a good decision.
r
The default implementation uses a
graphicsLayer
because it has to. Material shadows are handled at the
RenderNode
level, which is what a
graphicsLayer
creates by default
You’d use a
graphicsLayer
in one of two situations:
• If re-issuing all the draw calls is expensive or if you want to apply common transforms cheaply to all those draw calls (rotation for instance) without having to re-record them
• If rasterizing all the draw calls is expensive, you would use an offscreen
graphicsLayer
(which is effectively a GPU texture). This should rarely be needed, but it sometimes is for certain effects (blend modes) or correctness (stacked alpha)
j
My current modifier accepts an offset to customize the shadow spread, a
Shape
and a
Color
. Inside of my
drawIntoCanvas
block I assign the provided Color on the Canvas’ paint object and set anti alias to true, then I grab the outline of the specified Shape and draw it on the Canvas. Does using a graphicLayer seem like a misuse? The shadow will need alpha blending because some content with a Modifier will be drawn on top of a custom background that we’d like to still be visible.
r
A
graphicsLayer
seems unnecessary for what you are doing. Alpha blending is fine; alpha becomes a problem when you want to apply alpha to multiple overlapping primitives. It’s not the same drawing a 50% transparent blue rectangle and then a 50% transparent red rectangle that overlaps the blue rectangle, vs drawing the two rectangles 100% opaque and making the result 50% transparent
👍🏿 1
💯 1
j
Gotcha! Would you recommend to use a
DrawModifierNode
or just replace my
drawBehind {}
with a
drawWithCache {}
?