|
| 1 | +/* |
| 2 | + * Copyright 2025 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 <feature_override/FeatureOverrideParser.h> |
| 18 | + |
| 19 | +#include <chrono> |
| 20 | +#include <fstream> |
| 21 | +#include <sstream> |
| 22 | +#include <string> |
| 23 | +#include <sys/stat.h> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include <graphicsenv/FeatureOverrides.h> |
| 27 | +#include <log/log.h> |
| 28 | + |
| 29 | +#include "feature_config.pb.h" |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +void resetFeatureOverrides(android::FeatureOverrides &featureOverrides) { |
| 34 | + featureOverrides.mGlobalFeatures.clear(); |
| 35 | + featureOverrides.mPackageFeatures.clear(); |
| 36 | +} |
| 37 | + |
| 38 | +void initFeatureConfig(android::FeatureConfig &featureConfig, |
| 39 | + const feature_override::FeatureConfig &featureConfigProto) { |
| 40 | + featureConfig.mFeatureName = featureConfigProto.feature_name(); |
| 41 | + featureConfig.mEnabled = featureConfigProto.enabled(); |
| 42 | +} |
| 43 | + |
| 44 | +feature_override::FeatureOverrideProtos readFeatureConfigProtos(std::string configFilePath) { |
| 45 | + feature_override::FeatureOverrideProtos overridesProtos; |
| 46 | + |
| 47 | + std::ifstream protobufBinaryFile(configFilePath.c_str()); |
| 48 | + if (protobufBinaryFile.fail()) { |
| 49 | + ALOGE("Failed to open feature config file: `%s`.", configFilePath.c_str()); |
| 50 | + return overridesProtos; |
| 51 | + } |
| 52 | + |
| 53 | + std::stringstream buffer; |
| 54 | + buffer << protobufBinaryFile.rdbuf(); |
| 55 | + std::string serializedConfig = buffer.str(); |
| 56 | + std::vector<uint8_t> serialized( |
| 57 | + reinterpret_cast<const uint8_t *>(serializedConfig.data()), |
| 58 | + reinterpret_cast<const uint8_t *>(serializedConfig.data()) + |
| 59 | + serializedConfig.size()); |
| 60 | + |
| 61 | + if (!overridesProtos.ParseFromArray(serialized.data(), |
| 62 | + static_cast<int>(serialized.size()))) { |
| 63 | + ALOGE("Failed to parse GpuConfig protobuf data."); |
| 64 | + } |
| 65 | + |
| 66 | + return overridesProtos; |
| 67 | +} |
| 68 | + |
| 69 | +} // namespace |
| 70 | + |
| 71 | +namespace android { |
| 72 | + |
| 73 | +std::string FeatureOverrideParser::getFeatureOverrideFilePath() const { |
| 74 | + const std::string kConfigFilePath = "/system/etc/angle/feature_config_vk.binarypb"; |
| 75 | + |
| 76 | + return kConfigFilePath; |
| 77 | +} |
| 78 | + |
| 79 | +bool FeatureOverrideParser::shouldReloadFeatureOverrides() const { |
| 80 | + std::string configFilePath = getFeatureOverrideFilePath(); |
| 81 | + struct stat fileStat{}; |
| 82 | + if (stat(getFeatureOverrideFilePath().c_str(), &fileStat) != 0) { |
| 83 | + ALOGE("Error getting file information for '%s': %s", getFeatureOverrideFilePath().c_str(), |
| 84 | + strerror(errno)); |
| 85 | + // stat'ing the file failed, so return false since reading it will also likely fail. |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + return fileStat.st_mtime > mLastProtobufReadTime; |
| 90 | +} |
| 91 | + |
| 92 | +void FeatureOverrideParser::forceFileRead() { |
| 93 | + resetFeatureOverrides(mFeatureOverrides); |
| 94 | + mLastProtobufReadTime = 0; |
| 95 | +} |
| 96 | + |
| 97 | +void FeatureOverrideParser::parseFeatureOverrides() { |
| 98 | + const feature_override::FeatureOverrideProtos overridesProtos = readFeatureConfigProtos( |
| 99 | + getFeatureOverrideFilePath()); |
| 100 | + |
| 101 | + // Global feature overrides. |
| 102 | + for (const auto &featureConfigProto: overridesProtos.global_features()) { |
| 103 | + FeatureConfig featureConfig; |
| 104 | + initFeatureConfig(featureConfig, featureConfigProto); |
| 105 | + |
| 106 | + mFeatureOverrides.mGlobalFeatures.emplace_back(featureConfig); |
| 107 | + } |
| 108 | + |
| 109 | + // App-specific feature overrides. |
| 110 | + for (auto const &pkgConfigProto: overridesProtos.package_features()) { |
| 111 | + const std::string &packageName = pkgConfigProto.package_name(); |
| 112 | + |
| 113 | + if (mFeatureOverrides.mPackageFeatures.count(packageName)) { |
| 114 | + ALOGE("Package already has feature overrides! Skipping."); |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + std::vector<FeatureConfig> featureConfigs; |
| 119 | + for (const auto &featureConfigProto: pkgConfigProto.feature_configs()) { |
| 120 | + FeatureConfig featureConfig; |
| 121 | + initFeatureConfig(featureConfig, featureConfigProto); |
| 122 | + |
| 123 | + featureConfigs.emplace_back(featureConfig); |
| 124 | + } |
| 125 | + |
| 126 | + mFeatureOverrides.mPackageFeatures[packageName] = featureConfigs; |
| 127 | + } |
| 128 | + |
| 129 | + mLastProtobufReadTime = std::chrono::system_clock::to_time_t( |
| 130 | + std::chrono::system_clock::now()); |
| 131 | +} |
| 132 | + |
| 133 | +FeatureOverrides FeatureOverrideParser::getFeatureOverrides() { |
| 134 | + if (shouldReloadFeatureOverrides()) { |
| 135 | + parseFeatureOverrides(); |
| 136 | + } |
| 137 | + |
| 138 | + return mFeatureOverrides; |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace android |
0 commit comments