blob: 34c0ed8c2ef132ffdb50cfdd5ab6d2bdc6a71fde [file] [log] [blame]
Avichal Rakesh74b5ae72023-12-27 16:56:45 -08001/*
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
Emilian Peev31bd2422024-04-23 22:24:09 +000017#define LOG_TAG "Camera3-Utils"
18
Avichal Rakesh74b5ae72023-12-27 16:56:45 -080019#include "Utils.h"
20#include <android-base/properties.h>
21#include <com_android_internal_camera_flags.h>
Emilian Peev31bd2422024-04-23 22:24:09 +000022#include <utils/Errors.h>
23#include <utils/Log.h>
Avichal Rakesh74b5ae72023-12-27 16:56:45 -080024
25namespace android {
26
Emilian Peev31bd2422024-04-23 22:24:09 +000027namespace flags = com::android::internal::camera::flags;
Avichal Rakesh74b5ae72023-12-27 16:56:45 -080028
29constexpr const char *LEGACY_VNDK_VERSION_PROP = "ro.vndk.version";
30constexpr const char *BOARD_API_LEVEL_PROP = "ro.board.api_level";
31constexpr int MAX_VENDOR_API_LEVEL = 1000000;
32constexpr int FIRST_VNDK_VERSION = 202404;
33
34int getVNDKVersionFromProp(int defaultVersion) {
35 if (!com_android_internal_camera_flags_use_ro_board_api_level_for_vndk_version()) {
36 return base::GetIntProperty(LEGACY_VNDK_VERSION_PROP, defaultVersion);
37 }
38
39 int vndkVersion = base::GetIntProperty(BOARD_API_LEVEL_PROP, MAX_VENDOR_API_LEVEL);
40
41 if (vndkVersion == MAX_VENDOR_API_LEVEL) {
42 // Couldn't find property
43 return defaultVersion;
44 }
45
46 if (vndkVersion < __ANDROID_API_V__) {
47 // VNDK versions below V return the corresponding SDK version.
48 return vndkVersion;
49 }
50
51 // VNDK for Android V and above are of the format YYYYMM starting with 202404 and is bumped
52 // up once a year. So V would be 202404 and the next one would be 202504.
53 // This is the same assumption as that made in system/core/init/property_service.cpp.
54 vndkVersion = (vndkVersion - FIRST_VNDK_VERSION) / 100;
55 return __ANDROID_API_V__ + vndkVersion;
56}
57
Emilian Peev31bd2422024-04-23 22:24:09 +000058RunThreadWithRealtimePriority::RunThreadWithRealtimePriority(int tid)
59 : mTid(tid), mPreviousPolicy(sched_getscheduler(tid)) {
60 if (flags::realtime_priority_bump()) {
61 auto res = sched_getparam(mTid, &mPreviousParams);
62 if (res != OK) {
63 ALOGE("Can't retrieve thread scheduler parameters: %s (%d)", strerror(-res), res);
64 return;
65 }
66
67 struct sched_param param = {0};
68 param.sched_priority = kRequestThreadPriority;
69
70 res = sched_setscheduler(mTid, SCHED_FIFO, &param);
71 if (res != OK) {
72 ALOGW("Can't set realtime priority for thread: %s (%d)", strerror(-res), res);
73 } else {
74 ALOGD("Set real time priority for thread (tid %d)", mTid);
75 mPolicyBumped = true;
76 }
77 }
78}
79
80RunThreadWithRealtimePriority::~RunThreadWithRealtimePriority() {
81 if (mPolicyBumped && flags::realtime_priority_bump()) {
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