• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "soft_vsync.h"
17 
18 #include <thread>
19 #include <unistd.h>
20 
21 #include <hilog/log.h>
22 #include <vsync_module_c.h>
23 
24 namespace {
Log(const char * log)25 void Log(const char* log)
26 {
27     OHOS::HiviewDFX::HiLog::Info({LOG_CORE, 0, "WAYLAND_SERVER"}, "%{public}s", log);
28 }
29 } // namespace
30 
GetInstance()31 SoftVsync &SoftVsync::GetInstance()
32 {
33     static SoftVsync instance;
34     return instance;
35 }
36 
SoftVsyncThreadMain()37 void SoftVsync::SoftVsyncThreadMain()
38 {
39 
40     int32_t ret;
41     do {
42         ret = VsyncModuleStart();
43     } while (ret != 0);
44 
45     while (vsyncThreadRunning == true) {
46         VsyncModuleTrigger();
47         usleep(1e6 / 60);
48     }
49 }
50 
SoftVsyncStart()51 void SoftVsync::SoftVsyncStart()
52 {
53     if (vsyncThreadRunning == false) {
54         Log("start soft vsync thread");
55         vsyncThreadRunning = true;
56         auto threadMain = std::bind(&SoftVsync::SoftVsyncThreadMain, this);
57         softVsyncThread = std::make_unique<std::thread>(threadMain);
58     }
59 }
60 
SoftVsyncStop()61 void SoftVsync::SoftVsyncStop()
62 {
63     if (vsyncThreadRunning != true) {
64         return;
65     } else {
66         Log("stop soft vsync thread");
67         vsyncThreadRunning = false;
68         softVsyncThread->join();
69     }
70 }
71 
StartSoftVsyncThread()72 void StartSoftVsyncThread()
73 {
74     SoftVsync::GetInstance().SoftVsyncStart();
75 }
76 
StopSoftVsyncThread()77 void StopSoftVsyncThread()
78 {
79     SoftVsync::GetInstance().SoftVsyncStop();
80 }
81