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 #pragma once
18 
19 #include <unordered_map>
20 
21 #include <android-base/logging.h>
22 #include <android-base/thread_annotations.h>
23 #include <android/binder_manager.h>
24 
25 #include <aidl/android/hardware/power/stats/StateResidency.h>
26 #include <aidl/android/vendor/powerstats/BnPixelStateResidencyCallback.h>
27 #include <aidl/android/vendor/powerstats/IPixelStateResidencyCallback.h>
28 #include <aidl/android/vendor/powerstats/IPixelStateResidencyProvider.h>
29 
30 #include <thread>
31 
32 #include "../Statistics/VariableRefreshRateStatistic.h"
33 #include "DisplayStateResidencyProvider.h"
34 
35 using aidl::android::hardware::power::stats::StateResidency;
36 using aidl::android::vendor::powerstats::BnPixelStateResidencyCallback;
37 using aidl::android::vendor::powerstats::IPixelStateResidencyCallback;
38 using aidl::android::vendor::powerstats::IPixelStateResidencyProvider;
39 
40 namespace android::hardware::graphics::composer {
41 
42 class DisplayStateResidencyWatcher : public BnPixelStateResidencyCallback {
43 public:
44     DisplayStateResidencyWatcher(std::shared_ptr<CommonDisplayContextProvider> displayerInstance,
45                                  std::shared_ptr<StatisticsProvider> statisticsProvider);
46     virtual ~DisplayStateResidencyWatcher();
47 
48     virtual ndk::ScopedAStatus getStateResidency(std::vector<StateResidency>* stats) override;
49 
50     void registerWithPowerStats();
51     static void binderDied(void* cookie);
52 
53 private:
54     std::atomic_bool mRunning = true;
55     std::shared_ptr<IPixelStateResidencyProvider> mProvider;
56     ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
57     std::thread mTaskHandler;
58 
59     std::shared_ptr<StatisticsProvider> mStatisticsProvider;
60     std::unique_ptr<DisplayStateResidencyProvider> mDisplayPresentStatisticsProvider;
61 
62     std::string mEntityName;
63 };
64 
65 } // namespace android::hardware::graphics::composer
66