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 | |
| 21 | #include <sync/sync.h> |
| 22 | #include <ui/Fence.h> |
| 23 | #include <unistd.h> |
| 24 | #include <utils/Log.h> |
| 25 | #include <utils/Trace.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 29 | const sp<Fence> Fence::NO_FENCE = sp<Fence>(); |
| 30 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 31 | Fence::Fence() : |
| 32 | mFenceFd(-1) { |
| 33 | } |
| 34 | |
| 35 | Fence::Fence(int fenceFd) : |
| 36 | mFenceFd(fenceFd) { |
| 37 | } |
| 38 | |
| 39 | Fence::~Fence() { |
| 40 | if (mFenceFd != -1) { |
| 41 | close(mFenceFd); |
| 42 | } |
| 43 | } |
| 44 | |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame^] | 45 | status_t Fence::wait(unsigned int timeout) { |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 46 | ATRACE_CALL(); |
| 47 | if (mFenceFd == -1) { |
| 48 | return NO_ERROR; |
| 49 | } |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame^] | 50 | int err = sync_wait(mFenceFd, timeout); |
| 51 | return err < 0 ? -errno : status_t(NO_ERROR); |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame^] | 54 | status_t Fence::waitForever(unsigned int warningTimeout, const char* logname) { |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 55 | ATRACE_CALL(); |
| 56 | if (mFenceFd == -1) { |
| 57 | return NO_ERROR; |
| 58 | } |
| 59 | int err = sync_wait(mFenceFd, warningTimeout); |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame^] | 60 | if (err < 0 && errno == ETIME) { |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 61 | ALOGE("%s: fence %d didn't signal in %u ms", logname, mFenceFd, |
| 62 | warningTimeout); |
| 63 | err = sync_wait(mFenceFd, TIMEOUT_NEVER); |
| 64 | } |
Mathias Agopian | b5c9dcd | 2012-10-09 14:38:19 -0700 | [diff] [blame^] | 65 | return err < 0 ? -errno : status_t(NO_ERROR); |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 68 | sp<Fence> Fence::merge(const String8& name, const sp<Fence>& f1, |
| 69 | const sp<Fence>& f2) { |
| 70 | ATRACE_CALL(); |
| 71 | int result = sync_merge(name.string(), f1->mFenceFd, f2->mFenceFd); |
| 72 | if (result == -1) { |
Mathias Agopian | d83d67b | 2012-07-30 15:10:35 -0700 | [diff] [blame] | 73 | status_t err = -errno; |
| 74 | ALOGE("merge: sync_merge(\"%s\", %d, %d) returned an error: %s (%d)", |
| 75 | name.string(), f1->mFenceFd, f2->mFenceFd, |
| 76 | strerror(-err), err); |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 77 | return NO_FENCE; |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 78 | } |
| 79 | return sp<Fence>(new Fence(result)); |
| 80 | } |
| 81 | |
Jesse Hall | f9783af | 2012-06-25 13:54:23 -0700 | [diff] [blame] | 82 | int Fence::dup() const { |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 83 | if (mFenceFd == -1) { |
| 84 | return -1; |
| 85 | } |
Jesse Hall | f9783af | 2012-06-25 13:54:23 -0700 | [diff] [blame] | 86 | return ::dup(mFenceFd); |
| 87 | } |
| 88 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 89 | size_t Fence::getFlattenedSize() const { |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | size_t Fence::getFdCount() const { |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | status_t Fence::flatten(void* buffer, size_t size, int fds[], |
| 98 | size_t count) const { |
| 99 | if (size != 0 || count != 1) { |
| 100 | return BAD_VALUE; |
| 101 | } |
| 102 | |
| 103 | fds[0] = mFenceFd; |
| 104 | return NO_ERROR; |
| 105 | } |
| 106 | |
| 107 | status_t Fence::unflatten(void const* buffer, size_t size, int fds[], |
| 108 | size_t count) { |
| 109 | if (size != 0 || count != 1) { |
| 110 | return BAD_VALUE; |
| 111 | } |
| 112 | if (mFenceFd != -1) { |
| 113 | // Don't unflatten if we already have a valid fd. |
| 114 | return INVALID_OPERATION; |
| 115 | } |
| 116 | |
| 117 | mFenceFd = fds[0]; |
| 118 | return NO_ERROR; |
| 119 | } |
| 120 | |
| 121 | } // namespace android |