blob: a718da1db9e417493b05e09e09a40b5e44ddab9a [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
98 /** Allocates a buffer from a buffer pool.
99 *
100 * @param connectionId the connection id of the client.
101 * @param params the allocation parameters.
102 * @param bufferId the id of the allocated buffer.
103 * @param handle the native handle of the allocated buffer.
104 *
105 * @return OK when a buffer is successfully allocated.
106 * NO_MEMORY when there is no memory.
107 * CRITICAL_ERROR otherwise.
108 */
109 ResultStatus allocate(
110 ConnectionId connectionId,
111 const std::vector<uint8_t>& params,
112 BufferId *bufferId,
113 const native_handle_t** handle);
114
115 /**
116 * Fetches a buffer for the specified transaction.
117 *
118 * @param connectionId the id of receiving connection(client).
119 * @param transactionId the id of the transfer transaction.
120 * @param bufferId the id of the buffer to be fetched.
121 * @param handle the native handle of the fetched buffer.
122 *
123 * @return OK when a buffer is successfully fetched.
124 * NO_MEMORY when there is no memory.
125 * CRITICAL_ERROR otherwise.
126 */
127 ResultStatus fetch(
128 ConnectionId connectionId,
129 TransactionId transactionId,
130 BufferId bufferId,
131 const native_handle_t** handle);
132
133 /**
134 * Makes a connection to the buffer pool. The buffer pool client uses the
135 * created connection in order to communicate with the buffer pool. An
136 * FMQ for buffer status message is also created for the client.
137 *
138 * @param connection created connection
139 * @param pConnectionId the id of the created connection
140 * @param fmqDescPtr FMQ descriptor for shared buffer status message
141 * queue between a buffer pool and the client.
142 * @param local true when a connection request comes from local process,
143 * false otherwise.
144 *
145 * @return OK when a connection is successfully made.
146 * NO_MEMORY when there is no memory.
147 * CRITICAL_ERROR otherwise.
148 */
149 ResultStatus connect(
150 sp<Connection> *connection, ConnectionId *pConnectionId,
Sungtak Lee1cb0ccb2018-09-05 14:47:36 -0700151 const StatusDescriptor** fmqDescPtr, bool local);
Sungtak Leebbe37b62018-08-29 15:15:48 -0700152
153 /**
154 * Closes the specified connection to the client.
155 *
156 * @param connectionId the id of the connection.
157 *
158 * @return OK when the connection is closed.
159 * CRITICAL_ERROR otherwise.
160 */
161 ResultStatus close(ConnectionId connectionId);
162
163 /**
164 * Processes pending buffer status messages and perfoms periodic cache
165 * cleaning.
166 *
167 * @param clearCache if clearCache is true, it frees all buffers waiting
168 * to be recycled.
169 */
170 void cleanUp(bool clearCache);
171
172 /**
173 * Gets a hidl_death_recipient for remote connection death.
174 */
175 static sp<ConnectionDeathRecipient> getConnectionDeathRecipient();
176
177private:
178 class Impl;
179 std::unique_ptr<Impl> mImpl;
180};
181
182} // namespace implementation
183} // namespace V2_0
184} // namespace bufferpool
185} // namespace media
186} // namespace hardware
187} // namespace android
188
189#endif // ANDROID_HARDWARE_MEDIA_BUFFERPOOL_V2_0_ACCESSOR_H