blob: 5e79a5c13e45bf8b1d35fc8fc29f98aa6daa558b [file] [log] [blame]
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001/*
2 * Copyright (C) 2011 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 Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Mathias Agopian841cde52012-03-01 15:44:37 -080021#define ATRACE_TAG ATRACE_TAG_GRAPHICS
22
Lloyd Pique46a46b32018-01-31 19:01:18 -080023#include <pthread.h>
24#include <sched.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080025#include <sys/types.h>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080026
Lloyd Pique46a46b32018-01-31 19:01:18 -080027#include <chrono>
28#include <cstdint>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080029#include <optional>
30#include <type_traits>
Ady Abraham011f8ba2022-11-22 15:09:07 -080031#include <utility>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080032
Yiwei Zhang5434a782018-12-05 18:06:32 -080033#include <android-base/stringprintf.h>
34
Ady Abraham0bb6a472020-10-12 10:22:13 -070035#include <binder/IPCThreadState.h>
36
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070037#include <cutils/compiler.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080038#include <cutils/sched_policy.h>
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070039
Mathias Agopiand0566bc2011-11-17 17:49:17 -080040#include <gui/DisplayEventReceiver.h>
41
42#include <utils/Errors.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080043#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044
Marin Shalamanov23c44202020-12-22 19:09:20 +010045#include "DisplayHardware/DisplayMode.h"
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070046#include "FrameTimeline.h"
Ady Abraham011f8ba2022-11-22 15:09:07 -080047#include "VSyncDispatch.h"
48#include "VSyncTracker.h"
Marin Shalamanov23c44202020-12-22 19:09:20 +010049
50#include "EventThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080051
Ady Abraham62f216c2020-10-13 19:07:23 -070052#undef LOG_TAG
53#define LOG_TAG "EventThread"
54
Lloyd Pique46a46b32018-01-31 19:01:18 -080055using namespace std::chrono_literals;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080056
Lloyd Pique46a46b32018-01-31 19:01:18 -080057namespace android {
58
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080059using base::StringAppendF;
60using base::StringPrintf;
61
62namespace {
63
64auto vsyncPeriod(VSyncRequest request) {
65 return static_cast<std::underlying_type_t<VSyncRequest>>(request);
66}
67
68std::string toString(VSyncRequest request) {
69 switch (request) {
70 case VSyncRequest::None:
71 return "VSyncRequest::None";
72 case VSyncRequest::Single:
73 return "VSyncRequest::Single";
Tim Murrayb5daa912020-09-09 21:29:13 +000074 case VSyncRequest::SingleSuppressCallback:
75 return "VSyncRequest::SingleSuppressCallback";
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080076 default:
77 return StringPrintf("VSyncRequest::Periodic{period=%d}", vsyncPeriod(request));
78 }
79}
80
81std::string toString(const EventThreadConnection& connection) {
82 return StringPrintf("Connection{%p, %s}", &connection,
83 toString(connection.vsyncRequest).c_str());
84}
85
86std::string toString(const DisplayEventReceiver::Event& event) {
87 switch (event.header.type) {
88 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
Marin Shalamanova524a092020-07-27 21:39:55 +020089 return StringPrintf("Hotplug{displayId=%s, %s}",
90 to_string(event.header.displayId).c_str(),
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080091 event.hotplug.connected ? "connected" : "disconnected");
92 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
Rachel Leeb9c5a772022-02-04 21:17:37 -080093 return StringPrintf("VSync{displayId=%s, count=%u, expectedPresentationTime=%" PRId64
94 "}",
Marin Shalamanova524a092020-07-27 21:39:55 +020095 to_string(event.header.displayId).c_str(), event.vsync.count,
Rachel Leeb9c5a772022-02-04 21:17:37 -080096 event.vsync.vsyncData.preferredExpectedPresentationTime());
Marin Shalamanova7fe3042021-01-29 21:02:08 +010097 case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE:
98 return StringPrintf("ModeChanged{displayId=%s, modeId=%u}",
99 to_string(event.header.displayId).c_str(), event.modeChange.modeId);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800100 default:
101 return "Event{}";
102 }
103}
104
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800105DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp,
106 bool connected) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800107 DisplayEventReceiver::Event event;
108 event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp};
109 event.hotplug.connected = connected;
110 return event;
111}
112
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800113DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800114 uint32_t count, nsecs_t expectedPresentationTime,
Rachel Lee0d943202022-02-01 23:29:41 -0800115 nsecs_t deadlineTimestamp) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800116 DisplayEventReceiver::Event event;
117 event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp};
118 event.vsync.count = count;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800119 event.vsync.vsyncData.preferredFrameTimelineIndex = 0;
120 // Temporarily store the current vsync information in frameTimelines[0], marked as
121 // platform-preferred. When the event is dispatched later, the frame interval at that time is
122 // used with this information to generate multiple frame timeline choices.
123 event.vsync.vsyncData.frameTimelines[0] = {.vsyncId = FrameTimelineInfo::INVALID_VSYNC_ID,
124 .deadlineTimestamp = deadlineTimestamp,
125 .expectedPresentationTime =
126 expectedPresentationTime};
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800127 return event;
128}
129
Ady Abraham67434eb2022-12-01 17:48:12 -0800130DisplayEventReceiver::Event makeModeChanged(const scheduler::FrameRateMode& mode) {
Ady Abraham447052e2019-02-13 16:07:27 -0800131 DisplayEventReceiver::Event event;
Ady Abraham67434eb2022-12-01 17:48:12 -0800132 event.header = {DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE,
133 mode.modePtr->getPhysicalDisplayId(), systemTime()};
134 event.modeChange.modeId = mode.modePtr->getId().value();
135 event.modeChange.vsyncPeriod = mode.fps.getPeriodNsecs();
Ady Abraham447052e2019-02-13 16:07:27 -0800136 return event;
137}
138
Ady Abraham62f216c2020-10-13 19:07:23 -0700139DisplayEventReceiver::Event makeFrameRateOverrideEvent(PhysicalDisplayId displayId,
140 FrameRateOverride frameRateOverride) {
141 return DisplayEventReceiver::Event{
142 .header =
143 DisplayEventReceiver::Event::Header{
144 .type = DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE,
145 .displayId = displayId,
146 .timestamp = systemTime(),
147 },
148 .frameRateOverride = frameRateOverride,
149 };
150}
151
152DisplayEventReceiver::Event makeFrameRateOverrideFlushEvent(PhysicalDisplayId displayId) {
153 return DisplayEventReceiver::Event{
154 .header = DisplayEventReceiver::Event::Header{
155 .type = DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH,
156 .displayId = displayId,
157 .timestamp = systemTime(),
158 }};
159}
160
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800161} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -0800162
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700163EventThreadConnection::EventThreadConnection(EventThread* eventThread, uid_t callingUid,
164 ResyncCallback resyncCallback,
165 EventRegistrationFlags eventRegistration)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800166 : resyncCallback(std::move(resyncCallback)),
Ady Abraham0bb6a472020-10-12 10:22:13 -0700167 mOwnerUid(callingUid),
Ady Abraham62f216c2020-10-13 19:07:23 -0700168 mEventRegistration(eventRegistration),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800169 mEventThread(eventThread),
170 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800171
172EventThreadConnection::~EventThreadConnection() {
173 // do nothing here -- clean-up will happen automatically
174 // when the main thread wakes up
175}
176
177void EventThreadConnection::onFirstRef() {
178 // NOTE: mEventThread doesn't hold a strong reference on us
Ady Abrahamd11bade2022-08-01 16:18:03 -0700179 mEventThread->registerDisplayEventConnection(sp<EventThreadConnection>::fromExisting(this));
Ana Krulec85c39af2018-12-26 17:29:57 -0800180}
181
Huihong Luo6fac5232021-11-22 16:05:23 -0800182binder::Status EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
Ady Abraham899e8cd2022-06-06 15:16:06 -0700183 std::scoped_lock lock(mLock);
184 if (mChannel.initCheck() != NO_ERROR) {
185 return binder::Status::fromStatusT(NAME_NOT_FOUND);
186 }
187
Ana Krulec85c39af2018-12-26 17:29:57 -0800188 outChannel->setReceiveFd(mChannel.moveReceiveFd());
Alec Mouri967d5d72020-08-05 12:50:03 -0700189 outChannel->setSendFd(base::unique_fd(dup(mChannel.getSendFd())));
Huihong Luo6fac5232021-11-22 16:05:23 -0800190 return binder::Status::ok();
Ana Krulec85c39af2018-12-26 17:29:57 -0800191}
192
Huihong Luo6fac5232021-11-22 16:05:23 -0800193binder::Status EventThreadConnection::setVsyncRate(int rate) {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700194 mEventThread->setVsyncRate(static_cast<uint32_t>(rate),
195 sp<EventThreadConnection>::fromExisting(this));
Huihong Luo6fac5232021-11-22 16:05:23 -0800196 return binder::Status::ok();
Ana Krulec85c39af2018-12-26 17:29:57 -0800197}
198
Huihong Luo6fac5232021-11-22 16:05:23 -0800199binder::Status EventThreadConnection::requestNextVsync() {
Rachel Leeef2e21f2022-02-01 14:51:34 -0800200 ATRACE_CALL();
Ady Abrahamd11bade2022-08-01 16:18:03 -0700201 mEventThread->requestNextVsync(sp<EventThreadConnection>::fromExisting(this));
Huihong Luo6fac5232021-11-22 16:05:23 -0800202 return binder::Status::ok();
Ana Krulec85c39af2018-12-26 17:29:57 -0800203}
204
Rachel Leeb9c5a772022-02-04 21:17:37 -0800205binder::Status EventThreadConnection::getLatestVsyncEventData(
206 ParcelableVsyncEventData* outVsyncEventData) {
Rachel Leeef2e21f2022-02-01 14:51:34 -0800207 ATRACE_CALL();
Ady Abrahamd11bade2022-08-01 16:18:03 -0700208 outVsyncEventData->vsync =
209 mEventThread->getLatestVsyncEventData(sp<EventThreadConnection>::fromExisting(this));
Rachel Leeef2e21f2022-02-01 14:51:34 -0800210 return binder::Status::ok();
211}
212
Ana Krulec85c39af2018-12-26 17:29:57 -0800213status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
Ady Abraham62f216c2020-10-13 19:07:23 -0700214 constexpr auto toStatus = [](ssize_t size) {
215 return size < 0 ? status_t(size) : status_t(NO_ERROR);
216 };
217
218 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE ||
219 event.header.type == DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH) {
220 mPendingEvents.emplace_back(event);
221 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE) {
222 return status_t(NO_ERROR);
223 }
224
225 auto size = DisplayEventReceiver::sendEvents(&mChannel, mPendingEvents.data(),
226 mPendingEvents.size());
227 mPendingEvents.clear();
228 return toStatus(size);
229 }
230
231 auto size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
232 return toStatus(size);
Ana Krulec85c39af2018-12-26 17:29:57 -0800233}
234
235// ---------------------------------------------------------------------------
236
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800237EventThread::~EventThread() = default;
238
239namespace impl {
240
Leon Scroggins III31d41412022-11-18 16:42:53 -0500241EventThread::EventThread(const char* name, std::shared_ptr<scheduler::VsyncSchedule> vsyncSchedule,
242 IEventThreadCallback& eventThreadCallback,
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700243 android::frametimeline::TokenManager* tokenManager,
Ady Abraham011f8ba2022-11-22 15:09:07 -0800244 std::chrono::nanoseconds workDuration,
245 std::chrono::nanoseconds readyDuration)
246 : mThreadName(name),
247 mVsyncTracer(base::StringPrintf("VSYNC-%s", name), 0),
248 mWorkDuration(base::StringPrintf("VsyncWorkDuration-%s", name), workDuration),
249 mReadyDuration(readyDuration),
Leon Scroggins III31d41412022-11-18 16:42:53 -0500250 mVsyncSchedule(std::move(vsyncSchedule)),
251 mVsyncRegistration(mVsyncSchedule->getDispatch(), createDispatchCallback(), name),
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700252 mTokenManager(tokenManager),
Leon Scroggins III31d41412022-11-18 16:42:53 -0500253 mEventThreadCallback(eventThreadCallback) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800254 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
255 std::unique_lock<std::mutex> lock(mMutex);
256 threadMain(lock);
257 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800258
Dominik Laskowski6505f792019-09-18 11:10:05 -0700259 pthread_setname_np(mThread.native_handle(), mThreadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800260
261 pid_t tid = pthread_gettid_np(mThread.native_handle());
262
263 // Use SCHED_FIFO to minimize jitter
264 constexpr int EVENT_THREAD_PRIORITY = 2;
265 struct sched_param param = {0};
266 param.sched_priority = EVENT_THREAD_PRIORITY;
267 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
268 ALOGE("Couldn't set SCHED_FIFO for EventThread");
269 }
270
271 set_sched_policy(tid, SP_FOREGROUND);
272}
273
274EventThread::~EventThread() {
275 {
276 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800277 mState = State::Quit;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800278 mCondition.notify_all();
279 }
280 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700281}
282
Ady Abraham9c53ee72020-07-22 21:16:18 -0700283void EventThread::setDuration(std::chrono::nanoseconds workDuration,
284 std::chrono::nanoseconds readyDuration) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800285 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800286 mWorkDuration = workDuration;
287 mReadyDuration = readyDuration;
288
289 mVsyncRegistration.update({.workDuration = mWorkDuration.get().count(),
290 .readyDuration = mReadyDuration.count(),
291 .earliestVsync = mLastVsyncCallbackTime.ns()});
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700292}
293
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700294sp<EventThreadConnection> EventThread::createEventConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700295 ResyncCallback resyncCallback, EventRegistrationFlags eventRegistration) const {
Ady Abrahamd11bade2022-08-01 16:18:03 -0700296 return sp<EventThreadConnection>::make(const_cast<EventThread*>(this),
297 IPCThreadState::self()->getCallingUid(),
298 std::move(resyncCallback), eventRegistration);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800299}
300
Ana Krulec85c39af2018-12-26 17:29:57 -0800301status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800302 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700303
304 // this should never happen
305 auto it = std::find(mDisplayEventConnections.cbegin(),
306 mDisplayEventConnections.cend(), connection);
307 if (it != mDisplayEventConnections.cend()) {
308 ALOGW("DisplayEventConnection %p already exists", connection.get());
309 mCondition.notify_all();
310 return ALREADY_EXISTS;
311 }
312
313 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800314 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800315 return NO_ERROR;
316}
317
Ana Krulec85c39af2018-12-26 17:29:57 -0800318void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700319 auto it = std::find(mDisplayEventConnections.cbegin(),
320 mDisplayEventConnections.cend(), connection);
321 if (it != mDisplayEventConnections.cend()) {
322 mDisplayEventConnections.erase(it);
323 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800324}
325
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800326void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
327 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
328 return;
329 }
330
331 std::lock_guard<std::mutex> lock(mMutex);
332
333 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
334 if (connection->vsyncRequest != request) {
335 connection->vsyncRequest = request;
336 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800337 }
338}
339
Ady Abraham8532d012019-05-08 14:50:56 -0700340void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800341 if (connection->resyncCallback) {
342 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800343 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000344
Ana Krulec7d1d6832018-12-27 11:10:09 -0800345 std::lock_guard<std::mutex> lock(mMutex);
346
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800347 if (connection->vsyncRequest == VSyncRequest::None) {
348 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800349 mCondition.notify_all();
Tim Murrayb5daa912020-09-09 21:29:13 +0000350 } else if (connection->vsyncRequest == VSyncRequest::SingleSuppressCallback) {
351 connection->vsyncRequest = VSyncRequest::Single;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800352 }
Mathias Agopian23748662011-12-05 14:33:34 -0800353}
354
Rachel Leeef2e21f2022-02-01 14:51:34 -0800355VsyncEventData EventThread::getLatestVsyncEventData(
356 const sp<EventThreadConnection>& connection) const {
Rachel Leeb5223cf2022-03-14 14:39:27 -0700357 // Resync so that the vsync is accurate with hardware. getLatestVsyncEventData is an alternate
358 // way to get vsync data (instead of posting callbacks to Choreographer).
359 if (connection->resyncCallback) {
360 connection->resyncCallback();
361 }
362
Rachel Leeef2e21f2022-02-01 14:51:34 -0800363 VsyncEventData vsyncEventData;
Leon Scroggins III31d41412022-11-18 16:42:53 -0500364 const Fps frameInterval = mEventThreadCallback.getLeaderRenderFrameRate(connection->mOwnerUid);
365 vsyncEventData.frameInterval = frameInterval.getPeriodNsecs();
Ady Abraham011f8ba2022-11-22 15:09:07 -0800366 const auto [presentTime, deadline] = [&]() -> std::pair<nsecs_t, nsecs_t> {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800367 std::lock_guard<std::mutex> lock(mMutex);
Leon Scroggins III31d41412022-11-18 16:42:53 -0500368 const auto vsyncTime = mVsyncSchedule->getTracker().nextAnticipatedVSyncTimeFrom(
Ady Abraham011f8ba2022-11-22 15:09:07 -0800369 systemTime() + mWorkDuration.get().count() + mReadyDuration.count());
370 return {vsyncTime, vsyncTime - mReadyDuration.count()};
371 }();
Leon Scroggins III31d41412022-11-18 16:42:53 -0500372 generateFrameTimeline(vsyncEventData, frameInterval.getPeriodNsecs(),
373 systemTime(SYSTEM_TIME_MONOTONIC), presentTime, deadline);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800374 return vsyncEventData;
375}
376
Mathias Agopian22ffb112012-04-10 21:04:02 -0700377void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800378 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800379 if (!mVSyncState || mVSyncState->synthetic) {
380 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700381 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800382
383 mVSyncState->synthetic = true;
384 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700385}
386
387void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800388 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800389 if (!mVSyncState || !mVSyncState->synthetic) {
390 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700391 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800392
393 mVSyncState->synthetic = false;
394 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700395}
396
Ady Abraham011f8ba2022-11-22 15:09:07 -0800397void EventThread::onVsync(nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800398 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800399 mLastVsyncCallbackTime = TimePoint::fromNs(vsyncTime);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800400
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800401 LOG_FATAL_IF(!mVSyncState);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800402 mVsyncTracer = (mVsyncTracer + 1) % 2;
403 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, wakeupTime, ++mVSyncState->count,
404 vsyncTime, readyTime));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800405 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700406}
407
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800408void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800409 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700410
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800411 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700412 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700413}
414
Ady Abraham67434eb2022-12-01 17:48:12 -0800415void EventThread::onModeChanged(const scheduler::FrameRateMode& mode) {
Ady Abraham447052e2019-02-13 16:07:27 -0800416 std::lock_guard<std::mutex> lock(mMutex);
417
Ady Abraham690f4612021-07-01 23:24:03 -0700418 mPendingEvents.push_back(makeModeChanged(mode));
Ady Abraham447052e2019-02-13 16:07:27 -0800419 mCondition.notify_all();
420}
421
Ady Abraham62f216c2020-10-13 19:07:23 -0700422void EventThread::onFrameRateOverridesChanged(PhysicalDisplayId displayId,
423 std::vector<FrameRateOverride> overrides) {
424 std::lock_guard<std::mutex> lock(mMutex);
425
426 for (auto frameRateOverride : overrides) {
427 mPendingEvents.push_back(makeFrameRateOverrideEvent(displayId, frameRateOverride));
428 }
429 mPendingEvents.push_back(makeFrameRateOverrideFlushEvent(displayId));
430
431 mCondition.notify_all();
432}
433
Alec Mouri717bcb62020-02-10 17:07:19 -0800434size_t EventThread::getEventThreadConnectionCount() {
435 std::lock_guard<std::mutex> lock(mMutex);
436 return mDisplayEventConnections.size();
437}
438
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800439void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
440 DisplayEventConsumers consumers;
441
Dominik Laskowski029cc122019-01-23 19:52:06 -0800442 while (mState != State::Quit) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800443 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700444
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800445 // Determine next event to dispatch.
446 if (!mPendingEvents.empty()) {
447 event = mPendingEvents.front();
448 mPendingEvents.pop_front();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800449
Huihong Luoab8ffef2022-08-18 13:02:26 -0700450 if (event->header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
451 if (event->hotplug.connected && !mVSyncState) {
452 mVSyncState.emplace(event->header.displayId);
453 } else if (!event->hotplug.connected && mVSyncState &&
454 mVSyncState->displayId == event->header.displayId) {
455 mVSyncState.reset();
456 }
Dominik Laskowski029cc122019-01-23 19:52:06 -0800457 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700458 }
459
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800460 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700461
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800462 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700463 auto it = mDisplayEventConnections.begin();
464 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800465 if (const auto connection = it->promote()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800466 if (event && shouldConsumeEvent(*event, connection)) {
467 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700468 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700469
Ady Abraham011f8ba2022-11-22 15:09:07 -0800470 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
471
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700472 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700473 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700474 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700475 }
476 }
477
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800478 if (!consumers.empty()) {
479 dispatchEvent(*event, consumers);
480 consumers.clear();
481 }
482
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800483 if (mVSyncState && vsyncRequested) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800484 mState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800485 } else {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800486 ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected");
Ady Abraham011f8ba2022-11-22 15:09:07 -0800487 mState = State::Idle;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800488 }
489
Ady Abraham011f8ba2022-11-22 15:09:07 -0800490 if (mState == State::VSync) {
491 const auto scheduleResult =
492 mVsyncRegistration.schedule({.workDuration = mWorkDuration.get().count(),
493 .readyDuration = mReadyDuration.count(),
494 .earliestVsync = mLastVsyncCallbackTime.ns()});
495 LOG_ALWAYS_FATAL_IF(!scheduleResult, "Error scheduling callback");
496 } else {
497 mVsyncRegistration.cancel();
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700498 }
499
Ady Abraham011f8ba2022-11-22 15:09:07 -0800500 if (!mPendingEvents.empty()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800501 continue;
502 }
503
504 // Wait for event or client registration/request.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800505 if (mState == State::Idle) {
506 mCondition.wait(lock);
507 } else {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800508 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
509 // display is off, keep feeding clients at 60 Hz.
Ady Abraham5facfb12020-04-22 15:18:31 -0700510 const std::chrono::nanoseconds timeout =
511 mState == State::SyntheticVSync ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800512 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Ady Abraham5e7371c2020-03-24 14:47:24 -0700513 if (mState == State::VSync) {
514 ALOGW("Faking VSYNC due to driver stall for thread %s", mThreadName);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700515 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800516
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800517 LOG_FATAL_IF(!mVSyncState);
Ady Abraham5facfb12020-04-22 15:18:31 -0700518 const auto now = systemTime(SYSTEM_TIME_MONOTONIC);
Ady Abraham8cb21882020-08-26 18:22:05 -0700519 const auto deadlineTimestamp = now + timeout.count();
520 const auto expectedVSyncTime = deadlineTimestamp + timeout.count();
Ady Abraham5facfb12020-04-22 15:18:31 -0700521 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, now,
Ady Abraham8cb21882020-08-26 18:22:05 -0700522 ++mVSyncState->count, expectedVSyncTime,
Rachel Lee0d943202022-02-01 23:29:41 -0800523 deadlineTimestamp));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700524 }
525 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800526 }
Ady Abraham011f8ba2022-11-22 15:09:07 -0800527 // cancel any pending vsync event before exiting
528 mVsyncRegistration.cancel();
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800529}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700530
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800531bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
532 const sp<EventThreadConnection>& connection) const {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700533 const auto throttleVsync = [&] {
Leon Scroggins III31d41412022-11-18 16:42:53 -0500534 const auto& vsyncData = event.vsync.vsyncData;
Rachel Lee2248f522023-01-27 16:45:23 -0800535 if (connection->frameRate.isValid()) {
536 return !mVsyncSchedule->getTracker()
537 .isVSyncInPhase(vsyncData.preferredExpectedPresentationTime(),
538 connection->frameRate);
539 }
540
Leon Scroggins III31d41412022-11-18 16:42:53 -0500541 const auto expectedPresentTime =
542 TimePoint::fromNs(vsyncData.preferredExpectedPresentationTime());
543 return !mEventThreadCallback.isVsyncTargetForUid(expectedPresentTime,
544 connection->mOwnerUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700545 };
546
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800547 switch (event.header.type) {
548 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
549 return true;
550
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100551 case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE: {
Ady Abraham62f216c2020-10-13 19:07:23 -0700552 return connection->mEventRegistration.test(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700553 gui::ISurfaceComposer::EventRegistration::modeChanged);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700554 }
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700555
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800556 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
557 switch (connection->vsyncRequest) {
558 case VSyncRequest::None:
559 return false;
Tim Murrayb5daa912020-09-09 21:29:13 +0000560 case VSyncRequest::SingleSuppressCallback:
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800561 connection->vsyncRequest = VSyncRequest::None;
Tim Murrayb5daa912020-09-09 21:29:13 +0000562 return false;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700563 case VSyncRequest::Single: {
564 if (throttleVsync()) {
565 return false;
566 }
Tim Murrayb5daa912020-09-09 21:29:13 +0000567 connection->vsyncRequest = VSyncRequest::SingleSuppressCallback;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800568 return true;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700569 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800570 case VSyncRequest::Periodic:
Ady Abraham0bb6a472020-10-12 10:22:13 -0700571 if (throttleVsync()) {
572 return false;
573 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800574 return true;
575 default:
Ady Abraham0bb6a472020-10-12 10:22:13 -0700576 // We don't throttle vsync if the app set a vsync request rate
577 // since there is no easy way to do that and this is a very
578 // rare case
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800579 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
580 }
581
Ady Abraham62f216c2020-10-13 19:07:23 -0700582 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE:
583 [[fallthrough]];
584 case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH:
585 return connection->mEventRegistration.test(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700586 gui::ISurfaceComposer::EventRegistration::frameRateOverride);
Ady Abraham62f216c2020-10-13 19:07:23 -0700587
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800588 default:
589 return false;
590 }
591}
592
Rachel Lee8d0c6102021-11-03 22:00:25 +0000593int64_t EventThread::generateToken(nsecs_t timestamp, nsecs_t deadlineTimestamp,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800594 nsecs_t expectedPresentationTime) const {
Rachel Lee3f028662021-11-04 19:32:24 +0000595 if (mTokenManager != nullptr) {
596 return mTokenManager->generateTokenForPredictions(
Rachel Leeb9c5a772022-02-04 21:17:37 -0800597 {timestamp, deadlineTimestamp, expectedPresentationTime});
Rachel Lee3f028662021-11-04 19:32:24 +0000598 }
599 return FrameTimelineInfo::INVALID_VSYNC_ID;
600}
601
Rachel Leeb9c5a772022-02-04 21:17:37 -0800602void EventThread::generateFrameTimeline(VsyncEventData& outVsyncEventData, nsecs_t frameInterval,
603 nsecs_t timestamp,
604 nsecs_t preferredExpectedPresentationTime,
605 nsecs_t preferredDeadlineTimestamp) const {
Rachel Lee3f028662021-11-04 19:32:24 +0000606 // Add 1 to ensure the preferredFrameTimelineIndex entry (when multiplier == 0) is included.
Rachel Leeef2e21f2022-02-01 14:51:34 -0800607 for (int64_t multiplier = -VsyncEventData::kFrameTimelinesLength + 1, currentIndex = 0;
Rachel Lee18c34372022-01-20 13:57:18 -0800608 currentIndex < VsyncEventData::kFrameTimelinesLength; multiplier++) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800609 nsecs_t deadlineTimestamp = preferredDeadlineTimestamp + multiplier * frameInterval;
Rachel Lee3f028662021-11-04 19:32:24 +0000610 // Valid possible frame timelines must have future values.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800611 if (deadlineTimestamp > timestamp) {
Rachel Lee3f028662021-11-04 19:32:24 +0000612 if (multiplier == 0) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800613 outVsyncEventData.preferredFrameTimelineIndex = currentIndex;
Rachel Lee3f028662021-11-04 19:32:24 +0000614 }
Rachel Leeb9c5a772022-02-04 21:17:37 -0800615 nsecs_t expectedPresentationTime =
616 preferredExpectedPresentationTime + multiplier * frameInterval;
617 outVsyncEventData.frameTimelines[currentIndex] =
618 {.vsyncId =
619 generateToken(timestamp, deadlineTimestamp, expectedPresentationTime),
620 .deadlineTimestamp = deadlineTimestamp,
621 .expectedPresentationTime = expectedPresentationTime};
Rachel Lee3f028662021-11-04 19:32:24 +0000622 currentIndex++;
623 }
624 }
625}
626
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800627void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
628 const DisplayEventConsumers& consumers) {
629 for (const auto& consumer : consumers) {
Jorim Jaggic0086af2021-02-12 18:18:11 +0100630 DisplayEventReceiver::Event copy = event;
631 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
Leon Scroggins III31d41412022-11-18 16:42:53 -0500632 const Fps frameInterval =
633 mEventThreadCallback.getLeaderRenderFrameRate(consumer->mOwnerUid);
634 copy.vsync.vsyncData.frameInterval = frameInterval.getPeriodNsecs();
635 generateFrameTimeline(copy.vsync.vsyncData, frameInterval.getPeriodNsecs(),
636 copy.header.timestamp,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800637 event.vsync.vsyncData.preferredExpectedPresentationTime(),
638 event.vsync.vsyncData.preferredDeadlineTimestamp());
Jorim Jaggic0086af2021-02-12 18:18:11 +0100639 }
640 switch (consumer->postEvent(copy)) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800641 case NO_ERROR:
642 break;
643
644 case -EAGAIN:
645 // TODO: Try again if pipe is full.
646 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
647 toString(*consumer).c_str());
648 break;
649
650 default:
651 // Treat EPIPE and other errors as fatal.
652 removeDisplayEventConnectionLocked(consumer);
653 }
654 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700655}
656
Yiwei Zhang5434a782018-12-05 18:06:32 -0800657void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800658 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800659
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800660 StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
661 if (mVSyncState) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200662 StringAppendF(&result, "{displayId=%s, count=%u%s}\n",
663 to_string(mVSyncState->displayId).c_str(), mVSyncState->count,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800664 mVSyncState->synthetic ? ", synthetic" : "");
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800665 } else {
666 StringAppendF(&result, "none\n");
667 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800668
Ady Abraham011f8ba2022-11-22 15:09:07 -0800669 const auto relativeLastCallTime =
670 ticks<std::milli, float>(mLastVsyncCallbackTime - TimePoint::now());
671 StringAppendF(&result, "mWorkDuration=%.2f mReadyDuration=%.2f last vsync time ",
672 mWorkDuration.get().count() / 1e6f, mReadyDuration.count() / 1e6f);
673 StringAppendF(&result, "%.2fms relative to now\n", relativeLastCallTime);
674
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800675 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
676 for (const auto& event : mPendingEvents) {
677 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700678 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800679
680 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
681 for (const auto& ptr : mDisplayEventConnections) {
682 if (const auto connection = ptr.promote()) {
683 StringAppendF(&result, " %s\n", toString(*connection).c_str());
684 }
685 }
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400686 result += '\n';
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800687}
688
Dominik Laskowski029cc122019-01-23 19:52:06 -0800689const char* EventThread::toCString(State state) {
690 switch (state) {
691 case State::Idle:
692 return "Idle";
693 case State::Quit:
694 return "Quit";
695 case State::SyntheticVSync:
696 return "SyntheticVSync";
697 case State::VSync:
698 return "VSync";
699 }
700}
701
Leon Scroggins III31d41412022-11-18 16:42:53 -0500702void EventThread::onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule> schedule) {
703 std::lock_guard<std::mutex> lock(mMutex);
704 const bool reschedule = mVsyncRegistration.cancel() == scheduler::CancelResult::Cancelled;
705 mVsyncSchedule = std::move(schedule);
706 mVsyncRegistration =
707 scheduler::VSyncCallbackRegistration(mVsyncSchedule->getDispatch(),
708 createDispatchCallback(), mThreadName);
709 if (reschedule) {
710 mVsyncRegistration.schedule({.workDuration = mWorkDuration.get().count(),
711 .readyDuration = mReadyDuration.count(),
712 .earliestVsync = mLastVsyncCallbackTime.ns()});
713 }
714}
715
716scheduler::VSyncDispatch::Callback EventThread::createDispatchCallback() {
717 return [this](nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime) {
718 onVsync(vsyncTime, wakeupTime, readyTime);
719 };
720}
721
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800722} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800723} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800724
725// TODO(b/129481165): remove the #pragma below and fix conversion issues
726#pragma clang diagnostic pop // ignored "-Wconversion"