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 #include "VariableRefreshRateVersion.h"
18
19 #include <android-base/properties.h>
20 #include <log/log.h>
21 #include <vector>
22
23 namespace android::hardware::graphics::composer {
24
getBoardApiLevel()25 long getBoardApiLevel() {
26 long apiLevel = 0;
27 auto apiLevelString = android::base::GetProperty("ro.board.api_level", "");
28 if (!apiLevelString.empty()) {
29 char* endPos;
30 apiLevel = std::strtol(apiLevelString.c_str(), &endPos, 10);
31 if (*endPos != 0) apiLevel = 0;
32 }
33 return apiLevel;
34 }
35
getDisplayXrrVersion(const std::string & displayTypeIdentifier)36 std::pair<int, int> getDisplayXrrVersion(const std::string& displayTypeIdentifier) {
37 char* endPos;
38 long apiLevel = getBoardApiLevel();
39 size_t pos = 0;
40 std::string versionString;
41 float version;
42
43 std::string xrrVersionPropertyId = std::string(kXrrVersionPropertyIdPrefix) + "." +
44 displayTypeIdentifier + "." + std::string(kXrrVersionPropertyIdSuffix);
45 auto xrrVersionString = android::base::GetProperty(xrrVersionPropertyId, "");
46
47 std::vector<std::string> patterns;
48 while ((pos = xrrVersionString.find(':')) != std::string::npos) {
49 patterns.emplace_back(xrrVersionString.substr(0, pos));
50 xrrVersionString.erase(0, pos + 1);
51 }
52 patterns.emplace_back(xrrVersionString);
53 for (auto& pattern : patterns) {
54 pos = pattern.find('@');
55 if (pos == std::string::npos) {
56 // There are no limitations for this setting, so we can apply it directly.
57 versionString = pattern;
58 break;
59 } else {
60 std::string candidateVersionString = pattern.substr(0, pos);
61 pattern.erase(0, pos + 1);
62 if (!pattern.empty()) {
63 long minApiLevel = std::strtol(pattern.c_str(), &endPos, 10);
64 if (minApiLevel == 0) continue;
65 if (minApiLevel <= apiLevel) {
66 versionString = candidateVersionString;
67 break;
68 }
69 } else {
70 ALOGW("%s() disregard this setting by an empty minimum API level.", __func__);
71 }
72 }
73 }
74
75 // If the versionString does not represent a floating-point number, std::atof will return 0.0f.
76 version = std::atof(versionString.c_str());
77 // The integer part represents the major version.
78 int majorVersion = static_cast<int>(version);
79 // The decimal part represents the minor version.
80 int minorVersion = static_cast<int>(version * 10) % 10;
81
82 if ((majorVersion > kTotalXrrVersion) || (majorVersion < kMrr)) {
83 ALOGE("%s(): Illegal XRR major version (%d) detected; use MRR V1 instead.", __func__,
84 majorVersion);
85 majorVersion = kMrr;
86 minorVersion = kMrrDefaultVersion;
87 } else {
88 if ((minorVersion <= 0) ||
89 (minorVersion > ((majorVersion == kMrr) ? kMaxMrrVersion : kMaxVrrVersion))) {
90 ALOGE("%s(): Illegal XRR minor version (%d) detected; use default instead.", __func__,
91 minorVersion);
92 minorVersion = (majorVersion == kMrr ? kMrrDefaultVersion : kVrrDefaultVersion);
93 }
94 }
95 return std::make_pair(majorVersion, minorVersion);
96 }
97
98 } // namespace android::hardware::graphics::composer
99