1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/core/SkUtils.h"
9 #ifdef NOT_BUILD_FOR_OHOS_SDK
10 #include <parameters.h>
11 #endif
12 
13 const char SkHexadecimalDigits::gUpper[16] =
14     { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
15 const char SkHexadecimalDigits::gLower[16] =
16     { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
17 
18 // vma cache
19 static thread_local bool g_vmaCacheFlag = false;
20 
SkGetMemoryOptimizedFlag()21 bool SkGetMemoryOptimizedFlag()
22 {
23     // global flag for vma cache
24 #ifdef NOT_BUILD_FOR_OHOS_SDK
25     static bool g_memoryOptimizeFlag = OHOS::system::GetBoolParameter("sys.graphic.vma.opt", false);
26 #else
27     static bool g_memoryOptimizeFlag = false;
28 #endif
29     return g_memoryOptimizeFlag;
30 }
31 
SkGetVmaCacheFlag()32 bool SkGetVmaCacheFlag()
33 {
34     if (!SkGetMemoryOptimizedFlag()) {
35         return false;
36     }
37     return g_vmaCacheFlag;
38 }
39 
SkSetVmaCacheFlag(bool flag)40 void SkSetVmaCacheFlag(bool flag)
41 {
42     g_vmaCacheFlag = flag;
43 }
44 
45 #ifdef NOT_BUILD_FOR_OHOS_SDK
GetIntParamWithDefault(int paramValue, int maxValue, int defaultValue)46 int GetIntParamWithDefault(int paramValue, int maxValue, int defaultValue)
47 {
48     if (paramValue <= 0 || paramValue > maxValue) {
49         paramValue = defaultValue; // default value
50     }
51     return paramValue;
52 }
53 
GetBoolParamWithFlag(bool paramValue)54 bool GetBoolParamWithFlag(bool paramValue)
55 {
56     if (!SkGetMemoryOptimizedFlag()) {
57         return false;
58     }
59     return paramValue;
60 }
61 #endif
62 
SkGetVmaBlockSizeMB()63 int SkGetVmaBlockSizeMB()
64 {
65 #ifdef NOT_BUILD_FOR_OHOS_SDK
66     constexpr int DEFAULT_VMA_BLOCK_SIZE = 48;
67 #ifdef USE_LARGE_VMA_BLOCK
68     constexpr int MAX_VMA_BLOCK_SIZE = 256;
69     static int g_vmaBlockSize = GetIntParamWithDefault(
70         std::atoi(OHOS::system::GetParameter("sys.graphic.vma.blockSize", "48").c_str()),
71         MAX_VMA_BLOCK_SIZE, DEFAULT_VMA_BLOCK_SIZE);
72 #else
73     static int g_vmaBlockSize = DEFAULT_VMA_BLOCK_SIZE;
74 #endif
75 #else
76     static int g_vmaBlockSize = 4; // default value
77 #endif
78     return g_vmaBlockSize;
79 }
80 
SkGetNeedCachedMemroySize()81 int SkGetNeedCachedMemroySize()
82 {
83 #ifdef NOT_BUILD_FOR_OHOS_SDK
84     constexpr int MAX_VMA_CACHE_MEMORY_SIZE = 512 * 1024 * 1024;
85     constexpr int DEFAULT_VMA_CACHE_MEMORY_SIZE = 9000000;
86     static int g_vmaCacheMemorySize = GetIntParamWithDefault(
87         std::atoi(OHOS::system::GetParameter("sys.graphic.vma.minCachedSize", "9000000").c_str()),
88         MAX_VMA_CACHE_MEMORY_SIZE, DEFAULT_VMA_CACHE_MEMORY_SIZE);
89 #else
90     static int g_vmaCacheMemorySize = 0; // default value
91 #endif
92     return g_vmaCacheMemorySize;
93 }
94 
SkGetVmaDefragmentOn()95 bool SkGetVmaDefragmentOn()
96 {
97 #ifdef NOT_BUILD_FOR_OHOS_SDK
98     static bool g_vmaDefragmentFlag =
99         GetBoolParamWithFlag(OHOS::system::GetBoolParameter("sys.graphic.vma.defragment", true));
100     return g_vmaDefragmentFlag;
101 #else
102     return false;
103 #endif
104 }
105 
SkGetPreAllocFlag()106 bool SkGetPreAllocFlag()
107 {
108 #ifdef NOT_BUILD_FOR_OHOS_SDK
109     static bool g_vmaPreAllocFlag =
110         GetBoolParamWithFlag(OHOS::system::GetBoolParameter("sys.graphic.vma.preAlloc", false));
111     return g_vmaPreAllocFlag;
112 #else
113     return false;
114 #endif
115 }
116 
SkGetPreAllocDelay()117 size_t SkGetPreAllocDelay()
118 {
119 #ifdef NOT_BUILD_FOR_OHOS_SDK
120     constexpr int MAX_VMA_PREALLOC_DELAY = 5000;
121     constexpr int DEFAULT_VMA_PREALLOC_DELAY = 250;
122     static int g_vmaBlockCountMax = GetIntParamWithDefault(
123         std::atoi(OHOS::system::GetParameter("sys.graphic.vma.preAllocDelay", "250").c_str()),
124         MAX_VMA_PREALLOC_DELAY, DEFAULT_VMA_PREALLOC_DELAY);
125     return g_vmaBlockCountMax;
126 #else
127     return SIZE_MAX; // default value
128 #endif
129 }
130 
SkGetVmaBlockCountMax()131 size_t SkGetVmaBlockCountMax()
132 {
133 #ifdef NOT_BUILD_FOR_OHOS_SDK
134     constexpr int MAX_VMA_BLOCK_COUNT_MAX = 4096;
135     constexpr int DEFAULT_VMA_BLOCK_COUNT_MAX = 10;
136     static int g_vmaBlockCountMax = GetIntParamWithDefault(
137         std::atoi(OHOS::system::GetParameter("sys.graphic.vma.maxBlockCount", "10").c_str()),
138         MAX_VMA_BLOCK_COUNT_MAX, DEFAULT_VMA_BLOCK_COUNT_MAX);
139     return g_vmaBlockCountMax;
140 #else
141     return SIZE_MAX; // default value
142 #endif
143 }
144 
SkGetVmaDebugFlag()145 bool SkGetVmaDebugFlag()
146 {
147 #ifdef NOT_BUILD_FOR_OHOS_SDK
148     static bool g_vmaDebugFlag =
149         GetBoolParamWithFlag(std::atoi(OHOS::system::GetParameter("sys.graphic.vma.debug", "0").c_str()) != 0);
150     return g_vmaDebugFlag;
151 #else
152     return false;
153 #endif
154 }
155