• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_PARALLEL_SUB_THREAD_H
17 #define RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_PARALLEL_SUB_THREAD_H
18 
19 #include <condition_variable>
20 #include <cstdint>
21 #include <memory>
22 #include <thread>
23 #include "EGL/egl.h"
24 #include "EGL/eglext.h"
25 #include "include/core/SkImage.h"
26 #include "include/core/SkSurface.h"
27 #include "include/gpu/GrDirectContext.h"
28 #include "pipeline/parallel_render/rs_render_task.h"
29 #include "pipeline/rs_base_render_engine.h"
30 #include "pipeline/rs_display_render_node.h"
31 #include "pipeline/rs_paint_filter_canvas.h"
32 #ifdef NEW_RENDER_CONTEXT
33 #include "render_context_base.h"
34 #include "include/gpu/gl/GrGLInterface.h"
35 #else
36 #include "render_context/render_context.h"
37 #endif
38 #include "pipeline/rs_base_render_engine.h"
39 
40 namespace OHOS {
41 namespace Rosen {
42 class RSUniRenderVisitor;
43 class RSDisplayRenderNode;
44 enum class ParallelRenderType;
45 
46 class RSParallelSubThread {
47 public:
48     explicit RSParallelSubThread(int threadIndex);
49 #ifdef NEW_RENDER_CONTEXT
50     RSParallelSubThread(std::shared_ptr<RenderContextBase> context, ParallelRenderType renderType,
51         int threadIndex);
52 #else
53     RSParallelSubThread(RenderContext *context, ParallelRenderType renderType, int threadIndex);
54 #endif
55     ~RSParallelSubThread();
56 
57     void StartSubThread();
58     void WaitTaskSync();
59     void SetMainVisitor(RSUniRenderVisitor *mainVisitor);
60     bool GetRenderFinish();
61     void SetSuperTask(std::unique_ptr<RSSuperRenderTask> superRenderTask);
62     void SetCompositionTask(std::unique_ptr<RSCompositionTask> compositionTask);
63     EGLContext GetSharedContext() const;
64 #ifndef USE_ROSEN_DRAWING
65     sk_sp<SkSurface> GetSkSurface() const;
66     sk_sp<SkImage> GetTexture() const;
67 #else
68     std::shared_ptr<Drawing::Surface> GetDrawingSurface() const;
69     std::shared_ptr<Drawing::Image> GetTexture() const;
70 #endif
71     bool WaitReleaseFence();
GetUniVisitor()72     std::shared_ptr<RSUniRenderVisitor> GetUniVisitor() const
73     {
74         return visitor_;
75     }
WaitSubMainThreadEnd()76     void WaitSubMainThreadEnd()
77     {
78         subThread_->join();
79     }
80 
81 private:
82     void MainLoop();
83     void MainLoopHandlePrepareTask();
84     void StartRender();
85     void InitSubThread();
86     void Render();
87     void InitUniVisitor();
88     void Flush();
89     void CreateResource();
90     void CreatePbufferSurface();
91     void CreateShareEglContext();
92     void StartPrepare();
93     void Prepare();
94     void CalcCost();
95     void StartComposition();
96     void Composition();
97 #ifndef USE_ROSEN_DRAWING
98     sk_sp<GrDirectContext> CreateShareGrContext();
99     void AcquireSubSkSurface(int width, int height);
100 #else
101     std::shared_ptr<Drawing::GPUContext> CreateShareGPUContext();
102     void AcquireSubDrawingSurface(int width, int height);
103     bool FlushForRosenDrawing();
104 #endif
105 
106     uint32_t threadIndex_;
107     int surfaceWidth_ = 0;
108     int surfaceHeight_ = 0;
109 #ifndef USE_ROSEN_DRAWING
110     sk_sp<GrDirectContext> grContext_ = nullptr;
111     sk_sp<SkSurface> skSurface_ = nullptr;
112     SkCanvas *skCanvas_ = nullptr;
113 #else
114     std::shared_ptr<Drawing::GPUContext> drContext_ = nullptr;
115     std::shared_ptr<Drawing::Surface> surface_ = nullptr;
116     Drawing::Canvas *drCanvas_ = nullptr;
117 #endif
118     std::shared_ptr<RSPaintFilterCanvas> canvas_ = nullptr;
119     std::shared_ptr<RSUniRenderVisitor> visitor_;
120     std::shared_ptr<RSUniRenderVisitor> compositionVisitor_ = nullptr;
121     std::shared_ptr<RSBaseRenderEngine> processorRenderEngine_ = nullptr;
122     EGLContext eglShareContext_ = EGL_NO_CONTEXT;
123     EGLSurface eglPSurface_ = EGL_NO_SURFACE;
124     std::function<void()> mainLoop_;
125     std::thread *subThread_;
126     std::condition_variable cvFlush_;
127     std::mutex flushMutex_;
128 #ifdef NEW_RENDER_CONTEXT
129     std::shared_ptr<RenderContextBase> renderContext_ = nullptr;
130 #else
131     RenderContext *renderContext_ = nullptr;
132 #endif
133     std::unique_ptr<RSSuperRenderTask> threadTask_;
134     std::unique_ptr<RSSuperRenderTask> cacheThreadTask_;
135     std::unique_ptr<RSCompositionTask> compositionTask_ = nullptr;
136 
137     RSUniRenderVisitor *mainVisitor_ = nullptr;
138     ParallelRenderType renderType_;
139 #ifndef USE_ROSEN_DRAWING
140     sk_sp<SkImage> texture_;
141 #else
142     std::shared_ptr<Drawing::Image> texture_;
143 #endif
144     EGLSyncKHR eglSync_ = EGL_NO_SYNC_KHR;
145 
146     // Use for Vulkan
147     std::shared_ptr<RSDisplayRenderNode> displayNode_ = nullptr;
148     std::unique_ptr<RSRenderFrame> renderFrame_;
149 };
150 } // namespace Rosen
151 } // namespace OHOS
152 #endif // RENDER_SERVICE_CORE_PIPELINE_PARALLEL_RENDER_RS_PARALLEL_SUB_THREAD_H