blob: 04de492479c36eb19672c20f788061301ae2f46c [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 Laskowskie0e0cde2021-07-30 10:42:05 -070040struct ICompositor {
Dominik Laskowskif11728a2022-07-28 13:07:42 -070041 virtual void configure() = 0;
Dominik Laskowski08fbd852022-07-14 08:53:42 -070042 virtual bool commit(TimePoint frameTime, VsyncId, TimePoint expectedVsyncTime) = 0;
43 virtual void composite(TimePoint frameTime, VsyncId) = 0;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070044 virtual void sample() = 0;
45
46protected:
47 ~ICompositor() = default;
48};
Mathias Agopian8aedd472012-01-24 16:39:14 -080049
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070050template <typename F>
51class Task : public MessageHandler {
52 template <typename G>
53 friend auto makeTask(G&&);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070054
Ady Abrahamd11bade2022-08-01 16:18:03 -070055 template <typename... Args>
56 friend sp<Task<F>> sp<Task<F>>::make(Args&&... args);
57
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070058 explicit Task(F&& f) : mTask(std::move(f)) {}
Lloyd Pique78ce4182018-01-31 16:39:51 -080059
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070060 void handleMessage(const Message&) override { mTask(); }
Mathias Agopianbb641242010-05-18 17:06:55 -070061
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070062 using T = std::invoke_result_t<F>;
63 std::packaged_task<T()> mTask;
Mathias Agopianf61c57f2011-11-23 16:49:10 -080064};
Mathias Agopianf1d8e872009-04-20 19:39:12 -070065
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070066template <typename F>
67inline auto makeTask(F&& f) {
Ady Abraham81015532022-08-02 16:27:53 +000068 sp<Task<F>> task = sp<Task<F>>::make(std::forward<F>(f));
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070069 return std::make_pair(task, task->mTask.get_future());
70}
Mathias Agopianf1d8e872009-04-20 19:39:12 -070071
Mathias Agopianf61c57f2011-11-23 16:49:10 -080072class MessageQueue {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080073public:
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070074 virtual ~MessageQueue() = default;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080075
Ady Abraham55fa7272020-09-30 19:19:27 -070076 virtual void initVsync(scheduler::VSyncDispatch&, frametimeline::TokenManager&,
77 std::chrono::nanoseconds workDuration) = 0;
78 virtual void setDuration(std::chrono::nanoseconds workDuration) = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080079 virtual void waitMessage() = 0;
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070080 virtual void postMessage(sp<MessageHandler>&&) = 0;
Dominik Laskowskif11728a2022-07-28 13:07:42 -070081 virtual void scheduleConfigure() = 0;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070082 virtual void scheduleFrame() = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080083
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070084 using Clock = std::chrono::steady_clock;
85 virtual std::optional<Clock::time_point> getScheduledFrameTime() const = 0;
86};
Lloyd Pique3fcdef12018-01-22 17:14:00 -080087
88namespace impl {
89
Ady Abraham55fa7272020-09-30 19:19:27 -070090class MessageQueue : public android::MessageQueue {
91protected:
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080092 class Handler : public MessageHandler {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080093 MessageQueue& mQueue;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070094 std::atomic_bool mFramePending = false;
Dominik Laskowski08fbd852022-07-14 08:53:42 -070095
96 std::atomic<VsyncId> mVsyncId;
97 std::atomic<TimePoint> mExpectedVsyncTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -080098
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080099 public:
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700100 explicit Handler(MessageQueue& queue) : mQueue(queue) {}
Ady Abraham55fa7272020-09-30 19:19:27 -0700101 void handleMessage(const Message& message) override;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700102
103 bool isFramePending() const;
104
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700105 virtual void dispatchFrame(VsyncId, TimePoint expectedVsyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800106 };
107
108 friend class Handler;
109
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700110 // For tests.
111 MessageQueue(ICompositor&, sp<Handler>);
112
113 void vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime);
114
115private:
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700116 virtual void onFrameSignal(ICompositor&, VsyncId, TimePoint expectedVsyncTime) = 0;
117
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700118 ICompositor& mCompositor;
119 const sp<Looper> mLooper;
120 const sp<Handler> mHandler;
Ady Abraham55fa7272020-09-30 19:19:27 -0700121
122 struct Vsync {
123 frametimeline::TokenManager* tokenManager = nullptr;
124 std::unique_ptr<scheduler::VSyncCallbackRegistration> registration;
125
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700126 mutable std::mutex mutex;
Ady Abraham55fa7272020-09-30 19:19:27 -0700127 TracedOrdinal<std::chrono::nanoseconds> workDuration
128 GUARDED_BY(mutex) = {"VsyncWorkDuration-sf", std::chrono::nanoseconds(0)};
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700129 TimePoint lastCallbackTime GUARDED_BY(mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700130 std::optional<nsecs_t> scheduledFrameTime GUARDED_BY(mutex);
Ady Abraham55fa7272020-09-30 19:19:27 -0700131 TracedOrdinal<int> value = {"VSYNC-sf", 0};
132 };
133
Dominik Laskowski208235c2020-12-03 15:38:51 -0800134 Vsync mVsync;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700135
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800136public:
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700137 explicit MessageQueue(ICompositor&);
138
Ady Abraham55fa7272020-09-30 19:19:27 -0700139 void initVsync(scheduler::VSyncDispatch&, frametimeline::TokenManager&,
140 std::chrono::nanoseconds workDuration) override;
141 void setDuration(std::chrono::nanoseconds workDuration) override;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800142
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800143 void waitMessage() override;
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700144 void postMessage(sp<MessageHandler>&&) override;
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700145
Dominik Laskowskif11728a2022-07-28 13:07:42 -0700146 void scheduleConfigure() override;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700147 void scheduleFrame() override;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800148
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700149 std::optional<Clock::time_point> getScheduledFrameTime() const override;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700150};
151
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800152} // namespace impl
153} // namespace android