OpenHarmony系统解决方案 - 接入多个显示设备卡开机Logo

系统 OpenHarmony
在窗口子系统中AbstractDisplayController控制器的初始化与屏幕连接事件的接收存在时序问题,启动成功时窗口子系统收到屏幕连接事件的时机均在AbstractDisplayController控制器的初始化之后,所以可以正常进入系统。

想了解更多关于开源的内容,请访问:

51CTO 开源基础软件社区

https://ost.51cto.com

问题描述

问题环境

系统版本:OpenHarmony-3.2-Release。

问题现象

接入多个显示设备后,启动系统偶现卡开机Logo。

异常效果:

系统卡在开机Logo界面,长时间无法显示开机动画,并且无法进入系统。

OpenHarmony系统解决方案 - 接入多个显示设备卡开机Logo-开源基础软件社区OpenHarmony系统解决方案 - 接入多个显示设备卡开机Logo-开源基础软件社区

正常效果:

系统启动成功,显示开机动画,开机动画结束后显示锁屏界面。

OpenHarmony系统解决方案 - 接入多个显示设备卡开机Logo-开源基础软件社区OpenHarmony系统解决方案 - 接入多个显示设备卡开机Logo-开源基础软件社区

问题原因

  • 在窗口子系统中AbstractDisplayController控制器的初始化与屏幕连接事件的接收存在时序问题,启动成功时窗口子系统收到屏幕连接事件的时机均在AbstractDisplayController控制器的初始化之后,所以可以正常进入系统。
  • 当先接收到屏幕连接事件,再进行AbstractDisplayController控制器的初始化操作时,则会由于OnAbstractScreenConnect函数的处理逻辑导致设置默认屏幕操作失败。

解决方案

修改默认屏幕设置条件,解决AbstractDisplayController控制器加载慢于Display通知屏幕连接事件时,造成的无法设置默认屏幕问题。

修改文件:foundation/window/window_manager/dmserver/src/abstract_display_controller.cpp

AbstractDisplayController::OnAbstractScreenConnect函数中,将以下代码:

