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 namespace android {
27 
28 namespace flags = com::android::internal::camera::flags;
29 
30 namespace {
31 constexpr const char* LEGACY_VNDK_VERSION_PROP = "ro.vndk.version";
32 constexpr const char* BOARD_API_LEVEL_PROP = "ro.board.api_level";
33 constexpr int MAX_VENDOR_API_LEVEL = 1000000;
34 constexpr int FIRST_VNDK_VERSION = 202404;
35 
legacyGetVNDKVersionFromProp(int defaultVersion)36 int legacyGetVNDKVersionFromProp(int defaultVersion) {
37     if (!flags::use_ro_board_api_level_for_vndk_version()) {
38         return base::GetIntProperty(LEGACY_VNDK_VERSION_PROP, defaultVersion);
39     }
40 
41     int vndkVersion = base::GetIntProperty(BOARD_API_LEVEL_PROP, MAX_VENDOR_API_LEVEL);
42 
43     if (vndkVersion == MAX_VENDOR_API_LEVEL) {
44         // Couldn't find property
45         return defaultVersion;
46     }
47 
48     if (vndkVersion < __ANDROID_API_V__) {
49         // VNDK versions below V return the corresponding SDK version.
50         return vndkVersion;
51     }
52 
53     // VNDK for Android V and above are of the format YYYYMM starting with 202404 and is bumped
54     // up once a year. So V would be 202404 and the next one would be 202504.
55     // This is the same assumption as that made in system/core/init/property_service.cpp.
56     vndkVersion = (vndkVersion - FIRST_VNDK_VERSION) / 100;
57     return __ANDROID_API_V__ + vndkVersion;
58 }
59 }  // anonymous namespace
60 
getVNDKVersionFromProp(int defaultVersion)61 int getVNDKVersionFromProp(int defaultVersion) {
62     if (!flags::use_system_api_for_vndk_version()) {
63         return legacyGetVNDKVersionFromProp(defaultVersion);
64     }
65 
66     int vendorApiLevel = AVendorSupport_getVendorApiLevel();
67     if (vendorApiLevel == 0) {
68         // Couldn't find vendor API level, return default
69         return defaultVersion;
70     }
71 
72     // Vendor API level for Android V and above are of the format YYYYMM starting with 202404.
73     // AVendorSupport_getSdkApiLevelOf maps them back to SDK API levels while leaving older
74     // values unchanged.
75     return AVendorSupport_getSdkApiLevelOf(vendorApiLevel);
76 }
77 
RunThreadWithRealtimePriority(int tid)78 RunThreadWithRealtimePriority::RunThreadWithRealtimePriority(int tid)
79     : mTid(tid), mPreviousPolicy(sched_getscheduler(tid)) {
80     if (flags::realtime_priority_bump()) {
81         auto res = sched_getparam(mTid, &mPreviousParams);
82         if (res != OK) {
83             ALOGE("Can't retrieve thread scheduler parameters: %s (%d)", strerror(-res), res);
84             return;
85         }
86 
87         struct sched_param param = {0};
88         param.sched_priority = kRequestThreadPriority;
89 
90         res = sched_setscheduler(mTid, SCHED_FIFO, &param);
91         if (res != OK) {
92             ALOGW("Can't set realtime priority for thread: %s (%d)", strerror(-res), res);
93         } else {
94             ALOGD("Set real time priority for thread (tid %d)", mTid);
95             mPolicyBumped = true;
96         }
97     }
98 }
99 
~RunThreadWithRealtimePriority()100 RunThreadWithRealtimePriority::~RunThreadWithRealtimePriority() {
101     if (mPolicyBumped && flags::realtime_priority_bump()) {
102         auto res = sched_setscheduler(mTid, mPreviousPolicy, &mPreviousParams);
103         if (res != OK) {
104             ALOGE("Can't set regular priority for thread: %s (%d)", strerror(-res), res);
105         } else {
106             ALOGD("Set regular priority for thread (tid %d)", mTid);
107         }
108     }
109 }
110 
111 }  // namespace android
112