blob: 2a0b08417a774250b5481dfea018cba6b2b03410 [file] [log] [blame]
Sean Paul4057be32015-05-13 06:23:09 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Roman Stratiienkobde95662022-12-10 20:27:58 +020017#pragma once
Sean Paul4057be32015-05-13 06:23:09 -070018
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020019#include <condition_variable>
Roman Stratiienko863a3c22021-09-29 13:00:29 +030020#include <functional>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030021#include <map>
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020022#include <mutex>
23#include <thread>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030024
Roman Stratiienko13cc3662020-08-29 21:35:39 +030025#include "DrmDevice.h"
Sean Paul4057be32015-05-13 06:23:09 -070026
27namespace android {
28
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020029struct VSyncWorkerCallbacks {
30 std::function<void(uint64_t /*timestamp*/)> out_event;
31 std::function<uint32_t()> get_vperiod_ns;
32};
Sean Paul4057be32015-05-13 06:23:09 -070033
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020034class VSyncWorker {
35 public:
36 ~VSyncWorker() = default;
37
38 auto static CreateInstance(DrmDisplayPipeline *pipe,
39 VSyncWorkerCallbacks &callbacks)
40 -> std::shared_ptr<VSyncWorker>;
Sean Paul4057be32015-05-13 06:23:09 -070041
Adrian Salidofa37f672017-02-16 10:29:46 -080042 void VSyncControl(bool enabled);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020043 void StopThread();
Sean Paul4057be32015-05-13 06:23:09 -070044
45 private:
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020046 VSyncWorker() = default;
47
48 void ThreadFn(const std::shared_ptr<VSyncWorker> &vsw);
49
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020050 int64_t GetPhasedVSync(int64_t frame_ns, int64_t current) const;
Sean Paul4057be32015-05-13 06:23:09 -070051 int SyntheticWaitVBlank(int64_t *timestamp);
52
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020053 VSyncWorkerCallbacks callbacks_;
Sean Paul4057be32015-05-13 06:23:09 -070054
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020055 UniqueFd drm_fd_;
56 uint32_t high_crtc_ = 0;
57
58 bool enabled_ = false;
59 bool thread_exit_ = false;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020060 int64_t last_timestamp_ = -1;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020061
62 std::condition_variable cv_;
63 std::thread vswt_;
64 std::mutex mutex_;
Sean Paul4057be32015-05-13 06:23:09 -070065};
Sean Paulf72cccd2018-08-27 13:59:08 -040066} // namespace android