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     /** Returns whether the FMQ needs to be synced from the buffer pool */
98     bool needsSync();
99 
100     /**
101      * Posts a buffer release message to the buffer pool.
102      *
103      * @param connectionId  connection Id of the client.
104      * @param pending       currently pending buffer release messages.
105      * @param posted        posted buffer release messages.
106      */
107     void postBufferRelease(
108             ConnectionId connectionId,
109             std::list<BufferId> &pending, std::list<BufferId> &posted);
110 
111     /**
112      * Posts a buffer status message regarding the specified buffer
113      * transfer transaction.
114      *
115      * @param transactionId Id of the specified transaction.
116      * @param bufferId      buffer Id of the specified transaction.
117      * @param status        new status of the buffer.
118      * @param connectionId  connection Id of the client.
119      * @param targetId      connection Id of the receiver(only when the sender
120      *                      posts a status message).
121      * @param pending       currently pending buffer release messages.
122      * @param posted        posted buffer release messages.
123      *
124      * @return {@code true} when the specified message is posted,
125      *         {@code false} otherwise.
126      */
127     bool postBufferStatusMessage(
128             TransactionId transactionId,
129             BufferId bufferId,
130             BufferStatus status,
131             ConnectionId connectionId,
132             ConnectionId targetId,
133             std::list<BufferId> &pending, std::list<BufferId> &posted);
134 };
135 
136 }  // namespace implementation
137 }  // namespace V1_0
138 }  // namespace bufferpool
139 }  // namespace media
140 }  // namespace hardware
141 }  // namespace android
142 
143 #endif  // ANDROID_HARDWARE_MEDIA_BUFFERPOOL_V1_0_BUFFERSTATUS_H
144