Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 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_NDEBUG 0 |
| 18 | #define LOG_TAG "Camera3-BufferManager" |
| 19 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 20 | |
| 21 | #include <gui/ISurfaceComposer.h> |
| 22 | #include <private/gui/ComposerService.h> |
| 23 | #include <utils/Log.h> |
| 24 | #include <utils/Trace.h> |
| 25 | #include "utils/CameraTraces.h" |
| 26 | #include "Camera3BufferManager.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | namespace camera3 { |
| 31 | |
| 32 | Camera3BufferManager::Camera3BufferManager(const sp<IGraphicBufferAlloc>& allocator) : |
| 33 | mAllocator(allocator) { |
| 34 | if (allocator == NULL) { |
| 35 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 36 | mAllocator = composer->createGraphicBufferAlloc(); |
| 37 | if (mAllocator == NULL) { |
| 38 | ALOGE("createGraphicBufferAlloc failed"); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | Camera3BufferManager::~Camera3BufferManager() { |
| 44 | } |
| 45 | |
| 46 | status_t Camera3BufferManager::registerStream(const StreamInfo& streamInfo) { |
| 47 | ATRACE_CALL(); |
| 48 | |
| 49 | int streamId = streamInfo.streamId; |
| 50 | int streamSetId = streamInfo.streamSetId; |
| 51 | |
| 52 | if (streamId == CAMERA3_STREAM_ID_INVALID || streamSetId == CAMERA3_STREAM_SET_ID_INVALID) { |
| 53 | ALOGE("%s: Stream id (%d) or stream set id (%d) is invalid", |
| 54 | __FUNCTION__, streamId, streamSetId); |
| 55 | return BAD_VALUE; |
| 56 | } |
| 57 | if (streamInfo.totalBufferCount > kMaxBufferCount || streamInfo.totalBufferCount == 0) { |
| 58 | ALOGE("%s: Stream id (%d) with stream set id (%d) total buffer count %zu is invalid", |
| 59 | __FUNCTION__, streamId, streamSetId, streamInfo.totalBufferCount); |
| 60 | return BAD_VALUE; |
| 61 | } |
| 62 | if (!streamInfo.isConfigured) { |
| 63 | ALOGE("%s: Stream (%d) is not configured", __FUNCTION__, streamId); |
| 64 | return BAD_VALUE; |
| 65 | } |
| 66 | |
| 67 | // For Gralloc v1, try to allocate a buffer and see if it is successful, otherwise, stream |
| 68 | // buffer sharing for this newly added stream is not supported. For Gralloc v0, we don't |
| 69 | // need check this, as the buffers are not really shared between streams, the buffers are |
| 70 | // allocated for each stream individually, the allocation failure will be checked in |
| 71 | // getBufferForStream() call. |
| 72 | if (mGrallocVersion > HARDWARE_DEVICE_API_VERSION(0,1)) { |
| 73 | // TODO: To be implemented. |
| 74 | |
| 75 | // In case allocation fails, return invalid operation |
| 76 | return INVALID_OPERATION; |
| 77 | } |
| 78 | |
| 79 | Mutex::Autolock l(mLock); |
| 80 | if (mAllocator == NULL) { |
| 81 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 82 | return INVALID_OPERATION; |
| 83 | } |
| 84 | |
| 85 | // Check if this stream was registered with different stream set ID, if so, error out. |
| 86 | for (size_t i = 0; i < mStreamSetMap.size(); i++) { |
| 87 | ssize_t streamIdx = mStreamSetMap[i].streamInfoMap.indexOfKey(streamId); |
| 88 | if (streamIdx != NAME_NOT_FOUND && |
| 89 | mStreamSetMap[i].streamInfoMap[streamIdx].streamSetId != streamInfo.streamSetId) { |
| 90 | ALOGE("%s: It is illegal to register the same stream id with different stream set", |
| 91 | __FUNCTION__); |
| 92 | return BAD_VALUE; |
| 93 | } |
| 94 | } |
| 95 | // Check if there is an existing stream set registered; if not, create one; otherwise, add this |
| 96 | // stream info to the existing stream set entry. |
| 97 | ssize_t setIdx = mStreamSetMap.indexOfKey(streamSetId); |
| 98 | if (setIdx == NAME_NOT_FOUND) { |
| 99 | ALOGV("%s: stream set %d is not registered to stream set map yet, create it.", |
| 100 | __FUNCTION__, streamSetId); |
| 101 | // Create stream info map, then add to mStreamsetMap. |
| 102 | StreamSet newStreamSet; |
| 103 | setIdx = mStreamSetMap.add(streamSetId, newStreamSet); |
| 104 | } |
| 105 | // Update stream set map and water mark. |
| 106 | StreamSet& currentStreamSet = mStreamSetMap.editValueAt(setIdx); |
| 107 | ssize_t streamIdx = currentStreamSet.streamInfoMap.indexOfKey(streamId); |
| 108 | if (streamIdx != NAME_NOT_FOUND) { |
| 109 | ALOGW("%s: stream %d was already registered with stream set %d", |
| 110 | __FUNCTION__, streamId, streamSetId); |
| 111 | return OK; |
| 112 | } |
| 113 | currentStreamSet.streamInfoMap.add(streamId, streamInfo); |
| 114 | currentStreamSet.handoutBufferCountMap.add(streamId, 0); |
| 115 | |
| 116 | // The watermark should be the max of buffer count of each stream inside a stream set. |
| 117 | if (streamInfo.totalBufferCount > currentStreamSet.allocatedBufferWaterMark) { |
| 118 | currentStreamSet.allocatedBufferWaterMark = streamInfo.totalBufferCount; |
| 119 | } |
| 120 | |
| 121 | return OK; |
| 122 | } |
| 123 | |
| 124 | status_t Camera3BufferManager::unregisterStream(int streamId, int streamSetId) { |
| 125 | ATRACE_CALL(); |
| 126 | Mutex::Autolock l(mLock); |
| 127 | ALOGV("%s: unregister stream %d with stream set %d", __FUNCTION__, |
| 128 | streamId, streamSetId); |
| 129 | if (mAllocator == NULL) { |
| 130 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 131 | return INVALID_OPERATION; |
| 132 | } |
| 133 | |
| 134 | if (!checkIfStreamRegisteredLocked(streamId, streamSetId)){ |
| 135 | ALOGE("%s: stream %d with set id %d wasn't properly registered to this buffer manager!", |
| 136 | __FUNCTION__, streamId, streamSetId); |
| 137 | return BAD_VALUE; |
| 138 | } |
| 139 | |
| 140 | // De-list all the buffers associated with this stream first. |
| 141 | StreamSet& currentSet = mStreamSetMap.editValueFor(streamSetId); |
| 142 | BufferList& freeBufs = currentSet.freeBuffers; |
| 143 | BufferCountMap& handOutBufferCounts = currentSet.handoutBufferCountMap; |
| 144 | InfoMap& infoMap = currentSet.streamInfoMap; |
| 145 | removeBuffersFromBufferListLocked(freeBufs, streamId); |
| 146 | handOutBufferCounts.removeItem(streamId); |
| 147 | |
| 148 | // Remove the stream info from info map and recalculate the buffer count water mark. |
| 149 | infoMap.removeItem(streamId); |
| 150 | currentSet.allocatedBufferWaterMark = 0; |
| 151 | for (size_t i = 0; i < infoMap.size(); i++) { |
| 152 | if (infoMap[i].totalBufferCount > currentSet.allocatedBufferWaterMark) { |
| 153 | currentSet.allocatedBufferWaterMark = infoMap[i].totalBufferCount; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Remove this stream set if all its streams have been removed. |
| 158 | if (freeBufs.size() == 0 && handOutBufferCounts.size() == 0 && infoMap.size() == 0) { |
| 159 | mStreamSetMap.removeItem(streamSetId); |
| 160 | } |
| 161 | |
| 162 | return OK; |
| 163 | } |
| 164 | |
| 165 | status_t Camera3BufferManager::getBufferForStream(int streamId, int streamSetId, |
| 166 | sp<GraphicBuffer>* gb, int* fenceFd) { |
| 167 | ATRACE_CALL(); |
| 168 | |
| 169 | Mutex::Autolock l(mLock); |
| 170 | ALOGV("%s: get buffer for stream %d with stream set %d", __FUNCTION__, |
| 171 | streamId, streamSetId); |
| 172 | if (mAllocator == NULL) { |
| 173 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 174 | return INVALID_OPERATION; |
| 175 | } |
| 176 | |
| 177 | if (!checkIfStreamRegisteredLocked(streamId, streamSetId)) { |
| 178 | ALOGE("%s: stream %d is not registered with stream set %d yet!!!", |
| 179 | __FUNCTION__, streamId, streamSetId); |
| 180 | return BAD_VALUE; |
| 181 | } |
| 182 | |
| 183 | StreamSet &streamSet = mStreamSetMap.editValueFor(streamSetId); |
| 184 | GraphicBufferEntry buffer = |
| 185 | getFirstBufferFromBufferListLocked(streamSet.freeBuffers, streamId); |
| 186 | |
| 187 | if (mGrallocVersion < HARDWARE_DEVICE_API_VERSION(1,0)) { |
| 188 | // Allocate one if there is no free buffer available. |
| 189 | if (buffer.graphicBuffer == nullptr) { |
| 190 | const StreamInfo& info = streamSet.streamInfoMap.valueFor(streamId); |
| 191 | status_t res = OK; |
| 192 | buffer.fenceFd = -1; |
| 193 | buffer.graphicBuffer = mAllocator->createGraphicBuffer( |
| 194 | info.width, info.height, info.format, info.combinedUsage, &res); |
| 195 | ALOGV("%s: allocate a new graphic buffer %p with handle %p", |
| 196 | __FUNCTION__, buffer.graphicBuffer.get(), buffer.graphicBuffer->handle); |
| 197 | if (res != OK) { |
| 198 | ALOGE("%s: graphic buffer allocation failed: (error %d %s) ", |
| 199 | __FUNCTION__, res, strerror(-res)); |
| 200 | return res; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Increase the hand-out buffer count for tracking purpose. |
| 205 | BufferCountMap& handOutBufferCounts = streamSet.handoutBufferCountMap; |
| 206 | size_t& bufferCount = handOutBufferCounts.editValueFor(streamId); |
| 207 | bufferCount++; |
| 208 | |
| 209 | *gb = buffer.graphicBuffer; |
| 210 | *fenceFd = buffer.fenceFd; |
| 211 | ALOGV("%s: get buffer (%p) with handle (%p).", |
| 212 | __FUNCTION__, buffer.graphicBuffer.get(), buffer.graphicBuffer->handle); |
| 213 | |
| 214 | // Proactively free buffers for other streams if the current number of allocated buffers |
| 215 | // exceeds the water mark. This only for Gralloc V1, for V2, this logic can also be handled |
| 216 | // in returnBufferForStream() if we want to free buffer more quickly. |
| 217 | // TODO: probably should find out all the inactive stream IDs, and free the firstly found |
| 218 | // buffers for them. |
| 219 | StreamId firstOtherStreamId = CAMERA3_STREAM_ID_INVALID; |
| 220 | if (streamSet.streamInfoMap.size() > 1) { |
| 221 | for (size_t i = 0; i < streamSet.streamInfoMap.size(); i++) { |
| 222 | firstOtherStreamId = streamSet.streamInfoMap[i].streamId; |
| 223 | if (firstOtherStreamId != streamId) { |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | if (firstOtherStreamId == CAMERA3_STREAM_ID_INVALID) { |
| 228 | return OK; |
| 229 | } |
| 230 | |
| 231 | // This will drop the reference to one free buffer, which will effectively free one |
| 232 | // buffer (from the free buffer list) for the inactive streams. |
| 233 | size_t totalAllocatedBufferCount = streamSet.freeBuffers.size(); |
| 234 | for (size_t i = 0; i < streamSet.handoutBufferCountMap.size(); i++) { |
| 235 | totalAllocatedBufferCount += streamSet.handoutBufferCountMap[i]; |
| 236 | } |
| 237 | if (totalAllocatedBufferCount > streamSet.allocatedBufferWaterMark) { |
| 238 | getFirstBufferFromBufferListLocked(streamSet.freeBuffers, firstOtherStreamId); |
| 239 | } |
| 240 | } |
| 241 | } else { |
| 242 | // TODO: implement this. |
| 243 | return BAD_VALUE; |
| 244 | } |
| 245 | |
| 246 | return OK; |
| 247 | } |
| 248 | |
| 249 | status_t Camera3BufferManager::returnBufferForStream(int streamId, |
| 250 | int streamSetId, const sp<GraphicBuffer>& buffer, int fenceFd) { |
| 251 | ATRACE_CALL(); |
| 252 | Mutex::Autolock l(mLock); |
| 253 | ALOGV_IF(buffer != 0, "%s: return buffer (%p) with handle (%p) for stream %d and stream set %d", |
| 254 | __FUNCTION__, buffer.get(), buffer->handle, streamId, streamSetId); |
| 255 | if (mAllocator == NULL) { |
| 256 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 257 | return INVALID_OPERATION; |
| 258 | } |
| 259 | |
| 260 | if (!checkIfStreamRegisteredLocked(streamId, streamSetId)){ |
| 261 | ALOGV("%s: returning buffer for an already unregistered stream (stream %d with set id %d)," |
| 262 | "buffer will be dropped right away!", __FUNCTION__, streamId, streamSetId); |
| 263 | return OK; |
| 264 | } |
| 265 | |
| 266 | if (mGrallocVersion < HARDWARE_DEVICE_API_VERSION(1,0)) { |
| 267 | // Add to the freeBuffer list. |
| 268 | StreamSet& streamSet = mStreamSetMap.editValueFor(streamSetId); |
| 269 | if (buffer != 0) { |
| 270 | BufferEntry entry; |
| 271 | entry.add(streamId, GraphicBufferEntry(buffer, fenceFd)); |
| 272 | status_t res = addBufferToBufferListLocked(streamSet.freeBuffers, entry); |
| 273 | if (res != OK) { |
| 274 | ALOGE("%s: add buffer to free buffer list failed", __FUNCTION__); |
| 275 | return res; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // Update the hand-out buffer count for this buffer. |
| 280 | BufferCountMap& handOutBufferCounts = streamSet.handoutBufferCountMap; |
| 281 | size_t& bufferCount = handOutBufferCounts.editValueFor(streamId); |
| 282 | bufferCount--; |
| 283 | } else { |
| 284 | // TODO: implement this. |
| 285 | return BAD_VALUE; |
| 286 | } |
| 287 | |
| 288 | return OK; |
| 289 | } |
| 290 | |
| 291 | void Camera3BufferManager::dump(int fd, const Vector<String16>& args) const { |
| 292 | Mutex::Autolock l(mLock); |
| 293 | |
| 294 | (void) args; |
| 295 | String8 lines; |
| 296 | lines.appendFormat(" Total stream sets: %zu\n", mStreamSetMap.size()); |
| 297 | for (size_t i = 0; i < mStreamSetMap.size(); i++) { |
| 298 | lines.appendFormat(" Stream set %d has below streams:\n", mStreamSetMap.keyAt(i)); |
| 299 | for (size_t j = 0; j < mStreamSetMap[i].streamInfoMap.size(); j++) { |
| 300 | lines.appendFormat(" Stream %d\n", mStreamSetMap[i].streamInfoMap[j].streamId); |
| 301 | } |
| 302 | lines.appendFormat(" Stream set buffer count water mark: %zu\n", |
| 303 | mStreamSetMap[i].allocatedBufferWaterMark); |
| 304 | lines.appendFormat(" Handout buffer counts:\n"); |
| 305 | for (size_t m = 0; m < mStreamSetMap[i].handoutBufferCountMap.size(); m++) { |
| 306 | int streamId = mStreamSetMap[i].handoutBufferCountMap.keyAt(m); |
| 307 | size_t bufferCount = mStreamSetMap[i].handoutBufferCountMap.valueAt(m); |
| 308 | lines.appendFormat(" stream id: %d, buffer count: %zu.\n", |
| 309 | streamId, bufferCount); |
| 310 | } |
| 311 | |
| 312 | lines.appendFormat(" Free buffer count: %zu\n", |
| 313 | mStreamSetMap[i].freeBuffers.size()); |
| 314 | for (auto& bufEntry : mStreamSetMap[i].freeBuffers) { |
| 315 | for (size_t m = 0; m < bufEntry.size(); m++) { |
| 316 | const sp<GraphicBuffer>& buffer = bufEntry.valueAt(m).graphicBuffer; |
| 317 | int streamId = bufEntry.keyAt(m); |
| 318 | lines.appendFormat(" stream id: %d, buffer: %p, handle: %p.\n", |
| 319 | streamId, buffer.get(), buffer->handle); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | write(fd, lines.string(), lines.size()); |
| 324 | } |
| 325 | |
| 326 | bool Camera3BufferManager::checkIfStreamRegisteredLocked(int streamId, int streamSetId) const { |
| 327 | ssize_t setIdx = mStreamSetMap.indexOfKey(streamSetId); |
| 328 | if (setIdx == NAME_NOT_FOUND) { |
| 329 | ALOGV("%s: stream set %d is not registered to stream set map yet!", |
| 330 | __FUNCTION__, streamSetId); |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | ssize_t streamIdx = mStreamSetMap.valueAt(setIdx).streamInfoMap.indexOfKey(streamId); |
| 335 | if (streamIdx == NAME_NOT_FOUND) { |
| 336 | ALOGV("%s: stream %d is not registered to stream info map yet!", __FUNCTION__, streamId); |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | size_t bufferWaterMark = mStreamSetMap[setIdx].allocatedBufferWaterMark; |
| 341 | if (bufferWaterMark == 0 || bufferWaterMark > kMaxBufferCount) { |
| 342 | ALOGW("%s: stream %d with stream set %d is not registered correctly to stream set map," |
| 343 | " as the water mark (%zu) is wrong!", |
| 344 | __FUNCTION__, streamId, streamSetId, bufferWaterMark); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | status_t Camera3BufferManager::addBufferToBufferListLocked(BufferList& bufList, |
| 352 | const BufferEntry& buffer) { |
| 353 | // TODO: need add some sanity check here. |
| 354 | bufList.push_back(buffer); |
| 355 | |
| 356 | return OK; |
| 357 | } |
| 358 | |
| 359 | status_t Camera3BufferManager::removeBuffersFromBufferListLocked(BufferList& bufferList, |
| 360 | int streamId) { |
| 361 | BufferList::iterator i = bufferList.begin(); |
| 362 | while (i != bufferList.end()) { |
| 363 | ssize_t idx = i->indexOfKey(streamId); |
| 364 | if (idx != NAME_NOT_FOUND) { |
| 365 | i->removeItem(streamId); |
| 366 | if (i->isEmpty()) { |
| 367 | i = bufferList.erase(i); |
| 368 | } |
| 369 | break; |
| 370 | } else { |
| 371 | i++; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | ALOGW_IF(i == bufferList.end(), "%s: Unable to find buffers for stream %d", |
| 376 | __FUNCTION__, streamId); |
| 377 | |
| 378 | return OK; |
| 379 | } |
| 380 | |
| 381 | Camera3BufferManager::GraphicBufferEntry Camera3BufferManager::getFirstBufferFromBufferListLocked( |
| 382 | BufferList& buffers, int streamId) { |
| 383 | // Try to get the first buffer from the free buffer list if there is one. |
| 384 | GraphicBufferEntry entry; |
| 385 | BufferList::iterator i = buffers.begin(); |
| 386 | while (i != buffers.end()) { |
| 387 | ssize_t idx = i->indexOfKey(streamId); |
| 388 | if (idx != NAME_NOT_FOUND) { |
| 389 | entry = GraphicBufferEntry(i->valueAt(idx)); |
| 390 | i = buffers.erase(i); |
| 391 | break; |
| 392 | } else { |
| 393 | i++; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | ALOGV_IF(entry.graphicBuffer == 0, "%s: Unable to find free buffer for stream %d", |
| 398 | __FUNCTION__, streamId); |
| 399 | return entry; |
| 400 | } |
| 401 | |
| 402 | } // namespace camera3 |
| 403 | } // namespace android |