• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <vector>
17 
18 #ifndef USE_ROSEN_DRAWING
19 #include "include/gpu/GrDirectContext.h"
20 #else
21 #include "image/gpu_context.h"
22 #endif
23 
24 #include "memory/rs_dfx_string.h"
25 #include "memory/rs_memory_graphic.h"
26 #include "memory/rs_memory_track.h"
27 #include "pipeline/rs_surface_render_node.h"
28 
29 namespace OHOS::Rosen {
30 
31 class MemoryManager {
32 public:
33 #ifndef USE_ROSEN_DRAWING
34     static void DumpMemoryUsage(DfxString& log, const GrDirectContext* grContext, std::string& type);
35     static void DumpPidMemory(DfxString& log, int pid, const GrDirectContext* grContext);
36     static void DumpDrawingGpuMemory(DfxString& log, const GrDirectContext* grContext);
37     // Count memory for hidumper
38     static MemoryGraphic CountPidMemory(int pid, const GrDirectContext* grContext);
39     static MemoryGraphic CountSubMemory(int pid, const GrDirectContext* grContext);
40     static void CountMemory(std::vector<pid_t> pids, const GrDirectContext* grContext,
41         std::vector<MemoryGraphic>& mems);
42     static void ReleaseUnlockGpuResource(GrDirectContext* grContext, NodeId surfaceNodeId);
43     static void ReleaseUnlockGpuResource(GrDirectContext* grContext, pid_t pid);
44     static void ReleaseUnlockGpuResource(GrDirectContext* grContext, GrGpuResourceTag& tag);
45     static void ReleaseAllGpuResource(GrDirectContext* grContext, pid_t pid);
46     static void ReleaseAllGpuResource(GrDirectContext* grContext, GrGpuResourceTag& tag);
47     static void ReleaseUnlockGpuResource(GrDirectContext* grContext, bool scratchResourcesOnly = true);
48     static void ReleaseUnlockAndSafeCacheGpuResource(GrDirectContext* grContext);
49     static float GetAppGpuMemoryInMB(GrDirectContext* grContext);
50 #else
51     static void DumpMemoryUsage(DfxString& log, const Drawing::GPUContext* gpuContext, std::string& type);
52     static void DumpPidMemory(DfxString& log, int pid, const Drawing::GPUContext* gpuContext);
53     static MemoryGraphic CountSubMemory(int pid, const Drawing::GPUContext* gpuContext);
54     static void DumpDrawingGpuMemory(DfxString& log, const Drawing::GPUContext* grContext);
55     // Count memory for hidumper
56     static MemoryGraphic CountPidMemory(int pid, const Drawing::GPUContext* gpuContext);
57     static void CountMemory(std::vector<pid_t> pids,
58         const Drawing::GPUContext* gpuContext, std::vector<MemoryGraphic>& mems);
59     static void ReleaseUnlockGpuResource(Drawing::GPUContext* gpuContext, NodeId surfaceNodeId);
60     static void ReleaseUnlockGpuResource(Drawing::GPUContext* gpuContext, pid_t pid);
61     static void ReleaseUnlockGpuResource(Drawing::GPUContext* gpuContext, Drawing::GPUResourceTag& tag);
62     static void ReleaseAllGpuResource(Drawing::GPUContext* gpuContext, pid_t pid);
63     static void ReleaseAllGpuResource(Drawing::GPUContext* gpuContext, Drawing::GPUResourceTag& tag);
64     static void ReleaseUnlockGpuResource(Drawing::GPUContext* grContext, bool scratchResourcesOnly = true);
65     static void ReleaseUnlockAndSafeCacheGpuResource(Drawing::GPUContext* grContext);
66     static float GetAppGpuMemoryInMB(Drawing::GPUContext* gpuContext);
67 #endif
68 
69 private:
70     // rs memory = rs + skia cpu + skia gpu
71     static void DumpRenderServiceMemory(DfxString& log);
72     static void DumpDrawingCpuMemory(DfxString& log);
73 #ifndef USE_ROSEN_DRAWING
74     static void DumpAllGpuInfo(DfxString& log, const GrDirectContext* grContext);
75     static void DumpGpuCache(
76         DfxString& log, const GrDirectContext* grContext, GrGpuResourceTag* tag, std::string& name);
77 #else
78     static void DumpGpuCache(DfxString& log, const Drawing::GPUContext* gpuContext,
79         Drawing::GPUResourceTag* tag, std::string& name);
80     static void DumpAllGpuInfo(DfxString& log, const Drawing::GPUContext* grContext);
81 #endif
82     //jemalloc info
83     static void DumpMallocStat(std::string& log);
84 };
85 } // namespace OHOS::Rosen