blob: 54796a2896fe1f431d7619d35654de0207946bae [file] [log] [blame]
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -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
Fan Xufe097c72018-12-07 15:46:51 -080017#include <iomanip>
18#include <sstream>
19
Fan Xu93c94902018-11-01 12:22:05 -070020#include <android/hardware_buffer.h>
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070021#include <bufferhub/BufferHubService.h>
Fan Xu18d90ea2018-11-06 15:46:44 -080022#include <cutils/native_handle.h>
Fan Xu93c94902018-11-01 12:22:05 -070023#include <log/log.h>
Fan Xufe097c72018-12-07 15:46:51 -080024#include <system/graphics-base.h>
25
26using ::android::BufferHubDefs::MetadataHeader;
27using ::android::hardware::Void;
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070028
29namespace android {
30namespace frameworks {
31namespace bufferhub {
32namespace V1_0 {
33namespace implementation {
34
Fan Xu93c94902018-11-01 12:22:05 -070035Return<void> BufferHubService::allocateBuffer(const HardwareBufferDescription& description,
36 const uint32_t userMetadataSize,
Fan Xuca70b7b2018-10-31 13:20:12 -070037 allocateBuffer_cb _hidl_cb) {
Fan Xu93c94902018-11-01 12:22:05 -070038 AHardwareBuffer_Desc desc;
39 memcpy(&desc, &description, sizeof(AHardwareBuffer_Desc));
40
41 std::shared_ptr<BufferNode> node =
42 std::make_shared<BufferNode>(desc.width, desc.height, desc.layers, desc.format,
Fan Xu1c16df52018-11-19 16:27:27 -080043 desc.usage, userMetadataSize,
44 BufferHubIdGenerator::getInstance().getId());
Fan Xu93c94902018-11-01 12:22:05 -070045 if (node == nullptr || !node->IsValid()) {
46 ALOGE("%s: creating BufferNode failed.", __FUNCTION__);
Fan Xuf8f4a452018-11-29 16:26:30 -080047 _hidl_cb(/*status=*/BufferHubStatus::ALLOCATION_FAILED, /*bufferClient=*/nullptr,
48 /*bufferTraits=*/{});
Fan Xu93c94902018-11-01 12:22:05 -070049 return Void();
50 }
51
Fan Xu18d90ea2018-11-06 15:46:44 -080052 sp<BufferClient> client = BufferClient::create(this, node);
Fan Xu93c94902018-11-01 12:22:05 -070053 // Add it to list for bookkeeping and dumpsys.
Fan Xucd74d782018-11-26 13:51:25 -080054 std::lock_guard<std::mutex> lock(mClientSetMutex);
55 mClientSet.emplace(client);
Fan Xu93c94902018-11-01 12:22:05 -070056
Fan Xuf8f4a452018-11-29 16:26:30 -080057 BufferTraits bufferTraits = {/*bufferDesc=*/description,
58 /*bufferHandle=*/hidl_handle(node->buffer_handle()),
59 // TODO(b/116681016): return real data to client
60 /*bufferInfo=*/hidl_handle()};
61
62 _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
63 /*bufferTraits=*/bufferTraits);
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070064 return Void();
65}
66
Fan Xu467e08f2018-11-09 15:58:51 -080067Return<void> BufferHubService::importBuffer(const hidl_handle& tokenHandle,
Fan Xuca70b7b2018-10-31 13:20:12 -070068 importBuffer_cb _hidl_cb) {
Fan Xu467e08f2018-11-09 15:58:51 -080069 if (!tokenHandle.getNativeHandle() || tokenHandle->numFds != 0 || tokenHandle->numInts != 1) {
70 // nullptr handle or wrong format
Fan Xuf8f4a452018-11-29 16:26:30 -080071 _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr,
72 /*bufferTraits=*/{});
Fan Xu467e08f2018-11-09 15:58:51 -080073 return Void();
74 }
75
76 uint32_t token = tokenHandle->data[0];
77
78 wp<BufferClient> originClientWp;
79 {
80 std::lock_guard<std::mutex> lock(mTokenMapMutex);
81 auto iter = mTokenMap.find(token);
82 if (iter == mTokenMap.end()) {
83 // Invalid token
Fan Xuf8f4a452018-11-29 16:26:30 -080084 _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr,
85 /*bufferTraits=*/{});
Fan Xu467e08f2018-11-09 15:58:51 -080086 return Void();
87 }
88
89 originClientWp = iter->second;
90 mTokenMap.erase(iter);
91 }
92
93 // Check if original client is dead
94 sp<BufferClient> originClient = originClientWp.promote();
95 if (!originClient) {
96 // Should not happen since token should be removed if already gone
97 ALOGE("%s: original client %p gone!", __FUNCTION__, originClientWp.unsafe_get());
Fan Xuf8f4a452018-11-29 16:26:30 -080098 _hidl_cb(/*status=*/BufferHubStatus::BUFFER_FREED, /*bufferClient=*/nullptr,
99 /*bufferTraits=*/{});
Fan Xu467e08f2018-11-09 15:58:51 -0800100 return Void();
101 }
102
103 sp<BufferClient> client = new BufferClient(*originClient);
Fan Xuf8f4a452018-11-29 16:26:30 -0800104 uint32_t clientStateMask = client->getBufferNode()->AddNewActiveClientsBitToMask();
105 if (clientStateMask == 0U) {
106 // Reach max client count
107 ALOGE("%s: import failed, BufferNode#%u reached maximum clients.", __FUNCTION__,
108 client->getBufferNode()->id());
109 _hidl_cb(/*status=*/BufferHubStatus::MAX_CLIENT, /*bufferClient=*/nullptr,
110 /*bufferTraits=*/{});
111 return Void();
112 }
Fan Xu467e08f2018-11-09 15:58:51 -0800113
Fan Xucd74d782018-11-26 13:51:25 -0800114 std::lock_guard<std::mutex> lock(mClientSetMutex);
115 mClientSet.emplace(client);
Fan Xuf8f4a452018-11-29 16:26:30 -0800116
117 std::shared_ptr<BufferNode> node = client->getBufferNode();
118
119 HardwareBufferDescription bufferDesc;
120 memcpy(&bufferDesc, &node->buffer_desc(), sizeof(HardwareBufferDescription));
121
122 BufferTraits bufferTraits = {/*bufferDesc=*/bufferDesc,
123 /*bufferHandle=*/hidl_handle(node->buffer_handle()),
124 // TODO(b/116681016): return real data to client
125 /*bufferInfo=*/hidl_handle()};
126
127 _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
128 /*bufferTraits=*/bufferTraits);
Fan Xuca70b7b2018-10-31 13:20:12 -0700129 return Void();
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -0700130}
131
Fan Xufe097c72018-12-07 15:46:51 -0800132Return<void> BufferHubService::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) {
133 if (fd.getNativeHandle() == nullptr || fd->numFds < 1) {
134 ALOGE("%s: missing fd for writing.", __FUNCTION__);
135 return Void();
136 }
137
138 FILE* out = fdopen(dup(fd->data[0]), "w");
139
140 if (args.size() != 0) {
141 fprintf(out,
142 "Note: lshal bufferhub currently does not support args. Input arguments are "
143 "ignored.\n");
144 }
145
146 std::ostringstream stream;
147
148 // Get the number of clients of each buffer.
149 // Map from bufferId to bufferNode_clientCount pair.
150 std::map<int, std::pair<const std::shared_ptr<BufferNode>, uint32_t>> clientCount;
151 {
152 std::lock_guard<std::mutex> lock(mClientSetMutex);
153 for (auto iter = mClientSet.begin(); iter != mClientSet.end(); ++iter) {
154 sp<BufferClient> client = iter->promote();
155 if (client != nullptr) {
156 const std::shared_ptr<BufferNode> node = client->getBufferNode();
157 auto mapIter = clientCount.find(node->id());
158 if (mapIter != clientCount.end()) {
159 ++mapIter->second.second;
160 } else {
161 clientCount.emplace(node->id(),
162 std::pair<std::shared_ptr<BufferNode>, uint32_t>(node, 1U));
163 }
164 }
165 }
166 }
167
168 stream << "Active Buffers:\n";
169 stream << std::right;
170 stream << std::setw(6) << "Id";
171 stream << " ";
172 stream << std::setw(9) << "Clients";
173 stream << " ";
174 stream << std::setw(14) << "Geometry";
175 stream << " ";
176 stream << std::setw(6) << "Format";
177 stream << " ";
178 stream << std::setw(10) << "Usage";
179 stream << " ";
180 stream << std::setw(10) << "State";
181 stream << " ";
182 stream << std::setw(10) << "Index";
183 stream << std::endl;
184
185 for (auto iter = clientCount.begin(); iter != clientCount.end(); ++iter) {
186 const std::shared_ptr<BufferNode> node = std::move(iter->second.first);
187 const uint32_t clientCount = iter->second.second;
188 AHardwareBuffer_Desc desc = node->buffer_desc();
189
190 MetadataHeader* metadataHeader =
191 const_cast<BufferHubMetadata*>(&node->metadata())->metadata_header();
192 const uint32_t state = metadataHeader->buffer_state.load(std::memory_order_acquire);
193 const uint64_t index = metadataHeader->queue_index;
194
195 stream << std::right;
196 stream << std::setw(6) << /*Id=*/node->id();
197 stream << " ";
198 stream << std::setw(9) << /*Clients=*/clientCount;
199 stream << " ";
200 if (desc.format == HAL_PIXEL_FORMAT_BLOB) {
201 std::string size = std::to_string(desc.width) + " B";
202 stream << std::setw(14) << /*Geometry=*/size;
203 } else {
204 std::string dimensions = std::to_string(desc.width) + "x" +
205 std::to_string(desc.height) + "x" + std::to_string(desc.layers);
206 stream << std::setw(14) << /*Geometry=*/dimensions;
207 }
208 stream << " ";
209 stream << std::setw(6) << /*Format=*/desc.format;
210 stream << " ";
211 stream << "0x" << std::hex << std::setfill('0');
212 stream << std::setw(8) << /*Usage=*/desc.usage;
213 stream << std::dec << std::setfill(' ');
214 stream << " ";
215 stream << "0x" << std::hex << std::setfill('0');
216 stream << std::setw(8) << /*State=*/state;
217 stream << " ";
218 stream << std::setw(8) << /*Index=*/index;
219 stream << std::endl;
220 }
221
222 stream << std::endl;
223
224 // Get the number of tokens of each buffer.
225 // Map from bufferId to tokenCount
226 std::map<int, uint32_t> tokenCount;
227 {
228 std::lock_guard<std::mutex> lock(mTokenMapMutex);
229 for (auto iter = mTokenMap.begin(); iter != mTokenMap.end(); ++iter) {
230 sp<BufferClient> client = iter->second.promote();
231 if (client != nullptr) {
232 const std::shared_ptr<BufferNode> node = client->getBufferNode();
233 auto mapIter = tokenCount.find(node->id());
234 if (mapIter != tokenCount.end()) {
235 ++mapIter->second;
236 } else {
237 tokenCount.emplace(node->id(), 1U);
238 }
239 }
240 }
241 }
242
243 stream << "Unused Tokens:\n";
244 stream << std::right;
245 stream << std::setw(8) << "Buffer Id";
246 stream << " ";
247 stream << std::setw(6) << "Tokens";
248 stream << std::endl;
249
250 for (auto iter = tokenCount.begin(); iter != tokenCount.end(); ++iter) {
251 stream << std::right;
252 stream << std::setw(8) << /*Buffer Id=*/iter->first;
253 stream << " ";
254 stream << std::setw(6) << /*Tokens=*/iter->second;
255 stream << std::endl;
256 }
257
258 fprintf(out, "%s", stream.str().c_str());
259
260 fclose(out);
261 return Void();
262}
263
Fan Xud6cd6ba2018-11-15 16:46:55 -0800264hidl_handle BufferHubService::registerToken(const wp<BufferClient>& client) {
Fan Xu18d90ea2018-11-06 15:46:44 -0800265 uint32_t token;
266 std::lock_guard<std::mutex> lock(mTokenMapMutex);
267 do {
268 token = mTokenEngine();
269 } while (mTokenMap.find(token) != mTokenMap.end());
270
271 // native_handle_t use int[], so here need one slots to fit in uint32_t
272 native_handle_t* handle = native_handle_create(/*numFds=*/0, /*numInts=*/1);
273 handle->data[0] = token;
274
275 // returnToken owns the native_handle_t* thus doing lifecycle management
276 hidl_handle returnToken;
277 returnToken.setTo(handle, /*shoudOwn=*/true);
278
279 mTokenMap.emplace(token, client);
280 return returnToken;
281}
282
Fan Xua7422fe2018-11-19 15:21:32 -0800283void BufferHubService::onClientClosed(const BufferClient* client) {
284 removeTokenByClient(client);
285
Fan Xucd74d782018-11-26 13:51:25 -0800286 std::lock_guard<std::mutex> lock(mClientSetMutex);
287 auto iter = std::find(mClientSet.begin(), mClientSet.end(), client);
288 if (iter != mClientSet.end()) {
289 mClientSet.erase(iter);
Fan Xua7422fe2018-11-19 15:21:32 -0800290 }
291}
292
293void BufferHubService::removeTokenByClient(const BufferClient* client) {
294 std::lock_guard<std::mutex> lock(mTokenMapMutex);
295 auto iter = mTokenMap.begin();
296 while (iter != mTokenMap.end()) {
297 if (iter->second == client) {
298 auto oldIter = iter;
299 ++iter;
300 mTokenMap.erase(oldIter);
301 } else {
302 ++iter;
303 }
304 }
305}
306
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -0700307} // namespace implementation
308} // namespace V1_0
309} // namespace bufferhub
310} // namespace frameworks
311} // namespace android