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 
18 class GrVkCommandBuffer;
19 class GrVkFramebuffer;
20 class GrVkGpu;
21 class GrVkImageView;
22 class GrVkStencilAttachment;
23 
24 struct 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 
32 class GrVkRenderTarget: public GrRenderTarget, public virtual GrVkImage {
33 public:
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 
framebuffer()42     const GrVkFramebuffer* framebuffer() const { return fFramebuffer; }
colorAttachmentView()43     const GrVkImageView* colorAttachmentView() const { return fColorAttachmentView; }
msaaImageResource()44     const GrVkResource* msaaImageResource() const {
45         if (fMSAAImage) {
46             return fMSAAImage->fResource;
47         }
48         return nullptr;
49     }
msaaImage()50     GrVkImage* msaaImage() { return fMSAAImage; }
resolveAttachmentView()51     const GrVkImageView* resolveAttachmentView() const { return fResolveAttachmentView; }
52     const GrVkResource* stencilImageResource() const;
53     const GrVkImageView* stencilAttachmentView() const;
54 
simpleRenderPass()55     const GrVkRenderPass* simpleRenderPass() const { return fCachedSimpleRenderPass; }
compatibleRenderPassHandle()56     GrVkResourceProvider::CompatibleRPHandle compatibleRenderPassHandle() const {
57         return fCompatibleRPHandle;
58     }
59 
60     // override of GrRenderTarget
getResolveType()61     ResolveType getResolveType() const override {
62         if (this->numColorSamples() > 1) {
63             return kCanResolve_ResolveType;
64         }
65         return kAutoResolves_ResolveType;
66     }
67 
canAttemptStencilAttachment()68     bool canAttemptStencilAttachment() const override {
69         return true;
70     }
71 
72     GrBackendObject getRenderTargetHandle() const override;
73 
74     void getAttachmentsDescriptor(GrVkRenderPass::AttachmentsDescriptor* desc,
75                                   GrVkRenderPass::AttachmentFlags* flags) const;
76 
77     void addResources(GrVkCommandBuffer& commandBuffer) const;
78 
79 protected:
80     GrVkRenderTarget(GrVkGpu* gpu,
81                      const GrSurfaceDesc& desc,
82                      const GrVkImageInfo& info,
83                      const GrVkImageInfo& msaaInfo,
84                      const GrVkImageView* colorAttachmentView,
85                      const GrVkImageView* resolveAttachmentView,
86                      GrVkImage::Wrapped wrapped);
87 
88     GrVkRenderTarget(GrVkGpu* gpu,
89                      const GrSurfaceDesc& desc,
90                      const GrVkImageInfo& info,
91                      const GrVkImageView* colorAttachmentView,
92                      GrVkImage::Wrapped wrapped);
93 
94     GrVkGpu* getVkGpu() const;
95 
96     void onAbandon() override;
97     void onRelease() override;
98 
99     // This accounts for the texture's memory and any MSAA renderbuffer's memory.
onGpuMemorySize()100     size_t onGpuMemorySize() const override {
101         // The plus 1 is to account for the resolve texture.
102         // TODO: is this still correct?
103         return GrSurface::ComputeSize(fDesc, fDesc.fSampleCnt+1, false);
104     }
105 
106     void createFramebuffer(GrVkGpu* gpu);
107 
108     const GrVkImageView*       fColorAttachmentView;
109     GrVkImage*                 fMSAAImage;
110     const GrVkImageView*       fResolveAttachmentView;
111 
112 private:
113     GrVkRenderTarget(GrVkGpu* gpu,
114                      SkBudgeted,
115                      const GrSurfaceDesc& desc,
116                      const GrVkImageInfo& info,
117                      const GrVkImageInfo& msaaInfo,
118                      const GrVkImageView* colorAttachmentView,
119                      const GrVkImageView* resolveAttachmentView,
120                      GrVkImage::Wrapped wrapped);
121 
122     GrVkRenderTarget(GrVkGpu* gpu,
123                      SkBudgeted,
124                      const GrSurfaceDesc& desc,
125                      const GrVkImageInfo& info,
126                      const GrVkImageView* colorAttachmentView,
127                      GrVkImage::Wrapped wrapped);
128 
129     static GrVkRenderTarget* Create(GrVkGpu*, SkBudgeted, const GrSurfaceDesc&,
130                                     const GrVkImageInfo&, GrVkImage::Wrapped wrapped);
131 
132     bool completeStencilAttachment() override;
133 
134     void releaseInternalObjects();
135     void abandonInternalObjects();
136 
137     const GrVkFramebuffer*     fFramebuffer;
138 
139     // This is a cached pointer to a simple render pass. The render target should unref it
140     // once it is done with it.
141     const GrVkRenderPass*      fCachedSimpleRenderPass;
142     // This is a handle to be used to quickly get compatible GrVkRenderPasses for this render target
143     GrVkResourceProvider::CompatibleRPHandle fCompatibleRPHandle;
144 };
145 
146 #endif
147