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_TAG "EmulatedLogicalState"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20
21 #include "EmulatedLogicalRequestState.h"
22
23 #include <log/log.h>
24
25 #include "vendor_tag_defs.h"
26
27 namespace android {
28
EmulatedLogicalRequestState(uint32_t camera_id)29 EmulatedLogicalRequestState::EmulatedLogicalRequestState(uint32_t camera_id)
30 : logical_camera_id_(camera_id),
31 logical_request_state_(std::make_unique<EmulatedRequestState>(camera_id)) {
32 }
33
~EmulatedLogicalRequestState()34 EmulatedLogicalRequestState::~EmulatedLogicalRequestState() {
35 }
36
Initialize(std::unique_ptr<HalCameraMetadata> static_meta,PhysicalDeviceMapPtr physical_devices)37 status_t EmulatedLogicalRequestState::Initialize(
38 std::unique_ptr<HalCameraMetadata> static_meta,
39 PhysicalDeviceMapPtr physical_devices) {
40 if ((physical_devices.get() != nullptr) && (!physical_devices->empty())) {
41 zoom_ratio_physical_camera_info_ = GetZoomRatioPhysicalCameraInfo(
42 static_meta.get(), physical_devices.get());
43
44 physical_device_map_ = std::move(physical_devices);
45
46 static const float ZOOM_RATIO_THRESHOLD = 0.001f;
47 for (const auto& one_zoom_range : zoom_ratio_physical_camera_info_) {
48 ALOGV("%s: cameraId %d, focalLength %f, zoomRatioRange [%f, %f]",
49 __FUNCTION__, one_zoom_range.physical_camera_id,
50 one_zoom_range.focal_length, one_zoom_range.min_zoom_ratio,
51 one_zoom_range.max_zoom_ratio);
52 if (std::abs(one_zoom_range.min_zoom_ratio - 1.0f) < ZOOM_RATIO_THRESHOLD) {
53 current_physical_camera_ = one_zoom_range.physical_camera_id;
54 }
55 }
56
57 if (zoom_ratio_physical_camera_info_.size() > 1) {
58 is_logical_device_ = true;
59 for (const auto& it : *physical_device_map_) {
60 std::unique_ptr<EmulatedRequestState> physical_request_state =
61 std::make_unique<EmulatedRequestState>(it.first);
62 auto ret = physical_request_state->Initialize(
63 HalCameraMetadata::Clone(it.second.second.get()));
64 if (ret != OK) {
65 ALOGE("%s: Physical device: %u request state initialization failed!",
66 __FUNCTION__, it.first);
67 return ret;
68 }
69 physical_request_states_.emplace(it.first,
70 std::move(physical_request_state));
71 }
72 }
73 }
74
75 return logical_request_state_->Initialize(std::move(static_meta));
76 }
77
GetDefaultRequest(RequestTemplate type,std::unique_ptr<HalCameraMetadata> * default_settings)78 status_t EmulatedLogicalRequestState::GetDefaultRequest(
79 RequestTemplate type,
80 std::unique_ptr<HalCameraMetadata>* default_settings /*out*/) {
81 return logical_request_state_->GetDefaultRequest(type, default_settings);
82 };
83
UpdateActivePhysicalId(HalCameraMetadata * result_metadata,uint32_t device_id)84 void EmulatedLogicalRequestState::UpdateActivePhysicalId(
85 HalCameraMetadata* result_metadata, uint32_t device_id) {
86 if (result_metadata == nullptr) {
87 return;
88 }
89
90 auto device_id_str = std::to_string(device_id);
91 std::vector<uint8_t> result;
92 result.reserve(device_id_str.size() + 1);
93 result.insert(result.end(), device_id_str.begin(), device_id_str.end());
94 result.push_back('\0');
95
96 result_metadata->Set(ANDROID_LOGICAL_MULTI_CAMERA_ACTIVE_PHYSICAL_ID,
97 result.data(), result.size());
98 }
99
100 std::unique_ptr<HwlPipelineResult>
InitializeLogicalResult(uint32_t pipeline_id,uint32_t frame_number)101 EmulatedLogicalRequestState::InitializeLogicalResult(uint32_t pipeline_id,
102 uint32_t frame_number) {
103 auto ret = logical_request_state_->InitializeResult(pipeline_id, frame_number);
104 if (is_logical_device_) {
105 if ((physical_camera_output_ids_.get() != nullptr) &&
106 (!physical_camera_output_ids_->empty())) {
107 ret->physical_camera_results.reserve(physical_camera_output_ids_->size());
108 for (const auto& it : *physical_camera_output_ids_) {
109 ret->physical_camera_results[it] =
110 std::move(physical_request_states_[it]
111 ->InitializeResult(pipeline_id, frame_number)
112 ->result_metadata);
113
114 UpdateActivePhysicalId(ret->physical_camera_results[it].get(), it);
115 }
116 }
117
118 UpdateActivePhysicalId(ret->result_metadata.get(), current_physical_camera_);
119 }
120
121 return ret;
122 }
123
InitializeLogicalSettings(std::unique_ptr<HalCameraMetadata> request_settings,std::unique_ptr<std::set<uint32_t>> physical_camera_output_ids,EmulatedSensor::LogicalCameraSettings * logical_settings)124 status_t EmulatedLogicalRequestState::InitializeLogicalSettings(
125 std::unique_ptr<HalCameraMetadata> request_settings,
126 std::unique_ptr<std::set<uint32_t>> physical_camera_output_ids,
127 EmulatedSensor::LogicalCameraSettings* logical_settings /*out*/) {
128 if (logical_settings == nullptr) {
129 return BAD_VALUE;
130 }
131
132 // All logical and physical devices can potentially receive individual client
133 // requests (Currently this is not the case due to HWL API limitations).
134 // The emulated sensor can adapt its characteristics and apply most of them
135 // independently however the frame duration needs to be the same across all
136 // settings.
137 // Track the maximum frame duration and override this value at the end for all
138 // logical settings.
139 nsecs_t max_frame_duration = 0;
140 if (is_logical_device_) {
141 std::swap(physical_camera_output_ids_, physical_camera_output_ids);
142
143 for (const auto& physical_request_state : physical_request_states_) {
144 // All physical devices will receive requests and will keep
145 // updating their respective request state.
146 // However only physical devices referenced by client need to propagate
147 // and apply their settings.
148 EmulatedSensor::SensorSettings physical_sensor_settings;
149 auto ret = physical_request_state.second->InitializeSensorSettings(
150 HalCameraMetadata::Clone(request_settings.get()),
151 &physical_sensor_settings);
152 if (ret != OK) {
153 ALOGE(
154 "%s: Initialization of physical sensor settings for device id: %u "
155 "failed!",
156 __FUNCTION__, physical_request_state.first);
157 return ret;
158 }
159
160 if (physical_camera_output_ids_->find(physical_request_state.first) !=
161 physical_camera_output_ids_->end()) {
162 logical_settings->emplace(physical_request_state.first,
163 physical_sensor_settings);
164 if (max_frame_duration < physical_sensor_settings.exposure_time) {
165 max_frame_duration = physical_sensor_settings.exposure_time;
166 }
167 }
168 }
169 }
170
171 EmulatedSensor::SensorSettings sensor_settings;
172 auto ret = logical_request_state_->InitializeSensorSettings(
173 std::move(request_settings), &sensor_settings);
174 logical_settings->emplace(logical_camera_id_, sensor_settings);
175 if (max_frame_duration < sensor_settings.exposure_time) {
176 max_frame_duration = sensor_settings.exposure_time;
177 }
178
179 for (auto it : *logical_settings) {
180 it.second.frame_duration = max_frame_duration;
181 }
182
183 return ret;
184 }
185
186 std::unique_ptr<HalCameraMetadata>
AdaptLogicalCharacteristics(std::unique_ptr<HalCameraMetadata> logical_chars,PhysicalDeviceMapPtr physical_devices)187 EmulatedLogicalRequestState::AdaptLogicalCharacteristics(
188 std::unique_ptr<HalCameraMetadata> logical_chars,
189 PhysicalDeviceMapPtr physical_devices) {
190 if ((logical_chars.get() == nullptr) || (physical_devices.get() == nullptr)) {
191 return nullptr;
192 }
193
194 // Update 'android.logicalMultiCamera.physicalIds' according to the newly
195 // assigned physical ids.
196 // Additionally if possible try to emulate a logical camera device backed by
197 // physical devices with different focal lengths. Usually real logical
198 // cameras like that will have device specific logic to switch between
199 // physical sensors. Unfortunately we cannot infer this behavior using only
200 // static camera characteristics. Use a simplistic approach of inferring
201 // physical camera based on zoom ratio.
202 std::vector<ZoomRatioPhysicalCameraInfo> zoom_ratio_physical_camera_info =
203 GetZoomRatioPhysicalCameraInfo(logical_chars.get(),
204 physical_devices.get());
205
206 std::vector<uint8_t> physical_ids;
207 for (const auto& physical_device : *physical_devices) {
208 auto physical_id = std::to_string(physical_device.first);
209 physical_ids.insert(physical_ids.end(), physical_id.begin(),
210 physical_id.end());
211 physical_ids.push_back('\0');
212 }
213
214 if (zoom_ratio_physical_camera_info.size() > 1) {
215 float zoom_range[2];
216 zoom_range[0] = zoom_ratio_physical_camera_info[0].min_zoom_ratio;
217 zoom_range[1] =
218 zoom_ratio_physical_camera_info[zoom_ratio_physical_camera_info.size() - 1]
219 .max_zoom_ratio;
220 logical_chars->Set(ANDROID_CONTROL_ZOOM_RATIO_RANGE, zoom_range, 2);
221
222 logical_chars->Set(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
223 &zoom_range[1], 1);
224
225 logical_chars->Set(ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS,
226 physical_ids.data(), physical_ids.size());
227
228 // Possibly needs to be removed at some later point:
229 int32_t default_physical_id = physical_devices->begin()->first;
230 logical_chars->Set(google_camera_hal::kLogicalCamDefaultPhysicalId,
231 &default_physical_id, 1);
232
233 camera_metadata_ro_entry entry;
234 logical_chars->Get(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, &entry);
235 std::set<int32_t> keys(entry.data.i32, entry.data.i32 + entry.count);
236 keys.emplace(ANDROID_LOGICAL_MULTI_CAMERA_ACTIVE_PHYSICAL_ID);
237 std::vector<int32_t> keys_buffer(keys.begin(), keys.end());
238 logical_chars->Set(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
239 keys_buffer.data(), keys_buffer.size());
240
241 keys.clear();
242 keys_buffer.clear();
243 logical_chars->Get(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, &entry);
244 keys.insert(entry.data.i32, entry.data.i32 + entry.count);
245 // Due to API limitations we currently don't support individual physical requests
246 logical_chars->Erase(ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
247 keys.erase(ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
248 keys.emplace(ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS);
249 keys_buffer.insert(keys_buffer.end(), keys.begin(), keys.end());
250 logical_chars->Set(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
251 keys_buffer.data(), keys_buffer.size());
252 } else {
253 ALOGW(
254 "%s: The logical camera doesn't support combined zoom ratio ranges. "
255 "Emulation "
256 "could be"
257 " very limited in this case!",
258 __FUNCTION__);
259 }
260
261 return logical_chars;
262 }
263
UpdateRequestForDynamicStreams(HwlPipelineRequest * request,const std::vector<EmulatedPipeline> & pipelines,const DynamicStreamIdMapType & dynamic_stream_id_map,bool use_default_physical_camera)264 status_t EmulatedLogicalRequestState::UpdateRequestForDynamicStreams(
265 HwlPipelineRequest* request, const std::vector<EmulatedPipeline>& pipelines,
266 const DynamicStreamIdMapType& dynamic_stream_id_map,
267 bool use_default_physical_camera) {
268 if (request == nullptr) {
269 ALOGE("%s: Request must not be null!", __FUNCTION__);
270 return BAD_VALUE;
271 }
272
273 uint32_t pipeline_id = request->pipeline_id;
274 if (pipeline_id >= pipelines.size()) {
275 ALOGE("%s: Invalid pipeline id %d", __FUNCTION__, pipeline_id);
276 return BAD_VALUE;
277 }
278
279 // Only logical camera support dynamic size streams.
280 if (!is_logical_device_) return OK;
281
282 if (request->settings != nullptr) {
283 camera_metadata_ro_entry entry;
284 auto stat = request->settings->Get(ANDROID_CONTROL_ZOOM_RATIO, &entry);
285 if (stat != OK || entry.count != 1) {
286 ALOGW("%s: Zoom ratio absent from request, re-using older value!",
287 __FUNCTION__);
288 return BAD_VALUE;
289 }
290 if (!use_default_physical_camera) {
291 float zoom_ratio = entry.data.f[0];
292 for (const auto& one_range : zoom_ratio_physical_camera_info_) {
293 if (zoom_ratio >= one_range.min_zoom_ratio &&
294 zoom_ratio <= one_range.max_zoom_ratio) {
295 current_physical_camera_ = one_range.physical_camera_id;
296 break;
297 }
298 }
299 }
300 }
301
302 const auto& current_pipeline = pipelines[pipeline_id];
303 for (auto& output_buffer : request->output_buffers) {
304 auto& current_stream = current_pipeline.streams.at(output_buffer.stream_id);
305 if (current_stream.group_id == -1) continue;
306
307 const auto& stream_ids_for_camera =
308 dynamic_stream_id_map.find(current_physical_camera_);
309 if (stream_ids_for_camera == dynamic_stream_id_map.end()) {
310 ALOGW(
311 "%s: Failed to find physical camera id %d in dynamic stream id map!",
312 __FUNCTION__, current_physical_camera_);
313 continue;
314 }
315 const auto& stream_id =
316 stream_ids_for_camera->second.find(current_stream.group_id);
317 if (stream_id == stream_ids_for_camera->second.end()) {
318 ALOGW(
319 "%s: Failed to find group id %d in dynamic stream id map for camera "
320 "%d",
321 __FUNCTION__, current_stream.group_id, current_physical_camera_);
322 continue;
323 }
324
325 output_buffer.stream_id = stream_id->second;
326 }
327 return OK;
328 }
329
330 std::vector<ZoomRatioPhysicalCameraInfo>
GetZoomRatioPhysicalCameraInfo(const HalCameraMetadata * logical_chars,const PhysicalDeviceMap * physical_devices)331 EmulatedLogicalRequestState::GetZoomRatioPhysicalCameraInfo(
332 const HalCameraMetadata* logical_chars,
333 const PhysicalDeviceMap* physical_devices) {
334 std::vector<ZoomRatioPhysicalCameraInfo> zoom_ratio_physical_camera_info;
335 if ((logical_chars == nullptr) || (physical_devices == nullptr)) {
336 return zoom_ratio_physical_camera_info;
337 }
338
339 // Get the logical camera's focal length and sensor size
340 camera_metadata_ro_entry_t entry;
341 auto ret =
342 logical_chars->Get(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS, &entry);
343 if ((ret != OK) || (entry.count == 0)) {
344 return zoom_ratio_physical_camera_info;
345 }
346 float logical_focal_length = entry.data.f[0];
347 ret = logical_chars->Get(ANDROID_SENSOR_INFO_PHYSICAL_SIZE, &entry);
348 if ((ret != OK) || (entry.count == 0)) {
349 return zoom_ratio_physical_camera_info;
350 }
351 float logical_sensor_width = entry.data.f[0];
352
353 // Derive the zoom ratio boundary values for each physical camera id, based on
354 // focal lengths and camera sensor physical size.
355 for (const auto& physical_device : *physical_devices) {
356 ret = physical_device.second.second->Get(
357 ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS, &entry);
358 if ((ret == OK) && (entry.count > 0)) {
359 float focal_length = entry.data.f[0];
360 ret = physical_device.second.second->Get(
361 ANDROID_SENSOR_INFO_PHYSICAL_SIZE, &entry);
362 if ((ret == OK) && (entry.count > 0)) {
363 float sensor_width = entry.data.f[0];
364 ret = physical_device.second.second->Get(
365 ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM, &entry);
366 if ((ret == OK) && (entry.count > 0)) {
367 float max_digital_zoom = entry.data.f[0];
368 // focal length of ultrawide lens
369 float min_zoom_ratio = focal_length * logical_sensor_width /
370 (logical_focal_length * sensor_width);
371 float max_zoom_ratio = max_digital_zoom * min_zoom_ratio;
372 zoom_ratio_physical_camera_info.push_back(
373 {focal_length, min_zoom_ratio, max_zoom_ratio,
374 physical_device.first});
375 }
376 }
377 }
378 }
379
380 // Sort the mapping by ascending focal length
381 std::sort(zoom_ratio_physical_camera_info.begin(),
382 zoom_ratio_physical_camera_info.end(),
383 [](const ZoomRatioPhysicalCameraInfo& a,
384 const ZoomRatioPhysicalCameraInfo& b) {
385 return a.focal_length < b.focal_length;
386 });
387
388 // Modify the zoom ratio range for each focal length so that they don't
389 // overlap
390 for (size_t i = 0; i < zoom_ratio_physical_camera_info.size() - 1; i++) {
391 auto& current = zoom_ratio_physical_camera_info[i];
392 auto& next = zoom_ratio_physical_camera_info[i + 1];
393 if (current.max_zoom_ratio > next.min_zoom_ratio) {
394 current.max_zoom_ratio = next.min_zoom_ratio;
395 }
396 }
397
398 return zoom_ratio_physical_camera_info;
399 }
400
401 } // namespace android
402