• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_vk_image_manager.h"
17 
18 #include "include/core/SkColorSpace.h"
19 #include "native_buffer_inner.h"
20 #include "platform/common/rs_log.h"
21 #include "include/gpu/GrBackendSurface.h"
22 #include "pipeline/rs_hardware_thread.h"
23 #include "pipeline/rs_task_dispatcher.h"
24 #include "rs_trace.h"
25 #include "common/rs_optional_trace.h"
26 #include "params/rs_surface_render_params.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 namespace {
WaitAcquireFence(const sptr<SyncFence> & acquireFence)31 void WaitAcquireFence(const sptr<SyncFence>& acquireFence)
32 {
33     if (acquireFence == nullptr) {
34         return;
35     }
36     acquireFence->Wait(3000); // 3000ms
37 }
38 
39 constexpr size_t MAX_CACHE_SIZE = 16;
40 }
41 
~NativeVkImageRes()42 NativeVkImageRes::~NativeVkImageRes()
43 {
44     NativeBufferUtils::DeleteVkImage(mVulkanCleanupHelper);
45     DestroyNativeWindowBuffer(mNativeWindowBuffer);
46 }
47 
Create(sptr<OHOS::SurfaceBuffer> buffer)48 std::shared_ptr<NativeVkImageRes> NativeVkImageRes::Create(sptr<OHOS::SurfaceBuffer> buffer)
49 {
50     if (buffer == nullptr) {
51         ROSEN_LOGE("NativeVkImageRes::Create buffer is nullptr");
52         return nullptr;
53     }
54     auto width = buffer->GetSurfaceBufferWidth();
55     auto height = buffer->GetSurfaceBufferHeight();
56     NativeWindowBuffer* nativeWindowBuffer = CreateNativeWindowBufferFromSurfaceBuffer(&buffer);
57     bool isProtected = (buffer->GetUsage() & BUFFER_USAGE_PROTECTED) != 0;
58     auto backendTexture = NativeBufferUtils::MakeBackendTextureFromNativeBuffer(nativeWindowBuffer,
59         width, height, isProtected);
60     if (!backendTexture.IsValid() || !backendTexture.GetTextureInfo().GetVKTextureInfo()) {
61         DestroyNativeWindowBuffer(nativeWindowBuffer);
62         return nullptr;
63     }
64     return std::make_unique<NativeVkImageRes>(
65         nativeWindowBuffer,
66         backendTexture,
67         new NativeBufferUtils::VulkanCleanupHelper(RsVulkanContext::GetSingleton(),
68             backendTexture.GetTextureInfo().GetVKTextureInfo()->vkImage,
69             backendTexture.GetTextureInfo().GetVKTextureInfo()->vkAlloc.memory));
70 }
71 
MapVkImageFromSurfaceBuffer(const sptr<OHOS::SurfaceBuffer> & buffer,const sptr<SyncFence> & acquireFence,pid_t threadIndex)72 std::shared_ptr<NativeVkImageRes> RSVkImageManager::MapVkImageFromSurfaceBuffer(
73     const sptr<OHOS::SurfaceBuffer>& buffer,
74     const sptr<SyncFence>& acquireFence,
75     pid_t threadIndex)
76 {
77     if (buffer == nullptr) {
78         ROSEN_LOGE("RSVkImageManager::MapVkImageFromSurfaceBuffer buffer is nullptr");
79         return nullptr;
80     }
81     WaitAcquireFence(acquireFence);
82     std::lock_guard<std::mutex> lock(opMutex_);
83     bool isProtectedCondition = (buffer->GetUsage() & BUFFER_USAGE_PROTECTED) ||
84         RsVulkanContext::GetSingleton().GetIsProtected();
85     auto bufferId = buffer->GetSeqNum();
86     if (isProtectedCondition || imageCacheSeqs_.find(bufferId) == imageCacheSeqs_.end()) {
87         return NewImageCacheFromBuffer(buffer, threadIndex, isProtectedCondition);
88     } else {
89         return imageCacheSeqs_[bufferId];
90     }
91 }
92 
CreateImageCacheFromBuffer(sptr<OHOS::SurfaceBuffer> buffer,const sptr<SyncFence> & acquireFence)93 std::shared_ptr<NativeVkImageRes> RSVkImageManager::CreateImageCacheFromBuffer(sptr<OHOS::SurfaceBuffer> buffer,
94     const sptr<SyncFence>& acquireFence)
95 {
96     WaitAcquireFence(acquireFence);
97     auto bufferId = buffer->GetSeqNum();
98     auto imageCache = NativeVkImageRes::Create(buffer);
99     if (imageCache == nullptr) {
100         ROSEN_LOGE(
101             "RSVkImageManager::CreateImageCacheFromBuffer: failed to create ImageCache for buffer id %{public}d.",
102             bufferId);
103         return nullptr;
104     }
105     return imageCache;
106 }
107 
NewImageCacheFromBuffer(const sptr<OHOS::SurfaceBuffer> & buffer,pid_t threadIndex,bool isProtectedCondition)108 std::shared_ptr<NativeVkImageRes> RSVkImageManager::NewImageCacheFromBuffer(
109     const sptr<OHOS::SurfaceBuffer>& buffer, pid_t threadIndex, bool isProtectedCondition)
110 {
111     auto bufferId = buffer->GetSeqNum();
112     auto imageCache = NativeVkImageRes::Create(buffer);
113     if (imageCache == nullptr) {
114         ROSEN_LOGE("RSVkImageManager::NewImageCacheFromBuffer: failed to create ImageCache for buffer id %{public}d.",
115             bufferId);
116         return {};
117     }
118 
119     imageCache->SetThreadIndex(threadIndex);
120     if (isProtectedCondition) {
121         return imageCache;
122     }
123     imageCacheSeqs_.emplace(bufferId, imageCache);
124     cacheQueue_.push(bufferId);
125     return imageCache;
126 }
127 
ShrinkCachesIfNeeded()128 void RSVkImageManager::ShrinkCachesIfNeeded()
129 {
130     while (cacheQueue_.size() > MAX_CACHE_SIZE) {
131         const int32_t id = cacheQueue_.front();
132         UnMapVkImageFromSurfaceBuffer(id);
133         cacheQueue_.pop();
134     }
135 }
136 
UnMapVkImageFromSurfaceBuffer(int32_t seqNum)137 void RSVkImageManager::UnMapVkImageFromSurfaceBuffer(int32_t seqNum)
138 {
139     pid_t threadIndex = UNI_RENDER_THREAD_INDEX;
140     {
141         std::lock_guard<std::mutex> lock(opMutex_);
142         if (imageCacheSeqs_.count(seqNum) == 0) {
143             return;
144         }
145         threadIndex = imageCacheSeqs_[seqNum]->GetThreadIndex();
146     }
147     auto func = [this, seqNum, threadIndex]() {
148         {
149             std::lock_guard<std::mutex> lock(opMutex_);
150             if (imageCacheSeqs_.count(seqNum) == 0) {
151                 return;
152             }
153             (void)imageCacheSeqs_.erase(seqNum);
154         }
155     };
156     RSTaskDispatcher::GetInstance().PostTask(threadIndex, func);
157 }
158 
159 } // namespace Rosen
160 } // namespace OHOS