Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 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 | |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 17 | #include <array> |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 18 | #include <iomanip> |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 19 | #include <random> |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 20 | #include <sstream> |
| 21 | |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 22 | #include <android/hardware_buffer.h> |
Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 23 | #include <bufferhub/BufferHubService.h> |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 24 | #include <cutils/native_handle.h> |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 25 | #include <log/log.h> |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 26 | #include <openssl/hmac.h> |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 27 | #include <system/graphics-base.h> |
Fan Xu | efce32e | 2018-12-12 14:34:16 -0800 | [diff] [blame] | 28 | #include <ui/BufferHubDefs.h> |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 29 | |
| 30 | using ::android::BufferHubDefs::MetadataHeader; |
| 31 | using ::android::hardware::Void; |
Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace frameworks { |
| 35 | namespace bufferhub { |
| 36 | namespace V1_0 { |
| 37 | namespace implementation { |
| 38 | |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 39 | BufferHubService::BufferHubService() { |
| 40 | std::mt19937_64 randomEngine; |
| 41 | randomEngine.seed(time(nullptr)); |
| 42 | |
| 43 | mKey = randomEngine(); |
| 44 | } |
| 45 | |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 46 | Return<void> BufferHubService::allocateBuffer(const HardwareBufferDescription& description, |
| 47 | const uint32_t userMetadataSize, |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 48 | allocateBuffer_cb _hidl_cb) { |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 49 | 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 Xu | 1c16df5 | 2018-11-19 16:27:27 -0800 | [diff] [blame] | 54 | desc.usage, userMetadataSize, |
| 55 | BufferHubIdGenerator::getInstance().getId()); |
Tianyu Jiang | 727ede4 | 2019-02-01 11:44:51 -0800 | [diff] [blame] | 56 | if (node == nullptr || !node->isValid()) { |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 57 | ALOGE("%s: creating BufferNode failed.", __FUNCTION__); |
Fan Xu | f8f4a45 | 2018-11-29 16:26:30 -0800 | [diff] [blame] | 58 | _hidl_cb(/*status=*/BufferHubStatus::ALLOCATION_FAILED, /*bufferClient=*/nullptr, |
| 59 | /*bufferTraits=*/{}); |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 60 | return Void(); |
| 61 | } |
| 62 | |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 63 | sp<BufferClient> client = BufferClient::create(this, node); |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 64 | // Add it to list for bookkeeping and dumpsys. |
Fan Xu | cd74d78 | 2018-11-26 13:51:25 -0800 | [diff] [blame] | 65 | std::lock_guard<std::mutex> lock(mClientSetMutex); |
| 66 | mClientSet.emplace(client); |
Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 67 | |
Tianyu Jiang | 8adb319 | 2019-01-29 10:54:24 -0800 | [diff] [blame] | 68 | // Allocate memory for bufferInfo of type hidl_handle on the stack. See |
| 69 | // http://aosp/286282 for the usage of NATIVE_HANDLE_DECLARE_STORAGE. |
| 70 | NATIVE_HANDLE_DECLARE_STORAGE(bufferInfoStorage, BufferHubDefs::kBufferInfoNumFds, |
| 71 | BufferHubDefs::kBufferInfoNumInts); |
| 72 | hidl_handle bufferInfo = |
Tianyu Jiang | 727ede4 | 2019-02-01 11:44:51 -0800 | [diff] [blame] | 73 | buildBufferInfo(bufferInfoStorage, node->id(), node->addNewActiveClientsBitToMask(), |
| 74 | node->userMetadataSize(), node->metadata().ashmemFd(), |
Tianyu Jiang | 8adb319 | 2019-01-29 10:54:24 -0800 | [diff] [blame] | 75 | node->eventFd().get()); |
Jiwen 'Steve' Cai | dfe0f4c | 2019-01-15 21:49:07 -0800 | [diff] [blame] | 76 | // During the gralloc allocation carried out by BufferNode, gralloc allocator will populate the |
| 77 | // fields of its HardwareBufferDescription (i.e. strides) according to the actual |
| 78 | // gralloc implementation. We need to read those fields back and send them to the client via |
| 79 | // BufferTraits. |
| 80 | HardwareBufferDescription allocatedBufferDesc; |
| 81 | memcpy(&allocatedBufferDesc, &node->bufferDesc(), sizeof(AHardwareBuffer_Desc)); |
| 82 | BufferTraits bufferTraits = {/*bufferDesc=*/allocatedBufferDesc, |
Tianyu Jiang | 727ede4 | 2019-02-01 11:44:51 -0800 | [diff] [blame] | 83 | /*bufferHandle=*/hidl_handle(node->bufferHandle()), |
Tianyu Jiang | 8adb319 | 2019-01-29 10:54:24 -0800 | [diff] [blame] | 84 | /*bufferInfo=*/std::move(bufferInfo)}; |
Fan Xu | f8f4a45 | 2018-11-29 16:26:30 -0800 | [diff] [blame] | 85 | |
| 86 | _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client, |
Tianyu Jiang | 8adb319 | 2019-01-29 10:54:24 -0800 | [diff] [blame] | 87 | /*bufferTraits=*/std::move(bufferTraits)); |
Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 88 | return Void(); |
| 89 | } |
| 90 | |
Fan Xu | 467e08f | 2018-11-09 15:58:51 -0800 | [diff] [blame] | 91 | Return<void> BufferHubService::importBuffer(const hidl_handle& tokenHandle, |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 92 | importBuffer_cb _hidl_cb) { |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 93 | if (!tokenHandle.getNativeHandle() || tokenHandle->numFds != 0 || tokenHandle->numInts <= 1) { |
Fan Xu | 467e08f | 2018-11-09 15:58:51 -0800 | [diff] [blame] | 94 | // nullptr handle or wrong format |
Fan Xu | f8f4a45 | 2018-11-29 16:26:30 -0800 | [diff] [blame] | 95 | _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr, |
| 96 | /*bufferTraits=*/{}); |
Fan Xu | 467e08f | 2018-11-09 15:58:51 -0800 | [diff] [blame] | 97 | return Void(); |
| 98 | } |
| 99 | |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 100 | int tokenId = tokenHandle->data[0]; |
Fan Xu | 467e08f | 2018-11-09 15:58:51 -0800 | [diff] [blame] | 101 | |
| 102 | wp<BufferClient> originClientWp; |
| 103 | { |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 104 | std::lock_guard<std::mutex> lock(mTokenMutex); |
| 105 | auto iter = mTokenMap.find(tokenId); |
Fan Xu | 467e08f | 2018-11-09 15:58:51 -0800 | [diff] [blame] | 106 | if (iter == mTokenMap.end()) { |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 107 | // Token Id not exist |
| 108 | ALOGD("%s: token #%d not found.", __FUNCTION__, tokenId); |
Fan Xu | f8f4a45 | 2018-11-29 16:26:30 -0800 | [diff] [blame] | 109 | _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr, |
| 110 | /*bufferTraits=*/{}); |
Fan Xu | 467e08f | 2018-11-09 15:58:51 -0800 | [diff] [blame] | 111 | return Void(); |
| 112 | } |
| 113 | |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 114 | const std::vector<uint8_t>& tokenHMAC = iter->second.first; |
| 115 | |
| 116 | int numIntsForHMAC = (int)ceil(tokenHMAC.size() * sizeof(uint8_t) / (double)sizeof(int)); |
| 117 | if (tokenHandle->numInts - 1 != numIntsForHMAC) { |
| 118 | // HMAC size not match |
| 119 | ALOGD("%s: token #%d HMAC size not match. Expected: %d Actual: %d", __FUNCTION__, |
| 120 | tokenId, numIntsForHMAC, tokenHandle->numInts - 1); |
| 121 | _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr, |
| 122 | /*bufferTraits=*/{}); |
| 123 | return Void(); |
| 124 | } |
| 125 | |
| 126 | size_t hmacSize = tokenHMAC.size() * sizeof(uint8_t); |
| 127 | if (memcmp(tokenHMAC.data(), &tokenHandle->data[1], hmacSize) != 0) { |
| 128 | // HMAC not match |
| 129 | ALOGD("%s: token #%d HMAC not match.", __FUNCTION__, tokenId); |
| 130 | _hidl_cb(/*status=*/BufferHubStatus::INVALID_TOKEN, /*bufferClient=*/nullptr, |
| 131 | /*bufferTraits=*/{}); |
| 132 | return Void(); |
| 133 | } |
| 134 | |
| 135 | originClientWp = iter->second.second; |
Fan Xu | 467e08f | 2018-11-09 15:58:51 -0800 | [diff] [blame] | 136 | mTokenMap.erase(iter); |
| 137 | } |
| 138 | |
| 139 | // Check if original client is dead |
| 140 | sp<BufferClient> originClient = originClientWp.promote(); |
| 141 | if (!originClient) { |
| 142 | // Should not happen since token should be removed if already gone |
| 143 | ALOGE("%s: original client %p gone!", __FUNCTION__, originClientWp.unsafe_get()); |
Fan Xu | f8f4a45 | 2018-11-29 16:26:30 -0800 | [diff] [blame] | 144 | _hidl_cb(/*status=*/BufferHubStatus::BUFFER_FREED, /*bufferClient=*/nullptr, |
| 145 | /*bufferTraits=*/{}); |
Fan Xu | 467e08f | 2018-11-09 15:58:51 -0800 | [diff] [blame] | 146 | return Void(); |
| 147 | } |
| 148 | |
| 149 | sp<BufferClient> client = new BufferClient(*originClient); |
Tianyu Jiang | 727ede4 | 2019-02-01 11:44:51 -0800 | [diff] [blame] | 150 | uint32_t clientStateMask = client->getBufferNode()->addNewActiveClientsBitToMask(); |
Fan Xu | f8f4a45 | 2018-11-29 16:26:30 -0800 | [diff] [blame] | 151 | if (clientStateMask == 0U) { |
| 152 | // Reach max client count |
| 153 | ALOGE("%s: import failed, BufferNode#%u reached maximum clients.", __FUNCTION__, |
| 154 | client->getBufferNode()->id()); |
| 155 | _hidl_cb(/*status=*/BufferHubStatus::MAX_CLIENT, /*bufferClient=*/nullptr, |
| 156 | /*bufferTraits=*/{}); |
| 157 | return Void(); |
| 158 | } |
Fan Xu | 467e08f | 2018-11-09 15:58:51 -0800 | [diff] [blame] | 159 | |
Fan Xu | cd74d78 | 2018-11-26 13:51:25 -0800 | [diff] [blame] | 160 | std::lock_guard<std::mutex> lock(mClientSetMutex); |
| 161 | mClientSet.emplace(client); |
Fan Xu | f8f4a45 | 2018-11-29 16:26:30 -0800 | [diff] [blame] | 162 | |
| 163 | std::shared_ptr<BufferNode> node = client->getBufferNode(); |
| 164 | |
| 165 | HardwareBufferDescription bufferDesc; |
Tianyu Jiang | 727ede4 | 2019-02-01 11:44:51 -0800 | [diff] [blame] | 166 | memcpy(&bufferDesc, &node->bufferDesc(), sizeof(HardwareBufferDescription)); |
Fan Xu | f8f4a45 | 2018-11-29 16:26:30 -0800 | [diff] [blame] | 167 | |
Tianyu Jiang | 8adb319 | 2019-01-29 10:54:24 -0800 | [diff] [blame] | 168 | // Allocate memory for bufferInfo of type hidl_handle on the stack. See |
| 169 | // http://aosp/286282 for the usage of NATIVE_HANDLE_DECLARE_STORAGE. |
| 170 | NATIVE_HANDLE_DECLARE_STORAGE(bufferInfoStorage, BufferHubDefs::kBufferInfoNumFds, |
| 171 | BufferHubDefs::kBufferInfoNumInts); |
| 172 | hidl_handle bufferInfo = buildBufferInfo(bufferInfoStorage, node->id(), clientStateMask, |
Tianyu Jiang | 727ede4 | 2019-02-01 11:44:51 -0800 | [diff] [blame] | 173 | node->userMetadataSize(), node->metadata().ashmemFd(), |
| 174 | node->eventFd().get()); |
Fan Xu | f8f4a45 | 2018-11-29 16:26:30 -0800 | [diff] [blame] | 175 | BufferTraits bufferTraits = {/*bufferDesc=*/bufferDesc, |
Tianyu Jiang | 727ede4 | 2019-02-01 11:44:51 -0800 | [diff] [blame] | 176 | /*bufferHandle=*/hidl_handle(node->bufferHandle()), |
Tianyu Jiang | 8adb319 | 2019-01-29 10:54:24 -0800 | [diff] [blame] | 177 | /*bufferInfo=*/std::move(bufferInfo)}; |
Fan Xu | f8f4a45 | 2018-11-29 16:26:30 -0800 | [diff] [blame] | 178 | |
| 179 | _hidl_cb(/*status=*/BufferHubStatus::NO_ERROR, /*bufferClient=*/client, |
Tianyu Jiang | 8adb319 | 2019-01-29 10:54:24 -0800 | [diff] [blame] | 180 | /*bufferTraits=*/std::move(bufferTraits)); |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 181 | return Void(); |
Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 184 | Return<void> BufferHubService::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) { |
| 185 | if (fd.getNativeHandle() == nullptr || fd->numFds < 1) { |
| 186 | ALOGE("%s: missing fd for writing.", __FUNCTION__); |
| 187 | return Void(); |
| 188 | } |
| 189 | |
| 190 | FILE* out = fdopen(dup(fd->data[0]), "w"); |
| 191 | |
| 192 | if (args.size() != 0) { |
| 193 | fprintf(out, |
| 194 | "Note: lshal bufferhub currently does not support args. Input arguments are " |
| 195 | "ignored.\n"); |
| 196 | } |
| 197 | |
| 198 | std::ostringstream stream; |
| 199 | |
| 200 | // Get the number of clients of each buffer. |
| 201 | // Map from bufferId to bufferNode_clientCount pair. |
| 202 | std::map<int, std::pair<const std::shared_ptr<BufferNode>, uint32_t>> clientCount; |
| 203 | { |
| 204 | std::lock_guard<std::mutex> lock(mClientSetMutex); |
| 205 | for (auto iter = mClientSet.begin(); iter != mClientSet.end(); ++iter) { |
| 206 | sp<BufferClient> client = iter->promote(); |
| 207 | if (client != nullptr) { |
| 208 | const std::shared_ptr<BufferNode> node = client->getBufferNode(); |
| 209 | auto mapIter = clientCount.find(node->id()); |
| 210 | if (mapIter != clientCount.end()) { |
| 211 | ++mapIter->second.second; |
| 212 | } else { |
| 213 | clientCount.emplace(node->id(), |
| 214 | std::pair<std::shared_ptr<BufferNode>, uint32_t>(node, 1U)); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | stream << "Active Buffers:\n"; |
| 221 | stream << std::right; |
| 222 | stream << std::setw(6) << "Id"; |
| 223 | stream << " "; |
Tianyu Jiang | 0f69e5e | 2019-01-23 17:17:59 -0800 | [diff] [blame] | 224 | stream << std::setw(9) << "#Clients"; |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 225 | stream << " "; |
| 226 | stream << std::setw(14) << "Geometry"; |
| 227 | stream << " "; |
| 228 | stream << std::setw(6) << "Format"; |
| 229 | stream << " "; |
| 230 | stream << std::setw(10) << "Usage"; |
| 231 | stream << " "; |
| 232 | stream << std::setw(10) << "State"; |
| 233 | stream << " "; |
Jiwen 'Steve' Cai | f653d0b | 2019-01-15 10:52:20 -0800 | [diff] [blame] | 234 | stream << std::setw(8) << "Index"; |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 235 | stream << std::endl; |
| 236 | |
| 237 | for (auto iter = clientCount.begin(); iter != clientCount.end(); ++iter) { |
| 238 | const std::shared_ptr<BufferNode> node = std::move(iter->second.first); |
| 239 | const uint32_t clientCount = iter->second.second; |
Tianyu Jiang | 727ede4 | 2019-02-01 11:44:51 -0800 | [diff] [blame] | 240 | AHardwareBuffer_Desc desc = node->bufferDesc(); |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 241 | |
| 242 | MetadataHeader* metadataHeader = |
Tianyu Jiang | 727ede4 | 2019-02-01 11:44:51 -0800 | [diff] [blame] | 243 | const_cast<BufferHubMetadata*>(&node->metadata())->metadataHeader(); |
Tianyu Jiang | f377a76 | 2019-02-13 13:46:42 -0800 | [diff] [blame] | 244 | const uint32_t state = metadataHeader->bufferState.load(std::memory_order_acquire); |
| 245 | const uint64_t index = metadataHeader->queueIndex; |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 246 | |
| 247 | stream << std::right; |
| 248 | stream << std::setw(6) << /*Id=*/node->id(); |
| 249 | stream << " "; |
Tianyu Jiang | 0f69e5e | 2019-01-23 17:17:59 -0800 | [diff] [blame] | 250 | stream << std::setw(9) << /*#Clients=*/clientCount; |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 251 | stream << " "; |
| 252 | if (desc.format == HAL_PIXEL_FORMAT_BLOB) { |
| 253 | std::string size = std::to_string(desc.width) + " B"; |
| 254 | stream << std::setw(14) << /*Geometry=*/size; |
| 255 | } else { |
| 256 | std::string dimensions = std::to_string(desc.width) + "x" + |
| 257 | std::to_string(desc.height) + "x" + std::to_string(desc.layers); |
| 258 | stream << std::setw(14) << /*Geometry=*/dimensions; |
| 259 | } |
| 260 | stream << " "; |
| 261 | stream << std::setw(6) << /*Format=*/desc.format; |
| 262 | stream << " "; |
| 263 | stream << "0x" << std::hex << std::setfill('0'); |
| 264 | stream << std::setw(8) << /*Usage=*/desc.usage; |
| 265 | stream << std::dec << std::setfill(' '); |
| 266 | stream << " "; |
| 267 | stream << "0x" << std::hex << std::setfill('0'); |
| 268 | stream << std::setw(8) << /*State=*/state; |
Jiwen 'Steve' Cai | f653d0b | 2019-01-15 10:52:20 -0800 | [diff] [blame] | 269 | stream << std::dec << std::setfill(' '); |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 270 | stream << " "; |
| 271 | stream << std::setw(8) << /*Index=*/index; |
| 272 | stream << std::endl; |
| 273 | } |
| 274 | |
| 275 | stream << std::endl; |
| 276 | |
| 277 | // Get the number of tokens of each buffer. |
| 278 | // Map from bufferId to tokenCount |
| 279 | std::map<int, uint32_t> tokenCount; |
| 280 | { |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 281 | std::lock_guard<std::mutex> lock(mTokenMutex); |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 282 | for (auto iter = mTokenMap.begin(); iter != mTokenMap.end(); ++iter) { |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 283 | sp<BufferClient> client = iter->second.second.promote(); |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 284 | if (client != nullptr) { |
| 285 | const std::shared_ptr<BufferNode> node = client->getBufferNode(); |
| 286 | auto mapIter = tokenCount.find(node->id()); |
| 287 | if (mapIter != tokenCount.end()) { |
| 288 | ++mapIter->second; |
| 289 | } else { |
| 290 | tokenCount.emplace(node->id(), 1U); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | stream << "Unused Tokens:\n"; |
| 297 | stream << std::right; |
| 298 | stream << std::setw(8) << "Buffer Id"; |
| 299 | stream << " "; |
Tianyu Jiang | 0f69e5e | 2019-01-23 17:17:59 -0800 | [diff] [blame] | 300 | stream << std::setw(7) << "#Tokens"; |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 301 | stream << std::endl; |
| 302 | |
| 303 | for (auto iter = tokenCount.begin(); iter != tokenCount.end(); ++iter) { |
| 304 | stream << std::right; |
| 305 | stream << std::setw(8) << /*Buffer Id=*/iter->first; |
| 306 | stream << " "; |
Tianyu Jiang | 0f69e5e | 2019-01-23 17:17:59 -0800 | [diff] [blame] | 307 | stream << std::setw(7) << /*#Tokens=*/iter->second; |
Fan Xu | fe097c7 | 2018-12-07 15:46:51 -0800 | [diff] [blame] | 308 | stream << std::endl; |
| 309 | } |
| 310 | |
| 311 | fprintf(out, "%s", stream.str().c_str()); |
| 312 | |
| 313 | fclose(out); |
| 314 | return Void(); |
| 315 | } |
| 316 | |
Fan Xu | d6cd6ba | 2018-11-15 16:46:55 -0800 | [diff] [blame] | 317 | hidl_handle BufferHubService::registerToken(const wp<BufferClient>& client) { |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 318 | // Find next available token id |
| 319 | std::lock_guard<std::mutex> lock(mTokenMutex); |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 320 | do { |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 321 | ++mLastTokenId; |
| 322 | } while (mTokenMap.find(mLastTokenId) != mTokenMap.end()); |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 323 | |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 324 | std::array<uint8_t, EVP_MAX_MD_SIZE> hmac; |
| 325 | uint32_t hmacSize = 0U; |
| 326 | |
| 327 | HMAC(/*evp_md=*/EVP_sha256(), /*key=*/&mKey, /*key_len=*/kKeyLen, |
| 328 | /*data=*/(uint8_t*)&mLastTokenId, /*data_len=*/mTokenIdSize, |
| 329 | /*out=*/hmac.data(), /*out_len=*/&hmacSize); |
| 330 | |
| 331 | int numIntsForHMAC = (int)ceil(hmacSize / (double)sizeof(int)); |
| 332 | native_handle_t* handle = native_handle_create(/*numFds=*/0, /*numInts=*/1 + numIntsForHMAC); |
| 333 | handle->data[0] = mLastTokenId; |
| 334 | // Set all the the bits of last int to 0 since it might not be fully overwritten |
| 335 | handle->data[numIntsForHMAC] = 0; |
| 336 | memcpy(&handle->data[1], hmac.data(), hmacSize); |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 337 | |
| 338 | // returnToken owns the native_handle_t* thus doing lifecycle management |
| 339 | hidl_handle returnToken; |
| 340 | returnToken.setTo(handle, /*shoudOwn=*/true); |
| 341 | |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 342 | std::vector<uint8_t> hmacVec; |
| 343 | hmacVec.resize(hmacSize); |
| 344 | memcpy(hmacVec.data(), hmac.data(), hmacSize); |
| 345 | mTokenMap.emplace(mLastTokenId, std::pair(hmacVec, client)); |
| 346 | |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 347 | return returnToken; |
| 348 | } |
| 349 | |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 350 | void BufferHubService::onClientClosed(const BufferClient* client) { |
| 351 | removeTokenByClient(client); |
| 352 | |
Fan Xu | cd74d78 | 2018-11-26 13:51:25 -0800 | [diff] [blame] | 353 | std::lock_guard<std::mutex> lock(mClientSetMutex); |
| 354 | auto iter = std::find(mClientSet.begin(), mClientSet.end(), client); |
| 355 | if (iter != mClientSet.end()) { |
| 356 | mClientSet.erase(iter); |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Fan Xu | efce32e | 2018-12-12 14:34:16 -0800 | [diff] [blame] | 360 | // Implementation of this function should be consistent with the definition of bufferInfo handle in |
| 361 | // ui/BufferHubDefs.h. |
Tianyu Jiang | 8adb319 | 2019-01-29 10:54:24 -0800 | [diff] [blame] | 362 | hidl_handle BufferHubService::buildBufferInfo(char* bufferInfoStorage, int bufferId, |
| 363 | uint32_t clientBitMask, uint32_t userMetadataSize, |
| 364 | int metadataFd, int eventFd) { |
| 365 | native_handle_t* infoHandle = |
| 366 | native_handle_init(bufferInfoStorage, BufferHubDefs::kBufferInfoNumFds, |
| 367 | BufferHubDefs::kBufferInfoNumInts); |
Fan Xu | efce32e | 2018-12-12 14:34:16 -0800 | [diff] [blame] | 368 | |
Tianyu Jiang | 8adb319 | 2019-01-29 10:54:24 -0800 | [diff] [blame] | 369 | infoHandle->data[0] = metadataFd; |
| 370 | infoHandle->data[1] = eventFd; |
Fan Xu | 5cf47bc | 2019-01-15 15:02:15 -0800 | [diff] [blame] | 371 | infoHandle->data[2] = bufferId; |
Fan Xu | efce32e | 2018-12-12 14:34:16 -0800 | [diff] [blame] | 372 | // Use memcpy to convert to int without missing digit. |
| 373 | // TOOD(b/121345852): use bit_cast to unpack bufferInfo when C++20 becomes available. |
Fan Xu | 5cf47bc | 2019-01-15 15:02:15 -0800 | [diff] [blame] | 374 | memcpy(&infoHandle->data[3], &clientBitMask, sizeof(clientBitMask)); |
| 375 | memcpy(&infoHandle->data[4], &userMetadataSize, sizeof(userMetadataSize)); |
Fan Xu | efce32e | 2018-12-12 14:34:16 -0800 | [diff] [blame] | 376 | |
| 377 | hidl_handle bufferInfo; |
Tianyu Jiang | 8adb319 | 2019-01-29 10:54:24 -0800 | [diff] [blame] | 378 | bufferInfo.setTo(infoHandle, /*shouldOwn=*/false); |
Fan Xu | efce32e | 2018-12-12 14:34:16 -0800 | [diff] [blame] | 379 | |
| 380 | return bufferInfo; |
| 381 | } |
| 382 | |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 383 | void BufferHubService::removeTokenByClient(const BufferClient* client) { |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 384 | std::lock_guard<std::mutex> lock(mTokenMutex); |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 385 | auto iter = mTokenMap.begin(); |
| 386 | while (iter != mTokenMap.end()) { |
Fan Xu | 55b26a6 | 2018-12-19 11:03:14 -0800 | [diff] [blame] | 387 | if (iter->second.second == client) { |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 388 | auto oldIter = iter; |
| 389 | ++iter; |
| 390 | mTokenMap.erase(oldIter); |
| 391 | } else { |
| 392 | ++iter; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 397 | } // namespace implementation |
| 398 | } // namespace V1_0 |
| 399 | } // namespace bufferhub |
| 400 | } // namespace frameworks |
| 401 | } // namespace android |