Home | History | Annotate | Download | only in vk
      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 #include "vk/GrVkTypes.h"
     18 
     19 class GrVkCommandBuffer;
     20 class GrVkFramebuffer;
     21 class GrVkGpu;
     22 class GrVkImageView;
     23 class GrVkSecondaryCommandBuffer;
     24 class GrVkStencilAttachment;
     25 
     26 struct GrVkImageInfo;
     27 
     28 #ifdef SK_BUILD_FOR_WIN
     29 // Windows gives bogus warnings about inheriting asTexture/asRenderTarget via dominance.
     30 #pragma warning(push)
     31 #pragma warning(disable: 4250)
     32 #endif
     33 
     34 class GrVkRenderTarget: public GrRenderTarget, public virtual GrVkImage {
     35 public:
     36     static sk_sp<GrVkRenderTarget> MakeWrappedRenderTarget(GrVkGpu*, const GrSurfaceDesc&,
     37                                                            const GrVkImageInfo&,
     38                                                            sk_sp<GrVkImageLayout>);
     39 
     40     static sk_sp<GrVkRenderTarget> MakeSecondaryCBRenderTarget(GrVkGpu*, const GrSurfaceDesc&,
     41                                                                const GrVkDrawableInfo& vkInfo);
     42 
     43     ~GrVkRenderTarget() override;
     44 
     45     GrBackendFormat backendFormat() const override { return this->getBackendFormat(); }
     46 
     47     const GrVkFramebuffer* framebuffer() const { return fFramebuffer; }
     48     const GrVkImageView* colorAttachmentView() const { return fColorAttachmentView; }
     49     const GrVkResource* msaaImageResource() const {
     50         if (fMSAAImage) {
     51             return fMSAAImage->fResource;
     52         }
     53         return nullptr;
     54     }
     55     GrVkImage* msaaImage() { return fMSAAImage.get(); }
     56     const GrVkImageView* resolveAttachmentView() const { return fResolveAttachmentView; }
     57     const GrVkResource* stencilImageResource() const;
     58     const GrVkImageView* stencilAttachmentView() const;
     59 
     60     const GrVkRenderPass* simpleRenderPass() const { return fCachedSimpleRenderPass; }
     61     GrVkResourceProvider::CompatibleRPHandle compatibleRenderPassHandle() const {
     62         SkASSERT(!this->wrapsSecondaryCommandBuffer());
     63         return fCompatibleRPHandle;
     64     }
     65     const GrVkRenderPass* externalRenderPass() const {
     66         SkASSERT(this->wrapsSecondaryCommandBuffer());
     67         // We use the cached simple render pass to hold the external render pass.
     68         return fCachedSimpleRenderPass;
     69     }
     70 
     71     bool wrapsSecondaryCommandBuffer() const { return fSecondaryCommandBuffer != nullptr; }
     72     GrVkSecondaryCommandBuffer* getExternalSecondaryCommandBuffer() const {
     73         return fSecondaryCommandBuffer;
     74     }
     75 
     76     // override of GrRenderTarget
     77     ResolveType getResolveType() const override {
     78         if (this->numColorSamples() > 1) {
     79             return kCanResolve_ResolveType;
     80         }
     81         return kAutoResolves_ResolveType;
     82     }
     83 
     84     bool canAttemptStencilAttachment() const override {
     85         // We don't know the status of the stencil attachment for wrapped external secondary command
     86         // buffers so we just assume we don't have one.
     87         return !this->wrapsSecondaryCommandBuffer();
     88     }
     89 
     90     GrBackendRenderTarget getBackendRenderTarget() const override;
     91 
     92     void getAttachmentsDescriptor(GrVkRenderPass::AttachmentsDescriptor* desc,
     93                                   GrVkRenderPass::AttachmentFlags* flags) const;
     94 
     95     void addResources(GrVkCommandBuffer& commandBuffer) const;
     96 
     97 protected:
     98     GrVkRenderTarget(GrVkGpu* gpu,
     99                      const GrSurfaceDesc& desc,
    100                      const GrVkImageInfo& info,
    101                      sk_sp<GrVkImageLayout> layout,
    102                      const GrVkImageInfo& msaaInfo,
    103                      sk_sp<GrVkImageLayout> msaaLayout,
    104                      const GrVkImageView* colorAttachmentView,
    105                      const GrVkImageView* resolveAttachmentView,
    106                      GrBackendObjectOwnership);
    107 
    108     GrVkRenderTarget(GrVkGpu* gpu,
    109                      const GrSurfaceDesc& desc,
    110                      const GrVkImageInfo& info,
    111                      sk_sp<GrVkImageLayout> layout,
    112                      const GrVkImageView* colorAttachmentView,
    113                      GrBackendObjectOwnership);
    114 
    115     GrVkGpu* getVkGpu() const;
    116 
    117     void onAbandon() override;
    118     void onRelease() override;
    119 
    120     // This accounts for the texture's memory and any MSAA renderbuffer's memory.
    121     size_t onGpuMemorySize() const override {
    122         int numColorSamples = this->numColorSamples();
    123         if (numColorSamples > 1) {
    124             // Add one to account for the resolved VkImage.
    125             numColorSamples += 1;
    126         }
    127         return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
    128                                       numColorSamples, GrMipMapped::kNo);
    129     }
    130 
    131     void createFramebuffer(GrVkGpu* gpu);
    132 
    133     const GrVkImageView*       fColorAttachmentView;
    134     std::unique_ptr<GrVkImage> fMSAAImage;
    135     const GrVkImageView*       fResolveAttachmentView;
    136 
    137 private:
    138     GrVkRenderTarget(GrVkGpu* gpu,
    139                      const GrSurfaceDesc& desc,
    140                      const GrVkImageInfo& info,
    141                      sk_sp<GrVkImageLayout> layout,
    142                      const GrVkImageInfo& msaaInfo,
    143                      sk_sp<GrVkImageLayout> msaaLayout,
    144                      const GrVkImageView* colorAttachmentView,
    145                      const GrVkImageView* resolveAttachmentView);
    146 
    147     GrVkRenderTarget(GrVkGpu* gpu,
    148                      const GrSurfaceDesc& desc,
    149                      const GrVkImageInfo& info,
    150                      sk_sp<GrVkImageLayout> layout,
    151                      const GrVkImageView* colorAttachmentView);
    152 
    153 
    154     GrVkRenderTarget(GrVkGpu* gpu,
    155                      const GrSurfaceDesc& desc,
    156                      const GrVkImageInfo& info,
    157                      sk_sp<GrVkImageLayout> layout,
    158                      const GrVkRenderPass* renderPass,
    159                      GrVkSecondaryCommandBuffer* secondaryCommandBuffer);
    160 
    161     bool completeStencilAttachment() override;
    162 
    163     // In Vulkan we call the release proc after we are finished with the underlying
    164     // GrVkImage::Resource object (which occurs after the GPU has finished all work on it).
    165     void onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper) override {
    166         // Forward the release proc on to GrVkImage
    167         this->setResourceRelease(std::move(releaseHelper));
    168     }
    169 
    170     void releaseInternalObjects();
    171     void abandonInternalObjects();
    172 
    173     const GrVkFramebuffer*     fFramebuffer;
    174 
    175     // This is a cached pointer to a simple render pass. The render target should unref it
    176     // once it is done with it.
    177     const GrVkRenderPass*      fCachedSimpleRenderPass;
    178     // This is a handle to be used to quickly get compatible GrVkRenderPasses for this render target
    179     GrVkResourceProvider::CompatibleRPHandle fCompatibleRPHandle;
    180 
    181     // If this render target wraps an external VkCommandBuffer, then this pointer will be non-null
    182     // and will point to the GrVk object that, in turn, wraps the external VkCommandBuffer. In this
    183     // case the render target will not be backed by an actual VkImage and will thus be limited in
    184     // terms of what it can be used for.
    185     GrVkSecondaryCommandBuffer* fSecondaryCommandBuffer = nullptr;
    186 };
    187 
    188 #endif
    189