鸿蒙OS 开机动画流程

打印 上一主题 下一主题

主题 1857|帖子 1857|积分 5571

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
开机动画的启动与竣事

foundation\graphic\graphic_2d\graphic.cfg
  1. "services" : [{
  2.         "name" : "render_service",
  3.         "path" : ["/system/bin/render_service"],
  4.         "critical" : [0, 5, 60],
  5.         "importance" : -20,
  6.         "uid" : "graphics",
  7.         "gid" : ["system", "tp_host"],
  8.         "caps" : ["SYS_NICE"],
  9.         "permission" : [
  10.             "ohos.permission.REPORT_RESOURCE_SCHEDULE_EVENT"
  11.         ],
  12.         "secon" : "u:r:render_service:s0",
  13.         "jobs" : {
  14.             "on-restart" : "services:restartrender_service"
  15.         },
  16.         "once" : 0
  17.     }, {
  18.         "name" : "bootanimation",
  19.         "path" : ["/system/bin/bootanimation"],
  20.         "bootevents": [
  21.             "bootevent.bootanimation.started",
  22.             "bootevent.bootanimation.finished"
  23.         ],
  24.         "importance" : -20,
  25.         "once" : 1,
  26.         "uid" : "graphics",
  27.         "gid" : ["graphics"],
  28.         "secon" : "u:r:bootanimation:s0"
  29.     }
  30. ]
复制代码
foundation\graphic\graphic_2d\frameworks\bootanimation\src\main.cpp
  1. int main(int argc, const char *argv[])
  2. {
  3.     LOGI("main enter");
  4.     //等待renderService的初始化
  5.     WaitRenderServiceInit();
  6.     Rosen::RSInterfaces& interface = Rosen::RSInterfaces::GetInstance();
  7.     Rosen::ScreenId defaultId = interface.GetDefaultScreenId();
  8.     if (defaultId == Rosen::INVALID_SCREEN_ID) {
  9.         LOGE("invalid default screen id, return");
  10.         return 0;
  11.     }
  12.     //获取屏的宽高
  13.     Rosen::RSScreenModeInfo modeinfo = interface.GetScreenActiveMode(defaultId);
  14.     int screenWidth = modeinfo.GetScreenWidth();
  15.     int screenHeight = modeinfo.GetScreenHeight();
  16.     //运行动画
  17.     BootAnimation bootAnimation;
  18.     bootAnimation.Run(defaultId, screenWidth, screenHeight);
  19.     LOGI("main exit");
  20.     return 0;
  21. }
