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 
17 class GrVkCommandBuffer;
18 class GrVkFramebuffer;
19 class GrVkGpu;
20 class GrVkImageView;
21 class GrVkStencilAttachment;
22 
23 #ifdef SK_BUILD_FOR_WIN
24 // Windows gives bogus warnings about inheriting asTexture/asRenderTarget via dominance.
25 #pragma warning(push)
26 #pragma warning(disable: 4250)
27 #endif
28 
29 class GrVkRenderTarget: public GrRenderTarget, public virtual GrVkImage {
30 public:
31     static GrVkRenderTarget* CreateNewRenderTarget(GrVkGpu*, const GrSurfaceDesc&,
32                                                    GrGpuResource::LifeCycle,
33                                                    const GrVkImage::ImageDesc&);
34 
35     static GrVkRenderTarget* CreateWrappedRenderTarget(GrVkGpu*, const GrSurfaceDesc&,
36                                                        GrGpuResource::LifeCycle,
37                                                        const GrVkImage::Resource* resource);
38 
39     ~GrVkRenderTarget() override;
40 
framebuffer()41     const GrVkFramebuffer* framebuffer() const { return fFramebuffer; }
colorAttachmentView()42     const GrVkImageView* colorAttachmentView() const { return fColorAttachmentView; }
msaaImageResource()43     const GrVkImage::Resource* msaaImageResource() const { return fMSAAImageResource; }
resolveAttachmentView()44     const GrVkImageView* resolveAttachmentView() const { return fResolveAttachmentView; }
45     const GrVkImage::Resource* stencilImageResource() const;
46     const GrVkImageView* stencilAttachmentView() const;
47 
simpleRenderPass()48     const GrVkRenderPass* simpleRenderPass() const { return fCachedSimpleRenderPass; }
49 
50     // override of GrRenderTarget
getResolveType()51     ResolveType getResolveType() const override {
52         return kCanResolve_ResolveType;
53     }
54 
canAttemptStencilAttachment()55     bool canAttemptStencilAttachment() const override {
56         return true;
57     }
58 
59     GrBackendObject getRenderTargetHandle() const override;
60 
61     // Returns the total number of attachments
62     void getAttachmentsDescriptor(GrVkRenderPass::AttachmentsDescriptor* desc,
63                                   GrVkRenderPass::AttachmentFlags* flags) const;
64 
65     void addResources(GrVkCommandBuffer& commandBuffer) const;
66 
67 protected:
68     enum Derived { kDerived };
69 
70     GrVkRenderTarget(GrVkGpu* gpu,
71                      const GrSurfaceDesc& desc,
72                      GrGpuResource::LifeCycle,
73                      const GrVkImage::Resource* imageResource,
74                      const GrVkImage::Resource* msaaImageResource,
75                      const GrVkImageView* colorAttachmentView,
76                      const GrVkImageView* resolveAttachmentView);
77 
78     GrVkRenderTarget(GrVkGpu* gpu,
79                      const GrSurfaceDesc& desc,
80                      GrGpuResource::LifeCycle,
81                      const GrVkImage::Resource* imageResource,
82                      const GrVkImage::Resource* msaaImageResource,
83                      const GrVkImageView* colorAttachmentView,
84                      const GrVkImageView* resolveAttachmentView,
85                      Derived);
86 
87     GrVkRenderTarget(GrVkGpu* gpu,
88                      const GrSurfaceDesc& desc,
89                      GrGpuResource::LifeCycle,
90                      const GrVkImage::Resource* imageResource,
91                      const GrVkImageView* colorAttachmentView);
92 
93     GrVkRenderTarget(GrVkGpu* gpu,
94                      const GrSurfaceDesc& desc,
95                      GrGpuResource::LifeCycle,
96                      const GrVkImage::Resource* imageResource,
97                      const GrVkImageView* colorAttachmentView,
98                      Derived);
99 
100     static GrVkRenderTarget* Create(GrVkGpu*, const GrSurfaceDesc&,
101                                     GrGpuResource::LifeCycle,
102                                     const GrVkImage::Resource* imageResource);
103 
104     GrVkGpu* getVkGpu() const;
105 
106     void onAbandon() override;
107     void onRelease() override;
108 
109     // This accounts for the texture's memory and any MSAA renderbuffer's memory.
onGpuMemorySize()110     size_t onGpuMemorySize() const override {
111         SkASSERT(kUnknown_GrPixelConfig != fDesc.fConfig);
112         SkASSERT(!GrPixelConfigIsCompressed(fDesc.fConfig));
113         size_t colorBytes = GrBytesPerPixel(fDesc.fConfig);
114         SkASSERT(colorBytes > 0);
115         return fColorValuesPerPixel * fDesc.fWidth * fDesc.fHeight * colorBytes;
116     }
117 
118 private:
119     bool completeStencilAttachment() override;
120 
121     void createFramebuffer(GrVkGpu* gpu);
122 
123     void releaseInternalObjects();
124     void abandonInternalObjects();
125 
126     const GrVkFramebuffer*     fFramebuffer;
127     const GrVkImageView*       fColorAttachmentView;
128     const GrVkImage::Resource* fMSAAImageResource;
129     const GrVkImageView*       fResolveAttachmentView;
130     int                        fColorValuesPerPixel;
131 
132     // This is a cached pointer to a simple render pass. The render target should unref it
133     // once it is done with it.
134     const GrVkRenderPass*      fCachedSimpleRenderPass;
135 };
136 
137 #endif
138