Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 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 | |
Brian Anderson | 175a720 | 2016-10-10 16:52:56 -0700 | [diff] [blame^] | 17 | #include <ui/Fence.h> |
| 18 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 19 | #define LOG_TAG "Fence" |
| 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | //#define LOG_NDEBUG 0 |
| 22 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 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" |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 27 | #include <sync/sync.h> |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 28 | #pragma clang diagnostic pop |
| 29 | |
Brian Anderson | 175a720 | 2016-10-10 16:52:56 -0700 | [diff] [blame^] | 30 | #include <sys/types.h> |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | #include <utils/Log.h> |
Brian Anderson | 175a720 | 2016-10-10 16:52:56 -0700 | [diff] [blame^] | 33 | #include <utils/String8.h> |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 34 | #include <utils/Trace.h> |
| 35 | |
| 36 | namespace android { |
| 37 | |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 38 | const sp<Fence> Fence::NO_FENCE = sp<Fence>(new Fence); |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 39 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 40 | Fence::Fence() : |
| 41 | mFenceFd(-1) { |
| 42 | } |
| 43 | |
| 44 | Fence::Fence(int fenceFd) : |
| 45 | mFenceFd(fenceFd) { |
| 46 | } |
| 47 | |
| 48 | Fence::~Fence() { |
| 49 | if (mFenceFd != -1) { |
| 50 | close(mFenceFd); |
| 51 | } |
| 52 | } |
| 53 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 54 | status_t Fence::wait(int timeout) { |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 55 | ATRACE_CALL(); |
| 56 | if (mFenceFd == -1) { |
| 57 | return NO_ERROR; |
| 58 | } |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame] | 59 | int err = sync_wait(mFenceFd, timeout); |
| 60 | return err < 0 ? -errno : status_t(NO_ERROR); |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 63 | status_t Fence::waitForever(const char* logname) { |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 64 | ATRACE_CALL(); |
| 65 | if (mFenceFd == -1) { |
| 66 | return NO_ERROR; |
| 67 | } |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 68 | int warningTimeout = 3000; |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 69 | int err = sync_wait(mFenceFd, warningTimeout); |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame] | 70 | if (err < 0 && errno == ETIME) { |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 71 | ALOGE("%s: fence %d didn't signal in %u ms", logname, mFenceFd, |
| 72 | warningTimeout); |
| 73 | err = sync_wait(mFenceFd, TIMEOUT_NEVER); |
| 74 | } |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame] | 75 | return err < 0 ? -errno : status_t(NO_ERROR); |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Matthew Bouyack | fd4c8c3 | 2016-10-07 14:26:47 -0700 | [diff] [blame] | 78 | sp<Fence> Fence::merge(const char* name, const sp<Fence>& f1, |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 79 | const sp<Fence>& f2) { |
| 80 | ATRACE_CALL(); |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 81 | int result; |
| 82 | // Merge the two fences. In the case where one of the fences is not a |
| 83 | // valid fence (e.g. NO_FENCE) we merge the one valid fence with itself so |
| 84 | // that a new fence with the given name is created. |
| 85 | if (f1->isValid() && f2->isValid()) { |
Matthew Bouyack | fd4c8c3 | 2016-10-07 14:26:47 -0700 | [diff] [blame] | 86 | result = sync_merge(name, f1->mFenceFd, f2->mFenceFd); |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 87 | } else if (f1->isValid()) { |
Matthew Bouyack | fd4c8c3 | 2016-10-07 14:26:47 -0700 | [diff] [blame] | 88 | result = sync_merge(name, f1->mFenceFd, f1->mFenceFd); |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 89 | } else if (f2->isValid()) { |
Matthew Bouyack | fd4c8c3 | 2016-10-07 14:26:47 -0700 | [diff] [blame] | 90 | result = sync_merge(name, f2->mFenceFd, f2->mFenceFd); |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 91 | } else { |
| 92 | return NO_FENCE; |
| 93 | } |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 94 | if (result == -1) { |
Mathias Agopian | d83d67b | 2012-07-30 15:10:35 -0700 | [diff] [blame] | 95 | status_t err = -errno; |
| 96 | ALOGE("merge: sync_merge(\"%s\", %d, %d) returned an error: %s (%d)", |
Matthew Bouyack | fd4c8c3 | 2016-10-07 14:26:47 -0700 | [diff] [blame] | 97 | name, f1->mFenceFd, f2->mFenceFd, |
Mathias Agopian | d83d67b | 2012-07-30 15:10:35 -0700 | [diff] [blame] | 98 | strerror(-err), err); |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 99 | return NO_FENCE; |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 100 | } |
| 101 | return sp<Fence>(new Fence(result)); |
| 102 | } |
| 103 | |
Matthew Bouyack | fd4c8c3 | 2016-10-07 14:26:47 -0700 | [diff] [blame] | 104 | sp<Fence> Fence::merge(const String8& name, const sp<Fence>& f1, |
| 105 | const sp<Fence>& f2) { |
| 106 | return merge(name.string(), f1, f2); |
| 107 | } |
| 108 | |
Jesse Hall | f9783af | 2012-06-25 13:54:23 -0700 | [diff] [blame] | 109 | int Fence::dup() const { |
| 110 | return ::dup(mFenceFd); |
| 111 | } |
| 112 | |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 113 | nsecs_t Fence::getSignalTime() const { |
| 114 | if (mFenceFd == -1) { |
Brian Anderson | 221de2a | 2016-09-21 16:53:28 -0700 | [diff] [blame] | 115 | return SIGNAL_TIME_INVALID; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | struct sync_fence_info_data* finfo = sync_fence_info(mFenceFd); |
| 119 | if (finfo == NULL) { |
| 120 | ALOGE("sync_fence_info returned NULL for fd %d", mFenceFd); |
Brian Anderson | 221de2a | 2016-09-21 16:53:28 -0700 | [diff] [blame] | 121 | return SIGNAL_TIME_INVALID; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 122 | } |
| 123 | if (finfo->status != 1) { |
Jesse Hall | 7c36cd2 | 2013-01-14 15:59:32 -0800 | [diff] [blame] | 124 | sync_fence_info_free(finfo); |
Brian Anderson | 221de2a | 2016-09-21 16:53:28 -0700 | [diff] [blame] | 125 | return SIGNAL_TIME_PENDING; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | struct sync_pt_info* pinfo = NULL; |
| 129 | uint64_t timestamp = 0; |
| 130 | while ((pinfo = sync_pt_info(finfo, pinfo)) != NULL) { |
| 131 | if (pinfo->timestamp_ns > timestamp) { |
| 132 | timestamp = pinfo->timestamp_ns; |
| 133 | } |
| 134 | } |
| 135 | sync_fence_info_free(finfo); |
| 136 | |
| 137 | return nsecs_t(timestamp); |
| 138 | } |
| 139 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 140 | size_t Fence::getFlattenedSize() const { |
Dan Stoza | 6fbefbb | 2015-03-23 13:46:14 -0700 | [diff] [blame] | 141 | return 4; |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | size_t Fence::getFdCount() const { |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 145 | return isValid() ? 1 : 0; |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 148 | status_t Fence::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const { |
| 149 | if (size < getFlattenedSize() || count < getFdCount()) { |
| 150 | return NO_MEMORY; |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 151 | } |
Dan Stoza | 6fbefbb | 2015-03-23 13:46:14 -0700 | [diff] [blame] | 152 | // Cast to uint32_t since the size of a size_t can vary between 32- and |
| 153 | // 64-bit processes |
| 154 | FlattenableUtils::write(buffer, size, static_cast<uint32_t>(getFdCount())); |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 155 | if (isValid()) { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 156 | *fds++ = mFenceFd; |
| 157 | count--; |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 158 | } |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 159 | return NO_ERROR; |
| 160 | } |
| 161 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 162 | status_t Fence::unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count) { |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 163 | if (mFenceFd != -1) { |
| 164 | // Don't unflatten if we already have a valid fd. |
| 165 | return INVALID_OPERATION; |
| 166 | } |
| 167 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 168 | if (size < 1) { |
| 169 | return NO_MEMORY; |
| 170 | } |
| 171 | |
Colin Cross | 288f2ef | 2014-04-14 18:43:12 -0700 | [diff] [blame] | 172 | uint32_t numFds; |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 173 | FlattenableUtils::read(buffer, size, numFds); |
| 174 | |
| 175 | if (numFds > 1) { |
| 176 | return BAD_VALUE; |
| 177 | } |
| 178 | |
| 179 | if (count < numFds) { |
| 180 | return NO_MEMORY; |
| 181 | } |
| 182 | |
| 183 | if (numFds) { |
| 184 | mFenceFd = *fds++; |
| 185 | count--; |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 188 | return NO_ERROR; |
| 189 | } |
| 190 | |
| 191 | } // namespace android |