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