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