• 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 
18 namespace OHOS {
19 namespace Rosen {
VSyncController(const sptr<VSyncGenerator> & geng,int64_t offset)20 VSyncController::VSyncController(const sptr<VSyncGenerator> &geng, int64_t offset)
21     : generator_(geng), callbackMutex_(), callback_(nullptr),
22     offsetMutex_(), phaseOffset_(offset), enabled_(false)
23 {
24 }
25 
~VSyncController()26 VSyncController::~VSyncController()
27 {
28 }
29 
SetEnable(bool enbale,bool & isGeneratorEnable)30 VsyncError VSyncController::SetEnable(bool enbale, bool& isGeneratorEnable)
31 {
32     if (generator_ == nullptr) {
33         return VSYNC_ERROR_NULLPTR;
34     }
35     const sptr<VSyncGenerator> generator = generator_.promote();
36     if (generator == nullptr) {
37         return VSYNC_ERROR_NULLPTR;
38     }
39     std::lock_guard<std::mutex> locker(offsetMutex_);
40     VsyncError ret = VSYNC_ERROR_OK;
41     if (enbale) {
42         // If the sampler does not complete the sampling work, the generator does not work
43         // We need to tell the distributor to use the software vsync
44         isGeneratorEnable = generator->IsEnable();
45         if (isGeneratorEnable) {
46             ret = generator->AddListener(phaseOffset_, this);
47         } else {
48             ret = VSYNC_ERROR_API_FAILED;
49         }
50     } else {
51         ret = generator->RemoveListener(this);
52         isGeneratorEnable = enbale;
53     }
54 
55     enabled_ = isGeneratorEnable;
56     return ret;
57 }
58 
SetCallback(Callback * cb)59 VsyncError VSyncController::SetCallback(Callback *cb)
60 {
61     if (cb == nullptr) {
62         return VSYNC_ERROR_NULLPTR;
63     }
64     std::lock_guard<std::mutex> locker(callbackMutex_);
65     callback_ = cb;
66     return VSYNC_ERROR_OK;
67 }
68 
SetPhaseOffset(int64_t offset)69 VsyncError VSyncController::SetPhaseOffset(int64_t offset)
70 {
71     if (generator_ == nullptr) {
72         return VSYNC_ERROR_NULLPTR;
73     }
74     const sptr<VSyncGenerator> generator = generator_.promote();
75     if (generator == nullptr) {
76         return VSYNC_ERROR_NULLPTR;
77     }
78     std::lock_guard<std::mutex> locker(offsetMutex_);
79     phaseOffset_ = offset;
80     return generator->ChangePhaseOffset(this, phaseOffset_);
81 }
82 
OnVSyncEvent(int64_t now,int64_t period)83 void VSyncController::OnVSyncEvent(int64_t now, int64_t period)
84 {
85     Callback *cb = nullptr;
86     {
87         std::lock_guard<std::mutex> locker(callbackMutex_);
88         cb = callback_;
89     }
90     if (cb != nullptr) {
91         cb->OnVSyncEvent(now, period);
92     }
93 }
94 }
95 }
96