2012-06-13 23:31:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "Fence"
|
|
|
|
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
|
|
|
|
//#define LOG_NDEBUG 0
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
// We would eliminate the non-conforming zero-length array, but we can't since
|
|
|
|
// this is effectively included from the Linux kernel
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wzero-length-array"
|
2012-06-13 23:31:43 +00:00
|
|
|
#include <sync/sync.h>
|
2014-11-17 20:03:59 +00:00
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
2012-06-13 23:31:43 +00:00
|
|
|
#include <ui/Fence.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utils/Log.h>
|
|
|
|
#include <utils/Trace.h>
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
2012-12-20 22:05:45 +00:00
|
|
|
const sp<Fence> Fence::NO_FENCE = sp<Fence>(new Fence);
|
2012-06-14 21:45:17 +00:00
|
|
|
|
2012-06-13 23:31:43 +00:00
|
|
|
Fence::Fence() :
|
|
|
|
mFenceFd(-1) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Fence::Fence(int fenceFd) :
|
|
|
|
mFenceFd(fenceFd) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Fence::~Fence() {
|
|
|
|
if (mFenceFd != -1) {
|
|
|
|
close(mFenceFd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 20:03:59 +00:00
|
|
|
status_t Fence::wait(int timeout) {
|
2012-06-13 23:31:43 +00:00
|
|
|
ATRACE_CALL();
|
|
|
|
if (mFenceFd == -1) {
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
2012-10-09 21:38:19 +00:00
|
|
|
int err = sync_wait(mFenceFd, timeout);
|
|
|
|
return err < 0 ? -errno : status_t(NO_ERROR);
|
2012-06-13 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 01:03:22 +00:00
|
|
|
status_t Fence::waitForever(const char* logname) {
|
2012-10-01 21:05:20 +00:00
|
|
|
ATRACE_CALL();
|
|
|
|
if (mFenceFd == -1) {
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
2014-11-17 20:03:59 +00:00
|
|
|
int warningTimeout = 3000;
|
2012-10-01 21:05:20 +00:00
|
|
|
int err = sync_wait(mFenceFd, warningTimeout);
|
2012-10-09 21:38:19 +00:00
|
|
|
if (err < 0 && errno == ETIME) {
|
2012-10-01 21:05:20 +00:00
|
|
|
ALOGE("%s: fence %d didn't signal in %u ms", logname, mFenceFd,
|
|
|
|
warningTimeout);
|
|
|
|
err = sync_wait(mFenceFd, TIMEOUT_NEVER);
|
|
|
|
}
|
2012-10-09 21:38:19 +00:00
|
|
|
return err < 0 ? -errno : status_t(NO_ERROR);
|
2012-10-01 21:05:20 +00:00
|
|
|
}
|
|
|
|
|
2012-06-13 23:31:43 +00:00
|
|
|
sp<Fence> Fence::merge(const String8& name, const sp<Fence>& f1,
|
|
|
|
const sp<Fence>& f2) {
|
|
|
|
ATRACE_CALL();
|
2012-12-20 22:05:45 +00:00
|
|
|
int result;
|
|
|
|
// Merge the two fences. In the case where one of the fences is not a
|
|
|
|
// valid fence (e.g. NO_FENCE) we merge the one valid fence with itself so
|
|
|
|
// that a new fence with the given name is created.
|
|
|
|
if (f1->isValid() && f2->isValid()) {
|
|
|
|
result = sync_merge(name.string(), f1->mFenceFd, f2->mFenceFd);
|
|
|
|
} else if (f1->isValid()) {
|
|
|
|
result = sync_merge(name.string(), f1->mFenceFd, f1->mFenceFd);
|
|
|
|
} else if (f2->isValid()) {
|
|
|
|
result = sync_merge(name.string(), f2->mFenceFd, f2->mFenceFd);
|
|
|
|
} else {
|
|
|
|
return NO_FENCE;
|
|
|
|
}
|
2012-06-13 23:31:43 +00:00
|
|
|
if (result == -1) {
|
2012-07-30 22:10:35 +00:00
|
|
|
status_t err = -errno;
|
|
|
|
ALOGE("merge: sync_merge(\"%s\", %d, %d) returned an error: %s (%d)",
|
|
|
|
name.string(), f1->mFenceFd, f2->mFenceFd,
|
|
|
|
strerror(-err), err);
|
2012-06-14 21:45:17 +00:00
|
|
|
return NO_FENCE;
|
2012-06-13 23:31:43 +00:00
|
|
|
}
|
|
|
|
return sp<Fence>(new Fence(result));
|
|
|
|
}
|
|
|
|
|
2012-06-25 20:54:23 +00:00
|
|
|
int Fence::dup() const {
|
|
|
|
return ::dup(mFenceFd);
|
|
|
|
}
|
|
|
|
|
2012-11-09 03:23:28 +00:00
|
|
|
nsecs_t Fence::getSignalTime() const {
|
|
|
|
if (mFenceFd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sync_fence_info_data* finfo = sync_fence_info(mFenceFd);
|
|
|
|
if (finfo == NULL) {
|
|
|
|
ALOGE("sync_fence_info returned NULL for fd %d", mFenceFd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (finfo->status != 1) {
|
2013-01-14 23:59:32 +00:00
|
|
|
sync_fence_info_free(finfo);
|
2012-11-09 03:23:28 +00:00
|
|
|
return INT64_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sync_pt_info* pinfo = NULL;
|
|
|
|
uint64_t timestamp = 0;
|
|
|
|
while ((pinfo = sync_pt_info(finfo, pinfo)) != NULL) {
|
|
|
|
if (pinfo->timestamp_ns > timestamp) {
|
|
|
|
timestamp = pinfo->timestamp_ns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sync_fence_info_free(finfo);
|
|
|
|
|
|
|
|
return nsecs_t(timestamp);
|
|
|
|
}
|
|
|
|
|
2012-06-13 23:31:43 +00:00
|
|
|
size_t Fence::getFlattenedSize() const {
|
2013-07-30 04:24:40 +00:00
|
|
|
return 1;
|
2012-06-13 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Fence::getFdCount() const {
|
2012-12-20 22:05:45 +00:00
|
|
|
return isValid() ? 1 : 0;
|
2012-06-13 23:31:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 04:24:40 +00:00
|
|
|
status_t Fence::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const {
|
|
|
|
if (size < getFlattenedSize() || count < getFdCount()) {
|
|
|
|
return NO_MEMORY;
|
2012-06-13 23:31:43 +00:00
|
|
|
}
|
2014-11-17 20:03:59 +00:00
|
|
|
FlattenableUtils::write(buffer, size, getFdCount());
|
2012-12-20 22:05:45 +00:00
|
|
|
if (isValid()) {
|
2013-07-30 04:24:40 +00:00
|
|
|
*fds++ = mFenceFd;
|
|
|
|
count--;
|
2012-12-20 22:05:45 +00:00
|
|
|
}
|
2012-06-13 23:31:43 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-07-30 04:24:40 +00:00
|
|
|
status_t Fence::unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count) {
|
2012-06-13 23:31:43 +00:00
|
|
|
if (mFenceFd != -1) {
|
|
|
|
// Don't unflatten if we already have a valid fd.
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
2013-07-30 04:24:40 +00:00
|
|
|
if (size < 1) {
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-04-15 01:43:12 +00:00
|
|
|
uint32_t numFds;
|
2013-07-30 04:24:40 +00:00
|
|
|
FlattenableUtils::read(buffer, size, numFds);
|
|
|
|
|
|
|
|
if (numFds > 1) {
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count < numFds) {
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numFds) {
|
|
|
|
mFenceFd = *fds++;
|
|
|
|
count--;
|
2012-12-20 22:05:45 +00:00
|
|
|
}
|
|
|
|
|
2012-06-13 23:31:43 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace android
|