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 #ifndef RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_SUB_THREAD_H 17 #define RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_SUB_THREAD_H 18 19 #include <cstdint> 20 #include "EGL/egl.h" 21 #include "EGL/eglext.h" 22 #include "include/core/SkSurface.h" 23 #ifdef USE_M133_SKIA 24 #include "include/gpu/ganesh/GrDirectContext.h" 25 #else 26 #include "include/gpu/GrDirectContext.h" 27 #endif 28 #ifdef RS_ENABLE_VK 29 #ifdef USE_M133_SKIA 30 #include "include/gpu/vk/VulkanBackendContext.h" 31 #else 32 #include "include/gpu/vk/GrVkBackendContext.h" 33 #endif 34 #endif 35 #include "pipeline/parallel_render/rs_render_task.h" 36 #include "render_context/render_context.h" 37 #include "event_handler.h" 38 39 namespace OHOS::Rosen { 40 class RSSubThread { 41 public: RSSubThread(RenderContext * context,uint32_t threadIndex)42 RSSubThread(RenderContext* context, uint32_t threadIndex) : threadIndex_(threadIndex), renderContext_(context) {} 43 ~RSSubThread(); 44 45 pid_t Start(); 46 void PostTask(const std::function<void()>& task, const std::string& name = std::string()); 47 void PostSyncTask(const std::function<void()>& task); 48 void RemoveTask(const std::string& name); 49 void DrawableCache(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable); 50 void ReleaseSurface(); 51 void ReleaseCacheSurfaceOnly(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable); 52 void AddToReleaseQueue(std::shared_ptr<Drawing::Surface>&& surface); 53 void ResetGrContext(); 54 void ThreadSafetyReleaseTexture(); 55 void DumpMem(DfxString& log); 56 MemoryGraphic CountSubMem(int pid); 57 float GetAppGpuMemoryInMB(); GetDoingCacheProcessNum()58 unsigned int GetDoingCacheProcessNum() 59 { 60 return doingCacheProcessNum_.load(); 61 } DoingCacheProcessNumInc()62 inline void DoingCacheProcessNumInc() 63 { 64 doingCacheProcessNum_++; 65 } 66 void DrawableCacheWithSkImage(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable); GetGrContext()67 std::shared_ptr<Drawing::GPUContext> GetGrContext() const 68 { 69 return grContext_; 70 } 71 void UpdateGpuMemoryStatistics(); GetGpuMemoryOfPid()72 std::unordered_map<pid_t, size_t> GetGpuMemoryOfPid() 73 { 74 std::lock_guard<std::mutex> lock(memMutex_); 75 return gpuMemoryOfPid_; 76 } ErasePidOfGpuMemory(pid_t pid)77 void ErasePidOfGpuMemory(pid_t pid) 78 { 79 std::lock_guard<std::mutex> lock(memMutex_); 80 gpuMemoryOfPid_.erase(pid); 81 } 82 83 private: 84 void CreateShareEglContext(); 85 void DestroyShareEglContext(); 86 std::shared_ptr<Drawing::GPUContext> CreateShareGrContext(); 87 void SetHighContrastIfEnabled(RSPaintFilterCanvas& canvas); 88 NodeId GetSubAppNodeId(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable, 89 RSSurfaceRenderParams* surfaceParams); 90 bool CheckValid(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable); 91 92 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 93 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 94 uint32_t threadIndex_ = UNI_RENDER_THREAD_INDEX; 95 RenderContext *renderContext_ = nullptr; 96 #ifdef RS_ENABLE_GL 97 EGLContext eglShareContext_ = EGL_NO_CONTEXT; 98 #endif 99 std::shared_ptr<Drawing::GPUContext> grContext_ = nullptr; 100 std::mutex mutex_; 101 std::queue<std::shared_ptr<Drawing::Surface>> tmpSurfaces_; 102 std::atomic<unsigned int> doingCacheProcessNum_ = 0; 103 std::unordered_map<pid_t, size_t> gpuMemoryOfPid_; 104 std::mutex memMutex_; 105 }; 106 } 107 #endif // RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_SUB_THREAD_H