• 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 "vsync_controller.h"
17 #include <scoped_bytrace.h>
18 #include "vsync_log.h"
19 
20 namespace OHOS {
21 namespace Rosen {
VSyncController(const sptr<VSyncGenerator> & geng,int64_t offset)22 VSyncController::VSyncController(const sptr<VSyncGenerator> &geng, int64_t offset)
23     : generator_(geng), callbackMutex_(), callback_(nullptr),
24     offsetMutex_(), phaseOffset_(offset), normalPhaseOffset_(offset), enabled_(false), isDirectly_(false)
25 {
26 }
27 
~VSyncController()28 VSyncController::~VSyncController()
29 {
30 }
31 
ChangeAdaptiveStatus(bool isAdaptive)32 void VSyncController::ChangeAdaptiveStatus(bool isAdaptive)
33 {
34     if (!isAdaptive) {
35         ResetOffset();
36     }
37 }
38 
AdjustAdaptiveOffset(int64_t period,int64_t offset)39 void VSyncController::AdjustAdaptiveOffset(int64_t period, int64_t offset)
40 {
41     if (period <= 0) {
42         VLOGE("VSyncController::AdjustAdaptiveOffset: period is illegal.");
43         return;
44     }
45     std::lock_guard<std::mutex> locker(offsetMutex_);
46     auto remainder = (phaseOffset_ + offset) % period;
47 
48     phaseOffset_ = remainder;
49 }
50 
ResetOffset()51 void VSyncController::ResetOffset()
52 {
53     int64_t offset;
54     {
55         std::lock_guard<std::mutex> locker(offsetMutex_);
56         phaseOffset_ = normalPhaseOffset_;
57         offset = normalPhaseOffset_;
58     }
59     if (generator_ == nullptr) {
60         return;
61     }
62     const sptr<VSyncGenerator> generator = generator_.promote();
63     if (generator == nullptr) {
64         return;
65     }
66     generator->ChangePhaseOffset(this, offset);
67 }
68 
NeedPreexecuteAndUpdateTs(int64_t & timestamp,int64_t & period)69 bool VSyncController::NeedPreexecuteAndUpdateTs(int64_t& timestamp, int64_t& period)
70 {
71     if (generator_ == nullptr) {
72         return false;
73     }
74     const sptr<VSyncGenerator> generator = generator_.promote();
75     if (generator == nullptr) {
76         return false;
77     }
78     int64_t offset = 0;
79     bool flag = generator->NeedPreexecuteAndUpdateTs(timestamp, period, offset, lastVsyncTime_.load());
80     if (flag && offset > 0) {
81         AdjustAdaptiveOffset(period, offset);
82     }
83 
84     return flag;
85 }
86 
SetEnable(bool enable,bool & isGeneratorEnable)87 VsyncError VSyncController::SetEnable(bool enable, bool& isGeneratorEnable)
88 {
89     if (generator_ == nullptr) {
90         return VSYNC_ERROR_NULLPTR;
91     }
92     const sptr<VSyncGenerator> generator = generator_.promote();
93     if (generator == nullptr) {
94         return VSYNC_ERROR_NULLPTR;
95     }
96     VsyncError ret = VSYNC_ERROR_OK;
97     if (enable) {
98         // If the sampler does not complete the sampling work, the generator does not work
99         // We need to tell the distributor to use the software vsync
100         isGeneratorEnable = generator->IsEnable();
101         if (isGeneratorEnable) {
102             ret = generator->AddListener(this, isRS_, isDirectly_);
103             if (ret != VSYNC_ERROR_OK) {
104                 isGeneratorEnable = false;
105             }
106         } else {
107             ret = VSYNC_ERROR_API_FAILED;
108         }
109     } else {
110         ret = generator->RemoveListener(this);
111         if (ret == VSYNC_ERROR_OK) {
112             isGeneratorEnable = false;
113         }
114     }
115 
116     isRS_ = false;
117     isDirectly_ = false;
118     enabled_ = isGeneratorEnable;
119     return ret;
120 }
121 
SetCallback(Callback * cb)122 VsyncError VSyncController::SetCallback(Callback *cb)
123 {
124     if (cb == nullptr) {
125         return VSYNC_ERROR_NULLPTR;
126     }
127     std::lock_guard<std::mutex> locker(callbackMutex_);
128     callback_ = cb;
129     return VSYNC_ERROR_OK;
130 }
131 
SetPhaseOffset(int64_t offset)132 VsyncError VSyncController::SetPhaseOffset(int64_t offset)
133 {
134     if (generator_ == nullptr) {
135         return VSYNC_ERROR_NULLPTR;
136     }
137     const sptr<VSyncGenerator> generator = generator_.promote();
138     if (generator == nullptr) {
139         return VSYNC_ERROR_NULLPTR;
140     }
141     {
142         std::lock_guard<std::mutex> locker(offsetMutex_);
143         phaseOffset_ = offset;
144         normalPhaseOffset_ = offset;
145     }
146     return generator->ChangePhaseOffset(this, offset);
147 }
148 
OnVSyncEvent(int64_t now,int64_t period,uint32_t refreshRate,VSyncMode vsyncMode,uint32_t vsyncMaxRefreshRate)149 void VSyncController::OnVSyncEvent(int64_t now, int64_t period,
150     uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate)
151 {
152     Callback *cb = nullptr;
153     {
154         std::lock_guard<std::mutex> locker(callbackMutex_);
155         cb = callback_;
156     }
157     if (cb != nullptr) {
158         lastVsyncTime_ = now;
159         cb->OnVSyncEvent(now, period, refreshRate, vsyncMode, vsyncMaxRefreshRate);
160     }
161 }
162 
OnPhaseOffsetChanged(int64_t phaseOffset)163 void VSyncController::OnPhaseOffsetChanged(int64_t phaseOffset)
164 {
165     std::lock_guard<std::mutex> locker(offsetMutex_);
166     phaseOffset_ = phaseOffset;
167     normalPhaseOffset_ = phaseOffset;
168 }
169 
GetPhaseOffset()170 int64_t VSyncController::GetPhaseOffset()
171 {
172     std::lock_guard<std::mutex> locker(offsetMutex_);
173     return phaseOffset_;
174 }
175 
176 /* std::pair<id, refresh rate> */
OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t,uint32_t>> & refreshRates)177 void VSyncController::OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates)
178 {
179     Callback *cb = nullptr;
180     {
181         std::lock_guard<std::mutex> locker(callbackMutex_);
182         cb = callback_;
183     }
184     if (cb != nullptr) {
185         cb->OnConnsRefreshRateChanged(refreshRates);
186     }
187 }
188 }
189 }
190