How to draw a polygon with its background content blurred in skia?

329 views
Skip to first unread message

Lucida

unread,
Oct 12, 2023, 9:15:12 AM10/12/23
to skia-discuss
I have tried something like SkImageBlur and SkRuntimeEffect, but it seems that it's not a trival work.
Figma renders:

blur.jpg
 

John Stiles

unread,
Oct 12, 2023, 7:06:19 PM10/12/23
to skia-discuss
Take a look at this example code for a demonstration of applying filters on top of an image:
https://github.com/google/skia/blob/main/gm/imagefilters.cpp#L153
The example uses clipRRect, but clipPath should work equally well.

926339623fd0f2bf41d276e430e236d7.png

Lucida

unread,
Oct 13, 2023, 9:58:12 AM10/13/23
to skia-discuss
Thanks a lot!
It works. Before your answer, I save the background and write blur shader manually, it's really a mess. I dont' known there is already a dedicated parameter (backdrop in SaveLayerRec) for post processing. 

craste...@gmail.com

unread,
Oct 13, 2023, 11:55:59 AM10/13/23
to skia-discuss
It would be helpful if there was a fiddle

John Stiles

unread,
Oct 13, 2023, 12:05:30 PM10/13/23
to skia-d...@googlegroups.com
We've got tons of example test-suite slides; maintaining a Fiddle-based copy of each one would actually be a fair amount of work! However, it should be super straightforward to port this particular slide over if you would like to try.


--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/6cb999cb-a09a-4984-80d9-32b792086d56n%40googlegroups.com.

Joe

unread,
Dec 5, 2024, 12:07:51 AM12/5/24
to skia-discuss
Is there a way to achieve this w/o using a clip this way? I'd like a generic way of having this kind of effect for any drawing over the background. So essentially, having the "clip" be based on the draw operation that happens above the saved layer before restore. So this would be used to fill shapes/text and their strokes. Is this possible in a generic way w/o having to know exactly what shape is being draw to try and provide a matching clip?

Florin Malita

unread,
Dec 6, 2024, 2:59:29 PM12/6/24
to skia-d...@googlegroups.com
On Thu, Dec 5, 2024 at 12:07 AM Joe <nick...@gmail.com> wrote:
Is there a way to achieve this w/o using a clip this way? I'd like a generic way of having this kind of effect for any drawing over the background. So essentially, having the "clip" be based on the draw operation that happens above the saved layer before restore. So this would be used to fill shapes/text and their strokes. Is this possible in a generic way w/o having to know exactly what shape is being draw to try and provide a matching clip?

When using saveLayer's backdrop filter mechanism, the whole layer is initialized with the previous layer content.  So you must then "carve out" the actual shape somehow, lest it will overwrite everything on restore.

Clipping is the natural option, and could be generalized to any draw primitive (start with the draw geometry, and apply the stroke effects, etc).  It does fall short if the draw op doesn't have sharp/well determined edges (e.g. the content itself is blurred and you expect blurred edges to be blended/modulated).

The other option is to use the draw op content as a mask, via a second layer: draw the content into a nested layer, which then acts as a mask for the backdrop filter layer on restore (blend mode: kDstIn).


That removes the clip and also modulates based on content alpha, but could be slower due to the extra saveLayer.


 
On Friday, October 13, 2023 at 9:05:30 AM UTC-7 johns...@google.com wrote:
We've got tons of example test-suite slides; maintaining a Fiddle-based copy of each one would actually be a fair amount of work! However, it should be super straightforward to port this particular slide over if you would like to try.


On Fri, Oct 13, 2023 at 11:56 AM craste...@gmail.com <craste...@gmail.com> wrote:
It would be helpful if there was a fiddle

On Thursday, October 12, 2023 at 4:06:19 PM UTC-7 johns...@google.com wrote:
Take a look at this example code for a demonstration of applying filters on top of an image:
https://github.com/google/skia/blob/main/gm/imagefilters.cpp#L153
The example uses clipRRect, but clipPath should work equally well.

926339623fd0f2bf41d276e430e236d7.png

On Thursday, October 12, 2023 at 9:15:12 AM UTC-4 Lucida wrote:
I have tried something like SkImageBlur and SkRuntimeEffect, but it seems that it's not a trival work.
Figma renders:

blur.jpg
 

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/6cb999cb-a09a-4984-80d9-32b792086d56n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.

Joe

unread,
Dec 6, 2024, 11:35:32 PM12/6/24
to skia-discuss
this makes sense. i'm just not sure how to use an arbitrary shape (say w/ stroke) for clipping. i see various clip functions (i.e. clipRect, clipRRect, ...), but nothing that allows me to clip by a shape w/ stroke. also, how does this apply to text? i'm using paragraphs to draw text and would like to apply this effect to them, either entirely or to sub areas via the paint in paragraph styles.

Joe

unread,
Dec 10, 2024, 10:17:23 PM12/10/24
to skia-discuss
i'm wondering if i can accomplish this with a single ImageFilter, or a Shader. the reason is i really want to use this "frosted glass" as a re-usable paint that can be applied to fills, strokes and to text (via Paragraph also). so i'd like something i can just apply to a Paint like i can do with SKShaders.makeLinearGradient and apply it to the Paint.shader property.

is this fairly straightforward to do? i don't have any experience working w/ skia Shaders. so hoping there're off-the-shelf shaders i can compose to get this effect etc. if needed.

really appreciate any help with this. been stuck trying to do what i know is achievable for a while just b/c of my lack of skia knowledge.

Reply all
Reply to author
Forward
0 new messages