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         .minimumResourceRetention = 1_s,
32         .maximumResourceRetention = 10_s,
33         .useAlternativeUiHidden = true,
34         .purgeScratchOnly = false,
35 };
36 constexpr static MemoryPolicy sLowRamPolicy{
37         .useAlternativeUiHidden = true,
38         .purgeScratchOnly = false,
39 };
40 constexpr static MemoryPolicy sExtremeLowRam{
41         .initialMaxSurfaceAreaScale = 0.2f,
42         .surfaceSizeMultiplier = 5 * 4.0f,
43         .backgroundRetentionPercent = 0.2f,
44         .contextTimeout = 5_s,
45         .minimumResourceRetention = 1_s,
46         .useAlternativeUiHidden = true,
47         .purgeScratchOnly = false,
48         .releaseContextOnStoppedOnly = true,
49 };
50 
loadMemoryPolicy()51 const MemoryPolicy& loadMemoryPolicy() {
52     if (Properties::isSystemOrPersistent) {
53         return sPersistentOrSystemPolicy;
54     }
55     std::string memoryPolicy = base::GetProperty(PROPERTY_MEMORY_POLICY, "");
56     if (memoryPolicy == "default") {
57         return sDefaultMemoryPolicy;
58     }
59     if (memoryPolicy == "lowram") {
60         return sLowRamPolicy;
61     }
62     if (memoryPolicy == "extremelowram") {
63         return sExtremeLowRam;
64     }
65 
66     if (Properties::isLowRam) {
67         return sLowRamPolicy;
68     }
69     return sDefaultMemoryPolicy;
70 }
71 
72 }  // namespace android::uirenderer