From 99dacaa815afccefa1f8e571e8ee6be8d311015d Mon Sep 17 00:00:00 2001 From: Tianyu Jiang Date: Tue, 27 Nov 2018 18:47:00 -0800 Subject: Add BufferHubEventFd The main use case of BufferHubEventFd is to signal buffer state change cross multiple processes without explicit IPC. Intended architecture: There will be one BufferHubEventFd per BufferNode object and multiple clients of the same BufferNode should share the same kernel object of the eventFd. This CL focuses on proving eventfd and epollfd fit our needs. More work is needed for BufferHubEventFd to make it binder-parcelable with necessary constructors. Bug: 68770788 Test: BufferHubBuffer_test Change-Id: I7f7f5e5762fa81df61b649c306d209be2fd4236c --- libs/ui/BufferHubEventFd.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 libs/ui/BufferHubEventFd.cpp (limited to 'libs/ui/BufferHubEventFd.cpp') diff --git a/libs/ui/BufferHubEventFd.cpp b/libs/ui/BufferHubEventFd.cpp new file mode 100644 index 0000000000..978b3526f3 --- /dev/null +++ b/libs/ui/BufferHubEventFd.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 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. + */ + +#include + +#include +#include + +namespace android { + +BufferHubEventFd::BufferHubEventFd() : mFd(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)) {} + +status_t BufferHubEventFd::signal() const { + if (!isValid()) { + ALOGE("%s: cannot signal an invalid eventfd.", __FUNCTION__); + return DEAD_OBJECT; + } + + eventfd_write(mFd.get(), 1); + return OK; +} + +status_t BufferHubEventFd::clear() const { + if (!isValid()) { + ALOGE("%s: cannot clear an invalid eventfd.", __FUNCTION__); + return DEAD_OBJECT; + } + + eventfd_t value; + eventfd_read(mFd.get(), &value); + return OK; +} + +} // namespace android -- cgit v1.2.3-59-g8ed1b From d5855a65d8520b91286f5ca3b27f17a6b8d68b9e Mon Sep 17 00:00:00 2001 From: Fan Xu Date: Mon, 14 Jan 2019 15:52:42 -0800 Subject: Allow create BufferHubEventFd from existing fd Add a constructor to BufferHubEventFd. Rewrite EventFd_testDupEventFd to test on the constructor. Test: BufferHub_test Bug: 68770788 Change-Id: I565aff02c1a6e45498e64a817539952911850d48 --- libs/ui/BufferHubEventFd.cpp | 2 ++ libs/ui/include/ui/BufferHubEventFd.h | 6 ++++++ libs/ui/tests/BufferHubEventFd_test.cpp | 9 ++++----- 3 files changed, 12 insertions(+), 5 deletions(-) (limited to 'libs/ui/BufferHubEventFd.cpp') diff --git a/libs/ui/BufferHubEventFd.cpp b/libs/ui/BufferHubEventFd.cpp index 978b3526f3..bffc2ca803 100644 --- a/libs/ui/BufferHubEventFd.cpp +++ b/libs/ui/BufferHubEventFd.cpp @@ -23,6 +23,8 @@ namespace android { BufferHubEventFd::BufferHubEventFd() : mFd(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)) {} +BufferHubEventFd::BufferHubEventFd(int fd) : mFd(fd) {} + status_t BufferHubEventFd::signal() const { if (!isValid()) { ALOGE("%s: cannot signal an invalid eventfd.", __FUNCTION__); diff --git a/libs/ui/include/ui/BufferHubEventFd.h b/libs/ui/include/ui/BufferHubEventFd.h index 0e856bdb67..8772304c0f 100644 --- a/libs/ui/include/ui/BufferHubEventFd.h +++ b/libs/ui/include/ui/BufferHubEventFd.h @@ -29,6 +29,12 @@ public: */ BufferHubEventFd(); + /** + * Constructs from a valid event fd. Caller is responsible for the validity of the fd. Takes + * ownership. + */ + BufferHubEventFd(int fd); + /** * Returns whether this BufferHubEventFd holds a valid event_fd. */ diff --git a/libs/ui/tests/BufferHubEventFd_test.cpp b/libs/ui/tests/BufferHubEventFd_test.cpp index 92fb33ff48..2c9aa576fe 100644 --- a/libs/ui/tests/BufferHubEventFd_test.cpp +++ b/libs/ui/tests/BufferHubEventFd_test.cpp @@ -91,22 +91,21 @@ TEST_F(BufferHubEventFdTest, EventFd_testDupEventFd) { // Technically, the dupliated eventFd and the original eventFd are pointing // to the same kernel object. This test signals the duplicated eventFd but epolls the origianl // eventFd. - base::unique_fd dupedEventFd(dup(eventFd.get())); + BufferHubEventFd dupedEventFd(dup(eventFd.get())); ASSERT_GE(dupedEventFd.get(), 0); std::array events; EXPECT_EQ(epoll_wait(epollFd.get(), events.data(), events.size(), 0), 0); - eventfd_write(dupedEventFd.get(), 1); + dupedEventFd.signal(); EXPECT_EQ(epoll_wait(epollFd.get(), events.data(), events.size(), 0), 1); // The epoll fd is edge triggered, so it only responds to the eventFd once. EXPECT_EQ(epoll_wait(epollFd.get(), events.data(), events.size(), 0), 0); - eventfd_write(dupedEventFd.get(), 1); + dupedEventFd.signal(); - eventfd_t value; - eventfd_read(dupedEventFd.get(), &value); + dupedEventFd.clear(); EXPECT_EQ(epoll_wait(epollFd.get(), events.data(), events.size(), 0), 0); } -- cgit v1.2.3-59-g8ed1b