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