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