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_V1_0_BUFFERSTATUS_H
18 #define ANDROID_HARDWARE_MEDIA_BUFFERPOOL_V1_0_BUFFERSTATUS_H
19 
20 #include <android/hardware/media/bufferpool/1.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 V1_0 {
35 namespace implementation {
36 
37 /** Returns monotonic timestamp in Us since fixed point in time. */
38 int64_t getTimestampNow();
39 
40 /**
41  * A collection of FMQ for a buffer pool. buffer ownership/status change
42  * messages are sent via the FMQs from the clients.
43  */
44 class BufferStatusObserver {
45 private:
46     std::map<ConnectionId, std::unique_ptr<BufferStatusQueue>>
47             mBufferStatusQueues;
48 
49 public:
50     /** Creates an FMQ for the specified connection(client).
51      *
52      * @param connectionId  connection Id of the specified client.
53      * @param fmqDescPtr    double ptr of created FMQ's descriptor.
54      *
55      * @return OK if FMQ is created successfully.
56      *         NO_MEMORY when there is no memory.
57      *         CRITICAL_ERROR otherwise.
58      */
59     ResultStatus open(ConnectionId id, const QueueDescriptor** fmqDescPtr);
60 
61     /** Closes an FMQ for the specified connection(client).
62      *
63      * @param connectionId  connection Id of the specified client.
64      *
65      * @return OK if the specified connection is closed successfully.
66      *         CRITICAL_ERROR otherwise.
67      */
68     ResultStatus close(ConnectionId id);
69 
70     /** Retrieves all pending FMQ buffer status messages from clients.
71      *
72      * @param messages  retrieved pending messages.
73      */
74     void getBufferStatusChanges(std::vector<BufferStatusMessage> &messages);
75 };
76 
77 /**
78  * An FMQ for a buffer pool client. Buffer ownership/status change messages
79  * are sent via the fmq to the buffer pool.
80  */
81 class BufferStatusChannel {
82 private:
83     bool mValid;
84     std::unique_ptr<BufferStatusQueue> mBufferStatusQueue;
85 
86 public:
87     /**
88      * Connects to an FMQ from a descriptor of the created FMQ.
89      *
90      * @param fmqDesc   Descriptor of the created FMQ.
91      */
92     BufferStatusChannel(const QueueDescriptor &fmqDesc);
93 
94     /** Returns whether the FMQ is connected successfully. */
95     bool isValid();
96 
97     /**
98      * Posts a buffer release message to the buffer pool.
99      *
100      * @param connectionId  connection Id of the client.
101      * @param pending       currently pending buffer release messages.
102      * @param posted        posted buffer release messages.
103      */
104     void postBufferRelease(
105             ConnectionId connectionId,
106             std::list<BufferId> &pending, std::list<BufferId> &posted);
107 
108     /**
109      * Posts a buffer status message regarding the specified buffer
110      * transfer transaction.
111      *
112      * @param transactionId Id of the specified transaction.
113      * @param bufferId      buffer Id of the specified transaction.
114      * @param status        new status of the buffer.
115      * @param connectionId  connection Id of the client.
116      * @param targetId      connection Id of the receiver(only when the sender
117      *                      posts a status message).
118      * @param pending       currently pending buffer release messages.
119      * @param posted        posted buffer release messages.
120      *
121      * @return {@code true} when the specified message is posted,
122      *         {@code false} otherwise.
123      */
124     bool postBufferStatusMessage(
125             TransactionId transactionId,
126             BufferId bufferId,
127             BufferStatus status,
128             ConnectionId connectionId,
129             ConnectionId targetId,
130             std::list<BufferId> &pending, std::list<BufferId> &posted);
131 };
132 
133 }  // namespace implementation
134 }  // namespace V1_0
135 }  // namespace bufferpool
136 }  // namespace media
137 }  // namespace hardware
138 }  // namespace android
139 
140 #endif  // ANDROID_HARDWARE_MEDIA_BUFFERPOOL_V1_0_BUFFERSTATUS_H
141