1 /*
2  * Copyright (C) 2019 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 <utils/String8.h>
21 #include <utils/RefBase.h>
22 
23 typedef int64_t nsecs_t;
24 
25 namespace android {
26 
27 class Fence : public LightRefBase<Fence> {
28 public:
Fence()29     Fence() { }
Fence(int)30     Fence(int) { }
31     static const sp<Fence> NO_FENCE;
32     static constexpr nsecs_t SIGNAL_TIME_PENDING = INT64_MAX;
33     static constexpr nsecs_t SIGNAL_TIME_INVALID = -1;
merge(const char * name,const sp<Fence> & f1,const sp<Fence> & f2)34     static sp<Fence> merge(const char* name, const sp<Fence>& f1, const sp<Fence>& f2) {
35         return NO_FENCE;
36     }
37 
merge(const String8 & name,const sp<Fence> & f1,const sp<Fence> & f2)38     static sp<Fence> merge(const String8& name, const sp<Fence>& f1, const sp<Fence>& f2) {
39         return NO_FENCE;
40     }
41 
42     enum class Status {
43         Invalid,     // Fence is invalid
44         Unsignaled,  // Fence is valid but has not yet signaled
45         Signaled,    // Fence is valid and has signaled
46     };
47 
wait(int timeout)48     status_t wait(int timeout) { return OK; }
49 
waitForever(const char * logname)50     status_t waitForever(const char* logname) { return OK; }
51 
dup()52     int dup() const { return 0; }
53 
getStatus()54     inline Status getStatus() {
55         // The sync_wait call underlying wait() has been measured to be
56         // significantly faster than the sync_fence_info call underlying
57         // getSignalTime(), which might otherwise appear to be the more obvious
58         // way to check whether a fence has signaled.
59         switch (wait(0)) {
60             case NO_ERROR:
61                 return Status::Signaled;
62             case -ETIME:
63                 return Status::Unsignaled;
64             default:
65                 return Status::Invalid;
66         }
67     }
68 };
69 
70 } // namespace android
71 
72 #endif // ANDROID_FENCE_H
73