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 #include "screen_manager/rs_screen_manager.h" 32 33 namespace OHOS { 34 namespace Rosen { 35 36 class NativeVkImageRes { 37 public: NativeVkImageRes(NativeWindowBuffer * nativeWindowBuffer,Drawing::BackendTexture backendTexture,NativeBufferUtils::VulkanCleanupHelper * vulkanCleanupHelper)38 NativeVkImageRes(NativeWindowBuffer* nativeWindowBuffer, Drawing::BackendTexture backendTexture, 39 NativeBufferUtils::VulkanCleanupHelper* vulkanCleanupHelper) 40 : mNativeWindowBuffer(nativeWindowBuffer), 41 mBackendTexture_(backendTexture), 42 mVulkanCleanupHelper(vulkanCleanupHelper) 43 { 44 } 45 46 ~NativeVkImageRes(); 47 GetBackendTexture()48 const Drawing::BackendTexture& GetBackendTexture() const 49 { 50 return mBackendTexture_; 51 } 52 RefCleanupHelper()53 NativeBufferUtils::VulkanCleanupHelper* RefCleanupHelper() 54 { 55 return mVulkanCleanupHelper->Ref(); 56 } 57 58 static std::shared_ptr<NativeVkImageRes> Create(sptr<OHOS::SurfaceBuffer> buffer); 59 GetThreadIndex()60 pid_t GetThreadIndex() const 61 { 62 return threadIndex_; 63 } 64 65 void SetThreadIndex(const pid_t threadIndex = UNI_RENDER_THREAD_INDEX) 66 { 67 threadIndex_ = threadIndex; 68 } 69 SetBufferDeleteFromCacheFlag(const bool & flag)70 void SetBufferDeleteFromCacheFlag(const bool &flag) 71 { 72 isBufferDeleteFromCache = flag; 73 } 74 GetBufferDeleteFromCacheFlag()75 bool GetBufferDeleteFromCacheFlag() const 76 { 77 return isBufferDeleteFromCache; 78 } 79 80 private: 81 NativeWindowBuffer* mNativeWindowBuffer; 82 Drawing::BackendTexture mBackendTexture_; 83 NativeBufferUtils::VulkanCleanupHelper* mVulkanCleanupHelper; 84 pid_t threadIndex_ = UNI_RENDER_THREAD_INDEX; 85 bool isBufferDeleteFromCache = false; 86 }; 87 88 class RSVkImageManager { 89 public: 90 RSVkImageManager() = default; 91 ~RSVkImageManager() noexcept = default; 92 93 std::shared_ptr<NativeVkImageRes> MapVkImageFromSurfaceBuffer(const sptr<OHOS::SurfaceBuffer>& buffer, 94 const sptr<SyncFence>& acquireFence, pid_t threadIndex, ScreenId screenId); 95 void UnMapVkImageFromSurfaceBuffer(uint32_t seqNum, bool isMatchVirtualScreen = false); 96 void UnMapAllVkImageVirtualScreenCache(); 97 void ShrinkCachesIfNeeded(); 98 std::shared_ptr<NativeVkImageRes> CreateImageCacheFromBuffer(sptr<OHOS::SurfaceBuffer> buffer, 99 const sptr<SyncFence>& acquireFence); 100 void DumpVkImageInfo(std::string &dumpString); 101 private: 102 std::shared_ptr<NativeVkImageRes> NewImageCacheFromBuffer( 103 const sptr<OHOS::SurfaceBuffer>& buffer, pid_t threadIndex, bool isProtectedCondition, 104 bool isMatchVirtualScreen = false); 105 bool isVirtualScreen(ScreenId screenId); 106 mutable std::mutex opMutex_; 107 std::queue<uint32_t> cacheQueue_; // fifo, size restricted by MAX_CACHE_SIZE 108 std::unordered_map<uint32_t, std::shared_ptr<NativeVkImageRes>> imageCacheSeqs_; // guarded by opMutex_ 109 std::unordered_map<uint32_t, std::shared_ptr<NativeVkImageRes>> imageCacheVirtualScreenSeqs_; // guarded by opMutex_ 110 }; 111 112 } // namespace Rosen 113 } // namespace OHOS 114 115 #endif // RS_VK_IMAGE_MANAGER_H