1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef UTILS_INCLUDE_SYNC_FENCE_H 17 #define UTILS_INCLUDE_SYNC_FENCE_H 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <string> 22 #include <mutex> 23 #include <vector> 24 25 #include <refbase.h> 26 #include <message_parcel.h> 27 #include <unique_fd.h> 28 29 namespace OHOS { 30 31 using ns_sec_t = int64_t; 32 33 // same to linux/sync_file define 34 enum FenceStatus { 35 ERROR = -1, 36 ACTIVE = 0, 37 SIGNALED = 1 38 }; 39 40 struct SyncPointInfo { 41 uint64_t timestampNs; 42 FenceStatus status; 43 }; 44 45 class SyncTimeline : public RefBase { 46 public: 47 SyncTimeline() noexcept; 48 ~SyncTimeline() noexcept; 49 50 SyncTimeline(const SyncTimeline& rhs) = delete; 51 SyncTimeline& operator=(const SyncTimeline& rhs) = delete; 52 SyncTimeline(SyncTimeline&& rhs) = delete; 53 SyncTimeline& operator=(SyncTimeline&& rhs) = delete; 54 55 bool IsValid(); 56 int32_t IncreaseSyncPoint(uint32_t step = 1); 57 int32_t GenerateFence(std::string name, uint32_t point); 58 int32_t Dup() const; 59 /* this is dangerous, when you use it, do not operator the fd */ 60 int32_t Get() const; 61 62 private: 63 int32_t timeLineFd_ = -1; 64 int32_t timeLineNextPoint = 0; 65 bool isValid_ = false; 66 }; 67 68 class SyncFence : public RefBase { 69 public: 70 explicit SyncFence(int32_t fenceFd); 71 /* When the SyncFence is destroyed, the fd will be closed in UniqueFd */ 72 virtual ~SyncFence(); 73 74 SyncFence(const SyncFence& rhs) = delete; 75 SyncFence& operator=(const SyncFence& rhs) = delete; 76 SyncFence(SyncFence&& rhs) = delete; 77 SyncFence& operator=(SyncFence&& rhs) = delete; 78 79 static const sptr<SyncFence> INVALID_FENCE; 80 static const ns_sec_t INVALID_TIMESTAMP; 81 static const ns_sec_t FENCE_PENDING_TIMESTAMP; 82 int32_t Wait(uint32_t timeout); 83 static sptr<SyncFence> MergeFence(const std::string &name, 84 const sptr<SyncFence>& fence1, const sptr<SyncFence>& fence2); 85 ns_sec_t SyncFileReadTimestamp(); 86 int32_t Dup() const; 87 bool IsValid() const; 88 89 /* this is dangerous, when you use it, do not operator the fd */ 90 int32_t Get() const; 91 92 void WriteToMessageParcel(MessageParcel &parcel); 93 static sptr<SyncFence> ReadFromMessageParcel(MessageParcel &parcel); 94 95 private: 96 std::vector<SyncPointInfo> GetFenceInfo(); 97 FenceStatus GetStatus(); 98 99 UniqueFd fenceFd_; 100 static int SyncMerge(const char *name, int fd1, int fd2); 101 }; 102 103 } 104 105 #endif // UTILS_INCLUDE_SYNC_FENCE_H