blob: 2934af047c8cdc537cffd45b414d5793d1b8df72 [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;
75};
76
77// ---------------------------------------------------------------------------
78
79namespace impl {
80
Ady Abraham55fa7272020-09-30 19:19:27 -070081class MessageQueue : public android::MessageQueue {
82protected:
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080083 class Handler : public MessageHandler {
Lloyd Pique78ce4182018-01-31 16:39:51 -080084 enum { eventMaskInvalidate = 0x1, eventMaskRefresh = 0x2, eventMaskTransaction = 0x4 };
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080085 MessageQueue& mQueue;
86 int32_t mEventMask;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070087 std::atomic<int64_t> mVsyncId;
Ady Abraham5facfb12020-04-22 15:18:31 -070088 std::atomic<nsecs_t> mExpectedVSyncTime;
Lloyd Pique78ce4182018-01-31 16:39:51 -080089
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080090 public:
Lloyd Pique78ce4182018-01-31 16:39:51 -080091 explicit Handler(MessageQueue& queue) : mQueue(queue), mEventMask(0) {}
Ady Abraham55fa7272020-09-30 19:19:27 -070092 void handleMessage(const Message& message) override;
93 virtual void dispatchRefresh();
94 virtual void dispatchInvalidate(int64_t vsyncId, nsecs_t expectedVSyncTimestamp);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080095 };
96
97 friend class Handler;
98
99 sp<SurfaceFlinger> mFlinger;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800100 sp<Looper> mLooper;
Ady Abraham55fa7272020-09-30 19:19:27 -0700101
102 struct Vsync {
103 frametimeline::TokenManager* tokenManager = nullptr;
104 std::unique_ptr<scheduler::VSyncCallbackRegistration> registration;
105
106 std::mutex mutex;
107 TracedOrdinal<std::chrono::nanoseconds> workDuration
108 GUARDED_BY(mutex) = {"VsyncWorkDuration-sf", std::chrono::nanoseconds(0)};
109 std::chrono::nanoseconds lastCallbackTime GUARDED_BY(mutex) = std::chrono::nanoseconds{0};
Ady Abraham326ecde2020-11-06 15:05:53 -0800110 bool mScheduled GUARDED_BY(mutex) = false;
Ady Abraham55fa7272020-09-30 19:19:27 -0700111 TracedOrdinal<int> value = {"VSYNC-sf", 0};
112 };
113
Dominik Laskowski208235c2020-12-03 15:38:51 -0800114 struct Injector {
115 gui::BitTube tube;
116 std::mutex mutex;
117 sp<EventThreadConnection> connection GUARDED_BY(mutex);
118 };
Ady Abraham55fa7272020-09-30 19:19:27 -0700119
Dominik Laskowski208235c2020-12-03 15:38:51 -0800120 Vsync mVsync;
121 Injector mInjector;
122
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800123 sp<Handler> mHandler;
124
Ady Abraham55fa7272020-09-30 19:19:27 -0700125 void vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime);
Dominik Laskowski208235c2020-12-03 15:38:51 -0800126 void injectorCallback();
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700127
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800128public:
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800129 ~MessageQueue() override = default;
130 void init(const sp<SurfaceFlinger>& flinger) override;
Ady Abraham55fa7272020-09-30 19:19:27 -0700131 void initVsync(scheduler::VSyncDispatch&, frametimeline::TokenManager&,
132 std::chrono::nanoseconds workDuration) override;
133 void setDuration(std::chrono::nanoseconds workDuration) override;
Dominik Laskowski208235c2020-12-03 15:38:51 -0800134 void setInjector(sp<EventThreadConnection>) override;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800135
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800136 void waitMessage() override;
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700137 void postMessage(sp<MessageHandler>&&) override;
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700138
139 // sends INVALIDATE message at next VSYNC
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800140 void invalidate() override;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800141
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700142 // sends REFRESH message at next VSYNC
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800143 void refresh() override;
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700144};
145
Lloyd Pique3fcdef12018-01-22 17:14:00 -0800146} // namespace impl
147} // namespace android