blob: a8460c2e39b491d4620981edbccb2a4330cff18c [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#ifndef ANDROID_FENCE_H
18#define ANDROID_FENCE_H
19
20#include <stdint.h>
Jamie Gennisf25e1832012-06-13 16:31:43 -070021#include <sys/types.h>
22
23#include <ui/ANativeObjectBase.h>
24#include <ui/PixelFormat.h>
25#include <ui/Rect.h>
26#include <utils/Flattenable.h>
27#include <utils/String8.h>
28
29struct ANativeWindowBuffer;
30
31namespace android {
32
33// ===========================================================================
34// Fence
35// ===========================================================================
36
37class Fence
38 : public LightRefBase<Fence>, public Flattenable
39{
40public:
Jesse Hallef194142012-06-14 14:45:17 -070041 static const sp<Fence> NO_FENCE;
Jamie Gennisf25e1832012-06-13 16:31:43 -070042
43 // Construct a new Fence object with an invalid file descriptor. This
44 // should be done when the Fence object will be set up by unflattening
45 // serialized data.
46 Fence();
47
48 // Construct a new Fence object to manage a given fence file descriptor.
49 // When the new Fence object is destructed the file descriptor will be
50 // closed.
51 Fence(int fenceFd);
52
Jesse Hallc777b0b2012-06-28 12:52:05 -070053 // Check whether the Fence has an open fence file descriptor. Most Fence
54 // methods treat an invalid file descriptor just like a valid fence that
55 // is already signalled, so using this is usually not necessary.
56 bool isValid() const { return mFenceFd != -1; }
57
Jamie Gennisf25e1832012-06-13 16:31:43 -070058 // wait waits for up to timeout milliseconds for the fence to signal. If
59 // the fence signals then NO_ERROR is returned. If the timeout expires
60 // before the fence signals then -ETIME is returned. A timeout of
61 // TIMEOUT_NEVER may be used to indicate that the call should wait
62 // indefinitely for the fence to signal.
63 int wait(unsigned int timeout);
64
Jesse Hallba607d52012-10-01 14:05:20 -070065 // waitForever is a convenience function for waiting forever for a fence to
66 // signal (just like wait(TIMEOUT_NEVER)), but issuing an error to the
67 // system log and fence state to the kernel log if the wait lasts longer
68 // than warningTimeout. The logname argument should be a string identifying
69 // the caller and will be included in the log message.
70 int waitForever(unsigned int warningTimeout, const char* logname);
71
Jamie Gennisf25e1832012-06-13 16:31:43 -070072 // TIMEOUT_NEVER may be passed to the wait method to indicate that it
73 // should wait indefinitely for the fence to signal.
Jamie Gennis9f54ac32012-08-21 17:03:18 -070074 enum { TIMEOUT_NEVER = -1 };
Jamie Gennisf25e1832012-06-13 16:31:43 -070075
76 // merge combines two Fence objects, creating a new Fence object that
77 // becomes signaled when both f1 and f2 are signaled (even if f1 or f2 is
78 // destroyed before it becomes signaled). The name argument specifies the
79 // human-readable name to associated with the new Fence object.
80 static sp<Fence> merge(const String8& name, const sp<Fence>& f1,
81 const sp<Fence>& f2);
82
Jesse Hallf9783af2012-06-25 13:54:23 -070083 // Return a duplicate of the fence file descriptor. The caller is
84 // responsible for closing the returned file descriptor. On error, -1 will
85 // be returned and errno will indicate the problem.
86 int dup() const;
87
Jamie Gennisf25e1832012-06-13 16:31:43 -070088 // Flattenable interface
89 size_t getFlattenedSize() const;
90 size_t getFdCount() const;
91 status_t flatten(void* buffer, size_t size,
92 int fds[], size_t count) const;
93 status_t unflatten(void const* buffer, size_t size,
94 int fds[], size_t count);
95
96private:
97 // Only allow instantiation using ref counting.
98 friend class LightRefBase<Fence>;
99 virtual ~Fence();
100
101 // Disallow copying
102 Fence(const Fence& rhs);
103 Fence& operator = (const Fence& rhs);
104 const Fence& operator = (const Fence& rhs) const;
105
106 int mFenceFd;
107};
108
109}; // namespace android
110
111#endif // ANDROID_FENCE_H