blob: 58ce9b9a2fcaac987a9b4c07e1ec3af126b83dc2 [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
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080036class SurfaceFlinger;
Mathias Agopian8aedd472012-01-24 16:39:14 -080037
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070038template <typename F>
39class Task : public MessageHandler {
40 template <typename G>
41 friend auto makeTask(G&&);
Mathias Agopianf1d8e872009-04-20 19:39:12 -070042
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070043 explicit Task(F&& f) : mTask(std::move(f)) {}
Lloyd Pique78ce4182018-01-31 16:39:51 -080044
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070045 void handleMessage(const Message&) override { mTask(); }
Mathias Agopianbb641242010-05-18 17:06:55 -070046
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070047 using T = std::invoke_result_t<F>;
48 std::packaged_task<T()> mTask;
Mathias Agopianf61c57f2011-11-23 16:49:10 -080049};
Mathias Agopianf1d8e872009-04-20 19:39:12 -070050
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070051template <typename F>
52inline auto makeTask(F&& f) {
53 sp<Task<F>> task = new Task<F>(std::move(f));
54 return std::make_pair(task, task->mTask.get_future());
55}
Mathias Agopianf1d8e872009-04-20 19:39:12 -070056
Mathias Agopianf61c57f2011-11-23 16:49:10 -080057class MessageQueue {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080058public:
59 enum {
60 INVALIDATE = 0,
61 REFRESH = 1,
62 };
63
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070064 virtual ~MessageQueue() = default;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080065
66 virtual void init(const sp<SurfaceFlinger>& flinger) = 0;
Ady Abraham55fa7272020-09-30 19:19:27 -070067 virtual void initVsync(scheduler::VSyncDispatch&, frametimeline::TokenManager&,
68 std::chrono::nanoseconds workDuration) = 0;
69 virtual void setDuration(std::chrono::nanoseconds workDuration) = 0;
Dominik Laskowski208235c2020-12-03 15:38:51 -080070 virtual void setInjector(sp<EventThreadConnection>) = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080071 virtual void waitMessage() = 0;
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070072 virtual void postMessage(sp<MessageHandler>&&) = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080073 virtual void invalidate() = 0;
74 virtual void refresh() = 0;
Ady Abraham562c2712021-05-07 15:10:42 -070075 virtual std::optional<std::chrono::steady_clock::time_point> nextExpectedInvalidate() = 0;
Lloyd Pique3fcdef12018-01-22 17:14:00 -080076};
77
78// ---------------------------------------------------------------------------
79
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 {
Ady Abraham562c2712021-05-07 15:10:42 -070085 enum : uint32_t {
86 eventMaskInvalidate = 0x1,
87 eventMaskRefresh = 0x2,
88 eventMaskTransaction = 0x4
89 };
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080090 MessageQueue& mQueue;
Ady Abraham562c2712021-05-07 15:10:42 -070091 std::atomic<uint32_t> mEventMask;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070092 std::atomic<int64_t> mVsyncId;
Ady Abraham5facfb12020-04-22 15:18:31 -070093 std::atomic<nsecs_t> mExpectedVSyncTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -080094
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080095 public:
Lloyd Pique78ce4182018-01-31 16:39:51 -080096 explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {}
Ady Abraham55fa7272020-09-30 19:19:27 -070097 void handleMessage(const Message& message) override;
98 virtual void dispatchRefresh();
99 virtual void dispatchInvalidate(int64_t vsyncId, nsecs_t expectedVSyncTimestamp);
Ady Abraham562c2712021-05-07 15:10:42 -0700100 virtual bool invalidatePending();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800101 };
102
103 friend class Handler;
104
105 sp<SurfaceFlinger> mFlinger;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800106 sp<Looper> mLooper;
Ady Abraham55fa7272020-09-30 19:19:27 -0700107
108 struct Vsync {
109 frametimeline::TokenManager* tokenManager = nullptr;
110 std::unique_ptr<scheduler::VSyncCallbackRegistration> registration;
111
112 std::mutex mutex;
113 TracedOrdinal<std::chrono::nanoseconds> workDuration
114 GUARDED_BY(mutex) = {"VsyncWorkDuration-sf", std::chrono::nanoseconds(0)};
115 std::chrono::nanoseconds lastCallbackTime GUARDED_BY(mutex) = std::chrono::nanoseconds{0};
Ady Abraham562c2712021-05-07 15:10:42 -0700116 bool scheduled GUARDED_BY(mutex) = false;
117 std::optional<nsecs_t> expectedWakeupTime GUARDED_BY(mutex);
Ady Abraham55fa7272020-09-30 19:19:27 -0700118 TracedOrdinal<int> value = {"VSYNC-sf", 0};
119 };
120
Dominik Laskowski208235c2020-12-03 15:38:51 -0800121 struct Injector {
122 gui::BitTube tube;
123 std::mutex mutex;
124 sp<EventThreadConnection> connection GUARDED_BY(mutex);
125 };
Ady Abraham55fa7272020-09-30 19:19:27 -0700126
Dominik Laskowski208235c2020-12-03 15:38:51 -0800127 Vsync mVsync;
128 Injector mInjector;
129
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800130 sp<Handler> mHandler;
131
Ady Abraham55fa7272020-09-30 19:19:27 -0700132 void vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime);
Dominik Laskowski208235c2020-12-03 15:38:51 -0800133 void injectorCallback();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700134
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800135public:
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800136 ~MessageQueue() override = default;
137 void init(const sp<SurfaceFlinger>& flinger) override;
Ady Abraham55fa7272020-09-30 19:19:27 -0700138 void initVsync(scheduler::VSyncDispatch&, frametimeline::TokenManager&,
139 std::chrono::nanoseconds workDuration) override;
140 void setDuration(std::chrono::nanoseconds workDuration) override;
Dominik Laskowski208235c2020-12-03 15:38:51 -0800141 void setInjector(sp<EventThreadConnection>) 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
146 // sends INVALIDATE message at next VSYNC
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800147 void invalidate() override;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800148
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700149 // sends REFRESH message at next VSYNC
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800150 void refresh() override;
Ady Abraham562c2712021-05-07 15:10:42 -0700151
152 std::optional<std::chrono::steady_clock::time_point> nextExpectedInvalidate() override;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700153};
154
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800155} // namespace impl
156} // namespace android