1 /*
2 * Copyright (C) 2024 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 "Camera3-Utils"
18
19 #include "Utils.h"
20 #include <android-base/properties.h>
21 #include <com_android_internal_camera_flags.h>
22 #include <utils/Errors.h>
23 #include <utils/Log.h>
24 #include <vendorsupport/api_level.h>
25
26 #include <camera/CameraUtils.h>
27
28 namespace android {
29
30 /**
31 * Returns defaultVersion if the property is not found.
32 */
getVNDKVersionFromProp(int defaultVersion)33 int getVNDKVersionFromProp(int defaultVersion) {
34 int vendorApiLevel = AVendorSupport_getVendorApiLevel();
35 if (vendorApiLevel == 0) {
36 // Couldn't find vendor API level, return default
37 return defaultVersion;
38 }
39
40 // Vendor API level for Android V and above are of the format YYYYMM starting with 202404.
41 // AVendorSupport_getSdkApiLevelOf maps them back to SDK API levels while leaving older
42 // values unchanged.
43 return AVendorSupport_getSdkApiLevelOf(vendorApiLevel);
44 }
45
getVNDKVersion()46 int getVNDKVersion() {
47 static int kVndkVersion = getVNDKVersionFromProp(__ANDROID_API_FUTURE__);
48 return kVndkVersion;
49 }
50
getDeviceId(const CameraMetadata & cameraInfo)51 int32_t getDeviceId(const CameraMetadata& cameraInfo) {
52 if (!cameraInfo.exists(ANDROID_INFO_DEVICE_ID)) {
53 return kDefaultDeviceId;
54 }
55
56 const auto &deviceIdEntry = cameraInfo.find(ANDROID_INFO_DEVICE_ID);
57 return deviceIdEntry.data.i32[0];
58 }
59
RunThreadWithRealtimePriority(int tid)60 RunThreadWithRealtimePriority::RunThreadWithRealtimePriority(int tid)
61 : mTid(tid), mPreviousPolicy(sched_getscheduler(tid)) {
62 auto res = sched_getparam(mTid, &mPreviousParams);
63 if (res != OK) {
64 ALOGE("Can't retrieve thread scheduler parameters: %s (%d)", strerror(-res), res);
65 return;
66 }
67
68 struct sched_param param = {0};
69 param.sched_priority = kRequestThreadPriority;
70
71 res = sched_setscheduler(mTid, SCHED_FIFO, ¶m);
72 if (res != OK) {
73 ALOGW("Can't set realtime priority for thread: %s (%d)", strerror(-res), res);
74 } else {
75 ALOGD("Set real time priority for thread (tid %d)", mTid);
76 mPolicyBumped = true;
77 }
78 }
79
~RunThreadWithRealtimePriority()80 RunThreadWithRealtimePriority::~RunThreadWithRealtimePriority() {
81 if (mPolicyBumped) {
82 auto res = sched_setscheduler(mTid, mPreviousPolicy, &mPreviousParams);
83 if (res != OK) {
84 ALOGE("Can't set regular priority for thread: %s (%d)", strerror(-res), res);
85 } else {
86 ALOGD("Set regular priority for thread (tid %d)", mTid);
87 }
88 }
89 }
90
91 } // namespace android
92