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 SyncFence : public RefBase { 46 public: 47 explicit SyncFence(int32_t fenceFd); 48 /* When the SyncFence is destroyed, the fd will be closed in UniqueFd */ 49 virtual ~SyncFence(); 50 51 SyncFence(const SyncFence& rhs) = delete; 52 SyncFence& operator=(const SyncFence& rhs) = delete; 53 SyncFence(SyncFence&& rhs) = delete; 54 SyncFence& operator=(SyncFence&& rhs) = delete; 55 56 static const sptr<SyncFence> INVALID_FENCE; 57 static const ns_sec_t INVALID_TIMESTAMP; 58 static const ns_sec_t FENCE_PENDING_TIMESTAMP; 59 int32_t Wait(uint32_t timeout); 60 static sptr<SyncFence> MergeFence(const std::string &name, 61 const sptr<SyncFence>& fence1, const sptr<SyncFence>& fence2); 62 ns_sec_t SyncFileReadTimestamp(); 63 int32_t Dup() const; 64 bool IsValid() const; 65 66 /* this is dangerous, when you use it, do not operator the fd */ 67 int32_t Get() const; 68 69 void WriteToMessageParcel(MessageParcel &parcel); 70 static sptr<SyncFence> ReadFromMessageParcel(MessageParcel &parcel); 71 FenceStatus GetStatus(); 72 static sptr<SyncFence> InvalidFence(); 73 private: 74 std::vector<SyncPointInfo> GetFenceInfo(); 75 76 UniqueFd fenceFd_; 77 static int32_t SyncMerge(const char *name, int32_t fd1, int32_t fd2, int32_t &newFenceFd); 78 }; 79 80 } 81 82 #endif // UTILS_INCLUDE_SYNC_FENCE_H