blob: edb424b5b9b564bf20f9e2b81eb4631fb25b0383 [file] [log] [blame]
Mathias Agopianf1d8e872009-04-20 19:39:12 -07001/*
2 * Copyright (C) 2009 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
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070017#pragma once
Mathias Agopianf1d8e872009-04-20 19:39:12 -070018
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070019#include <cstdint>
20#include <future>
21#include <type_traits>
22#include <utility>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070023
Dominik Laskowski208235c2020-12-03 15:38:51 -080024#include <android-base/thread_annotations.h>
Huihong Luo6fac5232021-11-22 16:05:23 -080025#include <android/gui/IDisplayEventConnection.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080026#include <private/gui/BitTube.h>
Dominik Laskowski208235c2020-12-03 15:38:51 -080027#include <utils/Looper.h>
Ady Abrahamd11bade2022-08-01 16:18:03 -070028#include <utils/StrongPointer.h>
Dominik Laskowski208235c2020-12-03 15:38:51 -080029#include <utils/Timers.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080030
Dominik Laskowski08fbd852022-07-14 08:53:42 -070031#include <scheduler/Time.h>
32#include <scheduler/VsyncId.h>
33
Dominik Laskowskif654d572018-12-20 11:03:06 -080034#include "EventThread.h"
Ady Abraham55fa7272020-09-30 19:19:27 -070035#include "TracedOrdinal.h"
36#include "VSyncDispatch.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070037
38namespace android {
39
Dominik Laskowski63f12792023-01-21 16:58:22 -050040struct ICompositor;
Mathias Agopian8aedd472012-01-24 16:39:14 -080041
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070042template <typename F>
43class Task : public MessageHandler {
44 template <typename G>
45 friend auto makeTask(G&&);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070046
Ady Abrahamd11bade2022-08-01 16:18:03 -070047 template <typename... Args>
48 friend sp<Task<F>> sp<Task<F>>::make(Args&&... args);
49
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070050 explicit Task(F&& f) : mTask(std::move(f)) {}
Lloyd Pique78ce4182018-01-31 16:39:51 -080051
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070052 void handleMessage(const Message&) override { mTask(); }
Mathias Agopianbb641242010-05-18 17:06:55 -070053
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070054 using T = std::invoke_result_t<F>;
55 std::packaged_task<T()> mTask;
Mathias Agopianf61c57f2011-11-23 16:49:10 -080056};
Mathias Agopianf1d8e872009-04-20 19:39:12 -070057
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070058template <typename F>
59inline auto makeTask(F&& f) {
Ady Abraham81015532022-08-02 16:27:53 +000060 sp<Task<F>> task = sp<Task<F>>::make(std::forward<F>(f));
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070061 return std::make_pair(task, task->mTask.get_future());
62}
Mathias Agopianf1d8e872009-04-20 19:39:12 -070063
Mathias Agopianf61c57f2011-11-23 16:49:10 -080064class MessageQueue {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080065public:
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070066 virtual ~MessageQueue() = default;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080067
Leon Scroggins IIIa7be94e2024-01-23 12:24:30 -050068 virtual void initVsyncInternal(std::shared_ptr<scheduler::VSyncDispatch>,
69 frametimeline::TokenManager&,
70 std::chrono::nanoseconds workDuration) = 0;
Ady Abraham011f8ba2022-11-22 15:09:07 -080071 virtual void destroyVsync() = 0;
Ady Abraham55fa7272020-09-30 19:19:27 -070072 virtual void setDuration(std::chrono::nanoseconds workDuration) = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080073 virtual void waitMessage() = 0;
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070074 virtual void postMessage(sp<MessageHandler>&&) = 0;
Chavi Weingarten076acac2023-01-19 17:20:43 +000075 virtual void postMessageDelayed(sp<MessageHandler>&&, nsecs_t uptimeDelay) = 0;
Dominik Laskowskif11728a2022-07-28 13:07:42 -070076 virtual void scheduleConfigure() = 0;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070077 virtual void scheduleFrame() = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080078
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070079 using Clock = std::chrono::steady_clock;
80 virtual std::optional<Clock::time_point> getScheduledFrameTime() const = 0;
81};
Lloyd Pique3fcdef12018-01-22 17:14:00 -080082
83namespace impl {
84
Ady Abraham55fa7272020-09-30 19:19:27 -070085class MessageQueue : public android::MessageQueue {
86protected:
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080087 class Handler : public MessageHandler {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080088 MessageQueue& mQueue;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070089 std::atomic_bool mFramePending = false;
Dominik Laskowski08fbd852022-07-14 08:53:42 -070090
91 std::atomic<VsyncId> mVsyncId;
92 std::atomic<TimePoint> mExpectedVsyncTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -080093
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080094 public:
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070095 explicit Handler(MessageQueue& queue) : mQueue(queue) {}
Ady Abraham55fa7272020-09-30 19:19:27 -070096 void handleMessage(const Message& message) override;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070097
98 bool isFramePending() const;
99
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700100 virtual void dispatchFrame(VsyncId, TimePoint expectedVsyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800101 };
102
103 friend class Handler;
104
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700105 // For tests.
106 MessageQueue(ICompositor&, sp<Handler>);
107
108 void vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime);
109
Leon Scroggins III67388622023-02-06 20:36:20 -0500110 void onNewVsyncSchedule(std::shared_ptr<scheduler::VSyncDispatch>) EXCLUDES(mVsync.mutex);
111
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700112private:
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700113 virtual void onFrameSignal(ICompositor&, VsyncId, TimePoint expectedVsyncTime) = 0;
114
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700115 ICompositor& mCompositor;
116 const sp<Looper> mLooper;
117 const sp<Handler> mHandler;
Ady Abraham55fa7272020-09-30 19:19:27 -0700118
119 struct Vsync {
120 frametimeline::TokenManager* tokenManager = nullptr;
Ady Abraham55fa7272020-09-30 19:19:27 -0700121
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700122 mutable std::mutex mutex;
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -0400123 std::unique_ptr<scheduler::VSyncCallbackRegistration> registration GUARDED_BY(mutex);
Ady Abraham55fa7272020-09-30 19:19:27 -0700124 TracedOrdinal<std::chrono::nanoseconds> workDuration
125 GUARDED_BY(mutex) = {"VsyncWorkDuration-sf", std::chrono::nanoseconds(0)};
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700126 TimePoint lastCallbackTime GUARDED_BY(mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700127 std::optional<nsecs_t> scheduledFrameTime GUARDED_BY(mutex);
Ady Abraham55fa7272020-09-30 19:19:27 -0700128 TracedOrdinal<int> value = {"VSYNC-sf", 0};
129 };
130
Dominik Laskowski208235c2020-12-03 15:38:51 -0800131 Vsync mVsync;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700132
Leon Scroggins IIIfb7ed562023-04-05 11:39:25 -0400133 // Returns the old registration so it can be destructed outside the lock to
134 // avoid deadlock.
135 std::unique_ptr<scheduler::VSyncCallbackRegistration> onNewVsyncScheduleLocked(
136 std::shared_ptr<scheduler::VSyncDispatch>) REQUIRES(mVsync.mutex);
Leon Scroggins III67388622023-02-06 20:36:20 -0500137
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800138public:
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700139 explicit MessageQueue(ICompositor&);
140
Leon Scroggins IIIa7be94e2024-01-23 12:24:30 -0500141 void initVsyncInternal(std::shared_ptr<scheduler::VSyncDispatch>, frametimeline::TokenManager&,
142 std::chrono::nanoseconds workDuration) override;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800143 void destroyVsync() override;
Ady Abraham55fa7272020-09-30 19:19:27 -0700144 void setDuration(std::chrono::nanoseconds workDuration) override;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800146 void waitMessage() override;
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700147 void postMessage(sp<MessageHandler>&&) override;
Chavi Weingarten076acac2023-01-19 17:20:43 +0000148 void postMessageDelayed(sp<MessageHandler>&&, nsecs_t uptimeDelay) override;
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700149
Dominik Laskowskif11728a2022-07-28 13:07:42 -0700150 void scheduleConfigure() override;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700151 void scheduleFrame() override;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800152
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700153 std::optional<Clock::time_point> getScheduledFrameTime() const override;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700154};
155
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800156} // namespace impl
157} // namespace android