blob: 43235f24c3beee3d3175547972e254e7e94ed277 [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>
28
29using ::android::BufferHubDefs::MetadataHeader;
30using ::android::hardware::Void;
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070031
32namespace android {
33namespace frameworks {
34namespace bufferhub {
35namespace V1_0 {
36namespace implementation {
37
Fan Xu55b26a62018-12-19 11:03:14 -080038BufferHubService::BufferHubService() {
39 std::mt19937_64 randomEngine;
40 randomEngine.seed(time(nullptr));
41
42 mKey = randomEngine();
43}
44
Fan Xu93c94902018-11-01 12:22:05 -070045Return<void> BufferHubService::allocateBuffer(const HardwareBufferDescription& description,
46 const uint32_t userMetadataSize,
Fan Xuca70b7b2018-10-31 13:20:12 -070047 allocateBuffer_cb _hidl_cb) {
Fan Xu93c94902018-11-01 12:22:05 -070048 AHardwareBuffer_Desc desc;
49 memcpy(&desc, &description, sizeof(AHardwareBuffer_Desc));
50
51 std::shared_ptr<BufferNode> node =
52 std::make_shared<BufferNode>(desc.width, desc.height, desc.layers, desc.format,
Fan Xu1c16df52018-11-19 16:27:27 -080053 desc.usage, userMetadataSize,
54 BufferHubIdGenerator::getInstance().getId());
Fan Xu93c94902018-11-01 12:22:05 -070055 if (node == nullptr || !node->IsValid()) {
56 ALOGE("%s: creating BufferNode failed.", __FUNCTION__);
Fan Xuf8f4a452018-11-29 16:26:30 -080057 _hidl_cb(/*status=*/BufferHubStatus::ALLOCATION_FAILED, /*bufferClient=*/nullptr,
58 /*bufferTraits=*/{});
Fan Xu93c94902018-11-01 12:22:05 -070059 return Void();
60 }
61
Fan Xu18d90ea2018-11-06 15:46:44 -080062 sp<BufferClient> client = BufferClient::create(this, node);
Fan Xu93c94902018-11-01 12:22:05 -070063 // Add it to list for bookkeeping and dumpsys.
Fan Xucd74d782018-11-26 13:51:25 -080064 std::lock_guard<std::mutex> lock(mClientSetMutex);
65 mClientSet.emplace(client);
Fan Xu93c94902018-11-01 12:22:05 -070066
Fan Xuf8f4a452018-11-29 16:26:30 -080067 BufferTraits bufferTraits = {/*bufferDesc=*/description,
68 /*bufferHandle=*/hidl_handle(node->buffer_handle()),
69 // TODO(b/116681016): return real data to client
70 /*bufferInfo=*/hidl_handle()};
71
72 _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
73 /*bufferTraits=*/bufferTraits);
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070074 return Void();
75}
76
Fan Xu467e08f2018-11-09 15:58:51 -080077Return<void> BufferHubService::importBuffer(const hidl_handle& tokenHandle,
Fan Xuca70b7b2018-10-31 13:20:12 -070078 importBuffer_cb _hidl_cb) {
Fan Xu55b26a62018-12-19 11:03:14 -080079 if (!tokenHandle.getNativeHandle() || tokenHandle->numFds != 0 || tokenHandle->numInts <= 1) {
Fan Xu467e08f2018-11-09 15:58:51 -080080 // nullptr handle or wrong format
Fan Xuf8f4a452018-11-29 16:26:30 -080081 _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr,
82 /*bufferTraits=*/{});
Fan Xu467e08f2018-11-09 15:58:51 -080083 return Void();
84 }
85
Fan Xu55b26a62018-12-19 11:03:14 -080086 int tokenId = tokenHandle->data[0];
Fan Xu467e08f2018-11-09 15:58:51 -080087
88 wp<BufferClient> originClientWp;
89 {
Fan Xu55b26a62018-12-19 11:03:14 -080090 std::lock_guard<std::mutex> lock(mTokenMutex);
91 auto iter = mTokenMap.find(tokenId);
Fan Xu467e08f2018-11-09 15:58:51 -080092 if (iter == mTokenMap.end()) {
Fan Xu55b26a62018-12-19 11:03:14 -080093 // Token Id not exist
94 ALOGD("%s: token #%d not found.", __FUNCTION__, tokenId);
Fan Xuf8f4a452018-11-29 16:26:30 -080095 _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr,
96 /*bufferTraits=*/{});
Fan Xu467e08f2018-11-09 15:58:51 -080097 return Void();
98 }
99
Fan Xu55b26a62018-12-19 11:03:14 -0800100 const std::vector<uint8_t>& tokenHMAC = iter->second.first;
101
102 int numIntsForHMAC = (int)ceil(tokenHMAC.size() * sizeof(uint8_t) / (double)sizeof(int));
103 if (tokenHandle->numInts - 1 != numIntsForHMAC) {
104 // HMAC size not match
105 ALOGD("%s: token #%d HMAC size not match. Expected: %d Actual: %d", __FUNCTION__,
106 tokenId, numIntsForHMAC, tokenHandle->numInts - 1);
107 _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr,
108 /*bufferTraits=*/{});
109 return Void();
110 }
111
112 size_t hmacSize = tokenHMAC.size() * sizeof(uint8_t);
113 if (memcmp(tokenHMAC.data(), &tokenHandle->data[1], hmacSize) != 0) {
114 // HMAC not match
115 ALOGD("%s: token #%d HMAC not match.", __FUNCTION__, tokenId);
116 _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr,
117 /*bufferTraits=*/{});
118 return Void();
119 }
120
121 originClientWp = iter->second.second;
Fan Xu467e08f2018-11-09 15:58:51 -0800122 mTokenMap.erase(iter);
123 }
124
125 // Check if original client is dead
126 sp<BufferClient> originClient = originClientWp.promote();
127 if (!originClient) {
128 // Should not happen since token should be removed if already gone
129 ALOGE("%s: original client %p gone!", __FUNCTION__, originClientWp.unsafe_get());
Fan Xuf8f4a452018-11-29 16:26:30 -0800130 _hidl_cb(/*status=*/BufferHubStatus::BUFFER_FREED, /*bufferClient=*/nullptr,
131 /*bufferTraits=*/{});
Fan Xu467e08f2018-11-09 15:58:51 -0800132 return Void();
133 }
134
135 sp<BufferClient> client = new BufferClient(*originClient);
Fan Xuf8f4a452018-11-29 16:26:30 -0800136 uint32_t clientStateMask = client->getBufferNode()->AddNewActiveClientsBitToMask();
137 if (clientStateMask == 0U) {
138 // Reach max client count
139 ALOGE("%s: import failed, BufferNode#%u reached maximum clients.", __FUNCTION__,
140 client->getBufferNode()->id());
141 _hidl_cb(/*status=*/BufferHubStatus::MAX_CLIENT, /*bufferClient=*/nullptr,
142 /*bufferTraits=*/{});
143 return Void();
144 }
Fan Xu467e08f2018-11-09 15:58:51 -0800145
Fan Xucd74d782018-11-26 13:51:25 -0800146 std::lock_guard<std::mutex> lock(mClientSetMutex);
147 mClientSet.emplace(client);
Fan Xuf8f4a452018-11-29 16:26:30 -0800148
149 std::shared_ptr<BufferNode> node = client->getBufferNode();
150
151 HardwareBufferDescription bufferDesc;
152 memcpy(&bufferDesc, &node->buffer_desc(), sizeof(HardwareBufferDescription));
153
154 BufferTraits bufferTraits = {/*bufferDesc=*/bufferDesc,
155 /*bufferHandle=*/hidl_handle(node->buffer_handle()),
156 // TODO(b/116681016): return real data to client
157 /*bufferInfo=*/hidl_handle()};
158
159 _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client,
160 /*bufferTraits=*/bufferTraits);
Fan Xuca70b7b2018-10-31 13:20:12 -0700161 return Void();
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -0700162}
163
Fan Xufe097c72018-12-07 15:46:51 -0800164Return<void> BufferHubService::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) {
165 if (fd.getNativeHandle() == nullptr || fd->numFds < 1) {
166 ALOGE("%s: missing fd for writing.", __FUNCTION__);
167 return Void();
168 }
169
170 FILE* out = fdopen(dup(fd->data[0]), "w");
171
172 if (args.size() != 0) {
173 fprintf(out,
174 "Note: lshal bufferhub currently does not support args. Input arguments are "
175 "ignored.\n");
176 }
177
178 std::ostringstream stream;
179
180 // Get the number of clients of each buffer.
181 // Map from bufferId to bufferNode_clientCount pair.
182 std::map<int, std::pair<const std::shared_ptr<BufferNode>, uint32_t>> clientCount;
183 {
184 std::lock_guard<std::mutex> lock(mClientSetMutex);
185 for (auto iter = mClientSet.begin(); iter != mClientSet.end(); ++iter) {
186 sp<BufferClient> client = iter->promote();
187 if (client != nullptr) {
188 const std::shared_ptr<BufferNode> node = client->getBufferNode();
189 auto mapIter = clientCount.find(node->id());
190 if (mapIter != clientCount.end()) {
191 ++mapIter->second.second;
192 } else {
193 clientCount.emplace(node->id(),
194 std::pair<std::shared_ptr<BufferNode>, uint32_t>(node, 1U));
195 }
196 }
197 }
198 }
199
200 stream << "Active Buffers:\n";
201 stream << std::right;
202 stream << std::setw(6) << "Id";
203 stream << " ";
204 stream << std::setw(9) << "Clients";
205 stream << " ";
206 stream << std::setw(14) << "Geometry";
207 stream << " ";
208 stream << std::setw(6) << "Format";
209 stream << " ";
210 stream << std::setw(10) << "Usage";
211 stream << " ";
212 stream << std::setw(10) << "State";
213 stream << " ";
214 stream << std::setw(10) << "Index";
215 stream << std::endl;
216
217 for (auto iter = clientCount.begin(); iter != clientCount.end(); ++iter) {
218 const std::shared_ptr<BufferNode> node = std::move(iter->second.first);
219 const uint32_t clientCount = iter->second.second;
220 AHardwareBuffer_Desc desc = node->buffer_desc();
221
222 MetadataHeader* metadataHeader =
223 const_cast<BufferHubMetadata*>(&node->metadata())->metadata_header();
224 const uint32_t state = metadataHeader->buffer_state.load(std::memory_order_acquire);
225 const uint64_t index = metadataHeader->queue_index;
226
227 stream << std::right;
228 stream << std::setw(6) << /*Id=*/node->id();
229 stream << " ";
230 stream << std::setw(9) << /*Clients=*/clientCount;
231 stream << " ";
232 if (desc.format == HAL_PIXEL_FORMAT_BLOB) {
233 std::string size = std::to_string(desc.width) + " B";
234 stream << std::setw(14) << /*Geometry=*/size;
235 } else {
236 std::string dimensions = std::to_string(desc.width) + "x" +
237 std::to_string(desc.height) + "x" + std::to_string(desc.layers);
238 stream << std::setw(14) << /*Geometry=*/dimensions;
239 }
240 stream << " ";
241 stream << std::setw(6) << /*Format=*/desc.format;
242 stream << " ";
243 stream << "0x" << std::hex << std::setfill('0');
244 stream << std::setw(8) << /*Usage=*/desc.usage;
245 stream << std::dec << std::setfill(' ');
246 stream << " ";
247 stream << "0x" << std::hex << std::setfill('0');
248 stream << std::setw(8) << /*State=*/state;
249 stream << " ";
250 stream << std::setw(8) << /*Index=*/index;
251 stream << std::endl;
252 }
253
254 stream << std::endl;
255
256 // Get the number of tokens of each buffer.
257 // Map from bufferId to tokenCount
258 std::map<int, uint32_t> tokenCount;
259 {
Fan Xu55b26a62018-12-19 11:03:14 -0800260 std::lock_guard<std::mutex> lock(mTokenMutex);
Fan Xufe097c72018-12-07 15:46:51 -0800261 for (auto iter = mTokenMap.begin(); iter != mTokenMap.end(); ++iter) {
Fan Xu55b26a62018-12-19 11:03:14 -0800262 sp<BufferClient> client = iter->second.second.promote();
Fan Xufe097c72018-12-07 15:46:51 -0800263 if (client != nullptr) {
264 const std::shared_ptr<BufferNode> node = client->getBufferNode();
265 auto mapIter = tokenCount.find(node->id());
266 if (mapIter != tokenCount.end()) {
267 ++mapIter->second;
268 } else {
269 tokenCount.emplace(node->id(), 1U);
270 }
271 }
272 }
273 }
274
275 stream << "Unused Tokens:\n";
276 stream << std::right;
277 stream << std::setw(8) << "Buffer Id";
278 stream << " ";
279 stream << std::setw(6) << "Tokens";
280 stream << std::endl;
281
282 for (auto iter = tokenCount.begin(); iter != tokenCount.end(); ++iter) {
283 stream << std::right;
284 stream << std::setw(8) << /*Buffer Id=*/iter->first;
285 stream << " ";
286 stream << std::setw(6) << /*Tokens=*/iter->second;
287 stream << std::endl;
288 }
289
290 fprintf(out, "%s", stream.str().c_str());
291
292 fclose(out);
293 return Void();
294}
295
Fan Xud6cd6ba2018-11-15 16:46:55 -0800296hidl_handle BufferHubService::registerToken(const wp<BufferClient>& client) {
Fan Xu55b26a62018-12-19 11:03:14 -0800297 // Find next available token id
298 std::lock_guard<std::mutex> lock(mTokenMutex);
Fan Xu18d90ea2018-11-06 15:46:44 -0800299 do {
Fan Xu55b26a62018-12-19 11:03:14 -0800300 ++mLastTokenId;
301 } while (mTokenMap.find(mLastTokenId) != mTokenMap.end());
Fan Xu18d90ea2018-11-06 15:46:44 -0800302
Fan Xu55b26a62018-12-19 11:03:14 -0800303 std::array<uint8_t, EVP_MAX_MD_SIZE> hmac;
304 uint32_t hmacSize = 0U;
305
306 HMAC(/*evp_md=*/EVP_sha256(), /*key=*/&mKey, /*key_len=*/kKeyLen,
307 /*data=*/(uint8_t*)&mLastTokenId, /*data_len=*/mTokenIdSize,
308 /*out=*/hmac.data(), /*out_len=*/&hmacSize);
309
310 int numIntsForHMAC = (int)ceil(hmacSize / (double)sizeof(int));
311 native_handle_t* handle = native_handle_create(/*numFds=*/0, /*numInts=*/1 + numIntsForHMAC);
312 handle->data[0] = mLastTokenId;
313 // Set all the the bits of last int to 0 since it might not be fully overwritten
314 handle->data[numIntsForHMAC] = 0;
315 memcpy(&handle->data[1], hmac.data(), hmacSize);
Fan Xu18d90ea2018-11-06 15:46:44 -0800316
317 // returnToken owns the native_handle_t* thus doing lifecycle management
318 hidl_handle returnToken;
319 returnToken.setTo(handle, /*shoudOwn=*/true);
320
Fan Xu55b26a62018-12-19 11:03:14 -0800321 std::vector<uint8_t> hmacVec;
322 hmacVec.resize(hmacSize);
323 memcpy(hmacVec.data(), hmac.data(), hmacSize);
324 mTokenMap.emplace(mLastTokenId, std::pair(hmacVec, client));
325
Fan Xu18d90ea2018-11-06 15:46:44 -0800326 return returnToken;
327}
328
Fan Xua7422fe2018-11-19 15:21:32 -0800329void BufferHubService::onClientClosed(const BufferClient* client) {
330 removeTokenByClient(client);
331
Fan Xucd74d782018-11-26 13:51:25 -0800332 std::lock_guard<std::mutex> lock(mClientSetMutex);
333 auto iter = std::find(mClientSet.begin(), mClientSet.end(), client);
334 if (iter != mClientSet.end()) {
335 mClientSet.erase(iter);
Fan Xua7422fe2018-11-19 15:21:32 -0800336 }
337}
338
339void BufferHubService::removeTokenByClient(const BufferClient* client) {
Fan Xu55b26a62018-12-19 11:03:14 -0800340 std::lock_guard<std::mutex> lock(mTokenMutex);
Fan Xua7422fe2018-11-19 15:21:32 -0800341 auto iter = mTokenMap.begin();
342 while (iter != mTokenMap.end()) {
Fan Xu55b26a62018-12-19 11:03:14 -0800343 if (iter->second.second == client) {
Fan Xua7422fe2018-11-19 15:21:32 -0800344 auto oldIter = iter;
345 ++iter;
346 mTokenMap.erase(oldIter);
347 } else {
348 ++iter;
349 }
350 }
351}
352
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -0700353} // namespace implementation
354} // namespace V1_0
355} // namespace bufferhub
356} // namespace frameworks
357} // namespace android