blob: a1dda3a475ad17406c832aac23bd6cd77b07921a [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
17#define LOG_TAG "Fence"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
20
Dan Stozad3182402014-11-17 12:03:59 -080021// We would eliminate the non-conforming zero-length array, but we can't since
22// this is effectively included from the Linux kernel
23#pragma clang diagnostic push
24#pragma clang diagnostic ignored "-Wzero-length-array"
Jamie Gennisf25e1832012-06-13 16:31:43 -070025#include <sync/sync.h>
Dan Stozad3182402014-11-17 12:03:59 -080026#pragma clang diagnostic pop
27
Jamie Gennisf25e1832012-06-13 16:31:43 -070028#include <ui/Fence.h>
29#include <unistd.h>
30#include <utils/Log.h>
31#include <utils/Trace.h>
32
33namespace android {
34
Jamie Gennis1df8c342012-12-20 14:05:45 -080035const sp<Fence> Fence::NO_FENCE = sp<Fence>(new Fence);
Jesse Hallef194142012-06-14 14:45:17 -070036
Jamie Gennisf25e1832012-06-13 16:31:43 -070037Fence::Fence() :
38 mFenceFd(-1) {
39}
40
41Fence::Fence(int fenceFd) :
42 mFenceFd(fenceFd) {
43}
44
45Fence::~Fence() {
46 if (mFenceFd != -1) {
47 close(mFenceFd);
48 }
49}
50
Dan Stozad3182402014-11-17 12:03:59 -080051status_t Fence::wait(int timeout) {
Jamie Gennisf25e1832012-06-13 16:31:43 -070052 ATRACE_CALL();
53 if (mFenceFd == -1) {
54 return NO_ERROR;
55 }
Mathias Agopianb5c9dcd2012-10-09 14:38:19 -070056 int err = sync_wait(mFenceFd, timeout);
57 return err < 0 ? -errno : status_t(NO_ERROR);
Jamie Gennisf25e1832012-06-13 16:31:43 -070058}
59
Mathias Agopianea74d3b2013-05-16 18:03:22 -070060status_t Fence::waitForever(const char* logname) {
Jesse Hallba607d52012-10-01 14:05:20 -070061 ATRACE_CALL();
62 if (mFenceFd == -1) {
63 return NO_ERROR;
64 }
Dan Stozad3182402014-11-17 12:03:59 -080065 int warningTimeout = 3000;
Jesse Hallba607d52012-10-01 14:05:20 -070066 int err = sync_wait(mFenceFd, warningTimeout);
Mathias Agopianb5c9dcd2012-10-09 14:38:19 -070067 if (err < 0 && errno == ETIME) {
Jesse Hallba607d52012-10-01 14:05:20 -070068 ALOGE("%s: fence %d didn't signal in %u ms", logname, mFenceFd,
69 warningTimeout);
70 err = sync_wait(mFenceFd, TIMEOUT_NEVER);
71 }
Mathias Agopianb5c9dcd2012-10-09 14:38:19 -070072 return err < 0 ? -errno : status_t(NO_ERROR);
Jesse Hallba607d52012-10-01 14:05:20 -070073}
74
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -070075sp<Fence> Fence::merge(const char* name, const sp<Fence>& f1,
Jamie Gennisf25e1832012-06-13 16:31:43 -070076 const sp<Fence>& f2) {
77 ATRACE_CALL();
Jamie Gennis1df8c342012-12-20 14:05:45 -080078 int result;
79 // Merge the two fences. In the case where one of the fences is not a
80 // valid fence (e.g. NO_FENCE) we merge the one valid fence with itself so
81 // that a new fence with the given name is created.
82 if (f1->isValid() && f2->isValid()) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -070083 result = sync_merge(name, f1->mFenceFd, f2->mFenceFd);
Jamie Gennis1df8c342012-12-20 14:05:45 -080084 } else if (f1->isValid()) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -070085 result = sync_merge(name, f1->mFenceFd, f1->mFenceFd);
Jamie Gennis1df8c342012-12-20 14:05:45 -080086 } else if (f2->isValid()) {
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -070087 result = sync_merge(name, f2->mFenceFd, f2->mFenceFd);
Jamie Gennis1df8c342012-12-20 14:05:45 -080088 } else {
89 return NO_FENCE;
90 }
Jamie Gennisf25e1832012-06-13 16:31:43 -070091 if (result == -1) {
Mathias Agopiand83d67b2012-07-30 15:10:35 -070092 status_t err = -errno;
93 ALOGE("merge: sync_merge(\"%s\", %d, %d) returned an error: %s (%d)",
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -070094 name, f1->mFenceFd, f2->mFenceFd,
Mathias Agopiand83d67b2012-07-30 15:10:35 -070095 strerror(-err), err);
Jesse Hallef194142012-06-14 14:45:17 -070096 return NO_FENCE;
Jamie Gennisf25e1832012-06-13 16:31:43 -070097 }
98 return sp<Fence>(new Fence(result));
99}
100
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -0700101sp<Fence> Fence::merge(const String8& name, const sp<Fence>& f1,
102 const sp<Fence>& f2) {
103 return merge(name.string(), f1, f2);
104}
105
Jesse Hallf9783af2012-06-25 13:54:23 -0700106int Fence::dup() const {
107 return ::dup(mFenceFd);
108}
109
Jamie Gennis82dbc742012-11-08 19:23:28 -0800110nsecs_t Fence::getSignalTime() const {
111 if (mFenceFd == -1) {
Brian Anderson221de2a2016-09-21 16:53:28 -0700112 return SIGNAL_TIME_INVALID;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800113 }
114
115 struct sync_fence_info_data* finfo = sync_fence_info(mFenceFd);
116 if (finfo == NULL) {
117 ALOGE("sync_fence_info returned NULL for fd %d", mFenceFd);
Brian Anderson221de2a2016-09-21 16:53:28 -0700118 return SIGNAL_TIME_INVALID;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800119 }
120 if (finfo->status != 1) {
Jesse Hall7c36cd22013-01-14 15:59:32 -0800121 sync_fence_info_free(finfo);
Brian Anderson221de2a2016-09-21 16:53:28 -0700122 return SIGNAL_TIME_PENDING;
Jamie Gennis82dbc742012-11-08 19:23:28 -0800123 }
124
125 struct sync_pt_info* pinfo = NULL;
126 uint64_t timestamp = 0;
127 while ((pinfo = sync_pt_info(finfo, pinfo)) != NULL) {
128 if (pinfo->timestamp_ns > timestamp) {
129 timestamp = pinfo->timestamp_ns;
130 }
131 }
132 sync_fence_info_free(finfo);
133
134 return nsecs_t(timestamp);
135}
136
Jamie Gennisf25e1832012-06-13 16:31:43 -0700137size_t Fence::getFlattenedSize() const {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700138 return 4;
Jamie Gennisf25e1832012-06-13 16:31:43 -0700139}
140
141size_t Fence::getFdCount() const {
Jamie Gennis1df8c342012-12-20 14:05:45 -0800142 return isValid() ? 1 : 0;
Jamie Gennisf25e1832012-06-13 16:31:43 -0700143}
144
Mathias Agopiane1424282013-07-29 21:24:40 -0700145status_t Fence::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
146 if (size < getFlattenedSize() || count < getFdCount()) {
147 return NO_MEMORY;
Jamie Gennisf25e1832012-06-13 16:31:43 -0700148 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700149 // Cast to uint32_t since the size of a size_t can vary between 32- and
150 // 64-bit processes
151 FlattenableUtils::write(buffer, size, static_cast<uint32_t>(getFdCount()));
Jamie Gennis1df8c342012-12-20 14:05:45 -0800152 if (isValid()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700153 *fds++ = mFenceFd;
154 count--;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800155 }
Jamie Gennisf25e1832012-06-13 16:31:43 -0700156 return NO_ERROR;
157}
158
Mathias Agopiane1424282013-07-29 21:24:40 -0700159status_t Fence::unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count) {
Jamie Gennisf25e1832012-06-13 16:31:43 -0700160 if (mFenceFd != -1) {
161 // Don't unflatten if we already have a valid fd.
162 return INVALID_OPERATION;
163 }
164
Mathias Agopiane1424282013-07-29 21:24:40 -0700165 if (size < 1) {
166 return NO_MEMORY;
167 }
168
Colin Cross288f2ef2014-04-14 18:43:12 -0700169 uint32_t numFds;
Mathias Agopiane1424282013-07-29 21:24:40 -0700170 FlattenableUtils::read(buffer, size, numFds);
171
172 if (numFds > 1) {
173 return BAD_VALUE;
174 }
175
176 if (count < numFds) {
177 return NO_MEMORY;
178 }
179
180 if (numFds) {
181 mFenceFd = *fds++;
182 count--;
Jamie Gennis1df8c342012-12-20 14:05:45 -0800183 }
184
Jamie Gennisf25e1832012-06-13 16:31:43 -0700185 return NO_ERROR;
186}
187
188} // namespace android