1 /*
2  * Copyright (C) 2020 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 MINIFENCE_H
18 #define MINIFENCE_H
19 
20 #include <utils/RefBase.h>
21 
22 namespace android {
23 
24 /* MiniFence is a minimal re-implementation of Fence from libui. It exists to
25  * avoid linking the HWC2on1Adapter to libui and satisfy Treble requirements.
26  */
27 class MiniFence : public LightRefBase<MiniFence> {
28 public:
29     static const sp<MiniFence> NO_FENCE;
30 
31     // Construct a new MiniFence object with an invalid file descriptor.
32     MiniFence();
33 
34     // Construct a new MiniFence object to manage a given fence file descriptor.
35     // When the new MiniFence object is destructed the file descriptor will be
36     // closed.
37     explicit MiniFence(int fenceFd);
38 
39     // Not copyable or movable.
40     MiniFence(const MiniFence& rhs) = delete;
41     MiniFence& operator=(const MiniFence& rhs) = delete;
42     MiniFence(MiniFence&& rhs) = delete;
43     MiniFence& operator=(MiniFence&& rhs) = delete;
44 
45     // Return a duplicate of the fence file descriptor. The caller is
46     // responsible for closing the returned file descriptor. On error, -1 will
47     // be returned and errno will indicate the problem.
48     int dup() const;
49 
50 private:
51     // Only allow instantiation using ref counting.
52     friend class LightRefBase<MiniFence>;
53     ~MiniFence();
54 
55     int mFenceFd;
56 
57 };
58 }
59 #endif //MINIFENCE_H
60