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