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 #ifndef RS_VK_IMAGE_MANAGER_H
17 #define RS_VK_IMAGE_MANAGER_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <queue>
22 #include <unordered_map>
23 
24 #include "surface.h"
25 #include "sync_fence.h"
26 #include "vulkan/vulkan_core.h"
27 #include "platform/ohos/backend/rs_vulkan_context.h"
28 #include "native_window.h"
29 #include "common/rs_common_def.h"
30 #include "platform/ohos/backend/native_buffer_utils.h"
31 
32 namespace OHOS {
33 namespace Rosen {
34 
35 class NativeVkImageRes {
36 public:
NativeVkImageRes(NativeWindowBuffer * nativeWindowBuffer,Drawing::BackendTexture backendTexture,NativeBufferUtils::VulkanCleanupHelper * vulkanCleanupHelper)37     NativeVkImageRes(NativeWindowBuffer* nativeWindowBuffer, Drawing::BackendTexture backendTexture,
38         NativeBufferUtils::VulkanCleanupHelper* vulkanCleanupHelper)
39         : mNativeWindowBuffer(nativeWindowBuffer),
40         mBackendTexture_(backendTexture),
41         mVulkanCleanupHelper(vulkanCleanupHelper)
42     {
43     }
44 
45     ~NativeVkImageRes();
46 
GetBackendTexture()47     const Drawing::BackendTexture& GetBackendTexture() const
48     {
49         return mBackendTexture_;
50     }
51 
RefCleanupHelper()52     NativeBufferUtils::VulkanCleanupHelper* RefCleanupHelper()
53     {
54         return mVulkanCleanupHelper->Ref();
55     }
56 
57     static std::shared_ptr<NativeVkImageRes> Create(sptr<OHOS::SurfaceBuffer> buffer);
58 
GetThreadIndex()59     pid_t GetThreadIndex() const
60     {
61         return threadIndex_;
62     }
63 
64     void SetThreadIndex(const pid_t threadIndex = UNI_RENDER_THREAD_INDEX)
65     {
66         threadIndex_ = threadIndex;
67     }
68 
69 private:
70     NativeWindowBuffer* mNativeWindowBuffer;
71     Drawing::BackendTexture mBackendTexture_;
72     NativeBufferUtils::VulkanCleanupHelper* mVulkanCleanupHelper;
73     pid_t threadIndex_ = UNI_RENDER_THREAD_INDEX;
74 };
75 
76 class RSVkImageManager {
77 public:
78     RSVkImageManager() = default;
79     ~RSVkImageManager() noexcept = default;
80 
81     std::shared_ptr<NativeVkImageRes> MapVkImageFromSurfaceBuffer(const sptr<OHOS::SurfaceBuffer>& buffer,
82         const sptr<SyncFence>& acquireFence, pid_t threadIndex);
83     void UnMapVkImageFromSurfaceBuffer(int32_t seqNum);
84     void ShrinkCachesIfNeeded();
85     std::shared_ptr<NativeVkImageRes> CreateImageCacheFromBuffer(sptr<OHOS::SurfaceBuffer> buffer,
86         const sptr<SyncFence>& acquireFence);
87 
88 private:
89     std::shared_ptr<NativeVkImageRes> NewImageCacheFromBuffer(
90         const sptr<OHOS::SurfaceBuffer>& buffer, pid_t threadIndex);
91 
92     mutable std::mutex opMutex_;
93     std::queue<int32_t> cacheQueue_; // fifo, size restricted by MAX_CACHE_SIZE
94     std::unordered_map<int32_t, std::shared_ptr<NativeVkImageRes>> imageCacheSeqs_; // guarded by opMutex_
95 };
96 
97 } // namespace Rosen
98 } // namespace OHOS
99 
100 #endif // RS_VK_IMAGE_MANAGER_H