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