1 /* 2 * Copyright 2016 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef GrVkFramebuffer_DEFINED 9 #define GrVkFramebuffer_DEFINED 10 11 #include "include/gpu/GrTypes.h" 12 #include "include/gpu/vk/GrVkTypes.h" 13 #include "src/gpu/vk/GrVkManagedResource.h" 14 #include "src/gpu/vk/GrVkResourceProvider.h" 15 16 #include <cinttypes> 17 18 class GrVkGpu; 19 class GrVkImage; 20 class GrVkImageView; 21 class GrVkRenderPass; 22 23 class GrVkFramebuffer : public GrVkManagedResource { 24 public: 25 static sk_sp<const GrVkFramebuffer> Make(GrVkGpu* gpu, 26 SkISize dimensions, 27 sk_sp<const GrVkRenderPass> compatibleRenderPass, 28 GrVkImage* colorAttachment, 29 GrVkImage* resolveAttachment, 30 GrVkImage* stencilAttachment, 31 GrVkResourceProvider::CompatibleRPHandle); 32 33 // Used for wrapped external secondary command buffers 34 GrVkFramebuffer(const GrVkGpu* gpu, 35 sk_sp<GrVkImage> colorAttachment, 36 sk_sp<const GrVkRenderPass> renderPass, 37 std::unique_ptr<GrVkSecondaryCommandBuffer>); 38 framebuffer()39 VkFramebuffer framebuffer() const { 40 SkASSERT(!this->isExternal()); 41 return fFramebuffer; 42 } 43 isExternal()44 bool isExternal() const { return fExternalRenderPass.get(); } externalRenderPass()45 const GrVkRenderPass* externalRenderPass() const { return fExternalRenderPass.get(); } 46 std::unique_ptr<GrVkSecondaryCommandBuffer> externalCommandBuffer(); 47 48 // When we wrap a secondary command buffer, we will record GrManagedResources onto it which need 49 // to be kept alive till the command buffer gets submitted and the GPU has finished. However, in 50 // the wrapped case, we don't know when the command buffer gets submitted and when it is 51 // finished on the GPU since the client is in charge of that. However, we do require that the 52 // client keeps the GrVkSecondaryCBDrawContext alive and call releaseResources on it once the 53 // GPU is finished all the work. Thus we can use this to manage the lifetime of our 54 // GrVkSecondaryCommandBuffers. By storing them on the external GrVkFramebuffer owned by the 55 // GrVkRenderTarget, which is owned by the SkGpuDevice on the GrVkSecondaryCBDrawContext, we 56 // assure that the GrManagedResources held by the GrVkSecondaryCommandBuffer don't get deleted 57 // before they are allowed to. 58 void returnExternalGrSecondaryCommandBuffer(std::unique_ptr<GrVkSecondaryCommandBuffer>); 59 60 #ifdef SK_TRACE_MANAGED_RESOURCES dumpInfo()61 void dumpInfo() const override { 62 SkDebugf("GrVkFramebuffer: %" PRIdPTR " (%d refs)\n", 63 (intptr_t)fFramebuffer, this->getRefCnt()); 64 } 65 #endif 66 compatibleRenderPass()67 const GrVkRenderPass* compatibleRenderPass() const { return fCompatibleRenderPass.get(); } 68 compatibleRenderPassHandle()69 GrVkResourceProvider::CompatibleRPHandle compatibleRenderPassHandle() const { 70 return fCompatibleRenderPassHandle; 71 } 72 colorAttachment()73 GrVkImage* colorAttachment() { return fColorAttachment.get(); } resolveAttachment()74 GrVkImage* resolveAttachment() { return fResolveAttachment.get(); } stencilAttachment()75 GrVkImage* stencilAttachment() { return fStencilAttachment.get(); } 76 77 private: 78 GrVkFramebuffer(const GrVkGpu* gpu, 79 VkFramebuffer framebuffer, 80 sk_sp<GrVkImage> colorAttachment, 81 sk_sp<GrVkImage> resolveAttachment, 82 sk_sp<GrVkImage> stencilAttachment, 83 sk_sp<const GrVkRenderPass> compatibleRenderPass, 84 GrVkResourceProvider::CompatibleRPHandle); 85 86 ~GrVkFramebuffer() override; 87 88 void freeGPUData() const override; 89 void releaseResources(); 90 91 VkFramebuffer fFramebuffer = VK_NULL_HANDLE; 92 93 sk_sp<GrVkImage> fColorAttachment; 94 sk_sp<GrVkImage> fResolveAttachment; 95 sk_sp<GrVkImage> fStencilAttachment; 96 97 sk_sp<const GrVkRenderPass> fCompatibleRenderPass; 98 GrVkResourceProvider::CompatibleRPHandle fCompatibleRenderPassHandle; 99 100 sk_sp<const GrVkRenderPass> fExternalRenderPass; 101 std::unique_ptr<GrVkSecondaryCommandBuffer> fExternalCommandBuffer; 102 }; 103 104 #endif 105