Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 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> |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 21 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 22 | #include <utils/Flattenable.h> |
Brian Anderson | 175a720 | 2016-10-10 16:52:56 -0700 | [diff] [blame] | 23 | #include <utils/RefBase.h> |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 24 | #include <utils/Timers.h> |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 25 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Brian Anderson | 175a720 | 2016-10-10 16:52:56 -0700 | [diff] [blame] | 28 | class String8; |
| 29 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 30 | // =========================================================================== |
| 31 | // Fence |
| 32 | // =========================================================================== |
| 33 | |
| 34 | class Fence |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 35 | : public LightRefBase<Fence>, public Flattenable<Fence> |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 36 | { |
| 37 | public: |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 38 | static const sp<Fence> NO_FENCE; |
Brian Anderson | 221de2a | 2016-09-21 16:53:28 -0700 | [diff] [blame] | 39 | static constexpr nsecs_t SIGNAL_TIME_PENDING = INT64_MAX; |
| 40 | static constexpr nsecs_t SIGNAL_TIME_INVALID = -1; |
| 41 | static inline bool isValidTimestamp(nsecs_t time) { |
| 42 | return time >= 0 && time < INT64_MAX; |
| 43 | } |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 44 | |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 45 | // TIMEOUT_NEVER may be passed to the wait method to indicate that it |
| 46 | // should wait indefinitely for the fence to signal. |
| 47 | enum { TIMEOUT_NEVER = -1 }; |
| 48 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 49 | // Construct a new Fence object with an invalid file descriptor. This |
| 50 | // should be done when the Fence object will be set up by unflattening |
| 51 | // serialized data. |
| 52 | Fence(); |
| 53 | |
| 54 | // Construct a new Fence object to manage a given fence file descriptor. |
| 55 | // When the new Fence object is destructed the file descriptor will be |
| 56 | // closed. |
Chih-Hung Hsieh | 65d4787 | 2016-09-01 11:39:25 -0700 | [diff] [blame] | 57 | explicit Fence(int fenceFd); |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 58 | |
Brian Anderson | 175a720 | 2016-10-10 16:52:56 -0700 | [diff] [blame] | 59 | // Not copyable or movable. |
| 60 | Fence(const Fence& rhs) = delete; |
| 61 | Fence& operator=(const Fence& rhs) = delete; |
| 62 | Fence(Fence&& rhs) = delete; |
| 63 | Fence& operator=(Fence&& rhs) = delete; |
| 64 | |
Jesse Hall | c777b0b | 2012-06-28 12:52:05 -0700 | [diff] [blame] | 65 | // Check whether the Fence has an open fence file descriptor. Most Fence |
| 66 | // methods treat an invalid file descriptor just like a valid fence that |
| 67 | // is already signalled, so using this is usually not necessary. |
| 68 | bool isValid() const { return mFenceFd != -1; } |
| 69 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 70 | // wait waits for up to timeout milliseconds for the fence to signal. If |
| 71 | // the fence signals then NO_ERROR is returned. If the timeout expires |
| 72 | // before the fence signals then -ETIME is returned. A timeout of |
| 73 | // TIMEOUT_NEVER may be used to indicate that the call should wait |
| 74 | // indefinitely for the fence to signal. |
Dan Stoza | 303b9a5 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 75 | status_t wait(int timeout); |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 76 | |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 77 | // waitForever is a convenience function for waiting forever for a fence to |
| 78 | // signal (just like wait(TIMEOUT_NEVER)), but issuing an error to the |
| 79 | // system log and fence state to the kernel log if the wait lasts longer |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 80 | // than a warning timeout. |
| 81 | // The logname argument should be a string identifying |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 82 | // the caller and will be included in the log message. |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 83 | status_t waitForever(const char* logname); |
Jesse Hall | ba607d5 | 2012-10-01 14:05:20 -0700 | [diff] [blame] | 84 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 85 | // merge combines two Fence objects, creating a new Fence object that |
| 86 | // becomes signaled when both f1 and f2 are signaled (even if f1 or f2 is |
| 87 | // destroyed before it becomes signaled). The name argument specifies the |
| 88 | // human-readable name to associated with the new Fence object. |
Matthew Bouyack | fd4c8c3 | 2016-10-07 14:26:47 -0700 | [diff] [blame] | 89 | static sp<Fence> merge(const char* name, const sp<Fence>& f1, |
| 90 | const sp<Fence>& f2); |
| 91 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 92 | static sp<Fence> merge(const String8& name, const sp<Fence>& f1, |
| 93 | const sp<Fence>& f2); |
| 94 | |
Jesse Hall | f9783af | 2012-06-25 13:54:23 -0700 | [diff] [blame] | 95 | // Return a duplicate of the fence file descriptor. The caller is |
| 96 | // responsible for closing the returned file descriptor. On error, -1 will |
| 97 | // be returned and errno will indicate the problem. |
| 98 | int dup() const; |
| 99 | |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 100 | // getSignalTime returns the system monotonic clock time at which the |
| 101 | // fence transitioned to the signaled state. If the fence is not signaled |
Brian Anderson | 221de2a | 2016-09-21 16:53:28 -0700 | [diff] [blame] | 102 | // then SIGNAL_TIME_PENDING is returned. If the fence is invalid or if an |
| 103 | // error occurs then SIGNAL_TIME_INVALID is returned. |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 104 | nsecs_t getSignalTime() const; |
| 105 | |
Dan Stoza | a34320a | 2017-02-16 14:55:03 -0800 | [diff] [blame] | 106 | enum class Status { |
| 107 | Invalid, // Fence is invalid |
| 108 | Unsignaled, // Fence is valid but has not yet signaled |
| 109 | Signaled, // Fence is valid and has signaled |
| 110 | }; |
| 111 | |
| 112 | // getStatus() returns whether the fence has signaled yet. Prefer this to |
Dan Stoza | 5736f7d | 2016-10-12 10:35:17 -0700 | [diff] [blame] | 113 | // getSignalTime() or wait() if all you care about is whether the fence has |
Dan Stoza | a34320a | 2017-02-16 14:55:03 -0800 | [diff] [blame] | 114 | // signaled. |
| 115 | inline Status getStatus() { |
Dan Stoza | 5736f7d | 2016-10-12 10:35:17 -0700 | [diff] [blame] | 116 | // The sync_wait call underlying wait() has been measured to be |
| 117 | // significantly faster than the sync_fence_info call underlying |
| 118 | // getSignalTime(), which might otherwise appear to be the more obvious |
| 119 | // way to check whether a fence has signaled. |
Dan Stoza | 0eeb676 | 2016-10-13 09:45:00 -0700 | [diff] [blame] | 120 | switch (wait(0)) { |
| 121 | case NO_ERROR: |
Dan Stoza | a34320a | 2017-02-16 14:55:03 -0800 | [diff] [blame] | 122 | return Status::Signaled; |
Dan Stoza | 0eeb676 | 2016-10-13 09:45:00 -0700 | [diff] [blame] | 123 | case -ETIME: |
Dan Stoza | a34320a | 2017-02-16 14:55:03 -0800 | [diff] [blame] | 124 | return Status::Unsignaled; |
Dan Stoza | 0eeb676 | 2016-10-13 09:45:00 -0700 | [diff] [blame] | 125 | default: |
Dan Stoza | a34320a | 2017-02-16 14:55:03 -0800 | [diff] [blame] | 126 | return Status::Invalid; |
Dan Stoza | 0eeb676 | 2016-10-13 09:45:00 -0700 | [diff] [blame] | 127 | } |
Dan Stoza | 5736f7d | 2016-10-12 10:35:17 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 130 | // Flattenable interface |
| 131 | size_t getFlattenedSize() const; |
| 132 | size_t getFdCount() const; |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 133 | status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; |
| 134 | status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 135 | |
| 136 | private: |
| 137 | // Only allow instantiation using ref counting. |
| 138 | friend class LightRefBase<Fence>; |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 139 | ~Fence(); |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 140 | |
Jamie Gennis | f25e183 | 2012-06-13 16:31:43 -0700 | [diff] [blame] | 141 | int mFenceFd; |
| 142 | }; |
| 143 | |
| 144 | }; // namespace android |
| 145 | |
| 146 | #endif // ANDROID_FENCE_H |