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 "system/graphics-base-v1.0.h"
19 #define LOG_TAG "GCH_SnapshotRequestProcessor"
20 #define ATRACE_TAG ATRACE_TAG_CAMERA
21 #include <log/log.h>
22 #include <utils/Trace.h>
23 
24 #include "snapshot_request_processor.h"
25 #include "vendor_tag_defs.h"
26 
27 namespace android {
28 namespace google_camera_hal {
29 
Create(CameraDeviceSessionHwl * device_session_hwl,HwlSessionCallback session_callback,int32_t yuv_stream_id)30 std::unique_ptr<SnapshotRequestProcessor> SnapshotRequestProcessor::Create(
31     CameraDeviceSessionHwl* device_session_hwl,
32     HwlSessionCallback session_callback, int32_t yuv_stream_id) {
33   ATRACE_CALL();
34   if (device_session_hwl == nullptr) {
35     ALOGE("%s: device_session_hwl (%p) is nullptr", __FUNCTION__,
36           device_session_hwl);
37     return nullptr;
38   }
39 
40   auto request_processor = std::unique_ptr<SnapshotRequestProcessor>(
41       new SnapshotRequestProcessor(session_callback));
42   if (request_processor == nullptr) {
43     ALOGE("%s: Creating SnapshotRequestProcessor failed.", __FUNCTION__);
44     return nullptr;
45   }
46 
47   status_t res =
48       request_processor->Initialize(device_session_hwl, yuv_stream_id);
49   if (res != OK) {
50     ALOGE("%s: Initializing SnapshotRequestProcessor failed: %s (%d).",
51           __FUNCTION__, strerror(-res), res);
52     return nullptr;
53   }
54 
55   return request_processor;
56 }
57 
Initialize(CameraDeviceSessionHwl * device_session_hwl,int32_t yuv_stream_id)58 status_t SnapshotRequestProcessor::Initialize(
59     CameraDeviceSessionHwl* device_session_hwl, int32_t yuv_stream_id) {
60   ATRACE_CALL();
61   std::unique_ptr<HalCameraMetadata> characteristics;
62   status_t res = device_session_hwl->GetCameraCharacteristics(&characteristics);
63   if (res != OK) {
64     ALOGE("%s: GetCameraCharacteristics failed.", __FUNCTION__);
65     return BAD_VALUE;
66   }
67 
68   camera_metadata_ro_entry entry;
69   res = characteristics->Get(
70       ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, &entry);
71   if (res == OK) {
72     active_array_width_ = entry.data.i32[2];
73     active_array_height_ = entry.data.i32[3];
74     ALOGI("%s Active size (%d x %d).", __FUNCTION__, active_array_width_,
75           active_array_height_);
76   } else {
77     ALOGE("%s Get active size failed: %s (%d).", __FUNCTION__, strerror(-res),
78           res);
79     return res;
80   }
81 
82   yuv_stream_id_ = yuv_stream_id;
83 
84   return OK;
85 }
86 
ConfigureStreams(InternalStreamManager * internal_stream_manager,const StreamConfiguration & stream_config,StreamConfiguration * process_block_stream_config)87 status_t SnapshotRequestProcessor::ConfigureStreams(
88     InternalStreamManager* internal_stream_manager,
89     const StreamConfiguration& stream_config,
90     StreamConfiguration* process_block_stream_config) {
91   ATRACE_CALL();
92   if (process_block_stream_config == nullptr ||
93       internal_stream_manager == nullptr) {
94     ALOGE(
95         "%s: process_block_stream_config (%p) is nullptr or "
96         "internal_stream_manager (%p) is nullptr",
97         __FUNCTION__, process_block_stream_config, internal_stream_manager);
98     return BAD_VALUE;
99   }
100 
101   internal_stream_manager_ = internal_stream_manager;
102 
103   Stream yuv_stream;
104   yuv_stream.stream_type = StreamType::kInput;
105   yuv_stream.width = active_array_width_;
106   yuv_stream.height = active_array_height_;
107   yuv_stream.format = HAL_PIXEL_FORMAT_YCBCR_420_888;
108   yuv_stream.usage = 0;
109   yuv_stream.rotation = StreamRotation::kRotation0;
110   yuv_stream.data_space = HAL_DATASPACE_ARBITRARY;
111   // Set id back to yuv_stream and then HWL can get correct HAL stream ID
112   yuv_stream.id = yuv_stream_id_;
113 
114   process_block_stream_config->streams = stream_config.streams;
115   // Add internal yuv stream
116   process_block_stream_config->streams.push_back(yuv_stream);
117   process_block_stream_config->operation_mode = stream_config.operation_mode;
118   process_block_stream_config->session_params =
119       HalCameraMetadata::Clone(stream_config.session_params.get());
120   process_block_stream_config->stream_config_counter =
121       stream_config.stream_config_counter;
122 
123   return OK;
124 }
125 
SetProcessBlock(std::unique_ptr<ProcessBlock> process_block)126 status_t SnapshotRequestProcessor::SetProcessBlock(
127     std::unique_ptr<ProcessBlock> process_block) {
128   ATRACE_CALL();
129   if (process_block == nullptr) {
130     ALOGE("%s: process_block is nullptr", __FUNCTION__);
131     return BAD_VALUE;
132   }
133 
134   std::lock_guard<std::mutex> lock(process_block_lock_);
135   if (process_block_ != nullptr) {
136     ALOGE("%s: Already configured.", __FUNCTION__);
137     return ALREADY_EXISTS;
138   }
139 
140   process_block_ = std::move(process_block);
141   return OK;
142 }
143 
IsReadyForNextRequest()144 bool SnapshotRequestProcessor::IsReadyForNextRequest() {
145   ATRACE_CALL();
146   if (internal_stream_manager_ == nullptr) {
147     ALOGW("%s: internal_stream_manager_ nullptr", __FUNCTION__);
148     return false;
149   }
150   if (internal_stream_manager_->IsPendingBufferEmpty(yuv_stream_id_) == false) {
151     return false;
152   }
153   return true;
154 }
155 
ProcessRequest(const CaptureRequest & request)156 status_t SnapshotRequestProcessor::ProcessRequest(const CaptureRequest& request) {
157   ATRACE_CALL();
158   std::lock_guard<std::mutex> lock(process_block_lock_);
159   if (process_block_ == nullptr) {
160     ALOGE("%s: Not configured yet.", __FUNCTION__);
161     return NO_INIT;
162   }
163 
164   if (IsReadyForNextRequest() == false) {
165     return BAD_VALUE;
166   }
167 
168   CaptureRequest block_request;
169   block_request.frame_number = request.frame_number;
170   block_request.settings = HalCameraMetadata::Clone(request.settings.get());
171 
172   for (const auto& output_buffer : request.output_buffers) {
173     session_callback_.request_stream_buffers(
174         output_buffer.stream_id, /*buffer_sizes=*/1,
175         &block_request.output_buffers, request.frame_number);
176   }
177 
178   for (auto& [camera_id, physical_metadata] : request.physical_camera_settings) {
179     block_request.physical_camera_settings[camera_id] =
180         HalCameraMetadata::Clone(physical_metadata.get());
181   }
182 
183   // Get multiple yuv buffer and metadata from internal stream as input
184   status_t result = internal_stream_manager_->GetMostRecentStreamBuffer(
185       yuv_stream_id_, &(block_request.input_buffers),
186       &(block_request.input_buffer_metadata), /*payload_frames=*/kZslBufferSize);
187   if (result != OK) {
188     ALOGE("%s: frame:%d GetStreamBuffer failed.", __FUNCTION__,
189           request.frame_number);
190     session_callback_.return_stream_buffers(block_request.output_buffers);
191     return UNKNOWN_ERROR;
192   }
193 
194   // TODO(mhtan): may need to remove some metadata here.
195   std::vector<ProcessBlockRequest> block_requests(1);
196   block_requests[0].request = std::move(block_request);
197   ALOGD("%s: frame number %u is a snapshot request.", __FUNCTION__,
198         request.frame_number);
199 
200   result = process_block_->ProcessRequests(block_requests, request);
201   if (result != OK) {
202     session_callback_.return_stream_buffers(
203         block_requests[0].request.output_buffers);
204   }
205 
206   return result;
207 }
208 
Flush()209 status_t SnapshotRequestProcessor::Flush() {
210   ATRACE_CALL();
211   std::lock_guard<std::mutex> lock(process_block_lock_);
212   if (process_block_ == nullptr) {
213     return OK;
214   }
215 
216   return process_block_->Flush();
217 }
218 
219 }  // namespace google_camera_hal
220 }  // namespace android