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 "pixelstats-display.h"
18 #include <aidl/android/frameworks/stats/IStats.h>
19 #include <hardware/google/pixel/pixelstats/pixelatoms.pb.h>
20 #include "pixelatoms_defs.h"
21 
22 #include <android-base/logging.h>
23 #include <android/binder_manager.h>
24 #include <android/binder_process.h>
25 #include <sys/types.h>
26 #include <utils/Errors.h>
27 
28 namespace PixelAtoms = hardware::google::pixel::PixelAtoms;
29 
getStatsService()30 std::shared_ptr<IStats> getStatsService() {
31     const std::string instance = std::string() + IStats::descriptor + "/default";
32     static std::mutex mutex;
33     static bool isStatsDeclared = false;
34 
35     const std::lock_guard<std::mutex> lock(mutex);
36     if (!isStatsDeclared) {
37         isStatsDeclared = AServiceManager_isDeclared(instance.c_str());
38         if (!isStatsDeclared) {
39             LOG(ERROR) << "Stats service is not registered.";
40             return nullptr;
41         }
42     }
43 
44     return IStats::fromBinder(ndk::SpAIBinder(AServiceManager_getService(instance.c_str())));
45 }
46 
reportDisplayPortUsage(uint32_t width,uint32_t height,float refreshRate,uint32_t vendorId,uint32_t productId,bool enabled)47 void reportDisplayPortUsage(uint32_t width, uint32_t height, float refreshRate, uint32_t vendorId,
48                             uint32_t productId, bool enabled) {
49     const std::shared_ptr<IStats> stats_client = getStatsService();
50     if (!stats_client) {
51         LOG(ERROR) << "Unable to get AIDL Stats service";
52         return;
53     }
54 
55     std::vector<VendorAtomValue> values;
56     VendorAtomValue tmp;
57     tmp.set<VendorAtomValue::intValue>(width);
58     values.push_back(tmp);
59     tmp.set<VendorAtomValue::intValue>(height);
60     values.push_back(tmp);
61     tmp.set<VendorAtomValue::floatValue>(refreshRate);
62     values.push_back(tmp);
63     tmp.set<VendorAtomValue::intValue>(vendorId);
64     values.push_back(tmp);
65     tmp.set<VendorAtomValue::intValue>(productId);
66     values.push_back(tmp);
67     tmp.set<VendorAtomValue::boolValue>(enabled);
68     values.push_back(tmp);
69 
70     VendorAtom event = {.atomId = PixelAtoms::DISPLAY_PORT_USAGE, .values = std::move(values)};
71     const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
72     if (!ret.isOk()) LOG(ERROR) << "Unable to report DisplayPortUsage to IStats service";
73 }
74