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