• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "rs_upload_resource_thread.h"
17 
18 #include "rs_trace.h"
19 #include "platform/common/rs_system_properties.h"
20 #include "pipeline/rs_task_dispatcher.h"
21 #include "render/rs_resource_manager.h"
22 #ifdef RS_ENABLE_VK
23 #include "platform/ohos/backend/rs_vulkan_context.h"
24 #endif
25 #include "engine_adapter/skia_adapter/skia_gpu_context.h"
26 
27 namespace OHOS::Rosen {
28 // Task Related
Instance()29 RSUploadResourceThread& RSUploadResourceThread::Instance()
30 {
31     static RSUploadResourceThread sInstance;
32     return sInstance;
33 }
34 
RSUploadResourceThread()35 RSUploadResourceThread::RSUploadResourceThread()
36 {
37     runner_ = AppExecFwk::EventRunner::Create("RSUploadResourceThread");
38     handler_ = std::make_shared<AppExecFwk::EventHandler>(runner_);
39 }
40 
PostTask(const std::function<void ()> & task)41 void RSUploadResourceThread::PostTask(const std::function<void()>& task)
42 {
43     if (handler_) {
44         handler_->PostTask(task, AppExecFwk::EventQueue::Priority::IMMEDIATE);
45     }
46 }
47 
PostSyncTask(const std::function<void ()> & task)48 void RSUploadResourceThread::PostSyncTask(const std::function<void()>& task)
49 {
50     if (handler_) {
51         handler_->PostSyncTask(task, AppExecFwk::EventQueue::Priority::IMMEDIATE);
52     }
53 }
54 
PostTask(const std::function<void ()> & task,const std::string & name)55 void RSUploadResourceThread::PostTask(const std::function<void()>& task, const std::string& name)
56 {
57     if (handler_) {
58         handler_->PostImmediateTask(task, name);
59     }
60 }
61 
RemoveTask(const std::string & name)62 void RSUploadResourceThread::RemoveTask(const std::string& name)
63 {
64     if (handler_) {
65         handler_->RemoveTask(name);
66     }
67 }
68 
OnRenderEnd()69 void RSUploadResourceThread::OnRenderEnd()
70 {
71     if (IsEnable()) {
72         std::unique_lock<std::mutex> lock(uploadTaskMutex_);
73         enableTime_ = true;
74         uploadTaskCond_.notify_all();
75     }
76     ReleaseNotUsedPinnedViews();
77 }
78 
OnProcessBegin()79 void RSUploadResourceThread::OnProcessBegin()
80 {
81     if (IsEnable()) {
82         std::unique_lock<std::mutex> lock(uploadTaskMutex_);
83         enableTime_ = false;
84     }
85     frameCount_.fetch_add(1);
86 }
87 
WaitUntilRenderEnd()88 void RSUploadResourceThread::WaitUntilRenderEnd()
89 {
90     RS_TRACE_NAME("Waitfor render_service finish");
91     std::unique_lock<std::mutex> lock(uploadTaskMutex_);
92     uploadTaskCond_.wait(lock, [this]() { return enableTime_; });
93 }
94 
TaskIsValid(int64_t count)95 bool RSUploadResourceThread::TaskIsValid(int64_t count)
96 {
97     if (count < frameCount_.load()) {
98         return false;
99     }
100     WaitUntilRenderEnd();
101     return true;
102 }
103 
InitRenderContext(RenderContext * context)104 void RSUploadResourceThread::InitRenderContext(RenderContext* context)
105 {
106     renderContext_ = context;
107     isTargetPlatform_ = RSSystemProperties::IsPhoneType();
108     uploadProperity_ = RSSystemProperties::GetParallelUploadTexture();
109     PostTask([this]() {
110         grContext_ = CreateShareGrContext();
111         if (grContext_) {
112             auto skiaGPUContext  = grContext_->GetImpl<Drawing::SkiaGPUContext>();
113             if (!skiaGPUContext) {
114                 return;
115             } else {
116                 skContext_ = skiaGPUContext->GetGrContext();
117             }
118             if (skContext_) {
119                 skContext_->setResourceCollector(&resCollector_);
120             }
121         }
122         RSResourceManager::Instance().SetUploadTextureFunction(UploadTextureWithDrawing);
123     });
124 }
125 
126 #ifdef RS_ENABLE_GL
CreateShareEglContext()127 void RSUploadResourceThread::CreateShareEglContext()
128 {
129     if (Rosen::RSSystemProperties::GetGpuApiType() == Rosen::GpuApiType::OPENGL) {
130         if (renderContext_ == nullptr) {
131             RS_LOGE("renderContext_ is nullptr.");
132             return;
133         }
134         eglShareContext_ = renderContext_->CreateShareContext();
135         if (eglShareContext_ == EGL_NO_CONTEXT) {
136             RS_LOGE("eglShareContext_ is EGL_NO_CONTEXT");
137             return;
138         }
139         if (!eglMakeCurrent(renderContext_->GetEGLDisplay(), EGL_NO_SURFACE, EGL_NO_SURFACE, eglShareContext_)) {
140             RS_LOGE("eglMakeCurrent failed.");
141             return;
142         }
143     }
144 }
145 #endif
146 
GetShareGrContext() const147 std::shared_ptr<Drawing::GPUContext> RSUploadResourceThread::GetShareGrContext() const
148 {
149     return grContext_;
150 }
CreateShareGrContext()151 std::shared_ptr<Drawing::GPUContext> RSUploadResourceThread::CreateShareGrContext()
152 {
153     RS_TRACE_NAME("CreateShareGrContext");
154     std::shared_ptr<Drawing::GPUContext> gpuContext = nullptr;
155 #ifdef RS_ENABLE_GL
156     if (Rosen::RSSystemProperties::GetGpuApiType() == Rosen::GpuApiType::OPENGL) {
157         CreateShareEglContext();
158         gpuContext = std::make_shared<Drawing::GPUContext>();
159         Drawing::GPUContextOptions options;
160         auto handler = std::make_shared<MemoryHandler>();
161         auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
162         auto size = glesVersion ? strlen(glesVersion) : 0;
163         /* /data/service/el0/render_service is shader cache dir */
164         handler->ConfigureContext(&options, glesVersion, size, "/data/service/el0/render_service", true);
165 
166         if (!gpuContext->BuildFromGL(options)) {
167             RS_LOGE("nullptr gpuContext is null");
168             return nullptr;
169         }
170     }
171 #endif
172 
173 #ifdef RS_ENABLE_VK
174     if (OHOS::Rosen::RSSystemProperties::GetGpuApiType() == OHOS::Rosen::GpuApiType::VULKAN ||
175         OHOS::Rosen::RSSystemProperties::GetGpuApiType() == OHOS::Rosen::GpuApiType::DDGR) {
176         gpuContext = std::make_shared<Drawing::GPUContext>();
177         if (!gpuContext->BuildFromVK(RsVulkanContext::GetSingleton().GetGrVkBackendContext())) {
178             RS_LOGE("nullptr gpuContext is null");
179             return nullptr;
180         }
181     }
182 #endif
183     return gpuContext;
184 }
185 
ReleaseNotUsedPinnedViews()186 void RSUploadResourceThread::ReleaseNotUsedPinnedViews()
187 {
188     if (!grContext_ || OHOS::Rosen::RSSystemProperties::GetGpuApiType() == OHOS::Rosen::GpuApiType::DDGR) {
189         return;
190     }
191 
192     sk_sp<GrDirectContext> grContext = skContext_;
193     if (!grContext) {
194         return;
195     }
196     auto arrPtr = std::make_shared<std::vector<sk_sp<GrSurfaceProxy>>>();
197     resCollector_.SwapCollection(*arrPtr, CLEAN_VIEW_COUNT);
198     if (arrPtr->empty()) {
199         return;
200     }
201     PostTask([proxyArr = std::move(arrPtr)]() {
202         RSUploadResourceThread::Instance().WaitUntilRenderEnd();
203         RS_TRACE_NAME("ReleasePinnedViews");
204         proxyArr->clear();
205     });
206 }
207 }
208