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