1 /*
2  * Copyright (C) 2021 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 <cstdint>
20 #include <functional>
21 #include <mutex>
22 #include <optional>
23 #include <string>
24 
25 namespace android {
26 // Manages flags for SurfaceFlinger, including default values, system properties, and Mendel
27 // experiment configuration values. Can be called from any thread.
28 class FlagManager {
29 private:
30     // Effectively making the constructor private, while allowing std::make_unique to work
31     struct ConstructorTag {};
32 
33 public:
34     static const FlagManager& getInstance();
35     static FlagManager& getMutableInstance();
36 
37     FlagManager(ConstructorTag);
38     virtual ~FlagManager();
39 
40     void markBootCompleted();
41     void dump(std::string& result) const;
42 
43     void setUnitTestMode();
44 
45     /// Legacy server flags ///
46     bool test_flag() const;
47     bool use_adpf_cpu_hint() const;
48     bool use_skia_tracing() const;
49 
50     /// Trunk stable server flags ///
51     bool refresh_rate_overlay_on_external_display() const;
52     bool adpf_gpu_sf() const;
53     bool adpf_use_fmq_channel() const;
54     bool adpf_use_fmq_channel_fixed() const;
55 
56     /// Trunk stable readonly flags ///
57     bool connected_display() const;
58     bool frame_rate_category_mrr() const;
59     bool enable_small_area_detection() const;
60     bool misc1() const;
61     bool vrr_config() const;
62     bool hotplug2() const;
63     bool hdcp_level_hal() const;
64     bool multithreaded_present() const;
65     bool add_sf_skipped_frames_to_trace() const;
66     bool use_known_refresh_rate_for_fps_consistency() const;
67     bool cache_when_source_crop_layer_only_moved() const;
68     bool enable_fro_dependent_features() const;
69     bool display_protected() const;
70     bool fp16_client_target() const;
71     bool game_default_frame_rate() const;
72     bool enable_layer_command_batching() const;
73     bool screenshot_fence_preservation() const;
74     bool vulkan_renderengine() const;
75     bool vrr_bugfix_24q4() const;
76     bool renderable_buffer_usage() const;
77     bool restore_blur_step() const;
78     bool dont_skip_on_early_ro() const;
79     bool protected_if_client() const;
80     bool ce_fence_promise() const;
81     bool idle_screen_refresh_rate_timeout() const;
82     bool graphite_renderengine() const;
83     bool latch_unsignaled_with_auto_refresh_changed() const;
84     bool deprecate_vsync_sf() const;
85     bool allow_n_vsyncs_in_targeter() const;
86     bool detached_mirror() const;
87     bool commit_not_composited() const;
88     bool local_tonemap_screenshots() const;
89     bool override_trusted_overlay() const;
90     bool flush_buffer_slots_to_uncache() const;
91     bool force_compile_graphite_renderengine() const;
92     bool single_hop_screenshot() const;
93     bool trace_frame_rate_override() const;
94 
95 protected:
96     // overridden for unit tests
97     virtual std::optional<bool> getBoolProperty(const char*) const;
98     virtual bool getServerConfigurableFlag(const char*) const;
99 
100 private:
101     friend class TestableFlagManager;
102 
103     FlagManager(const FlagManager&) = delete;
104 
105     void dumpFlag(std::string& result, bool readonly, const char* name,
106                   std::function<bool()> getter) const;
107 
108     std::atomic_bool mBootCompleted = false;
109     std::atomic_bool mUnitTestMode = false;
110 
111     static std::unique_ptr<FlagManager> mInstance;
112     static std::once_flag mOnce;
113 };
114 } // namespace android
115