1 /*
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
17 #include <ui/Fence.h>
18
19 #define LOG_TAG "Fence"
20 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
21 //#define LOG_NDEBUG 0
22
23 // 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"
27 #include <sync/sync.h>
28 #pragma clang diagnostic pop
29
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <utils/Log.h>
33 #include <utils/String8.h>
34 #include <utils/Trace.h>
35
36 namespace android {
37
38 const sp<Fence> Fence::NO_FENCE = sp<Fence>(new Fence);
39
Fence(int fenceFd)40 Fence::Fence(int fenceFd) :
41 mFenceFd(fenceFd) {
42 }
43
Fence(base::unique_fd fenceFd)44 Fence::Fence(base::unique_fd fenceFd) :
45 mFenceFd(std::move(fenceFd)) {
46 }
47
wait(int timeout)48 status_t Fence::wait(int timeout) {
49 ATRACE_CALL();
50 if (mFenceFd == -1) {
51 return NO_ERROR;
52 }
53 int err = sync_wait(mFenceFd, timeout);
54 return err < 0 ? -errno : status_t(NO_ERROR);
55 }
56
waitForever(const char * logname)57 status_t Fence::waitForever(const char* logname) {
58 ATRACE_CALL();
59 if (mFenceFd == -1) {
60 return NO_ERROR;
61 }
62 int warningTimeout = 3000;
63 int err = sync_wait(mFenceFd, warningTimeout);
64 if (err < 0 && errno == ETIME) {
65 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
85 err = sync_wait(mFenceFd, TIMEOUT_NEVER);
86 }
87 return err < 0 ? -errno : status_t(NO_ERROR);
88 }
89
merge(const char * name,const sp<Fence> & f1,const sp<Fence> & f2)90 sp<Fence> Fence::merge(const char* name, const sp<Fence>& f1,
91 const sp<Fence>& f2) {
92 ATRACE_CALL();
93 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()) {
98 result = sync_merge(name, f1->mFenceFd, f2->mFenceFd);
99 } else if (f1->isValid()) {
100 result = sync_merge(name, f1->mFenceFd, f1->mFenceFd);
101 } else if (f2->isValid()) {
102 result = sync_merge(name, f2->mFenceFd, f2->mFenceFd);
103 } else {
104 return NO_FENCE;
105 }
106 if (result == -1) {
107 status_t err = -errno;
108 ALOGE("merge: sync_merge(\"%s\", %d, %d) returned an error: %s (%d)",
109 name, f1->mFenceFd.get(), f2->mFenceFd.get(),
110 strerror(-err), err);
111 return NO_FENCE;
112 }
113 return sp<Fence>(new Fence(result));
114 }
115
merge(const String8 & name,const sp<Fence> & f1,const sp<Fence> & f2)116 sp<Fence> Fence::merge(const String8& name, const sp<Fence>& f1,
117 const sp<Fence>& f2) {
118 return merge(name.string(), f1, f2);
119 }
120
dup() const121 int Fence::dup() const {
122 return ::dup(mFenceFd);
123 }
124
getSignalTime() const125 nsecs_t Fence::getSignalTime() const {
126 if (mFenceFd == -1) {
127 return SIGNAL_TIME_INVALID;
128 }
129
130 struct sync_file_info* finfo = sync_file_info(mFenceFd);
131 if (finfo == nullptr) {
132 ALOGE("sync_file_info returned NULL for fd %d", mFenceFd.get());
133 return SIGNAL_TIME_INVALID;
134 }
135
136 if (finfo->status != 1) {
137 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());
140 sync_file_info_free(finfo);
141 return status < 0 ? SIGNAL_TIME_INVALID : SIGNAL_TIME_PENDING;
142 }
143
144 uint64_t timestamp = 0;
145 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;
149 }
150 }
151
152 sync_file_info_free(finfo);
153 return nsecs_t(timestamp);
154 }
155
getFlattenedSize() const156 size_t Fence::getFlattenedSize() const {
157 return 4;
158 }
159
getFdCount() const160 size_t Fence::getFdCount() const {
161 return isValid() ? 1 : 0;
162 }
163
flatten(void * & buffer,size_t & size,int * & fds,size_t & count) const164 status_t Fence::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
165 if (size < getFlattenedSize() || count < getFdCount()) {
166 return NO_MEMORY;
167 }
168 // 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()));
171 if (isValid()) {
172 *fds++ = mFenceFd;
173 count--;
174 }
175 return NO_ERROR;
176 }
177
unflatten(void const * & buffer,size_t & size,int const * & fds,size_t & count)178 status_t Fence::unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count) {
179 if (mFenceFd != -1) {
180 // Don't unflatten if we already have a valid fd.
181 return INVALID_OPERATION;
182 }
183
184 if (size < getFlattenedSize()) {
185 return NO_MEMORY;
186 }
187
188 uint32_t numFds;
189 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) {
200 mFenceFd.reset(*fds++);
201 count--;
202 }
203
204 return NO_ERROR;
205 }
206
207 } // namespace android
208