1 /*
2 * Copyright (C) 2022 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 "GCH_AidlCameraDeviceSession"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20 #include "aidl_camera_device_session.h"
21
22 #include <android/binder_ibinder_platform.h>
23 #include <cutils/properties.h>
24 #include <cutils/trace.h>
25 #include <log/log.h>
26 #include <malloc.h>
27 #include <utils/Trace.h>
28
29 #include "aidl_profiler.h"
30 #include "aidl_utils.h"
31 namespace android {
32 namespace hardware {
33 namespace camera {
34 namespace device {
35 namespace implementation {
36
37 namespace aidl_utils = ::android::hardware::camera::implementation::aidl_utils;
38
39 using aidl::android::hardware::camera::common::Status;
40 using aidl::android::hardware::camera::device::BufferRequest;
41 using aidl::android::hardware::camera::device::BufferRequestStatus;
42 using aidl::android::hardware::camera::device::CaptureResult;
43 using aidl::android::hardware::camera::device::HalStream;
44 using aidl::android::hardware::camera::device::NotifyMsg;
45 using aidl::android::hardware::camera::device::StreamBuffer;
46 using aidl::android::hardware::camera::device::StreamBufferRet;
47 using aidl::android::hardware::camera::device::StreamBuffersVal;
48 using aidl_utils::ConvertToAidlReturn;
49 using ::android::hardware::thermal::V1_0::ThermalStatus;
50 using ::android::hardware::thermal::V1_0::ThermalStatusCode;
51 using ::android::hardware::thermal::V2_0::Temperature;
52 using ::android::hardware::thermal::V2_0::TemperatureType;
53
Create(const std::shared_ptr<ICameraDeviceCallback> & callback,std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session,std::shared_ptr<AidlProfiler> aidl_profiler)54 std::shared_ptr<AidlCameraDeviceSession> AidlCameraDeviceSession::Create(
55 const std::shared_ptr<ICameraDeviceCallback>& callback,
56 std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session,
57 std::shared_ptr<AidlProfiler> aidl_profiler) {
58 ATRACE_NAME("AidlCameraDeviceSession::Create");
59 auto session = ndk::SharedRefBase::make<AidlCameraDeviceSession>();
60 if (session == nullptr) {
61 ALOGE("%s: Cannot create a AidlCameraDeviceSession.", __FUNCTION__);
62 return nullptr;
63 }
64
65 status_t res =
66 session->Initialize(callback, std::move(device_session), aidl_profiler);
67 if (res != OK) {
68 ALOGE("%s: Initializing AidlCameraDeviceSession failed: %s(%d)",
69 __FUNCTION__, strerror(-res), res);
70 return nullptr;
71 }
72
73 return session;
74 }
75
~AidlCameraDeviceSession()76 AidlCameraDeviceSession::~AidlCameraDeviceSession() {
77 ATRACE_NAME("AidlCameraDeviceSession::~AidlCameraDeviceSession");
78 close();
79 // camera's closing, so flush any unused malloc pages
80 mallopt(M_PURGE, 0);
81 }
82
ProcessCaptureResult(std::unique_ptr<google_camera_hal::CaptureResult> hal_result)83 void AidlCameraDeviceSession::ProcessCaptureResult(
84 std::unique_ptr<google_camera_hal::CaptureResult> hal_result) {
85 std::shared_lock lock(aidl_device_callback_lock_);
86 if (aidl_device_callback_ == nullptr) {
87 ALOGE("%s: aidl_device_callback_ is nullptr", __FUNCTION__);
88 return;
89 }
90
91 {
92 std::lock_guard<std::mutex> pending_lock(pending_first_frame_buffers_mutex_);
93 if (!hal_result->output_buffers.empty() &&
94 num_pending_first_frame_buffers_ > 0 &&
95 first_request_frame_number_ == hal_result->frame_number) {
96 num_pending_first_frame_buffers_ -= hal_result->output_buffers.size();
97 if (num_pending_first_frame_buffers_ == 0) {
98 ALOGI("%s: First frame done", __FUNCTION__);
99 aidl_profiler_->FirstFrameEnd();
100 ATRACE_ASYNC_END("first_frame", 0);
101 ATRACE_ASYNC_END("switch_mode", 0);
102 }
103 }
104 }
105 for (auto& buffer : hal_result->output_buffers) {
106 aidl_profiler_->ProfileFrameRate("Stream " +
107 std::to_string(buffer.stream_id));
108 }
109
110 std::vector<CaptureResult> aidl_results(1);
111 status_t res = aidl_utils::ConvertToAidlCaptureResult(
112 result_metadata_queue_.get(), std::move(hal_result), &aidl_results[0]);
113 if (res != OK) {
114 ALOGE("%s: Converting to AIDL result failed: %s(%d)", __FUNCTION__,
115 strerror(-res), res);
116 return;
117 }
118
119 auto aidl_res = aidl_device_callback_->processCaptureResult(aidl_results);
120 if (!aidl_res.isOk()) {
121 ALOGE("%s: processCaptureResult transaction failed: %s.", __FUNCTION__,
122 aidl_res.getMessage());
123 return;
124 }
125 }
126
NotifyHalMessage(const google_camera_hal::NotifyMessage & hal_message)127 void AidlCameraDeviceSession::NotifyHalMessage(
128 const google_camera_hal::NotifyMessage& hal_message) {
129 std::shared_lock lock(aidl_device_callback_lock_);
130 if (aidl_device_callback_ == nullptr) {
131 ALOGE("%s: aidl_device_callback_ is nullptr", __FUNCTION__);
132 return;
133 }
134
135 std::vector<NotifyMsg> aidl_messages(1);
136 status_t res =
137 aidl_utils::ConverToAidlNotifyMessage(hal_message, &aidl_messages[0]);
138 if (res != OK) {
139 ALOGE("%s: Converting to AIDL message failed: %s(%d)", __FUNCTION__,
140 strerror(-res), res);
141 return;
142 }
143
144 auto aidl_res = aidl_device_callback_->notify(aidl_messages);
145 if (!aidl_res.isOk()) {
146 ALOGE("%s: notify transaction failed: %s.", __FUNCTION__,
147 aidl_res.getMessage());
148 return;
149 }
150 }
151
cleanupHandles(std::vector<native_handle_t * > & handles_to_delete)152 static void cleanupHandles(std::vector<native_handle_t*>& handles_to_delete) {
153 for (auto& handle : handles_to_delete) {
154 native_handle_delete(handle);
155 }
156 }
157
158 google_camera_hal::BufferRequestStatus
RequestStreamBuffers(const std::vector<google_camera_hal::BufferRequest> & hal_buffer_requests,std::vector<google_camera_hal::BufferReturn> * hal_buffer_returns)159 AidlCameraDeviceSession::RequestStreamBuffers(
160 const std::vector<google_camera_hal::BufferRequest>& hal_buffer_requests,
161 std::vector<google_camera_hal::BufferReturn>* hal_buffer_returns) {
162 std::shared_lock lock(aidl_device_callback_lock_);
163 if (aidl_device_callback_ == nullptr) {
164 ALOGE("%s: aidl_device_callback_ is nullptr", __FUNCTION__);
165 return google_camera_hal::BufferRequestStatus::kFailedUnknown;
166 }
167
168 if (hal_buffer_returns == nullptr) {
169 ALOGE("%s: hal_buffer_returns is nullptr", __FUNCTION__);
170 return google_camera_hal::BufferRequestStatus::kFailedUnknown;
171 }
172
173 std::vector<BufferRequest> aidl_buffer_requests;
174 status_t res = aidl_utils::ConvertToAidlBufferRequest(hal_buffer_requests,
175 &aidl_buffer_requests);
176 if (res != OK) {
177 ALOGE("%s: Converting to Aidl buffer request failed: %s(%d)", __FUNCTION__,
178 strerror(-res), res);
179 return google_camera_hal::BufferRequestStatus::kFailedUnknown;
180 }
181
182 BufferRequestStatus aidl_status;
183 std::vector<StreamBufferRet> stream_buffer_returns;
184 auto cb_status = aidl_device_callback_->requestStreamBuffers(
185 aidl_buffer_requests, &stream_buffer_returns, &aidl_status);
186
187 if (!cb_status.isOk()) {
188 ALOGE("%s: Transaction request stream buffers error: %s", __FUNCTION__,
189 cb_status.getMessage());
190 return google_camera_hal::BufferRequestStatus::kFailedUnknown;
191 }
192
193 google_camera_hal::BufferRequestStatus hal_buffer_request_status;
194 res = aidl_utils::ConvertToHalBufferRequestStatus(aidl_status,
195 &hal_buffer_request_status);
196 if (res != OK) {
197 ALOGE("%s: Converting to Hal buffer request status failed: %s(%d)",
198 __FUNCTION__, strerror(-res), res);
199 return google_camera_hal::BufferRequestStatus::kFailedUnknown;
200 }
201
202 hal_buffer_returns->clear();
203 // Converting AIDL stream buffer returns to HAL stream buffer returns.
204 for (auto& stream_buffer_return : stream_buffer_returns) {
205 google_camera_hal::BufferReturn hal_buffer_return;
206 res = aidl_utils::ConvertToHalBufferReturnStatus(stream_buffer_return,
207 &hal_buffer_return);
208 if (res != OK) {
209 ALOGE("%s: Converting to Hal buffer return status failed: %s(%d)",
210 __FUNCTION__, strerror(-res), res);
211 return google_camera_hal::BufferRequestStatus::kFailedUnknown;
212 }
213
214 using Tag = aidl::android::hardware::camera::device::StreamBuffersVal::Tag;
215 if (stream_buffer_return.val.getTag() == Tag::buffers) {
216 const std::vector<StreamBuffer>& aidl_buffers =
217 stream_buffer_return.val.get<Tag::buffers>();
218 std::vector<native_handle_t*> native_handles_to_delete;
219 for (auto& aidl_buffer : aidl_buffers) {
220 google_camera_hal::StreamBuffer hal_buffer = {};
221 aidl_utils::ConvertToHalStreamBuffer(aidl_buffer, &hal_buffer,
222 &native_handles_to_delete);
223 if (res != OK) {
224 ALOGE("%s: Converting to Hal stream buffer failed: %s(%d)",
225 __FUNCTION__, strerror(-res), res);
226 return google_camera_hal::BufferRequestStatus::kFailedUnknown;
227 }
228
229 if (!aidl_utils::IsAidlNativeHandleNull(aidl_buffer.acquireFence)) {
230 hal_buffer.acquire_fence = dupFromAidl(aidl_buffer.acquireFence);
231 if (hal_buffer.acquire_fence == nullptr) {
232 ALOGE("%s: Cloning Hal stream buffer acquire fence failed",
233 __FUNCTION__);
234 }
235 }
236
237 hal_buffer.release_fence = nullptr;
238 // If buffer handle is not null, we need to import buffer handle and
239 // return to the caller.
240 if (!aidl_utils::IsAidlNativeHandleNull(aidl_buffer.buffer)) {
241 native_handle_t* aidl_buffer_native_handle =
242 makeFromAidl(aidl_buffer.buffer);
243 if (buffer_mapper_v4_ != nullptr) {
244 hal_buffer.buffer = ImportBufferHandle<
245 android::hardware::graphics::mapper::V4_0::IMapper,
246 android::hardware::graphics::mapper::V4_0::Error>(
247 buffer_mapper_v4_, aidl_buffer_native_handle);
248 } else if (buffer_mapper_v3_ != nullptr) {
249 hal_buffer.buffer = ImportBufferHandle<
250 android::hardware::graphics::mapper::V3_0::IMapper,
251 android::hardware::graphics::mapper::V3_0::Error>(
252 buffer_mapper_v3_, aidl_buffer_native_handle);
253 } else {
254 hal_buffer.buffer = ImportBufferHandle<
255 android::hardware::graphics::mapper::V2_0::IMapper,
256 android::hardware::graphics::mapper::V2_0::Error>(
257 buffer_mapper_v2_, aidl_buffer_native_handle);
258 }
259 native_handle_delete(aidl_buffer_native_handle);
260 }
261
262 hal_buffer_return.val.buffers.push_back(hal_buffer);
263 }
264
265 cleanupHandles(native_handles_to_delete);
266 }
267
268 hal_buffer_returns->push_back(hal_buffer_return);
269 }
270
271 return hal_buffer_request_status;
272 }
273
274 template <class T, class U>
ImportBufferHandle(const sp<T> buffer_mapper_,const hidl_handle & buffer_hidl_handle)275 buffer_handle_t AidlCameraDeviceSession::ImportBufferHandle(
276 const sp<T> buffer_mapper_, const hidl_handle& buffer_hidl_handle) {
277 U mapper_error;
278 buffer_handle_t imported_buffer_handle;
279
280 auto hidl_res = buffer_mapper_->importBuffer(
281 buffer_hidl_handle, [&](const auto& error, const auto& buffer_handle) {
282 mapper_error = error;
283 imported_buffer_handle = static_cast<buffer_handle_t>(buffer_handle);
284 });
285 if (!hidl_res.isOk() || mapper_error != U::NONE) {
286 ALOGE("%s: Importing buffer failed: %s, mapper error %u", __FUNCTION__,
287 hidl_res.description().c_str(), mapper_error);
288 return nullptr;
289 }
290
291 return imported_buffer_handle;
292 }
293
ReturnStreamBuffers(const std::vector<google_camera_hal::StreamBuffer> & return_hal_buffers)294 void AidlCameraDeviceSession::ReturnStreamBuffers(
295 const std::vector<google_camera_hal::StreamBuffer>& return_hal_buffers) {
296 std::shared_lock lock(aidl_device_callback_lock_);
297 if (aidl_device_callback_ == nullptr) {
298 ALOGE("%s: aidl_device_callback_ is nullptr", __FUNCTION__);
299 return;
300 }
301
302 status_t res = OK;
303 std::vector<StreamBuffer> aidl_return_buffers;
304 aidl_return_buffers.resize(return_hal_buffers.size());
305 for (uint32_t i = 0; i < return_hal_buffers.size(); i++) {
306 res = aidl_utils::ConvertToAidlStreamBuffer(return_hal_buffers[i],
307 &aidl_return_buffers[i]);
308 if (res != OK) {
309 ALOGE("%s: Converting to Aidl stream buffer failed: %s(%d)", __FUNCTION__,
310 strerror(-res), res);
311 return;
312 }
313 }
314
315 auto aidl_res =
316 aidl_device_callback_->returnStreamBuffers(aidl_return_buffers);
317 if (!aidl_res.isOk()) {
318 ALOGE("%s: return stream buffers transaction failed: %s", __FUNCTION__,
319 aidl_res.getMessage());
320 return;
321 }
322 }
323
InitializeBufferMapper()324 status_t AidlCameraDeviceSession::InitializeBufferMapper() {
325 buffer_mapper_v4_ =
326 android::hardware::graphics::mapper::V4_0::IMapper::getService();
327 if (buffer_mapper_v4_ != nullptr) {
328 return OK;
329 }
330
331 buffer_mapper_v3_ =
332 android::hardware::graphics::mapper::V3_0::IMapper::getService();
333 if (buffer_mapper_v3_ != nullptr) {
334 return OK;
335 }
336
337 buffer_mapper_v2_ =
338 android::hardware::graphics::mapper::V2_0::IMapper::getService();
339 if (buffer_mapper_v2_ != nullptr) {
340 return OK;
341 }
342
343 ALOGE("%s: Getting buffer mapper failed.", __FUNCTION__);
344 return UNKNOWN_ERROR;
345 }
346
Initialize(const std::shared_ptr<ICameraDeviceCallback> & callback,std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session,std::shared_ptr<AidlProfiler> aidl_profiler)347 status_t AidlCameraDeviceSession::Initialize(
348 const std::shared_ptr<ICameraDeviceCallback>& callback,
349 std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session,
350 std::shared_ptr<AidlProfiler> aidl_profiler) {
351 ATRACE_NAME("AidlCameraDeviceSession::Initialize");
352 if (device_session == nullptr) {
353 ALOGE("%s: device_session is nullptr.", __FUNCTION__);
354 return BAD_VALUE;
355 }
356
357 if (aidl_profiler == nullptr) {
358 ALOGE("%s: aidl_profiler is nullptr.", __FUNCTION__);
359 return BAD_VALUE;
360 }
361
362 status_t res = CreateMetadataQueue(&request_metadata_queue_,
363 kRequestMetadataQueueSizeBytes,
364 "ro.vendor.camera.req.fmq.size");
365 if (res != OK) {
366 ALOGE("%s: Creating request metadata queue failed: %s(%d)", __FUNCTION__,
367 strerror(-res), res);
368 return res;
369 }
370
371 res = CreateMetadataQueue(&result_metadata_queue_,
372 kResultMetadataQueueSizeBytes,
373 "ro.vendor.camera.res.fmq.size");
374 if (res != OK) {
375 ALOGE("%s: Creating result metadata queue failed: %s(%d)", __FUNCTION__,
376 strerror(-res), res);
377 return res;
378 }
379
380 // Initialize buffer mapper
381 res = InitializeBufferMapper();
382 if (res != OK) {
383 ALOGE("%s: Initialize buffer mapper failed: %s(%d)", __FUNCTION__,
384 strerror(-res), res);
385 return res;
386 }
387
388 thermal_ = android::hardware::thermal::V2_0::IThermal::getService();
389 if (thermal_ == nullptr) {
390 ALOGE("%s: Getting thermal failed.", __FUNCTION__);
391 // Continue without getting thermal information.
392 }
393
394 aidl_device_callback_ = callback;
395 device_session_ = std::move(device_session);
396 aidl_profiler_ = aidl_profiler;
397
398 SetSessionCallbacks();
399 return OK;
400 }
401
SetSessionCallbacks()402 void AidlCameraDeviceSession::SetSessionCallbacks() {
403 google_camera_hal::CameraDeviceSessionCallback session_callback = {
404 .process_capture_result = google_camera_hal::ProcessCaptureResultFunc(
405 [this](std::unique_ptr<google_camera_hal::CaptureResult> result) {
406 ProcessCaptureResult(std::move(result));
407 }),
408 .notify = google_camera_hal::NotifyFunc(
409 [this](const google_camera_hal::NotifyMessage& message) {
410 NotifyHalMessage(message);
411 }),
412 .request_stream_buffers = google_camera_hal::RequestStreamBuffersFunc(
413 [this](
414 const std::vector<google_camera_hal::BufferRequest>&
415 hal_buffer_requests,
416 std::vector<google_camera_hal::BufferReturn>* hal_buffer_returns) {
417 return RequestStreamBuffers(hal_buffer_requests, hal_buffer_returns);
418 }),
419 .return_stream_buffers = google_camera_hal::ReturnStreamBuffersFunc(
420 [this](const std::vector<google_camera_hal::StreamBuffer>&
421 return_hal_buffers) {
422 ReturnStreamBuffers(return_hal_buffers);
423 }),
424 };
425
426 google_camera_hal::ThermalCallback thermal_callback = {
427 .register_thermal_changed_callback =
428 google_camera_hal::RegisterThermalChangedCallbackFunc(
429 [this](google_camera_hal::NotifyThrottlingFunc notify_throttling,
430 bool filter_type, google_camera_hal::TemperatureType type) {
431 return RegisterThermalChangedCallback(notify_throttling,
432 filter_type, type);
433 }),
434 .unregister_thermal_changed_callback =
435 google_camera_hal::UnregisterThermalChangedCallbackFunc(
436 [this]() { UnregisterThermalChangedCallback(); }),
437 };
438
439 device_session_->SetSessionCallback(session_callback, thermal_callback);
440 }
441
RegisterThermalChangedCallback(google_camera_hal::NotifyThrottlingFunc notify_throttling,bool filter_type,google_camera_hal::TemperatureType type)442 status_t AidlCameraDeviceSession::RegisterThermalChangedCallback(
443 google_camera_hal::NotifyThrottlingFunc notify_throttling, bool filter_type,
444 google_camera_hal::TemperatureType type) {
445 std::lock_guard<std::mutex> lock(hidl_thermal_mutex_);
446 if (thermal_ == nullptr) {
447 ALOGE("%s: thermal was not initialized.", __FUNCTION__);
448 return NO_INIT;
449 }
450
451 if (thermal_changed_callback_ != nullptr) {
452 ALOGE("%s: thermal changed callback is already registered.", __FUNCTION__);
453 return ALREADY_EXISTS;
454 }
455
456 TemperatureType hidl_type;
457 status_t res =
458 hidl_thermal_utils::ConvertToHidlTemperatureType(type, &hidl_type);
459 if (res != OK) {
460 ALOGE("%s: Converting to HIDL type failed: %s(%d)", __FUNCTION__,
461 strerror(-res), res);
462 return res;
463 }
464
465 std::unique_ptr<hidl_thermal_utils::HidlThermalChangedCallback> callback =
466 hidl_thermal_utils::HidlThermalChangedCallback::Create(notify_throttling);
467 thermal_changed_callback_ = callback.release();
468 ThermalStatus thermal_status;
469 auto hidl_res = thermal_->registerThermalChangedCallback(
470 thermal_changed_callback_, filter_type, hidl_type,
471 [&](ThermalStatus status) { thermal_status = status; });
472 if (!hidl_res.isOk() || thermal_status.code != ThermalStatusCode::SUCCESS) {
473 thermal_changed_callback_ = nullptr;
474 return UNKNOWN_ERROR;
475 }
476
477 return OK;
478 }
479
UnregisterThermalChangedCallback()480 void AidlCameraDeviceSession::UnregisterThermalChangedCallback() {
481 std::lock_guard<std::mutex> lock(hidl_thermal_mutex_);
482 if (thermal_changed_callback_ == nullptr) {
483 // no-op if no thermal changed callback is registered.
484 return;
485 }
486
487 if (thermal_ == nullptr) {
488 ALOGE("%s: thermal was not initialized.", __FUNCTION__);
489 return;
490 }
491
492 ThermalStatus thermal_status;
493 auto hidl_res = thermal_->unregisterThermalChangedCallback(
494 thermal_changed_callback_,
495 [&](ThermalStatus status) { thermal_status = status; });
496 if (!hidl_res.isOk() || thermal_status.code != ThermalStatusCode::SUCCESS) {
497 ALOGW("%s: Unregstering thermal callback failed: %s", __FUNCTION__,
498 thermal_status.debugMessage.c_str());
499 }
500
501 thermal_changed_callback_ = nullptr;
502 }
503
CreateMetadataQueue(std::unique_ptr<MetadataQueue> * metadata_queue,uint32_t default_size_bytes,const char * override_size_property)504 status_t AidlCameraDeviceSession::CreateMetadataQueue(
505 std::unique_ptr<MetadataQueue>* metadata_queue, uint32_t default_size_bytes,
506 const char* override_size_property) {
507 if (metadata_queue == nullptr) {
508 ALOGE("%s: metadata_queue is nullptr", __FUNCTION__);
509 return BAD_VALUE;
510 }
511
512 int32_t size = default_size_bytes;
513 if (override_size_property != nullptr) {
514 // Try to read the override size from the system property.
515 size = property_get_int32(override_size_property, default_size_bytes);
516 ALOGV("%s: request metadata queue size overridden to %d", __FUNCTION__,
517 size);
518 }
519
520 *metadata_queue =
521 std::make_unique<MetadataQueue>(static_cast<size_t>(size),
522 /*configureEventFlagWord*/ false);
523 if (!(*metadata_queue)->isValid()) {
524 ALOGE("%s: Creating metadata queue (size %d) failed.", __FUNCTION__, size);
525 return NO_INIT;
526 }
527
528 return OK;
529 }
530
constructDefaultRequestSettings(RequestTemplate type,CameraMetadata * aidl_return)531 ScopedAStatus AidlCameraDeviceSession::constructDefaultRequestSettings(
532 RequestTemplate type, CameraMetadata* aidl_return) {
533 ATRACE_NAME("AidlCameraDeviceSession::constructDefaultRequestSettings");
534 if (aidl_return == nullptr) {
535 return ScopedAStatus::fromServiceSpecificError(
536 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
537 }
538 aidl_return->metadata.clear();
539 if (device_session_ == nullptr) {
540 return ScopedAStatus::fromServiceSpecificError(
541 static_cast<int32_t>(Status::INTERNAL_ERROR));
542 }
543
544 google_camera_hal::RequestTemplate hal_type;
545 status_t res = aidl_utils::ConvertToHalTemplateType(type, &hal_type);
546 if (res != OK) {
547 return ScopedAStatus::fromServiceSpecificError(
548 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
549 }
550
551 std::unique_ptr<google_camera_hal::HalCameraMetadata> settings = nullptr;
552 res = device_session_->ConstructDefaultRequestSettings(hal_type, &settings);
553 if (res != OK) {
554 return aidl_utils::ConvertToAidlReturn(res);
555 }
556
557 uint32_t metadata_size = settings->GetCameraMetadataSize();
558 uint8_t* settings_p = (uint8_t*)settings->ReleaseCameraMetadata();
559 aidl_return->metadata.assign(settings_p, settings_p + metadata_size);
560 return ScopedAStatus::ok();
561 }
562
configureStreams(const StreamConfiguration & requestedConfiguration,std::vector<HalStream> * aidl_return)563 ScopedAStatus AidlCameraDeviceSession::configureStreams(
564 const StreamConfiguration& requestedConfiguration,
565 std::vector<HalStream>* aidl_return) {
566 ATRACE_NAME("AidlCameraDeviceSession::configureStreams");
567 if (aidl_return == nullptr) {
568 return ScopedAStatus::fromServiceSpecificError(
569 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
570 }
571 aidl_return->clear();
572 if (device_session_ == nullptr) {
573 return ScopedAStatus::fromServiceSpecificError(
574 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
575 }
576
577 auto profiler = aidl_profiler_->MakeScopedProfiler(
578 AidlProfiler::ScopedType::kConfigureStream,
579 device_session_->GetProfiler(aidl_profiler_->GetCameraId(),
580 aidl_profiler_->GetLatencyFlag()),
581 device_session_->GetProfiler(aidl_profiler_->GetCameraId(),
582 aidl_profiler_->GetFpsFlag()));
583
584 first_frame_requested_ = false;
585 num_pending_first_frame_buffers_ = 0;
586
587 google_camera_hal::StreamConfiguration hal_stream_config;
588 status_t res = aidl_utils::ConvertToHalStreamConfig(requestedConfiguration,
589 &hal_stream_config);
590 if (res != OK) {
591 return ScopedAStatus::fromServiceSpecificError(
592 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
593 }
594
595 std::vector<google_camera_hal::HalStream> hal_configured_streams;
596 res = device_session_->ConfigureStreams(hal_stream_config,
597 &hal_configured_streams);
598 if (res != OK) {
599 ALOGE("%s: Configuring streams failed: %s(%d)", __FUNCTION__,
600 strerror(-res), res);
601 return aidl_utils::ConvertToAidlReturn(res);
602 }
603
604 res = aidl_utils::ConvertToAidlHalStreamConfig(hal_configured_streams,
605 aidl_return);
606 if (res != OK) {
607 return aidl_utils::ConvertToAidlReturn(res);
608 }
609 return ScopedAStatus::ok();
610 }
611
getCaptureRequestMetadataQueue(::aidl::android::hardware::common::fmq::MQDescriptor<int8_t,SynchronizedReadWrite> * aidl_return)612 ScopedAStatus AidlCameraDeviceSession::getCaptureRequestMetadataQueue(
613 ::aidl::android::hardware::common::fmq::MQDescriptor<
614 int8_t, SynchronizedReadWrite>* aidl_return) {
615 *aidl_return = request_metadata_queue_->dupeDesc();
616 return ScopedAStatus::ok();
617 }
618
getCaptureResultMetadataQueue(::aidl::android::hardware::common::fmq::MQDescriptor<int8_t,SynchronizedReadWrite> * aidl_return)619 ScopedAStatus AidlCameraDeviceSession::getCaptureResultMetadataQueue(
620 ::aidl::android::hardware::common::fmq::MQDescriptor<
621 int8_t, SynchronizedReadWrite>* aidl_return) {
622 *aidl_return = result_metadata_queue_->dupeDesc();
623 return ScopedAStatus::ok();
624 }
625
processCaptureRequest(const std::vector<CaptureRequest> & requests,const std::vector<BufferCache> & cachesToRemove,int32_t * aidl_return)626 ScopedAStatus AidlCameraDeviceSession::processCaptureRequest(
627 const std::vector<CaptureRequest>& requests,
628 const std::vector<BufferCache>& cachesToRemove, int32_t* aidl_return) {
629 if (aidl_return == nullptr || device_session_ == nullptr) {
630 return ScopedAStatus::fromServiceSpecificError(
631 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
632 }
633 *aidl_return = 0;
634 bool profile_first_request = false;
635 if (!first_frame_requested_) {
636 first_frame_requested_ = true;
637 profile_first_request = true;
638 ATRACE_BEGIN("AidlCameraDeviceSession::FirstRequest");
639 num_pending_first_frame_buffers_ = requests[0].outputBuffers.size();
640 first_request_frame_number_ = requests[0].frameNumber;
641 aidl_profiler_->FirstFrameStart();
642 ATRACE_ASYNC_BEGIN("first_frame", 0);
643 }
644
645 std::vector<google_camera_hal::BufferCache> hal_buffer_caches;
646
647 status_t res =
648 aidl_utils::ConvertToHalBufferCaches(cachesToRemove, &hal_buffer_caches);
649 if (res != OK) {
650 if (profile_first_request) {
651 ATRACE_END();
652 }
653 return ScopedAStatus::fromServiceSpecificError(
654 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
655 }
656
657 device_session_->RemoveBufferCache(hal_buffer_caches);
658
659 // Converting AIDL requests to HAL requests.
660 std::vector<native_handle_t*> handles_to_delete;
661 std::vector<google_camera_hal::CaptureRequest> hal_requests;
662 for (auto& request : requests) {
663 google_camera_hal::CaptureRequest hal_request = {};
664 res = aidl_utils::ConvertToHalCaptureRequest(
665 request, request_metadata_queue_.get(), &hal_request,
666 &handles_to_delete);
667 if (res != OK) {
668 ALOGE("%s: Converting to HAL capture request failed: %s(%d)",
669 __FUNCTION__, strerror(-res), res);
670 if (profile_first_request) {
671 ATRACE_END();
672 }
673 cleanupHandles(handles_to_delete);
674 return aidl_utils::ConvertToAidlReturn(res);
675 }
676
677 hal_requests.push_back(std::move(hal_request));
678 }
679
680 uint32_t num_processed_requests = 0;
681 res = device_session_->ProcessCaptureRequest(hal_requests,
682 &num_processed_requests);
683 if (res != OK) {
684 ALOGE(
685 "%s: Processing capture request failed: %s(%d). Only processed %u"
686 " out of %zu.",
687 __FUNCTION__, strerror(-res), res, num_processed_requests,
688 hal_requests.size());
689 }
690 if (num_processed_requests > INT_MAX) {
691 cleanupHandles(handles_to_delete);
692 return ScopedAStatus::fromServiceSpecificError(
693 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
694 }
695 *aidl_return = (int32_t)num_processed_requests;
696 if (profile_first_request) {
697 ATRACE_END();
698 }
699 cleanupHandles(handles_to_delete);
700 return aidl_utils::ConvertToAidlReturn(res);
701 }
702
signalStreamFlush(const std::vector<int32_t> &,int32_t)703 ScopedAStatus AidlCameraDeviceSession::signalStreamFlush(
704 const std::vector<int32_t>&, int32_t) {
705 // TODO(b/143902312): Implement this.
706 return ScopedAStatus::ok();
707 }
708
flush()709 ScopedAStatus AidlCameraDeviceSession::flush() {
710 ATRACE_NAME("AidlCameraDeviceSession::flush");
711 ATRACE_ASYNC_BEGIN("switch_mode", 0);
712 if (device_session_ == nullptr) {
713 return ScopedAStatus::fromServiceSpecificError(
714 static_cast<int32_t>(Status::INTERNAL_ERROR));
715 }
716
717 auto profiler = aidl_profiler_->MakeScopedProfiler(
718 AidlProfiler::ScopedType::kFlush,
719 device_session_->GetProfiler(aidl_profiler_->GetCameraId(),
720 aidl_profiler_->GetLatencyFlag()),
721 device_session_->GetProfiler(aidl_profiler_->GetCameraId(),
722 aidl_profiler_->GetFpsFlag()));
723
724 status_t res = device_session_->Flush();
725 if (res != OK) {
726 ALOGE("%s: Flushing device failed: %s(%d).", __FUNCTION__, strerror(-res),
727 res);
728 return ScopedAStatus::fromServiceSpecificError(
729 static_cast<int32_t>(Status::INTERNAL_ERROR));
730 }
731
732 return ScopedAStatus::ok();
733 }
734
close()735 ScopedAStatus AidlCameraDeviceSession::close() {
736 ATRACE_NAME("AidlCameraDeviceSession::close");
737 if (device_session_ != nullptr) {
738 auto profiler = aidl_profiler_->MakeScopedProfiler(
739 AidlProfiler::ScopedType::kClose,
740 device_session_->GetProfiler(aidl_profiler_->GetCameraId(),
741 aidl_profiler_->GetLatencyFlag()),
742 device_session_->GetProfiler(aidl_profiler_->GetCameraId(),
743 aidl_profiler_->GetFpsFlag()));
744 device_session_ = nullptr;
745 }
746 return ScopedAStatus::ok();
747 }
748
switchToOffline(const std::vector<int32_t> &,CameraOfflineSessionInfo * out_offlineSessionInfo,std::shared_ptr<ICameraOfflineSession> * aidl_return)749 ScopedAStatus AidlCameraDeviceSession::switchToOffline(
750 const std::vector<int32_t>&,
751 CameraOfflineSessionInfo* out_offlineSessionInfo,
752 std::shared_ptr<ICameraOfflineSession>* aidl_return) {
753 *out_offlineSessionInfo = CameraOfflineSessionInfo();
754 *aidl_return = nullptr;
755 return ScopedAStatus::fromServiceSpecificError(
756 static_cast<int32_t>(Status::INTERNAL_ERROR));
757 }
758
isReconfigurationRequired(const CameraMetadata & oldSessionParams,const CameraMetadata & newSessionParams,bool * reconfiguration_required)759 ScopedAStatus AidlCameraDeviceSession::isReconfigurationRequired(
760 const CameraMetadata& oldSessionParams,
761 const CameraMetadata& newSessionParams, bool* reconfiguration_required) {
762 ATRACE_NAME("AidlCameraDeviceSession::isReconfigurationRequired");
763 if (reconfiguration_required == nullptr) {
764 return ScopedAStatus::fromServiceSpecificError(
765 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
766 }
767 *reconfiguration_required = true;
768 std::unique_ptr<google_camera_hal::HalCameraMetadata> old_hal_session_metadata;
769 status_t res = aidl_utils::ConvertToHalMetadata(
770 0, nullptr, oldSessionParams.metadata, &old_hal_session_metadata);
771 if (res != OK) {
772 ALOGE("%s: Converting to old session metadata failed: %s(%d)", __FUNCTION__,
773 strerror(-res), res);
774 return ScopedAStatus::fromServiceSpecificError(
775 static_cast<int32_t>(Status::INTERNAL_ERROR));
776 }
777
778 std::unique_ptr<google_camera_hal::HalCameraMetadata> new_hal_session_metadata;
779 res = aidl_utils::ConvertToHalMetadata(0, nullptr, newSessionParams.metadata,
780 &new_hal_session_metadata);
781 if (res != OK) {
782 ALOGE("%s: Converting to new session metadata failed: %s(%d)", __FUNCTION__,
783 strerror(-res), res);
784 return ScopedAStatus::fromServiceSpecificError(
785 static_cast<int32_t>(Status::INTERNAL_ERROR));
786 }
787
788 res = device_session_->IsReconfigurationRequired(
789 old_hal_session_metadata.get(), new_hal_session_metadata.get(),
790 reconfiguration_required);
791
792 if (res != OK) {
793 ALOGE("%s: IsReconfigurationRequired failed: %s(%d)", __FUNCTION__,
794 strerror(-res), res);
795 return ScopedAStatus::fromServiceSpecificError(
796 static_cast<int32_t>(Status::INTERNAL_ERROR));
797 }
798
799 return ScopedAStatus::ok();
800 }
801
createBinder()802 ::ndk::SpAIBinder AidlCameraDeviceSession::createBinder() {
803 auto binder = BnCameraDeviceSession::createBinder();
804 AIBinder_setInheritRt(binder.get(), true);
805 return binder;
806 }
807
808 } // namespace implementation
809 } // namespace device
810 } // namespace camera
811 } // namespace hardware
812 } // namespace android
813