• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <type_traits>
23 
24 #include <camera/CameraMetadata.h>
25 
26 namespace android {
27 
28 /**
29  * Magically convert an enum to its underlying integer type, mostly so they can be
30  * printed with printf-style formatters without warnings.
31  * Backport of C++23 std::to_underlying()
32  */
33 template<typename Enum>
eToI(Enum val)34 constexpr std::underlying_type_t<Enum> eToI(Enum val) {
35     return static_cast<std::underlying_type_t<Enum>>(val);
36 }
37 
38 /**
39  * Helper function for getting the current VNDK version.
40  *
41  * If the current VNDK version cannot be determined, this function returns
42  * __ANDROID_API_FUTURE__.
43  */
44 int getVNDKVersion();
45 
46 /**
47  * Returns the deviceId for the given camera metadata. For any virtual camera, this is the id
48  * of the virtual device owning the camera. For any real camera, this is kDefaultDeviceId.
49  */
50 int32_t getDeviceId(const CameraMetadata& cameraInfo);
51 
52 /**
53  * An instance of this class will raise the scheduling policy of a given
54  * given thread to real time and keep it this way throughout the lifetime
55  * of the object. The thread scheduling policy will revert back to its original
56  * state after the instances is released. By default the implementation will
57  * raise the priority of the current thread unless clients explicitly specify
58  * another thread id.
59  * Client must avoid:
60  *  - Keeping an instance of this class for extended and long running operations.
61  *    This is only intended for short/temporarily priority bumps that mitigate
62  *    scheduling delays within critical camera paths.
63  *  - Allocating instances of this class on the memory heap unless clients have
64  *    complete control over the object lifetime. It is preferable to allocate
65  *    instances of this class on the stack instead.
66  *  - Nesting multiple instances of this class using the same default or same thread id.
67  */
68 class RunThreadWithRealtimePriority final {
69   public:
70     RunThreadWithRealtimePriority(int tid = gettid());
71     ~RunThreadWithRealtimePriority();
72 
73     RunThreadWithRealtimePriority(const RunThreadWithRealtimePriority&) = delete;
74     RunThreadWithRealtimePriority& operator=(const RunThreadWithRealtimePriority&) = delete;
75 
76     // SCHED_FIFO priority for request submission thread in HFR mode
77     static const int kRequestThreadPriority = 1;
78 
79   private:
80     int mTid;
81     int mPreviousPolicy;
82     bool mPolicyBumped = false;
83     struct sched_param mPreviousParams;
84 };
85 
86 } // namespace android
87 
88 #endif //ANDROID_SERVERS_CAMERA_UTILS_H
89