123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "base/log/frame_report.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include <dlfcn.h>
1923b3eb3cSopenharmony_ci#include <frame_collector.h>
2023b3eb3cSopenharmony_ci
2123b3eb3cSopenharmony_ci#include "base/utils/utils.h"
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_cinamespace OHOS::Ace {
2423b3eb3cSopenharmony_cinamespace {
2523b3eb3cSopenharmony_ciconst std::string FRAME_AWARE_SO_PATH = "libframe_ui_intf.z.so";
2623b3eb3cSopenharmony_ci} // namespace
2723b3eb3cSopenharmony_ciFrameReport& FrameReport::GetInstance()
2823b3eb3cSopenharmony_ci{
2923b3eb3cSopenharmony_ci    static FrameReport instance;
3023b3eb3cSopenharmony_ci    return instance;
3123b3eb3cSopenharmony_ci}
3223b3eb3cSopenharmony_ci
3323b3eb3cSopenharmony_ciFrameReport::FrameReport() {}
3423b3eb3cSopenharmony_ci
3523b3eb3cSopenharmony_ciFrameReport::~FrameReport()
3623b3eb3cSopenharmony_ci{
3723b3eb3cSopenharmony_ci    CloseLibrary();
3823b3eb3cSopenharmony_ci}
3923b3eb3cSopenharmony_ci
4023b3eb3cSopenharmony_cibool FrameReport::LoadLibrary()
4123b3eb3cSopenharmony_ci{
4223b3eb3cSopenharmony_ci    if (!frameSchedSoLoaded_) {
4323b3eb3cSopenharmony_ci        frameSchedHandle_ = dlopen(FRAME_AWARE_SO_PATH.c_str(), RTLD_LAZY);
4423b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(frameSchedHandle_, false);
4523b3eb3cSopenharmony_ci        frameInitFunc_ = (FrameInitFunc)LoadSymbol("Init");
4623b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(frameInitFunc_, false);
4723b3eb3cSopenharmony_ci        frameGetEnableFunc_ = (FrameGetEnableFunc)LoadSymbol("GetSenseSchedEnable");
4823b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(frameGetEnableFunc_, false);
4923b3eb3cSopenharmony_ci        beginFlushAnimationFunc_ = (BeginFlushAnimationFunc)LoadSymbol("BeginFlushAnimation");
5023b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(beginFlushAnimationFunc_, false);
5123b3eb3cSopenharmony_ci        endFlushAnimationFunc_ = (EndFlushAnimationFunc)LoadSymbol("EndFlushAnimation");
5223b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(endFlushAnimationFunc_, false);
5323b3eb3cSopenharmony_ci        beginFlushBuildFunc_ = (BeginFlushBuildFunc)LoadSymbol("BeginFlushBuild");
5423b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(beginFlushBuildFunc_, false);
5523b3eb3cSopenharmony_ci        endFlushBuildFunc_ = (EndFlushBuildFunc)LoadSymbol("EndFlushBuild");
5623b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(endFlushBuildFunc_, false);
5723b3eb3cSopenharmony_ci        beginFlushLayoutFunc_ = (BeginFlushLayoutFunc)LoadSymbol("BeginFlushLayout");
5823b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(beginFlushLayoutFunc_, false);
5923b3eb3cSopenharmony_ci        endFlushLayoutFunc_ = (EndFlushLayoutFunc)LoadSymbol("EndFlushLayout");
6023b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(endFlushLayoutFunc_, false);
6123b3eb3cSopenharmony_ci        beginFlushRenderFunc_ = (BeginFlushRenderFunc)LoadSymbol("BeginFlushRender");
6223b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(beginFlushRenderFunc_, false);
6323b3eb3cSopenharmony_ci        endFlushRenderFunc_ = (EndFlushRenderFunc)LoadSymbol("EndFlushRender");
6423b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(endFlushRenderFunc_, false);
6523b3eb3cSopenharmony_ci        beginFlushRenderFinishFunc_ = (BeginFlushRenderFinishFunc)LoadSymbol("BeginFlushRenderFinish");
6623b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(beginFlushRenderFinishFunc_, false);
6723b3eb3cSopenharmony_ci        endFlushRenderFinishFunc_ = (EndFlushRenderFinishFunc)LoadSymbol("EndFlushRenderFinish");
6823b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(endFlushRenderFinishFunc_, false);
6923b3eb3cSopenharmony_ci        beginProcessPostFunc_ = (BeginProcessPostFlushFunc)LoadSymbol("BeginProcessPostFlush");
7023b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(beginProcessPostFunc_, false);
7123b3eb3cSopenharmony_ci        beginListFlingFunc_ = (BeginListFlingFunc)LoadSymbol("BeginListFling");
7223b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(beginListFlingFunc_, false);
7323b3eb3cSopenharmony_ci        endListFlingFunc_ = (EndListFlingFunc)LoadSymbol("EndListFling");
7423b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(endListFlingFunc_, false);
7523b3eb3cSopenharmony_ci        flushBeginFunc_ = (FlushBeginFunc)LoadSymbol("FlushBegin");
7623b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(flushBeginFunc_, false);
7723b3eb3cSopenharmony_ci        flushEndFunc_ = (FlushEndFunc)LoadSymbol("FlushEnd");
7823b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(flushEndFunc_, false);
7923b3eb3cSopenharmony_ci        setFrameParamFunc_ = (SetFrameParamFunc)LoadSymbol("SetFrameParam");
8023b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(setFrameParamFunc_, false);
8123b3eb3cSopenharmony_ci        enableSelfRenderFunc_ = (EnableSelfRenderFunc)LoadSymbol("EnableSelfRender");
8223b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(enableSelfRenderFunc_, false);
8323b3eb3cSopenharmony_ci        disableSelfRenderFunc_ = (DisableSelfRenderFunc)LoadSymbol("DisableSelfRender");
8423b3eb3cSopenharmony_ci        CHECK_NULL_RETURN(disableSelfRenderFunc_, false);
8523b3eb3cSopenharmony_ci        frameSchedSoLoaded_ = true;
8623b3eb3cSopenharmony_ci    }
8723b3eb3cSopenharmony_ci    return true;
8823b3eb3cSopenharmony_ci}
8923b3eb3cSopenharmony_ci
9023b3eb3cSopenharmony_civoid FrameReport::CloseLibrary()
9123b3eb3cSopenharmony_ci{
9223b3eb3cSopenharmony_ci    if (dlclose(frameSchedHandle_) != 0) {
9323b3eb3cSopenharmony_ci        LOGE("frame-ace:[CloseLibrary]libframe_ui_intf.so failed!\n");
9423b3eb3cSopenharmony_ci        return;
9523b3eb3cSopenharmony_ci    }
9623b3eb3cSopenharmony_ci    frameSchedHandle_ = nullptr;
9723b3eb3cSopenharmony_ci    frameSchedSoLoaded_ = false;
9823b3eb3cSopenharmony_ci}
9923b3eb3cSopenharmony_ci
10023b3eb3cSopenharmony_civoid* FrameReport::LoadSymbol(const char* symName)
10123b3eb3cSopenharmony_ci{
10223b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(frameSchedHandle_, nullptr);
10323b3eb3cSopenharmony_ci    return dlsym(frameSchedHandle_, symName);
10423b3eb3cSopenharmony_ci}
10523b3eb3cSopenharmony_ci
10623b3eb3cSopenharmony_civoid FrameReport::Init()
10723b3eb3cSopenharmony_ci{
10823b3eb3cSopenharmony_ci    if (LoadLibrary()) {
10923b3eb3cSopenharmony_ci        frameInitFunc_();
11023b3eb3cSopenharmony_ci        enable_ = frameGetEnableFunc_() != 0;
11123b3eb3cSopenharmony_ci    }
11223b3eb3cSopenharmony_ci}
11323b3eb3cSopenharmony_ci
11423b3eb3cSopenharmony_ciint FrameReport::GetEnable()
11523b3eb3cSopenharmony_ci{
11623b3eb3cSopenharmony_ci    return true;
11723b3eb3cSopenharmony_ci}
11823b3eb3cSopenharmony_ci
11923b3eb3cSopenharmony_ciint FrameReport::GetFrameReportEnable()
12023b3eb3cSopenharmony_ci{
12123b3eb3cSopenharmony_ci    if (!frameSchedSoLoaded_) {
12223b3eb3cSopenharmony_ci        return 0;
12323b3eb3cSopenharmony_ci    }
12423b3eb3cSopenharmony_ci    return frameGetEnableFunc_();
12523b3eb3cSopenharmony_ci}
12623b3eb3cSopenharmony_ci
12723b3eb3cSopenharmony_civoid FrameReport::BeginFlushAnimation()
12823b3eb3cSopenharmony_ci{
12923b3eb3cSopenharmony_ci    Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::AnimateStart);
13023b3eb3cSopenharmony_ci    if (!enable_) {
13123b3eb3cSopenharmony_ci        return;
13223b3eb3cSopenharmony_ci    }
13323b3eb3cSopenharmony_ci    beginFlushAnimationFunc_();
13423b3eb3cSopenharmony_ci}
13523b3eb3cSopenharmony_ci
13623b3eb3cSopenharmony_civoid FrameReport::EndFlushAnimation()
13723b3eb3cSopenharmony_ci{
13823b3eb3cSopenharmony_ci    Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::AnimateEnd);
13923b3eb3cSopenharmony_ci    if (!enable_) {
14023b3eb3cSopenharmony_ci        return;
14123b3eb3cSopenharmony_ci    }
14223b3eb3cSopenharmony_ci    endFlushAnimationFunc_();
14323b3eb3cSopenharmony_ci}
14423b3eb3cSopenharmony_ci
14523b3eb3cSopenharmony_civoid FrameReport::BeginFlushBuild()
14623b3eb3cSopenharmony_ci{
14723b3eb3cSopenharmony_ci    Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::BuildStart);
14823b3eb3cSopenharmony_ci    if (!enable_) {
14923b3eb3cSopenharmony_ci        return;
15023b3eb3cSopenharmony_ci    }
15123b3eb3cSopenharmony_ci    beginFlushBuildFunc_();
15223b3eb3cSopenharmony_ci}
15323b3eb3cSopenharmony_ci
15423b3eb3cSopenharmony_civoid FrameReport::EndFlushBuild()
15523b3eb3cSopenharmony_ci{
15623b3eb3cSopenharmony_ci    Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::BuildEnd);
15723b3eb3cSopenharmony_ci    if (!enable_) {
15823b3eb3cSopenharmony_ci        return;
15923b3eb3cSopenharmony_ci    }
16023b3eb3cSopenharmony_ci    endFlushBuildFunc_();
16123b3eb3cSopenharmony_ci}
16223b3eb3cSopenharmony_ci
16323b3eb3cSopenharmony_civoid FrameReport::BeginFlushLayout()
16423b3eb3cSopenharmony_ci{
16523b3eb3cSopenharmony_ci    Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::LayoutStart);
16623b3eb3cSopenharmony_ci    if (!enable_) {
16723b3eb3cSopenharmony_ci        return;
16823b3eb3cSopenharmony_ci    }
16923b3eb3cSopenharmony_ci    beginFlushLayoutFunc_();
17023b3eb3cSopenharmony_ci}
17123b3eb3cSopenharmony_ci
17223b3eb3cSopenharmony_civoid FrameReport::EndFlushLayout()
17323b3eb3cSopenharmony_ci{
17423b3eb3cSopenharmony_ci    Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::LayoutEnd);
17523b3eb3cSopenharmony_ci    if (!enable_) {
17623b3eb3cSopenharmony_ci        return;
17723b3eb3cSopenharmony_ci    }
17823b3eb3cSopenharmony_ci    endFlushLayoutFunc_();
17923b3eb3cSopenharmony_ci}
18023b3eb3cSopenharmony_ci
18123b3eb3cSopenharmony_civoid FrameReport::BeginFlushRender()
18223b3eb3cSopenharmony_ci{
18323b3eb3cSopenharmony_ci    Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::DrawStart);
18423b3eb3cSopenharmony_ci    if (!enable_) {
18523b3eb3cSopenharmony_ci        return;
18623b3eb3cSopenharmony_ci    }
18723b3eb3cSopenharmony_ci    beginFlushRenderFunc_();
18823b3eb3cSopenharmony_ci}
18923b3eb3cSopenharmony_ci
19023b3eb3cSopenharmony_civoid FrameReport::EndFlushRender()
19123b3eb3cSopenharmony_ci{
19223b3eb3cSopenharmony_ci    if (!enable_) {
19323b3eb3cSopenharmony_ci        return;
19423b3eb3cSopenharmony_ci    }
19523b3eb3cSopenharmony_ci    endFlushRenderFunc_();
19623b3eb3cSopenharmony_ci}
19723b3eb3cSopenharmony_ci
19823b3eb3cSopenharmony_civoid FrameReport::BeginFlushRenderFinish()
19923b3eb3cSopenharmony_ci{
20023b3eb3cSopenharmony_ci    if (!enable_) {
20123b3eb3cSopenharmony_ci        return;
20223b3eb3cSopenharmony_ci    }
20323b3eb3cSopenharmony_ci    beginFlushRenderFinishFunc_();
20423b3eb3cSopenharmony_ci}
20523b3eb3cSopenharmony_ci
20623b3eb3cSopenharmony_civoid FrameReport::EndFlushRenderFinish()
20723b3eb3cSopenharmony_ci{
20823b3eb3cSopenharmony_ci    Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::DrawEnd);
20923b3eb3cSopenharmony_ci    if (!enable_) {
21023b3eb3cSopenharmony_ci        return;
21123b3eb3cSopenharmony_ci    }
21223b3eb3cSopenharmony_ci    endFlushRenderFinishFunc_();
21323b3eb3cSopenharmony_ci}
21423b3eb3cSopenharmony_ci
21523b3eb3cSopenharmony_civoid FrameReport::BeginProcessPostFlush()
21623b3eb3cSopenharmony_ci{
21723b3eb3cSopenharmony_ci    if (!enable_) {
21823b3eb3cSopenharmony_ci        return;
21923b3eb3cSopenharmony_ci    }
22023b3eb3cSopenharmony_ci    beginProcessPostFunc_();
22123b3eb3cSopenharmony_ci}
22223b3eb3cSopenharmony_ci
22323b3eb3cSopenharmony_civoid FrameReport::BeginListFling()
22423b3eb3cSopenharmony_ci{
22523b3eb3cSopenharmony_ci    if (!enable_) {
22623b3eb3cSopenharmony_ci        return;
22723b3eb3cSopenharmony_ci    }
22823b3eb3cSopenharmony_ci    beginListFlingFunc_();
22923b3eb3cSopenharmony_ci}
23023b3eb3cSopenharmony_ci
23123b3eb3cSopenharmony_civoid FrameReport::EndListFling()
23223b3eb3cSopenharmony_ci{
23323b3eb3cSopenharmony_ci    if (!enable_) {
23423b3eb3cSopenharmony_ci        return;
23523b3eb3cSopenharmony_ci    }
23623b3eb3cSopenharmony_ci    endListFlingFunc_();
23723b3eb3cSopenharmony_ci}
23823b3eb3cSopenharmony_ci
23923b3eb3cSopenharmony_civoid FrameReport::FlushBegin()
24023b3eb3cSopenharmony_ci{
24123b3eb3cSopenharmony_ci    if (!enable_) {
24223b3eb3cSopenharmony_ci        return;
24323b3eb3cSopenharmony_ci    }
24423b3eb3cSopenharmony_ci    flushBeginFunc_();
24523b3eb3cSopenharmony_ci}
24623b3eb3cSopenharmony_ci
24723b3eb3cSopenharmony_civoid FrameReport::FlushEnd()
24823b3eb3cSopenharmony_ci{
24923b3eb3cSopenharmony_ci    if (!enable_) {
25023b3eb3cSopenharmony_ci        return;
25123b3eb3cSopenharmony_ci    }
25223b3eb3cSopenharmony_ci    flushEndFunc_();
25323b3eb3cSopenharmony_ci}
25423b3eb3cSopenharmony_ci
25523b3eb3cSopenharmony_civoid FrameReport::SetFrameParam(int requestId, int load, int schedFrameNum, int value)
25623b3eb3cSopenharmony_ci{
25723b3eb3cSopenharmony_ci    if (!enable_) {
25823b3eb3cSopenharmony_ci        return;
25923b3eb3cSopenharmony_ci    }
26023b3eb3cSopenharmony_ci    setFrameParamFunc_(requestId, load, schedFrameNum, value);
26123b3eb3cSopenharmony_ci}
26223b3eb3cSopenharmony_ci
26323b3eb3cSopenharmony_civoid FrameReport::EnableSelfRender()
26423b3eb3cSopenharmony_ci{
26523b3eb3cSopenharmony_ci    if (!enable_) {
26623b3eb3cSopenharmony_ci        return;
26723b3eb3cSopenharmony_ci    }
26823b3eb3cSopenharmony_ci    enableSelfRenderFunc_();
26923b3eb3cSopenharmony_ci}
27023b3eb3cSopenharmony_ci
27123b3eb3cSopenharmony_civoid FrameReport::DisableSelfRender()
27223b3eb3cSopenharmony_ci{
27323b3eb3cSopenharmony_ci    if (!enable_) {
27423b3eb3cSopenharmony_ci        return;
27523b3eb3cSopenharmony_ci    }
27623b3eb3cSopenharmony_ci    disableSelfRenderFunc_();
27723b3eb3cSopenharmony_ci}
27823b3eb3cSopenharmony_ci} // namespace OHOS::Ace
279