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 "EmulatedRequestProcessor"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19
20 #include "EmulatedRequestProcessor.h"
21
22 #include <HandleImporter.h>
23 #include <hardware/gralloc.h>
24 #include <log/log.h>
25 #include <sync/sync.h>
26 #include <utils/Timers.h>
27 #include <utils/Trace.h>
28
29 #include <memory>
30
31 #include "GrallocSensorBuffer.h"
32
33 namespace android {
34
35 using ::android::frameworks::sensorservice::V1_0::ISensorManager;
36 using ::android::frameworks::sensorservice::V1_0::Result;
37 using android::hardware::camera::common::V1_0::helper::HandleImporter;
38 using ::android::hardware::sensors::V1_0::SensorInfo;
39 using ::android::hardware::sensors::V1_0::SensorType;
40 using google_camera_hal::ErrorCode;
41 using google_camera_hal::HwlPipelineResult;
42 using google_camera_hal::MessageType;
43 using google_camera_hal::NotifyMessage;
44
EmulatedRequestProcessor(uint32_t camera_id,sp<EmulatedSensor> sensor,const HwlSessionCallback & session_callback)45 EmulatedRequestProcessor::EmulatedRequestProcessor(
46 uint32_t camera_id, sp<EmulatedSensor> sensor,
47 const HwlSessionCallback& session_callback)
48 : camera_id_(camera_id),
49 sensor_(sensor),
50 session_callback_(session_callback),
51 request_state_(std::make_unique<EmulatedLogicalRequestState>(camera_id)) {
52 ATRACE_CALL();
53 request_thread_ = std::thread([this] { this->RequestProcessorLoop(); });
54 importer_ = std::make_shared<HandleImporter>();
55 }
56
~EmulatedRequestProcessor()57 EmulatedRequestProcessor::~EmulatedRequestProcessor() {
58 ATRACE_CALL();
59 processor_done_ = true;
60 request_thread_.join();
61
62 auto ret = sensor_->ShutDown();
63 if (ret != OK) {
64 ALOGE("%s: Failed during sensor shutdown %s (%d)", __FUNCTION__,
65 strerror(-ret), ret);
66 }
67
68 if (sensor_event_queue_.get() != nullptr) {
69 sensor_event_queue_->disableSensor(sensor_handle_);
70 sensor_event_queue_.clear();
71 sensor_event_queue_ = nullptr;
72 }
73 }
74
ProcessPipelineRequests(uint32_t frame_number,std::vector<HwlPipelineRequest> & requests,const std::vector<EmulatedPipeline> & pipelines,const DynamicStreamIdMapType & dynamic_stream_id_map,bool use_default_physical_camera)75 status_t EmulatedRequestProcessor::ProcessPipelineRequests(
76 uint32_t frame_number, std::vector<HwlPipelineRequest>& requests,
77 const std::vector<EmulatedPipeline>& pipelines,
78 const DynamicStreamIdMapType& dynamic_stream_id_map,
79 bool use_default_physical_camera) {
80 ATRACE_CALL();
81 status_t res = OK;
82
83 std::unique_lock<std::mutex> lock(process_mutex_);
84
85 for (auto& request : requests) {
86 if (request.pipeline_id >= pipelines.size()) {
87 ALOGE("%s: Pipeline request with invalid pipeline id: %u", __FUNCTION__,
88 request.pipeline_id);
89 return BAD_VALUE;
90 }
91
92 while (pending_requests_.size() > EmulatedSensor::kPipelineDepth) {
93 auto result = request_condition_.wait_for(
94 lock, std::chrono::nanoseconds(
95 EmulatedSensor::kSupportedFrameDurationRange[1]));
96 if (result == std::cv_status::timeout) {
97 ALOGE("%s: Timed out waiting for a pending request slot", __FUNCTION__);
98 return TIMED_OUT;
99 }
100 }
101
102 res = request_state_->UpdateRequestForDynamicStreams(
103 &request, pipelines, dynamic_stream_id_map, use_default_physical_camera);
104 if (res != OK) {
105 ALOGE("%s: Failed to update request for dynamic streams: %s(%d)",
106 __FUNCTION__, strerror(-res), res);
107 return res;
108 }
109
110 auto output_buffers = CreateSensorBuffers(
111 frame_number, request.output_buffers,
112 pipelines[request.pipeline_id].streams, request.pipeline_id,
113 pipelines[request.pipeline_id].cb, /*override_width*/ 0,
114 /*override_height*/ 0);
115 if (output_buffers == nullptr) {
116 return NO_MEMORY;
117 }
118
119 auto input_buffers = CreateSensorBuffers(
120 frame_number, request.input_buffers,
121 pipelines[request.pipeline_id].streams, request.pipeline_id,
122 pipelines[request.pipeline_id].cb, request.input_width,
123 request.input_height);
124
125 // Check if there are any settings that need to be overridden.
126 camera_metadata_ro_entry_t entry;
127 if (request.settings.get() != nullptr) {
128 auto ret = request.settings.get()->Get(ANDROID_CONTROL_SETTINGS_OVERRIDE,
129 &entry);
130 if ((ret == OK) && (entry.count == 1)) {
131 std::unique_ptr<HalCameraMetadata> override_setting =
132 HalCameraMetadata::Clone(request.settings.get());
133 override_settings_.push({.settings = std::move(override_setting),
134 .frame_number = frame_number});
135 }
136 } else {
137 override_settings_.push(
138 {.settings = nullptr, .frame_number = frame_number});
139 }
140 pending_requests_.push(
141 {.frame_number = frame_number,
142 .pipeline_id = request.pipeline_id,
143 .callback = pipelines[request.pipeline_id].cb,
144 .settings = HalCameraMetadata::Clone(request.settings.get()),
145 .input_buffers = std::move(input_buffers),
146 .output_buffers = std::move(output_buffers)});
147 }
148
149 return OK;
150 }
151
CreateSensorBuffers(uint32_t frame_number,const std::vector<StreamBuffer> & buffers,const std::unordered_map<uint32_t,EmulatedStream> & streams,uint32_t pipeline_id,HwlPipelineCallback cb,int32_t override_width,int32_t override_height)152 std::unique_ptr<Buffers> EmulatedRequestProcessor::CreateSensorBuffers(
153 uint32_t frame_number, const std::vector<StreamBuffer>& buffers,
154 const std::unordered_map<uint32_t, EmulatedStream>& streams,
155 uint32_t pipeline_id, HwlPipelineCallback cb, int32_t override_width,
156 int32_t override_height) {
157 if (buffers.empty()) {
158 return nullptr;
159 }
160
161 std::vector<StreamBuffer> requested_buffers;
162 for (auto& buffer : buffers) {
163 if (buffer.buffer != nullptr) {
164 requested_buffers.push_back(buffer);
165 continue;
166 }
167
168 if (session_callback_.request_stream_buffers != nullptr) {
169 std::vector<StreamBuffer> one_requested_buffer;
170 status_t res = session_callback_.request_stream_buffers(
171 buffer.stream_id, 1, &one_requested_buffer, frame_number);
172 if (res != OK) {
173 ALOGE("%s: request_stream_buffers failed: %s(%d)", __FUNCTION__,
174 strerror(-res), res);
175 continue;
176 }
177 if (one_requested_buffer.size() != 1 ||
178 one_requested_buffer[0].buffer == nullptr) {
179 ALOGE("%s: request_stream_buffers failed to return a valid buffer",
180 __FUNCTION__);
181 continue;
182 }
183 requested_buffers.push_back(one_requested_buffer[0]);
184 }
185 }
186
187 if (requested_buffers.size() < buffers.size()) {
188 ALOGE(
189 "%s: Failed to acquire all sensor buffers: %zu acquired, %zu requested",
190 __FUNCTION__, requested_buffers.size(), buffers.size());
191 // This only happens for HAL buffer manager use case.
192 if (session_callback_.return_stream_buffers != nullptr) {
193 session_callback_.return_stream_buffers(requested_buffers);
194 }
195 requested_buffers.clear();
196 }
197
198 auto sensor_buffers = std::make_unique<Buffers>();
199 sensor_buffers->reserve(requested_buffers.size());
200 for (auto& buffer : requested_buffers) {
201 auto sensor_buffer = CreateSensorBuffer(
202 frame_number, streams.at(buffer.stream_id), pipeline_id, cb, buffer,
203 override_width, override_height);
204 if (sensor_buffer.get() != nullptr) {
205 sensor_buffers->push_back(std::move(sensor_buffer));
206 }
207 }
208
209 return sensor_buffers;
210 }
211
NotifyFailedRequest(const PendingRequest & request)212 void EmulatedRequestProcessor::NotifyFailedRequest(const PendingRequest& request) {
213 if (request.output_buffers != nullptr) {
214 // Mark all output buffers for this request in order not to send
215 // ERROR_BUFFER for them.
216 for (auto& output_buffer : *(request.output_buffers)) {
217 output_buffer->is_failed_request = true;
218 }
219 }
220
221 NotifyMessage msg = {
222 .type = MessageType::kError,
223 .message.error = {.frame_number = request.frame_number,
224 .error_stream_id = -1,
225 .error_code = ErrorCode::kErrorRequest}};
226 request.callback.notify(request.pipeline_id, msg);
227 }
228
Flush()229 status_t EmulatedRequestProcessor::Flush() {
230 std::lock_guard<std::mutex> lock(process_mutex_);
231 // First flush in-flight requests
232 auto ret = sensor_->Flush();
233
234 // Then the rest of the pending requests
235 while (!pending_requests_.empty()) {
236 const auto& request = pending_requests_.front();
237 NotifyFailedRequest(request);
238 pending_requests_.pop();
239 }
240
241 return ret;
242 }
243
GetBufferSizeAndStride(const EmulatedStream & stream,buffer_handle_t buffer,uint32_t * size,uint32_t * stride)244 status_t EmulatedRequestProcessor::GetBufferSizeAndStride(
245 const EmulatedStream& stream, buffer_handle_t buffer,
246 uint32_t* size /*out*/, uint32_t* stride /*out*/) {
247 if (size == nullptr) {
248 return BAD_VALUE;
249 }
250
251 switch (stream.override_format) {
252 case HAL_PIXEL_FORMAT_RGB_888:
253 *stride = stream.width * 3;
254 *size = (*stride) * stream.height;
255 break;
256 case HAL_PIXEL_FORMAT_RGBA_8888:
257 *stride = stream.width * 4;
258 *size = (*stride) * stream.height;
259 break;
260 case HAL_PIXEL_FORMAT_Y16:
261 if (stream.override_data_space == HAL_DATASPACE_DEPTH) {
262 *stride = AlignTo(AlignTo(stream.width, 2) * 2, 16);
263 *size = (*stride) * AlignTo(stream.height, 2);
264 } else {
265 return BAD_VALUE;
266 }
267 break;
268 case HAL_PIXEL_FORMAT_BLOB:
269 if (stream.override_data_space == HAL_DATASPACE_V0_JFIF) {
270 *size = stream.buffer_size;
271 *stride = *size;
272 } else {
273 return BAD_VALUE;
274 }
275 break;
276 case HAL_PIXEL_FORMAT_RAW16:
277 if (importer_->getMonoPlanarStrideBytes(buffer, stride) != NO_ERROR) {
278 *stride = stream.width * 2;
279 }
280 *size = (*stride) * stream.height;
281 break;
282 default:
283 return BAD_VALUE;
284 }
285
286 return OK;
287 }
288
LockSensorBuffer(const EmulatedStream & stream,buffer_handle_t buffer,int32_t width,int32_t height,SensorBuffer * sensor_buffer)289 status_t EmulatedRequestProcessor::LockSensorBuffer(
290 const EmulatedStream& stream, buffer_handle_t buffer, int32_t width,
291 int32_t height, SensorBuffer* sensor_buffer /*out*/) {
292 if (sensor_buffer == nullptr) {
293 return BAD_VALUE;
294 }
295
296 auto usage = GRALLOC_USAGE_SW_WRITE_OFTEN;
297 bool isYUV_420_888 = stream.override_format == HAL_PIXEL_FORMAT_YCBCR_420_888;
298 bool isP010 = static_cast<android_pixel_format_v1_1_t>(
299 stream.override_format) == HAL_PIXEL_FORMAT_YCBCR_P010;
300 if ((isYUV_420_888) || (isP010)) {
301 android::Rect map_rect = {0, 0, width, height};
302 auto yuv_layout = importer_->lockYCbCr(buffer, usage, map_rect);
303 if ((yuv_layout.y != nullptr) && (yuv_layout.cb != nullptr) &&
304 (yuv_layout.cr != nullptr)) {
305 sensor_buffer->plane.img_y_crcb.img_y =
306 static_cast<uint8_t*>(yuv_layout.y);
307 sensor_buffer->plane.img_y_crcb.img_cb =
308 static_cast<uint8_t*>(yuv_layout.cb);
309 sensor_buffer->plane.img_y_crcb.img_cr =
310 static_cast<uint8_t*>(yuv_layout.cr);
311 sensor_buffer->plane.img_y_crcb.y_stride = yuv_layout.ystride;
312 sensor_buffer->plane.img_y_crcb.cbcr_stride = yuv_layout.cstride;
313 sensor_buffer->plane.img_y_crcb.cbcr_step = yuv_layout.chroma_step;
314 if (isYUV_420_888 && (yuv_layout.chroma_step == 2) &&
315 std::abs(sensor_buffer->plane.img_y_crcb.img_cb -
316 sensor_buffer->plane.img_y_crcb.img_cr) != 1) {
317 ALOGE(
318 "%s: Unsupported YUV layout, chroma step: %zu U/V plane delta: %u",
319 __FUNCTION__, yuv_layout.chroma_step,
320 static_cast<unsigned>(
321 std::abs(sensor_buffer->plane.img_y_crcb.img_cb -
322 sensor_buffer->plane.img_y_crcb.img_cr)));
323 return BAD_VALUE;
324 }
325 sensor_buffer->plane.img_y_crcb.bytesPerPixel = isP010 ? 2 : 1;
326 } else {
327 ALOGE("%s: Failed to lock output buffer for stream id %d !", __FUNCTION__,
328 stream.id);
329 return BAD_VALUE;
330 }
331 } else {
332 uint32_t buffer_size = 0, stride = 0;
333 auto ret = GetBufferSizeAndStride(stream, buffer, &buffer_size, &stride);
334 if (ret != OK) {
335 ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__,
336 stream.override_format);
337 return BAD_VALUE;
338 }
339 if (stream.override_format == HAL_PIXEL_FORMAT_BLOB) {
340 sensor_buffer->plane.img.img =
341 static_cast<uint8_t*>(importer_->lock(buffer, usage, buffer_size));
342 } else {
343 android::Rect region{0, 0, width, height};
344 sensor_buffer->plane.img.img =
345 static_cast<uint8_t*>(importer_->lock(buffer, usage, region));
346 }
347 if (sensor_buffer->plane.img.img == nullptr) {
348 ALOGE("%s: Failed to lock output buffer!", __FUNCTION__);
349 return BAD_VALUE;
350 }
351 sensor_buffer->plane.img.stride_in_bytes = stride;
352 sensor_buffer->plane.img.buffer_size = buffer_size;
353 }
354
355 return OK;
356 }
357
CreateSensorBuffer(uint32_t frame_number,const EmulatedStream & emulated_stream,uint32_t pipeline_id,HwlPipelineCallback callback,StreamBuffer stream_buffer,int32_t override_width,int32_t override_height)358 std::unique_ptr<SensorBuffer> EmulatedRequestProcessor::CreateSensorBuffer(
359 uint32_t frame_number, const EmulatedStream& emulated_stream,
360 uint32_t pipeline_id, HwlPipelineCallback callback,
361 StreamBuffer stream_buffer, int32_t override_width,
362 int32_t override_height) {
363 auto buffer = std::make_unique<GrallocSensorBuffer>(importer_);
364
365 auto stream = emulated_stream;
366 // Make sure input stream formats are correctly mapped here
367 if (stream.is_input) {
368 stream.override_format = EmulatedSensor::OverrideFormat(
369 stream.override_format,
370 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
371 }
372 if (override_width > 0 && override_height > 0) {
373 buffer->width = override_width;
374 buffer->height = override_height;
375 } else {
376 buffer->width = stream.width;
377 buffer->height = stream.height;
378 }
379 buffer->format = static_cast<PixelFormat>(stream.override_format);
380 buffer->dataSpace = stream.override_data_space;
381 buffer->color_space = stream.color_space;
382 buffer->use_case = stream.use_case;
383 buffer->stream_buffer = stream_buffer;
384 buffer->pipeline_id = pipeline_id;
385 buffer->callback = callback;
386 buffer->frame_number = frame_number;
387 buffer->camera_id = emulated_stream.is_physical_camera_stream
388 ? emulated_stream.physical_camera_id
389 : camera_id_;
390 buffer->is_input = stream.is_input;
391 // In case buffer processing is successful, flip this flag accordingly
392 buffer->stream_buffer.status = BufferStatus::kError;
393
394 if (buffer->stream_buffer.buffer != nullptr) {
395 auto ret = LockSensorBuffer(stream, buffer->stream_buffer.buffer,
396 buffer->width, buffer->height, buffer.get());
397 if (ret != OK) {
398 buffer->is_failed_request = true;
399 buffer = nullptr;
400 }
401 }
402
403 if ((buffer.get() != nullptr) && (stream_buffer.acquire_fence != nullptr)) {
404 auto fence_status = importer_->importFence(stream_buffer.acquire_fence,
405 buffer->acquire_fence_fd);
406 if (!fence_status) {
407 ALOGE("%s: Failed importing acquire fence!", __FUNCTION__);
408 buffer->is_failed_request = true;
409 buffer = nullptr;
410 }
411 }
412
413 return buffer;
414 }
415
AcquireBuffers(Buffers * buffers)416 std::unique_ptr<Buffers> EmulatedRequestProcessor::AcquireBuffers(
417 Buffers* buffers) {
418 if ((buffers == nullptr) || (buffers->empty())) {
419 return nullptr;
420 }
421
422 auto acquired_buffers = std::make_unique<Buffers>();
423 acquired_buffers->reserve(buffers->size());
424 auto output_buffer = buffers->begin();
425 while (output_buffer != buffers->end()) {
426 status_t ret = OK;
427 if ((*output_buffer)->acquire_fence_fd >= 0) {
428 ret = sync_wait((*output_buffer)->acquire_fence_fd,
429 ns2ms(EmulatedSensor::kSupportedFrameDurationRange[1]));
430 if (ret != OK) {
431 ALOGE("%s: Fence sync failed: %s, (%d)", __FUNCTION__, strerror(-ret),
432 ret);
433 }
434 }
435
436 if (ret == OK) {
437 acquired_buffers->push_back(std::move(*output_buffer));
438 }
439
440 output_buffer = buffers->erase(output_buffer);
441 }
442
443 return acquired_buffers;
444 }
445
RequestProcessorLoop()446 void EmulatedRequestProcessor::RequestProcessorLoop() {
447 ATRACE_CALL();
448
449 bool vsync_status_ = true;
450 while (!processor_done_ && vsync_status_) {
451 {
452 std::lock_guard<std::mutex> lock(process_mutex_);
453 if (!pending_requests_.empty()) {
454 status_t ret;
455 const auto& request = pending_requests_.front();
456 auto frame_number = request.frame_number;
457 auto notify_callback = request.callback;
458 auto pipeline_id = request.pipeline_id;
459
460 auto output_buffers = AcquireBuffers(request.output_buffers.get());
461 auto input_buffers = AcquireBuffers(request.input_buffers.get());
462 if ((output_buffers != nullptr) && !output_buffers->empty()) {
463 std::unique_ptr<EmulatedSensor::LogicalCameraSettings> logical_settings =
464 std::make_unique<EmulatedSensor::LogicalCameraSettings>();
465
466 std::unique_ptr<std::set<uint32_t>> physical_camera_output_ids =
467 std::make_unique<std::set<uint32_t>>();
468 for (const auto& it : *output_buffers) {
469 if (it->camera_id != camera_id_) {
470 physical_camera_output_ids->emplace(it->camera_id);
471 }
472 }
473
474 // Repeating requests usually include valid settings only during the
475 // initial call. Afterwards an invalid settings pointer means that
476 // there are no changes in the parameters and Hal should re-use the
477 // last valid values.
478 // TODO: Add support for individual physical camera requests.
479 if (request.settings.get() != nullptr) {
480 auto override_frame_number =
481 ApplyOverrideSettings(frame_number, request.settings);
482 ret = request_state_->InitializeLogicalSettings(
483 HalCameraMetadata::Clone(request.settings.get()),
484 std::move(physical_camera_output_ids), override_frame_number,
485 logical_settings.get());
486 last_settings_ = HalCameraMetadata::Clone(request.settings.get());
487 } else {
488 auto override_frame_number =
489 ApplyOverrideSettings(frame_number, last_settings_);
490 ret = request_state_->InitializeLogicalSettings(
491 HalCameraMetadata::Clone(last_settings_.get()),
492 std::move(physical_camera_output_ids), override_frame_number,
493 logical_settings.get());
494 }
495
496 if (ret == OK) {
497 auto partial_result = request_state_->InitializeLogicalResult(
498 pipeline_id, frame_number,
499 /*partial result*/ true);
500 auto result = request_state_->InitializeLogicalResult(
501 pipeline_id, frame_number,
502 /*partial result*/ false);
503 // The screen rotation will be the same for all logical and physical devices
504 uint32_t screen_rotation = screen_rotation_;
505 for (auto it = logical_settings->begin();
506 it != logical_settings->end(); it++) {
507 it->second.screen_rotation = screen_rotation;
508 }
509
510 sensor_->SetCurrentRequest(
511 std::move(logical_settings), std::move(result),
512 std::move(partial_result), std::move(input_buffers),
513 std::move(output_buffers));
514 } else {
515 NotifyMessage msg{.type = MessageType::kError,
516 .message.error = {
517 .frame_number = frame_number,
518 .error_stream_id = -1,
519 .error_code = ErrorCode::kErrorResult,
520 }};
521
522 notify_callback.notify(pipeline_id, msg);
523 }
524 } else {
525 // No further processing is needed, just fail the result which will
526 // complete this request.
527 NotifyMessage msg{.type = MessageType::kError,
528 .message.error = {
529 .frame_number = frame_number,
530 .error_stream_id = -1,
531 .error_code = ErrorCode::kErrorResult,
532 }};
533
534 notify_callback.notify(pipeline_id, msg);
535 }
536
537 pending_requests_.pop();
538 request_condition_.notify_one();
539 }
540 }
541
542 vsync_status_ =
543 sensor_->WaitForVSync(EmulatedSensor::kSupportedFrameDurationRange[1]);
544 }
545 }
546
Initialize(std::unique_ptr<EmulatedCameraDeviceInfo> device_info,PhysicalDeviceMapPtr physical_devices)547 status_t EmulatedRequestProcessor::Initialize(
548 std::unique_ptr<EmulatedCameraDeviceInfo> device_info,
549 PhysicalDeviceMapPtr physical_devices) {
550 std::lock_guard<std::mutex> lock(process_mutex_);
551 return request_state_->Initialize(std::move(device_info),
552 std::move(physical_devices));
553 }
554
SetSessionCallback(const HwlSessionCallback & hwl_session_callback)555 void EmulatedRequestProcessor::SetSessionCallback(
556 const HwlSessionCallback& hwl_session_callback) {
557 std::lock_guard<std::mutex> lock(process_mutex_);
558 session_callback_ = hwl_session_callback;
559 }
560
GetDefaultRequest(RequestTemplate type,std::unique_ptr<HalCameraMetadata> * default_settings)561 status_t EmulatedRequestProcessor::GetDefaultRequest(
562 RequestTemplate type, std::unique_ptr<HalCameraMetadata>* default_settings) {
563 std::lock_guard<std::mutex> lock(process_mutex_);
564 return request_state_->GetDefaultRequest(type, default_settings);
565 }
566
ApplyOverrideSettings(uint32_t frame_number,const std::unique_ptr<HalCameraMetadata> & request_settings)567 uint32_t EmulatedRequestProcessor::ApplyOverrideSettings(
568 uint32_t frame_number,
569 const std::unique_ptr<HalCameraMetadata>& request_settings) {
570 while (!override_settings_.empty() && request_settings.get() != nullptr) {
571 auto override_frame_number = override_settings_.front().frame_number;
572 bool repeatingOverride = (override_settings_.front().settings == nullptr);
573 const auto& override_setting = repeatingOverride
574 ? last_override_settings_
575 : override_settings_.front().settings;
576
577 camera_metadata_ro_entry_t entry;
578 status_t ret =
579 override_setting->Get(ANDROID_CONTROL_SETTINGS_OVERRIDE, &entry);
580 bool overriding = false;
581 if ((ret == OK) && (entry.count == 1) &&
582 (entry.data.i32[0] == ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM)) {
583 ApplyOverrideZoom(override_setting, request_settings,
584 ANDROID_CONTROL_SETTINGS_OVERRIDE);
585 ApplyOverrideZoom(override_setting, request_settings,
586 ANDROID_CONTROL_ZOOM_RATIO);
587 ApplyOverrideZoom(override_setting, request_settings,
588 ANDROID_SCALER_CROP_REGION);
589 ApplyOverrideZoom(override_setting, request_settings,
590 ANDROID_CONTROL_AE_REGIONS);
591 ApplyOverrideZoom(override_setting, request_settings,
592 ANDROID_CONTROL_AWB_REGIONS);
593 ApplyOverrideZoom(override_setting, request_settings,
594 ANDROID_CONTROL_AF_REGIONS);
595 overriding = true;
596 }
597 if (!repeatingOverride) {
598 last_override_settings_ = HalCameraMetadata::Clone(override_setting.get());
599 }
600
601 override_settings_.pop();
602 // If there are multiple queued override settings, skip until the speed-up
603 // is at least 2 frames.
604 if (override_frame_number - frame_number >= kZoomSpeedup) {
605 // If the request's settings override isn't ON, do not return
606 // override_frame_number. Return 0 to indicate there is no
607 // override happening.
608 return overriding ? override_frame_number : 0;
609 }
610 }
611 return 0;
612 }
613
ApplyOverrideZoom(const std::unique_ptr<HalCameraMetadata> & override_setting,const std::unique_ptr<HalCameraMetadata> & request_settings,camera_metadata_tag tag)614 void EmulatedRequestProcessor::ApplyOverrideZoom(
615 const std::unique_ptr<HalCameraMetadata>& override_setting,
616 const std::unique_ptr<HalCameraMetadata>& request_settings,
617 camera_metadata_tag tag) {
618 status_t ret;
619 camera_metadata_ro_entry_t entry;
620 ret = override_setting->Get(tag, &entry);
621 if (ret == OK) {
622 if (entry.type == TYPE_INT32) {
623 request_settings->Set(tag, entry.data.i32, entry.count);
624 } else if (entry.type == TYPE_FLOAT) {
625 request_settings->Set(tag, entry.data.f, entry.count);
626 } else {
627 ALOGE("%s: Unsupported override key %d", __FUNCTION__, tag);
628 }
629 } else {
630 auto missing_tag = get_camera_metadata_tag_name(tag);
631 ALOGE("%s: %s needs to be specified for overriding zoom", __func__,
632 missing_tag);
633 }
634 }
635
onEvent(const Event & e)636 Return<void> EmulatedRequestProcessor::SensorHandler::onEvent(const Event& e) {
637 auto processor = processor_.lock();
638 if (processor.get() == nullptr) {
639 return Void();
640 }
641
642 if (e.sensorType == SensorType::ACCELEROMETER) {
643 // Heuristic approach for deducing the screen
644 // rotation depending on the reported
645 // accelerometer readings. We switch
646 // the screen rotation when one of the
647 // x/y axis gets close enough to the earth
648 // acceleration.
649 const uint32_t earth_accel = 9; // Switch threshold [m/s^2]
650 uint32_t x_accel = e.u.vec3.x;
651 uint32_t y_accel = e.u.vec3.y;
652 uint32_t z_accel = abs(e.u.vec3.z);
653 if (z_accel == earth_accel) {
654 return Void();
655 }
656
657 if (x_accel == earth_accel) {
658 processor->screen_rotation_ = 270;
659 } else if (x_accel == -earth_accel) {
660 processor->screen_rotation_ = 90;
661 } else if (y_accel == -earth_accel) {
662 processor->screen_rotation_ = 180;
663 } else {
664 processor->screen_rotation_ = 0;
665 }
666 } else {
667 ALOGE("%s: unexpected event received type: %d", __func__, e.sensorType);
668 }
669 return Void();
670 }
671
InitializeSensorQueue(std::weak_ptr<EmulatedRequestProcessor> processor)672 void EmulatedRequestProcessor::InitializeSensorQueue(
673 std::weak_ptr<EmulatedRequestProcessor> processor) {
674 if (sensor_event_queue_.get() != nullptr) {
675 return;
676 }
677
678 sp<ISensorManager> manager = ISensorManager::getService();
679 if (manager == nullptr) {
680 ALOGE("%s: Cannot get ISensorManager", __func__);
681 } else {
682 bool sensor_found = false;
683 manager->getSensorList([&](const auto& list, auto result) {
684 if (result != Result::OK) {
685 ALOGE("%s: Failed to retrieve sensor list!", __func__);
686 } else {
687 for (const SensorInfo& it : list) {
688 if (it.type == SensorType::ACCELEROMETER) {
689 sensor_found = true;
690 sensor_handle_ = it.sensorHandle;
691 }
692 }
693 }
694 });
695 if (sensor_found) {
696 manager->createEventQueue(
697 new SensorHandler(processor), [&](const auto& q, auto result) {
698 if (result != Result::OK) {
699 ALOGE("%s: Cannot create event queue", __func__);
700 return;
701 }
702 sensor_event_queue_ = q;
703 });
704
705 if (sensor_event_queue_.get() != nullptr) {
706 auto res = sensor_event_queue_->enableSensor(
707 sensor_handle_,
708 ns2us(EmulatedSensor::kSupportedFrameDurationRange[0]),
709 0 /*maxBatchReportLatencyUs*/);
710 if (res.isOk()) {
711 } else {
712 ALOGE("%s: Failed to enable sensor", __func__);
713 }
714 } else {
715 ALOGE("%s: Failed to create event queue", __func__);
716 }
717 }
718 }
719 }
720
721 } // namespace android
722