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 | |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 21 | // This is needed for stdint.h to define INT64_MAX in C++ |
| 22 | #define __STDC_LIMIT_MACROS |
| 23 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 24 | #include <sync/sync.h> |
| 25 | #include <ui/Fence.h> |
| 26 | #include <unistd.h> |
| 27 | #include <utils/Log.h> |
| 28 | #include <utils/Trace.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 32 | const sp<Fence> Fence::NO_FENCE = sp<Fence>(); |
| 33 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 34 | Fence::Fence() : |
| 35 | mFenceFd(-1) { |
| 36 | } |
| 37 | |
| 38 | Fence::Fence(int fenceFd) : |
| 39 | mFenceFd(fenceFd) { |
| 40 | } |
| 41 | |
| 42 | Fence::~Fence() { |
| 43 | if (mFenceFd != -1) { |
| 44 | close(mFenceFd); |
| 45 | } |
| 46 | } |
| 47 | |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame] | 48 | status_t Fence::wait(unsigned int timeout) { |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 49 | ATRACE_CALL(); |
| 50 | if (mFenceFd == -1) { |
| 51 | return NO_ERROR; |
| 52 | } |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame] | 53 | int err = sync_wait(mFenceFd, timeout); |
| 54 | return err < 0 ? -errno : status_t(NO_ERROR); |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame] | 57 | status_t Fence::waitForever(unsigned int warningTimeout, const char* logname) { |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 58 | ATRACE_CALL(); |
| 59 | if (mFenceFd == -1) { |
| 60 | return NO_ERROR; |
| 61 | } |
| 62 | int err = sync_wait(mFenceFd, warningTimeout); |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame] | 63 | if (err < 0 && errno == ETIME) { |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 64 | ALOGE("%s: fence %d didn't signal in %u ms", logname, mFenceFd, |
| 65 | warningTimeout); |
| 66 | err = sync_wait(mFenceFd, TIMEOUT_NEVER); |
| 67 | } |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame] | 68 | return err < 0 ? -errno : status_t(NO_ERROR); |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 71 | sp<Fence> Fence::merge(const String8& name, const sp<Fence>& f1, |
| 72 | const sp<Fence>& f2) { |
| 73 | ATRACE_CALL(); |
| 74 | int result = sync_merge(name.string(), f1->mFenceFd, f2->mFenceFd); |
| 75 | if (result == -1) { |
Mathias Agopian | d83d67b | 2012-07-30 15:10:35 -0700 | [diff] [blame] | 76 | status_t err = -errno; |
| 77 | ALOGE("merge: sync_merge(\"%s\", %d, %d) returned an error: %s (%d)", |
| 78 | name.string(), f1->mFenceFd, f2->mFenceFd, |
| 79 | strerror(-err), err); |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 80 | return NO_FENCE; |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 81 | } |
| 82 | return sp<Fence>(new Fence(result)); |
| 83 | } |
| 84 | |
Jesse Hall | f9783af | 2012-06-25 13:54:23 -0700 | [diff] [blame] | 85 | int Fence::dup() const { |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 86 | if (mFenceFd == -1) { |
| 87 | return -1; |
| 88 | } |
Jesse Hall | f9783af | 2012-06-25 13:54:23 -0700 | [diff] [blame] | 89 | return ::dup(mFenceFd); |
| 90 | } |
| 91 | |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 92 | nsecs_t Fence::getSignalTime() const { |
| 93 | if (mFenceFd == -1) { |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | struct sync_fence_info_data* finfo = sync_fence_info(mFenceFd); |
| 98 | if (finfo == NULL) { |
| 99 | ALOGE("sync_fence_info returned NULL for fd %d", mFenceFd); |
| 100 | return -1; |
| 101 | } |
| 102 | if (finfo->status != 1) { |
Jesse Hall | 7c36cd2 | 2013-01-14 15:59:32 -0800 | [diff] [blame] | 103 | sync_fence_info_free(finfo); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 104 | return INT64_MAX; |
| 105 | } |
| 106 | |
| 107 | struct sync_pt_info* pinfo = NULL; |
| 108 | uint64_t timestamp = 0; |
| 109 | while ((pinfo = sync_pt_info(finfo, pinfo)) != NULL) { |
| 110 | if (pinfo->timestamp_ns > timestamp) { |
| 111 | timestamp = pinfo->timestamp_ns; |
| 112 | } |
| 113 | } |
| 114 | sync_fence_info_free(finfo); |
| 115 | |
| 116 | return nsecs_t(timestamp); |
| 117 | } |
| 118 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 119 | size_t Fence::getFlattenedSize() const { |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | size_t Fence::getFdCount() const { |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | status_t Fence::flatten(void* buffer, size_t size, int fds[], |
| 128 | size_t count) const { |
| 129 | if (size != 0 || count != 1) { |
| 130 | return BAD_VALUE; |
| 131 | } |
| 132 | |
| 133 | fds[0] = mFenceFd; |
| 134 | return NO_ERROR; |
| 135 | } |
| 136 | |
| 137 | status_t Fence::unflatten(void const* buffer, size_t size, int fds[], |
| 138 | size_t count) { |
| 139 | if (size != 0 || count != 1) { |
| 140 | return BAD_VALUE; |
| 141 | } |
| 142 | if (mFenceFd != -1) { |
| 143 | // Don't unflatten if we already have a valid fd. |
| 144 | return INVALID_OPERATION; |
| 145 | } |
| 146 | |
| 147 | mFenceFd = fds[0]; |
| 148 | return NO_ERROR; |
| 149 | } |
| 150 | |
| 151 | } // namespace android |