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::HalCameraMetadata;
34 using google_camera_hal::HwlCameraProviderCallback;
35 using google_camera_hal::HwlPhysicalCameraDeviceStatusChangeFunc;
36 using google_camera_hal::HwlTorchModeStatusChangeFunc;
37 using google_camera_hal::VendorTagSection;
38 
39 class EmulatedCameraProviderHwlImpl : public CameraProviderHwl {
40  public:
41   // Return a unique pointer to EmulatedCameraProviderHwlImpl. Calling Create()
42   // again before the previous one is destroyed will fail.
43   static std::unique_ptr<EmulatedCameraProviderHwlImpl> Create();
44 
~EmulatedCameraProviderHwlImpl()45   virtual ~EmulatedCameraProviderHwlImpl() {
46     WaitForStatusCallbackFuture();
47   }
48 
49   // Override functions in CameraProviderHwl.
50   status_t SetCallback(const HwlCameraProviderCallback& callback) override;
51   status_t TriggerDeferredCallbacks() override;
52 
53   status_t GetVendorTags(
54       std::vector<VendorTagSection>* vendor_tag_sections) override;
55 
56   status_t GetVisibleCameraIds(std::vector<std::uint32_t>* camera_ids) override;
57 
IsSetTorchModeSupported()58   bool IsSetTorchModeSupported() override {
59     return true;
60   }
61 
62   status_t GetConcurrentStreamingCameraIds(
63       std::vector<std::unordered_set<uint32_t>>*) override;
64 
65   status_t IsConcurrentStreamCombinationSupported(
66       const std::vector<CameraIdAndStreamConfiguration>&, bool*) override;
67 
68   status_t CreateCameraDeviceHwl(
69       uint32_t camera_id,
70       std::unique_ptr<CameraDeviceHwl>* camera_device_hwl) override;
71 
72   status_t CreateBufferAllocatorHwl(std::unique_ptr<CameraBufferAllocatorHwl>*
73                                         camera_buffer_allocator_hwl) override;
74   // End of override functions in CameraProviderHwl.
75 
76  private:
77   status_t Initialize();
78   uint32_t ParseCharacteristics(const Json::Value& root, ssize_t id);
79   status_t GetTagFromName(const char* name, uint32_t* tag);
80   status_t WaitForQemuSfFakeCameraPropertyAvailable();
81   bool SupportsMandatoryConcurrentStreams(uint32_t camera_id);
82 
83   static const char* kConfigurationFileLocation[];
84 
85   std::vector<std::unique_ptr<HalCameraMetadata>> static_metadata_;
86   // Logical to physical camera Id mapping. Empty value vector in case
87   // of regular non-logical device.
88   std::unordered_map<uint32_t, std::vector<std::pair<CameraDeviceStatus, uint32_t>>> camera_id_map_;
89   HwlTorchModeStatusChangeFunc torch_cb_;
90   HwlPhysicalCameraDeviceStatusChangeFunc physical_camera_status_cb_;
91 
92   std::mutex status_callback_future_lock_;
93   std::future<void> status_callback_future_;
94   void WaitForStatusCallbackFuture();
95   void NotifyPhysicalCameraUnavailable();
96 };
97 
CreateCameraProviderHwl()98 extern "C" CameraProviderHwl* CreateCameraProviderHwl() {
99   auto provider = EmulatedCameraProviderHwlImpl::Create();
100   return provider.release();
101 }
102 
103 }  // namespace android
104 
105 #endif  // EMULATOR_CAMERA_HAL_HWL_CAMERA_PROVIDER_HWL_H
106