1 /*
2  * Copyright (C) 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 #include "MemoryPolicy.h"
18 
19 #include <android-base/properties.h>
20 
21 #include <optional>
22 #include <string_view>
23 
24 #include "Properties.h"
25 
26 namespace android::uirenderer {
27 
28 constexpr static MemoryPolicy sDefaultMemoryPolicy;
29 constexpr static MemoryPolicy sPersistentOrSystemPolicy{
30         .contextTimeout = 10_s,
31         .useAlternativeUiHidden = true,
32 };
33 constexpr static MemoryPolicy sLowRamPolicy{
34         .useAlternativeUiHidden = true,
35         .purgeScratchOnly = false,
36 };
37 constexpr static MemoryPolicy sExtremeLowRam{
38         .initialMaxSurfaceAreaScale = 0.2f,
39         .surfaceSizeMultiplier = 5 * 4.0f,
40         .backgroundRetentionPercent = 0.2f,
41         .contextTimeout = 5_s,
42         .minimumResourceRetention = 1_s,
43         .useAlternativeUiHidden = true,
44         .purgeScratchOnly = false,
45         .releaseContextOnStoppedOnly = true,
46 };
47 
loadMemoryPolicy()48 const MemoryPolicy& loadMemoryPolicy() {
49     if (Properties::isSystemOrPersistent) {
50         return sPersistentOrSystemPolicy;
51     }
52     std::string memoryPolicy = base::GetProperty(PROPERTY_MEMORY_POLICY, "");
53     if (memoryPolicy == "default") {
54         return sDefaultMemoryPolicy;
55     }
56     if (memoryPolicy == "lowram") {
57         return sLowRamPolicy;
58     }
59     if (memoryPolicy == "extremelowram") {
60         return sExtremeLowRam;
61     }
62 
63     if (Properties::isLowRam) {
64         return sLowRamPolicy;
65     }
66     return sDefaultMemoryPolicy;
67 }
68 
69 }  // namespace android::uirenderer