drm_hwcomposer: Rework autofd
Motivation:
Current implementation of UniqueFd can be used in a different ways,
making analytical tracking of FD lifecycle much harder than it may be.
Keep this part clean is very important, since any wrong code may open
a hard-to-detect runtime bugs and fd leaks, which may accidentally slip
into the production.
Implementation:
1. Combine UniqueFd anf OutputFd into single class.
2. Reduce the API to be minimal and sufficient.
3. Document the API and use cases.
4. Move to utils/UniqueFd.h.
5. dup(fd) was replaced with fcntl(fd, F_DUPFD_CLOEXEC)) to
address clang-tidy findings. Find more information at [1]
[1]: https://clang.llvm.org/extra/clang-tidy/checks/android-cloexec-dup.html
Signed-off-by: Roman Stratiienko <r.stratiienko@gmail.com>
diff --git a/include/autofd.h b/include/autofd.h
deleted file mode 100644
index 9af6c22..0000000
--- a/include/autofd.h
+++ /dev/null
@@ -1,106 +0,0 @@
-/*
- * Copyright (C) 2015 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.
- */
-
-#ifndef ANDROID_AUTO_FD_H_
-#define ANDROID_AUTO_FD_H_
-
-#include <unistd.h>
-
-namespace android {
-
-class UniqueFd {
- public:
- UniqueFd() = default;
- UniqueFd(int fd) : fd_(fd) {
- }
- UniqueFd(UniqueFd &&rhs) {
- fd_ = rhs.fd_;
- rhs.fd_ = -1;
- }
-
- UniqueFd &operator=(UniqueFd &&rhs) {
- Set(rhs.Release());
- return *this;
- }
-
- ~UniqueFd() {
- if (fd_ >= 0)
- close(fd_);
- }
-
- int Release() {
- int old_fd = fd_;
- fd_ = -1;
- return old_fd;
- }
-
- int Set(int fd) {
- if (fd_ >= 0)
- close(fd_);
- fd_ = fd;
- return fd_;
- }
-
- void Close() {
- if (fd_ >= 0)
- close(fd_);
- fd_ = -1;
- }
-
- int get() const {
- return fd_;
- }
-
- private:
- int fd_ = -1;
-};
-
-struct OutputFd {
- OutputFd() = default;
- OutputFd(int *fd) : fd_(fd) {
- }
- OutputFd(OutputFd &&rhs) {
- fd_ = rhs.fd_;
- rhs.fd_ = NULL;
- }
-
- OutputFd &operator=(OutputFd &&rhs) {
- fd_ = rhs.fd_;
- rhs.fd_ = NULL;
- return *this;
- }
-
- int Set(int fd) {
- if (*fd_ >= 0)
- close(*fd_);
- *fd_ = fd;
- return fd;
- }
-
- int get() {
- return *fd_;
- }
-
- operator bool() const {
- return fd_ != NULL;
- }
-
- private:
- int *fd_ = NULL;
-};
-} // namespace android
-
-#endif
diff --git a/include/drmhwcomposer.h b/include/drmhwcomposer.h
index 6955306..22af12b 100644
--- a/include/drmhwcomposer.h
+++ b/include/drmhwcomposer.h
@@ -24,9 +24,9 @@
#include <vector>
-#include "autofd.h"
#include "drm/DrmFbImporter.h"
#include "drmhwcgralloc.h"
+#include "utils/UniqueFd.h"
namespace android {
@@ -61,7 +61,6 @@
android_dataspace_t dataspace;
UniqueFd acquire_fence;
- OutputFd release_fence;
int ImportBuffer(DrmDevice *drmDevice);