blob: ad0ea72623c4f4c8e78fd9ee670955c33d18a675 [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;
Ady Abraham011f8ba2022-11-22 15:09:07 -080078 virtual void destroyVsync() = 0;
Ady Abraham55fa7272020-09-30 19:19:27 -070079 virtual void setDuration(std::chrono::nanoseconds workDuration) = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080080 virtual void waitMessage() = 0;
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070081 virtual void postMessage(sp<MessageHandler>&&) = 0;
Chavi Weingarten076acac2023-01-19 17:20:43 +000082 virtual void postMessageDelayed(sp<MessageHandler>&&, nsecs_t uptimeDelay) = 0;
Dominik Laskowskif11728a2022-07-28 13:07:42 -070083 virtual void scheduleConfigure() = 0;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070084 virtual void scheduleFrame() = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080085
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070086 using Clock = std::chrono::steady_clock;
87 virtual std::optional<Clock::time_point> getScheduledFrameTime() const = 0;
88};
Lloyd Pique3fcdef12018-01-22 17:14:00 -080089
90namespace impl {
91
Ady Abraham55fa7272020-09-30 19:19:27 -070092class MessageQueue : public android::MessageQueue {
93protected:
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080094 class Handler : public MessageHandler {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080095 MessageQueue& mQueue;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070096 std::atomic_bool mFramePending = false;
Dominik Laskowski08fbd852022-07-14 08:53:42 -070097
98 std::atomic<VsyncId> mVsyncId;
99 std::atomic<TimePoint> mExpectedVsyncTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -0800100
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800101 public:
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700102 explicit Handler(MessageQueue& queue) : mQueue(queue) {}
Ady Abraham55fa7272020-09-30 19:19:27 -0700103 void handleMessage(const Message& message) override;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700104
105 bool isFramePending() const;
106
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700107 virtual void dispatchFrame(VsyncId, TimePoint expectedVsyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800108 };
109
110 friend class Handler;
111
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700112 // For tests.
113 MessageQueue(ICompositor&, sp<Handler>);
114
115 void vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime);
116
117private:
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700118 virtual void onFrameSignal(ICompositor&, VsyncId, TimePoint expectedVsyncTime) = 0;
119
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700120 ICompositor& mCompositor;
121 const sp<Looper> mLooper;
122 const sp<Handler> mHandler;
Ady Abraham55fa7272020-09-30 19:19:27 -0700123
124 struct Vsync {
125 frametimeline::TokenManager* tokenManager = nullptr;
126 std::unique_ptr<scheduler::VSyncCallbackRegistration> registration;
127
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700128 mutable std::mutex mutex;
Ady Abraham55fa7272020-09-30 19:19:27 -0700129 TracedOrdinal<std::chrono::nanoseconds> workDuration
130 GUARDED_BY(mutex) = {"VsyncWorkDuration-sf", std::chrono::nanoseconds(0)};
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700131 TimePoint lastCallbackTime GUARDED_BY(mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700132 std::optional<nsecs_t> scheduledFrameTime GUARDED_BY(mutex);
Ady Abraham55fa7272020-09-30 19:19:27 -0700133 TracedOrdinal<int> value = {"VSYNC-sf", 0};
134 };
135
Dominik Laskowski208235c2020-12-03 15:38:51 -0800136 Vsync mVsync;
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;
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