blob: dd69d60580c0f0f680c134412fd6186eeaa19e57 [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>
Lloyd Pique3fcdef12018-01-22 17:14:00 -080025#include <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>
28#include <utils/Timers.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080029
Dominik Laskowskif654d572018-12-20 11:03:06 -080030#include "EventThread.h"
Ady Abraham55fa7272020-09-30 19:19:27 -070031#include "TracedOrdinal.h"
32#include "VSyncDispatch.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070033
34namespace android {
35
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070036struct ICompositor {
37 virtual bool commit(nsecs_t frameTime, int64_t vsyncId, nsecs_t expectedVsyncTime) = 0;
38 virtual void composite(nsecs_t frameTime) = 0;
39 virtual void sample() = 0;
40
41protected:
42 ~ICompositor() = default;
43};
Mathias Agopian8aedd472012-01-24 16:39:14 -080044
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070045template <typename F>
46class Task : public MessageHandler {
47 template <typename G>
48 friend auto makeTask(G&&);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070049
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) {
60 sp<Task<F>> task = new Task<F>(std::move(f));
61 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
Ady Abraham55fa7272020-09-30 19:19:27 -070068 virtual void initVsync(scheduler::VSyncDispatch&, frametimeline::TokenManager&,
69 std::chrono::nanoseconds workDuration) = 0;
70 virtual void setDuration(std::chrono::nanoseconds workDuration) = 0;
Dominik Laskowski208235c2020-12-03 15:38:51 -080071 virtual void setInjector(sp<EventThreadConnection>) = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080072 virtual void waitMessage() = 0;
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070073 virtual void postMessage(sp<MessageHandler>&&) = 0;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070074 virtual void scheduleFrame() = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080075
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070076 using Clock = std::chrono::steady_clock;
77 virtual std::optional<Clock::time_point> getScheduledFrameTime() const = 0;
78};
Lloyd Pique3fcdef12018-01-22 17:14:00 -080079
80namespace impl {
81
Ady Abraham55fa7272020-09-30 19:19:27 -070082class MessageQueue : public android::MessageQueue {
83protected:
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080084 class Handler : public MessageHandler {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080085 MessageQueue& mQueue;
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070086 std::atomic_bool mFramePending = false;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070087 std::atomic<int64_t> mVsyncId = 0;
88 std::atomic<nsecs_t> mExpectedVsyncTime = 0;
Lloyd Pique78ce4182018-01-31 16:39:51 -080089
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080090 public:
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070091 explicit Handler(MessageQueue& queue) : mQueue(queue) {}
Ady Abraham55fa7272020-09-30 19:19:27 -070092 void handleMessage(const Message& message) override;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070093
94 bool isFramePending() const;
95
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070096 virtual void dispatchFrame(int64_t vsyncId, nsecs_t expectedVsyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080097 };
98
99 friend class Handler;
100
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700101 // For tests.
102 MessageQueue(ICompositor&, sp<Handler>);
103
104 void vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime);
105
106private:
107 ICompositor& mCompositor;
108 const sp<Looper> mLooper;
109 const sp<Handler> mHandler;
Ady Abraham55fa7272020-09-30 19:19:27 -0700110
111 struct Vsync {
112 frametimeline::TokenManager* tokenManager = nullptr;
113 std::unique_ptr<scheduler::VSyncCallbackRegistration> registration;
114
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700115 mutable std::mutex mutex;
Ady Abraham55fa7272020-09-30 19:19:27 -0700116 TracedOrdinal<std::chrono::nanoseconds> workDuration
117 GUARDED_BY(mutex) = {"VsyncWorkDuration-sf", std::chrono::nanoseconds(0)};
118 std::chrono::nanoseconds lastCallbackTime GUARDED_BY(mutex) = std::chrono::nanoseconds{0};
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700119 std::optional<nsecs_t> scheduledFrameTime GUARDED_BY(mutex);
Ady Abraham55fa7272020-09-30 19:19:27 -0700120 TracedOrdinal<int> value = {"VSYNC-sf", 0};
121 };
122
Dominik Laskowski208235c2020-12-03 15:38:51 -0800123 struct Injector {
124 gui::BitTube tube;
125 std::mutex mutex;
126 sp<EventThreadConnection> connection GUARDED_BY(mutex);
127 };
Ady Abraham55fa7272020-09-30 19:19:27 -0700128
Dominik Laskowski208235c2020-12-03 15:38:51 -0800129 Vsync mVsync;
130 Injector mInjector;
131
Dominik Laskowski208235c2020-12-03 15:38:51 -0800132 void injectorCallback();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700133
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800134public:
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700135 explicit MessageQueue(ICompositor&);
136
Ady Abraham55fa7272020-09-30 19:19:27 -0700137 void initVsync(scheduler::VSyncDispatch&, frametimeline::TokenManager&,
138 std::chrono::nanoseconds workDuration) override;
139 void setDuration(std::chrono::nanoseconds workDuration) override;
Dominik Laskowski208235c2020-12-03 15:38:51 -0800140 void setInjector(sp<EventThreadConnection>) override;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800141
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800142 void waitMessage() override;
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700143 void postMessage(sp<MessageHandler>&&) override;
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700144
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700145 void scheduleFrame() override;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800146
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700147 std::optional<Clock::time_point> getScheduledFrameTime() const override;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700148};
149
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800150} // namespace impl
151} // namespace android