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