Android 12起,原生支持了窗口模糊接口能力。
Window blurs | Android Open Source Project
按照官方的描述分为以下两种
-
blur behind :使窗口后面的整个屏幕变得模糊
-
background blur :使指定区域的底层内容模糊
其本质都是对当前surface后面的surface进行模糊显示。
blur behind
公开接口可通过主题配置或者代码设置,两种方式任选其一
xml
<style name="BlurBehind" parent="Theme.CarLauncher">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBlurBehindEnabled">true</item>
<item name="android:windowBlurBehindRadius">70dp</item>
</style>
kotlin
// 代码方式设置
window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND)
window.attributes = window.attributes.apply {
blurBehindRadius = 70
}
blurBehindRadius参数会传递到system_server端 WindowState中,由Dimmer设置(新建了一个dimLayer),最终是调用
setBackgroundBlurRadius 接口


background blur
xml
<style name="BackgroundBlur" parent="Theme.CarLauncher">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackgroundBlurRadius">70dp</item>
</style>
kotlin
// 代码方式设置
window.setBackgroundBlurRadius(70)
setBackgroundBlurRadius时做了什么事?
仅仅是创建了一个BackgroundBlurDrawable并设置到DecorView的背景中




BackgroundBlurDrawable
BackgroundBlurDrawable到底做了什么神奇的事情实现了背景高斯模糊?
BackgroundBlurDrawable代码只有短短几百行,相对比较简单,
核心只是:记录下模糊参数:坐标范围,圆角值,模糊半径,透明度。
圆角值,模糊半径,透明度都可以通过BackgroundBlurDrawable公开接口设置,不多解释。
坐标范围是通过监听RenderNode位置变化获得

Aggregator可以保存多个BackgroundBlurDrawable,也就是说:
一个窗口上是可以使用多个BackgroundBlurDrawable

一个ViewRootImpl对应一个Aggregator,Aggregator对应多个BackgroundBlurDrawable

Aggregator如何连通ViewRootImpl和BackgroundBlurDrawable
每一次刷新界面时,如果Aggregator模糊区域发生更新 或者存在模糊区域,
会读取当前的模糊区域参数,发送给SurfaceFlinger端

每个BackgroundBlurDrawable都可以转换成一个BlurRegion数据结构,
所以多个Drawable就转换成了一个BlurRegion数组


一个BlurRegion可以转换为一个float数组,那么多个BlurRegion可转换为一个float二维数组
即 多个BackgroundBlurDrawable记录的模糊参数,最终转换成了float二维数组

然后将表示模糊参数的float二维数组通过setBlurRegions下设

有一个细节是:监听模糊坐标范围mRect更新并没有立即执行,而是作为Runable和帧数一起记下,在getBlurRegionsToDispatchToSf时再真正执行,这样避免了多线程同步问题。
如果mRect立即更新,该操作在线程池中执行,无法保证执行顺序。假如监听到第100帧的RenderNoder位置变化mRect立即更新了,但是renderThread此时正在请求渲染第99帧,却使用了第100帧的位置,就会出现位置错位的问题。


总结
综上,blur behind 本质上是使用了setBackgroundBlurRadius , background blur本质上是使用了setBlurRegions。
那么利用上面的接口,就可以实现surface的实时模糊显示。
比如:在一个surfaceView中显示视频或者3D画面,上面的surface使用上面的接口就可以实现模糊效果。