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 | |
| 45 | int Fence::wait(unsigned int timeout) { |
| 46 | ATRACE_CALL(); |
| 47 | if (mFenceFd == -1) { |
| 48 | return NO_ERROR; |
| 49 | } |
| 50 | return sync_wait(mFenceFd, timeout); |
| 51 | } |
| 52 | |
| 53 | sp<Fence> Fence::merge(const String8& name, const sp<Fence>& f1, |
| 54 | const sp<Fence>& f2) { |
| 55 | ATRACE_CALL(); |
| 56 | int result = sync_merge(name.string(), f1->mFenceFd, f2->mFenceFd); |
| 57 | if (result == -1) { |
| 58 | ALOGE("merge: sync_merge returned an error: %s (%d)", strerror(-errno), |
| 59 | errno); |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 60 | return NO_FENCE; |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 61 | } |
| 62 | return sp<Fence>(new Fence(result)); |
| 63 | } |
| 64 | |
Jesse Hall | f9783af | 2012-06-25 13:54:23 -0700 | [diff] [blame] | 65 | int Fence::dup() const { |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 66 | if (mFenceFd == -1) { |
| 67 | return -1; |
| 68 | } |
Jesse Hall | f9783af | 2012-06-25 13:54:23 -0700 | [diff] [blame] | 69 | return ::dup(mFenceFd); |
| 70 | } |
| 71 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 72 | size_t Fence::getFlattenedSize() const { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | size_t Fence::getFdCount() const { |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | status_t Fence::flatten(void* buffer, size_t size, int fds[], |
| 81 | size_t count) const { |
| 82 | if (size != 0 || count != 1) { |
| 83 | return BAD_VALUE; |
| 84 | } |
| 85 | |
| 86 | fds[0] = mFenceFd; |
| 87 | return NO_ERROR; |
| 88 | } |
| 89 | |
| 90 | status_t Fence::unflatten(void const* buffer, size_t size, int fds[], |
| 91 | size_t count) { |
| 92 | if (size != 0 || count != 1) { |
| 93 | return BAD_VALUE; |
| 94 | } |
| 95 | if (mFenceFd != -1) { |
| 96 | // Don't unflatten if we already have a valid fd. |
| 97 | return INVALID_OPERATION; |
| 98 | } |
| 99 | |
| 100 | mFenceFd = fds[0]; |
| 101 | return NO_ERROR; |
| 102 | } |
| 103 | |
| 104 | } // namespace android |