blob: ae10ff4989967f6e3d7d49d02a4d28f687a7e018 [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
Ady Abraham55fa7272020-09-30 19:19:27 -070017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080018
Mathias Agopian8aedd472012-01-24 16:39:14 -080019#include <binder/IPCThreadState.h>
20
Mathias Agopianf1d8e872009-04-20 19:39:12 -070021#include <utils/Log.h>
Lloyd Pique78ce4182018-01-31 16:39:51 -080022#include <utils/Timers.h>
23#include <utils/threads.h>
Mathias Agopian8aedd472012-01-24 16:39:14 -080024
Lloyd Pique3fcdef12018-01-22 17:14:00 -080025#include <gui/DisplayEventReceiver.h>
Mathias Agopianf1d8e872009-04-20 19:39:12 -070026
Mathias Agopian8aedd472012-01-24 16:39:14 -080027#include "EventThread.h"
Ady Abraham55fa7272020-09-30 19:19:27 -070028#include "FrameTimeline.h"
Lloyd Pique78ce4182018-01-31 16:39:51 -080029#include "MessageQueue.h"
Mathias Agopianf1d8e872009-04-20 19:39:12 -070030
Dominik Laskowskidd4ef272020-04-23 14:02:12 -070031namespace android::impl {
Lloyd Pique3fcdef12018-01-22 17:14:00 -080032
Dominik Laskowski08fbd852022-07-14 08:53:42 -070033void MessageQueue::Handler::dispatchFrame(VsyncId vsyncId, TimePoint expectedVsyncTime) {
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070034 if (!mFramePending.exchange(true)) {
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070035 mVsyncId = vsyncId;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070036 mExpectedVsyncTime = expectedVsyncTime;
Ady Abrahamd11bade2022-08-01 16:18:03 -070037 mQueue.mLooper->sendMessage(sp<MessageHandler>::fromExisting(this), Message());
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080038 }
39}
40
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070041bool MessageQueue::Handler::isFramePending() const {
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070042 return mFramePending.load();
Ady Abraham562c2712021-05-07 15:10:42 -070043}
44
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -070045void MessageQueue::Handler::handleMessage(const Message&) {
46 mFramePending.store(false);
Dominik Laskowski08fbd852022-07-14 08:53:42 -070047 mQueue.onFrameSignal(mQueue.mCompositor, mVsyncId, mExpectedVsyncTime);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080048}
49
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070050MessageQueue::MessageQueue(ICompositor& compositor)
51 : MessageQueue(compositor, sp<Handler>::make(*this)) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080052
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070053constexpr bool kAllowNonCallbacks = true;
54
55MessageQueue::MessageQueue(ICompositor& compositor, sp<Handler> handler)
56 : mCompositor(compositor),
57 mLooper(sp<Looper>::make(kAllowNonCallbacks)),
58 mHandler(std::move(handler)) {}
Mathias Agopian99ce5cd2012-01-31 18:24:27 -080059
Ady Abraham55fa7272020-09-30 19:19:27 -070060void MessageQueue::vsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime) {
61 ATRACE_CALL();
62 // Trace VSYNC-sf
63 mVsync.value = (mVsync.value + 1) % 2;
64
Dominik Laskowski08fbd852022-07-14 08:53:42 -070065 const auto expectedVsyncTime = TimePoint::fromNs(vsyncTime);
Ady Abraham55fa7272020-09-30 19:19:27 -070066 {
67 std::lock_guard lock(mVsync.mutex);
Dominik Laskowski08fbd852022-07-14 08:53:42 -070068 mVsync.lastCallbackTime = expectedVsyncTime;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070069 mVsync.scheduledFrameTime.reset();
Ady Abraham55fa7272020-09-30 19:19:27 -070070 }
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070071
Dominik Laskowski08fbd852022-07-14 08:53:42 -070072 const auto vsyncId = VsyncId{mVsync.tokenManager->generateTokenForPredictions(
73 {targetWakeupTime, readyTime, vsyncTime})};
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070074
Dominik Laskowski08fbd852022-07-14 08:53:42 -070075 mHandler->dispatchFrame(vsyncId, expectedVsyncTime);
Ady Abraham55fa7272020-09-30 19:19:27 -070076}
77
78void MessageQueue::initVsync(scheduler::VSyncDispatch& dispatch,
79 frametimeline::TokenManager& tokenManager,
80 std::chrono::nanoseconds workDuration) {
81 setDuration(workDuration);
82 mVsync.tokenManager = &tokenManager;
83 mVsync.registration = std::make_unique<
84 scheduler::VSyncCallbackRegistration>(dispatch,
85 std::bind(&MessageQueue::vsyncCallback, this,
86 std::placeholders::_1,
87 std::placeholders::_2,
88 std::placeholders::_3),
89 "sf");
90}
91
92void MessageQueue::setDuration(std::chrono::nanoseconds workDuration) {
93 ATRACE_CALL();
94 std::lock_guard lock(mVsync.mutex);
95 mVsync.workDuration = workDuration;
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -070096 if (mVsync.scheduledFrameTime) {
Dominik Laskowski08fbd852022-07-14 08:53:42 -070097 mVsync.scheduledFrameTime =
98 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
99 .readyDuration = 0,
100 .earliestVsync = mVsync.lastCallbackTime.ns()});
Ady Abraham326ecde2020-11-06 15:05:53 -0800101 }
Ana Krulec98b5b242018-08-10 15:03:23 -0700102}
103
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800104void MessageQueue::waitMessage() {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700105 do {
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800106 IPCThreadState::self()->flushCommands();
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800107 int32_t ret = mLooper->pollOnce(-1);
108 switch (ret) {
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800109 case Looper::POLL_WAKE:
110 case Looper::POLL_CALLBACK:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800111 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800112 case Looper::POLL_ERROR:
113 ALOGE("Looper::POLL_ERROR");
Pablo Ceballos53390e12015-08-04 11:25:59 -0700114 continue;
Brian Carlstromfe761ab2013-12-12 23:13:18 -0800115 case Looper::POLL_TIMEOUT:
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800116 // timeout (should not happen)
117 continue;
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800118 default:
119 // should not happen
Steve Blocke6f43dd2012-01-06 19:20:56 +0000120 ALOGE("Looper::pollOnce() returned unknown status %d", ret);
Mathias Agopianf61c57f2011-11-23 16:49:10 -0800121 continue;
122 }
123 } while (true);
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700124}
125
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700126void MessageQueue::postMessage(sp<MessageHandler>&& handler) {
127 mLooper->sendMessage(handler, Message());
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700128}
129
Dominik Laskowskif11728a2022-07-28 13:07:42 -0700130void MessageQueue::scheduleConfigure() {
131 struct ConfigureHandler : MessageHandler {
132 explicit ConfigureHandler(ICompositor& compositor) : compositor(compositor) {}
133
134 void handleMessage(const Message&) override { compositor.configure(); }
135
136 ICompositor& compositor;
137 };
138
139 // TODO(b/241285876): Batch configure tasks that happen within some duration.
140 postMessage(sp<ConfigureHandler>::make(mCompositor));
141}
142
Dominik Laskowski46f3e3b2021-08-10 11:44:24 -0700143void MessageQueue::scheduleFrame() {
Ady Abraham55fa7272020-09-30 19:19:27 -0700144 ATRACE_CALL();
Dominik Laskowski208235c2020-12-03 15:38:51 -0800145
Dominik Laskowski208235c2020-12-03 15:38:51 -0800146 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700147 mVsync.scheduledFrameTime =
Ady Abraham562c2712021-05-07 15:10:42 -0700148 mVsync.registration->schedule({.workDuration = mVsync.workDuration.get().count(),
149 .readyDuration = 0,
Dominik Laskowski08fbd852022-07-14 08:53:42 -0700150 .earliestVsync = mVsync.lastCallbackTime.ns()});
Mathias Agopian8aedd472012-01-24 16:39:14 -0800151}
152
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700153auto MessageQueue::getScheduledFrameTime() const -> std::optional<Clock::time_point> {
154 if (mHandler->isFramePending()) {
155 return Clock::now();
Ady Abraham562c2712021-05-07 15:10:42 -0700156 }
157
158 std::lock_guard lock(mVsync.mutex);
Dominik Laskowskie0e0cde2021-07-30 10:42:05 -0700159 if (const auto time = mVsync.scheduledFrameTime) {
160 return Clock::time_point(std::chrono::nanoseconds(*time));
Ady Abraham562c2712021-05-07 15:10:42 -0700161 }
162
163 return std::nullopt;
164}
165
Dominik Laskowskidd4ef272020-04-23 14:02:12 -0700166} // namespace android::impl