blob: 1ec50722b094ff88723b0151f699db29e99cde1d [file] [log] [blame]
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -07001/*
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 "AidlUtils"
Jayant Chowdharyb67a9572024-05-13 18:47:11 +000018//#define LOG_NDEBUG 0
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070019
20#include <aidl/AidlUtils.h>
Emilian Peev3a4fa462024-02-15 19:56:20 +000021#include <aidl/ExtensionMetadataTags.h>
Avichal Rakesh4baf7262024-03-20 19:16:04 -070022#include <aidl/SessionCharacteristicsTags.h>
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070023#include <aidl/VndkVersionMetadataTags.h>
24#include <aidlcommonsupport/NativeHandle.h>
Emilian Peev3a4fa462024-02-15 19:56:20 +000025#include <camera/StringUtils.h>
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070026#include <device3/Camera3StreamInterface.h>
27#include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h>
28#include <mediautils/AImageReaderUtils.h>
Eino-Ville Talvalab7723202024-06-24 17:45:51 -070029#include "utils/Utils.h"
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070030
31namespace android::hardware::cameraservice::utils::conversion::aidl {
32
33using aimg::AImageReader_getHGBPFromHandle;
34using hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
35
36// Note: existing data in dst will be gone. Caller still owns the memory of src
37void cloneToAidl(const camera_metadata_t* src, SCameraMetadata* dst) {
38 if (src == nullptr) {
39 ALOGW("%s:attempt to convert empty metadata to AIDL", __FUNCTION__);
40 return;
41 }
42 size_t size = get_camera_metadata_size(src);
43 uint8_t* startPtr = (uint8_t*)src;
44 uint8_t* endPtr = startPtr + size;
45 dst->metadata.assign(startPtr, endPtr);
46}
47
48// The camera metadata here is cloned. Since we're reading metadata over
49// the binder we would need to clone it in order to avoid alignment issues.
50bool cloneFromAidl(const SCameraMetadata &src, CameraMetadata *dst) {
51 const camera_metadata_t *buffer =
52 reinterpret_cast<const camera_metadata_t*>(src.metadata.data());
53 size_t expectedSize = src.metadata.size();
54 if (buffer != nullptr) {
55 int res = validate_camera_metadata_structure(buffer, &expectedSize);
56 if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
57 *dst = buffer;
58 } else {
59 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
60 return false;
61 }
62 }
63 return true;
64}
65
66int32_t convertFromAidl(SStreamConfigurationMode streamConfigurationMode) {
67 switch (streamConfigurationMode) {
68 case SStreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE:
69 return camera2::ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE;
70 case SStreamConfigurationMode::NORMAL_MODE:
71 return camera2::ICameraDeviceUser::NORMAL_MODE;
72 default:
73 // TODO: Fix this
74 return camera2::ICameraDeviceUser::VENDOR_MODE_START;
75 }
76}
77
78UOutputConfiguration convertFromAidl(const SOutputConfiguration &src) {
79 std::vector<sp<IGraphicBufferProducer>> iGBPs;
Avichal Rakesh62d45f12023-10-25 12:54:45 -070080 if (!src.surfaces.empty()) {
81 auto& surfaces = src.surfaces;
82 iGBPs.reserve(surfaces.size());
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070083
Avichal Rakesh62d45f12023-10-25 12:54:45 -070084 for (auto& sSurface : surfaces) {
85 sp<IGraphicBufferProducer> igbp =
86 Surface::getIGraphicBufferProducer(sSurface.get());
87 if (igbp == nullptr) {
88 ALOGE("%s: ANativeWindow (%p) not backed by a Surface.",
89 __FUNCTION__, sSurface.get());
90 continue;
91 }
92 iGBPs.push_back(igbp);
Avichal Rakeshb8610162023-11-08 18:21:10 -080093 }
Avichal Rakesh62d45f12023-10-25 12:54:45 -070094 } else {
95#pragma clang diagnostic push
96#pragma clang diagnostic ignored "-Wdeprecated-declarations"
97 // HIDL token manager (and consequently 'windowHandles') is deprecated and will be removed
98 // in the future. However, cameraservice must still support old NativeHandle pathway until
99 // all vendors have moved away from using NativeHandles
100 auto &windowHandles = src.windowHandles;
101#pragma clang diagnostic pop
102
103 iGBPs.reserve(windowHandles.size());
104
105 for (auto &handle : windowHandles) {
106 native_handle_t* nh = makeFromAidl(handle);
107 auto igbp = AImageReader_getHGBPFromHandle(nh);
108 if (igbp == nullptr) {
109 ALOGE("%s: Could not get HGBP from NativeHandle: %s. Skipping.",
110 __FUNCTION__, handle.toString().c_str());
111 continue;
112 }
113
114 iGBPs.push_back(new H2BGraphicBufferProducer(igbp));
115 native_handle_delete(nh);
116 }
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700117 }
Avichal Rakesh62d45f12023-10-25 12:54:45 -0700118
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700119 UOutputConfiguration outputConfiguration(
Xin Li65d53082023-08-25 14:16:11 -0700120 iGBPs, convertFromAidl(src.rotation), src.physicalCameraId,
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700121 src.windowGroupId, OutputConfiguration::SURFACE_TYPE_UNKNOWN, 0, 0,
Avichal Rakesh62d45f12023-10-25 12:54:45 -0700122 (iGBPs.size() > 1));
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700123 return outputConfiguration;
124}
125
126USessionConfiguration convertFromAidl(const SSessionConfiguration &src) {
127 USessionConfiguration sessionConfig(src.inputWidth, src.inputHeight,
128 src.inputFormat, static_cast<int>(src.operationMode));
129
130 for (const auto& os : src.outputStreams) {
131 UOutputConfiguration config = convertFromAidl(os);
132 sessionConfig.addOutputConfiguration(config);
133 }
134
135 return sessionConfig;
136}
137
138int convertFromAidl(SOutputConfiguration::Rotation rotation) {
139 switch(rotation) {
140 case SOutputConfiguration::Rotation::R270:
141 return android::camera3::CAMERA_STREAM_ROTATION_270;
142 case SOutputConfiguration::Rotation::R180:
143 return android::camera3::CAMERA_STREAM_ROTATION_180;
144 case SOutputConfiguration::Rotation::R90:
145 return android::camera3::CAMERA_STREAM_ROTATION_90;
146 case SOutputConfiguration::Rotation::R0:
147 default:
148 return android::camera3::CAMERA_STREAM_ROTATION_0;
149 }
150}
151
152int32_t convertFromAidl(STemplateId templateId) {
153 switch(templateId) {
154 case STemplateId::PREVIEW:
155 return camera2::ICameraDeviceUser::TEMPLATE_PREVIEW;
156 case STemplateId::STILL_CAPTURE:
157 return camera2::ICameraDeviceUser::TEMPLATE_STILL_CAPTURE;
158 case STemplateId::RECORD:
159 return camera2::ICameraDeviceUser::TEMPLATE_RECORD;
160 case STemplateId::VIDEO_SNAPSHOT:
161 return camera2::ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT;
162 case STemplateId::ZERO_SHUTTER_LAG:
163 return camera2::ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG;
164 case STemplateId::MANUAL:
165 return camera2::ICameraDeviceUser::TEMPLATE_MANUAL;
166 }
167}
168
169void convertToAidl(const camera2::utils::SubmitInfo& submitInfo, SSubmitInfo* hSubmitInfo) {
170 hSubmitInfo->requestId = submitInfo.mRequestId;
171 hSubmitInfo->lastFrameNumber = submitInfo.mLastFrameNumber;
172}
173
174
175SStatus convertToAidl(const binder::Status &status) {
176 if (status.isOk()) {
177 return SStatus::NO_ERROR;
178 }
179 if (status.exceptionCode() != EX_SERVICE_SPECIFIC) {
180 return SStatus::UNKNOWN_ERROR;
181 }
182
183 switch (status.serviceSpecificErrorCode()) {
184 case hardware::ICameraService::ERROR_DISCONNECTED:
185 return SStatus::DISCONNECTED;
186 case hardware::ICameraService::ERROR_CAMERA_IN_USE:
187 return SStatus::CAMERA_IN_USE;
188 case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE:
189 return SStatus::MAX_CAMERAS_IN_USE;
190 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
191 return SStatus::ILLEGAL_ARGUMENT;
192 case hardware::ICameraService::ERROR_DEPRECATED_HAL:
193 // Should not reach here since we filtered legacy HALs earlier
194 return SStatus::DEPRECATED_HAL;
195 case hardware::ICameraService::ERROR_DISABLED:
196 return SStatus::DISABLED;
197 case hardware::ICameraService::ERROR_PERMISSION_DENIED:
198 return SStatus::PERMISSION_DENIED;
199 case hardware::ICameraService::ERROR_INVALID_OPERATION:
200 return SStatus::INVALID_OPERATION;
201 default:
202 return SStatus::UNKNOWN_ERROR;
203 }
204}
205
206SCaptureResultExtras convertToAidl(const UCaptureResultExtras &src) {
207 SCaptureResultExtras dst;
208 dst.requestId = src.requestId;
209 dst.burstId = src.burstId;
210 dst.frameNumber = src.frameNumber;
211 dst.partialResultCount = src.partialResultCount;
212 dst.errorStreamId = src.errorStreamId;
Xin Li65d53082023-08-25 14:16:11 -0700213 dst.errorPhysicalCameraId = src.errorPhysicalCameraId;
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700214 return dst;
215}
216
217SErrorCode convertToAidl(int32_t errorCode) {
218 switch(errorCode) {
219 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED:
220 return SErrorCode::CAMERA_DISCONNECTED;
221 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE :
222 return SErrorCode::CAMERA_DEVICE;
223 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE:
224 return SErrorCode::CAMERA_SERVICE;
225 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
226 return SErrorCode::CAMERA_REQUEST;
227 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
228 return SErrorCode::CAMERA_RESULT;
229 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
230 return SErrorCode::CAMERA_BUFFER;
231 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED:
232 return SErrorCode::CAMERA_DISABLED;
233 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR:
234 return SErrorCode::CAMERA_INVALID_ERROR;
235 default:
236 return SErrorCode::CAMERA_UNKNOWN_ERROR;
237 }
238}
239
240std::vector<SPhysicalCaptureResultInfo> convertToAidl(
241 const std::vector<UPhysicalCaptureResultInfo>& src,
242 std::shared_ptr<CaptureResultMetadataQueue>& fmq) {
243 std::vector<SPhysicalCaptureResultInfo> dst;
244 dst.resize(src.size());
245 size_t i = 0;
246 for (auto &physicalCaptureResultInfo : src) {
247 dst[i++] = convertToAidl(physicalCaptureResultInfo, fmq);
248 }
249 return dst;
250}
251
252SPhysicalCaptureResultInfo convertToAidl(const UPhysicalCaptureResultInfo & src,
253 std::shared_ptr<CaptureResultMetadataQueue> & fmq) {
254 SPhysicalCaptureResultInfo dst;
Xin Li65d53082023-08-25 14:16:11 -0700255 dst.physicalCameraId = src.mPhysicalCameraId;
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700256
257 const camera_metadata_t *rawMetadata = src.mPhysicalCameraMetadata.getAndLock();
258 // Try using fmq at first.
259 size_t metadata_size = get_camera_metadata_size(rawMetadata);
260 if ((metadata_size > 0) && (fmq->availableToWrite() > 0)) {
261 if (fmq->write((int8_t *)rawMetadata, metadata_size)) {
262 dst.physicalCameraMetadata.set<SCaptureMetadataInfo::fmqMetadataSize>(metadata_size);
263 } else {
264 ALOGW("%s Couldn't use fmq, falling back to hwbinder", __FUNCTION__);
265 SCameraMetadata metadata;
266 cloneToAidl(rawMetadata, &metadata);
267 dst.physicalCameraMetadata.set<SCaptureMetadataInfo::metadata>(std::move(metadata));
268 }
269 }
270 src.mPhysicalCameraMetadata.unlock(rawMetadata);
271 return dst;
272}
273
274void convertToAidl(const std::vector<hardware::CameraStatus> &src,
275 std::vector<SCameraStatusAndId>* dst) {
276 dst->resize(src.size());
277 size_t i = 0;
278 for (const auto &statusAndId : src) {
279 auto &a = (*dst)[i++];
Xin Li65d53082023-08-25 14:16:11 -0700280 a.cameraId = statusAndId.cameraId;
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700281 a.deviceStatus = convertCameraStatusToAidl(statusAndId.status);
282 size_t numUnvailPhysicalCameras = statusAndId.unavailablePhysicalIds.size();
283 a.unavailPhysicalCameraIds.resize(numUnvailPhysicalCameras);
284 for (size_t j = 0; j < numUnvailPhysicalCameras; j++) {
Xin Li65d53082023-08-25 14:16:11 -0700285 a.unavailPhysicalCameraIds[j] = statusAndId.unavailablePhysicalIds[j];
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700286 }
287 }
288}
289
290SCameraDeviceStatus convertCameraStatusToAidl(int32_t src) {
291 SCameraDeviceStatus deviceStatus = SCameraDeviceStatus::STATUS_UNKNOWN;
292 switch(src) {
293 case hardware::ICameraServiceListener::STATUS_NOT_PRESENT:
294 deviceStatus = SCameraDeviceStatus::STATUS_NOT_PRESENT;
295 break;
296 case hardware::ICameraServiceListener::STATUS_PRESENT:
297 deviceStatus = SCameraDeviceStatus::STATUS_PRESENT;
298 break;
299 case hardware::ICameraServiceListener::STATUS_ENUMERATING:
300 deviceStatus = SCameraDeviceStatus::STATUS_ENUMERATING;
301 break;
302 case hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE:
303 deviceStatus = SCameraDeviceStatus::STATUS_NOT_AVAILABLE;
304 break;
305 default:
306 break;
307 }
308 return deviceStatus;
309}
310
311bool areBindersEqual(const ndk::SpAIBinder& b1, const ndk::SpAIBinder& b2) {
312 return !AIBinder_lt(b1.get(), b2.get()) && !AIBinder_lt(b2.get(), b1.get());
313}
314
315status_t filterVndkKeys(int vndkVersion, CameraMetadata &metadata, bool isStatic) {
316 if (vndkVersion == __ANDROID_API_FUTURE__) {
Avichal Rakesh74b5ae72023-12-27 16:56:45 -0800317 // VNDK version derived from ro.board.api_level is a version code-name that
318 // corresponds to the current SDK version.
Jayant Chowdharyb67a9572024-05-13 18:47:11 +0000319 ALOGV("%s: VNDK version is API FUTURE, not filtering any keys", __FUNCTION__);
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700320 return OK;
321 }
322 const auto &apiLevelToKeys =
323 isStatic ? static_api_level_to_keys : dynamic_api_level_to_keys;
324 // Find the vndk versions above the given vndk version. All the vndk
325 // versions above the given one, need to have their keys filtered from the
326 // metadata in order to avoid metadata invalidation.
327 auto it = apiLevelToKeys.upper_bound(vndkVersion);
Jayant Chowdharyb67a9572024-05-13 18:47:11 +0000328 ALOGV("%s: VNDK version for filtering is %d", __FUNCTION__ , vndkVersion);
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700329 while (it != apiLevelToKeys.end()) {
330 for (const auto &key : it->second) {
331 status_t res = metadata.erase(key);
Jayant Chowdharyb67a9572024-05-13 18:47:11 +0000332 // Should be okay to not use get_local_camera_metadata_tag_name
333 // since we're not filtering vendor tags
334 ALOGV("%s: Metadata key being filtered is %s", __FUNCTION__ ,
335 get_camera_metadata_tag_name(key));
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700336 if (res != OK) {
337 ALOGE("%s metadata key %d could not be erased", __FUNCTION__, key);
338 return res;
339 }
340 }
341 it++;
342 }
343 return OK;
344}
345
Avichal Rakesh4baf7262024-03-20 19:16:04 -0700346status_t copySessionCharacteristics(const CameraMetadata& from, CameraMetadata* to,
347 int queryVersion) {
348 // Ensure the vendor ID are the same before attempting
349 // anything else. If vendor IDs differ we cannot safely copy the characteristics.
350 if (from.getVendorId() != to->getVendorId()) {
Eino-Ville Talvalab7723202024-06-24 17:45:51 -0700351 ALOGE("%s: Incompatible CameraMetadata objects. Vendor IDs differ. From: %" PRIu64
352 "; To: %" PRIu64, __FUNCTION__, from.getVendorId(), to->getVendorId());
Avichal Rakesh4baf7262024-03-20 19:16:04 -0700353 return BAD_VALUE;
354 }
355
356 // Allow public tags according to the queryVersion
357 std::unordered_set<uint32_t> validPublicTags;
358 auto last = api_level_to_session_characteristic_keys.upper_bound(queryVersion);
359 for (auto it = api_level_to_session_characteristic_keys.begin(); it != last; it++) {
360 validPublicTags.insert(it->second.cbegin(), it->second.cend());
361 }
362
363 const camera_metadata_t* src = from.getAndLock();
364 camera_metadata_ro_entry_t entry{};
365 for (size_t i = 0; i < get_camera_metadata_entry_count(src); i++) {
366 int ret = get_camera_metadata_ro_entry(src, i, &entry);
367 if (ret != OK) {
Eino-Ville Talvalab7723202024-06-24 17:45:51 -0700368 ALOGE("%s: Could not fetch entry at index %zu. Error: %d", __FUNCTION__, i, ret);
Avichal Rakesh4baf7262024-03-20 19:16:04 -0700369 from.unlock(src);
370 return BAD_VALUE;
371 }
372
373 if (entry.tag < (uint32_t)VENDOR_SECTION_START &&
374 validPublicTags.find(entry.tag) == validPublicTags.end()) {
375 ALOGI("%s: Session Characteristics contains tag %s but not supported by query version "
376 "(%d)",
377 __FUNCTION__, get_camera_metadata_tag_name(entry.tag), queryVersion);
378 continue;
379 }
380
381 // The entry is either a vendor tag, or a valid session characteristic key.
382 // Copy over the value
383 to->update(entry);
384 }
385 from.unlock(src);
386 return OK;
387}
388
Emilian Peev3a4fa462024-02-15 19:56:20 +0000389bool areExtensionKeysSupported(const CameraMetadata& metadata) {
390 auto requestKeys = metadata.find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
391 if (requestKeys.count == 0) {
392 ALOGE("%s: No ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS entries!", __FUNCTION__);
393 return false;
394 }
395
396 auto resultKeys = metadata.find(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
397 if (resultKeys.count == 0) {
398 ALOGE("%s: No ANDROID_REQUEST_AVAILABLE_RESULT_KEYS entries!", __FUNCTION__);
399 return false;
400 }
401
402 for (const auto& extensionKey : extension_metadata_keys) {
403 if (std::find(requestKeys.data.i32, requestKeys.data.i32 + requestKeys.count, extensionKey)
404 != requestKeys.data.i32 + requestKeys.count) {
405 return true;
406 }
407
408 if (std::find(resultKeys.data.i32, resultKeys.data.i32 + resultKeys.count, extensionKey)
409 != resultKeys.data.i32 + resultKeys.count) {
410 return true;
411 }
412 }
413
414 return false;
415}
416
417status_t filterExtensionKeys(CameraMetadata* metadata /*out*/) {
418 if (metadata == nullptr) {
419 return BAD_VALUE;
420 }
421
422 for (const auto& key : extension_metadata_keys) {
423 status_t res = metadata->erase(key);
424 if (res != OK) {
425 ALOGE("%s metadata key %d could not be erased", __FUNCTION__, key);
426 return res;
427 }
428 }
429 return OK;
430}
431
Xin Li65d53082023-08-25 14:16:11 -0700432} // namespace android::hardware::cameraservice::utils::conversion::aidl