if (group->combination_ == ScreenCombination::SCREEN_ALONE || group->GetChildCount() == 1) {

替换为:

ScreenId dmsScreenId = abstractScreenController_->GetDefaultAbstractScreenId();
sptr<AbstractDisplay> display = GetAbstractDisplayByScreen(dmsScreenId);
if (group->combination_ == ScreenCombination::SCREEN_ALONE || group->GetChildCount() == 1 || display == nullptr) {

替换后完整函数代码:

void AbstractDisplayController::OnAbstractScreenConnect(sptr<AbstractScreen> absScreen)
{
    if (absScreen == nullptr) {
        WLOGFE("absScreen is null");
        return;
    }
    WLOGI("connect new screen. id:%{public}" PRIu64"", absScreen->dmsId_);
    std::lock_guard<std::recursive_mutex> lock(mutex_);
    sptr<AbstractScreenGroup> group = absScreen->GetGroup();
    if (group == nullptr) {
        WLOGE("the group information of the screen is wrong");
        return;
    }
    ScreenId dmsScreenId = abstractScreenController_->GetDefaultAbstractScreenId();
    sptr<AbstractDisplay> display = GetAbstractDisplayByScreen(dmsScreenId);
    if (group->combination_ == ScreenCombination::SCREEN_ALONE || group->GetChildCount() == 1 || display == nullptr) {
        BindAloneScreenLocked(absScreen);
    } else if (group->combination_ == ScreenCombination::SCREEN_MIRROR) {
        WLOGI("OnAbstractScreenConnect, ScreenCombination::SCREEN_MIRROR, AddScreenToMirrorLocked");
        AddScreenToMirrorLocked(absScreen);
    } else if (group->combination_ == ScreenCombination::SCREEN_EXPAND) {
        WLOGI("OnAbstractScreenConnect, ScreenCombination::SCREEN_EXPAND, AddScreenToExpandLocked");
        AddScreenToExpandLocked(absScreen);
    } else {
        WLOGE("support in future. combination:%{public}u", group->combination_);
    }
}

定位过程

落盘异常开机日志,发现Launcher和SystemUI等系统应用在启动过程中报错,无法获取DefaultDisplayInfo。

08-05 17:18:29.046   521  1088 D C04201/AbstractScreenController: <177>GetDefaultAbstractScreenId: GetDefaultAbstractScreenId, screen:0
08-05 17:18:29.046   521  1088 D C04201/DisplayManagerService: <190>GetDefaultDisplayInfo: GetDefaultDisplayInfo 0
08-05 17:18:29.046   521  1088 E C04201/AbstractDisplayController: <105>GetAbstractDisplayByScreen: fail to get AbstractDisplay 0
08-05 17:18:29.047   521  1088 E C04201/DisplayManagerService: <193>GetDefaultDisplayInfo: fail to get displayInfo by id: invalid display
08-05 17:18:29.047   240  1090 I C01800/SAMGR: SystemAbilityManagerStub::OnReceived, code = 12, callerPid = 1156, flags= 0
08-05 17:18:29.047  1156  1156 W C04201/DisplayManagerProxy: <52>GetDefaultDisplayInfo: DisplayManagerProxy::GetDefaultDisplayInfo SendRequest nullptr.
08-05 17:18:29.047  1156  1156 D C03f00/ArkCompiler: [ecmascript] Throw error: JsDisplayManager::OnGetDefaultDisplay failed.

查找默认屏幕设置逻辑,追踪Log发现默认屏幕信息从abstractDisplayMap_中获取。

// foundation/window/window_manager/dmserver/src/abstract_display_controller.cpp
sptr<AbstractDisplay> AbstractDisplayController::GetAbstractDisplayByScreen(ScreenId screenId) const
{
    if (screenId == SCREEN_ID_INVALID) {
        WLOGFE("screen id is invalid.");
        return nullptr;
    }
    std::lock_guard<std::recursive_mutex> lock(mutex_);
    for (auto iter : abstractDisplayMap_) {
        sptr<AbstractDisplay> display = iter.second;
        if (display->GetAbstractScreenId() == screenId) {
            return display;
        }
    }
    WLOGFE("fail to get AbstractDisplay %{public}" PRIu64"", screenId);
    return nullptr;
}

abstractDisplayMap_对象会在两个位置被insert数据,BindAloneScreenLocked函数和AddScreenToExpandLocked函数。而两个函数拥有同一个入口函数AbstractDisplayController::OnAbstractScreenConnect。

// foundation/window/window_manager/dmserver/src/abstract_display_controller.cpp
void AbstractDisplayController::OnAbstractScreenConnect(sptr<AbstractScreen> absScreen)
{
    if (absScreen == nullptr) {
        WLOGFE("absScreen is null");
        return;
    }
    WLOGI("connect new screen. id:%{public}" PRIu64"", absScreen->dmsId_);
    std::lock_guard<std::recursive_mutex> lock(mutex_);
    sptr<AbstractScreenGroup> group = absScreen->GetGroup();
    if (group == nullptr) {
        WLOGE("the group information of the screen is wrong");
        return;
    }
    if (group->combination_ == ScreenCombination::SCREEN_ALONE || group->GetChildCount() == 1) {
        BindAloneScreenLocked(absScreen);
    } else if (group->combination_ == ScreenCombination::SCREEN_MIRROR) {
        WLOGI("OnAbstractScreenConnect, ScreenCombination::SCREEN_MIRROR, AddScreenToMirrorLocked");
        AddScreenToMirrorLocked(absScreen);
    } else if (group->combination_ == ScreenCombination::SCREEN_EXPAND) {
        WLOGI("OnAbstractScreenConnect, ScreenCombination::SCREEN_EXPAND, AddScreenToExpandLocked");
        AddScreenToExpandLocked(absScreen);
    } else {
        WLOGE("support in future. combination:%{public}u", group->combination_);
    }
}

查看源码对应Log,发现异常情况下屏幕OnAbstractScreenConnect函数的执行操作是一起完成的,此时ScrrenID为0的屏幕ScreenCombination属性为ScreenCombination::SCREEN_MIRROR。而在正常情况下OnAbstractScreenConnect函数是分开执行的。

异常Log:

08-05 17:18:26.923   521   923 I C04201/AbstractDisplayController: <152>connect new screen. id:0
08-05 17:18:26.923   521   923 I C04201/AbstractDisplayController: <162>OnAbstractScreenConnect, ScreenCombination::SCREEN_MIRROR, AddScreenToMirrorLocked
08-05 17:18:26.923   521   656 I C04201/AbstractScreenController: <1161>operator(): screenId:2, trigger:[foundation]
08-05 17:18:26.923   332   430 I C01799/MemMgr: MultiAccountManager::Init The manager initial succeed, accountCount = 0.
08-05 17:18:26.923   521   923 I C04201/AbstractDisplayController: <558>bind display to mirror. screen:0
08-05 17:18:26.923   521   656 I C04200/ClientAgentContainer: <98>GetAgentsByType: no such type of agent registered! type:2
08-05 17:18:26.923   521   923 I C04201/AbstractScreenController: <203>RegisterAbstractScreenCallback: dmsScreenId :1
08-05 17:18:26.923   521   923 I C04201/AbstractDisplayController: <152>connect new screen. id:1
08-05 17:18:26.923   521   923 E C04201/AbstractScreenController: <159>did not find screen:18446744073709551615
08-05 17:18:26.923   521   923 E C04201/AbstractDisplayController: <156>the group information of the screen is wrong
08-05 17:18:26.923   521   923 I C04201/AbstractScreenController: <203>RegisterAbstractScreenCallback: dmsScreenId :2
08-05 17:18:26.923   521   923 I C04201/AbstractDisplayController: <152>connect new screen. id:2
08-05 17:18:26.923   521   923 I C04201/AbstractDisplayController: <162>OnAbstractScreenConnect, ScreenCombination::SCREEN_MIRROR, AddScreenToMirrorLocked
08-05 17:18:26.923   521   923 I C04201/AbstractDisplayController: <558>bind display to mirror. screen:2

正常Log:

08-05 17:23:07.081   547   770 I C04201/AbstractDisplayController: <152>connect new screen. id:0
08-05 17:23:07.081   547   770 D C04201/AbstractScreenController: <177>GetDefaultAbstractScreenId: GetDefaultAbstractScreenId, screen:0

08-05 17:23:07.088   547   770 I C04201/AbstractDisplayController: <152>connect new screen. id:2
08-05 17:23:07.088   547   770 I C04201/AbstractDisplayController: <162>OnAbstractScreenConnect, ScreenCombination::SCREEN_MIRROR, AddScreenToMirrorLocked
08-05 17:23:07.088   547   770 I C04201/AbstractDisplayController: <558>bind display to mirror. screen:2

查看OnAbstractScreenConnect的触发逻辑有两种。

第一种,AbstractDisplayController初始化时注册屏幕事件回调,注册后遍历dmsScreenMap_调用AbstractDisplayController::OnAbstractScreenConnect初始化在回调注册前记录的屏幕数据。

// foundation/window/window_manager/dmserver/src/abstract_screen_controller.cpp
void AbstractDisplayController::Init(sptr<AbstractScreenController> abstractScreenController)
{
    WLOGFD("display controller init");
    displayCount_ = 0;
    abstractScreenController_ = abstractScreenController;
    abstractScreenCallback_ = new(std::nothrow) AbstractScreenController::AbstractScreenCallback();
    if (abstractScreenCallback_ == nullptr) {
        WLOGFE("abstractScreenCallback init failed");
        return;
    }
    abstractScreenCallback_->onConnect_
        = std::bind(&AbstractDisplayController::OnAbstractScreenConnect, this, std::placeholders::_1);
    abstractScreenCallback_->onDisconnect_
        = std::bind(&AbstractDisplayController::OnAbstractScreenDisconnect, this, std::placeholders::_1);
    abstractScreenCallback_->onChange_
        = std::bind(&AbstractDisplayController::OnAbstractScreenChange, this, std::placeholders::_1,
        std::placeholders::_2);
    abstractScreenController->RegisterAbstractScreenCallback(abstractScreenCallback_);
}

void AbstractScreenController::RegisterAbstractScreenCallback(sptr<AbstractScreenCallback> cb)
{
    std::lock_guard<std::recursive_mutex> lock(mutex_);
    abstractScreenCallback_ = cb;
    for (auto& iter : dmsScreenMap_) {
        if (iter.second != nullptr && abstractScreenCallback_ != nullptr) {
            WLOGFI("dmsScreenId :%{public}" PRIu64"", iter.first);
            abstractScreenCallback_->onConnect_(iter.second);
        }
    }
}

第二种,当窗口子系统触发OnRsScreenConnectionChange回调时,会调用ProcessScreenConnected函数。如果abstractScreenCallback_回调函数注册则执行AbstractDisplayController::OnAbstractScreenConnect。

// foundation/window/window_manager/dmserver/src/abstract_screen_controller.cpp
void AbstractScreenController::OnRsScreenConnectionChange(ScreenId rsScreenId, ScreenEvent screenEvent)
{
    WLOGFI("rs screen event. id:%{public}, event:%{public}u", rsScreenId, static_cast<uint32_t>(screenEvent));
    if (screenEvent == ScreenEvent::CONNECTED) {
        auto task = [this, rsScreenId] {
            ProcessScreenConnected(rsScreenId);
        };
        controllerHandler_->PostTask(task, AppExecFwk::EventQueue::Priority::HIGH);
    }
    ···
}

void AbstractScreenController::ProcessScreenConnected(ScreenId rsScreenId)
{
    std::lock_guard<std::recursive_mutex> lock(mutex_);
    if (screenIdManager_.HasRsScreenId(rsScreenId)) {
        WLOGE("reconnect screen, screenId=%{public}" PRIu64"", rsScreenId);
        return;
    }
    WLOGFD("connect new screen");
    auto absScreen = InitAndGetScreen(rsScreenId);
    if (absScreen == nullptr) {
        return;
    }
    sptr<AbstractScreenGroup> screenGroup = AddToGroupLocked(absScreen);
    if (screenGroup == nullptr) {
        return;
    }
    ···
    if (abstractScreenCallback_ != nullptr) {
        abstractScreenCallback_->onConnect_(absScreen);
    }
    ···
}

如果OnAbstractScreenConnect在第二种情况执行,加载第一个屏幕时,则在group->GetChildCount() == 1时进入判断,执行BindAloneScreenLocked(absScreen);函数,系统正常运行。

如果OnAbstractScreenConnect在第一种情况执行,加载第一个屏幕时,会创建screenGroup。创建的group也会insert进dmsScreenMap_,此操作会导致异常Log中ScreenId为1的屏幕绑定异常。

// foundation/window/window_manager/dmserver/src/abstract_screen_controller.cpp
sptr<AbstractScreenGroup> AbstractScreenController::AddToGroupLocked(sptr<AbstractScreen> newScreen)
{
    sptr<AbstractScreenGroup> res;
    if (dmsScreenGroupMap_.empty()) {
        WLOGI("connect the first screen");
        // 第一块显示设备连接时进入
        res = AddAsFirstScreenLocked(newScreen); 
    } else {
        // 后续显示设备连接时进入
        res = AddAsSuccedentScreenLocked(newScreen);
    }
    return res;
}
sptr<AbstractScreenGroup> AbstractScreenController::AddAsFirstScreenLocked(sptr<AbstractScreen> newScreen)
{
    ScreenId dmsGroupScreenId = screenIdManager_.CreateAndGetNewScreenId(SCREEN_ID_INVALID);
    ···
    dmsScreenGroupMap_.insert(std::make_pair(dmsGroupScreenId, screenGroup));
    dmsScreenMap_.insert(std::make_pair(dmsGroupScreenId, screenGroup));
    ···
}

当第二块显示设备连接时,寻找第一块设备创建的group,并把自己添加进group中。

// foundation/window/window_manager/dmserver/src/abstract_screen_controller.cpp
sptr<AbstractScreenGroup> AbstractScreenController::AddAsSuccedentScreenLocked(sptr<AbstractScreen> newScreen)
{
    ScreenId defaultScreenId = GetDefaultAbstractScreenId();
    auto iter = dmsScreenMap_.find(defaultScreenId);
    if (iter == dmsScreenMap_.end()) {
        WLOGE("AddAsSuccedentScreenLocked. defaultScreenId:%{public}" PRIu64" is not in dmsScreenMap_.",
            defaultScreenId);
        return nullptr;
    }
    auto screen = iter->second;
    auto screenGroupIter = dmsScreenGroupMap_.find(screen->groupDmsId_);
    if (screenGroupIter == dmsScreenGroupMap_.end()) {
        WLOGE("AddAsSuccedentScreenLocked. groupDmsId:%{public}" PRIu64" is not in dmsScreenGroupMap_.",
            screen->groupDmsId_);
        return nullptr;
    }
    auto screenGroup = screenGroupIter->second;
    Point point;
    if (screenGroup->combination_ == ScreenCombination::SCREEN_EXPAND) {
        point = {screen->GetActiveScreenMode()->width_, 0};
    }
    screenGroup->AddChild(newScreen, point);
    return screenGroup;
}

当OnAbstractScreenConnect被执行时,获取到ScreenID为0的Group,此时Group内的屏幕数为2,所以无法进入BindAloneScreenLocked函数所在的判断,造成异常。

// foundation/window/window_manager/dmserver/src/abstract_display_controller.cpp
void AbstractDisplayController::OnAbstractScreenConnect(sptr<AbstractScreen> absScreen)
{
    if (absScreen == nullptr) {
        WLOGFE("absScreen is null");
        return;
    }
    WLOGI("connect new screen. id:%{public}" PRIu64"", absScreen->dmsId_);
    std::lock_guard<std::recursive_mutex> lock(mutex_);
    sptr<AbstractScreenGroup> group = absScreen->GetGroup();
    if (group == nullptr) {
        WLOGE("the group information of the screen is wrong");
        return;
    }
    if (group->combination_ == ScreenCombination::SCREEN_ALONE || group->GetChildCount() == 1) {
        BindAloneScreenLocked(absScreen);
    } else if (group->combination_ == ScreenCombination::SCREEN_MIRROR) {
        WLOGI("OnAbstractScreenConnect, ScreenCombination::SCREEN_MIRROR, AddScreenToMirrorLocked");
        AddScreenToMirrorLocked(absScreen);
    } else if (group->combination_ == ScreenCombination::SCREEN_EXPAND) {
        WLOGI("OnAbstractScreenConnect, ScreenCombination::SCREEN_EXPAND, AddScreenToExpandLocked");
        AddScreenToExpandLocked(absScreen);
    } else {
        WLOGE("support in future. combination:%{public}u", group->combination_);
    }
}

想了解更多关于开源的内容,请访问:

51CTO 开源基础软件社区

https://ost.51cto.com

责任编辑:jianghua 来源: 51CTO 开源基础软件社区
相关推荐

2023-07-10 16:06:50

鸿蒙检测锁屏应用

2023-07-18 14:05:30

鸿蒙

2010-12-21 17:36:12

2011-11-18 14:59:47

Aruba

2009-12-31 15:43:32

宽带接入网

2009-10-27 15:35:08

2009-10-29 13:46:30

ADSL接入解决方案

2009-07-29 09:43:15

银行网络无线接入

2009-12-30 15:08:17

无线接入技术

2009-10-30 16:42:27

无线接入解决方案

2009-11-02 18:10:39

综合接入解决方案

2009-10-29 13:54:33

远程接入解决方案

2011-05-18 15:30:13

2009-10-28 17:39:58

光接入网解决方案

2009-10-29 10:32:17

接入网解决方案

2009-10-27 15:49:43

2011-02-17 15:26:27

IADVoIPPPPoE

2009-02-19 10:13:00

2012-12-20 15:07:15

锐捷无线网络金融网络

2009-10-27 17:07:34

无线宽带接入解决方案
点赞
收藏

51CTO技术栈公众号