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