• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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_HARDWARE_MEDIA_BUFFERPOOL_V2_0_CLIENTMANAGER_H
18 #define ANDROID_HARDWARE_MEDIA_BUFFERPOOL_V2_0_CLIENTMANAGER_H
19 
20 #include <android/hardware/media/bufferpool/2.0/IClientManager.h>
21 #include <hidl/MQDescriptor.h>
22 #include <hidl/Status.h>
23 #include <memory>
24 #include "BufferPoolTypes.h"
25 
26 namespace android {
27 namespace hardware {
28 namespace media {
29 namespace bufferpool {
30 namespace V2_0 {
31 namespace implementation {
32 
33 using ::android::hardware::hidl_array;
34 using ::android::hardware::hidl_memory;
35 using ::android::hardware::hidl_string;
36 using ::android::hardware::hidl_vec;
37 using ::android::hardware::media::bufferpool::V2_0::IAccessor;
38 using ::android::hardware::media::bufferpool::V2_0::ResultStatus;
39 using ::android::hardware::Return;
40 using ::android::hardware::Void;
41 using ::android::sp;
42 
43 struct ClientManager : public IClientManager {
44     // Methods from ::android::hardware::media::bufferpool::V2_0::IClientManager follow.
45     Return<void> registerSender(const sp<::android::hardware::media::bufferpool::V2_0::IAccessor>& bufferPool, registerSender_cb _hidl_cb) override;
46 
47     /** Gets an instance. */
48     static sp<ClientManager> getInstance();
49 
50     /**
51      * Creates a local connection with a newly created buffer pool.
52      *
53      * @param allocator     for new buffer allocation.
54      * @param pConnectionId Id of the created connection. This is
55      *                      system-wide unique.
56      *
57      * @return OK when a buffer pool and a local connection is successfully
58      *         created.
59      *         NO_MEMORY when there is no memory.
60      *         CRITICAL_ERROR otherwise.
61      */
62     ResultStatus create(const std::shared_ptr<BufferPoolAllocator> &allocator,
63                         ConnectionId *pConnectionId);
64 
65     /**
66      * Register a created connection as sender for remote process.
67      *
68      * @param receiver      The remote receiving process.
69      * @param senderId      A local connection which will send buffers to.
70      * @param receiverId    Id of the created receiving connection on the receiver
71      *                      process.
72      *
73      * @return OK when the receiving connection is successfully created on the
74      *         receiver process.
75      *         NOT_FOUND when the sender connection was not found.
76      *         ALREADY_EXISTS the receiving connection is already made.
77      *         CRITICAL_ERROR otherwise.
78      */
79     ResultStatus registerSender(const sp<IClientManager> &receiver,
80                                 ConnectionId senderId,
81                                 ConnectionId *receiverId);
82 
83     /**
84      * Closes the specified connection.
85      *
86      * @param connectionId  The id of the connection.
87      *
88      * @return OK when the connection is closed.
89      *         NOT_FOUND when the specified connection was not found.
90      *         CRITICAL_ERROR otherwise.
91      */
92     ResultStatus close(ConnectionId connectionId);
93 
94     /**
95      * Evicts cached allocations. If it's local connection, release the
96      * previous allocations and do not recycle current active allocations.
97      *
98      * @param connectionId The id of the connection.
99      *
100      * @return OK when the connection is resetted.
101      *         NOT_FOUND when the specified connection was not found.
102      *         CRITICAL_ERROR otherwise.
103      */
104     ResultStatus flush(ConnectionId connectionId);
105 
106     /**
107      * Allocates a buffer from the specified connection.
108      *
109      * @param connectionId  The id of the connection.
110      * @param params        The allocation parameters.
111      * @param handle        The native handle to the allocated buffer. handle
112      *                      should be cloned before use.
113      * @param buffer        The allocated buffer.
114      *
115      * @return OK when a buffer was allocated successfully.
116      *         NOT_FOUND when the specified connection was not found.
117      *         NO_MEMORY when there is no memory.
118      *         CRITICAL_ERROR otherwise.
119      */
120     ResultStatus allocate(ConnectionId connectionId,
121                           const std::vector<uint8_t> &params,
122                           native_handle_t **handle,
123                           std::shared_ptr<BufferPoolData> *buffer);
124 
125     /**
126      * Receives a buffer for the transaction.
127      *
128      * @param connectionId  The id of the receiving connection.
129      * @param transactionId The id for the transaction.
130      * @param bufferId      The id for the buffer.
131      * @param timestampUs   The timestamp of the buffer is being sent.
132      * @param handle        The native handle to the allocated buffer. handle
133      *                      should be cloned before use.
134      * @param buffer        The received buffer.
135      *
136      * @return OK when a buffer was received successfully.
137      *         NOT_FOUND when the specified connection was not found.
138      *         NO_MEMORY when there is no memory.
139      *         CRITICAL_ERROR otherwise.
140      */
141     ResultStatus receive(ConnectionId connectionId,
142                          TransactionId transactionId,
143                          BufferId bufferId,
144                          int64_t timestampUs,
145                           native_handle_t **handle,
146                          std::shared_ptr<BufferPoolData> *buffer);
147 
148     /**
149      * Posts a buffer transfer transaction to the buffer pool. Sends a buffer
150      * to other remote clients(connection) after this call has been succeeded.
151      *
152      * @param receiverId    The id of the receiving connection.
153      * @param buffer        to transfer
154      * @param transactionId Id of the transfer transaction.
155      * @param timestampUs   The timestamp of the buffer transaction is being
156      *                      posted.
157      *
158      * @return OK when a buffer transaction was posted successfully.
159      *         NOT_FOUND when the sending connection was not found.
160      *         CRITICAL_ERROR otherwise.
161      */
162     ResultStatus postSend(ConnectionId receiverId,
163                           const std::shared_ptr<BufferPoolData> &buffer,
164                           TransactionId *transactionId,
165                           int64_t *timestampUs);
166 
167     /**
168      *  Time out inactive lingering connections and close.
169      */
170     void cleanUp();
171 
172     /** Destructs the manager of buffer pool clients.  */
173     ~ClientManager();
174 private:
175     static sp<ClientManager> sInstance;
176     static std::mutex sInstanceLock;
177 
178     class Impl;
179     const std::unique_ptr<Impl> mImpl;
180 
181     ClientManager();
182 };
183 
184 }  // namespace implementation
185 }  // namespace V2_0
186 }  // namespace bufferpool
187 }  // namespace media
188 }  // namespace hardware
189 }  // namespace android
190 
191 #endif  // ANDROID_HARDWARE_MEDIA_BUFFERPOOL_V2_0_CLIENTMANAGER_H
192