1 /*
2  * Copyright (C) 2019 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 #include <cstdint>
19 #define LOG_TAG "GCH_InternalStreamManager"
20 #define ATRACE_TAG ATRACE_TAG_CAMERA
21 #include <log/log.h>
22 #include <utils/Trace.h>
23 
24 #include "hal_utils.h"
25 #include "internal_stream_manager.h"
26 
27 namespace android {
28 namespace google_camera_hal {
29 
30 namespace {
GetNextAvailableStreamId()31 int32_t GetNextAvailableStreamId() {
32   static int32_t next_available_stream_id = kHalInternalStreamStart;
33   static std::mutex next_available_stream_id_mutex;
34   int32_t result;
35   {
36     std::lock_guard<std::mutex> lock(next_available_stream_id_mutex);
37     result = next_available_stream_id++;
38   }
39   return result;
40 }
41 }  // namespace
42 
Create(IHalBufferAllocator * buffer_allocator,int partial_result_count)43 std::unique_ptr<InternalStreamManager> InternalStreamManager::Create(
44     IHalBufferAllocator* buffer_allocator, int partial_result_count) {
45   ATRACE_CALL();
46   auto stream_manager =
47       std::unique_ptr<InternalStreamManager>(new InternalStreamManager());
48   if (stream_manager == nullptr) {
49     ALOGE("%s: Creating InternalStreamManager failed.", __FUNCTION__);
50     return nullptr;
51   }
52 
53   stream_manager->Initialize(buffer_allocator, partial_result_count);
54 
55   return stream_manager;
56 }
57 
Initialize(IHalBufferAllocator * buffer_allocator,int partial_result_count)58 void InternalStreamManager::Initialize(IHalBufferAllocator* buffer_allocator,
59                                        int partial_result_count) {
60   hwl_buffer_allocator_ = buffer_allocator;
61   partial_result_count_ = partial_result_count;
62 }
63 
IsStreamRegisteredLocked(int32_t stream_id) const64 status_t InternalStreamManager::IsStreamRegisteredLocked(int32_t stream_id) const {
65   return registered_streams_.find(stream_id) != registered_streams_.end();
66 }
67 
IsStreamAllocatedLocked(int32_t stream_id) const68 status_t InternalStreamManager::IsStreamAllocatedLocked(int32_t stream_id) const {
69   // Check if this stream is sharing buffers with another stream or owns a
70   // a buffer manager.
71   return shared_stream_owner_ids_.find(stream_id) !=
72              shared_stream_owner_ids_.end() ||
73          buffer_managers_.find(stream_id) != buffer_managers_.end();
74 }
75 
GetBufferManagerOwnerIdLocked(int32_t stream_id)76 int32_t InternalStreamManager::GetBufferManagerOwnerIdLocked(int32_t stream_id) {
77   int32_t owner_stream_id = stream_id;
78   auto owner_id_it = shared_stream_owner_ids_.find(stream_id);
79   if (owner_id_it != shared_stream_owner_ids_.end()) {
80     owner_stream_id = owner_id_it->second;
81   }
82 
83   if (buffer_managers_.find(owner_stream_id) == buffer_managers_.end()) {
84     return kInvalidStreamId;
85   }
86 
87   return owner_stream_id;
88 }
89 
RegisterNewInternalStream(const Stream & stream,int32_t * stream_id)90 status_t InternalStreamManager::RegisterNewInternalStream(const Stream& stream,
91                                                           int32_t* stream_id) {
92   ATRACE_CALL();
93   std::lock_guard<std::mutex> lock(stream_mutex_);
94   if (stream_id == nullptr) {
95     ALOGE("%s: stream_id is nullptr.", __FUNCTION__);
96     return BAD_VALUE;
97   }
98 
99   Stream internal_stream = stream;
100   int32_t id = stream.id;
101   // if stream.id is one of reserved ids in camera_buffer_allocator_hwl.h, we
102   // will use the given id so that HWL can use its predefined id to setup
103   // implementation defined internal stream format. other wise will use the next
104   // available unique id.
105   if (stream.id < kStreamIdReserve) {
106     id = GetNextAvailableStreamId();
107     internal_stream.id = id;
108   }
109   registered_streams_[id] = std::move(internal_stream);
110 
111   *stream_id = id;
112   return OK;
113 }
114 
GetBufferDescriptor(const Stream & stream,const HalStream & hal_stream,uint32_t additional_num_buffers,HalBufferDescriptor * buffer_descriptor)115 status_t InternalStreamManager::GetBufferDescriptor(
116     const Stream& stream, const HalStream& hal_stream,
117     uint32_t additional_num_buffers, HalBufferDescriptor* buffer_descriptor) {
118   ATRACE_CALL();
119   if (buffer_descriptor == nullptr) {
120     ALOGE("%s: buffer_descriptor is nullptr", __FUNCTION__);
121     return BAD_VALUE;
122   }
123 
124   if (stream.id != hal_stream.id) {
125     ALOGE("%s: IDs don't match: stream %d hal stream %d", __FUNCTION__,
126           stream.id, hal_stream.id);
127     return BAD_VALUE;
128   }
129 
130   buffer_descriptor->stream_id = stream.id;
131   buffer_descriptor->width = stream.width;
132   buffer_descriptor->height = stream.height;
133   buffer_descriptor->format = hal_stream.override_format;
134   buffer_descriptor->producer_flags = hal_stream.producer_usage;
135   buffer_descriptor->consumer_flags = hal_stream.consumer_usage;
136   buffer_descriptor->immediate_num_buffers = hal_stream.max_buffers;
137   buffer_descriptor->max_num_buffers =
138       hal_stream.max_buffers + additional_num_buffers;
139 
140   return OK;
141 }
142 
AllocateBuffers(const HalStream & hal_stream,uint32_t additional_num_buffers,bool need_vendor_buffer)143 status_t InternalStreamManager::AllocateBuffers(const HalStream& hal_stream,
144                                                 uint32_t additional_num_buffers,
145                                                 bool need_vendor_buffer) {
146   ATRACE_CALL();
147   std::lock_guard<std::mutex> lock(stream_mutex_);
148   return AllocateBuffersLocked(hal_stream, additional_num_buffers,
149                                need_vendor_buffer);
150 }
151 
AllocateBuffersLocked(const HalStream & hal_stream,uint32_t additional_num_buffers,bool need_vendor_buffer)152 status_t InternalStreamManager::AllocateBuffersLocked(
153     const HalStream& hal_stream, uint32_t additional_num_buffers,
154     bool need_vendor_buffer) {
155   ATRACE_CALL();
156   int32_t stream_id = hal_stream.id;
157 
158   if (!IsStreamRegisteredLocked(stream_id)) {
159     ALOGE("%s: Stream %d was not registered.", __FUNCTION__, stream_id);
160     return NAME_NOT_FOUND;
161   }
162 
163   if (IsStreamAllocatedLocked(stream_id)) {
164     ALOGE("%s: Stream %d is already allocated.", __FUNCTION__, stream_id);
165     return ALREADY_EXISTS;
166   }
167 
168   HalBufferDescriptor buffer_descriptor;
169   status_t res = GetBufferDescriptor(registered_streams_[stream_id], hal_stream,
170                                      additional_num_buffers, &buffer_descriptor);
171   if (res != OK) {
172     ALOGE("%s: Getting buffer descriptor failed: %s(%d)", __FUNCTION__,
173           strerror(-res), res);
174     return res;
175   }
176 
177   auto buffer_manager = std::make_unique<ZslBufferManager>(
178       need_vendor_buffer ? hwl_buffer_allocator_ : nullptr,
179       partial_result_count_);
180   if (buffer_manager == nullptr) {
181     ALOGE("%s: Failed to create a buffer manager for stream %d", __FUNCTION__,
182           stream_id);
183     return UNKNOWN_ERROR;
184   }
185 
186   res = buffer_manager->AllocateBuffers(buffer_descriptor);
187   if (res != OK) {
188     ALOGE(
189         "%s: Failed to allocate %u immediate buffers (max: %u) for stream %d: "
190         "%s(%d)",
191         __FUNCTION__, buffer_descriptor.immediate_num_buffers,
192         buffer_descriptor.max_num_buffers, stream_id, strerror(-res), res);
193     return res;
194   }
195 
196   buffer_managers_[stream_id] = std::move(buffer_manager);
197   return OK;
198 }
199 
AreStreamsCompatible(const Stream & stream_0,const HalStream & hal_stream_0,const Stream & stream_1,const HalStream & hal_stream_1) const200 bool InternalStreamManager::AreStreamsCompatible(
201     const Stream& stream_0, const HalStream& hal_stream_0,
202     const Stream& stream_1, const HalStream& hal_stream_1) const {
203   return stream_0.width == stream_1.width &&
204          stream_0.height == stream_1.height &&
205          stream_0.rotation == stream_1.rotation &&
206          hal_stream_0.override_format == hal_stream_1.override_format &&
207          hal_stream_0.producer_usage == hal_stream_1.producer_usage &&
208          hal_stream_0.consumer_usage == hal_stream_1.consumer_usage &&
209          hal_stream_0.override_data_space == hal_stream_1.override_data_space;
210 }
211 
CanHalStreamsShareBuffersLocked(const std::vector<HalStream> & hal_streams) const212 bool InternalStreamManager::CanHalStreamsShareBuffersLocked(
213     const std::vector<HalStream>& hal_streams) const {
214   if (hal_streams.size() < 2) {
215     ALOGV("%s: Cannot sharing buffers for %zu stream.", __FUNCTION__,
216           hal_streams.size());
217     return BAD_VALUE;
218   }
219 
220   int32_t first_stream_id = hal_streams[0].id;
221   for (uint32_t i = 0; i < hal_streams.size(); i++) {
222     int32_t stream_id = hal_streams[i].id;
223     if (!IsStreamRegisteredLocked(stream_id)) {
224       ALOGE("%s: stream id %d was not registered.", __FUNCTION__, stream_id);
225       return false;
226     }
227 
228     if (i == 0) {
229       // Skip the first one.
230       continue;
231     }
232 
233     if (!AreStreamsCompatible(registered_streams_.at(first_stream_id),
234                               hal_streams[0], registered_streams_.at(stream_id),
235                               hal_streams[i])) {
236       ALOGV("%s: Stream %d and %d are not compatible", __FUNCTION__,
237             first_stream_id, stream_id);
238       IF_ALOGV() {
239         hal_utils::DumpStream(registered_streams_.at(first_stream_id),
240                               "stream_0");
241         hal_utils::DumpStream(registered_streams_.at(stream_id), "stream_1");
242         hal_utils::DumpHalStream(hal_streams[0], "hal_stream_0");
243         hal_utils::DumpHalStream(hal_streams[i], "hal_stream_1");
244       }
245 
246       return false;
247     }
248   }
249 
250   return true;
251 }
252 
AllocateSharedBuffers(const std::vector<HalStream> & hal_streams,uint32_t additional_num_buffers,bool need_vendor_buffer)253 status_t InternalStreamManager::AllocateSharedBuffers(
254     const std::vector<HalStream>& hal_streams, uint32_t additional_num_buffers,
255     bool need_vendor_buffer) {
256   std::lock_guard<std::mutex> lock(stream_mutex_);
257   if (hal_streams.size() < 2) {
258     ALOGE("%s: Cannot sharing buffers for %zu stream.", __FUNCTION__,
259           hal_streams.size());
260     return BAD_VALUE;
261   }
262 
263   uint32_t max_buffers = 0;
264   uint32_t total_max_buffers = 0;
265 
266   // Find the maximum and total of all hal_streams' max_buffers.
267   for (auto& hal_stream : hal_streams) {
268     if (!IsStreamRegisteredLocked(hal_stream.id)) {
269       ALOGE("%s: Stream %d was not registered.", __FUNCTION__, hal_stream.id);
270       return BAD_VALUE;
271     }
272 
273     if (IsStreamAllocatedLocked(hal_stream.id)) {
274       ALOGE("%s: Stream %d has been allocated.", __FUNCTION__, hal_stream.id);
275       return BAD_VALUE;
276     }
277 
278     total_max_buffers += hal_stream.max_buffers;
279     max_buffers = std::max(max_buffers, hal_stream.max_buffers);
280   }
281 
282   if (!CanHalStreamsShareBuffersLocked(hal_streams)) {
283     ALOGE("%s: Streams cannot share buffers.", __FUNCTION__);
284     return BAD_VALUE;
285   }
286 
287   // Allocate the maximum of all hal_streams' max_buffers immediately and
288   // additional (total_max_buffers + additional_num_buffers - max_buffers)
289   // buffers.
290   HalStream hal_stream = hal_streams[0];
291   hal_stream.max_buffers = max_buffers;
292   uint32_t total_additional_num_buffers =
293       total_max_buffers + additional_num_buffers - max_buffers;
294 
295   status_t res = AllocateBuffersLocked(hal_stream, total_additional_num_buffers,
296                                        need_vendor_buffer);
297   if (res != OK) {
298     ALOGE("%s: Allocating buffers for stream %d failed: %s(%d)", __FUNCTION__,
299           hal_stream.id, strerror(-res), res);
300     return res;
301   }
302 
303   for (uint32_t i = 1; i < hal_streams.size(); i++) {
304     shared_stream_owner_ids_[hal_streams[i].id] = hal_streams[0].id;
305   }
306 
307   return OK;
308 }
309 
RemoveOwnerStreamIdLocked(int32_t old_owner_stream_id)310 status_t InternalStreamManager::RemoveOwnerStreamIdLocked(
311     int32_t old_owner_stream_id) {
312   int32_t new_owner_stream_id = kInvalidStreamId;
313 
314   if (buffer_managers_.find(old_owner_stream_id) == buffer_managers_.end()) {
315     ALOGE("%s: Stream %d does not own any buffer manager.", __FUNCTION__,
316           old_owner_stream_id);
317     return BAD_VALUE;
318   }
319 
320   // Find the first stream that shares the buffer manager that
321   // old_owner_stream_id owns, and update the rest of the streams to point to
322   // the new owner.
323   auto owner_stream_id_it = shared_stream_owner_ids_.begin();
324   while (owner_stream_id_it != shared_stream_owner_ids_.end()) {
325     if (owner_stream_id_it->second == old_owner_stream_id) {
326       if (new_owner_stream_id == kInvalidStreamId) {
327         // Found the first stream sharing the old owner's buffer manager.
328         // Make this the new buffer manager owner.
329         new_owner_stream_id = owner_stream_id_it->first;
330         owner_stream_id_it = shared_stream_owner_ids_.erase(owner_stream_id_it);
331         continue;
332       } else {
333         // Update the rest of the stream to point to the new owner.
334         owner_stream_id_it->second = new_owner_stream_id;
335       }
336     }
337     owner_stream_id_it++;
338   }
339 
340   if (new_owner_stream_id != kInvalidStreamId) {
341     // Update buffer manager owner.
342     buffer_managers_[new_owner_stream_id] =
343         std::move(buffer_managers_[old_owner_stream_id]);
344   }
345 
346   buffer_managers_.erase(old_owner_stream_id);
347   return OK;
348 }
349 
FreeStream(int32_t stream_id)350 void InternalStreamManager::FreeStream(int32_t stream_id) {
351   ATRACE_CALL();
352   std::lock_guard<std::mutex> lock(stream_mutex_);
353   registered_streams_.erase(stream_id);
354 
355   int32_t owner_stream_id = GetBufferManagerOwnerIdLocked(stream_id);
356   if (owner_stream_id == kInvalidStreamId) {
357     ALOGE("%s: Cannot find a owner stream ID for stream %d", __FUNCTION__,
358           stream_id);
359     return;
360   }
361 
362   if (stream_id == owner_stream_id) {
363     // Find a new owner if the owner is being freed.
364     status_t res = RemoveOwnerStreamIdLocked(owner_stream_id);
365     if (res != OK) {
366       ALOGE("%s: Removing owner stream failed: %s(%d)", __FUNCTION__,
367             strerror(-res), res);
368       return;
369     }
370   } else {
371     // If this stream is not the owner, just remove it from
372     // shared_stream_owner_ids_.
373     shared_stream_owner_ids_.erase(stream_id);
374   }
375 }
376 
GetStreamBuffer(int32_t stream_id,StreamBuffer * buffer)377 status_t InternalStreamManager::GetStreamBuffer(int32_t stream_id,
378                                                 StreamBuffer* buffer) {
379   ATRACE_CALL();
380   std::lock_guard<std::mutex> lock(stream_mutex_);
381 
382   if (!IsStreamAllocatedLocked(stream_id)) {
383     ALOGE("%s: Stream %d was not allocated.", __FUNCTION__, stream_id);
384     return ALREADY_EXISTS;
385   }
386 
387   if (buffer == nullptr) {
388     ALOGE("%s: buffer is nullptr", __FUNCTION__);
389     return BAD_VALUE;
390   }
391 
392   int32_t owner_stream_id = GetBufferManagerOwnerIdLocked(stream_id);
393   if (owner_stream_id == kInvalidStreamId) {
394     ALOGE("%s: Cannot find a owner stream ID for stream %d", __FUNCTION__,
395           stream_id);
396     return BAD_VALUE;
397   }
398 
399   buffer->buffer = buffer_managers_[owner_stream_id]->GetEmptyBuffer();
400   if (buffer->buffer == kInvalidBufferHandle) {
401     ALOGE("%s: Failed to get an empty buffer for stream %d (owner %d)",
402           __FUNCTION__, stream_id, owner_stream_id);
403     return UNKNOWN_ERROR;
404   }
405 
406   buffer->stream_id = stream_id;
407   buffer->buffer_id = 0;  // Buffer ID should be irrelevant internally in HAL.
408   buffer->status = BufferStatus::kOk;
409   buffer->acquire_fence = nullptr;
410   buffer->release_fence = nullptr;
411   return OK;
412 }
413 
IsPendingBufferEmpty(int32_t stream_id)414 bool InternalStreamManager::IsPendingBufferEmpty(int32_t stream_id) {
415   ATRACE_CALL();
416   std::lock_guard<std::mutex> lock(stream_mutex_);
417   if (!IsStreamAllocatedLocked(stream_id)) {
418     ALOGE("%s: Stream %d was not allocated.", __FUNCTION__, stream_id);
419     return false;
420   }
421 
422   int32_t owner_stream_id = GetBufferManagerOwnerIdLocked(stream_id);
423   if (owner_stream_id == kInvalidStreamId) {
424     ALOGE("%s: Cannot find a owner stream ID for stream %d", __FUNCTION__,
425           stream_id);
426     return false;
427   }
428 
429   return buffer_managers_[owner_stream_id]->IsPendingBufferEmpty();
430 }
431 
GetMostRecentStreamBuffer(int32_t stream_id,std::vector<StreamBuffer> * input_buffers,std::vector<std::unique_ptr<HalCameraMetadata>> * input_buffer_metadata,uint32_t payload_frames,int32_t min_filled_buffers)432 status_t InternalStreamManager::GetMostRecentStreamBuffer(
433     int32_t stream_id, std::vector<StreamBuffer>* input_buffers,
434     std::vector<std::unique_ptr<HalCameraMetadata>>* input_buffer_metadata,
435     uint32_t payload_frames, int32_t min_filled_buffers) {
436   ATRACE_CALL();
437   std::lock_guard<std::mutex> lock(stream_mutex_);
438 
439   if (static_cast<int32_t>(payload_frames) < min_filled_buffers) {
440     ALOGW("%s: payload frames %d is smaller than min filled buffers %d",
441           __FUNCTION__, payload_frames, min_filled_buffers);
442   }
443 
444   if (!IsStreamAllocatedLocked(stream_id)) {
445     ALOGE("%s: Stream %d was not allocated.", __FUNCTION__, stream_id);
446     return BAD_VALUE;
447   }
448 
449   int32_t owner_stream_id = GetBufferManagerOwnerIdLocked(stream_id);
450   if (owner_stream_id == kInvalidStreamId) {
451     ALOGE("%s: Cannot find a owner stream ID for stream %d", __FUNCTION__,
452           stream_id);
453     return BAD_VALUE;
454   }
455 
456   if (input_buffers == nullptr || input_buffer_metadata == nullptr) {
457     ALOGE("%s: input_buffers (%p) or input_buffer_metadata (%p) is nullptr",
458           __FUNCTION__, input_buffers, input_buffer_metadata);
459     return BAD_VALUE;
460   }
461 
462   std::vector<ZslBufferManager::ZslBuffer> filled_buffers;
463   buffer_managers_[owner_stream_id]->GetMostRecentZslBuffers(
464       &filled_buffers, payload_frames, min_filled_buffers);
465 
466   if (filled_buffers.size() == 0) {
467     ALOGE("%s: There is no input buffers.", __FUNCTION__);
468     return INVALID_OPERATION;
469   }
470 
471   // TODO(b/138592133): Remove AddPendingBuffers because internal stream manager
472   // should not be responsible for saving the pending buffers' metadata.
473   buffer_managers_[owner_stream_id]->AddPendingBuffers(filled_buffers);
474 
475   for (uint32_t i = 0; i < filled_buffers.size(); i++) {
476     StreamBuffer buffer = {};
477     buffer.stream_id = stream_id;
478     buffer.buffer_id = 0;  // Buffer ID should be irrelevant internally in HAL.
479     buffer.status = BufferStatus::kOk;
480     buffer.acquire_fence = nullptr;
481     buffer.release_fence = nullptr;
482     buffer.buffer = filled_buffers[i].buffer.buffer;
483     input_buffers->push_back(buffer);
484     if (filled_buffers[i].metadata == nullptr) {
485       std::vector<ZslBufferManager::ZslBuffer> buffers;
486       buffer_managers_[owner_stream_id]->CleanPendingBuffers(&buffers);
487       buffer_managers_[owner_stream_id]->ReturnZslBuffers(std::move(buffers));
488       return INVALID_OPERATION;
489     }
490     input_buffer_metadata->push_back(std::move(filled_buffers[i].metadata));
491   }
492 
493   return OK;
494 }
495 
ReturnZslStreamBuffers(uint32_t frame_number,int32_t stream_id)496 status_t InternalStreamManager::ReturnZslStreamBuffers(uint32_t frame_number,
497                                                        int32_t stream_id) {
498   ATRACE_CALL();
499   std::lock_guard<std::mutex> lock(stream_mutex_);
500 
501   if (!IsStreamAllocatedLocked(stream_id)) {
502     ALOGE("%s: Unknown stream ID %d.", __FUNCTION__, stream_id);
503     return BAD_VALUE;
504   }
505 
506   int32_t owner_stream_id = GetBufferManagerOwnerIdLocked(stream_id);
507   if (owner_stream_id == kInvalidStreamId) {
508     ALOGE("%s: Cannot find a owner stream ID for stream %d", __FUNCTION__,
509           stream_id);
510     return BAD_VALUE;
511   }
512 
513   std::vector<ZslBufferManager::ZslBuffer> zsl_buffers;
514   status_t res =
515       buffer_managers_[owner_stream_id]->CleanPendingBuffers(&zsl_buffers);
516   if (res != OK) {
517     ALOGE("%s: frame (%d)fail to return zsl stream buffers", __FUNCTION__,
518           frame_number);
519     return res;
520   }
521   buffer_managers_[owner_stream_id]->ReturnZslBuffers(std::move(zsl_buffers));
522 
523   return OK;
524 }
525 
ReturnStreamBuffer(const StreamBuffer & buffer)526 status_t InternalStreamManager::ReturnStreamBuffer(const StreamBuffer& buffer) {
527   ATRACE_CALL();
528   std::lock_guard<std::mutex> lock(stream_mutex_);
529   int32_t stream_id = buffer.stream_id;
530 
531   if (!IsStreamAllocatedLocked(stream_id)) {
532     ALOGE("%s: Unknown stream ID %d.", __FUNCTION__, stream_id);
533     return BAD_VALUE;
534   }
535 
536   int32_t owner_stream_id = GetBufferManagerOwnerIdLocked(stream_id);
537   if (owner_stream_id == kInvalidStreamId) {
538     ALOGE("%s: Cannot find a owner stream ID for stream %d", __FUNCTION__,
539           stream_id);
540     return BAD_VALUE;
541   }
542 
543   return buffer_managers_[owner_stream_id]->ReturnEmptyBuffer(buffer.buffer);
544 }
545 
ReturnFilledBuffer(uint32_t frame_number,const StreamBuffer & buffer)546 status_t InternalStreamManager::ReturnFilledBuffer(uint32_t frame_number,
547                                                    const StreamBuffer& buffer) {
548   ATRACE_CALL();
549   std::lock_guard<std::mutex> lock(stream_mutex_);
550   int32_t stream_id = buffer.stream_id;
551 
552   if (!IsStreamAllocatedLocked(stream_id)) {
553     ALOGE("%s: Unknown stream ID %d.", __FUNCTION__, stream_id);
554     return BAD_VALUE;
555   }
556 
557   int32_t owner_stream_id = GetBufferManagerOwnerIdLocked(stream_id);
558   if (owner_stream_id == kInvalidStreamId) {
559     ALOGE("%s: Cannot find a owner stream ID for stream %d", __FUNCTION__,
560           stream_id);
561     return BAD_VALUE;
562   }
563 
564   return buffer_managers_[owner_stream_id]->ReturnFilledBuffer(frame_number,
565                                                                buffer);
566 }
567 
ReturnMetadata(int32_t stream_id,uint32_t frame_number,const HalCameraMetadata * metadata,int partial_result)568 status_t InternalStreamManager::ReturnMetadata(int32_t stream_id,
569                                                uint32_t frame_number,
570                                                const HalCameraMetadata* metadata,
571                                                int partial_result) {
572   ATRACE_CALL();
573   std::lock_guard<std::mutex> lock(stream_mutex_);
574 
575   if (!IsStreamAllocatedLocked(stream_id)) {
576     ALOGE("%s: Unknown stream ID %d.", __FUNCTION__, stream_id);
577     return BAD_VALUE;
578   }
579 
580   int32_t owner_stream_id = GetBufferManagerOwnerIdLocked(stream_id);
581   if (owner_stream_id == kInvalidStreamId) {
582     ALOGE("%s: Cannot find a owner stream ID for stream %d", __FUNCTION__,
583           stream_id);
584     return BAD_VALUE;
585   }
586 
587   return buffer_managers_[owner_stream_id]->ReturnMetadata(
588       frame_number, metadata, partial_result);
589 }
590 
591 }  // namespace google_camera_hal
592 }  // namespace android