一、bootanimtion服务
1.1 init阶段bootanimation服务配置
从Android 7.0开始,谷歌将bootnimation服务从init.rc中分离出来,单独存放在frameworks/base/cmds/bootanimation/目录下bootanim.rc文件中:
1 | service bootanim /system/bin/bootanimation |
bootanimation服务的用户和用户组分别为graphic和graphics audio。需要关注的disabled,即在init启动阶段该服务是不会启动的。oneshot表示该服务只启动一次。
1.2 surfaceflinger服务调用bootnimation
当SurfaceFlinger服务启动时,它会通过修改系统属性ctl.start的值来通知init进程启动bootanimation服务。
早期的版本中,SurfaceFlinger服务是在SystemServer服务中启动的;Android 5.0之后变为init.rc脚本中控制其在init阶段启动。Android 7.0之后谷歌又将其独立为surfaceflinger.rc文件,存放在frameworks/native/services/surfaceflinger/目录下:
1 | service surfaceflinger /system/bin/surfaceflinger |
接下来看看SurfaceFlinger的入口文件main_surfaceflinger.cpp:
1 | int main(int, char**) { |
main函数主要任务就是:先新建SurfaceFlinger对象,然后调用init方法,接着调用run方法。
在SurfaceFlinger.cpp的init方法中,创建一系列事件线程后,调用startBootAnim方法启动动画。
1 | void SurfaceFlinger::init() { |
在startBootAnim函数中,将service.bootanim.exit属性置为0,将ctl.start属性置为bootanim。
1 | void SurfaceFlinger::startBootAnim() { |
1.3 init进程处理属性变化
下面来看init.cpp的main函数:
1 | int main(int argc, char** argv) { |
接着看property_service.cpp的start_property_service方法,调用register_epoll_handler函数监听属性变化。当属性值变化时,调用handle_property_set_fd
1 | void start_property_service() { |
1 | static void handle_property_set_fd() |
调用handle_control_message方法启动bootanima服务
1 | void handle_control_message(const std::string& msg, const std::string& name) { |
二、 开机动画启动
2.1 开机动画显示流程
1 | int main() |
接下来看看BootAnimation.h中的声明:
1 | class BootAnimation : public Thread, public IBinder::DeathRecipient |
接下来看看BootAnimation.cpp中各函数的具体实现:
BootAnimation间接继承自RefBase类,并重写了其onFirstRef方法,所以智能指针第一次调用BootAnimation时即先调用onFirstRef方法。
1 | void BootAnimation::onFirstRef() { |
当onFirstRef调用了父类的run方法后,系统会创建一个新线程,该线程执行前会先调用readyToRun方法执行一系列初始化操作:
1 | status_t BootAnimation::readyToRun() { |
调用threadLoop方法显示动画
1 | bool BootAnimation::threadLoop() |
2.2 系统默认动画配置
1 | bool BootAnimation::android() |
2.3 自定义动画配置
1 | bool BootAnimation::movie() |
loadAnimation方法加载动画
1 | BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) |
parseAnimationDesc方法解析desc文件,确定动画大小速度等信息。
1 | bool BootAnimation::parseAnimationDesc(Animation& animation) |
preloadZip处理片段信息
1 | bool BootAnimation::preloadZip(Animation& animation) |
playAnimation方法播放动画
1 | bool BootAnimation::playAnimation(const Animation& animation) |
releaseAnimation方法释放动画
1 | void BootAnimation::releaseAnimation(Animation* animation) const |
当SystemServer进程中关键服务都起来后,就会将Launcher启动起来,待Launcher启动后会发送idle通知给ActivityManagerService,进而传递给SurfaceFlinger,然后将service.bootanim.exit属性置为0,即退出动画播放。