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> |
Mathias Agopian | 01d56fe | 2017-02-07 17:12:36 -0800 | [diff] [blame^] | 22 | #include <gui/IGraphicBufferAlloc.h> |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 23 | #include <private/gui/ComposerService.h> |
| 24 | #include <utils/Log.h> |
| 25 | #include <utils/Trace.h> |
| 26 | #include "utils/CameraTraces.h" |
| 27 | #include "Camera3BufferManager.h" |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | namespace camera3 { |
| 32 | |
| 33 | Camera3BufferManager::Camera3BufferManager(const sp<IGraphicBufferAlloc>& allocator) : |
| 34 | mAllocator(allocator) { |
| 35 | if (allocator == NULL) { |
| 36 | sp<ISurfaceComposer> composer(ComposerService::getComposerService()); |
| 37 | mAllocator = composer->createGraphicBufferAlloc(); |
| 38 | if (mAllocator == NULL) { |
| 39 | ALOGE("createGraphicBufferAlloc failed"); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | Camera3BufferManager::~Camera3BufferManager() { |
| 45 | } |
| 46 | |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 47 | status_t Camera3BufferManager::registerStream(wp<Camera3OutputStream>& stream, |
| 48 | const StreamInfo& streamInfo) { |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 49 | ATRACE_CALL(); |
| 50 | |
| 51 | int streamId = streamInfo.streamId; |
| 52 | int streamSetId = streamInfo.streamSetId; |
| 53 | |
| 54 | if (streamId == CAMERA3_STREAM_ID_INVALID || streamSetId == CAMERA3_STREAM_SET_ID_INVALID) { |
| 55 | ALOGE("%s: Stream id (%d) or stream set id (%d) is invalid", |
| 56 | __FUNCTION__, streamId, streamSetId); |
| 57 | return BAD_VALUE; |
| 58 | } |
| 59 | if (streamInfo.totalBufferCount > kMaxBufferCount || streamInfo.totalBufferCount == 0) { |
| 60 | ALOGE("%s: Stream id (%d) with stream set id (%d) total buffer count %zu is invalid", |
| 61 | __FUNCTION__, streamId, streamSetId, streamInfo.totalBufferCount); |
| 62 | return BAD_VALUE; |
| 63 | } |
| 64 | if (!streamInfo.isConfigured) { |
| 65 | ALOGE("%s: Stream (%d) is not configured", __FUNCTION__, streamId); |
| 66 | return BAD_VALUE; |
| 67 | } |
| 68 | |
| 69 | // For Gralloc v1, try to allocate a buffer and see if it is successful, otherwise, stream |
| 70 | // buffer sharing for this newly added stream is not supported. For Gralloc v0, we don't |
| 71 | // need check this, as the buffers are not really shared between streams, the buffers are |
| 72 | // allocated for each stream individually, the allocation failure will be checked in |
| 73 | // getBufferForStream() call. |
| 74 | if (mGrallocVersion > HARDWARE_DEVICE_API_VERSION(0,1)) { |
| 75 | // TODO: To be implemented. |
| 76 | |
| 77 | // In case allocation fails, return invalid operation |
| 78 | return INVALID_OPERATION; |
| 79 | } |
| 80 | |
| 81 | Mutex::Autolock l(mLock); |
| 82 | if (mAllocator == NULL) { |
| 83 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 84 | return INVALID_OPERATION; |
| 85 | } |
| 86 | |
| 87 | // Check if this stream was registered with different stream set ID, if so, error out. |
| 88 | for (size_t i = 0; i < mStreamSetMap.size(); i++) { |
| 89 | ssize_t streamIdx = mStreamSetMap[i].streamInfoMap.indexOfKey(streamId); |
| 90 | if (streamIdx != NAME_NOT_FOUND && |
| 91 | mStreamSetMap[i].streamInfoMap[streamIdx].streamSetId != streamInfo.streamSetId) { |
| 92 | ALOGE("%s: It is illegal to register the same stream id with different stream set", |
| 93 | __FUNCTION__); |
| 94 | return BAD_VALUE; |
| 95 | } |
| 96 | } |
| 97 | // Check if there is an existing stream set registered; if not, create one; otherwise, add this |
| 98 | // stream info to the existing stream set entry. |
| 99 | ssize_t setIdx = mStreamSetMap.indexOfKey(streamSetId); |
| 100 | if (setIdx == NAME_NOT_FOUND) { |
| 101 | ALOGV("%s: stream set %d is not registered to stream set map yet, create it.", |
| 102 | __FUNCTION__, streamSetId); |
| 103 | // Create stream info map, then add to mStreamsetMap. |
| 104 | StreamSet newStreamSet; |
| 105 | setIdx = mStreamSetMap.add(streamSetId, newStreamSet); |
| 106 | } |
| 107 | // Update stream set map and water mark. |
| 108 | StreamSet& currentStreamSet = mStreamSetMap.editValueAt(setIdx); |
| 109 | ssize_t streamIdx = currentStreamSet.streamInfoMap.indexOfKey(streamId); |
| 110 | if (streamIdx != NAME_NOT_FOUND) { |
| 111 | ALOGW("%s: stream %d was already registered with stream set %d", |
| 112 | __FUNCTION__, streamId, streamSetId); |
| 113 | return OK; |
| 114 | } |
| 115 | currentStreamSet.streamInfoMap.add(streamId, streamInfo); |
| 116 | currentStreamSet.handoutBufferCountMap.add(streamId, 0); |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 117 | currentStreamSet.attachedBufferCountMap.add(streamId, 0); |
| 118 | mStreamMap.add(streamId, stream); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 119 | |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 120 | // The max allowed buffer count should be the max of buffer count of each stream inside a stream |
| 121 | // set. |
| 122 | if (streamInfo.totalBufferCount > currentStreamSet.maxAllowedBufferCount) { |
| 123 | currentStreamSet.maxAllowedBufferCount = streamInfo.totalBufferCount; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return OK; |
| 127 | } |
| 128 | |
| 129 | status_t Camera3BufferManager::unregisterStream(int streamId, int streamSetId) { |
| 130 | ATRACE_CALL(); |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 131 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 132 | Mutex::Autolock l(mLock); |
| 133 | ALOGV("%s: unregister stream %d with stream set %d", __FUNCTION__, |
| 134 | streamId, streamSetId); |
| 135 | if (mAllocator == NULL) { |
| 136 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 137 | return INVALID_OPERATION; |
| 138 | } |
| 139 | |
| 140 | if (!checkIfStreamRegisteredLocked(streamId, streamSetId)){ |
| 141 | ALOGE("%s: stream %d with set id %d wasn't properly registered to this buffer manager!", |
| 142 | __FUNCTION__, streamId, streamSetId); |
| 143 | return BAD_VALUE; |
| 144 | } |
| 145 | |
| 146 | // De-list all the buffers associated with this stream first. |
| 147 | StreamSet& currentSet = mStreamSetMap.editValueFor(streamSetId); |
| 148 | BufferList& freeBufs = currentSet.freeBuffers; |
| 149 | BufferCountMap& handOutBufferCounts = currentSet.handoutBufferCountMap; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 150 | BufferCountMap& attachedBufferCounts = currentSet.attachedBufferCountMap; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 151 | InfoMap& infoMap = currentSet.streamInfoMap; |
| 152 | removeBuffersFromBufferListLocked(freeBufs, streamId); |
| 153 | handOutBufferCounts.removeItem(streamId); |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 154 | attachedBufferCounts.removeItem(streamId); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 155 | |
| 156 | // Remove the stream info from info map and recalculate the buffer count water mark. |
| 157 | infoMap.removeItem(streamId); |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 158 | currentSet.maxAllowedBufferCount = 0; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 159 | for (size_t i = 0; i < infoMap.size(); i++) { |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 160 | if (infoMap[i].totalBufferCount > currentSet.maxAllowedBufferCount) { |
| 161 | currentSet.maxAllowedBufferCount = infoMap[i].totalBufferCount; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 164 | mStreamMap.removeItem(streamId); |
| 165 | |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 166 | // Lazy solution: when a stream is unregistered, the streams will be reconfigured, reset |
| 167 | // the water mark and let it grow again. |
| 168 | currentSet.allocatedBufferWaterMark = 0; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 169 | |
| 170 | // Remove this stream set if all its streams have been removed. |
| 171 | if (freeBufs.size() == 0 && handOutBufferCounts.size() == 0 && infoMap.size() == 0) { |
| 172 | mStreamSetMap.removeItem(streamSetId); |
| 173 | } |
| 174 | |
| 175 | return OK; |
| 176 | } |
| 177 | |
| 178 | status_t Camera3BufferManager::getBufferForStream(int streamId, int streamSetId, |
| 179 | sp<GraphicBuffer>* gb, int* fenceFd) { |
| 180 | ATRACE_CALL(); |
| 181 | |
| 182 | Mutex::Autolock l(mLock); |
| 183 | ALOGV("%s: get buffer for stream %d with stream set %d", __FUNCTION__, |
| 184 | streamId, streamSetId); |
| 185 | if (mAllocator == NULL) { |
| 186 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 187 | return INVALID_OPERATION; |
| 188 | } |
| 189 | |
| 190 | if (!checkIfStreamRegisteredLocked(streamId, streamSetId)) { |
| 191 | ALOGE("%s: stream %d is not registered with stream set %d yet!!!", |
| 192 | __FUNCTION__, streamId, streamSetId); |
| 193 | return BAD_VALUE; |
| 194 | } |
| 195 | |
| 196 | StreamSet &streamSet = mStreamSetMap.editValueFor(streamSetId); |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 197 | BufferCountMap& handOutBufferCounts = streamSet.handoutBufferCountMap; |
| 198 | size_t& bufferCount = handOutBufferCounts.editValueFor(streamId); |
| 199 | if (bufferCount >= streamSet.maxAllowedBufferCount) { |
| 200 | ALOGE("%s: bufferCount (%zu) exceeds the max allowed buffer count (%zu) of this stream set", |
| 201 | __FUNCTION__, bufferCount, streamSet.maxAllowedBufferCount); |
| 202 | return INVALID_OPERATION; |
| 203 | } |
| 204 | |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 205 | BufferCountMap& attachedBufferCounts = streamSet.attachedBufferCountMap; |
| 206 | size_t& attachedBufferCount = attachedBufferCounts.editValueFor(streamId); |
| 207 | if (attachedBufferCount > bufferCount) { |
| 208 | // We've already attached more buffers to this stream than we currently have |
| 209 | // outstanding, so have the stream just use an already-attached buffer |
| 210 | bufferCount++; |
| 211 | return ALREADY_EXISTS; |
| 212 | } |
| 213 | ALOGV("Stream %d set %d: Get buffer for stream: Allocate new", streamId, streamSetId); |
| 214 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 215 | GraphicBufferEntry buffer = |
| 216 | getFirstBufferFromBufferListLocked(streamSet.freeBuffers, streamId); |
| 217 | |
| 218 | if (mGrallocVersion < HARDWARE_DEVICE_API_VERSION(1,0)) { |
| 219 | // Allocate one if there is no free buffer available. |
| 220 | if (buffer.graphicBuffer == nullptr) { |
| 221 | const StreamInfo& info = streamSet.streamInfoMap.valueFor(streamId); |
| 222 | status_t res = OK; |
| 223 | buffer.fenceFd = -1; |
| 224 | buffer.graphicBuffer = mAllocator->createGraphicBuffer( |
Craig Donner | 4b2f818 | 2016-10-26 11:46:42 -0700 | [diff] [blame] | 225 | info.width, info.height, info.format, 1 /* layerCount */, |
| 226 | info.combinedUsage, &res); |
Zhijun He | 80fa619 | 2016-02-08 12:02:25 -0800 | [diff] [blame] | 227 | ALOGV("%s: allocating a new graphic buffer (%dx%d, format 0x%x) %p with handle %p", |
| 228 | __FUNCTION__, info.width, info.height, info.format, |
| 229 | buffer.graphicBuffer.get(), buffer.graphicBuffer->handle); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 230 | if (res != OK) { |
| 231 | ALOGE("%s: graphic buffer allocation failed: (error %d %s) ", |
| 232 | __FUNCTION__, res, strerror(-res)); |
| 233 | return res; |
| 234 | } |
Zhijun He | 80fa619 | 2016-02-08 12:02:25 -0800 | [diff] [blame] | 235 | ALOGV("%s: allocation done", __FUNCTION__); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 238 | // Increase the hand-out and attached buffer counts for tracking purposes. |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 239 | bufferCount++; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 240 | attachedBufferCount++; |
Zhijun He | 80fa619 | 2016-02-08 12:02:25 -0800 | [diff] [blame] | 241 | // Update the water mark to be the max hand-out buffer count + 1. An additional buffer is |
| 242 | // added to reduce the chance of buffer allocation during stream steady state, especially |
| 243 | // for cases where one stream is active, the other stream may request some buffers randomly. |
| 244 | if (bufferCount + 1 > streamSet.allocatedBufferWaterMark) { |
| 245 | streamSet.allocatedBufferWaterMark = bufferCount + 1; |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 246 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 247 | *gb = buffer.graphicBuffer; |
| 248 | *fenceFd = buffer.fenceFd; |
| 249 | ALOGV("%s: get buffer (%p) with handle (%p).", |
| 250 | __FUNCTION__, buffer.graphicBuffer.get(), buffer.graphicBuffer->handle); |
| 251 | |
| 252 | // Proactively free buffers for other streams if the current number of allocated buffers |
| 253 | // exceeds the water mark. This only for Gralloc V1, for V2, this logic can also be handled |
| 254 | // in returnBufferForStream() if we want to free buffer more quickly. |
| 255 | // TODO: probably should find out all the inactive stream IDs, and free the firstly found |
| 256 | // buffers for them. |
| 257 | StreamId firstOtherStreamId = CAMERA3_STREAM_ID_INVALID; |
| 258 | if (streamSet.streamInfoMap.size() > 1) { |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 259 | bool freeBufferIsAttached = false; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 260 | for (size_t i = 0; i < streamSet.streamInfoMap.size(); i++) { |
| 261 | firstOtherStreamId = streamSet.streamInfoMap[i].streamId; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 262 | if (firstOtherStreamId != streamId) { |
| 263 | |
| 264 | size_t otherBufferCount = |
| 265 | streamSet.handoutBufferCountMap.valueFor(firstOtherStreamId); |
| 266 | size_t otherAttachedBufferCount = |
| 267 | streamSet.attachedBufferCountMap.valueFor(firstOtherStreamId); |
| 268 | if (otherAttachedBufferCount > otherBufferCount) { |
| 269 | freeBufferIsAttached = true; |
| 270 | break; |
| 271 | } |
| 272 | if (hasBufferForStreamLocked(streamSet.freeBuffers, firstOtherStreamId)) { |
| 273 | freeBufferIsAttached = false; |
| 274 | break; |
| 275 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 276 | } |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 277 | firstOtherStreamId = CAMERA3_STREAM_ID_INVALID; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 278 | } |
| 279 | if (firstOtherStreamId == CAMERA3_STREAM_ID_INVALID) { |
| 280 | return OK; |
| 281 | } |
| 282 | |
| 283 | // This will drop the reference to one free buffer, which will effectively free one |
| 284 | // buffer (from the free buffer list) for the inactive streams. |
| 285 | size_t totalAllocatedBufferCount = streamSet.freeBuffers.size(); |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 286 | for (size_t i = 0; i < streamSet.attachedBufferCountMap.size(); i++) { |
| 287 | totalAllocatedBufferCount += streamSet.attachedBufferCountMap[i]; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 288 | } |
| 289 | if (totalAllocatedBufferCount > streamSet.allocatedBufferWaterMark) { |
Zhijun He | 80fa619 | 2016-02-08 12:02:25 -0800 | [diff] [blame] | 290 | ALOGV("%s: free a buffer from stream %d", __FUNCTION__, firstOtherStreamId); |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 291 | if (freeBufferIsAttached) { |
| 292 | ALOGV("Stream %d: Freeing buffer: detach", firstOtherStreamId); |
| 293 | sp<Camera3OutputStream> stream = |
| 294 | mStreamMap.valueFor(firstOtherStreamId).promote(); |
| 295 | if (stream == nullptr) { |
| 296 | ALOGE("%s: unable to promote stream %d to detach buffer", __FUNCTION__, |
| 297 | firstOtherStreamId); |
| 298 | return INVALID_OPERATION; |
| 299 | } |
| 300 | |
| 301 | // Detach and then drop the buffer. |
| 302 | // |
| 303 | // Need to unlock because the stream may also be calling |
| 304 | // into the buffer manager in parallel to signal buffer |
| 305 | // release, or acquire a new buffer. |
| 306 | { |
| 307 | mLock.unlock(); |
| 308 | sp<GraphicBuffer> buffer; |
| 309 | stream->detachBuffer(&buffer, /*fenceFd*/ nullptr); |
| 310 | mLock.lock(); |
| 311 | } |
| 312 | size_t& otherAttachedBufferCount = |
| 313 | streamSet.attachedBufferCountMap.editValueFor(firstOtherStreamId); |
| 314 | otherAttachedBufferCount--; |
| 315 | } else { |
| 316 | // Droppable buffer is in the free buffer list, grab and drop |
| 317 | getFirstBufferFromBufferListLocked(streamSet.freeBuffers, firstOtherStreamId); |
| 318 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | } else { |
| 322 | // TODO: implement this. |
| 323 | return BAD_VALUE; |
| 324 | } |
| 325 | |
| 326 | return OK; |
| 327 | } |
| 328 | |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 329 | status_t Camera3BufferManager::onBufferReleased(int streamId, int streamSetId) { |
| 330 | ATRACE_CALL(); |
| 331 | Mutex::Autolock l(mLock); |
| 332 | |
| 333 | ALOGV("Stream %d set %d: Buffer released", streamId, streamSetId); |
| 334 | if (mAllocator == NULL) { |
| 335 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 336 | return INVALID_OPERATION; |
| 337 | } |
| 338 | |
| 339 | if (!checkIfStreamRegisteredLocked(streamId, streamSetId)){ |
| 340 | ALOGV("%s: signaling buffer release for an already unregistered stream " |
| 341 | "(stream %d with set id %d)", __FUNCTION__, streamId, streamSetId); |
| 342 | return OK; |
| 343 | } |
| 344 | |
| 345 | if (mGrallocVersion < HARDWARE_DEVICE_API_VERSION(1,0)) { |
| 346 | StreamSet& streamSet = mStreamSetMap.editValueFor(streamSetId); |
| 347 | BufferCountMap& handOutBufferCounts = streamSet.handoutBufferCountMap; |
| 348 | size_t& bufferCount = handOutBufferCounts.editValueFor(streamId); |
| 349 | bufferCount--; |
| 350 | ALOGV("%s: Stream %d set %d: Buffer count now %zu", __FUNCTION__, streamId, streamSetId, |
| 351 | bufferCount); |
| 352 | } else { |
| 353 | // TODO: implement gralloc V1 support |
| 354 | return BAD_VALUE; |
| 355 | } |
| 356 | |
| 357 | return OK; |
| 358 | } |
| 359 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 360 | status_t Camera3BufferManager::returnBufferForStream(int streamId, |
| 361 | int streamSetId, const sp<GraphicBuffer>& buffer, int fenceFd) { |
| 362 | ATRACE_CALL(); |
| 363 | Mutex::Autolock l(mLock); |
| 364 | ALOGV_IF(buffer != 0, "%s: return buffer (%p) with handle (%p) for stream %d and stream set %d", |
| 365 | __FUNCTION__, buffer.get(), buffer->handle, streamId, streamSetId); |
| 366 | if (mAllocator == NULL) { |
| 367 | ALOGE("%s: allocator is NULL, buffer manager is bad state.", __FUNCTION__); |
| 368 | return INVALID_OPERATION; |
| 369 | } |
| 370 | |
| 371 | if (!checkIfStreamRegisteredLocked(streamId, streamSetId)){ |
| 372 | ALOGV("%s: returning buffer for an already unregistered stream (stream %d with set id %d)," |
| 373 | "buffer will be dropped right away!", __FUNCTION__, streamId, streamSetId); |
| 374 | return OK; |
| 375 | } |
| 376 | |
| 377 | if (mGrallocVersion < HARDWARE_DEVICE_API_VERSION(1,0)) { |
| 378 | // Add to the freeBuffer list. |
| 379 | StreamSet& streamSet = mStreamSetMap.editValueFor(streamSetId); |
| 380 | if (buffer != 0) { |
| 381 | BufferEntry entry; |
| 382 | entry.add(streamId, GraphicBufferEntry(buffer, fenceFd)); |
| 383 | status_t res = addBufferToBufferListLocked(streamSet.freeBuffers, entry); |
| 384 | if (res != OK) { |
| 385 | ALOGE("%s: add buffer to free buffer list failed", __FUNCTION__); |
| 386 | return res; |
| 387 | } |
| 388 | } |
| 389 | |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 390 | // Update the handed out and attached buffer count for this buffer. |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 391 | BufferCountMap& handOutBufferCounts = streamSet.handoutBufferCountMap; |
| 392 | size_t& bufferCount = handOutBufferCounts.editValueFor(streamId); |
| 393 | bufferCount--; |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 394 | size_t& attachedBufferCount = streamSet.attachedBufferCountMap.editValueFor(streamId); |
| 395 | attachedBufferCount--; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 396 | } else { |
| 397 | // TODO: implement this. |
| 398 | return BAD_VALUE; |
| 399 | } |
| 400 | |
| 401 | return OK; |
| 402 | } |
| 403 | |
| 404 | void Camera3BufferManager::dump(int fd, const Vector<String16>& args) const { |
| 405 | Mutex::Autolock l(mLock); |
| 406 | |
| 407 | (void) args; |
| 408 | String8 lines; |
| 409 | lines.appendFormat(" Total stream sets: %zu\n", mStreamSetMap.size()); |
| 410 | for (size_t i = 0; i < mStreamSetMap.size(); i++) { |
| 411 | lines.appendFormat(" Stream set %d has below streams:\n", mStreamSetMap.keyAt(i)); |
| 412 | for (size_t j = 0; j < mStreamSetMap[i].streamInfoMap.size(); j++) { |
| 413 | lines.appendFormat(" Stream %d\n", mStreamSetMap[i].streamInfoMap[j].streamId); |
| 414 | } |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 415 | lines.appendFormat(" Stream set max allowed buffer count: %zu\n", |
| 416 | mStreamSetMap[i].maxAllowedBufferCount); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 417 | lines.appendFormat(" Stream set buffer count water mark: %zu\n", |
| 418 | mStreamSetMap[i].allocatedBufferWaterMark); |
| 419 | lines.appendFormat(" Handout buffer counts:\n"); |
| 420 | for (size_t m = 0; m < mStreamSetMap[i].handoutBufferCountMap.size(); m++) { |
| 421 | int streamId = mStreamSetMap[i].handoutBufferCountMap.keyAt(m); |
| 422 | size_t bufferCount = mStreamSetMap[i].handoutBufferCountMap.valueAt(m); |
| 423 | lines.appendFormat(" stream id: %d, buffer count: %zu.\n", |
| 424 | streamId, bufferCount); |
| 425 | } |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 426 | lines.appendFormat(" Attached buffer counts:\n"); |
| 427 | for (size_t m = 0; m < mStreamSetMap[i].attachedBufferCountMap.size(); m++) { |
| 428 | int streamId = mStreamSetMap[i].attachedBufferCountMap.keyAt(m); |
| 429 | size_t bufferCount = mStreamSetMap[i].attachedBufferCountMap.valueAt(m); |
| 430 | lines.appendFormat(" stream id: %d, attached buffer count: %zu.\n", |
| 431 | streamId, bufferCount); |
| 432 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 433 | |
| 434 | lines.appendFormat(" Free buffer count: %zu\n", |
| 435 | mStreamSetMap[i].freeBuffers.size()); |
| 436 | for (auto& bufEntry : mStreamSetMap[i].freeBuffers) { |
| 437 | for (size_t m = 0; m < bufEntry.size(); m++) { |
| 438 | const sp<GraphicBuffer>& buffer = bufEntry.valueAt(m).graphicBuffer; |
| 439 | int streamId = bufEntry.keyAt(m); |
| 440 | lines.appendFormat(" stream id: %d, buffer: %p, handle: %p.\n", |
| 441 | streamId, buffer.get(), buffer->handle); |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | write(fd, lines.string(), lines.size()); |
| 446 | } |
| 447 | |
| 448 | bool Camera3BufferManager::checkIfStreamRegisteredLocked(int streamId, int streamSetId) const { |
| 449 | ssize_t setIdx = mStreamSetMap.indexOfKey(streamSetId); |
| 450 | if (setIdx == NAME_NOT_FOUND) { |
| 451 | ALOGV("%s: stream set %d is not registered to stream set map yet!", |
| 452 | __FUNCTION__, streamSetId); |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | ssize_t streamIdx = mStreamSetMap.valueAt(setIdx).streamInfoMap.indexOfKey(streamId); |
| 457 | if (streamIdx == NAME_NOT_FOUND) { |
| 458 | ALOGV("%s: stream %d is not registered to stream info map yet!", __FUNCTION__, streamId); |
| 459 | return false; |
| 460 | } |
| 461 | |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 462 | size_t bufferWaterMark = mStreamSetMap[setIdx].maxAllowedBufferCount; |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 463 | if (bufferWaterMark == 0 || bufferWaterMark > kMaxBufferCount) { |
| 464 | ALOGW("%s: stream %d with stream set %d is not registered correctly to stream set map," |
| 465 | " as the water mark (%zu) is wrong!", |
| 466 | __FUNCTION__, streamId, streamSetId, bufferWaterMark); |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | status_t Camera3BufferManager::addBufferToBufferListLocked(BufferList& bufList, |
| 474 | const BufferEntry& buffer) { |
| 475 | // TODO: need add some sanity check here. |
| 476 | bufList.push_back(buffer); |
| 477 | |
| 478 | return OK; |
| 479 | } |
| 480 | |
| 481 | status_t Camera3BufferManager::removeBuffersFromBufferListLocked(BufferList& bufferList, |
| 482 | int streamId) { |
| 483 | BufferList::iterator i = bufferList.begin(); |
| 484 | while (i != bufferList.end()) { |
| 485 | ssize_t idx = i->indexOfKey(streamId); |
| 486 | if (idx != NAME_NOT_FOUND) { |
Zhijun He | 1ff811b | 2016-01-26 14:39:51 -0800 | [diff] [blame] | 487 | ALOGV("%s: Remove a buffer for stream %d, free buffer total count: %zu", |
| 488 | __FUNCTION__, streamId, bufferList.size()); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 489 | i->removeItem(streamId); |
| 490 | if (i->isEmpty()) { |
| 491 | i = bufferList.erase(i); |
| 492 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 493 | } else { |
| 494 | i++; |
| 495 | } |
| 496 | } |
| 497 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 498 | return OK; |
| 499 | } |
| 500 | |
Zhijun He | 8d1a154 | 2016-01-29 20:28:21 -0800 | [diff] [blame] | 501 | bool Camera3BufferManager::hasBufferForStreamLocked(BufferList& buffers, int streamId) { |
| 502 | BufferList::iterator i = buffers.begin(); |
| 503 | while (i != buffers.end()) { |
| 504 | ssize_t idx = i->indexOfKey(streamId); |
| 505 | if (idx != NAME_NOT_FOUND) { |
| 506 | return true; |
| 507 | } |
| 508 | i++; |
| 509 | } |
| 510 | |
| 511 | return false; |
| 512 | } |
| 513 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 514 | Camera3BufferManager::GraphicBufferEntry Camera3BufferManager::getFirstBufferFromBufferListLocked( |
| 515 | BufferList& buffers, int streamId) { |
| 516 | // Try to get the first buffer from the free buffer list if there is one. |
| 517 | GraphicBufferEntry entry; |
| 518 | BufferList::iterator i = buffers.begin(); |
| 519 | while (i != buffers.end()) { |
| 520 | ssize_t idx = i->indexOfKey(streamId); |
| 521 | if (idx != NAME_NOT_FOUND) { |
| 522 | entry = GraphicBufferEntry(i->valueAt(idx)); |
| 523 | i = buffers.erase(i); |
| 524 | break; |
| 525 | } else { |
| 526 | i++; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | ALOGV_IF(entry.graphicBuffer == 0, "%s: Unable to find free buffer for stream %d", |
| 531 | __FUNCTION__, streamId); |
| 532 | return entry; |
| 533 | } |
| 534 | |
| 535 | } // namespace camera3 |
| 536 | } // namespace android |