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_ACCESSOR_H
18 #define ANDROID_HARDWARE_MEDIA_BUFFERPOOL_V2_0_ACCESSOR_H
19 
20 #include <android/hardware/media/bufferpool/2.0/IAccessor.h>
21 #include <android/hardware/media/bufferpool/2.0/IObserver.h>
22 #include <bufferpool/BufferPoolTypes.h>
23 #include <hidl/MQDescriptor.h>
24 #include <hidl/Status.h>
25 #include "BufferStatus.h"
26 
27 #include <set>
28 
29 namespace android {
30 namespace hardware {
31 namespace media {
32 namespace bufferpool {
33 namespace V2_0 {
34 namespace implementation {
35 
36 using ::android::hardware::hidl_array;
37 using ::android::hardware::hidl_memory;
38 using ::android::hardware::hidl_string;
39 using ::android::hardware::hidl_vec;
40 using ::android::hardware::Return;
41 using ::android::hardware::Void;
42 using ::android::sp;
43 
44 struct Accessor;
45 struct Connection;
46 
47 /**
48  * Receives death notifications from remote connections.
49  * On death notifications, the connections are closed and used resources
50  * are released.
51  */
52 struct ConnectionDeathRecipient : public hardware::hidl_death_recipient {
53     /**
54      * Registers a newly connected connection from remote processes.
55      */
56     void add(int64_t connectionId, const sp<Accessor> &accessor);
57 
58     /**
59      * Removes a connection.
60      */
61     void remove(int64_t connectionId);
62 
63     void addCookieToConnection(uint64_t cookie, int64_t connectionId);
64 
65     virtual void serviceDied(
66             uint64_t /* cookie */,
67             const wp<::android::hidl::base::V1_0::IBase>& /* who */
68             ) override;
69 
70 private:
71     std::mutex mLock;
72     std::map<uint64_t, std::set<int64_t>>  mCookieToConnections;
73     std::map<int64_t, uint64_t> mConnectionToCookie;
74     std::map<int64_t, const wp<Accessor>> mAccessors;
75 };
76 
77 /**
78  * A buffer pool accessor which enables a buffer pool to communicate with buffer
79  * pool clients. 1:1 correspondense holds between a buffer pool and an accessor.
80  */
81 struct Accessor : public IAccessor {
82     // Methods from ::android::hardware::media::bufferpool::V2_0::IAccessor follow.
83     Return<void> connect(const sp<::android::hardware::media::bufferpool::V2_0::IObserver>& observer, connect_cb _hidl_cb) override;
84 
85     /**
86      * Creates a buffer pool accessor which uses the specified allocator.
87      *
88      * @param allocator buffer allocator.
89      */
90     explicit Accessor(const std::shared_ptr<BufferPoolAllocator> &allocator);
91 
92     /** Destructs a buffer pool accessor. */
93     ~Accessor();
94 
95     /** Returns whether the accessor is valid. */
96     bool isValid();
97 
98     /** Invalidates all buffers which are owned by bufferpool */
99     ResultStatus flush();
100 
101     /** Allocates a buffer from a buffer pool.
102      *
103      * @param connectionId  the connection id of the client.
104      * @param params        the allocation parameters.
105      * @param bufferId      the id of the allocated buffer.
106      * @param handle        the native handle of the allocated buffer.
107      *
108      * @return OK when a buffer is successfully allocated.
109      *         NO_MEMORY when there is no memory.
110      *         CRITICAL_ERROR otherwise.
111      */
112     ResultStatus allocate(
113             ConnectionId connectionId,
114             const std::vector<uint8_t>& params,
115             BufferId *bufferId,
116             const native_handle_t** handle);
117 
118     /**
119      * Fetches a buffer for the specified transaction.
120      *
121      * @param connectionId  the id of receiving connection(client).
122      * @param transactionId the id of the transfer transaction.
123      * @param bufferId      the id of the buffer to be fetched.
124      * @param handle        the native handle of the fetched buffer.
125      *
126      * @return OK when a buffer is successfully fetched.
127      *         NO_MEMORY when there is no memory.
128      *         CRITICAL_ERROR otherwise.
129      */
130     ResultStatus fetch(
131             ConnectionId connectionId,
132             TransactionId transactionId,
133             BufferId bufferId,
134             const native_handle_t** handle);
135 
136     /**
137      * Makes a connection to the buffer pool. The buffer pool client uses the
138      * created connection in order to communicate with the buffer pool. An
139      * FMQ for buffer status message is also created for the client.
140      *
141      * @param observer      client observer for buffer invalidation
142      * @param local         true when a connection request comes from local process,
143      *                      false otherwise.
144      * @param connection    created connection
145      * @param pConnectionId the id of the created connection
146      * @param pMsgId        the id of the recent buffer pool message
147      * @param statusDescPtr FMQ descriptor for shared buffer status message
148      *                      queue between a buffer pool and the client.
149      * @param invDescPtr    FMQ descriptor for buffer invalidation message
150      *                      queue from a buffer pool to the client.
151      *
152      * @return OK when a connection is successfully made.
153      *         NO_MEMORY when there is no memory.
154      *         CRITICAL_ERROR otherwise.
155      */
156     ResultStatus connect(
157             const sp<IObserver>& observer,
158             bool local,
159             sp<Connection> *connection, ConnectionId *pConnectionId,
160             uint32_t *pMsgId,
161             const StatusDescriptor** statusDescPtr,
162             const InvalidationDescriptor** invDescPtr);
163 
164     /**
165      * Closes the specified connection to the client.
166      *
167      * @param connectionId  the id of the connection.
168      *
169      * @return OK when the connection is closed.
170      *         CRITICAL_ERROR otherwise.
171      */
172     ResultStatus close(ConnectionId connectionId);
173 
174     /**
175      * Processes pending buffer status messages and perfoms periodic cache
176      * cleaning.
177      *
178      * @param clearCache    if clearCache is true, it frees all buffers waiting
179      *                      to be recycled.
180      */
181     void cleanUp(bool clearCache);
182 
183     /**
184      * Gets a hidl_death_recipient for remote connection death.
185      */
186     static sp<ConnectionDeathRecipient> getConnectionDeathRecipient();
187 
188     static void createInvalidator();
189 
190     static void createEvictor();
191 
192 private:
193     class Impl;
194     std::shared_ptr<Impl> mImpl;
195 };
196 
197 }  // namespace implementation
198 }  // namespace V2_0
199 }  // namespace bufferpool
200 }  // namespace media
201 }  // namespace hardware
202 }  // namespace android
203 
204 #endif  // ANDROID_HARDWARE_MEDIA_BUFFERPOOL_V2_0_ACCESSOR_H
205