blob: acc381fcaf360893038738ff415779cd2545d4f4 [file] [log] [blame]
Ramakant Singha1547ee2019-11-20 15:44:42 +05301/*
Dileep Marchyadc8f45d2020-01-13 16:33:50 +05302* Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
Ramakant Singha1547ee2019-11-20 15:44:42 +05303*
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>
Dileep Marchyadc8f45d2020-01-13 16:33:50 +053038#include <vector>
Ramakant Singha1547ee2019-11-20 15:44:42 +053039
40namespace sdm {
41
42using std::shared_ptr;
43using std::string;
44
45class Fence {
46 public:
Dileep Marchyadc8f45d2020-01-13 16:33:50 +053047 enum class Status : int32_t {
48 kSignaled = 0,
49 kPending
50 };
51
52 // This class methods allow client to get access to the native file descriptor of fence object
53 // during the scope of this class object. Underlying file descriptor is duped and returned to
54 // the client. Duped file descriptors are closed as soon as scope ends. Client can get access
55 // to multiple fences using the same scoped reference.
56 class ScopedRef {
57 public:
58 ~ScopedRef();
59 int Get(const shared_ptr<Fence> &fence);
60
61 private:
62 std::vector<int> dup_fds_ = {};
63 };
64
Ramakant Singha1547ee2019-11-20 15:44:42 +053065 ~Fence();
66
Dileep Marchyadc8f45d2020-01-13 16:33:50 +053067 // Must be set once before using any other method of this class.
Ramakant Singha1547ee2019-11-20 15:44:42 +053068 static void Set(BufferSyncHandler *buffer_sync_handler);
69
70 // Ownership of the file descriptor is transferred to this method.
71 // Client must not close the file descriptor anymore regardless of the object creation status.
72 // nullptr will be retured for invalid fd i.e. -1.
73 static shared_ptr<Fence> Create(int fd);
74
75 // Ownership of returned fd lies with caller. Caller must explicitly close the fd.
76 static int Dup(const shared_ptr<Fence> &fence);
77
78 static shared_ptr<Fence> Merge(const shared_ptr<Fence> &fence1, const shared_ptr<Fence> &fence2);
Dileep Marchyadc8f45d2020-01-13 16:33:50 +053079
80 // Wait on null fence will return success.
Ramakant Singha1547ee2019-11-20 15:44:42 +053081 static DisplayError Wait(const shared_ptr<Fence> &fence);
82 static DisplayError Wait(const shared_ptr<Fence> &fence, int timeout);
Dileep Marchyadc8f45d2020-01-13 16:33:50 +053083
84 // Status check on null fence will return signaled.
85 static Status GetStatus(const shared_ptr<Fence> &fence);
86
Ramakant Singha1547ee2019-11-20 15:44:42 +053087 static string GetStr(const shared_ptr<Fence> &fence);
88
89 private:
90 explicit Fence(int fd);
91 Fence(const Fence &fence) = delete;
92 Fence& operator=(const Fence &fence) = delete;
93 Fence(Fence &&fence) = delete;
94 Fence& operator=(Fence &&fence) = delete;
95 static int Get(const shared_ptr<Fence> &fence);
96
97 static BufferSyncHandler *buffer_sync_handler_;
98 int fd_ = -1;
99};
100
101} // namespace sdm
102
103#endif // __FENCE_H__