复制代码
foundation\graphic\graphic_2d\frameworks\bootanimation\src\boot_animation.cpp
  1. void BootAnimation::Run(Rosen::ScreenId id, int screenWidth, int screenHeight)
  2. {
  3.     LOGI("Run enter");
  4.     //解析配置的json文件
  5.     animationConfig_.ParserCustomCfgFile();
  6.     Rosen::RSInterfaces& interface = Rosen::RSInterfaces::GetInstance();
  7.     //根据配置的json文件中是否配置了其他的屏,获取对应宽高
  8.     if (animationConfig_.GetRotateScreenId() >= 0) {
  9.         id = interface.GetActiveScreenId();
  10.         LOGI("GetActiveScreenId: " BPUBU64 "", id);
  11.         Rosen::RSScreenModeInfo modeinfo = interface.GetScreenActiveMode(id);
  12.         screenWidth = modeinfo.GetScreenWidth();
  13.         screenHeight = modeinfo.GetScreenHeight();
  14.         LOGI("screenWidth: %{public}d, screenHeight: %{public}d", screenWidth, screenHeight);
  15.         if (id > 0) {
  16.             LOGI("SetScreenPowerStatus POWER_STATUS_OFF_FAKE: 0");
  17.             interface.SetScreenPowerStatus(0, Rosen::ScreenPowerStatus::POWER_STATUS_OFF_FAKE);
  18.             LOGI("SetScreenPowerStatus POWER_STATUS_ON: " BPUBU64 "", id);
  19.             interface.SetScreenPowerStatus(id, Rosen::ScreenPowerStatus::POWER_STATUS_ON);
  20.         }
  21.     } else if (interface.GetScreenPowerStatus(id) != Rosen::ScreenPowerStatus::POWER_STATUS_ON) {
  22.         //设置屏幕状态为点亮状态
  23.         interface.SetScreenPowerStatus(id, Rosen::ScreenPowerStatus::POWER_STATUS_ON);
  24.     }
  25.     //异步处理init,playvideo,playsound
  26.     runner_ = AppExecFwk::EventRunner::Create(false);
  27.     mainHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner_);
  28.     mainHandler_->PostTask(std::bind(&BootAnimation::Init, this, id, screenWidth, screenHeight));
  29.     LOGI("PostTask Init");
  30. #ifdef PLAYER_FRAMEWORK_ENABLE
  31.     //根据配置设置是否支持开机视频
  32.     if (animationConfig_.IsBootVideoEnabled()) {
  33.         mainHandler_->PostTask(std::bind(&BootAnimation::PlayVideo, this));
  34.         LOGI("PostTask PlayVideo");
  35.     } else {
  36.         mainHandler_->PostTask(std::bind(&BootAnimation::PlaySound, this));
  37.         LOGI("PostTask PlaySound");
  38.     }
  39. #else
  40.     LOGI("player_framework part is not enabled.");
  41. #endif
  42.     runner_->Run();
  43. }
  44. ......
  45. void BootAnimation::Init(Rosen::ScreenId defaultId, int32_t width, int32_t height)
  46. {
  47.     defaultId_ = defaultId;
  48.     windowWidth_ = width;
  49.     windowHeight_ = height;
  50.     LOGI("Init enter, width: %{public}d, height: %{public}d", width, height);
  51.     //计算中心点坐标
  52.     InitPicCoordinates();
  53.     //配置RSDisplayNodeConfig的displayOffset,frame,bounds等参数
  54.     InitRsDisplayNode();
  55.     //配置RSSurfaceNodeConfig的PositionZ,FrameGravity,BackgroundColor,bounds等参数
  56.     InitRsSurfaceNode();
  57. #ifdef PLAYER_FRAMEWORK_ENABLE
  58.     //根据配置设置是否支持开机视频
  59.     if (animationConfig_.IsBootVideoEnabled()) {
  60.         LOGI("Init end");
  61.         return;
  62.     }
  63. #endif
  64.     //使用序列帧动画播放
  65.     LOGI("Playing boot animation using sequence frames.");
  66.     //投票bootevent.bootanimation.started 为true
  67.     system::SetParameter("bootevent.bootanimation.started", "true");
  68.     auto& rsClient = OHOS::Rosen::RSInterfaces::GetInstance();
  69.     while (receiver_ == nullptr) {
  70.         //获取VSyncReceiver的代理端,用于显示
  71.         receiver_ = rsClient.CreateVSyncReceiver("BootAnimation", mainHandler_);
  72.     }
  73.     VsyncError ret = receiver_->Init();
  74.     if (ret) {
  75.         LOGE("vsync receiver init failed: %{public}d", ret);
  76.         PostTask(std::bind(&AppExecFwk::EventRunner::Stop, runner_));
  77.         return;
  78.     }
  79.     //初始化renderSurface
  80.     InitRsSurface();
  81.     ROSEN_TRACE_BEGIN(HITRACE_TAG_GRAPHIC_AGP, "BootAnimation::preload");
  82.     //通过读取配置的图片动画,获取对应的大小
  83.     if (animationConfig_.ReadPicZipFile(imageVector_, freq_)) {
  84.         imgVecSize_ = imageVector_.size();
  85.     } else {
  86.         LOGE("Read PicZipFile failed");
  87.         PostTask(std::bind(&AppExecFwk::EventRunner::Stop, runner_));
  88.         return;
  89.     }
  90.     ROSEN_TRACE_END(HITRACE_TAG_GRAPHIC_AGP);
  91.     OHOS::Rosen::VSyncReceiver::FrameCallback fcb = {
  92.         .userData_ = this,
  93.         //显示回调
  94.         .callback_ = std::bind(&BootAnimation::OnVsync, this),
  95.     };
  96.     //开机动画的送显帧率
  97.     int32_t changefreq = static_cast<int32_t>((1000.0 / freq_) / 16);
  98.     ret = receiver_->SetVSyncRate(fcb, changefreq);
  99.     if (ret) {
  100.         PostTask(std::bind(&AppExecFwk::EventRunner::Stop, runner_));
  101.         LOGE("SetVSyncRate failed: %{public}d %{public}d %{public}d", ret, freq_, changefreq);
  102.         return;
  103.     } else {
  104.         LOGI("SetVSyncRate success: %{public}d %{public}d", freq_, changefreq);
  105.     }
  106. }
  107. ......
  108. void BootAnimation::OnVsync()
  109. {
  110.     //绘制
  111.     PostTask(std::bind(&BootAnimation::Draw, this));
  112. }
  113. ......
  114. void BootAnimation::Draw()
  115. {
  116.     if (picCurNo_ < (imgVecSize_ - 1)) {
  117.         picCurNo_ = picCurNo_ + 1;
  118.     } else {
  119.         //开机动画播放完成退出
  120.         CheckExitAnimation();
  121.         return;
  122.     }
  123.     ROSEN_TRACE_BEGIN(HITRACE_TAG_GRAPHIC_AGP, "BootAnimation::Draw RequestFrame");
  124.     auto frame = rsSurface_->RequestFrame(windowWidth_, windowHeight_);
  125.     if (frame == nullptr) {
  126.         LOGE("Draw frame is nullptr");
  127.         return;
  128.     }
  129.     ROSEN_TRACE_END(HITRACE_TAG_GRAPHIC_AGP);
  130. #ifdef NEW_RENDER_CONTEXT
  131.     if (rsSurface_ == nullptr) {
  132.         LOGE("rsSurface is nullptr");
  133.         return;
  134.     }
  135.     auto canvas = rsSurface_->GetCanvas();
  136.     //开始绘制
  137.     OnDraw(canvas, picCurNo_);
  138.     ROSEN_TRACE_BEGIN(HITRACE_TAG_GRAPHIC_AGP, "BootAnimation::Draw FlushFrame");
  139.     rsSurface_->FlushFrame();
  140.     ROSEN_TRACE_END(HITRACE_TAG_GRAPHIC_AGP);
  141. #else
  142.     framePtr_ = std::move(frame);
  143.     auto canvas = framePtr_->GetCanvas();
  144.     OnDraw(canvas, picCurNo_);
  145.     ROSEN_TRACE_BEGIN(HITRACE_TAG_GRAPHIC_AGP, "BootAnimation::Draw FlushFrame");
  146.     rsSurface_->FlushFrame(framePtr_);
  147.     ROSEN_TRACE_END(HITRACE_TAG_GRAPHIC_AGP);
  148. #endif
  149. }
  150. .....
  151. void BootAnimation::OnDraw(SkCanvas* canvas, int32_t curNo)
  152. {
  153.     if (canvas == nullptr) {
  154.         LOGE("OnDraw canvas is nullptr");
  155.         return;
  156.     }
  157.     if (curNo > (imgVecSize_ - 1) || curNo < 0) {
  158.         return;
  159.     }
  160.     std::shared_ptr<ImageStruct> imgstruct = imageVector_[curNo];
  161.     sk_sp<SkImage> image = imgstruct->imageData;
  162.     ROSEN_TRACE_BEGIN(HITRACE_TAG_GRAPHIC_AGP, "BootAnimation::OnDraw in drawRect");
  163.     SkPaint backPaint;
  164.     backPaint.setColor(SK_ColorBLACK);
  165.     canvas->drawRect(SkRect::MakeXYWH(0.0, 0.0, windowWidth_, windowHeight_), backPaint);
  166.     ROSEN_TRACE_END(HITRACE_TAG_GRAPHIC_AGP);
  167.     ROSEN_TRACE_BEGIN(HITRACE_TAG_GRAPHIC_AGP, "BootAnimation::OnDraw in drawImageRect");
  168.     SkPaint paint;
  169.     SkRect rect;
  170.     rect.setXYWH(pointX_, pointY_, realWidth_, realHeight_);
  171.     canvas->drawImageRect(image.get(), rect, SkSamplingOptions(), &paint);
  172.     ROSEN_TRACE_END(HITRACE_TAG_GRAPHIC_AGP);
  173. }
  174. ......
  175. bool BootAnimation::CheckExitAnimation()
  176. {
  177.     if (!isAnimationEnd_) {
  178.         LOGI("Boot animation is end");
  179.         //投票
  180.         system::SetParameter("bootevent.bootanimation.finished", "true");
  181.         isAnimationEnd_ = true;
  182.     }
  183.     //检查所有的投票事件是否都已经完成
  184.     std::string bootEventCompleted = system::GetParameter("bootevent.boot.completed", "false");
  185.     if (bootEventCompleted == "true") {
  186.         mainHandler_->PostTask(std::bind(&AppExecFwk::EventRunner::Stop, runner_));
  187.         LOGI("Read bootevent.boot.completed is true");
  188.         return true;
  189.     }
  190.     return false;
  191. }
  192. #ifdef PLAYER_FRAMEWORK_ENABLE
  193. void BootAnimation::PlaySound()
  194. {
  195.     LOGI("PlaySound start");
  196.     bool bootSoundEnabled = BootAnimationUtils::GetBootAnimationSoundEnabled();
  197.     if (bootSoundEnabled == true) {
  198.         LOGI("PlaySound read bootSoundEnabled is true");
  199.         if (soundPlayer_ == nullptr) {
  200.             soundPlayer_ = Media::PlayerFactory::CreatePlayer();
  201.         }
  202.         std::string uri = animationConfig_.GetSoundUrl();
  203.         soundPlayer_->SetSource(uri);
  204.         soundPlayer_->SetLooping(false);
  205.         soundPlayer_->PrepareAsync();
  206.         soundPlayer_->Play();
  207.     }
  208.     LOGI("PlaySound end");
  209. }
  210. #endif
  211. #ifdef PLAYER_FRAMEWORK_ENABLE
  212. void BootAnimation::PlayVideo()
  213. {
  214.     LOGI("PlayVideo start w:%{public}d h:%{public}d", windowWidth_, windowHeight_);
  215.     if (!animationConfig_.IsBootVideoEnabled()) {
  216.         return;
  217.     }
  218.     fcb_ = {
  219.         .userData_ = this,
  220.         .callback_ = std::bind(&BootAnimation::CloseVideoPlayer, this),
  221.     };
  222.     LOGI("PlayVideo setVideo screenId:%{public}d", (int32_t)defaultId_);
  223.     bootVideoPlayer_ = std::make_shared<BootVideoPlayer>();
  224.     bootVideoPlayer_->SetVideoPath(
  225.         defaultId_ == 0 ? animationConfig_.GetBootVideoPath() : animationConfig_.GetBootExtraVideoPath());
  226.     bootVideoPlayer_->SetPlayerSurface(rsSurfaceNode_ ? rsSurfaceNode_->GetSurface() : nullptr);
  227.     bootVideoPlayer_->SetCallback(&fcb_);
  228.     if (!bootVideoPlayer_->PlayVideo()) {
  229.         LOGE("Play video failed.");
  230.         CloseVideoPlayer();
  231.     }
  232. }
  233. #endif
