1 /*
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>
21 
22 #include <android-base/unique_fd.h>
23 #include <utils/Flattenable.h>
24 #include <utils/RefBase.h>
25 #include <utils/Timers.h>
26 
27 namespace android {
28 
29 class String8;
30 
31 // ===========================================================================
32 // Fence
33 // ===========================================================================
34 
35 class Fence
36     : public LightRefBase<Fence>, public Flattenable<Fence>
37 {
38 public:
39     static const sp<Fence> NO_FENCE;
40     static constexpr nsecs_t SIGNAL_TIME_PENDING = INT64_MAX;
41     static constexpr nsecs_t SIGNAL_TIME_INVALID = -1;
isValidTimestamp(nsecs_t time)42     static inline bool isValidTimestamp(nsecs_t time) {
43         return time >= 0 && time < INT64_MAX;
44     }
45 
46     // TIMEOUT_NEVER may be passed to the wait method to indicate that it
47     // should wait indefinitely for the fence to signal.
48     enum { TIMEOUT_NEVER = -1 };
49 
50     // Construct a new Fence object with an invalid file descriptor.  This
51     // should be done when the Fence object will be set up by unflattening
52     // serialized data.
53     Fence() = default;
54 
55     // Construct a new Fence object to manage a given fence file descriptor.
56     // When the new Fence object is destructed the file descriptor will be
57     // closed.
58     explicit Fence(int fenceFd);
59     explicit Fence(base::unique_fd fenceFd);
60 
61     // 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 
67     // 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.
isValid()70     bool isValid() const { return mFenceFd != -1; }
71 
72     // 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.
77     status_t wait(int timeout);
78 
79     // 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
82     // than a warning timeout.
83     // The logname argument should be a string identifying
84     // the caller and will be included in the log message.
85     status_t waitForever(const char* logname);
86 
87     // 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.
91     static sp<Fence> merge(const char* name, const sp<Fence>& f1,
92             const sp<Fence>& f2);
93 
94     static sp<Fence> merge(const String8& name, const sp<Fence>& f1,
95             const sp<Fence>& f2);
96 
97     // 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 
102     // getSignalTime returns the system monotonic clock time at which the
103     // fence transitioned to the signaled state.  If the fence is not signaled
104     // then SIGNAL_TIME_PENDING is returned.  If the fence is invalid or if an
105     // error occurs then SIGNAL_TIME_INVALID is returned.
106     nsecs_t getSignalTime() const;
107 
108     enum class Status {
109         Invalid,     // Fence is invalid
110         Unsignaled,  // Fence is valid but has not yet signaled
111         Signaled,    // Fence is valid and has signaled
112     };
113 
114     // getStatus() returns whether the fence has signaled yet. Prefer this to
115     // getSignalTime() or wait() if all you care about is whether the fence has
116     // signaled.
getStatus()117     inline Status getStatus() {
118         // The sync_wait call underlying wait() has been measured to be
119         // significantly faster than the sync_fence_info call underlying
120         // getSignalTime(), which might otherwise appear to be the more obvious
121         // way to check whether a fence has signaled.
122         switch (wait(0)) {
123             case NO_ERROR:
124                 return Status::Signaled;
125             case -ETIME:
126                 return Status::Unsignaled;
127             default:
128                 return Status::Invalid;
129         }
130     }
131 
132     // Flattenable interface
133     size_t getFlattenedSize() const;
134     size_t getFdCount() const;
135     status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
136     status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
137 
138 private:
139     // Only allow instantiation using ref counting.
140     friend class LightRefBase<Fence>;
141     ~Fence() = default;
142 
143     base::unique_fd mFenceFd;
144 };
145 
146 }; // namespace android
147 
148 #endif // ANDROID_FENCE_H
149