Fence.cpp revision f25e183a70bd631f75dce51e85b7d568472a0cdb
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
27namespace android {
28
29Fence::Fence() :
30    mFenceFd(-1) {
31}
32
33Fence::Fence(int fenceFd) :
34    mFenceFd(fenceFd) {
35}
36
37Fence::~Fence() {
38    if (mFenceFd != -1) {
39        close(mFenceFd);
40    }
41}
42
43int Fence::wait(unsigned int timeout) {
44    ATRACE_CALL();
45    if (mFenceFd == -1) {
46        return NO_ERROR;
47    }
48    return sync_wait(mFenceFd, timeout);
49}
50
51sp<Fence> Fence::merge(const String8& name, const sp<Fence>& f1,
52        const sp<Fence>& f2) {
53    ATRACE_CALL();
54    int result = sync_merge(name.string(), f1->mFenceFd, f2->mFenceFd);
55    if (result == -1) {
56        ALOGE("merge: sync_merge returned an error: %s (%d)", strerror(-errno),
57                errno);
58        return sp<Fence>();
59    }
60    return sp<Fence>(new Fence(result));
61}
62
63size_t Fence::getFlattenedSize() const {
64    return 0;
65}
66
67size_t Fence::getFdCount() const {
68    return 1;
69}
70
71status_t Fence::flatten(void* buffer, size_t size, int fds[],
72        size_t count) const {
73    if (size != 0 || count != 1) {
74        return BAD_VALUE;
75    }
76
77    fds[0] = mFenceFd;
78    return NO_ERROR;
79}
80
81status_t Fence::unflatten(void const* buffer, size_t size, int fds[],
82        size_t count) {
83    if (size != 0 || count != 1) {
84        return BAD_VALUE;
85    }
86    if (mFenceFd != -1) {
87        // Don't unflatten if we already have a valid fd.
88        return INVALID_OPERATION;
89    }
90
91    mFenceFd = fds[0];
92    return NO_ERROR;
93}
94
95} // namespace android
96