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