blob: 58df24c4e01b92c23a266ec3539f8f93a5a9933b [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
Jamie Gennisf25e1832012-06-13 16:31:43 -070022#include <utils/Flattenable.h>
Brian Anderson175a7202016-10-10 16:52:56 -070023#include <utils/RefBase.h>
Jamie Gennis82dbc742012-11-08 19:23:28 -080024#include <utils/Timers.h>
Jamie Gennisf25e1832012-06-13 16:31:43 -070025
Dan Stoza0eeb6762016-10-13 09:45:00 -070026#include <experimental/optional>
27
Jamie Gennisf25e1832012-06-13 16:31:43 -070028namespace android {
29
Brian Anderson175a7202016-10-10 16:52:56 -070030class String8;
31
Jamie Gennisf25e1832012-06-13 16:31:43 -070032// ===========================================================================
33// Fence
34// ===========================================================================
35
36class Fence
Mathias Agopiane1424282013-07-29 21:24:40 -070037 : public LightRefBase<Fence>, public Flattenable<Fence>
Jamie Gennisf25e1832012-06-13 16:31:43 -070038{
39public:
Jesse Hallef194142012-06-14 14:45:17 -070040 static const sp<Fence> NO_FENCE;
Brian Anderson221de2a2016-09-21 16:53:28 -070041 static constexpr nsecs_t SIGNAL_TIME_PENDING = INT64_MAX;
42 static constexpr nsecs_t SIGNAL_TIME_INVALID = -1;
43 static inline bool isValidTimestamp(nsecs_t time) {
44 return time >= 0 && time < INT64_MAX;
45 }
Jamie Gennisf25e1832012-06-13 16:31:43 -070046
Jamie Gennis82dbc742012-11-08 19:23:28 -080047 // TIMEOUT_NEVER may be passed to the wait method to indicate that it
48 // should wait indefinitely for the fence to signal.
49 enum { TIMEOUT_NEVER = -1 };
50
Jamie Gennisf25e1832012-06-13 16:31:43 -070051 // Construct a new Fence object with an invalid file descriptor. This
52 // should be done when the Fence object will be set up by unflattening
53 // serialized data.
54 Fence();
55
56 // Construct a new Fence object to manage a given fence file descriptor.
57 // When the new Fence object is destructed the file descriptor will be
58 // closed.
Chih-Hung Hsieh65d47872016-09-01 11:39:25 -070059 explicit Fence(int fenceFd);
Jamie Gennisf25e1832012-06-13 16:31:43 -070060
Brian Anderson175a7202016-10-10 16:52:56 -070061 // Not copyable or movable.
62 Fence(const Fence& rhs) = delete;
63 Fence& operator=(const Fence& rhs) = delete;
64 Fence(Fence&& rhs) = delete;
65 Fence& operator=(Fence&& rhs) = delete;
66
Jesse Hallc777b0b2012-06-28 12:52:05 -070067 // Check whether the Fence has an open fence file descriptor. Most Fence
68 // methods treat an invalid file descriptor just like a valid fence that
69 // is already signalled, so using this is usually not necessary.
70 bool isValid() const { return mFenceFd != -1; }
71
Jamie Gennisf25e1832012-06-13 16:31:43 -070072 // wait waits for up to timeout milliseconds for the fence to signal. If
73 // the fence signals then NO_ERROR is returned. If the timeout expires
74 // before the fence signals then -ETIME is returned. A timeout of
75 // TIMEOUT_NEVER may be used to indicate that the call should wait
76 // indefinitely for the fence to signal.
Dan Stoza303b9a52014-11-17 12:03:59 -080077 status_t wait(int timeout);
Jamie Gennisf25e1832012-06-13 16:31:43 -070078
Jesse Hallba607d52012-10-01 14:05:20 -070079 // waitForever is a convenience function for waiting forever for a fence to
80 // signal (just like wait(TIMEOUT_NEVER)), but issuing an error to the
81 // system log and fence state to the kernel log if the wait lasts longer
Mathias Agopianea74d3b2013-05-16 18:03:22 -070082 // than a warning timeout.
83 // The logname argument should be a string identifying
Jesse Hallba607d52012-10-01 14:05:20 -070084 // the caller and will be included in the log message.
Mathias Agopianea74d3b2013-05-16 18:03:22 -070085 status_t waitForever(const char* logname);
Jesse Hallba607d52012-10-01 14:05:20 -070086
Jamie Gennisf25e1832012-06-13 16:31:43 -070087 // merge combines two Fence objects, creating a new Fence object that
88 // becomes signaled when both f1 and f2 are signaled (even if f1 or f2 is
89 // destroyed before it becomes signaled). The name argument specifies the
90 // human-readable name to associated with the new Fence object.
Matthew Bouyackfd4c8c32016-10-07 14:26:47 -070091 static sp<Fence> merge(const char* name, const sp<Fence>& f1,
92 const sp<Fence>& f2);
93
Jamie Gennisf25e1832012-06-13 16:31:43 -070094 static sp<Fence> merge(const String8& name, const sp<Fence>& f1,
95 const sp<Fence>& f2);
96
Jesse Hallf9783af2012-06-25 13:54:23 -070097 // Return a duplicate of the fence file descriptor. The caller is
98 // responsible for closing the returned file descriptor. On error, -1 will
99 // be returned and errno will indicate the problem.
100 int dup() const;
101
Jamie Gennis82dbc742012-11-08 19:23:28 -0800102 // getSignalTime returns the system monotonic clock time at which the
103 // fence transitioned to the signaled state. If the fence is not signaled
Brian Anderson221de2a2016-09-21 16:53:28 -0700104 // then SIGNAL_TIME_PENDING is returned. If the fence is invalid or if an
105 // error occurs then SIGNAL_TIME_INVALID is returned.
Jamie Gennis82dbc742012-11-08 19:23:28 -0800106 nsecs_t getSignalTime() const;
107
Dan Stoza0eeb6762016-10-13 09:45:00 -0700108#if __cplusplus > 201103L
Dan Stoza5736f7d2016-10-12 10:35:17 -0700109 // hasSignaled returns whether the fence has signaled yet. Prefer this to
110 // getSignalTime() or wait() if all you care about is whether the fence has
Dan Stoza0eeb6762016-10-13 09:45:00 -0700111 // signaled. Returns an optional bool, which will have a value if there was
112 // no error.
113 inline std::experimental::optional<bool> hasSignaled() {
Dan Stoza5736f7d2016-10-12 10:35:17 -0700114 // The sync_wait call underlying wait() has been measured to be
115 // significantly faster than the sync_fence_info call underlying
116 // getSignalTime(), which might otherwise appear to be the more obvious
117 // way to check whether a fence has signaled.
Dan Stoza0eeb6762016-10-13 09:45:00 -0700118 switch (wait(0)) {
119 case NO_ERROR:
120 return true;
121 case -ETIME:
122 return false;
123 default:
124 return {};
125 }
Dan Stoza5736f7d2016-10-12 10:35:17 -0700126 }
Dan Stoza0eeb6762016-10-13 09:45:00 -0700127#endif
Dan Stoza5736f7d2016-10-12 10:35:17 -0700128
Jamie Gennisf25e1832012-06-13 16:31:43 -0700129 // Flattenable interface
130 size_t getFlattenedSize() const;
131 size_t getFdCount() const;
Mathias Agopiane1424282013-07-29 21:24:40 -0700132 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
133 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
Jamie Gennisf25e1832012-06-13 16:31:43 -0700134
135private:
136 // Only allow instantiation using ref counting.
137 friend class LightRefBase<Fence>;
Mathias Agopiane1424282013-07-29 21:24:40 -0700138 ~Fence();
Jamie Gennisf25e1832012-06-13 16:31:43 -0700139
Jamie Gennisf25e1832012-06-13 16:31:43 -0700140 int mFenceFd;
141};
142
143}; // namespace android
144
145#endif // ANDROID_FENCE_H