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 
17 #ifndef VSYNC_VSYNC_CONTROLLER_H
18 #define VSYNC_VSYNC_CONTROLLER_H
19 
20 #include <cstdint>
21 #include <refbase.h>
22 #include <mutex>
23 #include "vsync_generator.h"
24 #include "graphic_common.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 class VSyncController : public VSyncGenerator::Callback {
29 public:
30     class Callback {
31     public:
32         virtual void OnVSyncEvent(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode) = 0;
33         virtual ~Callback() = default;
34         /* std::pair<id, refresh rate> */
35         virtual void OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates) = 0;
36     };
37 
38     VSyncController(const sptr<VSyncGenerator> &geng, int64_t offset);
39     ~VSyncController();
40 
41     // nocopyable
42     VSyncController(const VSyncController &) = delete;
43     VSyncController &operator=(const VSyncController &) = delete;
44 
45     VsyncError SetEnable(bool enable, bool& isGeneratorEnable);
46     VsyncError SetCallback(Callback* cb);
47     VsyncError SetPhaseOffset(int64_t offset);
48 
49 private:
50 
51     void OnVSyncEvent(int64_t now, int64_t period, uint32_t refreshRate, VSyncMode vsyncMode);
52     void OnPhaseOffsetChanged(int64_t phaseOffset);
53     /* std::pair<id, refresh rate> */
54     void OnConnsRefreshRateChanged(const std::vector<std::pair<uint64_t, uint32_t>> &refreshRates);
55     wptr<VSyncGenerator> generator_;
56     std::mutex callbackMutex_;
57     Callback* callback_;
58 
59     std::mutex offsetMutex_;
60     int64_t phaseOffset_;
61     bool enabled_;
62 };
63 } // namespace Rosen
64 } // namespace OHOS
65 
66 #endif
67