• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if !GCH_HWL_USE_DLOPEN
27 #include "lyric_hwl/madvise_library_list.h"
28 #endif
29 #include "vendor_tag_defs.h"
30 #include "vendor_tag_utils.h"
31 
32 #if GCH_HWL_USE_DLOPEN
33 // HWL layer implementation path
34 constexpr std::string_view kCameraHwlLib = "libgooglecamerahwl_impl.so";
35 #endif
36 
37 namespace android {
38 namespace google_camera_hal {
39 
~CameraProvider()40 CameraProvider::~CameraProvider() {
41   VendorTagManager::GetInstance().Reset();
42   if (hwl_lib_handle_ != nullptr) {
43     dlclose(hwl_lib_handle_);
44     hwl_lib_handle_ = nullptr;
45   }
46 }
47 
Create(std::unique_ptr<CameraProviderHwl> camera_provider_hwl)48 std::unique_ptr<CameraProvider> CameraProvider::Create(
49     std::unique_ptr<CameraProviderHwl> camera_provider_hwl) {
50   ATRACE_CALL();
51   auto provider = std::unique_ptr<CameraProvider>(new CameraProvider());
52   if (provider == nullptr) {
53     ALOGE("%s: Creating CameraProvider failed.", __FUNCTION__);
54     return nullptr;
55   }
56 
57   status_t res = provider->Initialize(std::move(camera_provider_hwl));
58   if (res != OK) {
59     ALOGE("%s: Initializing CameraProvider failed: %s (%d).", __FUNCTION__,
60           strerror(-res), res);
61     return nullptr;
62   }
63 
64   return provider;
65 }
66 
Initialize(std::unique_ptr<CameraProviderHwl> camera_provider_hwl)67 status_t CameraProvider::Initialize(
68     std::unique_ptr<CameraProviderHwl> camera_provider_hwl) {
69   ATRACE_CALL();
70   // Advertise the HAL vendor tags to the camera metadata framework before
71   // creating a HWL provider.
72   status_t res = 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 = CreateHwl(&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) ==
269       camera_ids.end()) {
270     ALOGE("%s: camera_id: %u  invalid.", __FUNCTION__, camera_id);
271     return BAD_VALUE;
272   }
273 
274   std::unique_ptr<CameraDeviceHwl> camera_device_hwl;
275   res = camera_provider_hwl_->CreateCameraDeviceHwl(camera_id,
276                                                     &camera_device_hwl);
277   if (res != OK) {
278     ALOGE("%s: Creating CameraProviderHwl failed: %s(%d)", __FUNCTION__,
279           strerror(-res), res);
280     return res;
281   }
282 
283   const std::vector<std::string>* configure_streams_libs = nullptr;
284 
285 #if GCH_HWL_USE_DLOPEN
286   configure_streams_libs = reinterpret_cast<decltype(configure_streams_libs)>(
287       dlsym(hwl_lib_handle_, "configure_streams_libraries"));
288 #else
289   configure_streams_libs = &configure_streams_libraries;
290 #endif
291   *device =
292       CameraDevice::Create(std::move(camera_device_hwl),
293                            camera_allocator_hwl_.get(), configure_streams_libs);
294   if (*device == nullptr) {
295     return NO_INIT;
296   }
297 
298   return OK;
299 }
300 
CreateHwl(std::unique_ptr<CameraProviderHwl> * camera_provider_hwl)301 status_t CameraProvider::CreateHwl(
302     std::unique_ptr<CameraProviderHwl>* camera_provider_hwl) {
303   ATRACE_CALL();
304 #if GCH_HWL_USE_DLOPEN
305   CreateCameraProviderHwl_t create_hwl;
306 
307   ALOGI("%s:Loading %s library", __FUNCTION__, kCameraHwlLib.data());
308   hwl_lib_handle_ = dlopen(kCameraHwlLib.data(), RTLD_NOW);
309 
310   if (hwl_lib_handle_ == nullptr) {
311     ALOGE("HWL loading %s failed due to error: %s", kCameraHwlLib.data(),
312           dlerror());
313     return NO_INIT;
314   }
315 
316   create_hwl = (CreateCameraProviderHwl_t)dlsym(hwl_lib_handle_,
317                                                 "CreateCameraProviderHwl");
318   if (create_hwl == nullptr) {
319     ALOGE("%s: dlsym failed (%s).", __FUNCTION__, kCameraHwlLib.data());
320     dlclose(hwl_lib_handle_);
321     hwl_lib_handle_ = nullptr;
322     return NO_INIT;
323   }
324 
325   // Create the HWL camera provider
326   camera_provider_hwl->reset(create_hwl());
327 #else
328   camera_provider_hwl->reset(CreateCameraProviderHwl());
329 #endif
330 
331   if (*camera_provider_hwl == nullptr) {
332     ALOGE("Error! Creating CameraProviderHwl failed");
333     return UNKNOWN_ERROR;
334   }
335 
336   return OK;
337 }
338 
NotifyDeviceStateChange(google_camera_hal::DeviceState device_state)339 status_t CameraProvider::NotifyDeviceStateChange(
340     google_camera_hal::DeviceState device_state) {
341   if (camera_provider_hwl_ == nullptr) {
342     ALOGE("%s: null provider hwl", __FUNCTION__);
343     return NO_INIT;
344   }
345   camera_provider_hwl_->NotifyDeviceStateChange(device_state);
346   return OK;
347 }
348 }  // namespace google_camera_hal
349 }  // namespace android
350