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 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 libs/ui/include/ui/RingBuffer.h create mode 100644 libs/ui/tests/RingBuffer_test.cpp (limited to 'libs') 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 -- cgit v1.2.3-59-g8ed1b