1 /*
2 * Copyright (C) 2022 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_AidlCameraDevice"
18 //#define LOG_NDEBUG 0
19 #include "aidl_camera_device.h"
20
21 #include <android/binder_ibinder_platform.h>
22 #include <log/log.h>
23
24 #include "aidl_camera_device_session.h"
25 #include "aidl_profiler.h"
26 #include "aidl_utils.h"
27 #include "profiler_util.h"
28
29 namespace android {
30 namespace hardware {
31 namespace camera {
32 namespace device {
33 namespace implementation {
34
35 namespace aidl_utils = ::android::hardware::camera::implementation::aidl_utils;
36
37 using aidl::android::hardware::camera::common::Status;
38 using ::android::google_camera_hal::HalCameraMetadata;
39
40 const std::string AidlCameraDevice::kDeviceVersion = "1.1";
41
Create(std::unique_ptr<CameraDevice> google_camera_device)42 std::shared_ptr<AidlCameraDevice> AidlCameraDevice::Create(
43 std::unique_ptr<CameraDevice> google_camera_device) {
44 auto device = ndk::SharedRefBase::make<AidlCameraDevice>();
45 if (device == nullptr) {
46 ALOGE("%s: Cannot create a AidlCameraDevice.", __FUNCTION__);
47 return nullptr;
48 }
49
50 status_t res = device->Initialize(std::move(google_camera_device));
51 if (res != OK) {
52 ALOGE("%s: Initializing AidlCameraDevice failed: %s(%d)", __FUNCTION__,
53 strerror(-res), res);
54 return nullptr;
55 }
56
57 return device;
58 }
59
Initialize(std::unique_ptr<CameraDevice> google_camera_device)60 status_t AidlCameraDevice::Initialize(
61 std::unique_ptr<CameraDevice> google_camera_device) {
62 if (google_camera_device == nullptr) {
63 ALOGE("%s: google_camera_device is nullptr.", __FUNCTION__);
64 return BAD_VALUE;
65 }
66
67 camera_id_ = google_camera_device->GetPublicCameraId();
68 google_camera_device_ = std::move(google_camera_device);
69 aidl_profiler_ = google_camera_hal::AidlProfiler::Create(camera_id_);
70 if (aidl_profiler_ == nullptr) {
71 ALOGE("%s: Failed to create AidlProfiler.", __FUNCTION__);
72 return UNKNOWN_ERROR;
73 }
74 return OK;
75 }
76
getResourceCost(CameraResourceCost * resource_cost)77 ScopedAStatus AidlCameraDevice::getResourceCost(
78 CameraResourceCost* resource_cost) {
79 google_camera_hal::CameraResourceCost hal_cost;
80 if (resource_cost == nullptr) {
81 return ScopedAStatus::fromServiceSpecificError(
82 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
83 }
84 status_t res = google_camera_device_->GetResourceCost(&hal_cost);
85 if (res != OK) {
86 ALOGE("%s: Getting resource cost failed for camera %u: %s(%d)",
87 __FUNCTION__, camera_id_, strerror(-res), res);
88 return ScopedAStatus::fromServiceSpecificError(
89 static_cast<int32_t>(Status::INTERNAL_ERROR));
90 }
91
92 res = aidl_utils::ConvertToAidlResourceCost(hal_cost, resource_cost);
93 if (res != OK) {
94 return ScopedAStatus::fromServiceSpecificError(
95 static_cast<int32_t>(Status::INTERNAL_ERROR));
96 }
97
98 return ScopedAStatus::ok();
99 }
100
getCameraCharacteristics(CameraMetadata * characteristics_ret)101 ScopedAStatus AidlCameraDevice::getCameraCharacteristics(
102 CameraMetadata* characteristics_ret) {
103 if (characteristics_ret == nullptr) {
104 return ScopedAStatus::fromServiceSpecificError(
105 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
106 }
107 characteristics_ret->metadata.clear();
108 std::unique_ptr<HalCameraMetadata> characteristics;
109 status_t res =
110 google_camera_device_->GetCameraCharacteristics(&characteristics);
111 if (res != OK) {
112 ALOGE("%s: Getting camera characteristics for camera %u failed: %s(%d)",
113 __FUNCTION__, camera_id_, strerror(-res), res);
114 return ScopedAStatus::fromServiceSpecificError(
115 static_cast<int32_t>(Status::INTERNAL_ERROR));
116 }
117
118 if (characteristics == nullptr) {
119 ALOGE("%s: Camera characteristics for camera %u is nullptr.", __FUNCTION__,
120 camera_id_);
121 return ScopedAStatus::fromServiceSpecificError(
122 static_cast<int32_t>(Status::INTERNAL_ERROR));
123 }
124
125 uint32_t metadata_size = characteristics->GetCameraMetadataSize();
126 uint8_t* chars_p = (uint8_t*)characteristics->GetRawCameraMetadata();
127 characteristics_ret->metadata.assign(chars_p, chars_p + metadata_size);
128
129 return ScopedAStatus::ok();
130 }
131
setTorchMode(bool on)132 ScopedAStatus AidlCameraDevice::setTorchMode(bool on) {
133 google_camera_hal::TorchMode hal_torch_mode;
134 hal_torch_mode = on ? google_camera_hal::TorchMode::kOn
135 : google_camera_hal::TorchMode::kOff;
136 auto res = google_camera_device_->SetTorchMode(hal_torch_mode);
137 return aidl_utils::ConvertToAidlReturn(res);
138 }
139
turnOnTorchWithStrengthLevel(int32_t torch_strength)140 ScopedAStatus AidlCameraDevice::turnOnTorchWithStrengthLevel(
141 int32_t torch_strength) {
142 status_t res =
143 google_camera_device_->TurnOnTorchWithStrengthLevel(torch_strength);
144 return aidl_utils::ConvertToAidlReturn(res);
145 }
146
getTorchStrengthLevel(int32_t * strength_level)147 ScopedAStatus AidlCameraDevice::getTorchStrengthLevel(int32_t* strength_level) {
148 if (strength_level == nullptr) {
149 return ScopedAStatus::fromServiceSpecificError(
150 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
151 }
152 *strength_level = 0;
153 int32_t torch_strength;
154 status_t res = google_camera_device_->GetTorchStrengthLevel(torch_strength);
155 if (res != OK) {
156 ALOGE(
157 "%s: Getting camera flash unit torch strength level for camera %u "
158 "failed: %s(%d)",
159 __FUNCTION__, camera_id_, strerror(-res), res);
160 return ScopedAStatus::fromServiceSpecificError(
161 static_cast<int32_t>(Status::INTERNAL_ERROR));
162 }
163 *strength_level = torch_strength;
164 return ScopedAStatus::ok();
165 }
166
constructDefaultRequestSettings(RequestTemplate type,CameraMetadata * request_settings)167 ScopedAStatus AidlCameraDevice::constructDefaultRequestSettings(
168 RequestTemplate type, CameraMetadata* request_settings) {
169 if (request_settings == nullptr) {
170 return ScopedAStatus::fromServiceSpecificError(
171 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
172 }
173
174 request_settings->metadata.clear();
175
176 google_camera_hal::RequestTemplate hal_type;
177 status_t res = aidl_utils::ConvertToHalTemplateType(type, &hal_type);
178 if (res != OK) {
179 return ndk::ScopedAStatus::fromServiceSpecificError(
180 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
181 }
182
183 std::unique_ptr<HalCameraMetadata> settings;
184 res = google_camera_device_->ConstructDefaultRequestSettings(hal_type,
185 &settings);
186 if (res != OK) {
187 ALOGE("%s: ConstructDefaultRequestSettings for camera %u failed: %s(%d)",
188 __FUNCTION__, camera_id_, strerror(-res), res);
189 return aidl_utils::ConvertToAidlReturn(res);
190 }
191 if (settings == nullptr) {
192 ALOGE("%s: Default request settings for camera %u is nullptr.",
193 __FUNCTION__, camera_id_);
194 return ScopedAStatus::fromServiceSpecificError(
195 static_cast<int32_t>(Status::INTERNAL_ERROR));
196 }
197
198 uint32_t metadata_size = settings->GetCameraMetadataSize();
199 uint8_t* chars_p = (uint8_t*)settings->GetRawCameraMetadata();
200 request_settings->metadata.assign(chars_p, chars_p + metadata_size);
201
202 return ScopedAStatus::ok();
203 }
204
isStreamCombinationWithSettingsSupported(const StreamConfiguration & streamConfiguration,bool * supported)205 ScopedAStatus AidlCameraDevice::isStreamCombinationWithSettingsSupported(
206 const StreamConfiguration& streamConfiguration, bool* supported) {
207 return isStreamCombinationSupportedInternal(streamConfiguration, supported,
208 /*checkSettings*/ true);
209 }
210
getSessionCharacteristics(const StreamConfiguration & session_config,CameraMetadata * characteristics_ret)211 ScopedAStatus AidlCameraDevice::getSessionCharacteristics(
212 const StreamConfiguration& session_config,
213 CameraMetadata* characteristics_ret) {
214 google_camera_hal::StreamConfiguration stream_config;
215 status_t res =
216 aidl_utils::ConvertToHalStreamConfig(session_config, &stream_config);
217 if (res != OK) {
218 ALOGE("%s: ConvertToHalStreamConfig fail", __FUNCTION__);
219 return ScopedAStatus::fromServiceSpecificError(
220 static_cast<int32_t>(Status::INTERNAL_ERROR));
221 }
222
223 if (characteristics_ret == nullptr) {
224 return ScopedAStatus::fromServiceSpecificError(
225 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
226 }
227 characteristics_ret->metadata.clear();
228 std::unique_ptr<HalCameraMetadata> session_characteristics;
229 res = google_camera_device_->GetSessionCharacteristics(
230 stream_config, session_characteristics);
231 if (res != OK) {
232 ALOGE("%s: Getting session characteristics for camera %u failed: %s(%d)",
233 __FUNCTION__, camera_id_, strerror(-res), res);
234 return ScopedAStatus::fromServiceSpecificError(
235 static_cast<int32_t>(Status::INTERNAL_ERROR));
236 }
237
238 if (session_characteristics == nullptr) {
239 ALOGE("%s: Session characteristics for camera %u is nullptr.", __FUNCTION__,
240 camera_id_);
241 return ScopedAStatus::fromServiceSpecificError(
242 static_cast<int32_t>(Status::INTERNAL_ERROR));
243 }
244
245 uint32_t metadata_size = session_characteristics->GetCameraMetadataSize();
246 uint8_t* chars_p = (uint8_t*)session_characteristics->GetRawCameraMetadata();
247 characteristics_ret->metadata.assign(chars_p, chars_p + metadata_size);
248
249 return ScopedAStatus::ok();
250 }
251
getPhysicalCameraCharacteristics(const std::string & physicalCameraId,CameraMetadata * characteristics_ret)252 ScopedAStatus AidlCameraDevice::getPhysicalCameraCharacteristics(
253 const std::string& physicalCameraId, CameraMetadata* characteristics_ret) {
254 if (characteristics_ret == nullptr) {
255 return ScopedAStatus::fromServiceSpecificError(
256 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
257 }
258 characteristics_ret->metadata.clear();
259 std::unique_ptr<HalCameraMetadata> physical_characteristics;
260 uint32_t physical_camera_id = atoi(physicalCameraId.c_str());
261 status_t res = google_camera_device_->GetPhysicalCameraCharacteristics(
262 physical_camera_id, &physical_characteristics);
263 if (res != OK) {
264 ALOGE("%s: Getting physical characteristics for camera %u failed: %s(%d)",
265 __FUNCTION__, camera_id_, strerror(-res), res);
266 return aidl_utils::ConvertToAidlReturn(res);
267 }
268
269 if (physical_characteristics == nullptr) {
270 ALOGE("%s: Physical characteristics for camera %u is nullptr.",
271 __FUNCTION__, physical_camera_id);
272 return ScopedAStatus::fromServiceSpecificError(
273 static_cast<int32_t>(Status::INTERNAL_ERROR));
274 }
275
276 uint32_t metadata_size = physical_characteristics->GetCameraMetadataSize();
277 uint8_t* physical_characteristics_p =
278 (uint8_t*)physical_characteristics->GetRawCameraMetadata();
279 characteristics_ret->metadata.assign(
280 physical_characteristics_p, physical_characteristics_p + metadata_size);
281 return ScopedAStatus::ok();
282 }
283
open(const std::shared_ptr<ICameraDeviceCallback> & callback,std::shared_ptr<ICameraDeviceSession> * session_ret)284 ScopedAStatus AidlCameraDevice::open(
285 const std::shared_ptr<ICameraDeviceCallback>& callback,
286 std::shared_ptr<ICameraDeviceSession>* session_ret) {
287 if (session_ret == nullptr) {
288 return ScopedAStatus::fromServiceSpecificError(
289 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
290 }
291 *session_ret = nullptr;
292 auto profiler = aidl_profiler_->MakeScopedProfiler(
293 google_camera_hal::EventType::kOpen,
294 google_camera_device_->GetProfiler(camera_id_,
295 aidl_profiler_->GetLatencyFlag()),
296 google_camera_device_->GetProfiler(camera_id_,
297 aidl_profiler_->GetFpsFlag()));
298
299 std::unique_ptr<google_camera_hal::CameraDeviceSession> session;
300 status_t res = google_camera_device_->CreateCameraDeviceSession(&session);
301 if (res != OK || session == nullptr) {
302 ALOGE("%s: Creating CameraDeviceSession failed: %s(%d)", __FUNCTION__,
303 strerror(-res), res);
304 return aidl_utils::ConvertToAidlReturn(res);
305 }
306
307 auto aidl_session = AidlCameraDeviceSession::Create(
308 callback, std::move(session), aidl_profiler_);
309 if (aidl_session == nullptr) {
310 ALOGE("%s: Creating AidlCameraDeviceSession failed.", __FUNCTION__);
311 return aidl_utils::ConvertToAidlReturn(res);
312 }
313 *session_ret = aidl_session;
314 return ScopedAStatus::ok();
315 }
316
openInjectionSession(const std::shared_ptr<ICameraDeviceCallback> &,std::shared_ptr<ICameraInjectionSession> * session)317 ScopedAStatus AidlCameraDevice::openInjectionSession(
318 const std::shared_ptr<ICameraDeviceCallback>&,
319 std::shared_ptr<ICameraInjectionSession>* session) {
320 if (session == nullptr) {
321 return ScopedAStatus::fromServiceSpecificError(
322 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
323 }
324 *session = nullptr;
325 return ScopedAStatus::fromServiceSpecificError(
326 static_cast<int32_t>(Status::OPERATION_NOT_SUPPORTED));
327 }
328
dump(int fd,const char **,uint32_t)329 binder_status_t AidlCameraDevice::dump(int fd, const char** /*args*/,
330 uint32_t /*numArgs*/) {
331 google_camera_device_->DumpState(fd);
332 return OK;
333 }
334
isStreamCombinationSupported(const StreamConfiguration & streams,bool * supported)335 ScopedAStatus AidlCameraDevice::isStreamCombinationSupported(
336 const StreamConfiguration& streams, bool* supported) {
337 return isStreamCombinationSupportedInternal(streams, supported,
338 /*checkSettings*/ false);
339 }
340
isStreamCombinationSupportedInternal(const StreamConfiguration & streams,bool * supported,bool checkSettings)341 ScopedAStatus AidlCameraDevice::isStreamCombinationSupportedInternal(
342 const StreamConfiguration& streams, bool* supported, bool checkSettings) {
343 if (supported == nullptr) {
344 return ScopedAStatus::fromServiceSpecificError(
345 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
346 }
347 *supported = false;
348 google_camera_hal::StreamConfiguration stream_config;
349 status_t res = aidl_utils::ConvertToHalStreamConfig(streams, &stream_config);
350 if (res != OK) {
351 ALOGE("%s: ConvertToHalStreamConfig fail", __FUNCTION__);
352 return ScopedAStatus::fromServiceSpecificError(
353 static_cast<int32_t>(Status::INTERNAL_ERROR));
354 }
355 *supported = google_camera_device_->IsStreamCombinationSupported(
356 stream_config, checkSettings);
357 return ScopedAStatus::ok();
358 }
359
createBinder()360 ::ndk::SpAIBinder AidlCameraDevice::createBinder() {
361 auto binder = BnCameraDevice::createBinder();
362 AIBinder_setInheritRt(binder.get(), true);
363 return binder;
364 }
365 } // namespace implementation
366 } // namespace device
367 } // namespace camera
368 } // namespace hardware
369 } // namespace android
370