Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 257e0701 authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Snap for 11255311 from a5643cb0 to 24Q2-release

Change-Id: I9d9a7616a6040dfcda295265b9a7bd7fa617c51e
parents a83347cb a5643cb0
Loading
Loading
Loading
Loading
+33 −15
Original line number Diff line number Diff line
@@ -31,7 +31,11 @@ namespace aidl::android::os {
 */
class PersistableBundle {
   public:
    PersistableBundle() noexcept : mPBundle(APersistableBundle_new()) {}
    PersistableBundle() noexcept {
        if (__builtin_available(android __ANDROID_API_V__, *)) {
            mPBundle = APersistableBundle_new();
        }
    }
    // takes ownership of the APersistableBundle*
    PersistableBundle(APersistableBundle* _Nonnull bundle) noexcept : mPBundle(bundle) {}
    // takes ownership of the APersistableBundle*
@@ -327,21 +331,33 @@ class PersistableBundle {
    }

    bool getBooleanVector(const std::string& key, std::vector<bool>* _Nonnull vec) {
        if (__builtin_available(android __ANDROID_API_V__, *)) {
            return getVecInternal<bool>(&APersistableBundle_getBooleanVector, mPBundle, key.c_str(),
                                        vec);
        }
        return false;
    }
    bool getIntVector(const std::string& key, std::vector<int32_t>* _Nonnull vec) {
        if (__builtin_available(android __ANDROID_API_V__, *)) {
            return getVecInternal<int32_t>(&APersistableBundle_getIntVector, mPBundle, key.c_str(),
                                       vec);
        }
        return false;
    }
    bool getLongVector(const std::string& key, std::vector<int64_t>* _Nonnull vec) {
        if (__builtin_available(android __ANDROID_API_V__, *)) {
            return getVecInternal<int64_t>(&APersistableBundle_getLongVector, mPBundle, key.c_str(),
                                       vec);
        }
        return false;
    }
    bool getDoubleVector(const std::string& key, std::vector<double>* _Nonnull vec) {
        if (__builtin_available(android __ANDROID_API_V__, *)) {
            return getVecInternal<double>(&APersistableBundle_getDoubleVector, mPBundle, key.c_str(),
                                      vec);
        }
        return false;
    }

    // Takes ownership of and frees the char** and its elements.
    // Creates a new set or vector based on the array of char*.
@@ -361,6 +377,7 @@ class PersistableBundle {
    }

    bool getStringVector(const std::string& key, std::vector<std::string>* _Nonnull vec) {
        if (__builtin_available(android __ANDROID_API_V__, *)) {
            int32_t bytes = APersistableBundle_getStringVector(mPBundle, key.c_str(), nullptr, 0,
                                                            &stringAllocator, nullptr);
            if (bytes > 0) {
@@ -372,6 +389,7 @@ class PersistableBundle {
                    return true;
                }
            }
        }
        return false;
    }

+8 −24
Original line number Diff line number Diff line
@@ -222,6 +222,7 @@ bool DisplayDevice::initiateModeChange(display::DisplayModeRequest&& desiredMode
                                       const hal::VsyncPeriodChangeConstraints& constraints,
                                       hal::VsyncPeriodChangeTimeline& outTimeline) {
    mPendingModeOpt = std::move(desiredMode);
    mIsModeSetPending = true;

    const auto& mode = *mPendingModeOpt->mode.modePtr;

@@ -234,22 +235,9 @@ bool DisplayDevice::initiateModeChange(display::DisplayModeRequest&& desiredMode
    return true;
}

auto DisplayDevice::finalizeModeChange() -> ModeChange {
    if (!mPendingModeOpt) return NoModeChange{"No pending mode"};

    auto pendingMode = *std::exchange(mPendingModeOpt, std::nullopt);
    auto& pendingModePtr = pendingMode.mode.modePtr;

    if (!mRefreshRateSelector->displayModes().contains(pendingModePtr->getId())) {
        return NoModeChange{"Unknown pending mode"};
    }

    if (getActiveMode().modePtr->getResolution() != pendingModePtr->getResolution()) {
        return ResolutionChange{std::move(pendingMode)};
    }

    setActiveMode(pendingModePtr->getId(), pendingModePtr->getVsyncRate(), pendingMode.mode.fps);
    return RefreshRateChange{std::move(pendingMode)};
void DisplayDevice::finalizeModeChange(DisplayModeId modeId, Fps vsyncRate, Fps renderFps) {
    setActiveMode(modeId, vsyncRate, renderFps);
    mIsModeSetPending = false;
}

nsecs_t DisplayDevice::getVsyncPeriodFromHWC() const {
@@ -587,15 +575,11 @@ auto DisplayDevice::getDesiredMode() const -> DisplayModeRequestOpt {
    return mDesiredModeOpt;
}

auto DisplayDevice::takeDesiredMode() -> DisplayModeRequestOpt {
    DisplayModeRequestOpt desiredModeOpt;
    {
void DisplayDevice::clearDesiredMode() {
    std::scoped_lock lock(mDesiredModeLock);
        std::swap(mDesiredModeOpt, desiredModeOpt);
    mDesiredModeOpt.reset();
    mHasDesiredModeTrace = false;
}
    return desiredModeOpt;
}

void DisplayDevice::adjustRefreshRate(Fps pacesetterDisplayRefreshRate) {
    using fps_approx_ops::operator<=;
+7 −29
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@
#include <memory>
#include <string>
#include <unordered_map>
#include <variant>

#include <android-base/thread_annotations.h>
#include <android/native_window.h>
@@ -196,11 +195,12 @@ public:
    using DisplayModeRequestOpt = ftl::Optional<display::DisplayModeRequest>;

    DisplayModeRequestOpt getDesiredMode() const EXCLUDES(mDesiredModeLock);
    DisplayModeRequestOpt takeDesiredMode() EXCLUDES(mDesiredModeLock);
    void clearDesiredMode() EXCLUDES(mDesiredModeLock);

    bool isModeSetPending() const REQUIRES(kMainThreadContext) {
        return mPendingModeOpt.has_value();
    DisplayModeRequestOpt getPendingMode() const REQUIRES(kMainThreadContext) {
        return mPendingModeOpt;
    }
    bool isModeSetPending() const REQUIRES(kMainThreadContext) { return mIsModeSetPending; }

    scheduler::FrameRateMode getActiveMode() const REQUIRES(kMainThreadContext) {
        return mRefreshRateSelector->getActiveMode();
@@ -212,25 +212,8 @@ public:
                            hal::VsyncPeriodChangeTimeline& outTimeline)
            REQUIRES(kMainThreadContext);

    struct NoModeChange {
        const char* reason;
    };

    struct ResolutionChange {
        display::DisplayModeRequest activeMode;
    };

    struct RefreshRateChange {
        display::DisplayModeRequest activeMode;
    };

    using ModeChange = std::variant<NoModeChange, ResolutionChange, RefreshRateChange>;

    // Clears the pending DisplayModeRequest, and returns the ModeChange that occurred. If it was a
    // RefreshRateChange, the pending mode becomes the active mode. If it was a ResolutionChange,
    // the caller is responsible for resizing the framebuffer to match the active resolution by
    // recreating the DisplayDevice.
    ModeChange finalizeModeChange() REQUIRES(kMainThreadContext);
    void finalizeModeChange(DisplayModeId, Fps vsyncRate, Fps renderFps)
            REQUIRES(kMainThreadContext);

    scheduler::RefreshRateSelector& refreshRateSelector() const { return *mRefreshRateSelector; }

@@ -267,8 +250,6 @@ public:
    void dump(utils::Dumper&) const;

private:
    friend class TestableSurfaceFlinger;

    template <size_t N>
    inline std::string concatId(const char (&str)[N]) const {
        return std::string(ftl::Concat(str, ' ', getId().value).str());
@@ -319,15 +300,12 @@ private:
    // This parameter is only used for hdr/sdr ratio overlay
    float mHdrSdrRatio = 1.0f;

    // A DisplayModeRequest flows through three states: desired, pending, and active. Requests
    // within a frame are merged into a single desired request. Unless cleared, the request is
    // relayed to HWC on the next frame, and becomes pending. The mode becomes active once HWC
    // signals the present fence to confirm the mode set.
    mutable std::mutex mDesiredModeLock;
    DisplayModeRequestOpt mDesiredModeOpt GUARDED_BY(mDesiredModeLock);
    TracedOrdinal<bool> mHasDesiredModeTrace GUARDED_BY(mDesiredModeLock);

    DisplayModeRequestOpt mPendingModeOpt GUARDED_BY(kMainThreadContext);
    bool mIsModeSetPending GUARDED_BY(kMainThreadContext) = false;
};

struct DisplayDeviceState {
+69 −60
Original line number Diff line number Diff line
@@ -59,7 +59,6 @@
#include <ftl/concat.h>
#include <ftl/fake_guard.h>
#include <ftl/future.h>
#include <ftl/match.h>
#include <ftl/unit.h>
#include <gui/AidlStatusUtil.h>
#include <gui/BufferQueue.h>
@@ -1236,21 +1235,20 @@ status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& displayToken,
    return NO_ERROR;
}

void SurfaceFlinger::setDesiredMode(display::DisplayModeRequest&& desiredMode, bool force) {
    const auto mode = desiredMode.mode;
    const auto displayId = mode.modePtr->getPhysicalDisplayId();

void SurfaceFlinger::setDesiredMode(display::DisplayModeRequest&& request, bool force) {
    const auto displayId = request.mode.modePtr->getPhysicalDisplayId();
    ATRACE_NAME(ftl::Concat(__func__, ' ', displayId.value).c_str());

    const auto display = getDisplayDeviceLocked(displayId);
    if (!display) {
        ALOGW("%s: Unknown display %s", __func__, to_string(displayId).c_str());
        ALOGW("%s: display is no longer valid", __func__);
        return;
    }

    const bool emitEvent = desiredMode.emitEvent;
    const auto mode = request.mode;
    const bool emitEvent = request.emitEvent;

    switch (display->setDesiredMode(std::move(desiredMode), force)) {
    switch (display->setDesiredMode(std::move(request), force)) {
        case DisplayDevice::DesiredModeAction::InitiateDisplayModeSwitch:
            // DisplayDevice::setDesiredMode updated the render rate, so inform Scheduler.
            mScheduler->setRenderRate(displayId,
@@ -1346,55 +1344,61 @@ void SurfaceFlinger::finalizeDisplayModeChange(DisplayDevice& display) {
    const auto displayId = display.getPhysicalId();
    ATRACE_NAME(ftl::Concat(__func__, ' ', displayId.value).c_str());

    ftl::match(
            display.finalizeModeChange(),
            [this, displayId](DisplayDevice::RefreshRateChange change) {
                ftl::FakeGuard guard(mStateLock);

                if (change.activeMode.emitEvent) {
                    dispatchDisplayModeChangeEvent(displayId, change.activeMode.mode);
    const auto pendingModeOpt = display.getPendingMode();
    if (!pendingModeOpt) {
        // There is no pending mode change. This can happen if the active
        // display changed and the mode change happened on a different display.
        return;
    }

                applyActiveMode(std::move(change.activeMode));
            },
            [&](DisplayDevice::ResolutionChange change) {
    const auto& activeMode = pendingModeOpt->mode;

    if (display.getActiveMode().modePtr->getResolution() != activeMode.modePtr->getResolution()) {
        auto& state = mCurrentState.displays.editValueFor(display.getDisplayToken());
                // Assign a new sequence ID to recreate the display and so its framebuffer.
        // We need to generate new sequenceId in order to recreate the display (and this
        // way the framebuffer).
        state.sequenceId = DisplayDeviceState{}.sequenceId;
                state.physical->activeMode = change.activeMode.mode.modePtr.get();

                ftl::FakeGuard guard1(kMainThreadContext);
                ftl::FakeGuard guard2(mStateLock);
        state.physical->activeMode = activeMode.modePtr.get();
        processDisplayChangesLocked();

                applyActiveMode(std::move(change.activeMode));
            },
            [](DisplayDevice::NoModeChange noChange) {
                // TODO(b/255635821): Remove this case, as it should no longer happen.
                ALOGE("A mode change was initiated but not finalized: %s", noChange.reason);
            });
        // processDisplayChangesLocked will update all necessary components so we're done here.
        return;
    }

    display.finalizeModeChange(activeMode.modePtr->getId(), activeMode.modePtr->getVsyncRate(),
                               activeMode.fps);

    if (displayId == mActiveDisplayId) {
        mRefreshRateStats->setRefreshRate(activeMode.fps);
        updatePhaseConfiguration(activeMode.fps);
    }

    if (pendingModeOpt->emitEvent) {
        dispatchDisplayModeChangeEvent(displayId, activeMode);
    }
}

void SurfaceFlinger::dropModeRequest(display::DisplayModeRequest&& request) {
    if (request.mode.modePtr->getPhysicalDisplayId() == mActiveDisplayId) {
void SurfaceFlinger::dropModeRequest(const sp<DisplayDevice>& display) {
    display->clearDesiredMode();
    if (display->getPhysicalId() == mActiveDisplayId) {
        // TODO(b/255635711): Check for pending mode changes on other displays.
        mScheduler->setModeChangePending(false);
    }
}

void SurfaceFlinger::applyActiveMode(display::DisplayModeRequest&& activeMode) {
    auto activeModePtr = activeMode.mode.modePtr;
void SurfaceFlinger::applyActiveMode(const sp<DisplayDevice>& display) {
    const auto activeModeOpt = display->getDesiredMode();
    auto activeModePtr = activeModeOpt->mode.modePtr;
    const auto displayId = activeModePtr->getPhysicalDisplayId();
    const auto renderFps = activeMode.mode.fps;
    const auto renderFps = activeModeOpt->mode.fps;

    dropModeRequest(std::move(activeMode));
    dropModeRequest(display);

    constexpr bool kAllowToEnable = true;
    mScheduler->resyncToHardwareVsync(displayId, kAllowToEnable, std::move(activeModePtr).take());
    mScheduler->setRenderRate(displayId, renderFps);

    if (displayId == mActiveDisplayId) {
        mRefreshRateStats->setRefreshRate(renderFps);
        updatePhaseConfiguration(renderFps);
    }
}
@@ -1408,50 +1412,50 @@ void SurfaceFlinger::initiateDisplayModeChanges() {
        const auto display = getDisplayDeviceLocked(id);
        if (!display) continue;

        auto desiredModeOpt = display->takeDesiredMode();
        auto desiredModeOpt = display->getDesiredMode();
        if (!desiredModeOpt) {
            continue;
        }

        auto desiredMode = std::move(*desiredModeOpt);

        if (!shouldApplyRefreshRateSelectorPolicy(*display)) {
            dropModeRequest(std::move(desiredMode));
            dropModeRequest(display);
            continue;
        }

        const auto desiredModeId = desiredMode.mode.modePtr->getId();
        const auto desiredModeId = desiredModeOpt->mode.modePtr->getId();
        const auto displayModePtrOpt = physical.snapshot().displayModes().get(desiredModeId);

        if (!displayModePtrOpt) {
            ALOGW("%s: Unknown mode %d for display %s", __func__, desiredModeId.value(),
                  to_string(id).c_str());
            dropModeRequest(std::move(desiredMode));
            ALOGW("Desired display mode is no longer supported. Mode ID = %d",
                  desiredModeId.value());
            dropModeRequest(display);
            continue;
        }

        if (display->getActiveMode() == desiredMode.mode) {
            dropModeRequest(std::move(desiredMode));
        ALOGV("%s changing active mode to %d(%s) for display %s", __func__, desiredModeId.value(),
              to_string(displayModePtrOpt->get()->getVsyncRate()).c_str(),
              to_string(display->getId()).c_str());

        if (display->getActiveMode() == desiredModeOpt->mode) {
            applyActiveMode(display);
            continue;
        }

        // The desired mode is different from the active mode. However, the allowed modes might have
        // changed since setDesiredMode scheduled a mode transition.
        if (!display->refreshRateSelector().isModeAllowed(desiredMode.mode)) {
            dropModeRequest(std::move(desiredMode));
        // Desired active mode was set, it is different than the mode currently in use, however
        // allowed modes might have changed by the time we process the refresh.
        // Make sure the desired mode is still allowed
        if (!display->refreshRateSelector().isModeAllowed(desiredModeOpt->mode)) {
            dropModeRequest(display);
            continue;
        }

        ALOGV("Mode setting display %s to %d (%s)", to_string(id).c_str(), desiredModeId.value(),
              to_string(displayModePtrOpt.value().get()->getVsyncRate()).c_str());

        // TODO(b/142753666): Use constraints.
        // TODO(b/142753666) use constrains
        hal::VsyncPeriodChangeConstraints constraints;
        constraints.desiredTimeNanos = systemTime();
        constraints.seamlessRequired = false;
        hal::VsyncPeriodChangeTimeline outTimeline;

        if (!display->initiateModeChange(std::move(desiredMode), constraints, outTimeline)) {
        if (!display->initiateModeChange(std::move(*desiredModeOpt), constraints, outTimeline)) {
            continue;
        }

@@ -1471,6 +1475,11 @@ void SurfaceFlinger::initiateDisplayModeChanges() {
    if (displayToUpdateImmediately) {
        const auto display = getDisplayDeviceLocked(*displayToUpdateImmediately);
        finalizeDisplayModeChange(*display);

        const auto desiredModeOpt = display->getDesiredMode();
        if (desiredModeOpt && display->getActiveMode() == desiredModeOpt->mode) {
            applyActiveMode(display);
        }
    }
}

@@ -7395,7 +7404,7 @@ void SurfaceFlinger::kernelTimerChanged(bool expired) {
    if (!updateOverlay) return;

    // Update the overlay on the main thread to avoid race conditions with
    // RefreshRateSelector::getActiveMode.
    // RefreshRateSelector::getActiveMode
    static_cast<void>(mScheduler->schedule([=, this] {
        const auto display = FTL_FAKE_GUARD(mStateLock, getDefaultDisplayDeviceLocked());
        if (!display) {
+3 −3
Original line number Diff line number Diff line
@@ -727,9 +727,9 @@ private:
    void initiateDisplayModeChanges() REQUIRES(mStateLock, kMainThreadContext);
    void finalizeDisplayModeChange(DisplayDevice&) REQUIRES(mStateLock, kMainThreadContext);

    // TODO(b/241285191): Move to Scheduler.
    void dropModeRequest(display::DisplayModeRequest&&) REQUIRES(mStateLock);
    void applyActiveMode(display::DisplayModeRequest&&) REQUIRES(mStateLock);
    // TODO(b/241285191): Replace DisplayDevice with DisplayModeRequest, and move to Scheduler.
    void dropModeRequest(const sp<DisplayDevice>&) REQUIRES(mStateLock);
    void applyActiveMode(const sp<DisplayDevice>&) REQUIRES(mStateLock);

    // Called on the main thread in response to setPowerMode()
    void setPowerModeInternal(const sp<DisplayDevice>& display, hal::PowerMode mode)
Loading