libui: add the Fence class

This change adds the Fence class to libui for to wrap the libsync
functionality.

Change-Id: I93a31baeee608b93c14da807a32013dabf783f84
This commit is contained in:
Jamie Gennis 2012-06-13 16:31:43 -07:00 committed by Jesse Hall
parent aa049f0d19
commit f25e183a70
2 changed files with 189 additions and 0 deletions

94
include/ui/Fence.h Normal file
View File

@ -0,0 +1,94 @@
/*
* 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.
*/
#ifndef ANDROID_FENCE_H
#define ANDROID_FENCE_H
#include <stdint.h>
#include <limits.h>
#include <sys/types.h>
#include <ui/ANativeObjectBase.h>
#include <ui/PixelFormat.h>
#include <ui/Rect.h>
#include <utils/Flattenable.h>
#include <utils/String8.h>
struct ANativeWindowBuffer;
namespace android {
// ===========================================================================
// Fence
// ===========================================================================
class Fence
: public LightRefBase<Fence>, public Flattenable
{
public:
// Construct a new Fence object with an invalid file descriptor. This
// should be done when the Fence object will be set up by unflattening
// serialized data.
Fence();
// Construct a new Fence object to manage a given fence file descriptor.
// When the new Fence object is destructed the file descriptor will be
// closed.
Fence(int fenceFd);
// wait waits for up to timeout milliseconds for the fence to signal. If
// the fence signals then NO_ERROR is returned. If the timeout expires
// before the fence signals then -ETIME is returned. A timeout of
// TIMEOUT_NEVER may be used to indicate that the call should wait
// indefinitely for the fence to signal.
int wait(unsigned int timeout);
// TIMEOUT_NEVER may be passed to the wait method to indicate that it
// should wait indefinitely for the fence to signal.
enum { TIMEOUT_NEVER = UINT_MAX };
// merge combines two Fence objects, creating a new Fence object that
// becomes signaled when both f1 and f2 are signaled (even if f1 or f2 is
// destroyed before it becomes signaled). The name argument specifies the
// human-readable name to associated with the new Fence object.
static sp<Fence> merge(const String8& name, const sp<Fence>& f1,
const sp<Fence>& f2);
// Flattenable interface
size_t getFlattenedSize() const;
size_t getFdCount() const;
status_t flatten(void* buffer, size_t size,
int fds[], size_t count) const;
status_t unflatten(void const* buffer, size_t size,
int fds[], size_t count);
private:
// Only allow instantiation using ref counting.
friend class LightRefBase<Fence>;
virtual ~Fence();
// Disallow copying
Fence(const Fence& rhs);
Fence& operator = (const Fence& rhs);
const Fence& operator = (const Fence& rhs) const;
int mFenceFd;
};
}; // namespace android
#endif // ANDROID_FENCE_H

95
libs/ui/Fence.cpp Normal file
View File

@ -0,0 +1,95 @@
/*
* 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
#include <sync/sync.h>
#include <ui/Fence.h>
#include <unistd.h>
#include <utils/Log.h>
#include <utils/Trace.h>
namespace android {
Fence::Fence() :
mFenceFd(-1) {
}
Fence::Fence(int fenceFd) :
mFenceFd(fenceFd) {
}
Fence::~Fence() {
if (mFenceFd != -1) {
close(mFenceFd);
}
}
int Fence::wait(unsigned int timeout) {
ATRACE_CALL();
if (mFenceFd == -1) {
return NO_ERROR;
}
return sync_wait(mFenceFd, timeout);
}
sp<Fence> Fence::merge(const String8& name, const sp<Fence>& f1,
const sp<Fence>& f2) {
ATRACE_CALL();
int result = sync_merge(name.string(), f1->mFenceFd, f2->mFenceFd);
if (result == -1) {
ALOGE("merge: sync_merge returned an error: %s (%d)", strerror(-errno),
errno);
return sp<Fence>();
}
return sp<Fence>(new Fence(result));
}
size_t Fence::getFlattenedSize() const {
return 0;
}
size_t Fence::getFdCount() const {
return 1;
}
status_t Fence::flatten(void* buffer, size_t size, int fds[],
size_t count) const {
if (size != 0 || count != 1) {
return BAD_VALUE;
}
fds[0] = mFenceFd;
return NO_ERROR;
}
status_t Fence::unflatten(void const* buffer, size_t size, int fds[],
size_t count) {
if (size != 0 || count != 1) {
return BAD_VALUE;
}
if (mFenceFd != -1) {
// Don't unflatten if we already have a valid fd.
return INVALID_OPERATION;
}
mFenceFd = fds[0];
return NO_ERROR;
}
} // namespace android