blob: d2dbad276add8a2dd6e19248f281f44f86c07cdb [file] [log] [blame]
Jamie Gennisf25e1832012-06-13 16:31:43 -07001/*
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
Jesse Hallef194142012-06-14 14:45:17 -070029const sp<Fence> Fence::NO_FENCE = sp<Fence>();
30
Jamie Gennisf25e1832012-06-13 16:31:43 -070031Fence::Fence() :
32 mFenceFd(-1) {
33}
34
35Fence::Fence(int fenceFd) :
36 mFenceFd(fenceFd) {
37}
38
39Fence::~Fence() {
40 if (mFenceFd != -1) {
41 close(mFenceFd);
42 }
43}
44
45int 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
Jesse Hallba607d52012-10-01 14:05:20 -070053int Fence::waitForever(unsigned int warningTimeout, const char* logname) {
54 ATRACE_CALL();
55 if (mFenceFd == -1) {
56 return NO_ERROR;
57 }
58 int err = sync_wait(mFenceFd, warningTimeout);
59 if (err == -ETIME) {
60 ALOGE("%s: fence %d didn't signal in %u ms", logname, mFenceFd,
61 warningTimeout);
62 err = sync_wait(mFenceFd, TIMEOUT_NEVER);
63 }
64 return err;
65}
66
Jamie Gennisf25e1832012-06-13 16:31:43 -070067sp<Fence> Fence::merge(const String8& name, const sp<Fence>& f1,
68 const sp<Fence>& f2) {
69 ATRACE_CALL();
70 int result = sync_merge(name.string(), f1->mFenceFd, f2->mFenceFd);
71 if (result == -1) {
Mathias Agopiand83d67b2012-07-30 15:10:35 -070072 status_t err = -errno;
73 ALOGE("merge: sync_merge(\"%s\", %d, %d) returned an error: %s (%d)",
74 name.string(), f1->mFenceFd, f2->mFenceFd,
75 strerror(-err), err);
Jesse Hallef194142012-06-14 14:45:17 -070076 return NO_FENCE;
Jamie Gennisf25e1832012-06-13 16:31:43 -070077 }
78 return sp<Fence>(new Fence(result));
79}
80
Jesse Hallf9783af2012-06-25 13:54:23 -070081int Fence::dup() const {
Jesse Hallc777b0b2012-06-28 12:52:05 -070082 if (mFenceFd == -1) {
83 return -1;
84 }
Jesse Hallf9783af2012-06-25 13:54:23 -070085 return ::dup(mFenceFd);
86}
87
Jamie Gennisf25e1832012-06-13 16:31:43 -070088size_t Fence::getFlattenedSize() const {
89 return 0;
90}
91
92size_t Fence::getFdCount() const {
93 return 1;
94}
95
96status_t Fence::flatten(void* buffer, size_t size, int fds[],
97 size_t count) const {
98 if (size != 0 || count != 1) {
99 return BAD_VALUE;
100 }
101
102 fds[0] = mFenceFd;
103 return NO_ERROR;
104}
105
106status_t Fence::unflatten(void const* buffer, size_t size, int fds[],
107 size_t count) {
108 if (size != 0 || count != 1) {
109 return BAD_VALUE;
110 }
111 if (mFenceFd != -1) {
112 // Don't unflatten if we already have a valid fd.
113 return INVALID_OPERATION;
114 }
115
116 mFenceFd = fds[0];
117 return NO_ERROR;
118}
119
120} // namespace android