blob: 76402f2ee6787ecf1c6c9f9b4e6e682c13b9b3a0 [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#define LOG_TAG "BufferPoolStatus"
18//#define LOG_NDEBUG 0
19
Sungtak Leec7f9e2c2018-09-14 16:23:40 -070020#include <thread>
Sungtak Leebbe37b62018-08-29 15:15:48 -070021#include <time.h>
22#include "BufferStatus.h"
23
Devin Mooreb0e09632021-04-22 15:41:07 -070024// This is added to preserve ABI for b/184963385
25template bool android::hardware::MessageQueue<
26 android::hardware::media::bufferpool::V2_0::BufferStatusMessage,
27 android::hardware::kSynchronizedReadWrite>::
28 write(
29 const android::hardware::media::bufferpool::V2_0::BufferStatusMessage *,
30 size_t count);
31#ifndef __aarch64__
32template bool android::hardware::MessageQueue<
33 android::hardware::media::bufferpool::V2_0::BufferInvalidationMessage,
34 android::hardware::kUnsynchronizedWrite>::
35 write(const android::hardware::media::bufferpool::V2_0::
36 BufferInvalidationMessage *,
37 size_t count);
38#endif
Sungtak Leebbe37b62018-08-29 15:15:48 -070039namespace android {
40namespace hardware {
41namespace media {
42namespace bufferpool {
43namespace V2_0 {
44namespace implementation {
45
46int64_t getTimestampNow() {
47 int64_t stamp;
48 struct timespec ts;
49 // TODO: CLOCK_MONOTONIC_COARSE?
50 clock_gettime(CLOCK_MONOTONIC, &ts);
51 stamp = ts.tv_nsec / 1000;
52 stamp += (ts.tv_sec * 1000000LL);
53 return stamp;
54}
55
Sungtak Leec7f9e2c2018-09-14 16:23:40 -070056bool isMessageLater(uint32_t curMsgId, uint32_t prevMsgId) {
57 return curMsgId != prevMsgId && curMsgId - prevMsgId < prevMsgId - curMsgId;
58}
59
60bool isBufferInRange(BufferId from, BufferId to, BufferId bufferId) {
61 if (from < to) {
62 return from <= bufferId && bufferId < to;
63 } else { // wrap happens
64 return from <= bufferId || bufferId < to;
65 }
66}
67
Sungtak Leebbe37b62018-08-29 15:15:48 -070068static constexpr int kNumElementsInQueue = 1024*16;
69static constexpr int kMinElementsToSyncInQueue = 128;
70
71ResultStatus BufferStatusObserver::open(
Sungtak Lee1cb0ccb2018-09-05 14:47:36 -070072 ConnectionId id, const StatusDescriptor** fmqDescPtr) {
Sungtak Leebbe37b62018-08-29 15:15:48 -070073 if (mBufferStatusQueues.find(id) != mBufferStatusQueues.end()) {
74 // TODO: id collision log?
75 return ResultStatus::CRITICAL_ERROR;
76 }
77 std::unique_ptr<BufferStatusQueue> queue =
78 std::make_unique<BufferStatusQueue>(kNumElementsInQueue);
79 if (!queue || queue->isValid() == false) {
80 *fmqDescPtr = nullptr;
81 return ResultStatus::NO_MEMORY;
82 } else {
83 *fmqDescPtr = queue->getDesc();
84 }
85 auto result = mBufferStatusQueues.insert(
86 std::make_pair(id, std::move(queue)));
87 if (!result.second) {
88 *fmqDescPtr = nullptr;
89 return ResultStatus::NO_MEMORY;
90 }
91 return ResultStatus::OK;
92}
93
94ResultStatus BufferStatusObserver::close(ConnectionId id) {
95 if (mBufferStatusQueues.find(id) == mBufferStatusQueues.end()) {
96 return ResultStatus::CRITICAL_ERROR;
97 }
98 mBufferStatusQueues.erase(id);
99 return ResultStatus::OK;
100}
101
102void BufferStatusObserver::getBufferStatusChanges(std::vector<BufferStatusMessage> &messages) {
103 for (auto it = mBufferStatusQueues.begin(); it != mBufferStatusQueues.end(); ++it) {
104 BufferStatusMessage message;
105 size_t avail = it->second->availableToRead();
106 while (avail > 0) {
107 if (!it->second->read(&message, 1)) {
108 // Since avaliable # of reads are already confirmed,
109 // this should not happen.
110 // TODO: error handling (spurious client?)
111 ALOGW("FMQ message cannot be read from %lld", (long long)it->first);
112 return;
113 }
114 message.connectionId = it->first;
115 messages.push_back(message);
116 --avail;
117 }
118 }
119}
120
121BufferStatusChannel::BufferStatusChannel(
Sungtak Lee1cb0ccb2018-09-05 14:47:36 -0700122 const StatusDescriptor &fmqDesc) {
Sungtak Leebbe37b62018-08-29 15:15:48 -0700123 std::unique_ptr<BufferStatusQueue> queue =
124 std::make_unique<BufferStatusQueue>(fmqDesc);
125 if (!queue || queue->isValid() == false) {
126 mValid = false;
127 return;
128 }
129 mValid = true;
130 mBufferStatusQueue = std::move(queue);
131}
132
133bool BufferStatusChannel::isValid() {
134 return mValid;
135}
136
137bool BufferStatusChannel::needsSync() {
138 if (mValid) {
139 size_t avail = mBufferStatusQueue->availableToWrite();
140 return avail + kMinElementsToSyncInQueue < kNumElementsInQueue;
141 }
142 return false;
143}
144
145void BufferStatusChannel::postBufferRelease(
146 ConnectionId connectionId,
147 std::list<BufferId> &pending, std::list<BufferId> &posted) {
148 if (mValid && pending.size() > 0) {
149 size_t avail = mBufferStatusQueue->availableToWrite();
150 avail = std::min(avail, pending.size());
151 BufferStatusMessage message;
152 for (size_t i = 0 ; i < avail; ++i) {
153 BufferId id = pending.front();
154 message.newStatus = BufferStatus::NOT_USED;
155 message.bufferId = id;
156 message.connectionId = connectionId;
157 if (!mBufferStatusQueue->write(&message, 1)) {
158 // Since avaliable # of writes are already confirmed,
159 // this should not happen.
160 // TODO: error handing?
161 ALOGW("FMQ message cannot be sent from %lld", (long long)connectionId);
162 return;
163 }
164 pending.pop_front();
165 posted.push_back(id);
166 }
167 }
168}
169
Sungtak Leec7f9e2c2018-09-14 16:23:40 -0700170void BufferStatusChannel::postBufferInvalidateAck(
171 ConnectionId connectionId,
172 uint32_t invalidateId,
173 bool *invalidated) {
174 if (mValid && !*invalidated) {
175 size_t avail = mBufferStatusQueue->availableToWrite();
176 if (avail > 0) {
177 BufferStatusMessage message;
178 message.newStatus = BufferStatus::INVALIDATION_ACK;
179 message.bufferId = invalidateId;
180 message.connectionId = connectionId;
181 if (!mBufferStatusQueue->write(&message, 1)) {
182 // Since avaliable # of writes are already confirmed,
183 // this should not happen.
184 // TODO: error handing?
185 ALOGW("FMQ message cannot be sent from %lld", (long long)connectionId);
186 return;
187 }
188 *invalidated = true;
189 }
190 }
191}
192
Sungtak Leebbe37b62018-08-29 15:15:48 -0700193bool BufferStatusChannel::postBufferStatusMessage(
194 TransactionId transactionId, BufferId bufferId,
195 BufferStatus status, ConnectionId connectionId, ConnectionId targetId,
196 std::list<BufferId> &pending, std::list<BufferId> &posted) {
197 if (mValid) {
198 size_t avail = mBufferStatusQueue->availableToWrite();
199 size_t numPending = pending.size();
200 if (avail >= numPending + 1) {
201 BufferStatusMessage release, message;
202 for (size_t i = 0; i < numPending; ++i) {
203 BufferId id = pending.front();
204 release.newStatus = BufferStatus::NOT_USED;
205 release.bufferId = id;
206 release.connectionId = connectionId;
207 if (!mBufferStatusQueue->write(&release, 1)) {
208 // Since avaliable # of writes are already confirmed,
209 // this should not happen.
210 // TODO: error handling?
211 ALOGW("FMQ message cannot be sent from %lld", (long long)connectionId);
212 return false;
213 }
214 pending.pop_front();
215 posted.push_back(id);
216 }
217 message.transactionId = transactionId;
218 message.bufferId = bufferId;
219 message.newStatus = status;
220 message.connectionId = connectionId;
221 message.targetConnectionId = targetId;
222 // TODO : timesatamp
223 message.timestampUs = 0;
224 if (!mBufferStatusQueue->write(&message, 1)) {
225 // Since avaliable # of writes are already confirmed,
226 // this should not happen.
227 ALOGW("FMQ message cannot be sent from %lld", (long long)connectionId);
228 return false;
229 }
230 return true;
231 }
232 }
233 return false;
234}
235
Sungtak Leec7f9e2c2018-09-14 16:23:40 -0700236BufferInvalidationListener::BufferInvalidationListener(
237 const InvalidationDescriptor &fmqDesc) {
238 std::unique_ptr<BufferInvalidationQueue> queue =
239 std::make_unique<BufferInvalidationQueue>(fmqDesc);
240 if (!queue || queue->isValid() == false) {
241 mValid = false;
242 return;
243 }
244 mValid = true;
245 mBufferInvalidationQueue = std::move(queue);
246 // drain previous messages
247 size_t avail = std::min(
248 mBufferInvalidationQueue->availableToRead(), (size_t) kNumElementsInQueue);
249 std::vector<BufferInvalidationMessage> temp(avail);
250 if (avail > 0) {
251 mBufferInvalidationQueue->read(temp.data(), avail);
252 }
253}
254
255void BufferInvalidationListener::getInvalidations(
256 std::vector<BufferInvalidationMessage> &messages) {
257 // Try twice in case of overflow.
258 // TODO: handling overflow though it may not happen.
259 for (int i = 0; i < 2; ++i) {
260 size_t avail = std::min(
261 mBufferInvalidationQueue->availableToRead(), (size_t) kNumElementsInQueue);
262 if (avail > 0) {
263 std::vector<BufferInvalidationMessage> temp(avail);
264 if (mBufferInvalidationQueue->read(temp.data(), avail)) {
265 messages.reserve(messages.size() + avail);
266 for (auto it = temp.begin(); it != temp.end(); ++it) {
267 messages.push_back(*it);
268 }
269 break;
270 }
271 } else {
272 return;
273 }
274 }
275}
276
277bool BufferInvalidationListener::isValid() {
278 return mValid;
279}
280
281BufferInvalidationChannel::BufferInvalidationChannel()
282 : mValid(true),
283 mBufferInvalidationQueue(
284 std::make_unique<BufferInvalidationQueue>(kNumElementsInQueue, true)) {
285 if (!mBufferInvalidationQueue || mBufferInvalidationQueue->isValid() == false) {
286 mValid = false;
287 }
288}
289
290bool BufferInvalidationChannel::isValid() {
291 return mValid;
292}
293
294void BufferInvalidationChannel::getDesc(const InvalidationDescriptor **fmqDescPtr) {
295 if (mValid) {
296 *fmqDescPtr = mBufferInvalidationQueue->getDesc();
297 } else {
298 *fmqDescPtr = nullptr;
299 }
300}
301
302void BufferInvalidationChannel::postInvalidation(
303 uint32_t msgId, BufferId fromId, BufferId toId) {
304 BufferInvalidationMessage message;
305
306 message.messageId = msgId;
307 message.fromBufferId = fromId;
308 message.toBufferId = toId;
309 // TODO: handle failure (it does not happen normally.)
310 mBufferInvalidationQueue->write(&message);
311}
312
Sungtak Leebbe37b62018-08-29 15:15:48 -0700313} // namespace implementation
314} // namespace V2_0
315} // namespace bufferpool
316} // namespace media
317} // namespace hardware
318} // namespace android
319