1 /*
2  * Copyright (C) 2021 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 <cstddef>
19 #include <memory>
20 
21 #define LOG_TAG "GCH_CaptureSessionWrapperProcessBlock"
22 #define ATRACE_TAG ATRACE_TAG_CAMERA
23 #include <log/log.h>
24 #include <utils/Trace.h>
25 
26 #include "capture_session_utils.h"
27 #include "capture_session_wrapper_process_block.h"
28 #include "hal_types.h"
29 #include "hal_utils.h"
30 #include "process_block.h"
31 #include "result_processor.h"
32 #include "utils/Errors.h"
33 
34 namespace android {
35 namespace google_camera_hal {
36 
37 std::unique_ptr<CaptureSessionWrapperProcessBlock>
Create(const std::vector<ExternalCaptureSessionFactory * > & external_capture_session_entries,const std::vector<CaptureSessionEntryFuncs> & capture_session_entries,HwlSessionCallback hwl_session_callback,CameraBufferAllocatorHwl * camera_buffer_allocator_hwl,CameraDeviceSessionHwl * device_session_hwl,std::vector<HalStream> * hal_config)38 CaptureSessionWrapperProcessBlock::Create(
39     const std::vector<ExternalCaptureSessionFactory*>&
40         external_capture_session_entries,
41     const std::vector<CaptureSessionEntryFuncs>& capture_session_entries,
42     HwlSessionCallback hwl_session_callback,
43     CameraBufferAllocatorHwl* camera_buffer_allocator_hwl,
44     CameraDeviceSessionHwl* device_session_hwl,
45     std::vector<HalStream>* hal_config) {
46   ATRACE_CALL();
47   if (!IsSupported(device_session_hwl)) {
48     ALOGE("%s: Not supported.", __FUNCTION__);
49     return nullptr;
50   }
51 
52   auto block = std::unique_ptr<CaptureSessionWrapperProcessBlock>(
53       new CaptureSessionWrapperProcessBlock(
54           external_capture_session_entries, capture_session_entries,
55           hwl_session_callback, camera_buffer_allocator_hwl, device_session_hwl,
56           hal_config));
57   if (block == nullptr) {
58     ALOGE("%s: Creating CaptureSessionWrapperProcessBlock failed.",
59           __FUNCTION__);
60     return nullptr;
61   }
62 
63   return block;
64 }
65 
IsSupported(CameraDeviceSessionHwl * device_session_hwl)66 bool CaptureSessionWrapperProcessBlock::IsSupported(
67     CameraDeviceSessionHwl* device_session_hwl) {
68   if (device_session_hwl == nullptr) {
69     ALOGE("%s: device_session_hwl is nullptr", __FUNCTION__);
70     return false;
71   }
72 
73   return true;
74 }
75 
CaptureSessionWrapperProcessBlock(const std::vector<ExternalCaptureSessionFactory * > & external_capture_session_entries,const std::vector<CaptureSessionEntryFuncs> & capture_session_entries,HwlSessionCallback hwl_session_callback,CameraBufferAllocatorHwl * camera_buffer_allocator_hwl,CameraDeviceSessionHwl * camera_device_session_hwl,std::vector<HalStream> * hal_config)76 CaptureSessionWrapperProcessBlock::CaptureSessionWrapperProcessBlock(
77     const std::vector<ExternalCaptureSessionFactory*>&
78         external_capture_session_entries,
79     const std::vector<CaptureSessionEntryFuncs>& capture_session_entries,
80     HwlSessionCallback hwl_session_callback,
81     CameraBufferAllocatorHwl* camera_buffer_allocator_hwl,
82     CameraDeviceSessionHwl* camera_device_session_hwl,
83     std::vector<HalStream>* hal_config)
84     : kCameraId(camera_device_session_hwl->GetCameraId()),
85       external_capture_session_entries_(external_capture_session_entries),
86       capture_session_entries_(capture_session_entries),
87       hwl_session_callback_(hwl_session_callback),
88       camera_buffer_allocator_hwl_(camera_buffer_allocator_hwl),
89       camera_device_session_hwl_(camera_device_session_hwl),
90       hal_config_(hal_config) {
91 }
92 
SetResultProcessor(std::unique_ptr<ResultProcessor> result_processor)93 status_t CaptureSessionWrapperProcessBlock::SetResultProcessor(
94     std::unique_ptr<ResultProcessor> result_processor) {
95   ATRACE_CALL();
96   std::lock_guard<std::mutex> lock(result_processor_lock_);
97 
98   result_processor_ = std::move(result_processor);
99   return OK;
100 }
101 
ConfigureStreams(const StreamConfiguration & stream_config,const StreamConfiguration &)102 status_t CaptureSessionWrapperProcessBlock::ConfigureStreams(
103     const StreamConfiguration& stream_config,
104     const StreamConfiguration& /*overall_config*/) {
105   ATRACE_CALL();
106   std::lock_guard lock(configure_shared_mutex_);
107   if (is_configured_) {
108     ALOGE("%s: Already configured.", __FUNCTION__);
109     return ALREADY_EXISTS;
110   }
111 
112   if (result_processor_ == nullptr) {
113     ALOGE(
114         "%s: result processor is not set yet. Not able to set the callback "
115         "function.",
116         __FUNCTION__);
117     return BAD_VALUE;
118   }
119 
120   process_capture_result_ =
121       ProcessCaptureResultFunc([this](std::unique_ptr<CaptureResult> result) {
122         ProcessBlockResult process_block_result;
123         process_block_result.result = std::move(result);
124         result_processor_->ProcessResult(std::move(process_block_result));
125       });
126   notify_ = NotifyFunc([this](const NotifyMessage& message) {
127     ProcessBlockNotifyMessage process_block_message{.message = message};
128     result_processor_->Notify(std::move(process_block_message));
129   });
130 
131   // TODO(mhtan): Add one more stream here
132   embedded_capture_session_ = CreateCaptureSession(
133       stream_config, /*wrapper_capture_session_entries=*/{},
134       external_capture_session_entries_, capture_session_entries_,
135       hwl_session_callback_, camera_buffer_allocator_hwl_,
136       camera_device_session_hwl_, hal_config_, process_capture_result_, notify_);
137   if (embedded_capture_session_ == nullptr) {
138     ALOGE("%s: Not able to create embedded capture session.", __FUNCTION__);
139     return BAD_VALUE;
140   }
141 
142   is_configured_ = true;
143   return OK;
144 }
145 
GetConfiguredHalStreams(std::vector<HalStream> * hal_streams) const146 status_t CaptureSessionWrapperProcessBlock::GetConfiguredHalStreams(
147     std::vector<HalStream>* hal_streams) const {
148   ATRACE_CALL();
149   std::lock_guard lock(configure_shared_mutex_);
150   if (hal_streams == nullptr) {
151     ALOGE("%s: hal_streams is nullptr.", __FUNCTION__);
152     return BAD_VALUE;
153   }
154 
155   if (!is_configured_) {
156     ALOGE("%s: Not configured yet.", __FUNCTION__);
157     return NO_INIT;
158   }
159 
160   return camera_device_session_hwl_->GetConfiguredHalStream(pipeline_id_,
161                                                             hal_streams);
162 }
163 
ProcessRequests(const std::vector<ProcessBlockRequest> & process_block_requests,const CaptureRequest & remaining_session_request)164 status_t CaptureSessionWrapperProcessBlock::ProcessRequests(
165     const std::vector<ProcessBlockRequest>& process_block_requests,
166     const CaptureRequest& remaining_session_request) {
167   ATRACE_CALL();
168   CaptureRequest request;
169   request.frame_number = remaining_session_request.frame_number;
170   for (auto& metadata : request.input_buffer_metadata) {
171     request.input_buffer_metadata.push_back(
172         HalCameraMetadata::Clone(metadata.get()));
173   }
174   request.input_buffers = remaining_session_request.input_buffers;
175   request.input_height = remaining_session_request.input_height;
176   request.input_width = remaining_session_request.input_width;
177   for (auto& [camera_id, physical_metadata] :
178        remaining_session_request.physical_camera_settings) {
179     request.physical_camera_settings[camera_id] =
180         HalCameraMetadata::Clone(physical_metadata.get());
181   }
182   request.settings =
183       HalCameraMetadata::Clone(remaining_session_request.settings.get());
184 
185   request.output_buffers = process_block_requests[0].request.output_buffers;
186   for (auto& buffer : request.output_buffers) {
187     if (buffer.buffer != nullptr) {
188       buffer.buffer_id = buffer.stream_id;
189     }
190   }
191 
192   return embedded_capture_session_->ProcessRequest(request);
193 }
194 
Flush()195 status_t CaptureSessionWrapperProcessBlock::Flush() {
196   ATRACE_CALL();
197   std::shared_lock lock(configure_shared_mutex_);
198   if (!is_configured_) {
199     return OK;
200   }
201 
202   return camera_device_session_hwl_->Flush();
203 }
204 
205 }  // namespace google_camera_hal
206 }  // namespace android