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

Commit 78b82523 authored by Alec Mouri's avatar Alec Mouri
Browse files

Move RingBuffer from SF utils into libui

Stuff in libui or libgui or elsewhere upstack might want to use this.

Also remove PowerAdvisor's RingBuffer implementation, since it's basically the same thing.

Change-Id: I9d8d94fa0d7b8327b320ceed0d507b83979d033d
Bug: 360932099
Flag: EXEMPT refactor
Test: builds
parent 78a579cb
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@
#include <stddef.h>
#include <array>

namespace android::utils {
namespace android::ui {

template <class T, size_t SIZE>
class RingBuffer {
@@ -31,8 +31,8 @@ public:
    ~RingBuffer() = default;

    constexpr size_t capacity() const { return SIZE; }

    size_t size() const { return mCount; }
    bool isFull() const { return size() == capacity(); }

    T& next() {
        mHead = static_cast<size_t>(mHead + 1) % SIZE;
@@ -67,4 +67,4 @@ private:
    size_t mCount = 0;
};

} // namespace android::utils
} // namespace android::ui
+11 −0
Original line number Diff line number Diff line
@@ -143,6 +143,17 @@ cc_test {
    ],
}

cc_test {
    name: "RingBuffer_test",
    test_suites: ["device-tests"],
    shared_libs: ["libui"],
    srcs: ["RingBuffer_test.cpp"],
    cflags: [
        "-Wall",
        "-Werror",
    ],
}

cc_test {
    name: "Size_test",
    test_suites: ["device-tests"],
+70 −0
Original line number Diff line number Diff line
/*
 * Copyright 2025 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.
 */

#include <gtest/gtest.h>
#include <ui/RingBuffer.h>

namespace android::ui {

TEST(RingBuffer, basic) {
    RingBuffer<int, 5> rb;

    rb.next() = 1;
    ASSERT_EQ(1, rb.size());
    ASSERT_EQ(1, rb.back());
    ASSERT_EQ(1, rb.front());

    rb.next() = 2;
    ASSERT_EQ(2, rb.size());
    ASSERT_EQ(2, rb.back());
    ASSERT_EQ(1, rb.front());
    ASSERT_EQ(1, rb[-1]);

    rb.next() = 3;
    ASSERT_EQ(3, rb.size());
    ASSERT_EQ(3, rb.back());
    ASSERT_EQ(1, rb.front());
    ASSERT_EQ(2, rb[-1]);
    ASSERT_EQ(1, rb[-2]);

    rb.next() = 4;
    ASSERT_EQ(4, rb.size());
    ASSERT_EQ(4, rb.back());
    ASSERT_EQ(1, rb.front());
    ASSERT_EQ(3, rb[-1]);
    ASSERT_EQ(2, rb[-2]);
    ASSERT_EQ(1, rb[-3]);

    rb.next() = 5;
    ASSERT_EQ(5, rb.size());
    ASSERT_EQ(5, rb.back());
    ASSERT_EQ(1, rb.front());
    ASSERT_EQ(4, rb[-1]);
    ASSERT_EQ(3, rb[-2]);
    ASSERT_EQ(2, rb[-3]);
    ASSERT_EQ(1, rb[-4]);

    rb.next() = 6;
    ASSERT_EQ(5, rb.size());
    ASSERT_EQ(6, rb.back());
    ASSERT_EQ(2, rb.front());
    ASSERT_EQ(5, rb[-1]);
    ASSERT_EQ(4, rb[-2]);
    ASSERT_EQ(3, rb[-3]);
    ASSERT_EQ(2, rb[-4]);
}

} // namespace android::ui
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -19,11 +19,11 @@
#include <android-base/thread_annotations.h>
#include <android/gui/IHdrLayerInfoListener.h>
#include <binder/IBinder.h>
#include <ui/RingBuffer.h>
#include <utils/Timers.h>

#include <unordered_map>

#include "Utils/RingBuffer.h"
#include "WpHash.h"

namespace android {
@@ -102,7 +102,7 @@ private:
        EventHistoryEntry(const HdrLayerInfo& info) : info(info) { timestamp = systemTime(); }
    };

    utils::RingBuffer<EventHistoryEntry, 32> mHdrInfoHistory;
    ui::RingBuffer<EventHistoryEntry, 32> mHdrInfoHistory;
};

} // namespace android
 No newline at end of file
+7 −7
Original line number Diff line number Diff line
@@ -515,7 +515,7 @@ void PowerAdvisor::setRequiresRenderEngine(DisplayId displayId, bool requiresRen
}

void PowerAdvisor::setExpectedPresentTime(TimePoint expectedPresentTime) {
    mExpectedPresentTimes.append(expectedPresentTime);
    mExpectedPresentTimes.next() = expectedPresentTime;
}

void PowerAdvisor::setSfPresentTiming(TimePoint presentFenceTime, TimePoint presentEndTime) {
@@ -532,7 +532,7 @@ void PowerAdvisor::setHwcPresentDelayedTime(DisplayId displayId, TimePoint earli
}

void PowerAdvisor::setCommitStart(TimePoint commitStartTime) {
    mCommitStartTimes.append(commitStartTime);
    mCommitStartTimes.next() = commitStartTime;
}

void PowerAdvisor::setCompositeEnd(TimePoint compositeEndTime) {
@@ -579,7 +579,7 @@ std::optional<hal::WorkDuration> PowerAdvisor::estimateWorkDuration() {
    }

    // Tracks when we finish presenting to hwc
    TimePoint estimatedHwcEndTime = mCommitStartTimes[0];
    TimePoint estimatedHwcEndTime = mCommitStartTimes.back();

    // How long we spent this frame not doing anything, waiting for fences or vsync
    Duration idleDuration = 0ns;
@@ -643,13 +643,13 @@ std::optional<hal::WorkDuration> PowerAdvisor::estimateWorkDuration() {
    // Also add the frame delay duration since the target did not move while we were delayed
    Duration totalDuration = mFrameDelayDuration +
            std::max(estimatedHwcEndTime, estimatedGpuEndTime.value_or(TimePoint{0ns})) -
            mCommitStartTimes[0];
            mCommitStartTimes.back();
    Duration totalDurationWithoutGpu =
            mFrameDelayDuration + estimatedHwcEndTime - mCommitStartTimes[0];
            mFrameDelayDuration + estimatedHwcEndTime - mCommitStartTimes.back();

    // We finish SurfaceFlinger when post-composition finishes, so add that in here
    Duration flingerDuration =
            estimatedFlingerEndTime + mLastPostcompDuration - mCommitStartTimes[0];
            estimatedFlingerEndTime + mLastPostcompDuration - mCommitStartTimes.back();
    Duration estimatedGpuDuration = firstGpuTimeline.has_value()
            ? estimatedGpuEndTime.value_or(TimePoint{0ns}) - firstGpuTimeline->startTime
            : Duration::fromNs(0);
@@ -661,7 +661,7 @@ std::optional<hal::WorkDuration> PowerAdvisor::estimateWorkDuration() {
    hal::WorkDuration duration{
            .timeStampNanos = TimePoint::now().ns(),
            .durationNanos = combinedDuration.ns(),
            .workPeriodStartTimestampNanos = mCommitStartTimes[0].ns(),
            .workPeriodStartTimestampNanos = mCommitStartTimes.back().ns(),
            .cpuDurationNanos = supportsGpuReporting() ? cpuDuration.ns() : 0,
            .gpuDurationNanos = supportsGpuReporting() ? estimatedGpuDuration.ns() : 0,
    };
Loading