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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "GCH_CameraProvider"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 #include "camera_provider.h"
21 
22 #include <dlfcn.h>
23 #include <log/log.h>
24 #include <utils/Trace.h>
25 
26 #include "vendor_tag_defs.h"
27 #include "vendor_tag_utils.h"
28 
29 // HWL layer implementation path
30 #if defined(_LP64)
31 std::string kCameraHwlLib = "/vendor/lib64/libgooglecamerahwl_impl.so";
32 #else  // defined(_LP64)
33 std::string kCameraHwlLib = "/vendor/lib/libgooglecamerahwl_impl.so";
34 #endif
35 
36 namespace android {
37 namespace google_camera_hal {
38 
~CameraProvider()39 CameraProvider::~CameraProvider() {
40   VendorTagManager::GetInstance().Reset();
41   if (hwl_lib_handle_ != nullptr) {
42     dlclose(hwl_lib_handle_);
43     hwl_lib_handle_ = nullptr;
44   }
45 }
46 
Create(std::unique_ptr<CameraProviderHwl> camera_provider_hwl)47 std::unique_ptr<CameraProvider> CameraProvider::Create(
48     std::unique_ptr<CameraProviderHwl> camera_provider_hwl) {
49   ATRACE_CALL();
50   auto provider = std::unique_ptr<CameraProvider>(new CameraProvider());
51   if (provider == nullptr) {
52     ALOGE("%s: Creating CameraProvider failed.", __FUNCTION__);
53     return nullptr;
54   }
55 
56   status_t res = provider->Initialize(std::move(camera_provider_hwl));
57   if (res != OK) {
58     ALOGE("%s: Initializing CameraProvider failed: %s (%d).", __FUNCTION__,
59           strerror(-res), res);
60     return nullptr;
61   }
62 
63   return provider;
64 }
65 
Initialize(std::unique_ptr<CameraProviderHwl> camera_provider_hwl)66 status_t CameraProvider::Initialize(
67     std::unique_ptr<CameraProviderHwl> camera_provider_hwl) {
68   ATRACE_CALL();
69   // Advertise the HAL vendor tags to the camera metadata framework before
70   // creating a HWL provider.
71   status_t res = VendorTagManager::GetInstance().AddTags(kHalVendorTagSections);
72   if (res != OK) {
73     ALOGE("%s: Initializing VendorTagManager failed: %s(%d)", __FUNCTION__,
74           strerror(-res), res);
75     return res;
76   }
77 
78   if (camera_provider_hwl == nullptr) {
79     status_t res = CreateHwl(&camera_provider_hwl);
80     if (res != OK) {
81       ALOGE("%s: Creating CameraProviderHwlImpl failed.", __FUNCTION__);
82       return NO_INIT;
83     }
84   }
85 
86   res = camera_provider_hwl->CreateBufferAllocatorHwl(&camera_allocator_hwl_);
87   if (res == INVALID_OPERATION) {
88     camera_allocator_hwl_ = nullptr;
89     ALOGE("%s: HWL doesn't support vendor buffer allocation %s(%d)",
90           __FUNCTION__, strerror(-res), res);
91   } else if (res != OK) {
92     camera_allocator_hwl_ = nullptr;
93     ALOGE("%s: Creating CameraBufferAllocatorHwl failed: %s(%d)", __FUNCTION__,
94           strerror(-res), res);
95     return NO_INIT;
96   }
97 
98   camera_provider_hwl_ = std::move(camera_provider_hwl);
99   res = InitializeVendorTags();
100   if (res != OK) {
101     ALOGE("%s InitailizeVendorTags() failed: %s (%d).", __FUNCTION__,
102           strerror(-res), res);
103     camera_provider_hwl_ = nullptr;
104     return res;
105   }
106 
107   return OK;
108 }
109 
InitializeVendorTags()110 status_t CameraProvider::InitializeVendorTags() {
111   std::vector<VendorTagSection> hwl_tag_sections;
112   status_t res = camera_provider_hwl_->GetVendorTags(&hwl_tag_sections);
113   if (res != OK) {
114     ALOGE("%s: GetVendorTags() for HWL tags failed: %s(%d)", __FUNCTION__,
115           strerror(-res), res);
116     return res;
117   }
118 
119   // Combine HAL and HWL vendor tag sections
120   res = vendor_tag_utils::CombineVendorTags(
121       kHalVendorTagSections, hwl_tag_sections, &vendor_tag_sections_);
122   if (res != OK) {
123     ALOGE("%s: CombineVendorTags() failed: %s(%d)", __FUNCTION__,
124           strerror(-res), res);
125     return res;
126   }
127 
128   return OK;
129 }
130 
SetCallback(const CameraProviderCallback * callback)131 status_t CameraProvider::SetCallback(const CameraProviderCallback* callback) {
132   ATRACE_CALL();
133   if (callback == nullptr) {
134     return BAD_VALUE;
135   }
136 
137   provider_callback_ = callback;
138   if (camera_provider_hwl_ == nullptr) {
139     ALOGE("%s: Camera provider HWL was not initialized to set callback.",
140           __FUNCTION__);
141     return NO_INIT;
142   }
143 
144   hwl_provider_callback_.camera_device_status_change =
145       HwlCameraDeviceStatusChangeFunc(
146           [this](uint32_t camera_id, CameraDeviceStatus new_status) {
147             provider_callback_->camera_device_status_change(
148                 std::to_string(camera_id), new_status);
149           });
150 
151   hwl_provider_callback_.physical_camera_device_status_change =
152       HwlPhysicalCameraDeviceStatusChangeFunc(
153           [this](uint32_t camera_id, uint32_t physical_camera_id,
154                  CameraDeviceStatus new_status) {
155             provider_callback_->physical_camera_device_status_change(
156                 std::to_string(camera_id), std::to_string(physical_camera_id),
157                 new_status);
158           });
159 
160   hwl_provider_callback_.torch_mode_status_change = HwlTorchModeStatusChangeFunc(
161       [this](uint32_t camera_id, TorchModeStatus new_status) {
162         provider_callback_->torch_mode_status_change(std::to_string(camera_id),
163                                                      new_status);
164       });
165 
166   camera_provider_hwl_->SetCallback(hwl_provider_callback_);
167   return OK;
168 }
169 
TriggerDeferredCallbacks()170 status_t CameraProvider::TriggerDeferredCallbacks() {
171   ATRACE_CALL();
172   return camera_provider_hwl_->TriggerDeferredCallbacks();
173 }
174 
GetVendorTags(std::vector<VendorTagSection> * vendor_tag_sections) const175 status_t CameraProvider::GetVendorTags(
176     std::vector<VendorTagSection>* vendor_tag_sections) const {
177   ATRACE_CALL();
178   if (vendor_tag_sections == nullptr) {
179     return BAD_VALUE;
180   }
181 
182   if (camera_provider_hwl_ == nullptr) {
183     ALOGE("%s: Camera provider HWL was not initialized.", __FUNCTION__);
184     return NO_INIT;
185   }
186 
187   *vendor_tag_sections = vendor_tag_sections_;
188   return OK;
189 }
190 
GetCameraIdList(std::vector<uint32_t> * camera_ids) const191 status_t CameraProvider::GetCameraIdList(std::vector<uint32_t>* camera_ids) const {
192   ATRACE_CALL();
193   if (camera_ids == nullptr) {
194     ALOGE("%s: camera_ids is nullptr", __FUNCTION__);
195     return BAD_VALUE;
196   }
197 
198   status_t res = camera_provider_hwl_->GetVisibleCameraIds(camera_ids);
199   if (res != OK) {
200     ALOGE("%s: failed to get visible camera IDs from the camera provider",
201           __FUNCTION__);
202     return res;
203   }
204   return OK;
205 }
206 
IsSetTorchModeSupported() const207 bool CameraProvider::IsSetTorchModeSupported() const {
208   ATRACE_CALL();
209   if (camera_provider_hwl_ == nullptr) {
210     ALOGE("%s: Camera provider HWL was not initialized.", __FUNCTION__);
211     return NO_INIT;
212   }
213 
214   return camera_provider_hwl_->IsSetTorchModeSupported();
215 }
216 
IsConcurrentStreamCombinationSupported(const std::vector<CameraIdAndStreamConfiguration> & configs,bool * is_supported)217 status_t CameraProvider::IsConcurrentStreamCombinationSupported(
218     const std::vector<CameraIdAndStreamConfiguration>& configs,
219     bool* is_supported) {
220   ATRACE_CALL();
221   if (camera_provider_hwl_ == nullptr) {
222     ALOGE("%s: Camera provider HWL was not initialized.", __FUNCTION__);
223     return NO_INIT;
224   }
225   return camera_provider_hwl_->IsConcurrentStreamCombinationSupported(
226       configs, is_supported);
227 }
228 
229 // Get the combinations of camera ids which support concurrent streaming
GetConcurrentStreamingCameraIds(std::vector<std::unordered_set<uint32_t>> * camera_id_combinations)230 status_t CameraProvider::GetConcurrentStreamingCameraIds(
231     std::vector<std::unordered_set<uint32_t>>* camera_id_combinations) {
232   if (camera_id_combinations == nullptr) {
233     return BAD_VALUE;
234   }
235 
236   ATRACE_CALL();
237   if (camera_provider_hwl_ == nullptr) {
238     ALOGE("%s: Camera provider HWL was not initialized.", __FUNCTION__);
239     return NO_INIT;
240   }
241 
242   return camera_provider_hwl_->GetConcurrentStreamingCameraIds(
243       camera_id_combinations);
244 }
245 
CreateCameraDevice(uint32_t camera_id,std::unique_ptr<CameraDevice> * device)246 status_t CameraProvider::CreateCameraDevice(
247     uint32_t camera_id, std::unique_ptr<CameraDevice>* device) {
248   ATRACE_CALL();
249   std::vector<uint32_t> camera_ids;
250   if (device == nullptr) {
251     return BAD_VALUE;
252   }
253 
254   if (camera_provider_hwl_ == nullptr) {
255     ALOGE("%s: Camera provider HWL was not initialized.", __FUNCTION__);
256     return NO_INIT;
257   }
258 
259   // Check camera_id is valid.
260   status_t res = camera_provider_hwl_->GetVisibleCameraIds(&camera_ids);
261   if (res != OK) {
262     ALOGE("%s: failed to get visible camera IDs from the camera provider",
263           __FUNCTION__);
264     return res;
265   }
266 
267   if (std::find(camera_ids.begin(), camera_ids.end(), camera_id) ==
268       camera_ids.end()) {
269     ALOGE("%s: camera_id: %u  invalid.", __FUNCTION__, camera_id);
270     return BAD_VALUE;
271   }
272 
273   std::unique_ptr<CameraDeviceHwl> camera_device_hwl;
274   res = camera_provider_hwl_->CreateCameraDeviceHwl(camera_id,
275                                                     &camera_device_hwl);
276   if (res != OK) {
277     ALOGE("%s: Creating CameraProviderHwl failed: %s(%d)", __FUNCTION__,
278           strerror(-res), res);
279     return res;
280   }
281 
282   const std::vector<std::string>* configure_streams_libs = nullptr;
283 
284 #if GCH_HWL_USE_DLOPEN
285   configure_streams_libs = reinterpret_cast<decltype(configure_streams_libs)>(
286       dlsym(hwl_lib_handle_, "configure_streams_libraries"));
287 #endif
288   *device =
289       CameraDevice::Create(std::move(camera_device_hwl),
290                            camera_allocator_hwl_.get(), configure_streams_libs);
291   if (*device == nullptr) {
292     return NO_INIT;
293   }
294 
295   return OK;
296 }
297 
CreateHwl(std::unique_ptr<CameraProviderHwl> * camera_provider_hwl)298 status_t CameraProvider::CreateHwl(
299     std::unique_ptr<CameraProviderHwl>* camera_provider_hwl) {
300   ATRACE_CALL();
301 #if GCH_HWL_USE_DLOPEN
302   CreateCameraProviderHwl_t create_hwl;
303 
304   ALOGI("%s:Loading %s library", __FUNCTION__, kCameraHwlLib.c_str());
305   hwl_lib_handle_ = dlopen(kCameraHwlLib.c_str(), RTLD_NOW);
306 
307   if (hwl_lib_handle_ == nullptr) {
308     ALOGE("HWL loading %s failed due to error: %s", kCameraHwlLib.c_str(),
309           dlerror());
310     return NO_INIT;
311   }
312 
313   create_hwl = (CreateCameraProviderHwl_t)dlsym(hwl_lib_handle_,
314                                                 "CreateCameraProviderHwl");
315   if (create_hwl == nullptr) {
316     ALOGE("%s: dlsym failed (%s).", __FUNCTION__, kCameraHwlLib.c_str());
317     dlclose(hwl_lib_handle_);
318     hwl_lib_handle_ = nullptr;
319     return NO_INIT;
320   }
321 
322   // Create the HWL camera provider
323   *camera_provider_hwl = std::unique_ptr<CameraProviderHwl>(create_hwl());
324   if (*camera_provider_hwl == nullptr) {
325     ALOGE("Error! Creating CameraProviderHwl failed");
326     return UNKNOWN_ERROR;
327   }
328 #else
329   *camera_provider_hwl =
330       std::unique_ptr<CameraProviderHwl>(CreateCameraProviderHwl());
331 #endif
332 
333   return OK;
334 }
335 
NotifyDeviceStateChange(google_camera_hal::DeviceState device_state)336 status_t CameraProvider::NotifyDeviceStateChange(
337     google_camera_hal::DeviceState device_state) {
338   if (camera_provider_hwl_ == nullptr) {
339     ALOGE("%s: null provider hwl", __FUNCTION__);
340     return NO_INIT;
341   }
342   camera_provider_hwl_->NotifyDeviceStateChange(device_state);
343   return OK;
344 }
345 }  // namespace google_camera_hal
346 }  // namespace android
347