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