Android 高斯模糊(1) 窗口模糊及java侧基本流程简述

Android 12起,原生支持了窗口模糊接口能力。

Window blurs | Android Open Source Project

按照官方的描述分为以下两种

  1. blur behind :使窗口后面的整个屏幕变得模糊

  2. 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使用上面的接口就可以实现模糊效果。

相关推荐
Cutecat_24 分钟前
视频字幕处理工具横向:提取模式 vs 编辑模式,该如何选择
android·前端·ios·语音识别
2601_961765292 小时前
【分享】PlayerPro媒体音乐播放器 完整专业版
android·媒体
JohnnyDeng944 小时前
【Android】Android 包体积优化:R8/ProGuard 深度配置全攻略
android·性能优化·kotlin·jetpack
故渊at4 小时前
第九板块:Android 多媒体体系 | 第二十四篇:Camera Service 与 HAL3 成像流水线
android·camera·多媒体体系·hal3
Jinkxs7 小时前
Python基础 - 初识内置函数 Python自带的便捷工具
android·java·python
私人珍藏库7 小时前
【Android】VLLO-韩国热门手机剪辑APP
android·app·工具·软件·多功能
Cloud_Shy6188 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第六章 Item 40 - 43)
android·开发语言·人工智能·笔记·python·学习方法
AFinalStone9 小时前
Android12 U盘插拔链路源码全解析(五):Framework层(下) StorageManagerService
android·frameworks
林九生10 小时前
【实用技巧】MySQL 绿色版一键路径更新脚本详解 —— update_path.bat 深度解析
android·数据库·mysql
故渊at12 小时前
第十三板块:Android 综合架构与未来演进 | 第三十一篇:Android 架构演进与 Fuchsia OS 的挑战
android·架构·宏内核·微内核·fuchsia·ipc 性能博弈