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: 37 #ifndef USE_ROSEN_DRAWING NativeVkImageRes(NativeWindowBuffer * nativeWindowBuffer,GrBackendTexture backendTexture,NativeBufferUtils::VulkanCleanupHelper * vulkanCleanupHelper)38 NativeVkImageRes(NativeWindowBuffer* nativeWindowBuffer, GrBackendTexture backendTexture, 39 #else 40 NativeVkImageRes(NativeWindowBuffer* nativeWindowBuffer, Drawing::BackendTexture backendTexture, 41 #endif 42 NativeBufferUtils::VulkanCleanupHelper* vulkanCleanupHelper) 43 : mNativeWindowBuffer(nativeWindowBuffer), 44 mBackendTexture_(backendTexture), 45 mVulkanCleanupHelper(vulkanCleanupHelper) 46 { 47 } 48 49 ~NativeVkImageRes(); 50 51 #ifndef USE_ROSEN_DRAWING GetBackendTexture()52 const GrBackendTexture& GetBackendTexture() const 53 #else 54 const Drawing::BackendTexture& GetBackendTexture() const 55 #endif 56 { 57 return mBackendTexture_; 58 } 59 RefCleanupHelper()60 NativeBufferUtils::VulkanCleanupHelper* RefCleanupHelper() 61 { 62 return mVulkanCleanupHelper->Ref(); 63 } 64 65 static std::shared_ptr<NativeVkImageRes> Create(sptr<OHOS::SurfaceBuffer> buffer); 66 GetThreadIndex()67 uint32_t GetThreadIndex() const 68 { 69 return threadIndex_; 70 } 71 72 void SetThreadIndex(const uint32_t threadIndex = UNI_MAIN_THREAD_INDEX) 73 { 74 threadIndex_ = threadIndex; 75 } 76 77 private: 78 NativeWindowBuffer* mNativeWindowBuffer; 79 #ifndef USE_ROSEN_DRAWING 80 GrBackendTexture mBackendTexture_; 81 #else 82 Drawing::BackendTexture mBackendTexture_; 83 #endif 84 NativeBufferUtils::VulkanCleanupHelper* mVulkanCleanupHelper; 85 uint32_t threadIndex_ = UNI_MAIN_THREAD_INDEX; 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, uint32_t threadIndex); 95 void UnMapVkImageFromSurfaceBuffer(int32_t seqNum); 96 void UnMapVkImageFromSurfaceBufferForUniRedraw(int32_t seqNum); 97 void ShrinkCachesIfNeeded(bool isForUniRedraw = false); 98 std::shared_ptr<NativeVkImageRes> CreateImageCacheFromBuffer(sptr<OHOS::SurfaceBuffer> buffer, 99 const sptr<SyncFence>& acquireFence); 100 101 private: 102 std::shared_ptr<NativeVkImageRes> NewImageCacheFromBuffer( 103 const sptr<OHOS::SurfaceBuffer>& buffer, uint32_t threadIndex); 104 105 mutable std::mutex opMutex_; 106 static constexpr size_t MAX_CACHE_SIZE = 16; 107 std::queue<int32_t> cacheQueue_; // fifo, size restricted by MAX_CACHE_SIZE 108 std::unordered_map<int32_t, std::shared_ptr<NativeVkImageRes>> imageCacheSeqs_; // guarded by opMutex_ 109 }; 110 111 } // namespace Rosen 112 } // namespace OHOS 113 114 #endif // RS_VK_IMAGE_MANAGER_H