1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef EMULATOR_CAMERA_HAL_HWL_CAMERA_PROVIDER_HWL_H
18 #define EMULATOR_CAMERA_HAL_HWL_CAMERA_PROVIDER_HWL_H
19 
20 #include <camera_provider_hwl.h>
21 #include <hal_types.h>
22 #include <json/json.h>
23 #include <json/reader.h>
24 #include <future>
25 
26 namespace android {
27 
28 using google_camera_hal::CameraBufferAllocatorHwl;
29 using google_camera_hal::CameraDeviceHwl;
30 using google_camera_hal::CameraDeviceStatus;
31 using google_camera_hal::CameraIdAndStreamConfiguration;
32 using google_camera_hal::CameraProviderHwl;
33 using google_camera_hal::DeviceState;
34 using google_camera_hal::HalCameraMetadata;
35 using google_camera_hal::HwlCameraProviderCallback;
36 using google_camera_hal::HwlPhysicalCameraDeviceStatusChangeFunc;
37 using google_camera_hal::HwlTorchModeStatusChangeFunc;
38 using google_camera_hal::VendorTagSection;
39 
40 class EmulatedCameraProviderHwlImpl : public CameraProviderHwl {
41  public:
42   // Return a unique pointer to EmulatedCameraProviderHwlImpl. Calling Create()
43   // again before the previous one is destroyed will fail.
44   static std::unique_ptr<EmulatedCameraProviderHwlImpl> Create();
45 
~EmulatedCameraProviderHwlImpl()46   virtual ~EmulatedCameraProviderHwlImpl() {
47     WaitForStatusCallbackFuture();
48   }
49 
50   // Override functions in CameraProviderHwl.
51   status_t SetCallback(const HwlCameraProviderCallback& callback) override;
52   status_t TriggerDeferredCallbacks() override;
53 
54   status_t GetVendorTags(
55       std::vector<VendorTagSection>* vendor_tag_sections) override;
56 
57   status_t GetVisibleCameraIds(std::vector<std::uint32_t>* camera_ids) override;
58 
IsSetTorchModeSupported()59   bool IsSetTorchModeSupported() override {
60     return true;
61   }
62 
63   status_t GetConcurrentStreamingCameraIds(
64       std::vector<std::unordered_set<uint32_t>>*) override;
65 
66   status_t IsConcurrentStreamCombinationSupported(
67       const std::vector<CameraIdAndStreamConfiguration>&, bool*) override;
68 
69   status_t CreateCameraDeviceHwl(
70       uint32_t camera_id,
71       std::unique_ptr<CameraDeviceHwl>* camera_device_hwl) override;
72 
73   status_t CreateBufferAllocatorHwl(std::unique_ptr<CameraBufferAllocatorHwl>*
74                                         camera_buffer_allocator_hwl) override;
75 
76   status_t NotifyDeviceStateChange(DeviceState device_state) override;
77   // End of override functions in CameraProviderHwl.
78 
79  private:
80   status_t Initialize();
81   uint32_t ParseCharacteristics(const Json::Value& root, ssize_t id);
82   status_t GetTagFromName(const char* name, uint32_t* tag);
83   status_t WaitForQemuSfFakeCameraPropertyAvailable();
84   bool SupportsMandatoryConcurrentStreams(uint32_t camera_id);
85 
86   static const char* kConfigurationFileLocation[];
87 
88   std::vector<std::unique_ptr<HalCameraMetadata>> static_metadata_;
89   // Logical to physical camera Id mapping. Empty value vector in case
90   // of regular non-logical device.
91   std::unordered_map<uint32_t, std::vector<std::pair<CameraDeviceStatus, uint32_t>>> camera_id_map_;
92   HwlTorchModeStatusChangeFunc torch_cb_;
93   HwlPhysicalCameraDeviceStatusChangeFunc physical_camera_status_cb_;
94 
95   std::mutex status_callback_future_lock_;
96   std::future<void> status_callback_future_;
97   void WaitForStatusCallbackFuture();
98   void NotifyPhysicalCameraUnavailable();
99 };
100 
CreateCameraProviderHwl()101 extern "C" CameraProviderHwl* CreateCameraProviderHwl() {
102   auto provider = EmulatedCameraProviderHwlImpl::Create();
103   return provider.release();
104 }
105 
106 }  // namespace android
107 
108 #endif  // EMULATOR_CAMERA_HAL_HWL_CAMERA_PROVIDER_HWL_H
109