1 /*
2  * Copyright 2022 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 <android-base/thread_annotations.h>
20 #include <gui/DisplayEventReceiver.h>
21 #include <scheduler/Fps.h>
22 #include <sys/types.h>
23 #include <map>
24 #include <optional>
25 
26 #include "Utils/Dumper.h"
27 
28 namespace android::scheduler {
29 
30 class FrameRateOverrideMappings {
31     using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
32     using UidToFrameRateOverride = std::map<uid_t, Fps>;
33 
34 public:
35     std::optional<Fps> getFrameRateOverrideForUid(uid_t uid,
36                                                   bool supportsFrameRateOverrideByContent) const
37             EXCLUDES(mFrameRateOverridesLock);
38     std::vector<FrameRateOverride> getAllFrameRateOverrides(bool supportsFrameRateOverrideByContent)
39             EXCLUDES(mFrameRateOverridesLock);
40     bool updateFrameRateOverridesByContent(const UidToFrameRateOverride& frameRateOverrides)
41             EXCLUDES(mFrameRateOverridesLock);
42     void setGameModeRefreshRateForUid(FrameRateOverride frameRateOverride)
43             EXCLUDES(mFrameRateOverridesLock);
44     void setPreferredRefreshRateForUid(FrameRateOverride frameRateOverride)
45             EXCLUDES(mFrameRateOverridesLock);
46 
47     void dump(utils::Dumper&) const;
48 
49 private:
maxOverridesCount()50     size_t maxOverridesCount() const REQUIRES(mFrameRateOverridesLock) {
51         return std::max({mFrameRateOverridesByContent.size(),
52                          mFrameRateOverridesFromGameManager.size(),
53                          mFrameRateOverridesFromBackdoor.size()});
54     }
55 
56     void dump(utils::Dumper&, std::string_view name, const UidToFrameRateOverride&) const;
57 
58     // The frame rate override lists need their own mutex as they are being read
59     // by SurfaceFlinger, Scheduler and EventThread (as a callback) to prevent deadlocks
60     mutable std::mutex mFrameRateOverridesLock;
61 
62     // mappings between a UID and a preferred refresh rate that this app would
63     // run at.
64     UidToFrameRateOverride mFrameRateOverridesByContent GUARDED_BY(mFrameRateOverridesLock);
65     UidToFrameRateOverride mFrameRateOverridesFromBackdoor GUARDED_BY(mFrameRateOverridesLock);
66     UidToFrameRateOverride mFrameRateOverridesFromGameManager GUARDED_BY(mFrameRateOverridesLock);
67 };
68 
69 } // namespace android::scheduler
70