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 "offscreen_render/rs_offscreen_render_thread.h" 17 #include "platform/common/rs_log.h" 18 #include "platform/common/rs_system_properties.h" 19 #ifdef ROSEN_OHOS 20 #include "render_context/render_context.h" 21 #endif 22 23 #ifdef RES_BASE_SCHED_ENABLE 24 #include "qos.h" 25 #endif 26 27 namespace OHOS::Rosen { 28 Instance()29RSOffscreenRenderThread& RSOffscreenRenderThread::Instance() 30 { 31 static RSOffscreenRenderThread instance; 32 return instance; 33 } 34 RSOffscreenRenderThread()35RSOffscreenRenderThread::RSOffscreenRenderThread() 36 { 37 runner_ = AppExecFwk::EventRunner::Create("RSOffscreenRender"); 38 handler_ = std::make_shared<AppExecFwk::EventHandler>(runner_); 39 if (!RSSystemProperties::GetUniRenderEnabled()) { 40 return; 41 } 42 #ifdef ROSEN_OHOS 43 PostTask([this]() { 44 renderContext_ = std::make_shared<RenderContext>(); 45 #if defined(RS_ENABLE_GL) 46 if (RSSystemProperties::GetGpuApiType() == GpuApiType::OPENGL) { 47 renderContext_->InitializeEglContext(); 48 renderContext_->SetUpGpuContext(nullptr); 49 } 50 #endif // RS_ENABLE_GL 51 #if defined(RS_ENABLE_VK) 52 if (RSSystemProperties::GetGpuApiType() == GpuApiType::VULKAN || 53 RSSystemProperties::GetGpuApiType() == GpuApiType::DDGR) { 54 renderContext_->SetUpGpuContext(nullptr); 55 } 56 #endif // RS_ENABLE_VK 57 }); 58 #endif 59 60 #ifdef RES_BASE_SCHED_ENABLE 61 PostTask([this]() { 62 auto ret = OHOS::QOS::SetThreadQos(OHOS::QOS::QosLevel::QOS_USER_INTERACTIVE); 63 RS_LOGI("RSOffscreenRenderThread: SetThreadQos retcode = %{public}d", ret); 64 }); 65 #endif 66 } 67 PostTask(const std::function<void ()> & task)68void RSOffscreenRenderThread::PostTask(const std::function<void()>& task) 69 { 70 if (handler_) { 71 handler_->PostTask(task, AppExecFwk::EventQueue::Priority::IMMEDIATE); 72 } 73 } 74 InSertCaptureTask(NodeId nodeId,std::function<void ()> & task)75void RSOffscreenRenderThread::InSertCaptureTask(NodeId nodeId, std::function<void()>& task) 76 { 77 std::lock_guard<std::mutex> lockGuard(mutex_); 78 taskMap_[nodeId] = task; 79 } 80 GetCaptureTask(NodeId nodeId)81const std::function<void()> RSOffscreenRenderThread::GetCaptureTask(NodeId nodeId) 82 { 83 std::lock_guard<std::mutex> lockGuard(mutex_); 84 if (!taskMap_.empty()) { 85 auto iter = taskMap_.find(nodeId); 86 if (iter != taskMap_.end()) { 87 auto task = taskMap_[nodeId]; 88 taskMap_.erase(nodeId); 89 return task; 90 } 91 } 92 return nullptr; 93 } 94 95 #ifdef ROSEN_OHOS GetRenderContext()96const std::shared_ptr<RenderContext> RSOffscreenRenderThread::GetRenderContext() 97 { 98 if (!renderContext_) { 99 return nullptr; 100 } 101 return renderContext_; 102 } 103 CleanGrResource()104void RSOffscreenRenderThread::CleanGrResource() 105 { 106 PostTask([this]() { 107 if (!renderContext_) { 108 return; 109 } 110 auto grContext = renderContext_->GetDrGPUContext(); 111 if (grContext == nullptr) { 112 RS_LOGE("RSOffscreenRenderThread::grContext is nullptr"); 113 return; 114 } 115 grContext->FreeGpuResources(); 116 RS_LOGD("RSOffscreenRenderThread::CleanGrResource() finished"); 117 }); 118 } 119 #endif 120 } 121