blob: 8b433016100b509c2f949e754b4ebf72d9b86c43 [file] [log] [blame]
Sungtak Leebbe37b62018-08-29 15:15:48 -07001/*
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>
Sungtak Leed491f1f2018-10-05 15:56:56 -070021#include <android/hardware/media/bufferpool/2.0/IObserver.h>
Sungtak Leebbe37b62018-08-29 15:15:48 -070022#include <bufferpool/BufferPoolTypes.h>
23#include <hidl/MQDescriptor.h>
24#include <hidl/Status.h>
25#include "BufferStatus.h"
26
27#include <set>
28
29namespace android {
30namespace hardware {
31namespace media {
32namespace bufferpool {
33namespace V2_0 {
34namespace implementation {
35
36using ::android::hardware::hidl_array;
37using ::android::hardware::hidl_memory;
38using ::android::hardware::hidl_string;
39using ::android::hardware::hidl_vec;
40using ::android::hardware::Return;
41using ::android::hardware::Void;
42using ::android::sp;
43
44struct Accessor;
45struct 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 */
52struct 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
70private:
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 */
81struct Accessor : public IAccessor {
82 // Methods from ::android::hardware::media::bufferpool::V2_0::IAccessor follow.
Sungtak Leed491f1f2018-10-05 15:56:56 -070083 Return<void> connect(const sp<::android::hardware::media::bufferpool::V2_0::IObserver>& observer, connect_cb _hidl_cb) override;
Sungtak Leebbe37b62018-08-29 15:15:48 -070084
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
Sungtak Leec7f9e2c2018-09-14 16:23:40 -070098 /** Invalidates all buffers which are owned by bufferpool */
99 ResultStatus flush();
100
Sungtak Leebbe37b62018-08-29 15:15:48 -0700101 /** 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 *
Sungtak Leec7f9e2c2018-09-14 16:23:40 -0700141 * @param observer client observer for buffer invalidation
Sungtak Leebbe37b62018-08-29 15:15:48 -0700142 * @param local true when a connection request comes from local process,
143 * false otherwise.
Sungtak Leec7f9e2c2018-09-14 16:23:40 -0700144 * @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.
Sungtak Leebbe37b62018-08-29 15:15:48 -0700151 *
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(
Sungtak Leec7f9e2c2018-09-14 16:23:40 -0700157 const sp<IObserver>& observer,
158 bool local,
Sungtak Leebbe37b62018-08-29 15:15:48 -0700159 sp<Connection> *connection, ConnectionId *pConnectionId,
Sungtak Leec7f9e2c2018-09-14 16:23:40 -0700160 uint32_t *pMsgId,
161 const StatusDescriptor** statusDescPtr,
162 const InvalidationDescriptor** invDescPtr);
Sungtak Leebbe37b62018-08-29 15:15:48 -0700163
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
Sungtak Leed3128382018-11-07 17:30:37 -0800188 static void createInvalidator();
189
Sungtak Lee7651a272020-04-27 00:16:50 -0700190 static void createEvictor();
191
Sungtak Leebbe37b62018-08-29 15:15:48 -0700192private:
193 class Impl;
Sungtak Leec7f9e2c2018-09-14 16:23:40 -0700194 std::shared_ptr<Impl> mImpl;
Sungtak Leebbe37b62018-08-29 15:15:48 -0700195};
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