1/*
2 * Copyright 2015 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
9#ifndef GrVkRenderTarget_DEFINED
10#define GrVkRenderTarget_DEFINED
11
12#include "GrVkImage.h"
13#include "GrRenderTarget.h"
14
15#include "GrVkRenderPass.h"
16#include "GrVkResourceProvider.h"
17
18class GrVkCommandBuffer;
19class GrVkFramebuffer;
20class GrVkGpu;
21class GrVkImageView;
22class GrVkStencilAttachment;
23
24struct GrVkImageInfo;
25
26#ifdef SK_BUILD_FOR_WIN
27// Windows gives bogus warnings about inheriting asTexture/asRenderTarget via dominance.
28#pragma warning(push)
29#pragma warning(disable: 4250)
30#endif
31
32class GrVkRenderTarget: public GrRenderTarget, public virtual GrVkImage {
33public:
34    static GrVkRenderTarget* CreateNewRenderTarget(GrVkGpu*, SkBudgeted, const GrSurfaceDesc&,
35                                                   const GrVkImage::ImageDesc&);
36
37    static sk_sp<GrVkRenderTarget> MakeWrappedRenderTarget(GrVkGpu*, const GrSurfaceDesc&,
38                                                           const GrVkImageInfo*);
39
40    ~GrVkRenderTarget() override;
41
42    const GrVkFramebuffer* framebuffer() const { return fFramebuffer; }
43    const GrVkImageView* colorAttachmentView() const { return fColorAttachmentView; }
44    const GrVkResource* msaaImageResource() const {
45        if (fMSAAImage) {
46            return fMSAAImage->fResource;
47        }
48        return nullptr;
49    }
50    GrVkImage* msaaImage() { return fMSAAImage.get(); }
51    const GrVkImageView* resolveAttachmentView() const { return fResolveAttachmentView; }
52    const GrVkResource* stencilImageResource() const;
53    const GrVkImageView* stencilAttachmentView() const;
54
55    const GrVkRenderPass* simpleRenderPass() const { return fCachedSimpleRenderPass; }
56    GrVkResourceProvider::CompatibleRPHandle compatibleRenderPassHandle() const {
57        return fCompatibleRPHandle;
58    }
59
60    // override of GrRenderTarget
61    ResolveType getResolveType() const override {
62        if (this->numColorSamples() > 1) {
63            return kCanResolve_ResolveType;
64        }
65        return kAutoResolves_ResolveType;
66    }
67
68    bool canAttemptStencilAttachment() const override {
69        return true;
70    }
71
72    GrBackendObject getRenderTargetHandle() const override;
73    GrBackendRenderTarget getBackendRenderTarget() const override;
74
75    void getAttachmentsDescriptor(GrVkRenderPass::AttachmentsDescriptor* desc,
76                                  GrVkRenderPass::AttachmentFlags* flags) const;
77
78    void addResources(GrVkCommandBuffer& commandBuffer) const;
79
80protected:
81    GrVkRenderTarget(GrVkGpu* gpu,
82                     const GrSurfaceDesc& desc,
83                     const GrVkImageInfo& info,
84                     const GrVkImageInfo& msaaInfo,
85                     const GrVkImageView* colorAttachmentView,
86                     const GrVkImageView* resolveAttachmentView,
87                     GrBackendObjectOwnership);
88
89    GrVkRenderTarget(GrVkGpu* gpu,
90                     const GrSurfaceDesc& desc,
91                     const GrVkImageInfo& info,
92                     const GrVkImageView* colorAttachmentView,
93                     GrBackendObjectOwnership);
94
95    GrVkGpu* getVkGpu() const;
96
97    void onAbandon() override;
98    void onRelease() override;
99
100    // This accounts for the texture's memory and any MSAA renderbuffer's memory.
101    size_t onGpuMemorySize() const override {
102        int numColorSamples = this->numColorSamples();
103        if (numColorSamples > 1) {
104            // Add one to account for the resolved VkImage.
105            numColorSamples += 1;
106        }
107        return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
108                                      numColorSamples, GrMipMapped::kNo);
109    }
110
111    void createFramebuffer(GrVkGpu* gpu);
112
113    const GrVkImageView*       fColorAttachmentView;
114    std::unique_ptr<GrVkImage> fMSAAImage;
115    const GrVkImageView*       fResolveAttachmentView;
116
117private:
118    GrVkRenderTarget(GrVkGpu* gpu,
119                     SkBudgeted,
120                     const GrSurfaceDesc& desc,
121                     const GrVkImageInfo& info,
122                     const GrVkImageInfo& msaaInfo,
123                     const GrVkImageView* colorAttachmentView,
124                     const GrVkImageView* resolveAttachmentView,
125                     GrBackendObjectOwnership);
126
127    GrVkRenderTarget(GrVkGpu* gpu,
128                     SkBudgeted,
129                     const GrSurfaceDesc& desc,
130                     const GrVkImageInfo& info,
131                     const GrVkImageView* colorAttachmentView,
132                     GrBackendObjectOwnership);
133
134    static GrVkRenderTarget* Create(GrVkGpu*, SkBudgeted, const GrSurfaceDesc&,
135                                    const GrVkImageInfo&, GrBackendObjectOwnership);
136
137    bool completeStencilAttachment() override;
138
139    void releaseInternalObjects();
140    void abandonInternalObjects();
141
142    const GrVkFramebuffer*     fFramebuffer;
143
144    // This is a cached pointer to a simple render pass. The render target should unref it
145    // once it is done with it.
146    const GrVkRenderPass*      fCachedSimpleRenderPass;
147    // This is a handle to be used to quickly get compatible GrVkRenderPasses for this render target
148    GrVkResourceProvider::CompatibleRPHandle fCompatibleRPHandle;
149};
150
151#endif
152