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 #define LOG_TAG "BufferPoolStatus"
18 //#define LOG_NDEBUG 0
19
20 #include <time.h>
21 #include "BufferStatus.h"
22
23 namespace android {
24 namespace hardware {
25 namespace media {
26 namespace bufferpool {
27 namespace V1_0 {
28 namespace implementation {
29
getTimestampNow()30 int64_t getTimestampNow() {
31 int64_t stamp;
32 struct timespec ts;
33 // TODO: CLOCK_MONOTONIC_COARSE?
34 clock_gettime(CLOCK_MONOTONIC, &ts);
35 stamp = ts.tv_nsec / 1000;
36 stamp += (ts.tv_sec * 1000000LL);
37 return stamp;
38 }
39
40 static constexpr int kNumElementsInQueue = 1024*16;
41 static constexpr int kMinElementsToSyncInQueue = 128;
42
open(ConnectionId id,const QueueDescriptor ** fmqDescPtr)43 ResultStatus BufferStatusObserver::open(
44 ConnectionId id, const QueueDescriptor** fmqDescPtr) {
45 if (mBufferStatusQueues.find(id) != mBufferStatusQueues.end()) {
46 // TODO: id collision log?
47 return ResultStatus::CRITICAL_ERROR;
48 }
49 std::unique_ptr<BufferStatusQueue> queue =
50 std::make_unique<BufferStatusQueue>(kNumElementsInQueue);
51 if (!queue || queue->isValid() == false) {
52 *fmqDescPtr = nullptr;
53 return ResultStatus::NO_MEMORY;
54 } else {
55 *fmqDescPtr = queue->getDesc();
56 }
57 auto result = mBufferStatusQueues.insert(
58 std::make_pair(id, std::move(queue)));
59 if (!result.second) {
60 *fmqDescPtr = nullptr;
61 return ResultStatus::NO_MEMORY;
62 }
63 return ResultStatus::OK;
64 }
65
close(ConnectionId id)66 ResultStatus BufferStatusObserver::close(ConnectionId id) {
67 if (mBufferStatusQueues.find(id) == mBufferStatusQueues.end()) {
68 return ResultStatus::CRITICAL_ERROR;
69 }
70 mBufferStatusQueues.erase(id);
71 return ResultStatus::OK;
72 }
73
getBufferStatusChanges(std::vector<BufferStatusMessage> & messages)74 void BufferStatusObserver::getBufferStatusChanges(std::vector<BufferStatusMessage> &messages) {
75 for (auto it = mBufferStatusQueues.begin(); it != mBufferStatusQueues.end(); ++it) {
76 BufferStatusMessage message;
77 size_t avail = it->second->availableToRead();
78 while (avail > 0) {
79 if (!it->second->read(&message, 1)) {
80 // Since avaliable # of reads are already confirmed,
81 // this should not happen.
82 // TODO: error handling (spurious client?)
83 ALOGW("FMQ message cannot be read from %lld", (long long)it->first);
84 return;
85 }
86 message.connectionId = it->first;
87 messages.push_back(message);
88 --avail;
89 }
90 }
91 }
92
BufferStatusChannel(const QueueDescriptor & fmqDesc)93 BufferStatusChannel::BufferStatusChannel(
94 const QueueDescriptor &fmqDesc) {
95 std::unique_ptr<BufferStatusQueue> queue =
96 std::make_unique<BufferStatusQueue>(fmqDesc);
97 if (!queue || queue->isValid() == false) {
98 mValid = false;
99 return;
100 }
101 mValid = true;
102 mBufferStatusQueue = std::move(queue);
103 }
104
isValid()105 bool BufferStatusChannel::isValid() {
106 return mValid;
107 }
108
needsSync()109 bool BufferStatusChannel::needsSync() {
110 if (mValid) {
111 size_t avail = mBufferStatusQueue->availableToWrite();
112 return avail + kMinElementsToSyncInQueue < kNumElementsInQueue;
113 }
114 return false;
115 }
116
postBufferRelease(ConnectionId connectionId,std::list<BufferId> & pending,std::list<BufferId> & posted)117 void BufferStatusChannel::postBufferRelease(
118 ConnectionId connectionId,
119 std::list<BufferId> &pending, std::list<BufferId> &posted) {
120 if (mValid && pending.size() > 0) {
121 size_t avail = mBufferStatusQueue->availableToWrite();
122 avail = std::min(avail, pending.size());
123 BufferStatusMessage message;
124 for (size_t i = 0 ; i < avail; ++i) {
125 BufferId id = pending.front();
126 message.newStatus = BufferStatus::NOT_USED;
127 message.bufferId = id;
128 message.connectionId = connectionId;
129 if (!mBufferStatusQueue->write(&message, 1)) {
130 // Since avaliable # of writes are already confirmed,
131 // this should not happen.
132 // TODO: error handing?
133 ALOGW("FMQ message cannot be sent from %lld", (long long)connectionId);
134 return;
135 }
136 pending.pop_front();
137 posted.push_back(id);
138 }
139 }
140 }
141
postBufferStatusMessage(TransactionId transactionId,BufferId bufferId,BufferStatus status,ConnectionId connectionId,ConnectionId targetId,std::list<BufferId> & pending,std::list<BufferId> & posted)142 bool BufferStatusChannel::postBufferStatusMessage(
143 TransactionId transactionId, BufferId bufferId,
144 BufferStatus status, ConnectionId connectionId, ConnectionId targetId,
145 std::list<BufferId> &pending, std::list<BufferId> &posted) {
146 if (mValid) {
147 size_t avail = mBufferStatusQueue->availableToWrite();
148 size_t numPending = pending.size();
149 if (avail >= numPending + 1) {
150 BufferStatusMessage release, message;
151 for (size_t i = 0; i < numPending; ++i) {
152 BufferId id = pending.front();
153 release.newStatus = BufferStatus::NOT_USED;
154 release.bufferId = id;
155 release.connectionId = connectionId;
156 if (!mBufferStatusQueue->write(&release, 1)) {
157 // Since avaliable # of writes are already confirmed,
158 // this should not happen.
159 // TODO: error handling?
160 ALOGW("FMQ message cannot be sent from %lld", (long long)connectionId);
161 return false;
162 }
163 pending.pop_front();
164 posted.push_back(id);
165 }
166 message.transactionId = transactionId;
167 message.bufferId = bufferId;
168 message.newStatus = status;
169 message.connectionId = connectionId;
170 message.targetConnectionId = targetId;
171 // TODO : timesatamp
172 message.timestampUs = 0;
173 if (!mBufferStatusQueue->write(&message, 1)) {
174 // Since avaliable # of writes are already confirmed,
175 // this should not happen.
176 ALOGW("FMQ message cannot be sent from %lld", (long long)connectionId);
177 return false;
178 }
179 return true;
180 }
181 }
182 return false;
183 }
184
185 } // namespace implementation
186 } // namespace V1_0
187 } // namespace bufferpool
188 } // namespace media
189 } // namespace hardware
190 } // namespace android
191
192