1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "memory_handler.h"
17 #include "render_context_log.h"
18 #include <set>
19 #include <cmath>
20
21 namespace OHOS {
22 namespace Rosen {
ConfigureContext(Drawing::GPUContextOptions * context,const char * identity,const size_t size,const std::string & cacheFilePath,bool isUni)23 void MemoryHandler::ConfigureContext(Drawing::GPUContextOptions* context, const char* identity,
24 const size_t size, const std::string& cacheFilePath, bool isUni)
25 {
26 context->SetAllowPathMaskCaching(true);
27 auto& cache = ShaderCache::Instance();
28 cache.SetFilePath(cacheFilePath);
29 cache.InitShaderCache(identity, size, isUni);
30 context->SetPersistentCache(&cache);
31 }
32
ClearRedundantResources(Drawing::GPUContext * gpuContext)33 void MemoryHandler::ClearRedundantResources(Drawing::GPUContext* gpuContext)
34 {
35 if (gpuContext != nullptr) {
36 LOGD("gpuContext clear redundant resources");
37 gpuContext->Flush();
38 // GPU resources that haven't been used in the past 10 seconds
39 gpuContext->PerformDeferredCleanup(std::chrono::seconds(10));
40 }
41 }
42
QuerryShader()43 std::string MemoryHandler::QuerryShader()
44 {
45 const auto& cache = ShaderCache::Instance();
46 if (!cache.IfInitialized()) {
47 LOGD("ShaderCache is not intialized.");
48 }
49 size_t shaderRam = cache.QuerryShaderSize();
50 size_t shaderNum = cache.QuerryShaderNum();
51 std::string ramString = "ShaderCache RAM used:" + std::to_string(shaderRam / 1024) + " kB\n";
52 ramString += "ShaderCache num is: " + std::to_string(shaderNum);
53 return ramString;
54 }
55
ClearShader()56 std::string MemoryHandler::ClearShader()
57 {
58 const auto& cache = ShaderCache::Instance();
59 LOGD("All shaders are cleaned");
60 cache.CleanAllShaders();
61 return "All shaders are cleaned, RAM freed: 0";
62 }
63 } // namespace Rosen
64 } // namespace OHOS