blob: 4be0a3a9d9752a7b0e48583821c06926909ecf2f [file] [log] [blame]
Jamie Gennisf25e1832012-06-13 16:31:43 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Anderson175a7202016-10-10 16:52:56 -070017#include <ui/Fence.h>
18
Jamie Gennisf25e1832012-06-13 16:31:43 -070019#define LOG_TAG "Fence"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
Dan Stozad3182402014-11-17 12:03:59 -080023// We would eliminate the non-conforming zero-length array, but we can't since
24// this is effectively included from the Linux kernel
25#pragma clang diagnostic push
26#pragma clang diagnostic ignored "-Wzero-length-array"
Jamie Gennisf25e1832012-06-13 16:31:43 -070027#include <sync/sync.h>
Dan Stozad3182402014-11-17 12:03:59 -080028#pragma clang diagnostic pop
29
Brian Anderson175a7202016-10-10 16:52:56 -070030#include <sys/types.h>
Jamie Gennisf25e1832012-06-13 16:31:43 -070031#include <unistd.h>
32#include <utils/Log.h>
Brian Anderson175a7202016-10-10 16:52:56 -070033#include <utils/String8.h>
Jamie Gennisf25e1832012-06-13 16:31:43 -070034#include <utils/Trace.h>
35
36namespace android {
37
Jamie Gennis1df8c342012-12-20 14:05:45 -080038const sp<Fence> Fence::NO_FENCE = sp<Fence>(new Fence);
Jesse Hallef194142012-06-14 14:45:17 -070039
Jamie Gennisf25e1832012-06-13 16:31:43 -070040Fence::Fence(int fenceFd) :
41 mFenceFd(fenceFd) {
42}
43
Chia-I Wudf1badd2017-12-27 11:10:20 -080044Fence::Fence(base::unique_fd fenceFd) :
45 mFenceFd(std::move(fenceFd)) {
Jamie Gennisf25e1832012-06-13 16:31:43 -070046}
47
Dan Stozad3182402014-11-17 12:03:59 -080048status_t Fence::wait(int timeout) {
Jamie Gennisf25e1832012-06-13 16:31:43 -070049 ATRACE_CALL();
50 if (mFenceFd == -1) {
51 return NO_ERROR;
52 }
Mathias Agopianb5c9dcd2012-10-09 14:38:19 -070053 int err = sync_wait(mFenceFd, timeout);
54 return err < 0 ? -errno : status_t(NO_ERROR);
Jamie Gennisf25e1832012-06-13 16:31:43 -070055}
56
Mathias Agopianea74d3b2013-05-16 18:03:22 -070057status_t Fence::waitForever(const char* logname) {
Jesse Hallba607d52012-10-01 14:05:20 -070058 ATRACE_CALL();
59 if (mFenceFd == -1) {
60 return NO_ERROR;
61 }
Dan Stozad3182402014-11-17 12:03:59 -080062 int warningTimeout = 3000;
Jesse Hallba607d52012-10-01 14:05:20 -070063 int err = sync_wait(mFenceFd, warningTimeout);
Mathias Agopianb5c9dcd2012-10-09 14:38:19 -070064 if (err < 0 && errno == ETIME) {
Iris Changd174e382019-12-04 15:34:18 +080065 ALOGE("waitForever: %s: fence %d didn't signal in %u ms", logname, mFenceFd.get(),
66 warningTimeout);
67
68 struct sync_file_info* finfo = sync_file_info(mFenceFd);
69 if (finfo) {
70 // status: active(0) signaled(1) error(<0)
71 ALOGI("waitForever: fence(%s) status(%d)", finfo->name, finfo->status);
72
73 struct sync_fence_info* pinfo = sync_get_fence_info(finfo);
74 for (uint32_t i = 0; i < finfo->num_fences; i++) {
75 uint64_t ts_sec = pinfo[i].timestamp_ns / 1000000000LL;
76 uint64_t ts_usec = (pinfo[i].timestamp_ns % 1000000000LL) / 1000LL;
77
78 ALOGI("waitForever: sync point: timeline(%s) drv(%s) status(%d) timestamp(%" PRIu64
79 ".%06" PRIu64 ")",
80 pinfo[i].obj_name, pinfo[i].driver_name, pinfo[i].status, ts_sec, ts_usec);
81 }
82 sync_file_info_free(finfo);
83 }
84
Jesse Hallba607d52012-10-01 14:05:20 -070085 err = sync_wait(mFenceFd, TIMEOUT_NEVER);
86 }
Mathias Agopianb5c9dcd2012-10-09 14:38:19 -070087 return err < 0 ? -errno : status_t(NO_ERROR);
Jesse Hallba607d52012-10-01 14:05:20 -070088}
89
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -070090sp<Fence> Fence::merge(const char* name, const sp<Fence>& f1,
Jamie Gennisf25e1832012-06-13 16:31:43 -070091 const sp<Fence>& f2) {
92 ATRACE_CALL();
Jamie Gennis1df8c342012-12-20 14:05:45 -080093 int result;
94 // Merge the two fences. In the case where one of the fences is not a
95 // valid fence (e.g. NO_FENCE) we merge the one valid fence with itself so
96 // that a new fence with the given name is created.
97 if (f1->isValid() && f2->isValid()) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -070098 result = sync_merge(name, f1->mFenceFd, f2->mFenceFd);
Jamie Gennis1df8c342012-12-20 14:05:45 -080099 } else if (f1->isValid()) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700100 result = sync_merge(name, f1->mFenceFd, f1->mFenceFd);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800101 } else if (f2->isValid()) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700102 result = sync_merge(name, f2->mFenceFd, f2->mFenceFd);
Jamie Gennis1df8c342012-12-20 14:05:45 -0800103 } else {
104 return NO_FENCE;
105 }
Jamie Gennisf25e1832012-06-13 16:31:43 -0700106 if (result == -1) {
Mathias Agopiand83d67b2012-07-30 15:10:35 -0700107 status_t err = -errno;
108 ALOGE("merge: sync_merge(\"%s\", %d, %d) returned an error: %s (%d)",
Chia-I Wudf1badd2017-12-27 11:10:20 -0800109 name, f1->mFenceFd.get(), f2->mFenceFd.get(),
Mathias Agopiand83d67b2012-07-30 15:10:35 -0700110 strerror(-err), err);
Jesse Hallef194142012-06-14 14:45:17 -0700111 return NO_FENCE;
Jamie Gennisf25e1832012-06-13 16:31:43 -0700112 }
113 return sp<Fence>(new Fence(result));
114}
115
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700116sp<Fence> Fence::merge(const String8& name, const sp<Fence>& f1,
117 const sp<Fence>& f2) {
Tomasz Wasilczykf2add402023-08-11 00:06:51 +0000118 return merge(name.c_str(), f1, f2);
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700119}
120
Jesse Hallf9783af2012-06-25 13:54:23 -0700121int Fence::dup() const {
122 return ::dup(mFenceFd);
123}
124
Jamie Gennis82dbc742012-11-08 19:23:28 -0800125nsecs_t Fence::getSignalTime() const {
126 if (mFenceFd == -1) {
Brian Anderson221de2a2016-09-21 16:53:28 -0700127 return SIGNAL_TIME_INVALID;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800128 }
129
Jesse Halle158ca72018-05-18 15:58:23 -0700130 struct sync_file_info* finfo = sync_file_info(mFenceFd);
Yi Kong48d76082019-03-24 02:01:06 -0700131 if (finfo == nullptr) {
Jesse Halle158ca72018-05-18 15:58:23 -0700132 ALOGE("sync_file_info returned NULL for fd %d", mFenceFd.get());
Brian Anderson221de2a2016-09-21 16:53:28 -0700133 return SIGNAL_TIME_INVALID;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800134 }
Yao, Kefeiad741cc2021-11-01 15:20:31 +0800135
Jamie Gennis82dbc742012-11-08 19:23:28 -0800136 if (finfo->status != 1) {
Yao, Kefeiad741cc2021-11-01 15:20:31 +0800137 const auto status = finfo->status;
138 ALOGE_IF(status < 0, "%s: sync_file_info contains an error: <%d> for fd: <%d>", __func__,
139 status, mFenceFd.get());
Jesse Halle158ca72018-05-18 15:58:23 -0700140 sync_file_info_free(finfo);
Yao, Kefeiad741cc2021-11-01 15:20:31 +0800141 return status < 0 ? SIGNAL_TIME_INVALID : SIGNAL_TIME_PENDING;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800142 }
143
Jamie Gennis82dbc742012-11-08 19:23:28 -0800144 uint64_t timestamp = 0;
Jesse Halle158ca72018-05-18 15:58:23 -0700145 struct sync_fence_info* pinfo = sync_get_fence_info(finfo);
146 for (size_t i = 0; i < finfo->num_fences; i++) {
147 if (pinfo[i].timestamp_ns > timestamp) {
148 timestamp = pinfo[i].timestamp_ns;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800149 }
150 }
Jamie Gennis82dbc742012-11-08 19:23:28 -0800151
Jesse Halle158ca72018-05-18 15:58:23 -0700152 sync_file_info_free(finfo);
Jamie Gennis82dbc742012-11-08 19:23:28 -0800153 return nsecs_t(timestamp);
154}
155
Jamie Gennisf25e1832012-06-13 16:31:43 -0700156size_t Fence::getFlattenedSize() const {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700157 return 4;
Jamie Gennisf25e1832012-06-13 16:31:43 -0700158}
159
160size_t Fence::getFdCount() const {
Jamie Gennis1df8c342012-12-20 14:05:45 -0800161 return isValid() ? 1 : 0;
Jamie Gennisf25e1832012-06-13 16:31:43 -0700162}
163
Mathias Agopiane1424282013-07-29 21:24:40 -0700164status_t Fence::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
165 if (size < getFlattenedSize() || count < getFdCount()) {
166 return NO_MEMORY;
Jamie Gennisf25e1832012-06-13 16:31:43 -0700167 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700168 // Cast to uint32_t since the size of a size_t can vary between 32- and
169 // 64-bit processes
170 FlattenableUtils::write(buffer, size, static_cast<uint32_t>(getFdCount()));
Jamie Gennis1df8c342012-12-20 14:05:45 -0800171 if (isValid()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700172 *fds++ = mFenceFd;
173 count--;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800174 }
Jamie Gennisf25e1832012-06-13 16:31:43 -0700175 return NO_ERROR;
176}
177
Mathias Agopiane1424282013-07-29 21:24:40 -0700178status_t Fence::unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Jamie Gennisf25e1832012-06-13 16:31:43 -0700179 if (mFenceFd != -1) {
180 // Don't unflatten if we already have a valid fd.
181 return INVALID_OPERATION;
182 }
183
Chris Forbes98096022017-05-10 13:12:00 -0700184 if (size < getFlattenedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700185 return NO_MEMORY;
186 }
187
Colin Cross288f2ef2014-04-14 18:43:12 -0700188 uint32_t numFds;
Mathias Agopiane1424282013-07-29 21:24:40 -0700189 FlattenableUtils::read(buffer, size, numFds);
190
191 if (numFds > 1) {
192 return BAD_VALUE;
193 }
194
195 if (count < numFds) {
196 return NO_MEMORY;
197 }
198
199 if (numFds) {
Chia-I Wudf1badd2017-12-27 11:10:20 -0800200 mFenceFd.reset(*fds++);
Mathias Agopiane1424282013-07-29 21:24:40 -0700201 count--;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800202 }
203
Jamie Gennisf25e1832012-06-13 16:31:43 -0700204 return NO_ERROR;
205}
206
207} // namespace android