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 #include "include/gpu/GrDirectContext.h"
24 #ifdef RS_ENABLE_VK
25 #include "include/gpu/vk/GrVkBackendContext.h"
26 #endif
27 #include "pipeline/parallel_render/rs_render_task.h"
28 #include "render_context/render_context.h"
29 #include "event_handler.h"
30 
31 namespace OHOS::Rosen {
32 class RSSubThread {
33 public:
RSSubThread(RenderContext * context,uint32_t threadIndex)34     RSSubThread(RenderContext* context, uint32_t threadIndex) : threadIndex_(threadIndex), renderContext_(context) {}
35     ~RSSubThread();
36 
37     pid_t Start();
38     void PostTask(const std::function<void()>& task, const std::string& name = std::string());
39     void PostSyncTask(const std::function<void()>& task);
40     void RemoveTask(const std::string& name);
41     void RenderCache(const std::shared_ptr<RSSuperRenderTask>& threadTask);
42     void DrawableCache(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable);
43     void ReleaseSurface();
44     void ReleaseCacheSurfaceOnly(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable);
45     void AddToReleaseQueue(std::shared_ptr<Drawing::Surface>&& surface);
46     void ResetGrContext();
47     void ThreadSafetyReleaseTexture();
48     void DumpMem(DfxString& log);
49     float GetAppGpuMemoryInMB();
GetDoingCacheProcessNum()50     unsigned int GetDoingCacheProcessNum()
51     {
52         return doingCacheProcessNum_.load();
53     }
DoingCacheProcessNumInc()54     inline void DoingCacheProcessNumInc()
55     {
56         doingCacheProcessNum_++;
57     }
58     void DrawableCacheWithSkImage(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable);
59     void DrawableCacheWithDma(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> nodeDrawable);
GetGrContext()60     std::shared_ptr<Drawing::GPUContext> GetGrContext() const
61     {
62         return grContext_;
63     }
64 
65 private:
66     void CreateShareEglContext();
67     void DestroyShareEglContext();
68     std::shared_ptr<Drawing::GPUContext> CreateShareGrContext();
69     void SetHighContrastIfEnabled(RSPaintFilterCanvas& canvas);
70 
71     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
72     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
73     uint32_t threadIndex_ = UNI_RENDER_THREAD_INDEX;
74     RenderContext *renderContext_ = nullptr;
75 #ifdef RS_ENABLE_GL
76     EGLContext eglShareContext_ = EGL_NO_CONTEXT;
77 #endif
78     std::shared_ptr<Drawing::GPUContext> grContext_ = nullptr;
79     std::mutex mutex_;
80     std::queue<std::shared_ptr<Drawing::Surface>> tmpSurfaces_;
81     std::atomic<unsigned int> doingCacheProcessNum_ = 0;
82 };
83 }
84 #endif // RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_SUB_THREAD_H