libui: use unique_fd in Fence
Use unique_fd internally. Add a constructor to create a Fence from
a unique_fd.
Test: boots
Change-Id: I36e7a337885a6559ed6fe82c56b718f56108aaf8
diff --git a/libs/ui/Android.bp b/libs/ui/Android.bp
index 102964f..abe856e 100644
--- a/libs/ui/Android.bp
+++ b/libs/ui/Android.bp
@@ -95,6 +95,7 @@
],
header_libs: [
+ "libbase_headers",
"libnativebase_headers",
"libhardware_headers",
],
@@ -107,6 +108,7 @@
],
export_header_lib_headers: [
+ "libbase_headers",
"libnativebase_headers",
"libhardware_headers",
],
diff --git a/libs/ui/Fence.cpp b/libs/ui/Fence.cpp
index b67f4d9..ff53aa8 100644
--- a/libs/ui/Fence.cpp
+++ b/libs/ui/Fence.cpp
@@ -37,18 +37,12 @@
const sp<Fence> Fence::NO_FENCE = sp<Fence>(new Fence);
-Fence::Fence() :
- mFenceFd(-1) {
-}
-
Fence::Fence(int fenceFd) :
mFenceFd(fenceFd) {
}
-Fence::~Fence() {
- if (mFenceFd != -1) {
- close(mFenceFd);
- }
+Fence::Fence(base::unique_fd fenceFd) :
+ mFenceFd(std::move(fenceFd)) {
}
status_t Fence::wait(int timeout) {
@@ -68,7 +62,7 @@
int warningTimeout = 3000;
int err = sync_wait(mFenceFd, warningTimeout);
if (err < 0 && errno == ETIME) {
- ALOGE("%s: fence %d didn't signal in %u ms", logname, mFenceFd,
+ ALOGE("%s: fence %d didn't signal in %u ms", logname, mFenceFd.get(),
warningTimeout);
err = sync_wait(mFenceFd, TIMEOUT_NEVER);
}
@@ -94,7 +88,7 @@
if (result == -1) {
status_t err = -errno;
ALOGE("merge: sync_merge(\"%s\", %d, %d) returned an error: %s (%d)",
- name, f1->mFenceFd, f2->mFenceFd,
+ name, f1->mFenceFd.get(), f2->mFenceFd.get(),
strerror(-err), err);
return NO_FENCE;
}
@@ -117,7 +111,7 @@
struct sync_fence_info_data* finfo = sync_fence_info(mFenceFd);
if (finfo == NULL) {
- ALOGE("sync_fence_info returned NULL for fd %d", mFenceFd);
+ ALOGE("sync_fence_info returned NULL for fd %d", mFenceFd.get());
return SIGNAL_TIME_INVALID;
}
if (finfo->status != 1) {
@@ -181,7 +175,7 @@
}
if (numFds) {
- mFenceFd = *fds++;
+ mFenceFd.reset(*fds++);
count--;
}
diff --git a/libs/ui/include/ui/Fence.h b/libs/ui/include/ui/Fence.h
index 37811bc..ec67fa9 100644
--- a/libs/ui/include/ui/Fence.h
+++ b/libs/ui/include/ui/Fence.h
@@ -19,6 +19,7 @@
#include <stdint.h>
+#include <android-base/unique_fd.h>
#include <utils/Flattenable.h>
#include <utils/RefBase.h>
#include <utils/Timers.h>
@@ -49,12 +50,13 @@
// Construct a new Fence object with an invalid file descriptor. This
// should be done when the Fence object will be set up by unflattening
// serialized data.
- Fence();
+ Fence() = default;
// Construct a new Fence object to manage a given fence file descriptor.
// When the new Fence object is destructed the file descriptor will be
// closed.
explicit Fence(int fenceFd);
+ explicit Fence(base::unique_fd fenceFd);
// Not copyable or movable.
Fence(const Fence& rhs) = delete;
@@ -136,9 +138,9 @@
private:
// Only allow instantiation using ref counting.
friend class LightRefBase<Fence>;
- ~Fence();
+ ~Fence() = default;
- int mFenceFd;
+ base::unique_fd mFenceFd;
};
}; // namespace android
diff --git a/services/surfaceflinger/BufferLayerConsumer.cpp b/services/surfaceflinger/BufferLayerConsumer.cpp
index 16c3254..bfe3dd1 100644
--- a/services/surfaceflinger/BufferLayerConsumer.cpp
+++ b/services/surfaceflinger/BufferLayerConsumer.cpp
@@ -392,12 +392,12 @@
if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
if (SyncFeatures::getInstance().useNativeFenceSync()) {
- int fenceFd = mRE.flush().release();
+ base::unique_fd fenceFd = mRE.flush();
if (fenceFd == -1) {
BLC_LOGE("syncForReleaseLocked: failed to flush RenderEngine");
return UNKNOWN_ERROR;
}
- sp<Fence> fence(new Fence(fenceFd));
+ sp<Fence> fence(new Fence(std::move(fenceFd)));
status_t err = addReleaseFenceLocked(mCurrentTexture,
mCurrentTextureImage->graphicBuffer(), fence);
if (err != OK) {