1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2015 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci
9cb93a386Sopenharmony_ci#ifndef GrVkRenderTarget_DEFINED
10cb93a386Sopenharmony_ci#define GrVkRenderTarget_DEFINED
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ci#include "src/gpu/GrRenderTarget.h"
13cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkImage.h"
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ci#include "include/gpu/vk/GrVkTypes.h"
16cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkRenderPass.h"
17cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkResourceProvider.h"
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ciclass GrVkFramebuffer;
20cb93a386Sopenharmony_ciclass GrVkGpu;
21cb93a386Sopenharmony_ciclass GrVkImageView;
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_cistruct GrVkImageInfo;
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ciclass GrVkRenderTarget : public GrRenderTarget {
26cb93a386Sopenharmony_cipublic:
27cb93a386Sopenharmony_ci    static sk_sp<GrVkRenderTarget> MakeWrappedRenderTarget(GrVkGpu*,
28cb93a386Sopenharmony_ci                                                           SkISize,
29cb93a386Sopenharmony_ci                                                           int sampleCnt,
30cb93a386Sopenharmony_ci                                                           const GrVkImageInfo&,
31cb93a386Sopenharmony_ci                                                           sk_sp<GrBackendSurfaceMutableStateImpl>);
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci    static sk_sp<GrVkRenderTarget> MakeSecondaryCBRenderTarget(GrVkGpu*,
34cb93a386Sopenharmony_ci                                                               SkISize,
35cb93a386Sopenharmony_ci                                                               const GrVkDrawableInfo& vkInfo);
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci    ~GrVkRenderTarget() override;
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_ci    GrBackendFormat backendFormat() const override;
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci    using SelfDependencyFlags = GrVkRenderPass::SelfDependencyFlags;
42cb93a386Sopenharmony_ci    using LoadFromResolve = GrVkRenderPass::LoadFromResolve;
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci    const GrVkFramebuffer* getFramebuffer(bool withResolve,
45cb93a386Sopenharmony_ci                                          bool withStencil,
46cb93a386Sopenharmony_ci                                          SelfDependencyFlags selfDepFlags,
47cb93a386Sopenharmony_ci                                          LoadFromResolve);
48cb93a386Sopenharmony_ci    const GrVkFramebuffer* getFramebuffer(const GrVkRenderPass& renderPass) {
49cb93a386Sopenharmony_ci        return this->getFramebuffer(renderPass.hasResolveAttachment(),
50cb93a386Sopenharmony_ci                                    renderPass.hasStencilAttachment(),
51cb93a386Sopenharmony_ci                                    renderPass.selfDependencyFlags(),
52cb93a386Sopenharmony_ci                                    renderPass.loadFromResolve());
53cb93a386Sopenharmony_ci    }
54cb93a386Sopenharmony_ci
55cb93a386Sopenharmony_ci    GrVkImage* colorAttachment() const {
56cb93a386Sopenharmony_ci        SkASSERT(!this->wrapsSecondaryCommandBuffer());
57cb93a386Sopenharmony_ci        return fColorAttachment.get();
58cb93a386Sopenharmony_ci    }
59cb93a386Sopenharmony_ci    const GrVkImageView* colorAttachmentView() const {
60cb93a386Sopenharmony_ci        SkASSERT(!this->wrapsSecondaryCommandBuffer());
61cb93a386Sopenharmony_ci        return this->colorAttachment()->framebufferView();
62cb93a386Sopenharmony_ci    }
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_ci    GrVkImage* resolveAttachment() const {
65cb93a386Sopenharmony_ci        SkASSERT(!this->wrapsSecondaryCommandBuffer());
66cb93a386Sopenharmony_ci        return fResolveAttachment.get();
67cb93a386Sopenharmony_ci    }
68cb93a386Sopenharmony_ci    const GrVkImageView* resolveAttachmentView() const {
69cb93a386Sopenharmony_ci        SkASSERT(!this->wrapsSecondaryCommandBuffer());
70cb93a386Sopenharmony_ci        return fResolveAttachment->framebufferView();
71cb93a386Sopenharmony_ci    }
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci    // Returns the GrVkImage of the non-msaa attachment. If the color attachment has 1 sample,
74cb93a386Sopenharmony_ci    // then the color attachment will be returned. Otherwise, the resolve attachment is returned.
75cb93a386Sopenharmony_ci    // Note that in this second case the resolve attachment may be null if this was created by
76cb93a386Sopenharmony_ci    // wrapping an msaa VkImage.
77cb93a386Sopenharmony_ci    GrVkImage* nonMSAAAttachment() const;
78cb93a386Sopenharmony_ci
79cb93a386Sopenharmony_ci    // Returns the attachment that is used for all external client facing operations. This will be
80cb93a386Sopenharmony_ci    // either a wrapped color attachment or the resolve attachment for created VkImages.
81cb93a386Sopenharmony_ci    GrVkImage* externalAttachment() const {
82cb93a386Sopenharmony_ci        return fResolveAttachment ? fResolveAttachment.get() : fColorAttachment.get();
83cb93a386Sopenharmony_ci    }
84cb93a386Sopenharmony_ci
85cb93a386Sopenharmony_ci    const GrVkRenderPass* getSimpleRenderPass(
86cb93a386Sopenharmony_ci            bool withResolve,
87cb93a386Sopenharmony_ci            bool withStencil,
88cb93a386Sopenharmony_ci            SelfDependencyFlags selfDepFlags,
89cb93a386Sopenharmony_ci            LoadFromResolve);
90cb93a386Sopenharmony_ci    GrVkResourceProvider::CompatibleRPHandle compatibleRenderPassHandle(
91cb93a386Sopenharmony_ci            bool withResolve,
92cb93a386Sopenharmony_ci            bool withStencil,
93cb93a386Sopenharmony_ci            SelfDependencyFlags selfDepFlags,
94cb93a386Sopenharmony_ci            LoadFromResolve);
95cb93a386Sopenharmony_ci
96cb93a386Sopenharmony_ci    bool wrapsSecondaryCommandBuffer() const { return SkToBool(fExternalFramebuffer); }
97cb93a386Sopenharmony_ci    sk_sp<GrVkFramebuffer> externalFramebuffer() const;
98cb93a386Sopenharmony_ci
99cb93a386Sopenharmony_ci    bool canAttemptStencilAttachment(bool useMSAASurface) const override;
100cb93a386Sopenharmony_ci
101cb93a386Sopenharmony_ci    GrBackendRenderTarget getBackendRenderTarget() const override;
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_ci    void getAttachmentsDescriptor(GrVkRenderPass::AttachmentsDescriptor* desc,
104cb93a386Sopenharmony_ci                                  GrVkRenderPass::AttachmentFlags* flags,
105cb93a386Sopenharmony_ci                                  bool withResolve,
106cb93a386Sopenharmony_ci                                  bool withStencil);
107cb93a386Sopenharmony_ci
108cb93a386Sopenharmony_ci    // Reconstruct the render target attachment information from the programInfo. This includes
109cb93a386Sopenharmony_ci    // which attachments the render target will have (color, stencil) and the attachments' formats
110cb93a386Sopenharmony_ci    // and sample counts - cf. getAttachmentsDescriptor.
111cb93a386Sopenharmony_ci    static void ReconstructAttachmentsDescriptor(const GrVkCaps& vkCaps,
112cb93a386Sopenharmony_ci                                                 const GrProgramInfo& programInfo,
113cb93a386Sopenharmony_ci                                                 GrVkRenderPass::AttachmentsDescriptor* desc,
114cb93a386Sopenharmony_ci                                                 GrVkRenderPass::AttachmentFlags* flags);
115cb93a386Sopenharmony_ci
116cb93a386Sopenharmony_ciprotected:
117cb93a386Sopenharmony_ci    enum class CreateType {
118cb93a386Sopenharmony_ci        kDirectlyWrapped, // We need to register this in the ctor
119cb93a386Sopenharmony_ci        kFromTextureRT,   // Skip registering this to cache since TexRT will handle it
120cb93a386Sopenharmony_ci    };
121cb93a386Sopenharmony_ci
122cb93a386Sopenharmony_ci    GrVkRenderTarget(GrVkGpu* gpu,
123cb93a386Sopenharmony_ci                     SkISize dimensions,
124cb93a386Sopenharmony_ci                     sk_sp<GrVkImage> colorAttachment,
125cb93a386Sopenharmony_ci                     sk_sp<GrVkImage> resolveImage,
126cb93a386Sopenharmony_ci                     CreateType createType);
127cb93a386Sopenharmony_ci
128cb93a386Sopenharmony_ci    void onAbandon() override;
129cb93a386Sopenharmony_ci    void onRelease() override;
130cb93a386Sopenharmony_ci
131cb93a386Sopenharmony_ci    // This returns zero since the memory should all be handled by the attachments
132cb93a386Sopenharmony_ci    size_t onGpuMemorySize() const override { return 0; }
133cb93a386Sopenharmony_ci
134cb93a386Sopenharmony_ciprivate:
135cb93a386Sopenharmony_ci    // For external framebuffers that wrap a secondary command buffer
136cb93a386Sopenharmony_ci    GrVkRenderTarget(GrVkGpu* gpu,
137cb93a386Sopenharmony_ci                     SkISize dimensions,
138cb93a386Sopenharmony_ci                     sk_sp<GrVkFramebuffer> externalFramebuffer);
139cb93a386Sopenharmony_ci
140cb93a386Sopenharmony_ci    void setFlags();
141cb93a386Sopenharmony_ci
142cb93a386Sopenharmony_ci    GrVkGpu* getVkGpu() const;
143cb93a386Sopenharmony_ci
144cb93a386Sopenharmony_ci    GrVkImage* dynamicMSAAAttachment();
145cb93a386Sopenharmony_ci    GrVkImage* msaaAttachment();
146cb93a386Sopenharmony_ci
147cb93a386Sopenharmony_ci    // This return since the memory should all be handled by the attachments
148cb93a386Sopenharmony_ci    void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const override {
149cb93a386Sopenharmony_ci        return;
150cb93a386Sopenharmony_ci    }
151cb93a386Sopenharmony_ci
152cb93a386Sopenharmony_ci    std::pair<const GrVkRenderPass*, GrVkResourceProvider::CompatibleRPHandle>
153cb93a386Sopenharmony_ci        createSimpleRenderPass(bool withResolve,
154cb93a386Sopenharmony_ci                               bool withStencil,
155cb93a386Sopenharmony_ci                               SelfDependencyFlags selfDepFlags,
156cb93a386Sopenharmony_ci                               LoadFromResolve);
157cb93a386Sopenharmony_ci    void createFramebuffer(bool withResolve,
158cb93a386Sopenharmony_ci                           bool withStencil,
159cb93a386Sopenharmony_ci                           SelfDependencyFlags selfDepFlags,
160cb93a386Sopenharmony_ci                           LoadFromResolve);
161cb93a386Sopenharmony_ci
162cb93a386Sopenharmony_ci    bool completeStencilAttachment(GrAttachment* stencil, bool useMSAASurface) override;
163cb93a386Sopenharmony_ci
164cb93a386Sopenharmony_ci    // In Vulkan we call the release proc after we are finished with the underlying
165cb93a386Sopenharmony_ci    // GrVkImage::Resource object (which occurs after the GPU has finished all work on it).
166cb93a386Sopenharmony_ci    void onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper) override {
167cb93a386Sopenharmony_ci        // Forward the release proc on to the GrVkImage of the release attachment if we have one,
168cb93a386Sopenharmony_ci        // otherwise the color attachment.
169cb93a386Sopenharmony_ci        GrVkImage* attachment =
170cb93a386Sopenharmony_ci                fResolveAttachment ? fResolveAttachment.get() : fColorAttachment.get();
171cb93a386Sopenharmony_ci        attachment->setResourceRelease(std::move(releaseHelper));
172cb93a386Sopenharmony_ci    }
173cb93a386Sopenharmony_ci
174cb93a386Sopenharmony_ci    void releaseInternalObjects();
175cb93a386Sopenharmony_ci
176cb93a386Sopenharmony_ci    sk_sp<GrVkImage> fColorAttachment;
177cb93a386Sopenharmony_ci    sk_sp<GrVkImage> fResolveAttachment;
178cb93a386Sopenharmony_ci    sk_sp<GrVkImage> fDynamicMSAAAttachment;
179cb93a386Sopenharmony_ci
180cb93a386Sopenharmony_ci    // We can have a renderpass with and without resolve attachment, stencil attachment,
181cb93a386Sopenharmony_ci    // input attachment dependency, advanced blend dependency, and loading from resolve. All 5 of
182cb93a386Sopenharmony_ci    // these being completely orthogonal. Thus we have a total of 32 types of render passes. We then
183cb93a386Sopenharmony_ci    // cache a framebuffer for each type of these render passes.
184cb93a386Sopenharmony_ci    static constexpr int kNumCachedFramebuffers = 32;
185cb93a386Sopenharmony_ci
186cb93a386Sopenharmony_ci    sk_sp<const GrVkFramebuffer> fCachedFramebuffers[kNumCachedFramebuffers];
187cb93a386Sopenharmony_ci
188cb93a386Sopenharmony_ci    const GrVkDescriptorSet* fCachedInputDescriptorSet = nullptr;
189cb93a386Sopenharmony_ci
190cb93a386Sopenharmony_ci    sk_sp<GrVkFramebuffer> fExternalFramebuffer;
191cb93a386Sopenharmony_ci};
192cb93a386Sopenharmony_ci
193cb93a386Sopenharmony_ci#endif
194