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 // Return the underlying file descriptor without giving up ownership. The 103 // returned file descriptor is only valid for as long as the owning Fence 104 // object lives. (If the situation is unclear, dup() is always a safer 105 // option.) get()106 int get() const { return mFenceFd.get(); } 107 108 // getSignalTime returns the system monotonic clock time at which the 109 // fence transitioned to the signaled state. If the fence is not signaled 110 // then SIGNAL_TIME_PENDING is returned. If the fence is invalid or if an 111 // error occurs then SIGNAL_TIME_INVALID is returned. 112 nsecs_t getSignalTime() const; 113 114 enum class Status { 115 Invalid, // Fence is invalid 116 Unsignaled, // Fence is valid but has not yet signaled 117 Signaled, // Fence is valid and has signaled 118 }; 119 120 // getStatus() returns whether the fence has signaled yet. Prefer this to 121 // getSignalTime() or wait() if all you care about is whether the fence has 122 // signaled. getStatus()123 inline Status getStatus() { 124 // The sync_wait call underlying wait() has been measured to be 125 // significantly faster than the sync_fence_info call underlying 126 // getSignalTime(), which might otherwise appear to be the more obvious 127 // way to check whether a fence has signaled. 128 switch (wait(0)) { 129 case NO_ERROR: 130 return Status::Signaled; 131 case -ETIME: 132 return Status::Unsignaled; 133 default: 134 return Status::Invalid; 135 } 136 } 137 138 // Flattenable interface 139 size_t getFlattenedSize() const; 140 size_t getFdCount() const; 141 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; 142 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); 143 144 private: 145 // Only allow instantiation using ref counting. 146 friend class LightRefBase<Fence>; 147 ~Fence() = default; 148 149 base::unique_fd mFenceFd; 150 }; 151 152 }; // namespace android 153 154 #endif // ANDROID_FENCE_H 155