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_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 
30 namespace android {
31 namespace hardware {
32 namespace media {
33 namespace bufferpool {
34 namespace V2_0 {
35 namespace implementation {
36 
37 /** Returns monotonic timestamp in Us since fixed point in time. */
38 int64_t getTimestampNow();
39 
40 bool isMessageLater(uint32_t curMsgId, uint32_t prevMsgId);
41 
42 bool isBufferInRange(BufferId from, BufferId to, BufferId bufferId);
43 
44 /**
45  * 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.
47  */
48 class BufferStatusObserver {
49 private:
50     std::map<ConnectionId, std::unique_ptr<BufferStatusQueue>>
51             mBufferStatusQueues;
52 
53 public:
54     /** Creates a buffer status message FMQ for the specified
55      * connection(client).
56      *
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      */
64     ResultStatus open(ConnectionId id, const StatusDescriptor** fmqDescPtr);
65 
66     /** Closes a buffer status message FMQ for the specified
67      * connection(client).
68      *
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 /**
84  * 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.
86  */
87 class BufferStatusChannel {
88 private:
89     bool mValid;
90     std::unique_ptr<BufferStatusQueue> mBufferStatusQueue;
91 
92 public:
93     /**
94      * Connects to a buffer status message FMQ from a descriptor of
95      * the created FMQ.
96      *
97      * @param fmqDesc   Descriptor of the created FMQ.
98      */
99     BufferStatusChannel(const StatusDescriptor &fmqDesc);
100 
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);
141 
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  */
163 class BufferInvalidationListener {
164 private:
165     bool mValid;
166     std::unique_ptr<BufferInvalidationQueue> mBufferInvalidationQueue;
167 
168 public:
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  */
191 class BufferInvalidationChannel {
192 private:
193     bool mValid;
194     std::unique_ptr<BufferInvalidationQueue> mBufferInvalidationQueue;
195 
196 public:
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);
221 };
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
231