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