1/*
2 * Copyright (c) 2023 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#ifndef OHOS_VSYNC_STATION_H
17#define OHOS_VSYNC_STATION_H
18
19#include <atomic>
20#include <functional>
21#include <map>
22#include <memory>
23#include <unordered_set>
24
25#include <refbase.h>
26#include <vsync_receiver.h>
27
28#include "wm_common.h"
29#include "wm_single_instance.h"
30#include <ui/rs_display_node.h>
31
32namespace OHOS {
33namespace Rosen {
34class VsyncStation {
35public:
36    explicit VsyncStation(NodeId nodeId);
37    ~VsyncStation()
38    {
39        std::lock_guard<std::mutex> lock(mtx_);
40        destroyed_ = true;
41    }
42
43    void RequestVsync(const std::shared_ptr<VsyncCallback>& vsyncCallback);
44    int64_t GetVSyncPeriod();
45    void RemoveCallback();
46
47private:
48    static void OnVsync(int64_t nanoTimestamp, int64_t frameCount, void* client);
49    void VsyncCallbackInner(int64_t nanoTimestamp, int64_t frameCount);
50    void OnVsyncTimeOut();
51    void Init();
52
53    std::mutex mtx_;
54    NodeId nodeId_ = 0;
55    bool hasRequestedVsync_ = false;
56    bool hasInitVsyncReceiver_ = false;
57    bool destroyed_ = false;
58    const std::string VSYNC_THREAD_ID = "VsyncThread";
59    std::shared_ptr<OHOS::Rosen::VSyncReceiver> receiver_ = nullptr;
60    std::unordered_set<std::shared_ptr<VsyncCallback>> vsyncCallbacks_;
61    VSyncReceiver::FrameCallback frameCallback_ = {
62        .userData_ = this,
63        .callbackWithId_ = OnVsync,
64    };
65};
66} // namespace Rosen
67} // namespace OHOS
68#endif // OHOS_VSYNC_STATION_H