1 /* 2 * Copyright (c) 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 NATIVE_BUFFER_UTILS 17 #define NATIVE_BUFFER_UTILS 18 19 #include <atomic> 20 21 #include "native_buffer_inner.h" 22 #include "sync_fence.h" 23 #include "rs_vulkan_context.h" 24 #include "native_window.h" 25 #ifdef USE_M133_SKIA 26 #include "include/gpu/ganesh/GrDirectContext.h" 27 #include "include/gpu/ganesh/GrBackendSemaphore.h" 28 #else 29 #include "include/gpu/GrDirectContext.h" 30 #include "include/gpu/GrBackendSemaphore.h" 31 #endif 32 #include "include/core/SkSurface.h" 33 #include "draw/surface.h" 34 #include "image/image.h" 35 #include "drawing/engine_adapter/skia_adapater/skia_color_space.h" 36 37 namespace OHOS::Rosen { 38 struct DestroySemaphoreInfo { 39 PFN_vkDestroySemaphore mDestroyFunction; 40 VkDevice mDevice; 41 VkSemaphore mSemaphore; 42 43 std::atomic<int> mRefs = 2; DestroySemaphoreInfoDestroySemaphoreInfo44 DestroySemaphoreInfo(PFN_vkDestroySemaphore destroyFunction, VkDevice device, 45 VkSemaphore semaphore) 46 : mDestroyFunction(destroyFunction), mDevice(device), mSemaphore(semaphore) {} 47 DestroySemaphoreDestroySemaphoreInfo48 static void DestroySemaphore(void *context) 49 { 50 if (context == nullptr) { 51 return; 52 } 53 DestroySemaphoreInfo* info = reinterpret_cast<DestroySemaphoreInfo*>(context); 54 --info->mRefs; 55 if (info->mRefs == 0) { 56 info->mDestroyFunction(info->mDevice, info->mSemaphore, nullptr); 57 delete info; 58 } 59 } 60 }; 61 62 namespace NativeBufferUtils { 63 constexpr uint32_t VKIMAGE_LIMIT_SIZE = 10000 * 10000; // Vk-Image Size need less than 10000*10000 64 void DeleteVkImage(void* context); 65 class VulkanCleanupHelper { 66 public: 67 VulkanCleanupHelper(RsVulkanContext& vkContext, VkImage image, VkDeviceMemory memory, 68 const std::string& statName = "") 69 : fDevice_(vkContext.GetDevice()), 70 fImage_(image), 71 fMemory_(memory), 72 fDestroyImage_(vkContext.GetRsVulkanInterface().vkDestroyImage), 73 fFreeMemory_(vkContext.GetRsVulkanInterface().vkFreeMemory), 74 fStatName(statName), 75 fRefCnt_(1) {} ~VulkanCleanupHelper()76 ~VulkanCleanupHelper() 77 { 78 if (fStatName.length()) { 79 RsVulkanMemStat& memStat = RsVulkanContext::GetSingleton().GetRsVkMemStat(); 80 memStat.DeleteResource(fStatName); 81 } 82 fDestroyImage_(fDevice_, fImage_, nullptr); 83 fFreeMemory_(fDevice_, fMemory_, nullptr); 84 } 85 Ref()86 VulkanCleanupHelper* Ref() 87 { 88 (void)fRefCnt_.fetch_add(+1, std::memory_order_relaxed); 89 return this; 90 } 91 UnRef()92 void UnRef() 93 { 94 if (fRefCnt_.fetch_add(-1, std::memory_order_acq_rel) == 1) { 95 delete this; 96 } 97 } 98 99 private: 100 VkDevice fDevice_; 101 VkImage fImage_; 102 VkDeviceMemory fMemory_; 103 PFN_vkDestroyImage fDestroyImage_; 104 PFN_vkFreeMemory fFreeMemory_; 105 std::string fStatName; 106 mutable std::atomic<int32_t> fRefCnt_; 107 }; 108 109 struct NativeSurfaceInfo { 110 NativeSurfaceInfo() = default; 111 NativeSurfaceInfo(NativeSurfaceInfo &&) = default; 112 NativeSurfaceInfo &operator=(NativeSurfaceInfo &&) = default; 113 NativeSurfaceInfo(const NativeSurfaceInfo &) = delete; 114 NativeSurfaceInfo &operator=(const NativeSurfaceInfo &) = delete; 115 116 NativeWindow* window = nullptr; 117 NativeWindowBuffer* nativeWindowBuffer = nullptr; 118 VkImage image = VK_NULL_HANDLE; // skia will destroy image 119 std::unique_ptr<SyncFence> fence = nullptr; 120 std::shared_ptr<Drawing::Surface> drawingSurface = nullptr; 121 int32_t lastPresentedCount = -1; 122 GraphicColorGamut graphicColorGamut = GRAPHIC_COLOR_GAMUT_INVALID; ~NativeSurfaceInfoNativeSurfaceInfo123 ~NativeSurfaceInfo() 124 { 125 drawingSurface = nullptr; 126 if (window != nullptr) { 127 NativeObjectUnreference(window); 128 window = nullptr; 129 } 130 if (nativeWindowBuffer != nullptr) { 131 NativeObjectUnreference(nativeWindowBuffer); 132 nativeWindowBuffer = nullptr; 133 } 134 } 135 }; 136 137 bool MakeFromNativeWindowBuffer(std::shared_ptr<Drawing::GPUContext> skContext, NativeWindowBuffer* nativeWindowBuffer, 138 NativeSurfaceInfo& nativeSurface, int width, int height, bool isProtected = false); 139 140 Drawing::BackendTexture MakeBackendTextureFromNativeBuffer(NativeWindowBuffer* nativeWindowBuffer, 141 int width, int height, bool isProtected = false); 142 143 std::shared_ptr<Drawing::Surface> CreateFromNativeWindowBuffer(Drawing::GPUContext* gpuContext, 144 const Drawing::ImageInfo& imageInfo, NativeSurfaceInfo& nativeSurface); 145 146 #ifdef RS_ENABLE_VK 147 uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties); 148 void SetVkImageInfo(std::shared_ptr<OHOS::Rosen::Drawing::VKTextureInfo> vkImageInfo, 149 const VkImageCreateInfo& imageInfo); 150 Drawing::BackendTexture MakeBackendTexture( 151 uint32_t width, uint32_t height, pid_t pid, VkFormat format = VK_FORMAT_R8G8B8A8_UNORM); 152 Drawing::BackendTexture SetBackendTexture(RsVulkanInterface& vkContext, VkDevice device, VkImage image, 153 uint32_t width, uint32_t height, VkDeviceMemory memory, VkImageCreateInfo imageInfo, pid_t pid); 154 155 void CreateVkSemaphore(VkSemaphore& semaphore); 156 157 void GetFenceFdFromSemaphore(VkSemaphore& semaphore, int32_t& syncFenceFd); 158 #endif 159 } 160 } // OHOS::Rosen 161 #endif