summaryrefslogtreecommitdiff
path: root/common/hal/hidl_service/hidl_camera_device_session.cc
blob: d0dc010be03ffc6d3058ff5d1f2e132184ac0d8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "GCH_HidlCameraDeviceSession"
#define ATRACE_TAG ATRACE_TAG_CAMERA
//#define LOG_NDEBUG 0
#include "hidl_camera_device_session.h"

#include <cutils/properties.h>
#include <cutils/trace.h>
#include <log/log.h>
#include <malloc.h>
#include <utils/Trace.h>

#include "hidl_profiler.h"
#include "hidl_utils.h"

namespace android {
namespace hardware {
namespace camera {
namespace device {
namespace V3_7 {
namespace implementation {

namespace hidl_utils = ::android::hardware::camera::implementation::hidl_utils;

using ::android::hardware::camera::device::V3_2::NotifyMsg;
using ::android::hardware::camera::device::V3_2::StreamBuffer;
using ::android::hardware::camera::device::V3_4::CaptureResult;
using ::android::hardware::camera::device::V3_5::BufferRequest;
using ::android::hardware::camera::device::V3_5::BufferRequestStatus;
using ::android::hardware::camera::device::V3_5::StreamBufferRet;
using ::android::hardware::camera::device::V3_5::StreamBuffersVal;
using ::android::hardware::camera::device::V3_6::HalStreamConfiguration;
using ::android::hardware::thermal::V1_0::ThermalStatus;
using ::android::hardware::thermal::V1_0::ThermalStatusCode;
using ::android::hardware::thermal::V2_0::Temperature;
using ::android::hardware::thermal::V2_0::TemperatureType;

std::unique_ptr<HidlCameraDeviceSession> HidlCameraDeviceSession::Create(
    const sp<V3_2::ICameraDeviceCallback>& callback,
    std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session,
    std::shared_ptr<HidlProfiler> hidl_profiler) {
  ATRACE_NAME("HidlCameraDeviceSession::Create");
  auto session =
      std::unique_ptr<HidlCameraDeviceSession>(new HidlCameraDeviceSession());
  if (session == nullptr) {
    ALOGE("%s: Cannot create a HidlCameraDeviceSession.", __FUNCTION__);
    return nullptr;
  }

  status_t res =
      session->Initialize(callback, std::move(device_session), hidl_profiler);
  if (res != OK) {
    ALOGE("%s: Initializing HidlCameraDeviceSession failed: %s(%d)",
          __FUNCTION__, strerror(-res), res);
    return nullptr;
  }

  return session;
}

HidlCameraDeviceSession::~HidlCameraDeviceSession() {
  ATRACE_NAME("HidlCameraDeviceSession::~HidlCameraDeviceSession");
  close();
  // camera's closing, so flush any unused malloc pages
  mallopt(M_PURGE, 0);
}

void HidlCameraDeviceSession::ProcessCaptureResult(
    std::unique_ptr<google_camera_hal::CaptureResult> hal_result) {
  std::shared_lock lock(hidl_device_callback_lock_);
  if (hidl_device_callback_ == nullptr) {
    ALOGE("%s: hidl_device_callback_ is nullptr", __FUNCTION__);
    return;
  }

  {
    std::lock_guard<std::mutex> pending_lock(pending_first_frame_buffers_mutex_);
    if (!hal_result->output_buffers.empty() &&
        num_pending_first_frame_buffers_ > 0 &&
        first_request_frame_number_ == hal_result->frame_number) {
      num_pending_first_frame_buffers_ -= hal_result->output_buffers.size();
      if (num_pending_first_frame_buffers_ == 0) {
        ALOGI("%s: First frame done", __FUNCTION__);
        hidl_profiler_->FirstFrameEnd();
        ATRACE_ASYNC_END("first_frame", 0);
        ATRACE_ASYNC_END("switch_mode", 0);
      }
    }
  }
  for (auto& buffer : hal_result->output_buffers) {
    hidl_profiler_->ProfileFrameRate("Stream " +
                                     std::to_string(buffer.stream_id));
  }

  hidl_vec<CaptureResult> hidl_results(1);
  status_t res = hidl_utils::ConvertToHidlCaptureResult(
      result_metadata_queue_.get(), std::move(hal_result), &hidl_results[0]);
  if (res != OK) {
    ALOGE("%s: Converting to HIDL result failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return;
  }

  auto hidl_res = hidl_device_callback_->processCaptureResult_3_4(hidl_results);
  if (!hidl_res.isOk()) {
    ALOGE("%s: processCaptureResult transaction failed: %s.", __FUNCTION__,
          hidl_res.description().c_str());
    return;
  }
}

void HidlCameraDeviceSession::NotifyHalMessage(
    const google_camera_hal::NotifyMessage& hal_message) {
  std::shared_lock lock(hidl_device_callback_lock_);
  if (hidl_device_callback_ == nullptr) {
    ALOGE("%s: hidl_device_callback_ is nullptr", __FUNCTION__);
    return;
  }

  hidl_vec<NotifyMsg> hidl_messages(1);
  status_t res =
      hidl_utils::ConverToHidlNotifyMessage(hal_message, &hidl_messages[0]);
  if (res != OK) {
    ALOGE("%s: Converting to HIDL message failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return;
  }

  auto hidl_res = hidl_device_callback_->notify(hidl_messages);
  if (!hidl_res.isOk()) {
    ALOGE("%s: notify transaction failed: %s.", __FUNCTION__,
          hidl_res.description().c_str());
    return;
  }
}

google_camera_hal::BufferRequestStatus
HidlCameraDeviceSession::RequestStreamBuffers(
    const std::vector<google_camera_hal::BufferRequest>& hal_buffer_requests,
    std::vector<google_camera_hal::BufferReturn>* hal_buffer_returns) {
  std::shared_lock lock(hidl_device_callback_lock_);
  if (hidl_device_callback_ == nullptr) {
    ALOGE("%s: hidl_device_callback_ is nullptr", __FUNCTION__);
    return google_camera_hal::BufferRequestStatus::kFailedUnknown;
  }

  if (hal_buffer_returns == nullptr) {
    ALOGE("%s: hal_buffer_returns is nullptr", __FUNCTION__);
    return google_camera_hal::BufferRequestStatus::kFailedUnknown;
  }

  hidl_vec<BufferRequest> hidl_buffer_requests;
  status_t res = hidl_utils::ConvertToHidlBufferRequest(hal_buffer_requests,
                                                        &hidl_buffer_requests);
  if (res != OK) {
    ALOGE("%s: Converting to Hidl buffer request failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return google_camera_hal::BufferRequestStatus::kFailedUnknown;
  }

  BufferRequestStatus hidl_status;
  hidl_vec<StreamBufferRet> stream_buffer_returns;
  auto cb_status = hidl_device_callback_->requestStreamBuffers(
      hidl_buffer_requests, [&hidl_status, &stream_buffer_returns](
                                BufferRequestStatus status_ret,
                                const hidl_vec<StreamBufferRet>& buffer_ret) {
        hidl_status = status_ret;
        stream_buffer_returns = std::move(buffer_ret);
      });
  if (!cb_status.isOk()) {
    ALOGE("%s: Transaction request stream buffers error: %s", __FUNCTION__,
          cb_status.description().c_str());
    return google_camera_hal::BufferRequestStatus::kFailedUnknown;
  }

  google_camera_hal::BufferRequestStatus hal_buffer_request_status;
  res = hidl_utils::ConvertToHalBufferRequestStatus(hidl_status,
                                                    &hal_buffer_request_status);
  if (res != OK) {
    ALOGE("%s: Converting to Hal buffer request status failed: %s(%d)",
          __FUNCTION__, strerror(-res), res);
    return google_camera_hal::BufferRequestStatus::kFailedUnknown;
  }

  hal_buffer_returns->clear();
  // Converting HIDL stream buffer returns to HAL stream buffer returns.
  for (auto& stream_buffer_return : stream_buffer_returns) {
    google_camera_hal::BufferReturn hal_buffer_return;
    res = hidl_utils::ConvertToHalBufferReturnStatus(stream_buffer_return,
                                                     &hal_buffer_return);
    if (res != OK) {
      ALOGE("%s: Converting to Hal buffer return status failed: %s(%d)",
            __FUNCTION__, strerror(-res), res);
      return google_camera_hal::BufferRequestStatus::kFailedUnknown;
    }

    if (stream_buffer_return.val.getDiscriminator() ==
        V3_5::StreamBuffersVal::hidl_discriminator::buffers) {
      const hidl_vec<StreamBuffer>& hidl_buffers =
          stream_buffer_return.val.buffers();
      for (auto& hidl_buffer : hidl_buffers) {
        google_camera_hal::StreamBuffer hal_buffer = {};
        hidl_utils::ConvertToHalStreamBuffer(hidl_buffer, &hal_buffer);
        if (res != OK) {
          ALOGE("%s: Converting to Hal stream buffer failed: %s(%d)",
                __FUNCTION__, strerror(-res), res);
          return google_camera_hal::BufferRequestStatus::kFailedUnknown;
        }

        if (hidl_buffer.acquireFence.getNativeHandle() != nullptr) {
          hal_buffer.acquire_fence =
              native_handle_clone(hidl_buffer.acquireFence.getNativeHandle());
          if (hal_buffer.acquire_fence == nullptr) {
            ALOGE("%s: Cloning Hal stream buffer acquire fence failed",
                  __FUNCTION__);
          }
        }

        hal_buffer.release_fence = nullptr;
        // If buffer handle is not null, we need to import buffer handle and
        // return to the caller.
        if (hidl_buffer.buffer.getNativeHandle() != nullptr) {
          if (buffer_mapper_v4_ != nullptr) {
            hal_buffer.buffer = ImportBufferHandle<
                android::hardware::graphics::mapper::V4_0::IMapper,
                android::hardware::graphics::mapper::V4_0::Error>(
                buffer_mapper_v4_, hidl_buffer.buffer);
          } else if (buffer_mapper_v3_ != nullptr) {
            hal_buffer.buffer = ImportBufferHandle<
                android::hardware::graphics::mapper::V3_0::IMapper,
                android::hardware::graphics::mapper::V3_0::Error>(
                buffer_mapper_v3_, hidl_buffer.buffer);
          } else {
            hal_buffer.buffer = ImportBufferHandle<
                android::hardware::graphics::mapper::V2_0::IMapper,
                android::hardware::graphics::mapper::V2_0::Error>(
                buffer_mapper_v2_, hidl_buffer.buffer);
          }
        }

        hal_buffer_return.val.buffers.push_back(hal_buffer);
      }
    }

    hal_buffer_returns->push_back(hal_buffer_return);
  }

  return hal_buffer_request_status;
}

template <class T, class U>
buffer_handle_t HidlCameraDeviceSession::ImportBufferHandle(
    const sp<T> buffer_mapper_, const hidl_handle& buffer_hidl_handle) {
  U mapper_error;
  buffer_handle_t imported_buffer_handle;

  auto hidl_res = buffer_mapper_->importBuffer(
      buffer_hidl_handle, [&](const auto& error, const auto& buffer_handle) {
        mapper_error = error;
        imported_buffer_handle = static_cast<buffer_handle_t>(buffer_handle);
      });
  if (!hidl_res.isOk() || mapper_error != U::NONE) {
    ALOGE("%s: Importing buffer failed: %s, mapper error %u", __FUNCTION__,
          hidl_res.description().c_str(), mapper_error);
    return nullptr;
  }

  return imported_buffer_handle;
}

void HidlCameraDeviceSession::ReturnStreamBuffers(
    const std::vector<google_camera_hal::StreamBuffer>& return_hal_buffers) {
  std::shared_lock lock(hidl_device_callback_lock_);
  if (hidl_device_callback_ == nullptr) {
    ALOGE("%s: hidl_device_callback_ is nullptr", __FUNCTION__);
    return;
  }

  status_t res = OK;
  hidl_vec<StreamBuffer> hidl_return_buffers;
  hidl_return_buffers.resize(return_hal_buffers.size());
  for (uint32_t i = 0; i < return_hal_buffers.size(); i++) {
    res = hidl_utils::ConvertToHidlStreamBuffer(return_hal_buffers[i],
                                                &hidl_return_buffers[i]);
    if (res != OK) {
      ALOGE("%s: Converting to Hidl stream buffer failed: %s(%d)", __FUNCTION__,
            strerror(-res), res);
      return;
    }
  }

  hidl_device_callback_->returnStreamBuffers(hidl_return_buffers);
}

status_t HidlCameraDeviceSession::InitializeBufferMapper() {
  buffer_mapper_v4_ =
      android::hardware::graphics::mapper::V4_0::IMapper::getService();
  if (buffer_mapper_v4_ != nullptr) {
    return OK;
  }

  buffer_mapper_v3_ =
      android::hardware::graphics::mapper::V3_0::IMapper::getService();
  if (buffer_mapper_v3_ != nullptr) {
    return OK;
  }

  buffer_mapper_v2_ =
      android::hardware::graphics::mapper::V2_0::IMapper::getService();
  if (buffer_mapper_v2_ != nullptr) {
    return OK;
  }

  ALOGE("%s: Getting buffer mapper failed.", __FUNCTION__);
  return UNKNOWN_ERROR;
}

status_t HidlCameraDeviceSession::Initialize(
    const sp<V3_2::ICameraDeviceCallback>& callback,
    std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session,
    std::shared_ptr<HidlProfiler> hidl_profiler) {
  ATRACE_NAME("HidlCameraDeviceSession::Initialize");
  if (device_session == nullptr) {
    ALOGE("%s: device_session is nullptr.", __FUNCTION__);
    return BAD_VALUE;
  }

  if (hidl_profiler == nullptr) {
    ALOGE("%s: hidl_profiler is nullptr.", __FUNCTION__);
    return BAD_VALUE;
  }

  status_t res = CreateMetadataQueue(&request_metadata_queue_,
                                     kRequestMetadataQueueSizeBytes,
                                     "ro.vendor.camera.req.fmq.size");
  if (res != OK) {
    ALOGE("%s: Creating request metadata queue failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return res;
  }

  res = CreateMetadataQueue(&result_metadata_queue_,
                            kResultMetadataQueueSizeBytes,
                            "ro.vendor.camera.res.fmq.size");
  if (res != OK) {
    ALOGE("%s: Creating result metadata queue failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return res;
  }

  // Cast V3.2 callback to V3.5
  auto cast_res = ICameraDeviceCallback::castFrom(callback);
  if (!cast_res.isOk()) {
    ALOGE("%s: Cannot convert to V3.5 device callback.", __FUNCTION__);
    return UNKNOWN_ERROR;
  }

  // Initialize buffer mapper
  res = InitializeBufferMapper();
  if (res != OK) {
    ALOGE("%s: Initialize buffer mapper failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return res;
  }

  thermal_ = android::hardware::thermal::V2_0::IThermal::getService();
  if (thermal_ == nullptr) {
    ALOGE("%s: Getting thermal failed.", __FUNCTION__);
    // Continue without getting thermal information.
  }

  hidl_device_callback_ = cast_res;
  device_session_ = std::move(device_session);
  hidl_profiler_ = hidl_profiler;

  SetSessionCallbacks();
  return OK;
}

void HidlCameraDeviceSession::SetSessionCallbacks() {
  google_camera_hal::CameraDeviceSessionCallback session_callback = {
      .process_capture_result = google_camera_hal::ProcessCaptureResultFunc(
          [this](std::unique_ptr<google_camera_hal::CaptureResult> result) {
            ProcessCaptureResult(std::move(result));
          }),
      .notify = google_camera_hal::NotifyFunc(
          [this](const google_camera_hal::NotifyMessage& message) {
            NotifyHalMessage(message);
          }),
      .request_stream_buffers = google_camera_hal::RequestStreamBuffersFunc(
          [this](
              const std::vector<google_camera_hal::BufferRequest>&
                  hal_buffer_requests,
              std::vector<google_camera_hal::BufferReturn>* hal_buffer_returns) {
            return RequestStreamBuffers(hal_buffer_requests, hal_buffer_returns);
          }),
      .return_stream_buffers = google_camera_hal::ReturnStreamBuffersFunc(
          [this](const std::vector<google_camera_hal::StreamBuffer>&
                     return_hal_buffers) {
            ReturnStreamBuffers(return_hal_buffers);
          }),
  };

  google_camera_hal::ThermalCallback thermal_callback = {
      .register_thermal_changed_callback =
          google_camera_hal::RegisterThermalChangedCallbackFunc(
              [this](google_camera_hal::NotifyThrottlingFunc notify_throttling,
                     bool filter_type, google_camera_hal::TemperatureType type) {
                return RegisterThermalChangedCallback(notify_throttling,
                                                      filter_type, type);
              }),
      .unregister_thermal_changed_callback =
          google_camera_hal::UnregisterThermalChangedCallbackFunc(
              [this]() { UnregisterThermalChangedCallback(); }),
  };

  device_session_->SetSessionCallback(session_callback, thermal_callback);
}

status_t HidlCameraDeviceSession::RegisterThermalChangedCallback(
    google_camera_hal::NotifyThrottlingFunc notify_throttling, bool filter_type,
    google_camera_hal::TemperatureType type) {
  std::lock_guard<std::mutex> lock(hidl_thermal_mutex_);
  if (thermal_ == nullptr) {
    ALOGE("%s: thermal was not initialized.", __FUNCTION__);
    return NO_INIT;
  }

  if (thermal_changed_callback_ != nullptr) {
    ALOGE("%s: thermal changed callback is already registered.", __FUNCTION__);
    return ALREADY_EXISTS;
  }

  TemperatureType hidl_type;
  status_t res =
      hidl_thermal_utils::ConvertToHidlTemperatureType(type, &hidl_type);
  if (res != OK) {
    ALOGE("%s: Converting to HIDL type failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return res;
  }

  std::unique_ptr<hidl_thermal_utils::HidlThermalChangedCallback> callback =
      hidl_thermal_utils::HidlThermalChangedCallback::Create(notify_throttling);
  thermal_changed_callback_ = callback.release();
  ThermalStatus thermal_status;
  auto hidl_res = thermal_->registerThermalChangedCallback(
      thermal_changed_callback_, filter_type, hidl_type,
      [&](ThermalStatus status) { thermal_status = status; });
  if (!hidl_res.isOk() || thermal_status.code != ThermalStatusCode::SUCCESS) {
    thermal_changed_callback_ = nullptr;
    return UNKNOWN_ERROR;
  }

  return OK;
}

void HidlCameraDeviceSession::UnregisterThermalChangedCallback() {
  std::lock_guard<std::mutex> lock(hidl_thermal_mutex_);
  if (thermal_changed_callback_ == nullptr) {
    // no-op if no thermal changed callback is registered.
    return;
  }

  if (thermal_ == nullptr) {
    ALOGE("%s: thermal was not initialized.", __FUNCTION__);
    return;
  }

  ThermalStatus thermal_status;
  auto hidl_res = thermal_->unregisterThermalChangedCallback(
      thermal_changed_callback_,
      [&](ThermalStatus status) { thermal_status = status; });
  if (!hidl_res.isOk() || thermal_status.code != ThermalStatusCode::SUCCESS) {
    ALOGW("%s: Unregstering thermal callback failed: %s", __FUNCTION__,
          thermal_status.debugMessage.c_str());
  }

  thermal_changed_callback_ = nullptr;
}

status_t HidlCameraDeviceSession::CreateMetadataQueue(
    std::unique_ptr<MetadataQueue>* metadata_queue, uint32_t default_size_bytes,
    const char* override_size_property) {
  if (metadata_queue == nullptr) {
    ALOGE("%s: metadata_queue is nullptr", __FUNCTION__);
    return BAD_VALUE;
  }

  int32_t size = default_size_bytes;
  if (override_size_property != nullptr) {
    // Try to read the override size from the system property.
    size = property_get_int32(override_size_property, default_size_bytes);
    ALOGV("%s: request metadata queue size overridden to %d", __FUNCTION__,
          size);
  }

  *metadata_queue = std::make_unique<MetadataQueue>(
      static_cast<size_t>(size), /*configureEventFlagWord=*/false);
  if (!(*metadata_queue)->isValid()) {
    ALOGE("%s: Creating metadata queue (size %d) failed.", __FUNCTION__, size);
    return NO_INIT;
  }

  return OK;
}

Return<void> HidlCameraDeviceSession::constructDefaultRequestSettings(
    RequestTemplate type,
    ICameraDeviceSession::constructDefaultRequestSettings_cb _hidl_cb) {
  ATRACE_NAME("HidlCameraDeviceSession::constructDefaultRequestSettings");
  V3_2::CameraMetadata hidl_metadata;

  if (device_session_ == nullptr) {
    _hidl_cb(Status::INTERNAL_ERROR, hidl_metadata);
    return Void();
  }

  google_camera_hal::RequestTemplate hal_type;
  status_t res = hidl_utils::ConvertToHalTemplateType(type, &hal_type);
  if (res != OK) {
    _hidl_cb(Status::ILLEGAL_ARGUMENT, hidl_metadata);
    return Void();
  }

  std::unique_ptr<google_camera_hal::HalCameraMetadata> settings = nullptr;
  res = device_session_->ConstructDefaultRequestSettings(hal_type, &settings);
  if (res != OK) {
    _hidl_cb(hidl_utils::ConvertToHidlStatus(res), hidl_metadata);
    return Void();
  }

  uint32_t metadata_size = settings->GetCameraMetadataSize();
  hidl_metadata.setToExternal((uint8_t*)settings->ReleaseCameraMetadata(),
                              metadata_size, /*shouldOwn=*/true);
  _hidl_cb(Status::OK, hidl_metadata);

  return Void();
}

Return<void> HidlCameraDeviceSession::configureStreams_3_7(
    const StreamConfiguration& requestedConfiguration,
    ICameraDeviceSession::configureStreams_3_6_cb _hidl_cb) {
  ATRACE_NAME("HidlCameraDeviceSession::configureStreams_3_7");
  HalStreamConfiguration hidl_hal_configs;
  if (device_session_ == nullptr) {
    _hidl_cb(Status::ILLEGAL_ARGUMENT, hidl_hal_configs);
    return Void();
  }

  auto profiler = hidl_profiler_->MakeScopedProfiler(
      HidlProfiler::ScopedType::kConfigureStream);

  first_frame_requested_ = false;
  num_pending_first_frame_buffers_ = 0;

  google_camera_hal::StreamConfiguration hal_stream_config;
  status_t res = hidl_utils::ConverToHalStreamConfig(requestedConfiguration,
                                                     &hal_stream_config);
  if (res != OK) {
    _hidl_cb(Status::ILLEGAL_ARGUMENT, hidl_hal_configs);
    return Void();
  }

  std::vector<google_camera_hal::HalStream> hal_configured_streams;
  res = device_session_->ConfigureStreams(hal_stream_config,
                                          &hal_configured_streams);
  if (res != OK) {
    ALOGE("%s: Configuring streams failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    _hidl_cb(hidl_utils::ConvertToHidlStatus(res), hidl_hal_configs);
    return Void();
  }

  res = hidl_utils::ConvertToHidlHalStreamConfig(hal_configured_streams,
                                                 &hidl_hal_configs);
  _hidl_cb(hidl_utils::ConvertToHidlStatus(res), hidl_hal_configs);

  return Void();
}

Return<void> HidlCameraDeviceSession::getCaptureRequestMetadataQueue(
    ICameraDeviceSession::getCaptureRequestMetadataQueue_cb _hidl_cb) {
  _hidl_cb(*request_metadata_queue_->getDesc());
  return Void();
}

Return<void> HidlCameraDeviceSession::getCaptureResultMetadataQueue(
    ICameraDeviceSession::getCaptureResultMetadataQueue_cb _hidl_cb) {
  _hidl_cb(*result_metadata_queue_->getDesc());
  return Void();
}

Return<void> HidlCameraDeviceSession::processCaptureRequest_3_7(
    const hidl_vec<CaptureRequest>& requests,
    const hidl_vec<BufferCache>& cachesToRemove,
    processCaptureRequest_3_7_cb _hidl_cb) {
  if (device_session_ == nullptr) {
    _hidl_cb(Status::ILLEGAL_ARGUMENT, 0);
    return Void();
  }

  bool profile_first_request = false;
  if (!first_frame_requested_) {
    first_frame_requested_ = true;
    profile_first_request = true;
    ATRACE_BEGIN("HidlCameraDeviceSession::FirstRequest");
    num_pending_first_frame_buffers_ =
        requests[0].v3_4.v3_2.outputBuffers.size();
    first_request_frame_number_ = requests[0].v3_4.v3_2.frameNumber;
    hidl_profiler_->FirstFrameStart();
    ATRACE_ASYNC_BEGIN("first_frame", 0);
  }

  std::vector<google_camera_hal::BufferCache> hal_buffer_caches;

  status_t res =
      hidl_utils::ConvertToHalBufferCaches(cachesToRemove, &hal_buffer_caches);
  if (res != OK) {
    _hidl_cb(Status::ILLEGAL_ARGUMENT, 0);
    if (profile_first_request) {
      ATRACE_END();
    }
    return Void();
  }

  device_session_->RemoveBufferCache(hal_buffer_caches);

  // Converting HIDL requests to HAL requests.
  std::vector<google_camera_hal::CaptureRequest> hal_requests;
  for (auto& request : requests) {
    google_camera_hal::CaptureRequest hal_request = {};
    res = hidl_utils::ConvertToHalCaptureRequest(
        request, request_metadata_queue_.get(), &hal_request);
    if (res != OK) {
      ALOGE("%s: Converting to HAL capture request failed: %s(%d)",
            __FUNCTION__, strerror(-res), res);
      _hidl_cb(hidl_utils::ConvertToHidlStatus(res), 0);
      if (profile_first_request) {
        ATRACE_END();
      }
      return Void();
    }

    hal_requests.push_back(std::move(hal_request));
  }

  uint32_t num_processed_requests = 0;
  res = device_session_->ProcessCaptureRequest(hal_requests,
                                               &num_processed_requests);
  if (res != OK) {
    ALOGE(
        "%s: Processing capture request failed: %s(%d). Only processed %u"
        " out of %zu.",
        __FUNCTION__, strerror(-res), res, num_processed_requests,
        hal_requests.size());
  }

  _hidl_cb(hidl_utils::ConvertToHidlStatus(res), num_processed_requests);
  if (profile_first_request) {
    ATRACE_END();
  }
  return Void();
}

Return<void> HidlCameraDeviceSession::signalStreamFlush(
    const hidl_vec<int32_t>& /*streamIds*/, uint32_t /*streamConfigCounter*/) {
  // TODO(b/143902312): Implement this.
  return Void();
}

Return<Status> HidlCameraDeviceSession::flush() {
  ATRACE_NAME("HidlCameraDeviceSession::flush");
  ATRACE_ASYNC_BEGIN("switch_mode", 0);
  if (device_session_ == nullptr) {
    return Status::INTERNAL_ERROR;
  }

  hidl_profiler_->SetLatencyProfiler(device_session_->GetProfiler(
      hidl_profiler_->GetCameraId(), hidl_profiler_->GetLatencyFlag()));
  hidl_profiler_->SetFpsProfiler(device_session_->GetProfiler(
      hidl_profiler_->GetCameraId(), hidl_profiler_->GetFpsFlag()));
  auto profiler =
      hidl_profiler_->MakeScopedProfiler(HidlProfiler::ScopedType::kFlush);

  status_t res = device_session_->Flush();
  if (res != OK) {
    ALOGE("%s: Flushing device failed: %s(%d).", __FUNCTION__, strerror(-res),
          res);
    return Status::INTERNAL_ERROR;
  }

  return Status::OK;
}

Return<void> HidlCameraDeviceSession::close() {
  ATRACE_NAME("HidlCameraDeviceSession::close");
  if (device_session_ != nullptr) {
    auto profiler =
        hidl_profiler_->MakeScopedProfiler(HidlProfiler::ScopedType::kClose);
    device_session_ = nullptr;
  }
  return Void();
}

Return<void> HidlCameraDeviceSession::isReconfigurationRequired(
    const V3_2::CameraMetadata& oldSessionParams,
    const V3_2::CameraMetadata& newSessionParams,
    ICameraDeviceSession::isReconfigurationRequired_cb _hidl_cb) {
  ATRACE_NAME("HidlCameraDeviceSession::isReconfigurationRequired");
  std::unique_ptr<google_camera_hal::HalCameraMetadata> old_hal_session_metadata;
  status_t res = hidl_utils::ConvertToHalMetadata(0, nullptr, oldSessionParams,
                                                  &old_hal_session_metadata);
  if (res != OK) {
    ALOGE("%s: Converting to old session metadata failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    _hidl_cb(Status::INTERNAL_ERROR, true);
    return Void();
  }

  std::unique_ptr<google_camera_hal::HalCameraMetadata> new_hal_session_metadata;
  res = hidl_utils::ConvertToHalMetadata(0, nullptr, newSessionParams,
                                         &new_hal_session_metadata);
  if (res != OK) {
    ALOGE("%s: Converting to new session metadata failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    _hidl_cb(Status::INTERNAL_ERROR, true);
    return Void();
  }

  bool reconfiguration_required = true;
  res = device_session_->IsReconfigurationRequired(
      old_hal_session_metadata.get(), new_hal_session_metadata.get(),
      &reconfiguration_required);

  if (res != OK) {
    ALOGE("%s: IsReconfigurationRequired failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    _hidl_cb(Status::INTERNAL_ERROR, true);
    return Void();
  }

  _hidl_cb(Status::OK, reconfiguration_required);
  return Void();
}

Return<void> HidlCameraDeviceSession::configureStreams(
    const V3_2::StreamConfiguration&, configureStreams_cb _hidl_cb) {
  _hidl_cb(Status::ILLEGAL_ARGUMENT, V3_2::HalStreamConfiguration());
  return Void();
}
Return<void> HidlCameraDeviceSession::configureStreams_3_3(
    const V3_2::StreamConfiguration&, configureStreams_3_3_cb _hidl_cb) {
  _hidl_cb(Status::ILLEGAL_ARGUMENT, V3_3::HalStreamConfiguration());
  return Void();
}
Return<void> HidlCameraDeviceSession::configureStreams_3_4(
    const V3_4::StreamConfiguration&, configureStreams_3_4_cb _hidl_cb) {
  _hidl_cb(Status::ILLEGAL_ARGUMENT, V3_4::HalStreamConfiguration());
  return Void();
}

Return<void> HidlCameraDeviceSession::configureStreams_3_5(
    const V3_5::StreamConfiguration& requestedConfiguration,
    configureStreams_3_5_cb _hidl_cb) {
  configureStreams_3_6(
      requestedConfiguration,
      [_hidl_cb](Status s, device::V3_6::HalStreamConfiguration halConfig) {
        V3_4::HalStreamConfiguration halConfig3_4;
        halConfig3_4.streams.resize(halConfig.streams.size());
        for (size_t i = 0; i < halConfig.streams.size(); i++) {
          halConfig3_4.streams[i] = halConfig.streams[i].v3_4;
        }
        _hidl_cb(s, halConfig3_4);
      });

  return Void();
}

Return<void> HidlCameraDeviceSession::configureStreams_3_6(
    const V3_5::StreamConfiguration& requestedConfiguration,
    configureStreams_3_6_cb _hidl_cb) {
  StreamConfiguration requestedConfiguration3_7;
  requestedConfiguration3_7.streams.resize(
      requestedConfiguration.v3_4.streams.size());
  for (size_t i = 0; i < requestedConfiguration.v3_4.streams.size(); i++) {
    requestedConfiguration3_7.streams[i].v3_4 =
        requestedConfiguration.v3_4.streams[i];
    requestedConfiguration3_7.streams[i].groupId = -1;
  }
  requestedConfiguration3_7.operationMode =
      requestedConfiguration.v3_4.operationMode;
  requestedConfiguration3_7.sessionParams =
      requestedConfiguration.v3_4.sessionParams;
  requestedConfiguration3_7.streamConfigCounter =
      requestedConfiguration.streamConfigCounter;
  requestedConfiguration3_7.multiResolutionInputImage = false;

  configureStreams_3_7(requestedConfiguration3_7, _hidl_cb);
  return Void();
}

Return<void> HidlCameraDeviceSession::switchToOffline(
    const hidl_vec<int32_t>&, switchToOffline_cb _hidl_cb) {
  _hidl_cb(Status::ILLEGAL_ARGUMENT, V3_6::CameraOfflineSessionInfo(), nullptr);
  return Void();
}

Return<void> HidlCameraDeviceSession::processCaptureRequest(
    const hidl_vec<V3_2::CaptureRequest>& requests,
    const hidl_vec<BufferCache>& cachesToRemove,
    processCaptureRequest_cb _hidl_cb) {
  hidl_vec<V3_4::CaptureRequest> requests_3_4;
  requests_3_4.resize(requests.size());
  for (uint32_t i = 0; i < requests_3_4.size(); i++) {
    requests_3_4[i].v3_2 = requests[i];
  }

  return processCaptureRequest_3_4(requests_3_4, cachesToRemove, _hidl_cb);
}

Return<void> HidlCameraDeviceSession::processCaptureRequest_3_4(
    const hidl_vec<V3_4::CaptureRequest>& requests,
    const hidl_vec<BufferCache>& cachesToRemove,
    processCaptureRequest_cb _hidl_cb) {
  hidl_vec<V3_7::CaptureRequest> requests_3_7;
  requests_3_7.resize(requests.size());
  for (uint32_t i = 0; i < requests_3_7.size(); i++) {
    requests_3_7[i].v3_4 = requests[i];
  }

  return processCaptureRequest_3_7(requests_3_7, cachesToRemove, _hidl_cb);
}

}  // namespace implementation
}  // namespace V3_7
}  // namespace device
}  // namespace camera
}  // namespace hardware
}  // namespace android