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