Changyeon Jo | b927b88 | 2019-11-21 10:16:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #pragma once |
| 18 | |
| 19 | #include "unique_fd.h" |
| 20 | #include "unique_fence.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace automotive { |
| 24 | namespace evs { |
| 25 | namespace V1_1 { |
| 26 | namespace implementation { |
| 27 | |
| 28 | // This is a simple C++ wrapper around the sw_sync interface. It is used to |
| 29 | // create sync fences using timeline semantics. |
| 30 | // |
| 31 | // The timeline has two counters, a fence event counter maintained here in this |
| 32 | // class, and the timeline counter hidden in the driver. The one in the driver |
| 33 | // is initialized to zero when creating the timeline, and the one here is |
| 34 | // initialized to one. The counters are meant to be independently incremented. |
| 35 | // |
| 36 | // When the driver counter is incremented, all fences that were created with |
| 37 | // counts after the previous value of the timeline counter, and before (and |
| 38 | // including) the new value are signaled by the driver. |
| 39 | // |
| 40 | // All fences are signaled if the timeline is also destroyed. |
| 41 | // |
| 42 | // The typical uses of these fences is to acquire a fence for some future point |
| 43 | // on the timeline, and incrementing the local fence event counter to |
| 44 | // distinguish between separate events. Then later when the event actually |
| 45 | // occurs you increment the drivers count. |
| 46 | // |
| 47 | // Since the fences are file descriptors, they can be easily sent to another |
| 48 | // process, which can wait for them to signal without needing to define some |
| 49 | // other IPC mechanism to communicate the event. If the fence is sent well in |
| 50 | // advance, there should be minimal latency too. |
| 51 | // |
| 52 | // Instances of this class cannot be copied, but can be moved. |
| 53 | class UniqueTimeline { |
| 54 | public: |
| 55 | // Initializes the timeline, using the given initial_fence_couter value. |
| 56 | explicit UniqueTimeline(unsigned initial_fence_counter); |
| 57 | |
| 58 | ~UniqueTimeline(); |
| 59 | |
| 60 | // Returns true if it is possible to create timelines. |
| 61 | static bool Supported(); |
| 62 | |
| 63 | // Creates a fence fd using the current value of the fence counter. |
| 64 | // A negative value is returned on error. |
| 65 | UniqueFence CreateFence(const char* name); |
| 66 | |
| 67 | // Increments the counter used when creating fences |
| 68 | void BumpFenceEventCounter() { fence_counter_ += 1; } |
| 69 | |
| 70 | // Increments the drivers version of the counter, signaling any fences in the |
| 71 | // range. |
| 72 | void BumpTimelineEventCounter(); |
| 73 | |
| 74 | private: |
| 75 | void BumpTimelineEventCounter(unsigned); |
| 76 | |
| 77 | // The timeline file descriptor. |
| 78 | UniqueFd fd_{-1}; |
| 79 | |
| 80 | // The counter used when creating fences on the timeline. |
| 81 | unsigned fence_counter_{0}; |
| 82 | |
| 83 | // The effective count for the timeline. The kernel driver has the actual |
| 84 | // value, we just track what it should be. If it ever becomes out of sync, |
| 85 | // it could be a problem for releasing fences on destruction. |
| 86 | unsigned timeline_counter_{0}; |
| 87 | }; |
| 88 | |
| 89 | } // namespace implementation |
| 90 | } // namespace V1_1 |
| 91 | } // namespace evs |
| 92 | } // namespace automotive |
| 93 | } // namespace android |
| 94 | |