1 /*
2 * Copyright 2023 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 #include <gui/FrameRateUtils.h>
18 #include <system/window.h>
19 #include <utils/Log.h>
20
21 #include <cmath>
22
23 #include <com_android_graphics_libgui_flags.h>
24
25 namespace android {
26 using namespace com::android::graphics::libgui;
27 // Returns true if the frameRate is valid.
28 //
29 // @param frameRate the frame rate in Hz
30 // @param compatibility a ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_*
31 // @param changeFrameRateStrategy a ANATIVEWINDOW_CHANGE_FRAME_RATE_*
32 // @param functionName calling function or nullptr. Used for logging
33 // @param privileged whether caller has unscoped surfaceflinger access
ValidateFrameRate(float frameRate,int8_t compatibility,int8_t changeFrameRateStrategy,const char * inFunctionName,bool privileged)34 bool ValidateFrameRate(float frameRate, int8_t compatibility, int8_t changeFrameRateStrategy,
35 const char* inFunctionName, bool privileged) {
36 const char* functionName = inFunctionName != nullptr ? inFunctionName : "call";
37 int floatClassification = std::fpclassify(frameRate);
38 if (frameRate < 0 || floatClassification == FP_INFINITE || floatClassification == FP_NAN) {
39 ALOGE("%s failed - invalid frame rate %f", functionName, frameRate);
40 return false;
41 }
42
43 if (compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT &&
44 compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE &&
45 compatibility != ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_GTE &&
46 (!privileged ||
47 (compatibility != ANATIVEWINDOW_FRAME_RATE_EXACT &&
48 compatibility != ANATIVEWINDOW_FRAME_RATE_NO_VOTE))) {
49 ALOGE("%s failed - invalid compatibility value %d privileged: %s", functionName,
50 compatibility, privileged ? "yes" : "no");
51 return false;
52 }
53
54 if (__builtin_available(android 31, *)) {
55 if (changeFrameRateStrategy != ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS &&
56 changeFrameRateStrategy != ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS) {
57 ALOGE("%s failed - invalid change frame rate strategy value %d", functionName,
58 changeFrameRateStrategy);
59 if (flags::bq_setframerate()) {
60 return false;
61 }
62 }
63 }
64
65 return true;
66 }
67
68 } // namespace android
69