• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cinttypes>
18 
19 #include <android-base/stringprintf.h>
20 #include <binder/Parcel.h>
21 #include <graphicsenv/FeatureOverrides.h>
22 
23 namespace android {
24 
25 using base::StringAppendF;
26 
writeToParcel(Parcel * parcel) const27 status_t FeatureConfig::writeToParcel(Parcel* parcel) const {
28     status_t status;
29 
30     status = parcel->writeUtf8AsUtf16(mFeatureName);
31     if (status != OK) {
32         return status;
33     }
34     status = parcel->writeBool(mEnabled);
35     if (status != OK) {
36         return status;
37     }
38     // Number of GPU vendor IDs.
39     status = parcel->writeVectorSize(mGpuVendorIDs);
40     if (status != OK) {
41         return status;
42     }
43     // GPU vendor IDs.
44     for (const auto& vendorID : mGpuVendorIDs) {
45         status = parcel->writeUint32(vendorID);
46         if (status != OK) {
47             return status;
48         }
49     }
50 
51     return OK;
52 }
53 
readFromParcel(const Parcel * parcel)54 status_t FeatureConfig::readFromParcel(const Parcel* parcel) {
55     status_t status;
56 
57     status = parcel->readUtf8FromUtf16(&mFeatureName);
58     if (status != OK) {
59         return status;
60     }
61     status = parcel->readBool(&mEnabled);
62     if (status != OK) {
63         return status;
64     }
65     // Number of GPU vendor IDs.
66     int numGpuVendorIDs;
67     status = parcel->readInt32(&numGpuVendorIDs);
68     if (status != OK) {
69         return status;
70     }
71     // GPU vendor IDs.
72     for (int i = 0; i < numGpuVendorIDs; i++) {
73         uint32_t gpuVendorIdUint;
74         status = parcel->readUint32(&gpuVendorIdUint);
75         if (status != OK) {
76             return status;
77         }
78         mGpuVendorIDs.emplace_back(gpuVendorIdUint);
79     }
80 
81     return OK;
82 }
83 
toString() const84 std::string FeatureConfig::toString() const {
85     std::string result;
86     StringAppendF(&result, "Feature: %s\n", mFeatureName.c_str());
87     StringAppendF(&result, "      Status: %s\n", mEnabled ? "enabled" : "disabled");
88     for (const auto& vendorID : mGpuVendorIDs) {
89         // vkjson outputs decimal, so print both formats.
90         StringAppendF(&result, "      GPU Vendor ID: 0x%04X (%d)\n", vendorID, vendorID);
91     }
92 
93     return result;
94 }
95 
writeToParcel(Parcel * parcel) const96 status_t FeatureOverrides::writeToParcel(Parcel* parcel) const {
97     status_t status;
98     // Number of global feature configs.
99     status = parcel->writeVectorSize(mGlobalFeatures);
100     if (status != OK) {
101         return status;
102     }
103     // Global feature configs.
104     for (const auto& cfg : mGlobalFeatures) {
105         status = cfg.writeToParcel(parcel);
106         if (status != OK) {
107             return status;
108         }
109     }
110     // Number of package feature overrides.
111     status = parcel->writeInt32(static_cast<int32_t>(mPackageFeatures.size()));
112     if (status != OK) {
113         return status;
114     }
115     for (const auto& feature : mPackageFeatures) {
116         // Package name.
117         status = parcel->writeUtf8AsUtf16(feature.first);
118         if (status != OK) {
119             return status;
120         }
121         // Number of package feature configs.
122         status = parcel->writeVectorSize(feature.second);
123         if (status != OK) {
124             return status;
125         }
126         // Package feature configs.
127         for (const auto& cfg : feature.second) {
128             status = cfg.writeToParcel(parcel);
129             if (status != OK) {
130                 return status;
131             }
132         }
133     }
134 
135     return OK;
136 }
137 
readFromParcel(const Parcel * parcel)138 status_t FeatureOverrides::readFromParcel(const Parcel* parcel) {
139     status_t status;
140 
141     // Number of global feature configs.
142     status = parcel->resizeOutVector(&mGlobalFeatures);
143     if (status != OK) {
144         return status;
145     }
146     // Global feature configs.
147     for (FeatureConfig& cfg : mGlobalFeatures) {
148         status = cfg.readFromParcel(parcel);
149         if (status != OK) {
150             return status;
151         }
152     }
153 
154     // Number of package feature overrides.
155     int numPkgOverrides;
156     status = parcel->readInt32(&numPkgOverrides);
157     if (status != OK) {
158         return status;
159     }
160     // Package feature overrides.
161     for (int i = 0; i < numPkgOverrides; i++) {
162         // Package name.
163         std::string name;
164         status = parcel->readUtf8FromUtf16(&name);
165         if (status != OK) {
166             return status;
167         }
168         std::vector<FeatureConfig> cfgs;
169         // Number of package feature configs.
170         int numCfgs;
171         status = parcel->readInt32(&numCfgs);
172         if (status != OK) {
173             return status;
174         }
175         // Package feature configs.
176         for (int j = 0; j < numCfgs; j++) {
177             FeatureConfig cfg;
178             status = cfg.readFromParcel(parcel);
179             if (status != OK) {
180                 return status;
181             }
182             cfgs.emplace_back(cfg);
183         }
184         mPackageFeatures[name] = cfgs;
185     }
186 
187     return OK;
188 }
189 
toString() const190 std::string FeatureOverrides::toString() const {
191     std::string result;
192     result.append("Global Features:\n");
193     for (auto& cfg : mGlobalFeatures) {
194         result.append("  " + cfg.toString());
195     }
196     result.append("\n");
197     result.append("Package Features:\n");
198     for (const auto& packageFeature : mPackageFeatures) {
199         result.append("  Package:");
200         StringAppendF(&result, " %s\n", packageFeature.first.c_str());
201         for (auto& cfg : packageFeature.second) {
202             result.append("    " + cfg.toString());
203         }
204     }
205 
206     return result;
207 }
208 
209 } // namespace android
210