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), enabled_(false)
25 {
26 }
27
~VSyncController()28 VSyncController::~VSyncController()
29 {
30 }
31
SetEnable(bool enable,bool & isGeneratorEnable)32 VsyncError VSyncController::SetEnable(bool enable, bool& isGeneratorEnable)
33 {
34 if (generator_ == nullptr) {
35 return VSYNC_ERROR_NULLPTR;
36 }
37 const sptr<VSyncGenerator> generator = generator_.promote();
38 if (generator == nullptr) {
39 return VSYNC_ERROR_NULLPTR;
40 }
41 int64_t phaseOffset;
42 {
43 std::lock_guard<std::mutex> locker(offsetMutex_);
44 phaseOffset = phaseOffset_;
45 }
46 VsyncError ret = VSYNC_ERROR_OK;
47 if (enable) {
48 // If the sampler does not complete the sampling work, the generator does not work
49 // We need to tell the distributor to use the software vsync
50 isGeneratorEnable = generator->IsEnable();
51 if (isGeneratorEnable) {
52 ret = generator->AddListener(phaseOffset, this);
53 } else {
54 ret = VSYNC_ERROR_API_FAILED;
55 }
56 } else {
57 ret = generator->RemoveListener(this);
58 isGeneratorEnable = enable;
59 }
60
61 enabled_ = isGeneratorEnable;
62 return ret;
63 }
64
SetCallback(Callback * cb)65 VsyncError VSyncController::SetCallback(Callback *cb)
66 {
67 if (cb == nullptr) {
68 return VSYNC_ERROR_NULLPTR;
69 }
70 std::lock_guard<std::mutex> locker(callbackMutex_);
71 callback_ = cb;
72 return VSYNC_ERROR_OK;
73 }
74
SetPhaseOffset(int64_t offset)75 VsyncError VSyncController::SetPhaseOffset(int64_t offset)
76 {
77 if (generator_ == nullptr) {
78 return VSYNC_ERROR_NULLPTR;
79 }
80 const sptr<VSyncGenerator> generator = generator_.promote();
81 if (generator == nullptr) {
82 return VSYNC_ERROR_NULLPTR;
83 }
84 {
85 std::lock_guard<std::mutex> locker(offsetMutex_);
86 phaseOffset_ = offset;
87 }
88 return generator->ChangePhaseOffset(this, offset);
89 }
90
OnVSyncEvent(int64_t now,int64_t period,uint32_t refreshRate,VSyncMode vsyncMode)91 void VSyncController::OnVSyncEvent(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode)
92 {
93 Callback *cb = nullptr;
94 {
95 std::lock_guard<std::mutex> locker(callbackMutex_);
96 cb = callback_;
97 }
98 if (cb != nullptr) {
99 cb->OnVSyncEvent(now, period, refreshRate, vsyncMode);
100 }
101 }
102
OnPhaseOffsetChanged(int64_t phaseOffset)103 void VSyncController::OnPhaseOffsetChanged(int64_t phaseOffset)
104 {
105 std::lock_guard<std::mutex> locker(offsetMutex_);
106 phaseOffset_ = phaseOffset;
107 }
108
109 /* std::pair<id, refresh rate> */
OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t,uint32_t>> & refreshRates)110 void VSyncController::OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates)
111 {
112 Callback *cb = nullptr;
113 {
114 std::lock_guard<std::mutex> locker(callbackMutex_);
115 cb = callback_;
116 }
117 if (cb != nullptr) {
118 cb->OnConnsRefreshRateChanged(refreshRates);
119 }
120 }
121 }
122 }
123