1 
2 /*
3  * Copyright (c) 2024 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "ressched_event_listener.h"
17 
18 #include "res_sched_client.h"
19 #include "res_type.h"
20 #include "rs_trace.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 std::once_flag ResschedEventListener::createFlag_;
25 sptr<ResschedEventListener> ResschedEventListener::instance_ = nullptr;
26 constexpr uint64_t SAMPLE_TIME = 100000000;
GetInstance()27 sptr<ResschedEventListener> ResschedEventListener::GetInstance() noexcept
28 {
29     std::call_once(createFlag_, []() {
30         instance_ = new ResschedEventListener();
31     });
32     return instance_;
33 }
34 
OnReceiveEvent(uint32_t eventType,uint32_t eventValue,std::unordered_map<std::string,std::string> extInfo)35 void ResschedEventListener::OnReceiveEvent(uint32_t eventType, uint32_t eventValue,
36     std::unordered_map<std::string, std::string> extInfo)
37 {
38     if (eventType == ResourceSchedule::ResType::EventType::EVENT_DRAW_FRAME_REPORT &&
39         eventValue == ResourceSchedule::ResType::EventValue::EVENT_VALUE_DRAW_FRAME_REPORT_START) {
40         isNeedReport_ = true;
41         isFirstReport_ = true;
42     } else if (eventType == ResourceSchedule::ResType::EventType::EVENT_DRAW_FRAME_REPORT &&
43         eventValue == ResourceSchedule::ResType::EventValue::EVENT_VALUE_DRAW_FRAME_REPORT_STOP) {
44         isNeedReport_ = false;
45         isFirstReport_ = false;
46     }
47 }
48 
ReportFrameToRSS()49 void ResschedEventListener::ReportFrameToRSS()
50 {
51     if (GetIsNeedReport()) {
52         uint64_t currTime = static_cast<uint64_t>(
53             std::chrono::duration_cast<std::chrono::nanoseconds>(
54                 std::chrono::steady_clock::now().time_since_epoch()).count());
55         if (GetIsFirstReport() ||
56             lastReportTime_ == 0 || currTime - lastReportTime_ >= SAMPLE_TIME) {
57             RS_TRACE_BEGIN("ReportFrameToRSS");
58             uint32_t type = OHOS::ResourceSchedule::ResType::RES_TYPE_SEND_FRAME_EVENT;
59             int64_t value = 0;
60             std::unordered_map<std::string, std::string> mapPayload;
61             OHOS::ResourceSchedule::ResSchedClient::GetInstance().ReportData(type, value, mapPayload);
62             SetIsFirstReport(false);
63             lastReportTime_ = static_cast<uint64_t>(
64                 std::chrono::duration_cast<std::chrono::nanoseconds>(
65                     std::chrono::steady_clock::now().time_since_epoch()).count());
66             RS_TRACE_END();
67         }
68     }
69 }
70 
GetIsNeedReport() const71 bool ResschedEventListener::GetIsNeedReport() const
72 {
73     return isNeedReport_.load();
74 }
75 
GetIsFirstReport() const76 bool ResschedEventListener::GetIsFirstReport() const
77 {
78     return isFirstReport_.load();
79 }
80 
SetIsFirstReport(bool value)81 void ResschedEventListener::SetIsFirstReport(bool value)
82 {
83     isFirstReport_ = value;
84 }
85 } // namespace Rosen
86 } // namespace OHOS
87