• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd
3  * This file contains confidential and proprietary information of
4  * OSWare Technology Co., Ltd
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "sorft_vsync.h"
20 #include <chrono>
21 #include <sys/time.h>
22 #include "display_common.h"
23 
24 namespace OHOS {
25 namespace HDI {
26 namespace DISPLAY {
SorftVsync()27 SorftVsync::SorftVsync() {}
28 
Init()29 int32_t SorftVsync::Init()
30 {
31     thread_ = std::make_unique<std::thread>([this]() { WorkThread(); });
32     DISPLAY_CHK_RETURN((thread_ == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("can not create thread"));
33     running_ = true;
34     return DISPLAY_SUCCESS;
35 }
36 
GetInstance()37 SorftVsync &SorftVsync::GetInstance()
38 {
39     static SorftVsync instance;
40     static std::once_flag once;
41     std::call_once(once, [&]() {
42         int ret = instance.Init();
43         if (ret != DISPLAY_SUCCESS) {
44             DISPLAY_LOGE("Vsync Worker Init failed");
45         }
46     });
47     return instance;
48 }
49 
~SorftVsync()50 SorftVsync::~SorftVsync()
51 {
52     DISPLAY_LOGD();
53     {
54         std::lock_guard<std::mutex> lg(mutext_);
55         running_ = false;
56     }
57     condition_.notify_one();
58     if (thread_ != nullptr) {
59         thread_->join();
60     }
61 }
62 
CheckRuning()63 bool SorftVsync::CheckRuning()
64 {
65     std::unique_lock<std::mutex> ul(mutext_);
66     condition_.wait(ul, [this]() { return (enable_ || !running_); });
67     return running_;
68 }
69 
EnableVsync(bool enable)70 void SorftVsync::EnableVsync(bool enable)
71 {
72     DISPLAY_LOGD();
73     {
74         std::lock_guard<std::mutex> lg(mutext_);
75         enable_ = enable;
76     }
77     condition_.notify_one();
78 }
79 
WorkThread()80 void SorftVsync::WorkThread()
81 {
82     DISPLAY_LOGD();
83     unsigned int seq = 0;
84     uint64_t time = 0;
85     const int SIXTY_HZ_OF_TIME = 16666;
86     struct timespec ts;
87     while (CheckRuning()) {
88         // wait the vblank
89         usleep(SIXTY_HZ_OF_TIME);
90         seq++;
91         if (callback_ != nullptr) {
92             clock_gettime(CLOCK_MONOTONIC, &ts);
93             time = ts.tv_nsec;
94             callback_->Vsync(seq, time);
95         } else {
96             DISPLAY_LOGE("the callbac is nullptr");
97         }
98     }
99 }
100 
ReqesterVBlankCb(std::shared_ptr<VsyncCallBack> & cb)101 void SorftVsync::ReqesterVBlankCb(std::shared_ptr<VsyncCallBack> &cb)
102 {
103     DISPLAY_LOGD();
104     DISPLAY_CHK_RETURN_NOT_VALUE((cb == nullptr), DISPLAY_LOGE("the VBlankCallback is nullptr "));
105     callback_ = cb;
106 }
107 } // namespace OHOS
108 } // namespace HDI
109 } // namespace DISPLAY
110