blob: fa658382a34dcf971e771d02a205132fdf4aed32 [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_BUFFERSTATUS_H
18#define ANDROID_HARDWARE_MEDIA_BUFFERPOOL_V2_0_BUFFERSTATUS_H
19
20#include <android/hardware/media/bufferpool/2.0/types.h>
21#include <bufferpool/BufferPoolTypes.h>
22#include <fmq/MessageQueue.h>
23#include <hidl/MQDescriptor.h>
24#include <hidl/Status.h>
25#include <memory>
26#include <mutex>
27#include <vector>
28#include <list>
29
30namespace android {
31namespace hardware {
32namespace media {
33namespace bufferpool {
34namespace V2_0 {
35namespace implementation {
36
37/** Returns monotonic timestamp in Us since fixed point in time. */
38int64_t getTimestampNow();
39
Sungtak Leec7f9e2c2018-09-14 16:23:40 -070040bool isMessageLater(uint32_t curMsgId, uint32_t prevMsgId);
41
42bool isBufferInRange(BufferId from, BufferId to, BufferId bufferId);
43
Sungtak Leebbe37b62018-08-29 15:15:48 -070044/**
Sungtak Leec7f9e2c2018-09-14 16:23:40 -070045 * A collection of buffer status message FMQ for a buffer pool. buffer
46 * ownership/status change messages are sent via the FMQs from the clients.
Sungtak Leebbe37b62018-08-29 15:15:48 -070047 */
48class BufferStatusObserver {
49private:
50 std::map<ConnectionId, std::unique_ptr<BufferStatusQueue>>
51 mBufferStatusQueues;
52
53public:
Sungtak Leec7f9e2c2018-09-14 16:23:40 -070054 /** Creates a buffer status message FMQ for the specified
55 * connection(client).
Sungtak Leebbe37b62018-08-29 15:15:48 -070056 *
57 * @param connectionId connection Id of the specified client.
58 * @param fmqDescPtr double ptr of created FMQ's descriptor.
59 *
60 * @return OK if FMQ is created successfully.
61 * NO_MEMORY when there is no memory.
62 * CRITICAL_ERROR otherwise.
63 */
Sungtak Lee1cb0ccb2018-09-05 14:47:36 -070064 ResultStatus open(ConnectionId id, const StatusDescriptor** fmqDescPtr);
Sungtak Leebbe37b62018-08-29 15:15:48 -070065
Sungtak Leec7f9e2c2018-09-14 16:23:40 -070066 /** Closes a buffer status message FMQ for the specified
67 * connection(client).
Sungtak Leebbe37b62018-08-29 15:15:48 -070068 *
69 * @param connectionId connection Id of the specified client.
70 *
71 * @return OK if the specified connection is closed successfully.
72 * CRITICAL_ERROR otherwise.
73 */
74 ResultStatus close(ConnectionId id);
75
76 /** Retrieves all pending FMQ buffer status messages from clients.
77 *
78 * @param messages retrieved pending messages.
79 */
80 void getBufferStatusChanges(std::vector<BufferStatusMessage> &messages);
81};
82
83/**
Sungtak Leec7f9e2c2018-09-14 16:23:40 -070084 * A buffer status message FMQ for a buffer pool client. Buffer ownership/status
85 * change messages are sent via the fmq to the buffer pool.
Sungtak Leebbe37b62018-08-29 15:15:48 -070086 */
87class BufferStatusChannel {
88private:
89 bool mValid;
90 std::unique_ptr<BufferStatusQueue> mBufferStatusQueue;
91
92public:
93 /**
Sungtak Leec7f9e2c2018-09-14 16:23:40 -070094 * Connects to a buffer status message FMQ from a descriptor of
95 * the created FMQ.
Sungtak Leebbe37b62018-08-29 15:15:48 -070096 *
97 * @param fmqDesc Descriptor of the created FMQ.
98 */
Sungtak Lee1cb0ccb2018-09-05 14:47:36 -070099 BufferStatusChannel(const StatusDescriptor &fmqDesc);
Sungtak Leebbe37b62018-08-29 15:15:48 -0700100
101 /** Returns whether the FMQ is connected successfully. */
102 bool isValid();
103
104 /** Returns whether the FMQ needs to be synced from the buffer pool */
105 bool needsSync();
106
107 /**
108 * Posts a buffer release message to the buffer pool.
109 *
110 * @param connectionId connection Id of the client.
111 * @param pending currently pending buffer release messages.
112 * @param posted posted buffer release messages.
113 */
114 void postBufferRelease(
115 ConnectionId connectionId,
116 std::list<BufferId> &pending, std::list<BufferId> &posted);
117
118 /**
119 * Posts a buffer status message regarding the specified buffer
120 * transfer transaction.
121 *
122 * @param transactionId Id of the specified transaction.
123 * @param bufferId buffer Id of the specified transaction.
124 * @param status new status of the buffer.
125 * @param connectionId connection Id of the client.
126 * @param targetId connection Id of the receiver(only when the sender
127 * posts a status message).
128 * @param pending currently pending buffer release messages.
129 * @param posted posted buffer release messages.
130 *
131 * @return {@code true} when the specified message is posted,
132 * {@code false} otherwise.
133 */
134 bool postBufferStatusMessage(
135 TransactionId transactionId,
136 BufferId bufferId,
137 BufferStatus status,
138 ConnectionId connectionId,
139 ConnectionId targetId,
140 std::list<BufferId> &pending, std::list<BufferId> &posted);
Sungtak Leec7f9e2c2018-09-14 16:23:40 -0700141
142 /**
143 * Posts a buffer invaliadation messge to the buffer pool.
144 *
145 * @param connectionId connection Id of the client.
146 * @param invalidateId invalidation ack to the buffer pool.
147 * if invalidation id is zero, the ack will not be
148 * posted.
149 * @param invalidated sets {@code true} only when the invalidation ack is
150 * posted.
151 */
152 void postBufferInvalidateAck(
153 ConnectionId connectionId,
154 uint32_t invalidateId,
155 bool *invalidated);
156};
157
158/**
159 * A buffer invalidation FMQ for a buffer pool client. Buffer invalidation
160 * messages are received via the fmq from the buffer pool. Buffer invalidation
161 * messages are handled as soon as possible.
162 */
163class BufferInvalidationListener {
164private:
165 bool mValid;
166 std::unique_ptr<BufferInvalidationQueue> mBufferInvalidationQueue;
167
168public:
169 /**
170 * Connects to a buffer invalidation FMQ from a descriptor of the created FMQ.
171 *
172 * @param fmqDesc Descriptor of the created FMQ.
173 */
174 BufferInvalidationListener(const InvalidationDescriptor &fmqDesc);
175
176 /** Retrieves all pending buffer invalidation messages from the buffer pool.
177 *
178 * @param messages retrieved pending messages.
179 */
180 void getInvalidations(std::vector<BufferInvalidationMessage> &messages);
181
182 /** Returns whether the FMQ is connected succesfully. */
183 bool isValid();
184};
185
186/**
187 * A buffer invalidation FMQ for a buffer pool. A buffer pool will send buffer
188 * invalidation messages to the clients via the FMQ. The FMQ is shared among
189 * buffer pool clients.
190 */
191class BufferInvalidationChannel {
192private:
193 bool mValid;
194 std::unique_ptr<BufferInvalidationQueue> mBufferInvalidationQueue;
195
196public:
197 /**
198 * Creates a buffer invalidation FMQ for a buffer pool.
199 */
200 BufferInvalidationChannel();
201
202 /** Returns whether the FMQ is connected succesfully. */
203 bool isValid();
204
205 /**
206 * Retrieves the descriptor of a buffer invalidation FMQ. the descriptor may
207 * be passed to the client for buffer invalidation handling.
208 *
209 * @param fmqDescPtr double ptr of created FMQ's descriptor.
210 */
211 void getDesc(const InvalidationDescriptor **fmqDescPtr);
212
213 /** Posts a buffer invalidation for invalidated buffers.
214 *
215 * @param msgId Invalidation message id which is used when clients send
216 * acks back via BufferStatusMessage
217 * @param fromId The start bufferid of the invalidated buffers(inclusive)
218 * @param toId The end bufferId of the invalidated buffers(inclusive)
219 */
220 void postInvalidation(uint32_t msgId, BufferId fromId, BufferId toId);
Sungtak Leebbe37b62018-08-29 15:15:48 -0700221};
222
223} // namespace implementation
224} // namespace V2_0
225} // namespace bufferpool
226} // namespace media
227} // namespace hardware
228} // namespace android
229
230#endif // ANDROID_HARDWARE_MEDIA_BUFFERPOOL_V2_0_BUFFERSTATUS_H