From 78b8252396fd3b7e5e6c78870b5163fc140c1b17 Mon Sep 17 00:00:00 2001 From: Alec Mouri Date: Tue, 25 Feb 2025 14:35:12 -0800 Subject: 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 --- libs/ui/include/ui/RingBuffer.h | 70 ++++++++++++++++++++++ libs/ui/tests/Android.bp | 11 ++++ libs/ui/tests/RingBuffer_test.cpp | 70 ++++++++++++++++++++++ services/surfaceflinger/HdrLayerInfoReporter.h | 4 +- .../surfaceflinger/PowerAdvisor/PowerAdvisor.cpp | 14 ++--- .../surfaceflinger/PowerAdvisor/PowerAdvisor.h | 26 +------- .../Scheduler/include/scheduler/FrameTargeter.h | 4 +- services/surfaceflinger/Utils/RingBuffer.h | 70 ---------------------- 8 files changed, 165 insertions(+), 104 deletions(-) create mode 100644 libs/ui/include/ui/RingBuffer.h create mode 100644 libs/ui/tests/RingBuffer_test.cpp delete mode 100644 services/surfaceflinger/Utils/RingBuffer.h diff --git a/libs/ui/include/ui/RingBuffer.h b/libs/ui/include/ui/RingBuffer.h new file mode 100644 index 0000000000..31d5a950ef --- /dev/null +++ b/libs/ui/include/ui/RingBuffer.h @@ -0,0 +1,70 @@ +/* + * Copyright 2023 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. + */ + +#pragma once + +#include +#include + +namespace android::ui { + +template +class RingBuffer { + RingBuffer(const RingBuffer&) = delete; + void operator=(const RingBuffer&) = delete; + +public: + RingBuffer() = default; + ~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(mHead + 1) % SIZE; + if (mCount < SIZE) { + mCount++; + } + return mBuffer[static_cast(mHead)]; + } + + T& front() { return (*this)[0]; } + const T& front() const { return (*this)[0]; } + + T& back() { return (*this)[size() - 1]; } + const T& back() const { return (*this)[size() - 1]; } + + T& operator[](size_t index) { + return mBuffer[(static_cast(mHead + 1) + index) % mCount]; + } + + const T& operator[](size_t index) const { + return mBuffer[(static_cast(mHead + 1) + index) % mCount]; + } + + void clear() { + mCount = 0; + mHead = -1; + } + +private: + std::array mBuffer; + int mHead = -1; + size_t mCount = 0; +}; + +} // namespace android::ui diff --git a/libs/ui/tests/Android.bp b/libs/ui/tests/Android.bp index 2d8a1e326c..2b11786df3 100644 --- a/libs/ui/tests/Android.bp +++ b/libs/ui/tests/Android.bp @@ -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"], diff --git a/libs/ui/tests/RingBuffer_test.cpp b/libs/ui/tests/RingBuffer_test.cpp new file mode 100644 index 0000000000..9839492b9f --- /dev/null +++ b/libs/ui/tests/RingBuffer_test.cpp @@ -0,0 +1,70 @@ +/* + * 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 +#include + +namespace android::ui { + +TEST(RingBuffer, basic) { + RingBuffer 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 diff --git a/services/surfaceflinger/HdrLayerInfoReporter.h b/services/surfaceflinger/HdrLayerInfoReporter.h index 614f33fbce..758b111e41 100644 --- a/services/surfaceflinger/HdrLayerInfoReporter.h +++ b/services/surfaceflinger/HdrLayerInfoReporter.h @@ -19,11 +19,11 @@ #include #include #include +#include #include #include -#include "Utils/RingBuffer.h" #include "WpHash.h" namespace android { @@ -102,7 +102,7 @@ private: EventHistoryEntry(const HdrLayerInfo& info) : info(info) { timestamp = systemTime(); } }; - utils::RingBuffer mHdrInfoHistory; + ui::RingBuffer mHdrInfoHistory; }; } // namespace android \ No newline at end of file diff --git a/services/surfaceflinger/PowerAdvisor/PowerAdvisor.cpp b/services/surfaceflinger/PowerAdvisor/PowerAdvisor.cpp index cd7210c627..788448d079 100644 --- a/services/surfaceflinger/PowerAdvisor/PowerAdvisor.cpp +++ b/services/surfaceflinger/PowerAdvisor/PowerAdvisor.cpp @@ -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 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 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 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, }; diff --git a/services/surfaceflinger/PowerAdvisor/PowerAdvisor.h b/services/surfaceflinger/PowerAdvisor/PowerAdvisor.h index 540a9df2ca..b97160a3e5 100644 --- a/services/surfaceflinger/PowerAdvisor/PowerAdvisor.h +++ b/services/surfaceflinger/PowerAdvisor/PowerAdvisor.h @@ -23,6 +23,7 @@ #include #include +#include #include // FMQ library in IPower does questionable conversions @@ -247,27 +248,6 @@ private: std::optional estimateGpuTiming(std::optional previousEndTime); }; - template - class RingBuffer { - std::array elements = {}; - size_t mIndex = 0; - size_t numElements = 0; - - public: - void append(T item) { - mIndex = (mIndex + 1) % N; - numElements = std::min(N, numElements + 1); - elements[mIndex] = item; - } - bool isFull() const { return numElements == N; } - // Allows access like [0] == current, [-1] = previous, etc.. - T& operator[](int offset) { - size_t positiveOffset = - static_cast((offset % static_cast(N)) + static_cast(N)); - return elements[(mIndex + positiveOffset) % N]; - } - }; - // Filter and sort the display ids by a given property std::vector getOrderedDisplayIds( std::optional DisplayTimingData::*sortBy); @@ -287,9 +267,9 @@ private: // Last frame's post-composition duration Duration mLastPostcompDuration{0ns}; // Buffer of recent commit start times - RingBuffer mCommitStartTimes; + ui::RingBuffer mCommitStartTimes; // Buffer of recent expected present times - RingBuffer mExpectedPresentTimes; + ui::RingBuffer mExpectedPresentTimes; // Most recent present fence time, provided by SF after composition engine finishes presenting TimePoint mLastPresentFenceTime; // Most recent composition engine present end time, returned with the present fence from SF diff --git a/services/surfaceflinger/Scheduler/include/scheduler/FrameTargeter.h b/services/surfaceflinger/Scheduler/include/scheduler/FrameTargeter.h index 813d4dedff..ff461d2d85 100644 --- a/services/surfaceflinger/Scheduler/include/scheduler/FrameTargeter.h +++ b/services/surfaceflinger/Scheduler/include/scheduler/FrameTargeter.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -34,7 +35,6 @@ // TODO(b/185536303): Pull to FTL. #include "../../../TracedOrdinal.h" #include "../../../Utils/Dumper.h" -#include "../../../Utils/RingBuffer.h" namespace android::scheduler { @@ -108,7 +108,7 @@ protected: std::pair expectedSignaledPresentFence( Period vsyncPeriod, Period minFramePeriod) const; std::array mPresentFencesLegacy; - utils::RingBuffer mPresentFences; + ui::RingBuffer mPresentFences; FrameTime mLastSignaledFrameTime; diff --git a/services/surfaceflinger/Utils/RingBuffer.h b/services/surfaceflinger/Utils/RingBuffer.h deleted file mode 100644 index 215472b388..0000000000 --- a/services/surfaceflinger/Utils/RingBuffer.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2023 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. - */ - -#pragma once - -#include -#include - -namespace android::utils { - -template -class RingBuffer { - RingBuffer(const RingBuffer&) = delete; - void operator=(const RingBuffer&) = delete; - -public: - RingBuffer() = default; - ~RingBuffer() = default; - - constexpr size_t capacity() const { return SIZE; } - - size_t size() const { return mCount; } - - T& next() { - mHead = static_cast(mHead + 1) % SIZE; - if (mCount < SIZE) { - mCount++; - } - return mBuffer[static_cast(mHead)]; - } - - T& front() { return (*this)[0]; } - const T& front() const { return (*this)[0]; } - - T& back() { return (*this)[size() - 1]; } - const T& back() const { return (*this)[size() - 1]; } - - T& operator[](size_t index) { - return mBuffer[(static_cast(mHead + 1) + index) % mCount]; - } - - const T& operator[](size_t index) const { - return mBuffer[(static_cast(mHead + 1) + index) % mCount]; - } - - void clear() { - mCount = 0; - mHead = -1; - } - -private: - std::array mBuffer; - int mHead = -1; - size_t mCount = 0; -}; - -} // namespace android::utils -- cgit v1.2.3-59-g8ed1b