blob: 01aa7ed43c8bec030a0e561ec0d1b38961a42d5c [file] [log] [blame]
Ady Abraham6cdd3fd2023-09-07 18:45:58 -07001/*
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
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070017#include <gui/FrameRateUtils.h>
18#include <system/window.h>
19#include <utils/Log.h>
20
21#include <cmath>
22
Ady Abraham107788e2023-10-17 12:31:08 -070023#include <com_android_graphics_libgui_flags.h>
24
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070025namespace android {
Ady Abraham107788e2023-10-17 12:31:08 -070026using namespace com::android::graphics::libgui;
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070027// 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
34bool 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 &&
Rachel Leeef9fb682024-02-23 11:04:33 -080045 compatibility != ANATIVEWINDOW_FRAME_RATE_GTE &&
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070046 (!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);
Ady Abraham107788e2023-10-17 12:31:08 -070059 if (flags::bq_setframerate()) {
Ady Abraham6cdd3fd2023-09-07 18:45:58 -070060 return false;
61 }
62 }
63 }
64
65 return true;
66}
67
68} // namespace android