复制代码
foundation\graphic\graphic_2d\frameworks\bootanimation\src\boot_animationconfig.cpp
  1. void BootAnimationConfig::ParserCustomCfgFile()
  2. {
  3.     //读取对应的开机播放的图片动画,视频,音频
  4.     std::string file = GetCustomCfgFile();
  5.     if (!file.empty()) {
  6.         //解析出对应的文件地址
  7.         ReadCustomBootConfig(file, custConfig_);
  8.         //根据配置设置是否支持开机视频
  9.         CheckBootVideoEnabled();
  10.     }
  11. }
  12. ......
  13. std::string BootAnimationConfig::GetCustomCfgFile()
  14. {
  15.     std::string ret = "";
  16.     char buf[MAX_PATH_LEN] = {0};
  17.     //获取文件etc/bootanimation/bootanimation_custom_config.json
  18.     char *pfile = GetOneCfgFile(BOOT_CUSTOM_PATHSUFFIX.c_str(), buf, MAX_PATH_LEN);
  19.     if (pfile != nullptr) {
  20.         LOGI("get one filePath:%{public}s", pfile);
  21.         return pfile;
  22.     } else {
  23.         LOGI("not find %{public}s", BOOT_CUSTOM_PATHSUFFIX.c_str());
  24.     }
  25.     return ret;
  26. }
复制代码
bootanimation_custom_config.json文件如下
设置了对应的开机播放的图片动画,视频,音频
  1. {
  2.         "cust.bootanimation.pics": "/sys_prod/etc/bootanimation/cust_bootpic.zip",
  3.         "cust.bootanimation.sounds": "/sys_prod/etc/bootanimation/cust_bootsound.wav",
  4.         "cust.bootanimation.video": "/sys_prod/etc/bootanimation/cust_bootvideo.mp4"
  5. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

麻花痒

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表