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