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 RS_SURFACE_OHOS_VULKAN_H 17 #define RS_SURFACE_OHOS_VULKAN_H 18 19 #include <cstdint> 20 21 #include <list> 22 #include <unordered_map> 23 #include "native_window.h" 24 #include "vulkan/vulkan_core.h" 25 #include "platform/ohos/backend/rs_vulkan_context.h" 26 #include "sync_fence.h" 27 #include "native_buffer_utils.h" 28 #include "image/image.h" 29 #include "platform/ohos/rs_surface_ohos.h" 30 #include "rs_surface_frame_ohos_vulkan.h" 31 #include <surface.h> 32 33 typedef enum VkSemaphoreExtTypeHUAWEI { 34 VK_SEMAPHORE_EXT_TYPE_HTS_HUAWEI = 0x80000000, 35 VK_SEMAPHORE_EXT_TYPE_FFTS_HUAWEI = 0x80000001, 36 } VkSemaphoreExtTypeHUAWEI; 37 38 typedef struct VkSemaphoreExtTypeCreateInfoHUAWEI { 39 VkStructureTypeHUAWEI sType; 40 const void* pNext; 41 VkSemaphoreExtTypeHUAWEI semaphoreExtType; 42 uint32_t eventId; 43 } VkSemaphoreExtTypeCreateInfoHUAWEI; 44 45 namespace OHOS { 46 namespace Rosen { 47 class RSSurfaceOhosVulkan : public RSSurfaceOhos { 48 public: 49 explicit RSSurfaceOhosVulkan(const sptr<Surface>& producer); 50 ~RSSurfaceOhosVulkan() override; 51 IsValid()52 bool IsValid() const override 53 { 54 return producer_ != nullptr; 55 } 56 57 std::unique_ptr<RSSurfaceFrame> RequestFrame( 58 int32_t width, int32_t height, uint64_t uiTimestamp, bool useAFBC = true, bool isProtected = false) override; 59 bool FlushFrame(std::unique_ptr<RSSurfaceFrame>& frame, uint64_t uiTimestamp) override; 60 void SetColorSpace(GraphicColorGamut colorSpace) override; 61 void SetSurfaceBufferUsage(uint64_t usage) override; 62 void SetSurfacePixelFormat(int32_t pixelFormat) override; 63 sptr<SurfaceBuffer> GetCurrentBuffer() override; 64 void ClearBuffer() override; 65 void ResetBufferAge() override; 66 void SetUiTimeStamp(const std::unique_ptr<RSSurfaceFrame>& frame, uint64_t uiTimestamp) override; SetSkContext(std::shared_ptr<Drawing::GPUContext> skContext)67 void SetSkContext(std::shared_ptr<Drawing::GPUContext> skContext) 68 { 69 mSkContext = skContext; 70 } WaitSurfaceClear()71 void WaitSurfaceClear() 72 { 73 if (mSkContext) { 74 mSkContext->FlushAndSubmit(true); 75 } 76 mSurfaceMap.clear(); 77 } 78 79 int DupReservedFlushFd(); 80 81 int32_t RequestNativeWindowBuffer(NativeWindowBuffer** nativeWindowBuffer, int32_t width, int32_t height, 82 int& fenceFd, bool useAFBC, bool isProtected = false); 83 bool PreAllocateProtectedBuffer(int32_t width, int32_t height); 84 #if defined(ROSEN_OHOS) && defined(ENABLE_HPAE_BLUR) 85 bool NeedSubmitWithFFTS(); 86 void HcsSubmitGPGpuAndHpae(uint64_t preFrameId, int64_t curFrameId); 87 #endif 88 89 private: 90 struct NativeWindow* mNativeWindow = nullptr; 91 int mWidth = -1; 92 int mHeight = -1; 93 int mReservedFlushFd = -1; 94 void SetNativeWindowInfo(int32_t width, int32_t height, bool useAFBC, bool isProtected = false); 95 int32_t mPresentCount = 0; 96 std::list<NativeWindowBuffer*> mSurfaceList; 97 std::list<std::pair<NativeWindowBuffer*, int>> protectedSurfaceBufferList_; 98 std::mutex protectedSurfaceBufferListMutex_; 99 std::unordered_map<NativeWindowBuffer*, NativeBufferUtils::NativeSurfaceInfo> mSurfaceMap; 100 std::shared_ptr<Drawing::GPUContext> mSkContext = nullptr; 101 void CreateVkSemaphore(VkSemaphore& semaphore, 102 RsVulkanContext& vkContext, NativeBufferUtils::NativeSurfaceInfo& nativeSurface); 103 void PrepareHdrSemaphoreVector( 104 GrBackendSemaphore& backendSemaphore, NativeBufferUtils::NativeSurfaceInfo& surface, 105 std::vector<uint64_t> &frameIdVec, 106 std::vector<GrBackendSemaphore> &semphoreVec); 107 }; 108 109 } // namespace Rosen 110 } // namespace OHOS 111 112 #endif // RS_SURFACE_OHOS_VULKAN_H 113