1 /*
2  * Copyright (C) 2015 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 "Properties.h"
18 #include "Debug.h"
19 #include "DeviceInfo.h"
20 #include "HWUIProperties.sysprop.h"
21 #include "SkTraceEventCommon.h"
22 
23 #include <algorithm>
24 #include <cstdlib>
25 
26 #include <cutils/compiler.h>
27 #include <cutils/properties.h>
28 #include <log/log.h>
29 
30 namespace android {
31 namespace uirenderer {
32 
33 bool Properties::debugLayersUpdates = false;
34 bool Properties::debugOverdraw = false;
35 bool Properties::showDirtyRegions = false;
36 bool Properties::skipEmptyFrames = true;
37 bool Properties::useBufferAge = true;
38 bool Properties::enablePartialUpdates = true;
39 
40 DebugLevel Properties::debugLevel = kDebugDisabled;
41 OverdrawColorSet Properties::overdrawColorSet = OverdrawColorSet::Default;
42 
43 float Properties::overrideLightRadius = -1.0f;
44 float Properties::overrideLightPosY = -1.0f;
45 float Properties::overrideLightPosZ = -1.0f;
46 float Properties::overrideAmbientRatio = -1.0f;
47 int Properties::overrideAmbientShadowStrength = -1;
48 int Properties::overrideSpotShadowStrength = -1;
49 
50 ProfileType Properties::sProfileType = ProfileType::None;
51 bool Properties::sDisableProfileBars = false;
52 RenderPipelineType Properties::sRenderPipelineType = RenderPipelineType::NotInitialized;
53 bool Properties::enableHighContrastText = false;
54 
55 bool Properties::waitForGpuCompletion = false;
56 bool Properties::forceDrawFrame = false;
57 
58 bool Properties::filterOutTestOverhead = false;
59 bool Properties::disableVsync = false;
60 bool Properties::skpCaptureEnabled = false;
61 bool Properties::enableRTAnimations = true;
62 
63 bool Properties::runningInEmulator = false;
64 bool Properties::debuggingEnabled = false;
65 bool Properties::isolatedProcess = false;
66 
67 int Properties::contextPriority = 0;
68 int Properties::defaultRenderAhead = -1;
69 
property_get_int(const char * key,int defaultValue)70 static int property_get_int(const char* key, int defaultValue) {
71     char buf[PROPERTY_VALUE_MAX] = {
72             '\0',
73     };
74 
75     if (property_get(key, buf, "") > 0) {
76         return atoi(buf);
77     }
78     return defaultValue;
79 }
80 
load()81 bool Properties::load() {
82     char property[PROPERTY_VALUE_MAX];
83     bool prevDebugLayersUpdates = debugLayersUpdates;
84     bool prevDebugOverdraw = debugOverdraw;
85 
86     debugOverdraw = false;
87     if (property_get(PROPERTY_DEBUG_OVERDRAW, property, nullptr) > 0) {
88         INIT_LOGD("  Overdraw debug enabled: %s", property);
89         if (!strcmp(property, "show")) {
90             debugOverdraw = true;
91             overdrawColorSet = OverdrawColorSet::Default;
92         } else if (!strcmp(property, "show_deuteranomaly")) {
93             debugOverdraw = true;
94             overdrawColorSet = OverdrawColorSet::Deuteranomaly;
95         }
96     }
97 
98     sProfileType = ProfileType::None;
99     if (property_get(PROPERTY_PROFILE, property, "") > 0) {
100         if (!strcmp(property, PROPERTY_PROFILE_VISUALIZE_BARS)) {
101             sProfileType = ProfileType::Bars;
102         } else if (!strcmp(property, "true")) {
103             sProfileType = ProfileType::Console;
104         }
105     }
106 
107     debugLayersUpdates = property_get_bool(PROPERTY_DEBUG_LAYERS_UPDATES, false);
108     INIT_LOGD("  Layers updates debug enabled: %d", debugLayersUpdates);
109 
110     showDirtyRegions = property_get_bool(PROPERTY_DEBUG_SHOW_DIRTY_REGIONS, false);
111 
112     debugLevel = (DebugLevel)property_get_int(PROPERTY_DEBUG, kDebugDisabled);
113 
114     skipEmptyFrames = property_get_bool(PROPERTY_SKIP_EMPTY_DAMAGE, true);
115     useBufferAge = property_get_bool(PROPERTY_USE_BUFFER_AGE, true);
116     enablePartialUpdates = property_get_bool(PROPERTY_ENABLE_PARTIAL_UPDATES, true);
117 
118     filterOutTestOverhead = property_get_bool(PROPERTY_FILTER_TEST_OVERHEAD, false);
119 
120     skpCaptureEnabled = debuggingEnabled && property_get_bool(PROPERTY_CAPTURE_SKP_ENABLED, false);
121 
122     SkAndroidFrameworkTraceUtil::setEnableTracing(
123             property_get_bool(PROPERTY_SKIA_ATRACE_ENABLED, false));
124 
125     runningInEmulator = property_get_bool(PROPERTY_QEMU_KERNEL, false);
126 
127     defaultRenderAhead = std::max(-1, std::min(2, property_get_int(PROPERTY_RENDERAHEAD,
128             render_ahead().value_or(0))));
129 
130     return (prevDebugLayersUpdates != debugLayersUpdates) || (prevDebugOverdraw != debugOverdraw);
131 }
132 
overrideProperty(const char * name,const char * value)133 void Properties::overrideProperty(const char* name, const char* value) {
134     if (!strcmp(name, "disableProfileBars")) {
135         sDisableProfileBars = !strcmp(value, "true");
136         ALOGD("profile bars %s", sDisableProfileBars ? "disabled" : "enabled");
137         return;
138     } else if (!strcmp(name, "ambientRatio")) {
139         overrideAmbientRatio = std::min(std::max(atof(value), 0.0), 10.0);
140         ALOGD("ambientRatio = %.2f", overrideAmbientRatio);
141         return;
142     } else if (!strcmp(name, "lightRadius")) {
143         overrideLightRadius = std::min(std::max(atof(value), 0.0), 3000.0);
144         ALOGD("lightRadius = %.2f", overrideLightRadius);
145         return;
146     } else if (!strcmp(name, "lightPosY")) {
147         overrideLightPosY = std::min(std::max(atof(value), 0.0), 3000.0);
148         ALOGD("lightPos Y = %.2f", overrideLightPosY);
149         return;
150     } else if (!strcmp(name, "lightPosZ")) {
151         overrideLightPosZ = std::min(std::max(atof(value), 0.0), 3000.0);
152         ALOGD("lightPos Z = %.2f", overrideLightPosZ);
153         return;
154     } else if (!strcmp(name, "ambientShadowStrength")) {
155         overrideAmbientShadowStrength = atoi(value);
156         ALOGD("ambient shadow strength = 0x%x out of 0xff", overrideAmbientShadowStrength);
157         return;
158     } else if (!strcmp(name, "spotShadowStrength")) {
159         overrideSpotShadowStrength = atoi(value);
160         ALOGD("spot shadow strength = 0x%x out of 0xff", overrideSpotShadowStrength);
161         return;
162     }
163     ALOGD("failed overriding property %s to %s", name, value);
164 }
165 
getProfileType()166 ProfileType Properties::getProfileType() {
167     if (CC_UNLIKELY(sDisableProfileBars && sProfileType == ProfileType::Bars))
168         return ProfileType::None;
169     return sProfileType;
170 }
171 
peekRenderPipelineType()172 RenderPipelineType Properties::peekRenderPipelineType() {
173     // If sRenderPipelineType has been locked, just return the locked type immediately.
174     if (sRenderPipelineType != RenderPipelineType::NotInitialized) {
175         return sRenderPipelineType;
176     }
177     bool useVulkan = use_vulkan().value_or(false);
178     char prop[PROPERTY_VALUE_MAX];
179     property_get(PROPERTY_RENDERER, prop, useVulkan ? "skiavk" : "skiagl");
180     if (!strcmp(prop, "skiavk")) {
181         return RenderPipelineType::SkiaVulkan;
182     }
183     return RenderPipelineType::SkiaGL;
184 }
185 
getRenderPipelineType()186 RenderPipelineType Properties::getRenderPipelineType() {
187     sRenderPipelineType = peekRenderPipelineType();
188     return sRenderPipelineType;
189 }
190 
overrideRenderPipelineType(RenderPipelineType type)191 void Properties::overrideRenderPipelineType(RenderPipelineType type) {
192 #if !defined(HWUI_GLES_WRAP_ENABLED)
193     // If we're doing actual rendering then we can't change the renderer after it's been set.
194     // Unit tests can freely change this as often as it wants, though, as there's no actual
195     // GL rendering happening
196     if (sRenderPipelineType != RenderPipelineType::NotInitialized) {
197         return;
198     }
199 #endif
200     sRenderPipelineType = type;
201 }
202 
203 }  // namespace uirenderer
204 }  // namespace android
205