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 process_block_stream_config->log_id = stream_config.log_id;
123
124 return OK;
125 }
126
SetProcessBlock(std::unique_ptr<ProcessBlock> process_block)127 status_t SnapshotRequestProcessor::SetProcessBlock(
128 std::unique_ptr<ProcessBlock> process_block) {
129 ATRACE_CALL();
130 if (process_block == nullptr) {
131 ALOGE("%s: process_block is nullptr", __FUNCTION__);
132 return BAD_VALUE;
133 }
134
135 std::lock_guard<std::mutex> lock(process_block_lock_);
136 if (process_block_ != nullptr) {
137 ALOGE("%s: Already configured.", __FUNCTION__);
138 return ALREADY_EXISTS;
139 }
140
141 process_block_ = std::move(process_block);
142 return OK;
143 }
144
IsReadyForNextRequest()145 bool SnapshotRequestProcessor::IsReadyForNextRequest() {
146 ATRACE_CALL();
147 if (internal_stream_manager_ == nullptr) {
148 ALOGW("%s: internal_stream_manager_ nullptr", __FUNCTION__);
149 return false;
150 }
151 if (internal_stream_manager_->IsPendingBufferEmpty(yuv_stream_id_) == false) {
152 return false;
153 }
154 return true;
155 }
156
ProcessRequest(const CaptureRequest & request)157 status_t SnapshotRequestProcessor::ProcessRequest(const CaptureRequest& request) {
158 ATRACE_CALL();
159 std::lock_guard<std::mutex> lock(process_block_lock_);
160 if (process_block_ == nullptr) {
161 ALOGE("%s: Not configured yet.", __FUNCTION__);
162 return NO_INIT;
163 }
164
165 if (IsReadyForNextRequest() == false) {
166 return BAD_VALUE;
167 }
168
169 CaptureRequest block_request;
170 block_request.frame_number = request.frame_number;
171 block_request.settings = HalCameraMetadata::Clone(request.settings.get());
172
173 for (const auto& output_buffer : request.output_buffers) {
174 session_callback_.request_stream_buffers(
175 output_buffer.stream_id, /*buffer_sizes=*/1,
176 &block_request.output_buffers, request.frame_number);
177 }
178
179 for (auto& [camera_id, physical_metadata] : request.physical_camera_settings) {
180 block_request.physical_camera_settings[camera_id] =
181 HalCameraMetadata::Clone(physical_metadata.get());
182 }
183
184 // Get multiple yuv buffer and metadata from internal stream as input
185 status_t result = internal_stream_manager_->GetMostRecentStreamBuffer(
186 yuv_stream_id_, &(block_request.input_buffers),
187 &(block_request.input_buffer_metadata), /*payload_frames=*/kZslBufferSize);
188 if (result != OK) {
189 ALOGE("%s: frame:%d GetStreamBuffer failed.", __FUNCTION__,
190 request.frame_number);
191 session_callback_.return_stream_buffers(block_request.output_buffers);
192 return UNKNOWN_ERROR;
193 }
194
195 // TODO(mhtan): may need to remove some metadata here.
196 std::vector<ProcessBlockRequest> block_requests(1);
197 block_requests[0].request = std::move(block_request);
198 ALOGD("%s: frame number %u is a snapshot request.", __FUNCTION__,
199 request.frame_number);
200
201 result = process_block_->ProcessRequests(block_requests, request);
202 if (result != OK) {
203 session_callback_.return_stream_buffers(
204 block_requests[0].request.output_buffers);
205 }
206
207 return result;
208 }
209
Flush()210 status_t SnapshotRequestProcessor::Flush() {
211 ATRACE_CALL();
212 std::lock_guard<std::mutex> lock(process_block_lock_);
213 if (process_block_ == nullptr) {
214 return OK;
215 }
216
217 return process_block_->Flush();
218 }
219
220 } // namespace google_camera_hal
221 } // namespace android