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 #include "include/gpu/GrDirectContext.h" 26 #include "include/gpu/GrBackendSemaphore.h" 27 #include "include/core/SkSurface.h" 28 #ifdef USE_ROSEN_DRAWING 29 #include "draw/surface.h" 30 #include "image/image.h" 31 #include "drawing/engine_adapter/skia_adapater/skia_color_space.h" 32 #endif 33 34 namespace OHOS::Rosen { 35 namespace NativeBufferUtils { 36 void DeleteVkImage(void* context); 37 class VulkanCleanupHelper { 38 public: VulkanCleanupHelper(RsVulkanContext & vkContext,VkImage image,VkDeviceMemory memory)39 VulkanCleanupHelper(RsVulkanContext& vkContext, VkImage image, VkDeviceMemory memory) 40 : fDevice_(vkContext.GetDevice()), 41 fImage_(image), 42 fMemory_(memory), 43 fDestroyImage_(vkContext.vkDestroyImage), 44 fFreeMemory_(vkContext.vkFreeMemory), 45 fRefCnt_(1) {} ~VulkanCleanupHelper()46 ~VulkanCleanupHelper() 47 { 48 fDestroyImage_(fDevice_, fImage_, nullptr); 49 fFreeMemory_(fDevice_, fMemory_, nullptr); 50 } 51 Ref()52 VulkanCleanupHelper* Ref() 53 { 54 (void)fRefCnt_.fetch_add(+1, std::memory_order_relaxed); 55 return this; 56 } 57 UnRef()58 void UnRef() 59 { 60 if (fRefCnt_.fetch_add(-1, std::memory_order_acq_rel) == 1) { 61 delete this; 62 } 63 } 64 65 private: 66 VkDevice fDevice_; 67 VkImage fImage_; 68 VkDeviceMemory fMemory_; 69 PFN_vkDestroyImage fDestroyImage_; 70 PFN_vkFreeMemory fFreeMemory_; 71 mutable std::atomic<int32_t> fRefCnt_; 72 }; 73 74 struct NativeSurfaceInfo { 75 NativeSurfaceInfo() = default; 76 NativeSurfaceInfo(NativeSurfaceInfo &&) = default; 77 NativeSurfaceInfo &operator=(NativeSurfaceInfo &&) = default; 78 NativeSurfaceInfo(const NativeSurfaceInfo &) = delete; 79 NativeSurfaceInfo &operator=(const NativeSurfaceInfo &) = delete; 80 81 NativeWindow* window = nullptr; 82 NativeWindowBuffer* nativeWindowBuffer = nullptr; 83 VkImage image = VK_NULL_HANDLE; // skia will destroy image 84 std::unique_ptr<SyncFence> fence = nullptr; 85 #ifndef USE_ROSEN_DRAWING 86 sk_sp<SkSurface> skSurface = nullptr; 87 #else 88 std::shared_ptr<Drawing::Surface> drawingSurface = nullptr; 89 #endif 90 int32_t lastPresentedCount = -1; 91 GraphicColorGamut graphicColorGamut = GRAPHIC_COLOR_GAMUT_INVALID; ~NativeSurfaceInfoNativeSurfaceInfo92 ~NativeSurfaceInfo() 93 { 94 #ifndef USE_ROSEN_DRAWING 95 skSurface = nullptr; 96 #else 97 drawingSurface = nullptr; 98 #endif 99 if (window != nullptr) { 100 NativeObjectUnreference(window); 101 window = nullptr; 102 } 103 if (nativeWindowBuffer != nullptr) { 104 NativeObjectUnreference(nativeWindowBuffer); 105 nativeWindowBuffer = nullptr; 106 } 107 } 108 }; 109 110 #ifndef USE_ROSEN_DRAWING 111 bool MakeFromNativeWindowBuffer(sk_sp<GrDirectContext> skContext, NativeWindowBuffer* nativeWindowBuffer, 112 NativeSurfaceInfo& nativeSurface, int width, int height); 113 114 GrBackendTexture MakeBackendTextureFromNativeBuffer(NativeWindowBuffer* nativeWindowBuffer, 115 int width, int height); 116 #else 117 bool MakeFromNativeWindowBuffer(std::shared_ptr<Drawing::GPUContext> skContext, NativeWindowBuffer* nativeWindowBuffer, 118 NativeSurfaceInfo& nativeSurface, int width, int height); 119 120 Drawing::BackendTexture MakeBackendTextureFromNativeBuffer(NativeWindowBuffer* nativeWindowBuffer, 121 int width, int height); 122 #endif 123 } 124 } // OHOS::Rosen 125 #endif