1 /*
2  * Copyright 2019 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 <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
18 #include <android/hardware/configstore/1.1/ISurfaceFlingerConfigs.h>
19 #include <android/hardware/configstore/1.1/types.h>
20 #include <configstore/Utils.h>
21 
22 #include <cstdlib>
23 #include <tuple>
24 
25 #include "SurfaceFlingerProperties.h"
26 
27 namespace android {
28 namespace sysprop {
29 using namespace android::hardware::configstore;
30 using namespace android::hardware::configstore::V1_0;
31 using android::hardware::graphics::common::V1_2::Dataspace;
32 using android::hardware::graphics::common::V1_2::PixelFormat;
33 using android::ui::DisplayPrimaries;
34 
vsync_event_phase_offset_ns(int64_t defaultValue)35 int64_t vsync_event_phase_offset_ns(int64_t defaultValue) {
36     auto temp = SurfaceFlingerProperties::vsync_event_phase_offset_ns();
37     if (temp.has_value()) {
38         return *temp;
39     }
40     return getInt64<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::vsyncEventPhaseOffsetNs>(
41             defaultValue);
42 }
43 
vsync_sf_event_phase_offset_ns(int64_t defaultValue)44 int64_t vsync_sf_event_phase_offset_ns(int64_t defaultValue) {
45     auto temp = SurfaceFlingerProperties::vsync_sf_event_phase_offset_ns();
46     if (temp.has_value()) {
47         return *temp;
48     }
49     return getInt64<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::vsyncSfEventPhaseOffsetNs>(
50             defaultValue);
51 }
52 
use_context_priority(bool defaultValue)53 bool use_context_priority(bool defaultValue) {
54     auto temp = SurfaceFlingerProperties::use_context_priority();
55     if (temp.has_value()) {
56         return *temp;
57     }
58     return getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::useContextPriority>(
59             defaultValue);
60 }
61 
max_frame_buffer_acquired_buffers(int64_t defaultValue)62 int64_t max_frame_buffer_acquired_buffers(int64_t defaultValue) {
63     auto temp = SurfaceFlingerProperties::max_frame_buffer_acquired_buffers();
64     if (temp.has_value()) {
65         return *temp;
66     }
67     return getInt64<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::maxFrameBufferAcquiredBuffers>(
68             defaultValue);
69 }
70 
has_wide_color_display(bool defaultValue)71 bool has_wide_color_display(bool defaultValue) {
72     auto temp = SurfaceFlingerProperties::has_wide_color_display();
73     if (temp.has_value()) {
74         return *temp;
75     }
76     return getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
77             defaultValue);
78 }
79 
running_without_sync_framework(bool defaultValue)80 bool running_without_sync_framework(bool defaultValue) {
81     auto temp = SurfaceFlingerProperties::running_without_sync_framework();
82     if (temp.has_value()) {
83         return !(*temp);
84     }
85     return getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasSyncFramework>(defaultValue);
86 }
87 
has_HDR_display(bool defaultValue)88 bool has_HDR_display(bool defaultValue) {
89     auto temp = SurfaceFlingerProperties::has_HDR_display();
90     if (temp.has_value()) {
91         return *temp;
92     }
93     return getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(defaultValue);
94 }
95 
present_time_offset_from_vsync_ns(int64_t defaultValue)96 int64_t present_time_offset_from_vsync_ns(int64_t defaultValue) {
97     auto temp = SurfaceFlingerProperties::present_time_offset_from_vsync_ns();
98     if (temp.has_value()) {
99         return *temp;
100     }
101     return getInt64<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::presentTimeOffsetFromVSyncNs>(
102             defaultValue);
103 }
104 
force_hwc_copy_for_virtual_displays(bool defaultValue)105 bool force_hwc_copy_for_virtual_displays(bool defaultValue) {
106     auto temp = SurfaceFlingerProperties::force_hwc_copy_for_virtual_displays();
107     if (temp.has_value()) {
108         return *temp;
109     }
110     return getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::useHwcForRGBtoYUV>(
111             defaultValue);
112 }
113 
max_virtual_display_dimension(int64_t defaultValue)114 int64_t max_virtual_display_dimension(int64_t defaultValue) {
115     auto temp = SurfaceFlingerProperties::max_virtual_display_dimension();
116     if (temp.has_value()) {
117         return *temp;
118     }
119     return getUInt64<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::maxVirtualDisplaySize>(
120             defaultValue);
121 }
122 
use_vr_flinger(bool defaultValue)123 bool use_vr_flinger(bool defaultValue) {
124     auto temp = SurfaceFlingerProperties::use_vr_flinger();
125     if (temp.has_value()) {
126         return *temp;
127     }
128     return getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::useVrFlinger>(defaultValue);
129 }
130 
start_graphics_allocator_service(bool defaultValue)131 bool start_graphics_allocator_service(bool defaultValue) {
132     auto temp = SurfaceFlingerProperties::start_graphics_allocator_service();
133     if (temp.has_value()) {
134         return *temp;
135     }
136     return getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::startGraphicsAllocatorService>(
137             defaultValue);
138 }
139 
primary_display_orientation(SurfaceFlingerProperties::primary_display_orientation_values defaultValue)140 SurfaceFlingerProperties::primary_display_orientation_values primary_display_orientation(
141         SurfaceFlingerProperties::primary_display_orientation_values defaultValue) {
142     auto temp = SurfaceFlingerProperties::primary_display_orientation();
143     if (temp.has_value()) {
144         return *temp;
145     }
146     auto configDefault = DisplayOrientation::ORIENTATION_0;
147     switch (defaultValue) {
148         case SurfaceFlingerProperties::primary_display_orientation_values::ORIENTATION_90:
149             configDefault = DisplayOrientation::ORIENTATION_90;
150             break;
151         case SurfaceFlingerProperties::primary_display_orientation_values::ORIENTATION_180:
152             configDefault = DisplayOrientation::ORIENTATION_180;
153             break;
154         case SurfaceFlingerProperties::primary_display_orientation_values::ORIENTATION_270:
155             configDefault = DisplayOrientation::ORIENTATION_270;
156             break;
157         default:
158             configDefault = DisplayOrientation::ORIENTATION_0;
159             break;
160     }
161     DisplayOrientation result =
162             getDisplayOrientation<V1_1::ISurfaceFlingerConfigs,
163                                   &V1_1::ISurfaceFlingerConfigs::primaryDisplayOrientation>(
164                     configDefault);
165     switch (result) {
166         case DisplayOrientation::ORIENTATION_90:
167             return SurfaceFlingerProperties::primary_display_orientation_values::ORIENTATION_90;
168         case DisplayOrientation::ORIENTATION_180:
169             return SurfaceFlingerProperties::primary_display_orientation_values::ORIENTATION_180;
170         case DisplayOrientation::ORIENTATION_270:
171             return SurfaceFlingerProperties::primary_display_orientation_values::ORIENTATION_270;
172         default:
173             break;
174     }
175     return SurfaceFlingerProperties::primary_display_orientation_values::ORIENTATION_0;
176 }
177 
use_color_management(bool defaultValue)178 bool use_color_management(bool defaultValue) {
179     auto tmpuseColorManagement = SurfaceFlingerProperties::use_color_management();
180     auto tmpHasHDRDisplayVal = has_HDR_display(defaultValue);
181     auto tmpHasWideColorDisplayVal = has_wide_color_display(defaultValue);
182 
183     auto tmpuseColorManagementVal = tmpuseColorManagement.has_value() ? *tmpuseColorManagement :
184         defaultValue;
185 
186     return tmpuseColorManagementVal || tmpHasHDRDisplayVal || tmpHasWideColorDisplayVal;
187 }
188 
default_composition_dataspace(Dataspace defaultValue)189 int64_t default_composition_dataspace(Dataspace defaultValue) {
190     auto temp = SurfaceFlingerProperties::default_composition_dataspace();
191     if (temp.has_value()) {
192         return *temp;
193     }
194     return static_cast<int64_t>(defaultValue);
195 }
196 
default_composition_pixel_format(PixelFormat defaultValue)197 int32_t default_composition_pixel_format(PixelFormat defaultValue) {
198     auto temp = SurfaceFlingerProperties::default_composition_pixel_format();
199     if (temp.has_value()) {
200         return *temp;
201     }
202     return static_cast<int32_t>(defaultValue);
203 }
204 
wcg_composition_dataspace(Dataspace defaultValue)205 int64_t wcg_composition_dataspace(Dataspace defaultValue) {
206     auto temp = SurfaceFlingerProperties::wcg_composition_dataspace();
207     if (temp.has_value()) {
208         return *temp;
209     }
210     return static_cast<int64_t>(defaultValue);
211 }
212 
wcg_composition_pixel_format(PixelFormat defaultValue)213 int32_t wcg_composition_pixel_format(PixelFormat defaultValue) {
214     auto temp = SurfaceFlingerProperties::wcg_composition_pixel_format();
215     if (temp.has_value()) {
216         return *temp;
217     }
218     return static_cast<int32_t>(defaultValue);
219 }
220 
color_space_agnostic_dataspace(Dataspace defaultValue)221 int64_t color_space_agnostic_dataspace(Dataspace defaultValue) {
222     auto temp = SurfaceFlingerProperties::color_space_agnostic_dataspace();
223     if (temp.has_value()) {
224         return *temp;
225     }
226     return static_cast<int64_t>(defaultValue);
227 }
228 
refresh_rate_switching(bool defaultValue)229 bool refresh_rate_switching(bool defaultValue) {
230     auto temp = SurfaceFlingerProperties::refresh_rate_switching();
231     if (temp.has_value()) {
232         return *temp;
233     }
234     return defaultValue;
235 }
236 
set_idle_timer_ms(int32_t defaultValue)237 int32_t set_idle_timer_ms(int32_t defaultValue) {
238     auto temp = SurfaceFlingerProperties::set_idle_timer_ms();
239     if (temp.has_value()) {
240         return *temp;
241     }
242     return defaultValue;
243 }
244 
set_touch_timer_ms(int32_t defaultValue)245 int32_t set_touch_timer_ms(int32_t defaultValue) {
246     auto temp = SurfaceFlingerProperties::set_touch_timer_ms();
247     if (temp.has_value()) {
248         return *temp;
249     }
250     return defaultValue;
251 }
252 
set_display_power_timer_ms(int32_t defaultValue)253 int32_t set_display_power_timer_ms(int32_t defaultValue) {
254     auto temp = SurfaceFlingerProperties::set_display_power_timer_ms();
255     if (temp.has_value()) {
256         return *temp;
257     }
258     return defaultValue;
259 }
260 
use_smart_90_for_video(bool defaultValue)261 bool use_smart_90_for_video(bool defaultValue) {
262     auto temp = SurfaceFlingerProperties::use_smart_90_for_video();
263     if (temp.has_value()) {
264         return *temp;
265     }
266     return defaultValue;
267 }
268 
enable_protected_contents(bool defaultValue)269 bool enable_protected_contents(bool defaultValue) {
270     auto temp = SurfaceFlingerProperties::enable_protected_contents();
271     if (temp.has_value()) {
272         return *temp;
273     }
274     return defaultValue;
275 }
276 
support_kernel_idle_timer(bool defaultValue)277 bool support_kernel_idle_timer(bool defaultValue) {
278     auto temp = SurfaceFlingerProperties::support_kernel_idle_timer();
279     if (temp.has_value()) {
280         return *temp;
281     }
282     return defaultValue;
283 }
284 
285 #define DISPLAY_PRIMARY_SIZE 3
286 
287 constexpr float kSrgbRedX = 0.4123f;
288 constexpr float kSrgbRedY = 0.2126f;
289 constexpr float kSrgbRedZ = 0.0193f;
290 constexpr float kSrgbGreenX = 0.3576f;
291 constexpr float kSrgbGreenY = 0.7152f;
292 constexpr float kSrgbGreenZ = 0.1192f;
293 constexpr float kSrgbBlueX = 0.1805f;
294 constexpr float kSrgbBlueY = 0.0722f;
295 constexpr float kSrgbBlueZ = 0.9506f;
296 constexpr float kSrgbWhiteX = 0.9505f;
297 constexpr float kSrgbWhiteY = 1.0000f;
298 constexpr float kSrgbWhiteZ = 1.0891f;
299 
getDisplayNativePrimaries()300 DisplayPrimaries getDisplayNativePrimaries() {
301     auto mDisplay_primary_red = SurfaceFlingerProperties::display_primary_red();
302     auto mDisplay_primary_green = SurfaceFlingerProperties::display_primary_green();
303     auto mDisplay_primary_blue = SurfaceFlingerProperties::display_primary_blue();
304     auto mDisplay_primary_white = SurfaceFlingerProperties::display_primary_white();
305     // To avoid null point exception.
306     mDisplay_primary_red.resize(DISPLAY_PRIMARY_SIZE);
307     mDisplay_primary_green.resize(DISPLAY_PRIMARY_SIZE);
308     mDisplay_primary_blue.resize(DISPLAY_PRIMARY_SIZE);
309     mDisplay_primary_white.resize(DISPLAY_PRIMARY_SIZE);
310     DisplayPrimaries primaries =
311             {{static_cast<float>(mDisplay_primary_red[0].value_or(kSrgbRedX)),
312               static_cast<float>(mDisplay_primary_red[1].value_or(kSrgbRedY)),
313               static_cast<float>(mDisplay_primary_red[2].value_or(kSrgbRedZ))},
314              {static_cast<float>(mDisplay_primary_green[0].value_or(kSrgbGreenX)),
315               static_cast<float>(mDisplay_primary_green[1].value_or(kSrgbGreenY)),
316               static_cast<float>(mDisplay_primary_green[2].value_or(kSrgbGreenZ))},
317              {static_cast<float>(mDisplay_primary_blue[0].value_or(kSrgbBlueX)),
318               static_cast<float>(mDisplay_primary_blue[1].value_or(kSrgbBlueY)),
319               static_cast<float>(mDisplay_primary_blue[2].value_or(kSrgbBlueZ))},
320              {static_cast<float>(mDisplay_primary_white[0].value_or(kSrgbWhiteX)),
321               static_cast<float>(mDisplay_primary_white[1].value_or(kSrgbWhiteY)),
322               static_cast<float>(mDisplay_primary_white[2].value_or(kSrgbWhiteZ))}};
323 
324     return primaries;
325 }
326 
327 } // namespace sysprop
328 } // namespace android
329