• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "effect_vulkan_context.h"
17 
18 namespace OHOS::Rosen {
19 std::map<int, std::shared_ptr<Drawing::GPUContext>> EffectVulkanContext::drawingContextMap_;
20 std::mutex EffectVulkanContext::drawingContextMutex_;
21 
EffectVulkanContext(std::string cacheDir)22 EffectVulkanContext::EffectVulkanContext(std::string cacheDir)
23 {
24     vulkanInterface_ = std::make_shared<RsVulkanInterface>();
25     vulkanInterface_->Init(VulkanInterfaceType::BASIC_RENDER, false);
26 }
27 
~EffectVulkanContext()28 EffectVulkanContext::~EffectVulkanContext()
29 {
30     std::lock_guard<std::mutex> lock(drawingContextMutex_);
31     drawingContextMap_.clear();
32 }
33 
GetSingleton(const std::string & cacheDir)34 EffectVulkanContext& EffectVulkanContext::GetSingleton(const std::string& cacheDir)
35 {
36     static EffectVulkanContext singleton = EffectVulkanContext(cacheDir);
37     return singleton;
38 }
39 
ReleaseDrawingContextForThread(int tid)40 void EffectVulkanContext::ReleaseDrawingContextForThread(int tid)
41 {
42     std::lock_guard<std::mutex> lock(drawingContextMutex_);
43     drawingContextMap_.erase(tid);
44 }
45 
SaveNewDrawingContext(int tid,std::shared_ptr<Drawing::GPUContext> drawingContext)46 void EffectVulkanContext::SaveNewDrawingContext(int tid, std::shared_ptr<Drawing::GPUContext> drawingContext)
47 {
48     std::lock_guard<std::mutex> lock(drawingContextMutex_);
49     static thread_local auto func = [tid]() {
50         EffectVulkanContext::ReleaseDrawingContextForThread(tid);
51     };
52     static thread_local auto drawContextHolder = std::make_shared<DrawContextHolder>(func);
53     drawingContextMap_[tid] = drawingContext;
54 }
55 
CreateDrawingContext()56 std::shared_ptr<Drawing::GPUContext> EffectVulkanContext::CreateDrawingContext()
57 {
58     static thread_local int tid = gettid();
59     {
60         std::lock_guard<std::mutex> lock(drawingContextMutex_);
61         auto iter = drawingContextMap_.find(tid);
62         if (iter != drawingContextMap_.end() && iter->second != nullptr) {
63             return iter->second; // has created before
64         }
65     }
66     auto context = vulkanInterface_->DoCreateDrawingContext();
67     SaveNewDrawingContext(tid, context);
68 
69     return context;
70 }
71 }