blob: d45891fc5812b0a7cb5bfb005e79be2de5dbb799 [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>
Mathias Agopian01d56fe2017-02-07 17:12:36 -080022#include <gui/IGraphicBufferAlloc.h>
Zhijun He125684a2015-12-26 15:07:30 -080023#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
29namespace android {
30
31namespace camera3 {
32
33Camera3BufferManager::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
44Camera3BufferManager::~Camera3BufferManager() {
45}
46
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -070047status_t Camera3BufferManager::registerStream(wp<Camera3OutputStream>& stream,
48 const StreamInfo& streamInfo) {
Zhijun He125684a2015-12-26 15:07:30 -080049 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 Talvala77c1a352016-06-13 12:32:43 -0700117 currentStreamSet.attachedBufferCountMap.add(streamId, 0);
118 mStreamMap.add(streamId, stream);
Zhijun He125684a2015-12-26 15:07:30 -0800119
Zhijun He8d1a1542016-01-29 20:28:21 -0800120 // 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 He125684a2015-12-26 15:07:30 -0800124 }
125
126 return OK;
127}
128
129status_t Camera3BufferManager::unregisterStream(int streamId, int streamSetId) {
130 ATRACE_CALL();
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700131
Zhijun He125684a2015-12-26 15:07:30 -0800132 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 Talvala77c1a352016-06-13 12:32:43 -0700150 BufferCountMap& attachedBufferCounts = currentSet.attachedBufferCountMap;
Zhijun He125684a2015-12-26 15:07:30 -0800151 InfoMap& infoMap = currentSet.streamInfoMap;
152 removeBuffersFromBufferListLocked(freeBufs, streamId);
153 handOutBufferCounts.removeItem(streamId);
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700154 attachedBufferCounts.removeItem(streamId);
Zhijun He125684a2015-12-26 15:07:30 -0800155
156 // Remove the stream info from info map and recalculate the buffer count water mark.
157 infoMap.removeItem(streamId);
Zhijun He8d1a1542016-01-29 20:28:21 -0800158 currentSet.maxAllowedBufferCount = 0;
Zhijun He125684a2015-12-26 15:07:30 -0800159 for (size_t i = 0; i < infoMap.size(); i++) {
Zhijun He8d1a1542016-01-29 20:28:21 -0800160 if (infoMap[i].totalBufferCount > currentSet.maxAllowedBufferCount) {
161 currentSet.maxAllowedBufferCount = infoMap[i].totalBufferCount;
Zhijun He125684a2015-12-26 15:07:30 -0800162 }
163 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700164 mStreamMap.removeItem(streamId);
165
Zhijun He8d1a1542016-01-29 20:28:21 -0800166 // 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 He125684a2015-12-26 15:07:30 -0800169
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
178status_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 He8d1a1542016-01-29 20:28:21 -0800197 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 Talvala77c1a352016-06-13 12:32:43 -0700205 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 He125684a2015-12-26 15:07:30 -0800215 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 Donner4b2f8182016-10-26 11:46:42 -0700225 info.width, info.height, info.format, 1 /* layerCount */,
226 info.combinedUsage, &res);
Zhijun He80fa6192016-02-08 12:02:25 -0800227 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 He125684a2015-12-26 15:07:30 -0800230 if (res != OK) {
231 ALOGE("%s: graphic buffer allocation failed: (error %d %s) ",
232 __FUNCTION__, res, strerror(-res));
233 return res;
234 }
Zhijun He80fa6192016-02-08 12:02:25 -0800235 ALOGV("%s: allocation done", __FUNCTION__);
Zhijun He125684a2015-12-26 15:07:30 -0800236 }
237
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700238 // Increase the hand-out and attached buffer counts for tracking purposes.
Zhijun He125684a2015-12-26 15:07:30 -0800239 bufferCount++;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700240 attachedBufferCount++;
Zhijun He80fa6192016-02-08 12:02:25 -0800241 // 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 He8d1a1542016-01-29 20:28:21 -0800246 }
Zhijun He125684a2015-12-26 15:07:30 -0800247 *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 Talvala77c1a352016-06-13 12:32:43 -0700259 bool freeBufferIsAttached = false;
Zhijun He125684a2015-12-26 15:07:30 -0800260 for (size_t i = 0; i < streamSet.streamInfoMap.size(); i++) {
261 firstOtherStreamId = streamSet.streamInfoMap[i].streamId;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700262 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 He125684a2015-12-26 15:07:30 -0800276 }
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700277 firstOtherStreamId = CAMERA3_STREAM_ID_INVALID;
Zhijun He125684a2015-12-26 15:07:30 -0800278 }
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 Talvala77c1a352016-06-13 12:32:43 -0700286 for (size_t i = 0; i < streamSet.attachedBufferCountMap.size(); i++) {
287 totalAllocatedBufferCount += streamSet.attachedBufferCountMap[i];
Zhijun He125684a2015-12-26 15:07:30 -0800288 }
289 if (totalAllocatedBufferCount > streamSet.allocatedBufferWaterMark) {
Zhijun He80fa6192016-02-08 12:02:25 -0800290 ALOGV("%s: free a buffer from stream %d", __FUNCTION__, firstOtherStreamId);
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700291 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 He125684a2015-12-26 15:07:30 -0800319 }
320 }
321 } else {
322 // TODO: implement this.
323 return BAD_VALUE;
324 }
325
326 return OK;
327}
328
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700329status_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 He125684a2015-12-26 15:07:30 -0800360status_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 Talvala77c1a352016-06-13 12:32:43 -0700390 // Update the handed out and attached buffer count for this buffer.
Zhijun He125684a2015-12-26 15:07:30 -0800391 BufferCountMap& handOutBufferCounts = streamSet.handoutBufferCountMap;
392 size_t& bufferCount = handOutBufferCounts.editValueFor(streamId);
393 bufferCount--;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700394 size_t& attachedBufferCount = streamSet.attachedBufferCountMap.editValueFor(streamId);
395 attachedBufferCount--;
Zhijun He125684a2015-12-26 15:07:30 -0800396 } else {
397 // TODO: implement this.
398 return BAD_VALUE;
399 }
400
401 return OK;
402}
403
404void 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 He8d1a1542016-01-29 20:28:21 -0800415 lines.appendFormat(" Stream set max allowed buffer count: %zu\n",
416 mStreamSetMap[i].maxAllowedBufferCount);
Zhijun He125684a2015-12-26 15:07:30 -0800417 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 Talvala77c1a352016-06-13 12:32:43 -0700426 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 He125684a2015-12-26 15:07:30 -0800433
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
448bool 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 He8d1a1542016-01-29 20:28:21 -0800462 size_t bufferWaterMark = mStreamSetMap[setIdx].maxAllowedBufferCount;
Zhijun He125684a2015-12-26 15:07:30 -0800463 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
473status_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
481status_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 He1ff811b2016-01-26 14:39:51 -0800487 ALOGV("%s: Remove a buffer for stream %d, free buffer total count: %zu",
488 __FUNCTION__, streamId, bufferList.size());
Zhijun He125684a2015-12-26 15:07:30 -0800489 i->removeItem(streamId);
490 if (i->isEmpty()) {
491 i = bufferList.erase(i);
492 }
Zhijun He125684a2015-12-26 15:07:30 -0800493 } else {
494 i++;
495 }
496 }
497
Zhijun He125684a2015-12-26 15:07:30 -0800498 return OK;
499}
500
Zhijun He8d1a1542016-01-29 20:28:21 -0800501bool 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 He125684a2015-12-26 15:07:30 -0800514Camera3BufferManager::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