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 #ifndef ANDROID_SERVERS_CAMERA_UTILS_H
18 #define ANDROID_SERVERS_CAMERA_UTILS_H
19 
20 #include <sched.h>
21 #include <unistd.h>
22 
23 namespace android {
24 
25 /**
26  * As of Android V, ro.board.api_level returns the year and month of release (ex. 202404)
27  * instead of release SDK version. This function maps year/month format back to release
28  * SDK version.
29  *
30  * Returns defaultVersion if the property is not found.
31  */
32 int getVNDKVersionFromProp(int defaultVersion);
33 
34 /**
35  * An instance of this class will raise the scheduling policy of a given
36  * given thread to real time and keep it this way throughout the lifetime
37  * of the object. The thread scheduling policy will revert back to its original
38  * state after the instances is released. By default the implementation will
39  * raise the priority of the current thread unless clients explicitly specify
40  * another thread id.
41  * Client must avoid:
42  *  - Keeping an instance of this class for extended and long running operations.
43  *    This is only intended for short/temporarily priority bumps that mitigate
44  *    scheduling delays within critical camera paths.
45  *  - Allocating instances of this class on the memory heap unless clients have
46  *    complete control over the object lifetime. It is preferable to allocate
47  *    instances of this class on the stack instead.
48  *  - Nesting multiple instances of this class using the same default or same thread id.
49  */
50 class RunThreadWithRealtimePriority final {
51   public:
52     RunThreadWithRealtimePriority(int tid = gettid());
53     ~RunThreadWithRealtimePriority();
54 
55     RunThreadWithRealtimePriority(const RunThreadWithRealtimePriority&) = delete;
56     RunThreadWithRealtimePriority& operator=(const RunThreadWithRealtimePriority&) = delete;
57 
58     // SCHED_FIFO priority for request submission thread in HFR mode
59     static const int kRequestThreadPriority = 1;
60 
61   private:
62     int mTid;
63     int mPreviousPolicy;
64     bool mPolicyBumped = false;
65     struct sched_param mPreviousParams;
66 };
67 
68 } // namespace android
69 
70 #endif //ANDROID_SERVERS_CAMERA_UTILS_H
71