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_TAG "GCH_HidlCameraDevice"
18 //#define LOG_NDEBUG 0
19 #include "hidl_camera_device.h"
20 
21 #include <log/log.h>
22 
23 #include "hidl_camera_device_session.h"
24 #include "hidl_profiler.h"
25 #include "hidl_utils.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace camera {
30 namespace device {
31 namespace V3_7 {
32 namespace implementation {
33 
34 namespace hidl_utils = ::android::hardware::camera::implementation::hidl_utils;
35 
36 using ::android::google_camera_hal::HalCameraMetadata;
37 
38 const std::string HidlCameraDevice::kDeviceVersion = "3.7";
39 
Create(std::unique_ptr<CameraDevice> google_camera_device)40 std::unique_ptr<HidlCameraDevice> HidlCameraDevice::Create(
41     std::unique_ptr<CameraDevice> google_camera_device) {
42   auto device = std::unique_ptr<HidlCameraDevice>(new HidlCameraDevice());
43   if (device == nullptr) {
44     ALOGE("%s: Cannot create a HidlCameraDevice.", __FUNCTION__);
45     return nullptr;
46   }
47 
48   status_t res = device->Initialize(std::move(google_camera_device));
49   if (res != OK) {
50     ALOGE("%s: Initializing HidlCameraDevice failed: %s(%d)", __FUNCTION__,
51           strerror(-res), res);
52     return nullptr;
53   }
54 
55   return device;
56 }
57 
Initialize(std::unique_ptr<CameraDevice> google_camera_device)58 status_t HidlCameraDevice::Initialize(
59     std::unique_ptr<CameraDevice> google_camera_device) {
60   if (google_camera_device == nullptr) {
61     ALOGE("%s: google_camera_device is nullptr.", __FUNCTION__);
62     return BAD_VALUE;
63   }
64 
65   camera_id_ = google_camera_device->GetPublicCameraId();
66   google_camera_device_ = std::move(google_camera_device);
67   hidl_profiler_ = HidlProfiler::Create(camera_id_);
68   if (hidl_profiler_ == nullptr) {
69     ALOGE("%s: Failed to create HidlProfiler.", __FUNCTION__);
70     return UNKNOWN_ERROR;
71   }
72   hidl_profiler_->SetLatencyProfiler(google_camera_device_->GetProfiler(
73       camera_id_, hidl_profiler_->GetLatencyFlag()));
74   hidl_profiler_->SetFpsProfiler(google_camera_device_->GetProfiler(
75       camera_id_, hidl_profiler_->GetFpsFlag()));
76 
77   return OK;
78 }
79 
getResourceCost(ICameraDevice::getResourceCost_cb _hidl_cb)80 Return<void> HidlCameraDevice::getResourceCost(
81     ICameraDevice::getResourceCost_cb _hidl_cb) {
82   google_camera_hal::CameraResourceCost hal_cost;
83   CameraResourceCost hidl_cost;
84 
85   status_t res = google_camera_device_->GetResourceCost(&hal_cost);
86   if (res != OK) {
87     ALOGE("%s: Getting resource cost failed for camera %u: %s(%d)",
88           __FUNCTION__, camera_id_, strerror(-res), res);
89     _hidl_cb(Status::INTERNAL_ERROR, hidl_cost);
90     return Void();
91   }
92 
93   res = hidl_utils::ConvertToHidlResourceCost(hal_cost, &hidl_cost);
94   if (res != OK) {
95     _hidl_cb(Status::INTERNAL_ERROR, hidl_cost);
96     return Void();
97   }
98 
99   _hidl_cb(Status::OK, hidl_cost);
100   return Void();
101 }
102 
getCameraCharacteristics(ICameraDevice::getCameraCharacteristics_cb _hidl_cb)103 Return<void> HidlCameraDevice::getCameraCharacteristics(
104     ICameraDevice::getCameraCharacteristics_cb _hidl_cb) {
105   V3_2::CameraMetadata hidl_characteristics;
106   std::unique_ptr<HalCameraMetadata> characteristics;
107   status_t res =
108       google_camera_device_->GetCameraCharacteristics(&characteristics);
109   if (res != OK) {
110     ALOGE("%s: Getting camera characteristics for camera %u failed: %s(%d)",
111           __FUNCTION__, camera_id_, strerror(-res), res);
112     _hidl_cb(Status::INTERNAL_ERROR, hidl_characteristics);
113     return Void();
114   }
115 
116   if (characteristics == nullptr) {
117     ALOGE("%s: Camera characteristics for camera %u is nullptr.", __FUNCTION__,
118           camera_id_);
119     _hidl_cb(Status::INTERNAL_ERROR, hidl_characteristics);
120     return Void();
121   }
122 
123   uint32_t metadata_size = characteristics->GetCameraMetadataSize();
124   hidl_characteristics.setToExternal(
125       (uint8_t*)characteristics->ReleaseCameraMetadata(), metadata_size,
126       /*shouldOwn*/ true);
127 
128   _hidl_cb(Status::OK, hidl_characteristics);
129   return Void();
130 }
131 
setTorchMode(TorchMode mode)132 Return<Status> HidlCameraDevice::setTorchMode(TorchMode mode) {
133   google_camera_hal::TorchMode hal_torch_mode;
134   status_t res = hidl_utils::ConvertToHalTorchMode(mode, &hal_torch_mode);
135   if (res != OK) {
136     ALOGE("%s: failed to convert torch mode", __FUNCTION__);
137     return Status::INTERNAL_ERROR;
138   }
139 
140   res = google_camera_device_->SetTorchMode(hal_torch_mode);
141   return hidl_utils::ConvertToHidlStatus(res);
142 }
143 
open(const sp<V3_2::ICameraDeviceCallback> & callback,ICameraDevice::open_cb _hidl_cb)144 Return<void> HidlCameraDevice::open(
145     const sp<V3_2::ICameraDeviceCallback>& callback,
146     ICameraDevice::open_cb _hidl_cb) {
147   auto profiler =
148       hidl_profiler_->MakeScopedProfiler(HidlProfiler::ScopedType::kOpen);
149 
150   std::unique_ptr<google_camera_hal::CameraDeviceSession> session;
151   status_t res = google_camera_device_->CreateCameraDeviceSession(&session);
152   if (res != OK || session == nullptr) {
153     ALOGE("%s: Creating CameraDeviceSession failed: %s(%d)", __FUNCTION__,
154           strerror(-res), res);
155     _hidl_cb(hidl_utils::ConvertToHidlStatus(res), nullptr);
156     return Void();
157   }
158 
159   auto hidl_session = HidlCameraDeviceSession::Create(
160       callback, std::move(session), hidl_profiler_);
161   if (hidl_session == nullptr) {
162     ALOGE("%s: Creating HidlCameraDeviceSession failed.", __FUNCTION__);
163     _hidl_cb(hidl_utils::ConvertToHidlStatus(res), nullptr);
164     return Void();
165   }
166 
167   _hidl_cb(Status::OK, hidl_session.release());
168   return Void();
169 }
170 
dumpState(const::android::hardware::hidl_handle & handle)171 Return<void> HidlCameraDevice::dumpState(
172     const ::android::hardware::hidl_handle& handle) {
173   if (handle.getNativeHandle() == nullptr) {
174     ALOGE("%s: handle is nullptr", __FUNCTION__);
175     return Void();
176   }
177 
178   if (handle->numFds != 1 || handle->numInts != 0) {
179     ALOGE("%s: handle must contain 1 fd(%d) and 0 ints(%d)", __FUNCTION__,
180           handle->numFds, handle->numInts);
181     return Void();
182   }
183 
184   int fd = handle->data[0];
185   google_camera_device_->DumpState(fd);
186   return Void();
187 }
188 
getPhysicalCameraCharacteristics(const hidl_string & physicalCameraId,ICameraDevice::getPhysicalCameraCharacteristics_cb _hidl_cb)189 Return<void> HidlCameraDevice::getPhysicalCameraCharacteristics(
190     const hidl_string& physicalCameraId,
191     ICameraDevice::getPhysicalCameraCharacteristics_cb _hidl_cb) {
192   V3_2::CameraMetadata hidl_characteristics;
193   std::unique_ptr<HalCameraMetadata> physical_characteristics;
194 
195   uint32_t physical_camera_id = atoi(physicalCameraId.c_str());
196   status_t res = google_camera_device_->GetPhysicalCameraCharacteristics(
197       physical_camera_id, &physical_characteristics);
198   if (res != OK) {
199     ALOGE("%s: Getting physical characteristics for camera %u failed: %s(%d)",
200           __FUNCTION__, camera_id_, strerror(-res), res);
201     _hidl_cb(hidl_utils::ConvertToHidlStatus(res), hidl_characteristics);
202     return Void();
203   }
204 
205   if (physical_characteristics == nullptr) {
206     ALOGE("%s: Physical characteristics for camera %u is nullptr.",
207           __FUNCTION__, physical_camera_id);
208     _hidl_cb(Status::INTERNAL_ERROR, hidl_characteristics);
209     return Void();
210   }
211 
212   uint32_t metadata_size = physical_characteristics->GetCameraMetadataSize();
213   hidl_characteristics.setToExternal(
214       (uint8_t*)physical_characteristics->ReleaseCameraMetadata(),
215       metadata_size, /*shouldOwn=*/true);
216 
217   _hidl_cb(Status::OK, hidl_characteristics);
218   return Void();
219 }
220 
isStreamCombinationSupported(const V3_4::StreamConfiguration & streams,ICameraDevice::isStreamCombinationSupported_cb _hidl_cb)221 Return<void> HidlCameraDevice::isStreamCombinationSupported(
222     const V3_4::StreamConfiguration& streams,
223     ICameraDevice::isStreamCombinationSupported_cb _hidl_cb) {
224   StreamConfiguration streams3_7;
225 
226   hidl_utils::ConvertStreamConfigurationV34ToV37(streams, &streams3_7);
227 
228   return isStreamCombinationSupported_3_7(streams3_7, _hidl_cb);
229 }
230 
isStreamCombinationSupported_3_7(const V3_7::StreamConfiguration & streams,ICameraDevice::isStreamCombinationSupported_cb _hidl_cb)231 Return<void> HidlCameraDevice::isStreamCombinationSupported_3_7(
232     const V3_7::StreamConfiguration& streams,
233     ICameraDevice::isStreamCombinationSupported_cb _hidl_cb) {
234   bool is_supported = false;
235   google_camera_hal::StreamConfiguration stream_config;
236   status_t res = hidl_utils::ConverToHalStreamConfig(streams, &stream_config);
237   if (res != OK) {
238     ALOGE("%s: ConverToHalStreamConfig fail", __FUNCTION__);
239     _hidl_cb(Status::INTERNAL_ERROR, is_supported);
240     return Void();
241   }
242   is_supported =
243       google_camera_device_->IsStreamCombinationSupported(stream_config);
244 
245   _hidl_cb(Status::OK, is_supported);
246   return Void();
247 }
248 
249 }  // namespace implementation
250 }  // namespace V3_7
251 }  // namespace device
252 }  // namespace camera
253 }  // namespace hardware
254 }  // namespace android
255