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 | |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 116 | // The max allowed buffer count should be the max of buffer count of each stream inside a stream |
| 117 | // set. |
| 118 | if (streamInfo.totalBufferCount > currentStreamSet.maxAllowedBufferCount) { |
| 119 | currentStreamSet.maxAllowedBufferCount = streamInfo.totalBufferCount; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return OK; |
| 123 | } |
| 124 | |
| 125 | status_t Camera3BufferManager::unregisterStream(int streamId, int streamSetId) { |
| 126 | ATRACE_CALL(); |
| 127 | Mutex::Autolock l(mLock); |
| 128 | ALOGV("%s: unregister stream %d with stream set %d", __FUNCTION__, |
| 129 | streamId, streamSetId); |
| 130 | if (mAllocator == NULL) { |
| 131 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 132 | return INVALID_OPERATION; |
| 133 | } |
| 134 | |
| 135 | if (!checkIfStreamRegisteredLocked(streamId, streamSetId)){ |
| 136 | ALOGE("%s: stream %d with set id %d wasn't properly registered to this buffer manager!", |
| 137 | __FUNCTION__, streamId, streamSetId); |
| 138 | return BAD_VALUE; |
| 139 | } |
| 140 | |
| 141 | // De-list all the buffers associated with this stream first. |
| 142 | StreamSet& currentSet = mStreamSetMap.editValueFor(streamSetId); |
| 143 | BufferList& freeBufs = currentSet.freeBuffers; |
| 144 | BufferCountMap& handOutBufferCounts = currentSet.handoutBufferCountMap; |
| 145 | InfoMap& infoMap = currentSet.streamInfoMap; |
| 146 | removeBuffersFromBufferListLocked(freeBufs, streamId); |
| 147 | handOutBufferCounts.removeItem(streamId); |
| 148 | |
| 149 | // Remove the stream info from info map and recalculate the buffer count water mark. |
| 150 | infoMap.removeItem(streamId); |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 151 | currentSet.maxAllowedBufferCount = 0; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 152 | for (size_t i = 0; i < infoMap.size(); i++) { |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 153 | if (infoMap[i].totalBufferCount > currentSet.maxAllowedBufferCount) { |
| 154 | currentSet.maxAllowedBufferCount = infoMap[i].totalBufferCount; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 157 | // Lazy solution: when a stream is unregistered, the streams will be reconfigured, reset |
| 158 | // the water mark and let it grow again. |
| 159 | currentSet.allocatedBufferWaterMark = 0; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 160 | |
| 161 | // Remove this stream set if all its streams have been removed. |
| 162 | if (freeBufs.size() == 0 && handOutBufferCounts.size() == 0 && infoMap.size() == 0) { |
| 163 | mStreamSetMap.removeItem(streamSetId); |
| 164 | } |
| 165 | |
| 166 | return OK; |
| 167 | } |
| 168 | |
| 169 | status_t Camera3BufferManager::getBufferForStream(int streamId, int streamSetId, |
| 170 | sp<GraphicBuffer>* gb, int* fenceFd) { |
| 171 | ATRACE_CALL(); |
| 172 | |
| 173 | Mutex::Autolock l(mLock); |
| 174 | ALOGV("%s: get buffer for stream %d with stream set %d", __FUNCTION__, |
| 175 | streamId, streamSetId); |
| 176 | if (mAllocator == NULL) { |
| 177 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 178 | return INVALID_OPERATION; |
| 179 | } |
| 180 | |
| 181 | if (!checkIfStreamRegisteredLocked(streamId, streamSetId)) { |
| 182 | ALOGE("%s: stream %d is not registered with stream set %d yet!!!", |
| 183 | __FUNCTION__, streamId, streamSetId); |
| 184 | return BAD_VALUE; |
| 185 | } |
| 186 | |
| 187 | StreamSet &streamSet = mStreamSetMap.editValueFor(streamSetId); |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 188 | BufferCountMap& handOutBufferCounts = streamSet.handoutBufferCountMap; |
| 189 | size_t& bufferCount = handOutBufferCounts.editValueFor(streamId); |
| 190 | if (bufferCount >= streamSet.maxAllowedBufferCount) { |
| 191 | ALOGE("%s: bufferCount (%zu) exceeds the max allowed buffer count (%zu) of this stream set", |
| 192 | __FUNCTION__, bufferCount, streamSet.maxAllowedBufferCount); |
| 193 | return INVALID_OPERATION; |
| 194 | } |
| 195 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 196 | GraphicBufferEntry buffer = |
| 197 | getFirstBufferFromBufferListLocked(streamSet.freeBuffers, streamId); |
| 198 | |
| 199 | if (mGrallocVersion < HARDWARE_DEVICE_API_VERSION(1,0)) { |
| 200 | // Allocate one if there is no free buffer available. |
| 201 | if (buffer.graphicBuffer == nullptr) { |
| 202 | const StreamInfo& info = streamSet.streamInfoMap.valueFor(streamId); |
| 203 | status_t res = OK; |
| 204 | buffer.fenceFd = -1; |
| 205 | buffer.graphicBuffer = mAllocator->createGraphicBuffer( |
| 206 | info.width, info.height, info.format, info.combinedUsage, &res); |
Zhijun He | 80fa619 | 2016-02-08 12:02:25 -0800 | [diff] [blame] | 207 | ALOGV("%s: allocating a new graphic buffer (%dx%d, format 0x%x) %p with handle %p", |
| 208 | __FUNCTION__, info.width, info.height, info.format, |
| 209 | buffer.graphicBuffer.get(), buffer.graphicBuffer->handle); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 210 | if (res != OK) { |
| 211 | ALOGE("%s: graphic buffer allocation failed: (error %d %s) ", |
| 212 | __FUNCTION__, res, strerror(-res)); |
| 213 | return res; |
| 214 | } |
Zhijun He | 80fa619 | 2016-02-08 12:02:25 -0800 | [diff] [blame] | 215 | ALOGV("%s: allocation done", __FUNCTION__); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Increase the hand-out buffer count for tracking purpose. |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 219 | bufferCount++; |
Zhijun He | 80fa619 | 2016-02-08 12:02:25 -0800 | [diff] [blame] | 220 | // Update the water mark to be the max hand-out buffer count + 1. An additional buffer is |
| 221 | // added to reduce the chance of buffer allocation during stream steady state, especially |
| 222 | // for cases where one stream is active, the other stream may request some buffers randomly. |
| 223 | if (bufferCount + 1 > streamSet.allocatedBufferWaterMark) { |
| 224 | streamSet.allocatedBufferWaterMark = bufferCount + 1; |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 225 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 226 | *gb = buffer.graphicBuffer; |
| 227 | *fenceFd = buffer.fenceFd; |
| 228 | ALOGV("%s: get buffer (%p) with handle (%p).", |
| 229 | __FUNCTION__, buffer.graphicBuffer.get(), buffer.graphicBuffer->handle); |
| 230 | |
| 231 | // Proactively free buffers for other streams if the current number of allocated buffers |
| 232 | // exceeds the water mark. This only for Gralloc V1, for V2, this logic can also be handled |
| 233 | // in returnBufferForStream() if we want to free buffer more quickly. |
| 234 | // TODO: probably should find out all the inactive stream IDs, and free the firstly found |
| 235 | // buffers for them. |
| 236 | StreamId firstOtherStreamId = CAMERA3_STREAM_ID_INVALID; |
| 237 | if (streamSet.streamInfoMap.size() > 1) { |
| 238 | for (size_t i = 0; i < streamSet.streamInfoMap.size(); i++) { |
| 239 | firstOtherStreamId = streamSet.streamInfoMap[i].streamId; |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 240 | if (firstOtherStreamId != streamId && |
| 241 | hasBufferForStreamLocked(streamSet.freeBuffers, firstOtherStreamId)) { |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 242 | break; |
| 243 | } |
| 244 | } |
| 245 | if (firstOtherStreamId == CAMERA3_STREAM_ID_INVALID) { |
| 246 | return OK; |
| 247 | } |
| 248 | |
| 249 | // This will drop the reference to one free buffer, which will effectively free one |
| 250 | // buffer (from the free buffer list) for the inactive streams. |
| 251 | size_t totalAllocatedBufferCount = streamSet.freeBuffers.size(); |
| 252 | for (size_t i = 0; i < streamSet.handoutBufferCountMap.size(); i++) { |
| 253 | totalAllocatedBufferCount += streamSet.handoutBufferCountMap[i]; |
| 254 | } |
| 255 | if (totalAllocatedBufferCount > streamSet.allocatedBufferWaterMark) { |
Zhijun He | 80fa619 | 2016-02-08 12:02:25 -0800 | [diff] [blame] | 256 | ALOGV("%s: free a buffer from stream %d", __FUNCTION__, firstOtherStreamId); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 257 | getFirstBufferFromBufferListLocked(streamSet.freeBuffers, firstOtherStreamId); |
| 258 | } |
| 259 | } |
| 260 | } else { |
| 261 | // TODO: implement this. |
| 262 | return BAD_VALUE; |
| 263 | } |
| 264 | |
| 265 | return OK; |
| 266 | } |
| 267 | |
| 268 | status_t Camera3BufferManager::returnBufferForStream(int streamId, |
| 269 | int streamSetId, const sp<GraphicBuffer>& buffer, int fenceFd) { |
| 270 | ATRACE_CALL(); |
| 271 | Mutex::Autolock l(mLock); |
| 272 | ALOGV_IF(buffer != 0, "%s: return buffer (%p) with handle (%p) for stream %d and stream set %d", |
| 273 | __FUNCTION__, buffer.get(), buffer->handle, streamId, streamSetId); |
| 274 | if (mAllocator == NULL) { |
| 275 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 276 | return INVALID_OPERATION; |
| 277 | } |
| 278 | |
| 279 | if (!checkIfStreamRegisteredLocked(streamId, streamSetId)){ |
| 280 | ALOGV("%s: returning buffer for an already unregistered stream (stream %d with set id %d)," |
| 281 | "buffer will be dropped right away!", __FUNCTION__, streamId, streamSetId); |
| 282 | return OK; |
| 283 | } |
| 284 | |
| 285 | if (mGrallocVersion < HARDWARE_DEVICE_API_VERSION(1,0)) { |
| 286 | // Add to the freeBuffer list. |
| 287 | StreamSet& streamSet = mStreamSetMap.editValueFor(streamSetId); |
| 288 | if (buffer != 0) { |
| 289 | BufferEntry entry; |
| 290 | entry.add(streamId, GraphicBufferEntry(buffer, fenceFd)); |
| 291 | status_t res = addBufferToBufferListLocked(streamSet.freeBuffers, entry); |
| 292 | if (res != OK) { |
| 293 | ALOGE("%s: add buffer to free buffer list failed", __FUNCTION__); |
| 294 | return res; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Update the hand-out buffer count for this buffer. |
| 299 | BufferCountMap& handOutBufferCounts = streamSet.handoutBufferCountMap; |
| 300 | size_t& bufferCount = handOutBufferCounts.editValueFor(streamId); |
| 301 | bufferCount--; |
| 302 | } else { |
| 303 | // TODO: implement this. |
| 304 | return BAD_VALUE; |
| 305 | } |
| 306 | |
| 307 | return OK; |
| 308 | } |
| 309 | |
| 310 | void Camera3BufferManager::dump(int fd, const Vector<String16>& args) const { |
| 311 | Mutex::Autolock l(mLock); |
| 312 | |
| 313 | (void) args; |
| 314 | String8 lines; |
| 315 | lines.appendFormat(" Total stream sets: %zu\n", mStreamSetMap.size()); |
| 316 | for (size_t i = 0; i < mStreamSetMap.size(); i++) { |
| 317 | lines.appendFormat(" Stream set %d has below streams:\n", mStreamSetMap.keyAt(i)); |
| 318 | for (size_t j = 0; j < mStreamSetMap[i].streamInfoMap.size(); j++) { |
| 319 | lines.appendFormat(" Stream %d\n", mStreamSetMap[i].streamInfoMap[j].streamId); |
| 320 | } |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 321 | lines.appendFormat(" Stream set max allowed buffer count: %zu\n", |
| 322 | mStreamSetMap[i].maxAllowedBufferCount); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 323 | lines.appendFormat(" Stream set buffer count water mark: %zu\n", |
| 324 | mStreamSetMap[i].allocatedBufferWaterMark); |
| 325 | lines.appendFormat(" Handout buffer counts:\n"); |
| 326 | for (size_t m = 0; m < mStreamSetMap[i].handoutBufferCountMap.size(); m++) { |
| 327 | int streamId = mStreamSetMap[i].handoutBufferCountMap.keyAt(m); |
| 328 | size_t bufferCount = mStreamSetMap[i].handoutBufferCountMap.valueAt(m); |
| 329 | lines.appendFormat(" stream id: %d, buffer count: %zu.\n", |
| 330 | streamId, bufferCount); |
| 331 | } |
| 332 | |
| 333 | lines.appendFormat(" Free buffer count: %zu\n", |
| 334 | mStreamSetMap[i].freeBuffers.size()); |
| 335 | for (auto& bufEntry : mStreamSetMap[i].freeBuffers) { |
| 336 | for (size_t m = 0; m < bufEntry.size(); m++) { |
| 337 | const sp<GraphicBuffer>& buffer = bufEntry.valueAt(m).graphicBuffer; |
| 338 | int streamId = bufEntry.keyAt(m); |
| 339 | lines.appendFormat(" stream id: %d, buffer: %p, handle: %p.\n", |
| 340 | streamId, buffer.get(), buffer->handle); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | write(fd, lines.string(), lines.size()); |
| 345 | } |
| 346 | |
| 347 | bool Camera3BufferManager::checkIfStreamRegisteredLocked(int streamId, int streamSetId) const { |
| 348 | ssize_t setIdx = mStreamSetMap.indexOfKey(streamSetId); |
| 349 | if (setIdx == NAME_NOT_FOUND) { |
| 350 | ALOGV("%s: stream set %d is not registered to stream set map yet!", |
| 351 | __FUNCTION__, streamSetId); |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | ssize_t streamIdx = mStreamSetMap.valueAt(setIdx).streamInfoMap.indexOfKey(streamId); |
| 356 | if (streamIdx == NAME_NOT_FOUND) { |
| 357 | ALOGV("%s: stream %d is not registered to stream info map yet!", __FUNCTION__, streamId); |
| 358 | return false; |
| 359 | } |
| 360 | |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 361 | size_t bufferWaterMark = mStreamSetMap[setIdx].maxAllowedBufferCount; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 362 | if (bufferWaterMark == 0 || bufferWaterMark > kMaxBufferCount) { |
| 363 | ALOGW("%s: stream %d with stream set %d is not registered correctly to stream set map," |
| 364 | " as the water mark (%zu) is wrong!", |
| 365 | __FUNCTION__, streamId, streamSetId, bufferWaterMark); |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | status_t Camera3BufferManager::addBufferToBufferListLocked(BufferList& bufList, |
| 373 | const BufferEntry& buffer) { |
| 374 | // TODO: need add some sanity check here. |
| 375 | bufList.push_back(buffer); |
| 376 | |
| 377 | return OK; |
| 378 | } |
| 379 | |
| 380 | status_t Camera3BufferManager::removeBuffersFromBufferListLocked(BufferList& bufferList, |
| 381 | int streamId) { |
| 382 | BufferList::iterator i = bufferList.begin(); |
| 383 | while (i != bufferList.end()) { |
| 384 | ssize_t idx = i->indexOfKey(streamId); |
| 385 | if (idx != NAME_NOT_FOUND) { |
Zhijun He | 1ff811b | 2016-01-26 14:39:51 -0800 | [diff] [blame] | 386 | ALOGV("%s: Remove a buffer for stream %d, free buffer total count: %zu", |
| 387 | __FUNCTION__, streamId, bufferList.size()); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 388 | i->removeItem(streamId); |
| 389 | if (i->isEmpty()) { |
| 390 | i = bufferList.erase(i); |
| 391 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 392 | } else { |
| 393 | i++; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | ALOGW_IF(i == bufferList.end(), "%s: Unable to find buffers for stream %d", |
| 398 | __FUNCTION__, streamId); |
| 399 | |
| 400 | return OK; |
| 401 | } |
| 402 | |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 403 | bool Camera3BufferManager::hasBufferForStreamLocked(BufferList& buffers, int streamId) { |
| 404 | BufferList::iterator i = buffers.begin(); |
| 405 | while (i != buffers.end()) { |
| 406 | ssize_t idx = i->indexOfKey(streamId); |
| 407 | if (idx != NAME_NOT_FOUND) { |
| 408 | return true; |
| 409 | } |
| 410 | i++; |
| 411 | } |
| 412 | |
| 413 | return false; |
| 414 | } |
| 415 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 416 | Camera3BufferManager::GraphicBufferEntry Camera3BufferManager::getFirstBufferFromBufferListLocked( |
| 417 | BufferList& buffers, int streamId) { |
| 418 | // Try to get the first buffer from the free buffer list if there is one. |
| 419 | GraphicBufferEntry entry; |
| 420 | BufferList::iterator i = buffers.begin(); |
| 421 | while (i != buffers.end()) { |
| 422 | ssize_t idx = i->indexOfKey(streamId); |
| 423 | if (idx != NAME_NOT_FOUND) { |
| 424 | entry = GraphicBufferEntry(i->valueAt(idx)); |
| 425 | i = buffers.erase(i); |
| 426 | break; |
| 427 | } else { |
| 428 | i++; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | ALOGV_IF(entry.graphicBuffer == 0, "%s: Unable to find free buffer for stream %d", |
| 433 | __FUNCTION__, streamId); |
| 434 | return entry; |
| 435 | } |
| 436 | |
| 437 | } // namespace camera3 |
| 438 | } // namespace android |