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 <log/log.h>
21 #include <utils/Trace.h>
22 
23 #include <dlfcn.h>
24 
25 #include "camera_provider.h"
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 =
72       VendorTagManager::GetInstance().AddTags(kHalVendorTagSections);
73   if (res != OK) {
74     ALOGE("%s: Initializing VendorTagManager failed: %s(%d)", __FUNCTION__,
75           strerror(-res), res);
76     return res;
77   }
78 
79   if (camera_provider_hwl == nullptr) {
80     status_t res = CreateCameraProviderHwl(&camera_provider_hwl);
81     if (res != OK) {
82       ALOGE("%s: Creating CameraProviderHwlImpl failed.", __FUNCTION__);
83       return NO_INIT;
84     }
85   }
86 
87   res = camera_provider_hwl->CreateBufferAllocatorHwl(&camera_allocator_hwl_);
88   if (res == INVALID_OPERATION) {
89     camera_allocator_hwl_ = nullptr;
90     ALOGE("%s: HWL doesn't support vendor buffer allocation %s(%d)",
91           __FUNCTION__, strerror(-res), res);
92   } else if (res != OK) {
93     camera_allocator_hwl_ = nullptr;
94     ALOGE("%s: Creating CameraBufferAllocatorHwl failed: %s(%d)", __FUNCTION__,
95           strerror(-res), res);
96     return NO_INIT;
97   }
98 
99   camera_provider_hwl_ = std::move(camera_provider_hwl);
100   res = InitializeVendorTags();
101   if (res != OK) {
102     ALOGE("%s InitailizeVendorTags() failed: %s (%d).", __FUNCTION__,
103           strerror(-res), res);
104     camera_provider_hwl_ = nullptr;
105     return res;
106   }
107 
108   return OK;
109 }
110 
InitializeVendorTags()111 status_t CameraProvider::InitializeVendorTags() {
112   std::vector<VendorTagSection> hwl_tag_sections;
113   status_t res = camera_provider_hwl_->GetVendorTags(&hwl_tag_sections);
114   if (res != OK) {
115     ALOGE("%s: GetVendorTags() for HWL tags failed: %s(%d)", __FUNCTION__,
116           strerror(-res), res);
117     return res;
118   }
119 
120   // Combine HAL and HWL vendor tag sections
121   res = vendor_tag_utils::CombineVendorTags(
122       kHalVendorTagSections, hwl_tag_sections, &vendor_tag_sections_);
123   if (res != OK) {
124     ALOGE("%s: CombineVendorTags() failed: %s(%d)", __FUNCTION__,
125           strerror(-res), res);
126     return res;
127   }
128 
129   return OK;
130 }
131 
SetCallback(const CameraProviderCallback * callback)132 status_t CameraProvider::SetCallback(const CameraProviderCallback* callback) {
133   ATRACE_CALL();
134   if (callback == nullptr) {
135     return BAD_VALUE;
136   }
137 
138   provider_callback_ = callback;
139   if (camera_provider_hwl_ == nullptr) {
140     ALOGE("%s: Camera provider HWL was not initialized to set callback.",
141           __FUNCTION__);
142     return NO_INIT;
143   }
144 
145   hwl_provider_callback_.camera_device_status_change =
146       HwlCameraDeviceStatusChangeFunc(
147           [this](uint32_t camera_id, CameraDeviceStatus new_status) {
148             provider_callback_->camera_device_status_change(
149                 std::to_string(camera_id), new_status);
150           });
151 
152   hwl_provider_callback_.physical_camera_device_status_change =
153       HwlPhysicalCameraDeviceStatusChangeFunc(
154           [this](uint32_t camera_id, uint32_t physical_camera_id,
155                  CameraDeviceStatus new_status) {
156             provider_callback_->physical_camera_device_status_change(
157                 std::to_string(camera_id), std::to_string(physical_camera_id),
158                 new_status);
159           });
160 
161   hwl_provider_callback_.torch_mode_status_change = HwlTorchModeStatusChangeFunc(
162       [this](uint32_t camera_id, TorchModeStatus new_status) {
163         provider_callback_->torch_mode_status_change(std::to_string(camera_id),
164                                                      new_status);
165       });
166 
167   camera_provider_hwl_->SetCallback(hwl_provider_callback_);
168   return OK;
169 }
170 
TriggerDeferredCallbacks()171 status_t CameraProvider::TriggerDeferredCallbacks() {
172   ATRACE_CALL();
173   return camera_provider_hwl_->TriggerDeferredCallbacks();
174 }
175 
GetVendorTags(std::vector<VendorTagSection> * vendor_tag_sections) const176 status_t CameraProvider::GetVendorTags(
177     std::vector<VendorTagSection>* vendor_tag_sections) const {
178   ATRACE_CALL();
179   if (vendor_tag_sections == nullptr) {
180     return BAD_VALUE;
181   }
182 
183   if (camera_provider_hwl_ == nullptr) {
184     ALOGE("%s: Camera provider HWL was not initialized.", __FUNCTION__);
185     return NO_INIT;
186   }
187 
188   *vendor_tag_sections = vendor_tag_sections_;
189   return OK;
190 }
191 
GetCameraIdList(std::vector<uint32_t> * camera_ids) const192 status_t CameraProvider::GetCameraIdList(std::vector<uint32_t>* camera_ids) const {
193   ATRACE_CALL();
194   if (camera_ids == nullptr) {
195     ALOGE("%s: camera_ids is nullptr", __FUNCTION__);
196     return BAD_VALUE;
197   }
198 
199   status_t res = camera_provider_hwl_->GetVisibleCameraIds(camera_ids);
200   if (res != OK) {
201     ALOGE("%s: failed to get visible camera IDs from the camera provider",
202           __FUNCTION__);
203     return res;
204   }
205   return OK;
206 }
207 
IsSetTorchModeSupported() const208 bool CameraProvider::IsSetTorchModeSupported() const {
209   ATRACE_CALL();
210   if (camera_provider_hwl_ == nullptr) {
211     ALOGE("%s: Camera provider HWL was not initialized.", __FUNCTION__);
212     return NO_INIT;
213   }
214 
215   return camera_provider_hwl_->IsSetTorchModeSupported();
216 }
217 
IsConcurrentStreamCombinationSupported(const std::vector<CameraIdAndStreamConfiguration> & configs,bool * is_supported)218 status_t CameraProvider::IsConcurrentStreamCombinationSupported(
219     const std::vector<CameraIdAndStreamConfiguration>& configs,
220     bool* is_supported) {
221   ATRACE_CALL();
222   if (camera_provider_hwl_ == nullptr) {
223     ALOGE("%s: Camera provider HWL was not initialized.", __FUNCTION__);
224     return NO_INIT;
225   }
226   return camera_provider_hwl_->IsConcurrentStreamCombinationSupported(
227       configs, is_supported);
228 }
229 
230 // Get the combinations of camera ids which support concurrent streaming
GetConcurrentStreamingCameraIds(std::vector<std::unordered_set<uint32_t>> * camera_id_combinations)231 status_t CameraProvider::GetConcurrentStreamingCameraIds(
232     std::vector<std::unordered_set<uint32_t>>* camera_id_combinations) {
233   if (camera_id_combinations == nullptr) {
234     return BAD_VALUE;
235   }
236 
237   ATRACE_CALL();
238   if (camera_provider_hwl_ == nullptr) {
239     ALOGE("%s: Camera provider HWL was not initialized.", __FUNCTION__);
240     return NO_INIT;
241   }
242 
243   return camera_provider_hwl_->GetConcurrentStreamingCameraIds(
244       camera_id_combinations);
245 }
246 
CreateCameraDevice(uint32_t camera_id,std::unique_ptr<CameraDevice> * device)247 status_t CameraProvider::CreateCameraDevice(
248     uint32_t camera_id, std::unique_ptr<CameraDevice>* device) {
249   ATRACE_CALL();
250   std::vector<uint32_t> camera_ids;
251   if (device == nullptr) {
252     return BAD_VALUE;
253   }
254 
255   if (camera_provider_hwl_ == nullptr) {
256     ALOGE("%s: Camera provider HWL was not initialized.", __FUNCTION__);
257     return NO_INIT;
258   }
259 
260   // Check camera_id is valid.
261   status_t res = camera_provider_hwl_->GetVisibleCameraIds(&camera_ids);
262   if (res != OK) {
263     ALOGE("%s: failed to get visible camera IDs from the camera provider",
264           __FUNCTION__);
265     return res;
266   }
267 
268   if (std::find(camera_ids.begin(), camera_ids.end(), camera_id) == 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   *device = CameraDevice::Create(std::move(camera_device_hwl),
283                                  camera_allocator_hwl_.get());
284   if (*device == nullptr) {
285     return NO_INIT;
286   }
287 
288   return OK;
289 }
290 
CreateCameraProviderHwl(std::unique_ptr<CameraProviderHwl> * camera_provider_hwl)291 status_t CameraProvider::CreateCameraProviderHwl(
292     std::unique_ptr<CameraProviderHwl>* camera_provider_hwl) {
293   ATRACE_CALL();
294   CreateCameraProviderHwl_t create_hwl;
295 
296   ALOGI("%s:Loading %s library", __FUNCTION__, kCameraHwlLib.c_str());
297   hwl_lib_handle_ = dlopen(kCameraHwlLib.c_str(), RTLD_NOW);
298 
299   if (hwl_lib_handle_ == nullptr) {
300     ALOGE("HWL loading %s failed.", kCameraHwlLib.c_str());
301     return NO_INIT;
302   }
303 
304   create_hwl = (CreateCameraProviderHwl_t)dlsym(hwl_lib_handle_,
305                                                 "CreateCameraProviderHwl");
306   if (create_hwl == nullptr) {
307     ALOGE("%s: dlsym failed (%s).", __FUNCTION__, kCameraHwlLib.c_str());
308     dlclose(hwl_lib_handle_);
309     hwl_lib_handle_ = nullptr;
310     return NO_INIT;
311   }
312 
313   // Create the HWL camera provider
314   *camera_provider_hwl = std::unique_ptr<CameraProviderHwl>(create_hwl());
315   if (*camera_provider_hwl == nullptr) {
316     ALOGE("Error! Creating CameraProviderHwl failed");
317     return UNKNOWN_ERROR;
318   }
319 
320   return OK;
321 }
322 }  // namespace google_camera_hal
323 }  // namespace android
324