• 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), 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             if (ret != VSYNC_ERROR_OK) {
54                 isGeneratorEnable = false;
55             }
56         } else {
57             ret = VSYNC_ERROR_API_FAILED;
58         }
59     } else {
60         ret = generator->RemoveListener(this);
61         if (ret == VSYNC_ERROR_OK) {
62             isGeneratorEnable = false;
63         }
64     }
65 
66     enabled_ = isGeneratorEnable;
67     return ret;
68 }
69 
SetCallback(Callback * cb)70 VsyncError VSyncController::SetCallback(Callback *cb)
71 {
72     if (cb == nullptr) {
73         return VSYNC_ERROR_NULLPTR;
74     }
75     std::lock_guard<std::mutex> locker(callbackMutex_);
76     callback_ = cb;
77     return VSYNC_ERROR_OK;
78 }
79 
SetPhaseOffset(int64_t offset)80 VsyncError VSyncController::SetPhaseOffset(int64_t offset)
81 {
82     if (generator_ == nullptr) {
83         return VSYNC_ERROR_NULLPTR;
84     }
85     const sptr<VSyncGenerator> generator = generator_.promote();
86     if (generator == nullptr) {
87         return VSYNC_ERROR_NULLPTR;
88     }
89     {
90         std::lock_guard<std::mutex> locker(offsetMutex_);
91         phaseOffset_ = offset;
92     }
93     return generator->ChangePhaseOffset(this, offset);
94 }
95 
OnVSyncEvent(int64_t now,int64_t period,uint32_t refreshRate,VSyncMode vsyncMode,uint32_t vsyncMaxRefreshRate)96 void VSyncController::OnVSyncEvent(int64_t now, int64_t period,
97     uint32_t refreshRate, VSyncMode vsyncMode, uint32_t vsyncMaxRefreshRate)
98 {
99     Callback *cb = nullptr;
100     {
101         std::lock_guard<std::mutex> locker(callbackMutex_);
102         cb = callback_;
103     }
104     if (cb != nullptr) {
105         cb->OnVSyncEvent(now, period, refreshRate, vsyncMode, vsyncMaxRefreshRate);
106     }
107 }
108 
OnPhaseOffsetChanged(int64_t phaseOffset)109 void VSyncController::OnPhaseOffsetChanged(int64_t phaseOffset)
110 {
111     std::lock_guard<std::mutex> locker(offsetMutex_);
112     phaseOffset_ = phaseOffset;
113 }
114 
115 /* std::pair<id, refresh rate> */
OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t,uint32_t>> & refreshRates)116 void VSyncController::OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates)
117 {
118     Callback *cb = nullptr;
119     {
120         std::lock_guard<std::mutex> locker(callbackMutex_);
121         cb = callback_;
122     }
123     if (cb != nullptr) {
124         cb->OnConnsRefreshRateChanged(refreshRates);
125     }
126 }
127 }
128 }
129