1 /*
2 * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *     * Redistributions of source code must retain the above copyright
8 *       notice, this list of conditions and the following disclaimer.
9 *     * Redistributions in binary form must reproduce the above
10 *       copyright notice, this list of conditions and the following
11 *       disclaimer in the documentation and/or other materials provided
12 *       with the distribution.
13 *     * Neither the name of The Linux Foundation nor the names of its
14 *       contributors may be used to endorse or promote products derived
15 *       from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #ifndef __FENCE_H__
31 #define __FENCE_H__
32 
33 #include <core/buffer_sync_handler.h>
34 #include <unistd.h>
35 #include <utility>
36 #include <memory>
37 #include <string>
38 #include <vector>
39 
40 namespace sdm {
41 
42 using std::shared_ptr;
43 using std::string;
44 using std::to_string;
45 
46 class Fence {
47  public:
48   enum class Status : int32_t {
49     kSignaled = 0,
50     kPending
51   };
52 
53   // This class methods allow client to get access to the native file descriptor of fence object
54   // during the scope of this class object. Underlying file descriptor is duped and returned to
55   // the client. Duped file descriptors are closed as soon as scope ends. Client can get access
56   // to multiple fences using the same scoped reference.
57   class ScopedRef {
58    public:
59     ~ScopedRef();
60     int Get(const shared_ptr<Fence> &fence);
61 
62    private:
63     std::vector<int> dup_fds_ = {};
64   };
65 
66   ~Fence();
67 
68   // Must be set once before using any other method of this class.
69   static void Set(BufferSyncHandler *buffer_sync_handler);
70 
71   // Ownership of the file descriptor is transferred to this method.
72   // Client must not close the file descriptor anymore regardless of the object creation status.
73   // nullptr will be retured for invalid fd i.e. -1.
74   static shared_ptr<Fence> Create(int fd, const string &name);
75 
76   // Ownership of returned fd lies with caller. Caller must explicitly close the fd.
77   static int Dup(const shared_ptr<Fence> &fence);
78 
79   static shared_ptr<Fence> Merge(const shared_ptr<Fence> &fence1, const shared_ptr<Fence> &fence2);
80 
81   static shared_ptr<Fence> Merge(const std::vector<shared_ptr<Fence>> &fences,
82                                  bool ignore_signaled);
83 
84   // Wait on null fence will return success.
85   static DisplayError Wait(const shared_ptr<Fence> &fence);
86   static DisplayError Wait(const shared_ptr<Fence> &fence, int timeout);
87 
88   // Status check on null fence will return signaled.
89   static Status GetStatus(const shared_ptr<Fence> &fence);
90 
91   static string GetStr(const shared_ptr<Fence> &fence);
92 
93   // Write all fences info to the output stream.
94   static void Dump(std::ostringstream *os);
95 
96  private:
97   explicit Fence(int fd, const string &name);
98   Fence(const Fence &fence) = delete;
99   Fence& operator=(const Fence &fence) = delete;
100   Fence(Fence &&fence) = delete;
101   Fence& operator=(Fence &&fence) = delete;
102   static int Get(const shared_ptr<Fence> &fence);
103 
104   static BufferSyncHandler *g_buffer_sync_handler_;
105   static std::vector<std::weak_ptr<Fence>> wps_;
106   int fd_ = -1;
107   string name_ = "";
108 };
109 
110 }  // namespace sdm
111 
112 #endif  // __FENCE_H__
113