blob: ad49cd62edf783abcced50b6156547da5d21ac1d [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 Xu55b26a62018-12-19 11:03:14 -080017#include <array>
Fan Xufe097c72018-12-07 15:46:51 -080018#include <iomanip>
Fan Xu55b26a62018-12-19 11:03:14 -080019#include <random>
Fan Xufe097c72018-12-07 15:46:51 -080020#include <sstream>
21
Fan Xu93c94902018-11-01 12:22:05 -070022#include <android/hardware_buffer.h>
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070023#include <bufferhub/BufferHubService.h>
Fan Xu18d90ea2018-11-06 15:46:44 -080024#include <cutils/native_handle.h>
Fan Xu93c94902018-11-01 12:22:05 -070025#include <log/log.h>
Fan Xu55b26a62018-12-19 11:03:14 -080026#include <openssl/hmac.h>
Fan Xufe097c72018-12-07 15:46:51 -080027#include <system/graphics-base.h>
Fan Xuefce32e2018-12-12 14:34:16 -080028#include <ui/BufferHubDefs.h>
Fan Xufe097c72018-12-07 15:46:51 -080029
30using ::android::BufferHubDefs::MetadataHeader;
31using ::android::hardware::Void;
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070032
33namespace android {
34namespace frameworks {
35namespace bufferhub {
36namespace V1_0 {
37namespace implementation {
38
Fan Xu55b26a62018-12-19 11:03:14 -080039BufferHubService::BufferHubService() {
40 std::mt19937_64 randomEngine;
41 randomEngine.seed(time(nullptr));
42
43 mKey = randomEngine();
44}
45
Fan Xu93c94902018-11-01 12:22:05 -070046Return<void> BufferHubService::allocateBuffer(const HardwareBufferDescription& description,
47 const uint32_t userMetadataSize,
Fan Xuca70b7b2018-10-31 13:20:12 -070048 allocateBuffer_cb _hidl_cb) {
Fan Xu93c94902018-11-01 12:22:05 -070049 AHardwareBuffer_Desc desc;
50 memcpy(&desc, &description, sizeof(AHardwareBuffer_Desc));
51
52 std::shared_ptr<BufferNode> node =
53 std::make_shared<BufferNode>(desc.width, desc.height, desc.layers, desc.format,
Fan Xu1c16df52018-11-19 16:27:27 -080054 desc.usage, userMetadataSize,
55 BufferHubIdGenerator::getInstance().getId());
Fan Xu93c94902018-11-01 12:22:05 -070056 if (node == nullptr || !node->IsValid()) {
57 ALOGE("%s: creating BufferNode failed.", __FUNCTION__);
Fan Xuf8f4a452018-11-29 16:26:30 -080058 _hidl_cb(/*status=*/BufferHubStatus::ALLOCATION_FAILED, /*bufferClient=*/nullptr,
59 /*bufferTraits=*/{});
Fan Xu93c94902018-11-01 12:22:05 -070060 return Void();
61 }
62
Fan Xu18d90ea2018-11-06 15:46:44 -080063 sp<BufferClient> client = BufferClient::create(this, node);
Fan Xu93c94902018-11-01 12:22:05 -070064 // Add it to list for bookkeeping and dumpsys.
Fan Xucd74d782018-11-26 13:51:25 -080065 std::lock_guard<std::mutex> lock(mClientSetMutex);
66 mClientSet.emplace(client);
Fan Xu93c94902018-11-01 12:22:05 -070067
Fan Xuefce32e2018-12-12 14:34:16 -080068 hidl_handle bufferInfo =
69 buildBufferInfo(node->id(), node->AddNewActiveClientsBitToMask(),
70 node->user_metadata_size(), node->metadata().ashmem_fd());
Fan Xuf8f4a452018-11-29 16:26:30 -080071 BufferTraits bufferTraits = {/*bufferDesc=*/description,
72 /*bufferHandle=*/hidl_handle(node->buffer_handle()),
Fan Xuefce32e2018-12-12 14:34:16 -080073 /*bufferInfo=*/bufferInfo};
Fan Xuf8f4a452018-11-29 16:26:30 -080074
75 _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
76 /*bufferTraits=*/bufferTraits);
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070077 return Void();
78}
79
Fan Xu467e08f2018-11-09 15:58:51 -080080Return<void> BufferHubService::importBuffer(const hidl_handle& tokenHandle,
Fan Xuca70b7b2018-10-31 13:20:12 -070081 importBuffer_cb _hidl_cb) {
Fan Xu55b26a62018-12-19 11:03:14 -080082 if (!tokenHandle.getNativeHandle() || tokenHandle->numFds != 0 || tokenHandle->numInts <= 1) {
Fan Xu467e08f2018-11-09 15:58:51 -080083 // nullptr handle or wrong format
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
Fan Xu55b26a62018-12-19 11:03:14 -080089 int tokenId = tokenHandle->data[0];
Fan Xu467e08f2018-11-09 15:58:51 -080090
91 wp<BufferClient> originClientWp;
92 {
Fan Xu55b26a62018-12-19 11:03:14 -080093 std::lock_guard<std::mutex> lock(mTokenMutex);
94 auto iter = mTokenMap.find(tokenId);
Fan Xu467e08f2018-11-09 15:58:51 -080095 if (iter == mTokenMap.end()) {
Fan Xu55b26a62018-12-19 11:03:14 -080096 // Token Id not exist
97 ALOGD("%s: token #%d not found.", __FUNCTION__, tokenId);
Fan Xuf8f4a452018-11-29 16:26:30 -080098 _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr,
99 /*bufferTraits=*/{});
Fan Xu467e08f2018-11-09 15:58:51 -0800100 return Void();
101 }
102
Fan Xu55b26a62018-12-19 11:03:14 -0800103 const std::vector<uint8_t>& tokenHMAC = iter->second.first;
104
105 int numIntsForHMAC = (int)ceil(tokenHMAC.size() * sizeof(uint8_t) / (double)sizeof(int));
106 if (tokenHandle->numInts - 1 != numIntsForHMAC) {
107 // HMAC size not match
108 ALOGD("%s: token #%d HMAC size not match. Expected: %d Actual: %d", __FUNCTION__,
109 tokenId, numIntsForHMAC, tokenHandle->numInts - 1);
110 _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr,
111 /*bufferTraits=*/{});
112 return Void();
113 }
114
115 size_t hmacSize = tokenHMAC.size() * sizeof(uint8_t);
116 if (memcmp(tokenHMAC.data(), &tokenHandle->data[1], hmacSize) != 0) {
117 // HMAC not match
118 ALOGD("%s: token #%d HMAC not match.", __FUNCTION__, tokenId);
119 _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr,
120 /*bufferTraits=*/{});
121 return Void();
122 }
123
124 originClientWp = iter->second.second;
Fan Xu467e08f2018-11-09 15:58:51 -0800125 mTokenMap.erase(iter);
126 }
127
128 // Check if original client is dead
129 sp<BufferClient> originClient = originClientWp.promote();
130 if (!originClient) {
131 // Should not happen since token should be removed if already gone
132 ALOGE("%s: original client %p gone!", __FUNCTION__, originClientWp.unsafe_get());
Fan Xuf8f4a452018-11-29 16:26:30 -0800133 _hidl_cb(/*status=*/BufferHubStatus::BUFFER_FREED, /*bufferClient=*/nullptr,
134 /*bufferTraits=*/{});
Fan Xu467e08f2018-11-09 15:58:51 -0800135 return Void();
136 }
137
138 sp<BufferClient> client = new BufferClient(*originClient);
Fan Xuf8f4a452018-11-29 16:26:30 -0800139 uint32_t clientStateMask = client->getBufferNode()->AddNewActiveClientsBitToMask();
140 if (clientStateMask == 0U) {
141 // Reach max client count
142 ALOGE("%s: import failed, BufferNode#%u reached maximum clients.", __FUNCTION__,
143 client->getBufferNode()->id());
144 _hidl_cb(/*status=*/BufferHubStatus::MAX_CLIENT, /*bufferClient=*/nullptr,
145 /*bufferTraits=*/{});
146 return Void();
147 }
Fan Xu467e08f2018-11-09 15:58:51 -0800148
Fan Xucd74d782018-11-26 13:51:25 -0800149 std::lock_guard<std::mutex> lock(mClientSetMutex);
150 mClientSet.emplace(client);
Fan Xuf8f4a452018-11-29 16:26:30 -0800151
152 std::shared_ptr<BufferNode> node = client->getBufferNode();
153
154 HardwareBufferDescription bufferDesc;
155 memcpy(&bufferDesc, &node->buffer_desc(), sizeof(HardwareBufferDescription));
156
Fan Xuefce32e2018-12-12 14:34:16 -0800157 hidl_handle bufferInfo =
158 buildBufferInfo(node->id(), clientStateMask, node->user_metadata_size(),
159 node->metadata().ashmem_fd());
Fan Xuf8f4a452018-11-29 16:26:30 -0800160 BufferTraits bufferTraits = {/*bufferDesc=*/bufferDesc,
161 /*bufferHandle=*/hidl_handle(node->buffer_handle()),
Fan Xuefce32e2018-12-12 14:34:16 -0800162 /*bufferInfo=*/bufferInfo};
Fan Xuf8f4a452018-11-29 16:26:30 -0800163
164 _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
165 /*bufferTraits=*/bufferTraits);
Fan Xuca70b7b2018-10-31 13:20:12 -0700166 return Void();
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -0700167}
168
Fan Xufe097c72018-12-07 15:46:51 -0800169Return<void> BufferHubService::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) {
170 if (fd.getNativeHandle() == nullptr || fd->numFds < 1) {
171 ALOGE("%s: missing fd for writing.", __FUNCTION__);
172 return Void();
173 }
174
175 FILE* out = fdopen(dup(fd->data[0]), "w");
176
177 if (args.size() != 0) {
178 fprintf(out,
179 "Note: lshal bufferhub currently does not support args. Input arguments are "
180 "ignored.\n");
181 }
182
183 std::ostringstream stream;
184
185 // Get the number of clients of each buffer.
186 // Map from bufferId to bufferNode_clientCount pair.
187 std::map<int, std::pair<const std::shared_ptr<BufferNode>, uint32_t>> clientCount;
188 {
189 std::lock_guard<std::mutex> lock(mClientSetMutex);
190 for (auto iter = mClientSet.begin(); iter != mClientSet.end(); ++iter) {
191 sp<BufferClient> client = iter->promote();
192 if (client != nullptr) {
193 const std::shared_ptr<BufferNode> node = client->getBufferNode();
194 auto mapIter = clientCount.find(node->id());
195 if (mapIter != clientCount.end()) {
196 ++mapIter->second.second;
197 } else {
198 clientCount.emplace(node->id(),
199 std::pair<std::shared_ptr<BufferNode>, uint32_t>(node, 1U));
200 }
201 }
202 }
203 }
204
205 stream << "Active Buffers:\n";
206 stream << std::right;
207 stream << std::setw(6) << "Id";
208 stream << " ";
209 stream << std::setw(9) << "Clients";
210 stream << " ";
211 stream << std::setw(14) << "Geometry";
212 stream << " ";
213 stream << std::setw(6) << "Format";
214 stream << " ";
215 stream << std::setw(10) << "Usage";
216 stream << " ";
217 stream << std::setw(10) << "State";
218 stream << " ";
219 stream << std::setw(10) << "Index";
220 stream << std::endl;
221
222 for (auto iter = clientCount.begin(); iter != clientCount.end(); ++iter) {
223 const std::shared_ptr<BufferNode> node = std::move(iter->second.first);
224 const uint32_t clientCount = iter->second.second;
225 AHardwareBuffer_Desc desc = node->buffer_desc();
226
227 MetadataHeader* metadataHeader =
228 const_cast<BufferHubMetadata*>(&node->metadata())->metadata_header();
229 const uint32_t state = metadataHeader->buffer_state.load(std::memory_order_acquire);
230 const uint64_t index = metadataHeader->queue_index;
231
232 stream << std::right;
233 stream << std::setw(6) << /*Id=*/node->id();
234 stream << " ";
235 stream << std::setw(9) << /*Clients=*/clientCount;
236 stream << " ";
237 if (desc.format == HAL_PIXEL_FORMAT_BLOB) {
238 std::string size = std::to_string(desc.width) + " B";
239 stream << std::setw(14) << /*Geometry=*/size;
240 } else {
241 std::string dimensions = std::to_string(desc.width) + "x" +
242 std::to_string(desc.height) + "x" + std::to_string(desc.layers);
243 stream << std::setw(14) << /*Geometry=*/dimensions;
244 }
245 stream << " ";
246 stream << std::setw(6) << /*Format=*/desc.format;
247 stream << " ";
248 stream << "0x" << std::hex << std::setfill('0');
249 stream << std::setw(8) << /*Usage=*/desc.usage;
250 stream << std::dec << std::setfill(' ');
251 stream << " ";
252 stream << "0x" << std::hex << std::setfill('0');
253 stream << std::setw(8) << /*State=*/state;
254 stream << " ";
255 stream << std::setw(8) << /*Index=*/index;
256 stream << std::endl;
257 }
258
259 stream << std::endl;
260
261 // Get the number of tokens of each buffer.
262 // Map from bufferId to tokenCount
263 std::map<int, uint32_t> tokenCount;
264 {
Fan Xu55b26a62018-12-19 11:03:14 -0800265 std::lock_guard<std::mutex> lock(mTokenMutex);
Fan Xufe097c72018-12-07 15:46:51 -0800266 for (auto iter = mTokenMap.begin(); iter != mTokenMap.end(); ++iter) {
Fan Xu55b26a62018-12-19 11:03:14 -0800267 sp<BufferClient> client = iter->second.second.promote();
Fan Xufe097c72018-12-07 15:46:51 -0800268 if (client != nullptr) {
269 const std::shared_ptr<BufferNode> node = client->getBufferNode();
270 auto mapIter = tokenCount.find(node->id());
271 if (mapIter != tokenCount.end()) {
272 ++mapIter->second;
273 } else {
274 tokenCount.emplace(node->id(), 1U);
275 }
276 }
277 }
278 }
279
280 stream << "Unused Tokens:\n";
281 stream << std::right;
282 stream << std::setw(8) << "Buffer Id";
283 stream << " ";
284 stream << std::setw(6) << "Tokens";
285 stream << std::endl;
286
287 for (auto iter = tokenCount.begin(); iter != tokenCount.end(); ++iter) {
288 stream << std::right;
289 stream << std::setw(8) << /*Buffer Id=*/iter->first;
290 stream << " ";
291 stream << std::setw(6) << /*Tokens=*/iter->second;
292 stream << std::endl;
293 }
294
295 fprintf(out, "%s", stream.str().c_str());
296
297 fclose(out);
298 return Void();
299}
300
Fan Xud6cd6ba2018-11-15 16:46:55 -0800301hidl_handle BufferHubService::registerToken(const wp<BufferClient>& client) {
Fan Xu55b26a62018-12-19 11:03:14 -0800302 // Find next available token id
303 std::lock_guard<std::mutex> lock(mTokenMutex);
Fan Xu18d90ea2018-11-06 15:46:44 -0800304 do {
Fan Xu55b26a62018-12-19 11:03:14 -0800305 ++mLastTokenId;
306 } while (mTokenMap.find(mLastTokenId) != mTokenMap.end());
Fan Xu18d90ea2018-11-06 15:46:44 -0800307
Fan Xu55b26a62018-12-19 11:03:14 -0800308 std::array<uint8_t, EVP_MAX_MD_SIZE> hmac;
309 uint32_t hmacSize = 0U;
310
311 HMAC(/*evp_md=*/EVP_sha256(), /*key=*/&mKey, /*key_len=*/kKeyLen,
312 /*data=*/(uint8_t*)&mLastTokenId, /*data_len=*/mTokenIdSize,
313 /*out=*/hmac.data(), /*out_len=*/&hmacSize);
314
315 int numIntsForHMAC = (int)ceil(hmacSize / (double)sizeof(int));
316 native_handle_t* handle = native_handle_create(/*numFds=*/0, /*numInts=*/1 + numIntsForHMAC);
317 handle->data[0] = mLastTokenId;
318 // Set all the the bits of last int to 0 since it might not be fully overwritten
319 handle->data[numIntsForHMAC] = 0;
320 memcpy(&handle->data[1], hmac.data(), hmacSize);
Fan Xu18d90ea2018-11-06 15:46:44 -0800321
322 // returnToken owns the native_handle_t* thus doing lifecycle management
323 hidl_handle returnToken;
324 returnToken.setTo(handle, /*shoudOwn=*/true);
325
Fan Xu55b26a62018-12-19 11:03:14 -0800326 std::vector<uint8_t> hmacVec;
327 hmacVec.resize(hmacSize);
328 memcpy(hmacVec.data(), hmac.data(), hmacSize);
329 mTokenMap.emplace(mLastTokenId, std::pair(hmacVec, client));
330
Fan Xu18d90ea2018-11-06 15:46:44 -0800331 return returnToken;
332}
333
Fan Xua7422fe2018-11-19 15:21:32 -0800334void BufferHubService::onClientClosed(const BufferClient* client) {
335 removeTokenByClient(client);
336
Fan Xucd74d782018-11-26 13:51:25 -0800337 std::lock_guard<std::mutex> lock(mClientSetMutex);
338 auto iter = std::find(mClientSet.begin(), mClientSet.end(), client);
339 if (iter != mClientSet.end()) {
340 mClientSet.erase(iter);
Fan Xua7422fe2018-11-19 15:21:32 -0800341 }
342}
343
Fan Xuefce32e2018-12-12 14:34:16 -0800344// Implementation of this function should be consistent with the definition of bufferInfo handle in
345// ui/BufferHubDefs.h.
Fan Xu6b4b1e52018-12-18 14:03:44 -0800346hidl_handle BufferHubService::buildBufferInfo(int bufferId, uint32_t clientBitMask,
Fan Xuefce32e2018-12-12 14:34:16 -0800347 uint32_t userMetadataSize, const int metadataFd) {
348 native_handle_t* infoHandle = native_handle_create(BufferHubDefs::kBufferInfoNumFds,
349 BufferHubDefs::kBufferInfoNumInts);
350
351 infoHandle->data[0] = dup(metadataFd);
Fan Xu6b4b1e52018-12-18 14:03:44 -0800352 infoHandle->data[1] = bufferId;
Fan Xuefce32e2018-12-12 14:34:16 -0800353 // Use memcpy to convert to int without missing digit.
354 // TOOD(b/121345852): use bit_cast to unpack bufferInfo when C++20 becomes available.
Fan Xuefce32e2018-12-12 14:34:16 -0800355 memcpy(&infoHandle->data[2], &clientBitMask, sizeof(clientBitMask));
356 memcpy(&infoHandle->data[3], &userMetadataSize, sizeof(userMetadataSize));
357
358 hidl_handle bufferInfo;
359 bufferInfo.setTo(infoHandle, /*shouldOwn=*/true);
360
361 return bufferInfo;
362}
363
Fan Xua7422fe2018-11-19 15:21:32 -0800364void BufferHubService::removeTokenByClient(const BufferClient* client) {
Fan Xu55b26a62018-12-19 11:03:14 -0800365 std::lock_guard<std::mutex> lock(mTokenMutex);
Fan Xua7422fe2018-11-19 15:21:32 -0800366 auto iter = mTokenMap.begin();
367 while (iter != mTokenMap.end()) {
Fan Xu55b26a62018-12-19 11:03:14 -0800368 if (iter->second.second == client) {
Fan Xua7422fe2018-11-19 15:21:32 -0800369 auto oldIter = iter;
370 ++iter;
371 mTokenMap.erase(oldIter);
372 } else {
373 ++iter;
374 }
375 }
376}
377
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -0700378} // namespace implementation
379} // namespace V1_0
380} // namespace bufferhub
381} // namespace frameworks
382} // namespace android