1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
|
/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#include "Scheduler.h"
#include <algorithm>
#include <cinttypes>
#include <cstdint>
#include <memory>
#include <numeric>
#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
#include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
#include <configstore/Utils.h>
#include <cutils/properties.h>
#include <input/InputWindow.h>
#include <system/window.h>
#include <ui/DisplayStatInfo.h>
#include <utils/Timers.h>
#include <utils/Trace.h>
#include "DispSync.h"
#include "DispSyncSource.h"
#include "EventControlThread.h"
#include "EventThread.h"
#include "IdleTimer.h"
#include "InjectVSyncSource.h"
#include "LayerInfo.h"
#include "SchedulerUtils.h"
#include "SurfaceFlingerProperties.h"
namespace android {
using namespace android::hardware::configstore;
using namespace android::hardware::configstore::V1_0;
using namespace android::sysprop;
#define RETURN_VALUE_IF_INVALID(value) \
if (handle == nullptr || mConnections.count(handle->id) == 0) return value
#define RETURN_IF_INVALID() \
if (handle == nullptr || mConnections.count(handle->id) == 0) return
std::atomic<int64_t> Scheduler::sNextId = 0;
Scheduler::Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function,
const scheduler::RefreshRateConfigs& refreshRateConfig)
: mHasSyncFramework(running_without_sync_framework(true)),
mDispSyncPresentTimeOffset(present_time_offset_from_vsync_ns(0)),
mPrimaryHWVsyncEnabled(false),
mHWVsyncAvailable(false),
mRefreshRateConfigs(refreshRateConfig) {
// Note: We create a local temporary with the real DispSync implementation
// type temporarily so we can initialize it with the configured values,
// before storing it for more generic use using the interface type.
auto primaryDispSync = std::make_unique<impl::DispSync>("SchedulerDispSync");
primaryDispSync->init(mHasSyncFramework, mDispSyncPresentTimeOffset);
mPrimaryDispSync = std::move(primaryDispSync);
mEventControlThread = std::make_unique<impl::EventControlThread>(function);
mSetIdleTimerMs = set_idle_timer_ms(0);
mSupportKernelTimer = support_kernel_idle_timer(false);
mSetTouchTimerMs = set_touch_timer_ms(0);
mSetDisplayPowerTimerMs = set_display_power_timer_ms(0);
char value[PROPERTY_VALUE_MAX];
property_get("debug.sf.set_idle_timer_ms", value, "0");
int int_value = atoi(value);
if (int_value) {
mSetIdleTimerMs = atoi(value);
}
if (mSetIdleTimerMs > 0) {
if (mSupportKernelTimer) {
mIdleTimer =
std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(
mSetIdleTimerMs),
[this] { resetKernelTimerCallback(); },
[this] {
expiredKernelTimerCallback();
});
} else {
mIdleTimer = std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(
mSetIdleTimerMs),
[this] { resetTimerCallback(); },
[this] { expiredTimerCallback(); });
}
mIdleTimer->start();
}
if (mSetTouchTimerMs > 0) {
// Touch events are coming to SF every 100ms, so the timer needs to be higher than that
mTouchTimer =
std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(mSetTouchTimerMs),
[this] { resetTouchTimerCallback(); },
[this] { expiredTouchTimerCallback(); });
mTouchTimer->start();
}
if (mSetDisplayPowerTimerMs > 0) {
mDisplayPowerTimer =
std::make_unique<scheduler::IdleTimer>(std::chrono::milliseconds(
mSetDisplayPowerTimerMs),
[this] { resetDisplayPowerTimerCallback(); },
[this] {
expiredDisplayPowerTimerCallback();
});
mDisplayPowerTimer->start();
}
}
Scheduler::~Scheduler() {
// Ensure the IdleTimer thread is joined before we start destroying state.
mDisplayPowerTimer.reset();
mTouchTimer.reset();
mIdleTimer.reset();
}
sp<Scheduler::ConnectionHandle> Scheduler::createConnection(
const char* connectionName, nsecs_t phaseOffsetNs, nsecs_t offsetThresholdForNextVsync,
ResyncCallback resyncCallback,
impl::EventThread::InterceptVSyncsCallback interceptCallback) {
const int64_t id = sNextId++;
ALOGV("Creating a connection handle with ID: %" PRId64 "\n", id);
std::unique_ptr<EventThread> eventThread =
makeEventThread(connectionName, mPrimaryDispSync.get(), phaseOffsetNs,
offsetThresholdForNextVsync, std::move(interceptCallback));
auto eventThreadConnection =
createConnectionInternal(eventThread.get(), std::move(resyncCallback),
ISurfaceComposer::eConfigChangedSuppress);
mConnections.emplace(id,
std::make_unique<Connection>(new ConnectionHandle(id),
eventThreadConnection,
std::move(eventThread)));
return mConnections[id]->handle;
}
std::unique_ptr<EventThread> Scheduler::makeEventThread(
const char* connectionName, DispSync* dispSync, nsecs_t phaseOffsetNs,
nsecs_t offsetThresholdForNextVsync,
impl::EventThread::InterceptVSyncsCallback interceptCallback) {
std::unique_ptr<VSyncSource> eventThreadSource =
std::make_unique<DispSyncSource>(dispSync, phaseOffsetNs, offsetThresholdForNextVsync,
true, connectionName);
return std::make_unique<impl::EventThread>(std::move(eventThreadSource),
std::move(interceptCallback), connectionName);
}
sp<EventThreadConnection> Scheduler::createConnectionInternal(
EventThread* eventThread, ResyncCallback&& resyncCallback,
ISurfaceComposer::ConfigChanged configChanged) {
return eventThread->createEventConnection(std::move(resyncCallback), configChanged);
}
sp<IDisplayEventConnection> Scheduler::createDisplayEventConnection(
const sp<Scheduler::ConnectionHandle>& handle, ResyncCallback resyncCallback,
ISurfaceComposer::ConfigChanged configChanged) {
RETURN_VALUE_IF_INVALID(nullptr);
return createConnectionInternal(mConnections[handle->id]->thread.get(),
std::move(resyncCallback), configChanged);
}
EventThread* Scheduler::getEventThread(const sp<Scheduler::ConnectionHandle>& handle) {
RETURN_VALUE_IF_INVALID(nullptr);
return mConnections[handle->id]->thread.get();
}
sp<EventThreadConnection> Scheduler::getEventConnection(const sp<ConnectionHandle>& handle) {
RETURN_VALUE_IF_INVALID(nullptr);
return mConnections[handle->id]->eventConnection;
}
void Scheduler::hotplugReceived(const sp<Scheduler::ConnectionHandle>& handle,
PhysicalDisplayId displayId, bool connected) {
RETURN_IF_INVALID();
mConnections[handle->id]->thread->onHotplugReceived(displayId, connected);
}
void Scheduler::onScreenAcquired(const sp<Scheduler::ConnectionHandle>& handle) {
RETURN_IF_INVALID();
mConnections[handle->id]->thread->onScreenAcquired();
}
void Scheduler::onScreenReleased(const sp<Scheduler::ConnectionHandle>& handle) {
RETURN_IF_INVALID();
mConnections[handle->id]->thread->onScreenReleased();
}
void Scheduler::onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
int32_t configId) {
RETURN_IF_INVALID();
mConnections[handle->id]->thread->onConfigChanged(displayId, configId);
}
void Scheduler::dump(const sp<Scheduler::ConnectionHandle>& handle, std::string& result) const {
RETURN_IF_INVALID();
mConnections.at(handle->id)->thread->dump(result);
}
void Scheduler::setPhaseOffset(const sp<Scheduler::ConnectionHandle>& handle, nsecs_t phaseOffset) {
RETURN_IF_INVALID();
mConnections[handle->id]->thread->setPhaseOffset(phaseOffset);
}
void Scheduler::getDisplayStatInfo(DisplayStatInfo* stats) {
stats->vsyncTime = mPrimaryDispSync->computeNextRefresh(0);
stats->vsyncPeriod = mPrimaryDispSync->getPeriod();
}
void Scheduler::enableHardwareVsync() {
std::lock_guard<std::mutex> lock(mHWVsyncLock);
if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
mPrimaryDispSync->beginResync();
mEventControlThread->setVsyncEnabled(true);
mPrimaryHWVsyncEnabled = true;
}
}
void Scheduler::disableHardwareVsync(bool makeUnavailable) {
std::lock_guard<std::mutex> lock(mHWVsyncLock);
if (mPrimaryHWVsyncEnabled) {
mEventControlThread->setVsyncEnabled(false);
mPrimaryDispSync->endResync();
mPrimaryHWVsyncEnabled = false;
}
if (makeUnavailable) {
mHWVsyncAvailable = false;
}
}
void Scheduler::resyncToHardwareVsync(bool makeAvailable, nsecs_t period) {
{
std::lock_guard<std::mutex> lock(mHWVsyncLock);
if (makeAvailable) {
mHWVsyncAvailable = makeAvailable;
} else if (!mHWVsyncAvailable) {
// Hardware vsync is not currently available, so abort the resync
// attempt for now
return;
}
}
if (period <= 0) {
return;
}
setVsyncPeriod(period);
}
ResyncCallback Scheduler::makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod) {
std::weak_ptr<VsyncState> ptr = mPrimaryVsyncState;
return [ptr, getVsyncPeriod = std::move(getVsyncPeriod)]() {
if (const auto vsync = ptr.lock()) {
vsync->resync(getVsyncPeriod);
}
};
}
void Scheduler::VsyncState::resync(const GetVsyncPeriod& getVsyncPeriod) {
static constexpr nsecs_t kIgnoreDelay = ms2ns(750);
const nsecs_t now = systemTime();
const nsecs_t last = lastResyncTime.exchange(now);
if (now - last > kIgnoreDelay) {
scheduler.resyncToHardwareVsync(false, getVsyncPeriod());
}
}
void Scheduler::setRefreshSkipCount(int count) {
mPrimaryDispSync->setRefreshSkipCount(count);
}
void Scheduler::setVsyncPeriod(const nsecs_t period) {
std::lock_guard<std::mutex> lock(mHWVsyncLock);
mPrimaryDispSync->setPeriod(period);
if (!mPrimaryHWVsyncEnabled) {
mPrimaryDispSync->beginResync();
mEventControlThread->setVsyncEnabled(true);
mPrimaryHWVsyncEnabled = true;
}
}
void Scheduler::addResyncSample(const nsecs_t timestamp, bool* periodFlushed) {
bool needsHwVsync = false;
*periodFlushed = false;
{ // Scope for the lock
std::lock_guard<std::mutex> lock(mHWVsyncLock);
if (mPrimaryHWVsyncEnabled) {
needsHwVsync = mPrimaryDispSync->addResyncSample(timestamp, periodFlushed);
}
}
if (needsHwVsync) {
enableHardwareVsync();
} else {
disableHardwareVsync(false);
}
}
void Scheduler::addPresentFence(const std::shared_ptr<FenceTime>& fenceTime) {
if (mPrimaryDispSync->addPresentFence(fenceTime)) {
enableHardwareVsync();
} else {
disableHardwareVsync(false);
}
}
void Scheduler::setIgnorePresentFences(bool ignore) {
mPrimaryDispSync->setIgnorePresentFences(ignore);
}
nsecs_t Scheduler::getDispSyncExpectedPresentTime() {
return mPrimaryDispSync->expectedPresentTime();
}
void Scheduler::dumpPrimaryDispSync(std::string& result) const {
mPrimaryDispSync->dump(result);
}
std::unique_ptr<scheduler::LayerHistory::LayerHandle> Scheduler::registerLayer(
std::string const& name, int windowType) {
RefreshRateType refreshRateType = (windowType == InputWindowInfo::TYPE_WALLPAPER)
? RefreshRateType::DEFAULT
: RefreshRateType::PERFORMANCE;
const auto refreshRate = mRefreshRateConfigs.getRefreshRate(refreshRateType);
const uint32_t performanceFps = (refreshRate) ? refreshRate->fps : 0;
const auto defaultRefreshRate = mRefreshRateConfigs.getRefreshRate(RefreshRateType::DEFAULT);
const uint32_t defaultFps = (defaultRefreshRate) ? defaultRefreshRate->fps : 0;
return mLayerHistory.createLayer(name, defaultFps, performanceFps);
}
void Scheduler::addLayerPresentTimeAndHDR(
const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
nsecs_t presentTime, bool isHDR) {
mLayerHistory.insert(layerHandle, presentTime, isHDR);
}
void Scheduler::setLayerVisibility(
const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, bool visible) {
mLayerHistory.setVisibility(layerHandle, visible);
}
void Scheduler::withPrimaryDispSync(std::function<void(DispSync&)> const& fn) {
fn(*mPrimaryDispSync);
}
void Scheduler::updateFpsBasedOnContent() {
auto [refreshRate, isHDR] = mLayerHistory.getDesiredRefreshRateAndHDR();
const uint32_t refreshRateRound = std::round(refreshRate);
RefreshRateType newRefreshRateType;
{
std::lock_guard<std::mutex> lock(mFeatureStateLock);
if (mContentRefreshRate == refreshRateRound && mIsHDRContent == isHDR) {
return;
}
mContentRefreshRate = refreshRateRound;
ATRACE_INT("ContentFPS", mContentRefreshRate);
mIsHDRContent = isHDR;
ATRACE_INT("ContentHDR", mIsHDRContent);
mCurrentContentFeatureState = refreshRateRound > 0
? ContentFeatureState::CONTENT_DETECTION_ON
: ContentFeatureState::CONTENT_DETECTION_OFF;
newRefreshRateType = calculateRefreshRateType();
if (mRefreshRateType == newRefreshRateType) {
return;
}
mRefreshRateType = newRefreshRateType;
}
changeRefreshRate(newRefreshRateType, ConfigEvent::Changed);
}
void Scheduler::setChangeRefreshRateCallback(
const ChangeRefreshRateCallback&& changeRefreshRateCallback) {
std::lock_guard<std::mutex> lock(mCallbackLock);
mChangeRefreshRateCallback = changeRefreshRateCallback;
}
void Scheduler::setGetCurrentRefreshRateTypeCallback(
const GetCurrentRefreshRateTypeCallback&& getCurrentRefreshRateTypeCallback) {
std::lock_guard<std::mutex> lock(mCallbackLock);
mGetCurrentRefreshRateTypeCallback = getCurrentRefreshRateTypeCallback;
}
void Scheduler::setGetVsyncPeriodCallback(const GetVsyncPeriod&& getVsyncPeriod) {
std::lock_guard<std::mutex> lock(mCallbackLock);
mGetVsyncPeriod = getVsyncPeriod;
}
void Scheduler::updateFrameSkipping(const int64_t skipCount) {
ATRACE_INT("FrameSkipCount", skipCount);
if (mSkipCount != skipCount) {
// Only update DispSync if it hasn't been updated yet.
mPrimaryDispSync->setRefreshSkipCount(skipCount);
mSkipCount = skipCount;
}
}
void Scheduler::resetIdleTimer() {
if (mIdleTimer) {
mIdleTimer->reset();
}
}
void Scheduler::notifyTouchEvent() {
if (mTouchTimer) {
mTouchTimer->reset();
}
if (mSupportKernelTimer) {
resetIdleTimer();
}
// Touch event will boost the refresh rate to performance.
// Clear Layer History to get fresh FPS detection
mLayerHistory.clearHistory();
}
void Scheduler::setDisplayPowerState(bool normal) {
{
std::lock_guard<std::mutex> lock(mFeatureStateLock);
mIsDisplayPowerStateNormal = normal;
}
if (mDisplayPowerTimer) {
mDisplayPowerTimer->reset();
}
// Display Power event will boost the refresh rate to performance.
// Clear Layer History to get fresh FPS detection
mLayerHistory.clearHistory();
}
void Scheduler::resetTimerCallback() {
handleTimerStateChanged(&mCurrentIdleTimerState, IdleTimerState::RESET, false);
ATRACE_INT("ExpiredIdleTimer", 0);
}
void Scheduler::resetKernelTimerCallback() {
ATRACE_INT("ExpiredKernelIdleTimer", 0);
std::lock_guard<std::mutex> lock(mCallbackLock);
if (mGetVsyncPeriod && mGetCurrentRefreshRateTypeCallback) {
// If we're not in performance mode then the kernel timer shouldn't do
// anything, as the refresh rate during DPU power collapse will be the
// same.
if (mGetCurrentRefreshRateTypeCallback() == Scheduler::RefreshRateType::PERFORMANCE) {
resyncToHardwareVsync(true, mGetVsyncPeriod());
}
}
}
void Scheduler::expiredTimerCallback() {
handleTimerStateChanged(&mCurrentIdleTimerState, IdleTimerState::EXPIRED, false);
ATRACE_INT("ExpiredIdleTimer", 1);
}
void Scheduler::resetTouchTimerCallback() {
handleTimerStateChanged(&mCurrentTouchState, TouchState::ACTIVE, true);
ATRACE_INT("TouchState", 1);
}
void Scheduler::expiredTouchTimerCallback() {
handleTimerStateChanged(&mCurrentTouchState, TouchState::INACTIVE, true);
ATRACE_INT("TouchState", 0);
}
void Scheduler::resetDisplayPowerTimerCallback() {
handleTimerStateChanged(&mDisplayPowerTimerState, DisplayPowerTimerState::RESET, true);
ATRACE_INT("ExpiredDisplayPowerTimer", 0);
}
void Scheduler::expiredDisplayPowerTimerCallback() {
handleTimerStateChanged(&mDisplayPowerTimerState, DisplayPowerTimerState::EXPIRED, true);
ATRACE_INT("ExpiredDisplayPowerTimer", 1);
}
void Scheduler::expiredKernelTimerCallback() {
std::lock_guard<std::mutex> lock(mCallbackLock);
ATRACE_INT("ExpiredKernelIdleTimer", 1);
if (mGetCurrentRefreshRateTypeCallback) {
if (mGetCurrentRefreshRateTypeCallback() != Scheduler::RefreshRateType::PERFORMANCE) {
// Disable HW Vsync if the timer expired, as we don't need it
// enabled if we're not pushing frames, and if we're in PERFORMANCE
// mode then we'll need to re-update the DispSync model anyways.
disableHardwareVsync(false);
}
}
}
std::string Scheduler::doDump() {
std::ostringstream stream;
stream << "+ Idle timer interval: " << mSetIdleTimerMs << " ms" << std::endl;
stream << "+ Touch timer interval: " << mSetTouchTimerMs << " ms" << std::endl;
return stream.str();
}
template <class T>
void Scheduler::handleTimerStateChanged(T* currentState, T newState, bool eventOnContentDetection) {
ConfigEvent event = ConfigEvent::None;
RefreshRateType newRefreshRateType;
{
std::lock_guard<std::mutex> lock(mFeatureStateLock);
if (*currentState == newState) {
return;
}
*currentState = newState;
newRefreshRateType = calculateRefreshRateType();
if (mRefreshRateType == newRefreshRateType) {
return;
}
mRefreshRateType = newRefreshRateType;
if (eventOnContentDetection &&
mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_ON) {
event = ConfigEvent::Changed;
}
}
changeRefreshRate(newRefreshRateType, event);
}
Scheduler::RefreshRateType Scheduler::calculateRefreshRateType() {
// HDR content is not supported on PERFORMANCE mode
if (mForceHDRContentToDefaultRefreshRate && mIsHDRContent) {
return RefreshRateType::DEFAULT;
}
// If Display Power is not in normal operation we want to be in performance mode.
// When coming back to normal mode, a grace period is given with DisplayPowerTimer
if (!mIsDisplayPowerStateNormal || mDisplayPowerTimerState == DisplayPowerTimerState::RESET) {
return RefreshRateType::PERFORMANCE;
}
// As long as touch is active we want to be in performance mode
if (mCurrentTouchState == TouchState::ACTIVE) {
return RefreshRateType::PERFORMANCE;
}
// If timer has expired as it means there is no new content on the screen
if (mCurrentIdleTimerState == IdleTimerState::EXPIRED) {
return RefreshRateType::DEFAULT;
}
// If content detection is off we choose performance as we don't know the content fps
if (mCurrentContentFeatureState == ContentFeatureState::CONTENT_DETECTION_OFF) {
return RefreshRateType::PERFORMANCE;
}
// Content detection is on, find the appropriate refresh rate with minimal error
auto begin = mRefreshRateConfigs.getRefreshRates().cbegin();
// Skip POWER_SAVING config as it is not a real config
if (begin->first == RefreshRateType::POWER_SAVING) {
++begin;
}
auto iter = min_element(begin, mRefreshRateConfigs.getRefreshRates().cend(),
[rate = mContentRefreshRate](const auto& l, const auto& r) -> bool {
return std::abs(l.second->fps - static_cast<float>(rate)) <
std::abs(r.second->fps - static_cast<float>(rate));
});
RefreshRateType currRefreshRateType = iter->first;
// Some content aligns better on higher refresh rate. For example for 45fps we should choose
// 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
// align well with both
constexpr float MARGIN = 0.05f;
float ratio = mRefreshRateConfigs.getRefreshRate(currRefreshRateType)->fps /
float(mContentRefreshRate);
if (std::abs(std::round(ratio) - ratio) > MARGIN) {
while (iter != mRefreshRateConfigs.getRefreshRates().cend()) {
ratio = iter->second->fps / float(mContentRefreshRate);
if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
currRefreshRateType = iter->first;
break;
}
++iter;
}
}
return currRefreshRateType;
}
Scheduler::RefreshRateType Scheduler::getPreferredRefreshRateType() {
std::lock_guard<std::mutex> lock(mFeatureStateLock);
return mRefreshRateType;
}
void Scheduler::changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent) {
std::lock_guard<std::mutex> lock(mCallbackLock);
if (mChangeRefreshRateCallback) {
mChangeRefreshRateCallback(refreshRateType, configEvent);
}
}
} // namespace android
|