blob: 9dde82b07d1d9cc10cdf823d5d0a2ec69fdfe333 [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 Laskowskif654d572018-12-20 11:03:06 -080031#include "EventThread.h"
Ady Abraham55fa7272020-09-30 19:19:27 -070032#include "TracedOrdinal.h"
33#include "VSyncDispatch.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070034
35namespace android {
36
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070037struct ICompositor {
38 virtual bool commit(nsecs_t frameTime, int64_t vsyncId, nsecs_t expectedVsyncTime) = 0;
Ady Abraham302ebed2022-03-07 16:03:41 -080039 virtual void composite(nsecs_t frameTime, int64_t vsyncId) = 0;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070040 virtual void sample() = 0;
41
42protected:
43 ~ICompositor() = default;
44};
Mathias Agopian8aedd472012-01-24 16:39:14 -080045
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070046template <typename F>
47class Task : public MessageHandler {
48 template <typename G>
49 friend auto makeTask(G&&);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070050
Ady Abrahamd11bade2022-08-01 16:18:03 -070051 template <typename... Args>
52 friend sp<Task<F>> sp<Task<F>>::make(Args&&... args);
53
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070054 explicit Task(F&& f) : mTask(std::move(f)) {}
Lloyd Pique78ce4182018-01-31 16:39:51 -080055
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070056 void handleMessage(const Message&) override { mTask(); }
Mathias Agopianbb641242010-05-18 17:06:55 -070057
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070058 using T = std::invoke_result_t<F>;
59 std::packaged_task<T()> mTask;
Mathias Agopianf61c57f2011-11-23 16:49:10 -080060};
Mathias Agopianf1d8e872009-04-20 19:39:12 -070061
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070062template <typename F>
63inline auto makeTask(F&& f) {
Ady Abraham81015532022-08-02 16:27:53 +000064 sp<Task<F>> task = sp<Task<F>>::make(std::forward<F>(f));
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070065 return std::make_pair(task, task->mTask.get_future());
66}
Mathias Agopianf1d8e872009-04-20 19:39:12 -070067
Mathias Agopianf61c57f2011-11-23 16:49:10 -080068class MessageQueue {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080069public:
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070070 virtual ~MessageQueue() = default;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080071
Ady Abraham55fa7272020-09-30 19:19:27 -070072 virtual void initVsync(scheduler::VSyncDispatch&, frametimeline::TokenManager&,
73 std::chrono::nanoseconds workDuration) = 0;
74 virtual void setDuration(std::chrono::nanoseconds workDuration) = 0;
Dominik Laskowski208235c2020-12-03 15:38:51 -080075 virtual void setInjector(sp<EventThreadConnection>) = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080076 virtual void waitMessage() = 0;
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070077 virtual void postMessage(sp<MessageHandler>&&) = 0;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070078 virtual void scheduleFrame() = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080079
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070080 using Clock = std::chrono::steady_clock;
81 virtual std::optional<Clock::time_point> getScheduledFrameTime() const = 0;
82};
Lloyd Pique3fcdef12018-01-22 17:14:00 -080083
84namespace impl {
85
Ady Abraham55fa7272020-09-30 19:19:27 -070086class MessageQueue : public android::MessageQueue {
87protected:
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080088 class Handler : public MessageHandler {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080089 MessageQueue& mQueue;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070090 std::atomic_bool mFramePending = false;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070091 std::atomic<int64_t> mVsyncId = 0;
92 std::atomic<nsecs_t> mExpectedVsyncTime = 0;
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 Laskowski46f3e3b2021-08-10 11:44:24 -0700100 virtual void dispatchFrame(int64_t vsyncId, nsecs_t 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
110private:
111 ICompositor& mCompositor;
112 const sp<Looper> mLooper;
113 const sp<Handler> mHandler;
Ady Abraham55fa7272020-09-30 19:19:27 -0700114
115 struct Vsync {
116 frametimeline::TokenManager* tokenManager = nullptr;
117 std::unique_ptr<scheduler::VSyncCallbackRegistration> registration;
118
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700119 mutable std::mutex mutex;
Ady Abraham55fa7272020-09-30 19:19:27 -0700120 TracedOrdinal<std::chrono::nanoseconds> workDuration
121 GUARDED_BY(mutex) = {"VsyncWorkDuration-sf", std::chrono::nanoseconds(0)};
122 std::chrono::nanoseconds lastCallbackTime GUARDED_BY(mutex) = std::chrono::nanoseconds{0};
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700123 std::optional<nsecs_t> scheduledFrameTime GUARDED_BY(mutex);
Ady Abraham55fa7272020-09-30 19:19:27 -0700124 TracedOrdinal<int> value = {"VSYNC-sf", 0};
125 };
126
Dominik Laskowski208235c2020-12-03 15:38:51 -0800127 struct Injector {
128 gui::BitTube tube;
129 std::mutex mutex;
130 sp<EventThreadConnection> connection GUARDED_BY(mutex);
131 };
Ady Abraham55fa7272020-09-30 19:19:27 -0700132
Dominik Laskowski208235c2020-12-03 15:38:51 -0800133 Vsync mVsync;
134 Injector mInjector;
135
Dominik Laskowski208235c2020-12-03 15:38:51 -0800136 void injectorCallback();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700137
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800138public:
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700139 explicit MessageQueue(ICompositor&);
140
Ady Abraham55fa7272020-09-30 19:19:27 -0700141 void initVsync(scheduler::VSyncDispatch&, frametimeline::TokenManager&,
142 std::chrono::nanoseconds workDuration) override;
143 void setDuration(std::chrono::nanoseconds workDuration) override;
Dominik Laskowski208235c2020-12-03 15:38:51 -0800144 void setInjector(sp<EventThreadConnection>) 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;
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700148
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700149 void scheduleFrame() override;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800150
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700151 std::optional<Clock::time_point> getScheduledFrameTime() const override;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700152};
153
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800154} // namespace impl
155} // namespace android