xref: /aosp_15_r20/frameworks/av/services/camera/libcameraservice/utils/Utils.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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 
getVNDKVersionFromProp(int defaultVersion)28 int getVNDKVersionFromProp(int defaultVersion) {
29     int vendorApiLevel = AVendorSupport_getVendorApiLevel();
30     if (vendorApiLevel == 0) {
31         // Couldn't find vendor API level, return default
32         return defaultVersion;
33     }
34 
35     // Vendor API level for Android V and above are of the format YYYYMM starting with 202404.
36     // AVendorSupport_getSdkApiLevelOf maps them back to SDK API levels while leaving older
37     // values unchanged.
38     return AVendorSupport_getSdkApiLevelOf(vendorApiLevel);
39 }
40 
RunThreadWithRealtimePriority(int tid)41 RunThreadWithRealtimePriority::RunThreadWithRealtimePriority(int tid)
42     : mTid(tid), mPreviousPolicy(sched_getscheduler(tid)) {
43     auto res = sched_getparam(mTid, &mPreviousParams);
44     if (res != OK) {
45         ALOGE("Can't retrieve thread scheduler parameters: %s (%d)", strerror(-res), res);
46         return;
47     }
48 
49     struct sched_param param = {0};
50     param.sched_priority = kRequestThreadPriority;
51 
52     res = sched_setscheduler(mTid, SCHED_FIFO, &param);
53     if (res != OK) {
54         ALOGW("Can't set realtime priority for thread: %s (%d)", strerror(-res), res);
55     } else {
56         ALOGD("Set real time priority for thread (tid %d)", mTid);
57         mPolicyBumped = true;
58     }
59 }
60 
~RunThreadWithRealtimePriority()61 RunThreadWithRealtimePriority::~RunThreadWithRealtimePriority() {
62     if (mPolicyBumped) {
63         auto res = sched_setscheduler(mTid, mPreviousPolicy, &mPreviousParams);
64         if (res != OK) {
65             ALOGE("Can't set regular priority for thread: %s (%d)", strerror(-res), res);
66         } else {
67             ALOGD("Set regular priority for thread (tid %d)", mTid);
68         }
69     }
70 }
71 
72 }  // namespace android
73