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 "src/gpu/GrRenderTarget.h" 13 #include "src/gpu/vk/GrVkImage.h" 14 15 #include "include/gpu/vk/GrVkTypes.h" 16 #include "src/gpu/vk/GrVkRenderPass.h" 17 #include "src/gpu/vk/GrVkResourceProvider.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 int sampleCnt, 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 backendFormat()45 GrBackendFormat backendFormat() const override { return this->getBackendFormat(); } 46 framebuffer()47 const GrVkFramebuffer* framebuffer() const { return fFramebuffer; } colorAttachmentView()48 const GrVkImageView* colorAttachmentView() const { return fColorAttachmentView; } msaaImageResource()49 const GrVkResource* msaaImageResource() const { 50 if (fMSAAImage) { 51 return fMSAAImage->fResource; 52 } 53 return nullptr; 54 } msaaImage()55 GrVkImage* msaaImage() { return fMSAAImage.get(); } resolveAttachmentView()56 const GrVkImageView* resolveAttachmentView() const { return fResolveAttachmentView; } 57 const GrVkResource* stencilImageResource() const; 58 const GrVkImageView* stencilAttachmentView() const; 59 simpleRenderPass()60 const GrVkRenderPass* simpleRenderPass() const { return fCachedSimpleRenderPass; } compatibleRenderPassHandle()61 GrVkResourceProvider::CompatibleRPHandle compatibleRenderPassHandle() const { 62 SkASSERT(!this->wrapsSecondaryCommandBuffer()); 63 return fCompatibleRPHandle; 64 } externalRenderPass()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 wrapsSecondaryCommandBuffer()71 bool wrapsSecondaryCommandBuffer() const { return fSecondaryCommandBuffer != VK_NULL_HANDLE; } getExternalSecondaryCommandBuffer()72 VkCommandBuffer getExternalSecondaryCommandBuffer() const { 73 return fSecondaryCommandBuffer; 74 } 75 76 // override of GrRenderTarget getResolveType()77 ResolveType getResolveType() const override { 78 if (this->numSamples() > 1) { 79 return kCanResolve_ResolveType; 80 } 81 return kAutoResolves_ResolveType; 82 } 83 canAttemptStencilAttachment()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 int sampleCnt, 101 const GrVkImageInfo& info, 102 sk_sp<GrVkImageLayout> layout, 103 const GrVkImageInfo& msaaInfo, 104 sk_sp<GrVkImageLayout> msaaLayout, 105 const GrVkImageView* colorAttachmentView, 106 const GrVkImageView* resolveAttachmentView, 107 GrBackendObjectOwnership); 108 109 GrVkRenderTarget(GrVkGpu* gpu, 110 const GrSurfaceDesc& desc, 111 const GrVkImageInfo& info, 112 sk_sp<GrVkImageLayout> layout, 113 const GrVkImageView* colorAttachmentView, 114 GrBackendObjectOwnership); 115 116 GrVkGpu* getVkGpu() const; 117 118 void onAbandon() override; 119 void onRelease() override; 120 121 // This accounts for the texture's memory and any MSAA renderbuffer's memory. onGpuMemorySize()122 size_t onGpuMemorySize() const override { 123 int numColorSamples = this->numSamples(); 124 if (numColorSamples > 1) { 125 // Add one to account for the resolved VkImage. 126 numColorSamples += 1; 127 } 128 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 129 numColorSamples, GrMipMapped::kNo); 130 } 131 132 void createFramebuffer(GrVkGpu* gpu); 133 134 const GrVkImageView* fColorAttachmentView; 135 std::unique_ptr<GrVkImage> fMSAAImage; 136 const GrVkImageView* fResolveAttachmentView; 137 138 private: 139 GrVkRenderTarget(GrVkGpu* gpu, 140 const GrSurfaceDesc& desc, 141 int sampleCnt, 142 const GrVkImageInfo& info, 143 sk_sp<GrVkImageLayout> layout, 144 const GrVkImageInfo& msaaInfo, 145 sk_sp<GrVkImageLayout> msaaLayout, 146 const GrVkImageView* colorAttachmentView, 147 const GrVkImageView* resolveAttachmentView); 148 149 GrVkRenderTarget(GrVkGpu* gpu, 150 const GrSurfaceDesc& desc, 151 const GrVkImageInfo& info, 152 sk_sp<GrVkImageLayout> layout, 153 const GrVkImageView* colorAttachmentView); 154 155 156 GrVkRenderTarget(GrVkGpu* gpu, 157 const GrSurfaceDesc& desc, 158 const GrVkImageInfo& info, 159 sk_sp<GrVkImageLayout> layout, 160 const GrVkRenderPass* renderPass, 161 VkCommandBuffer secondaryCommandBuffer); 162 163 bool completeStencilAttachment() override; 164 165 // In Vulkan we call the release proc after we are finished with the underlying 166 // GrVkImage::Resource object (which occurs after the GPU has finished all work on it). onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper)167 void onSetRelease(sk_sp<GrRefCntedCallback> releaseHelper) override { 168 // Forward the release proc on to GrVkImage 169 this->setResourceRelease(std::move(releaseHelper)); 170 } 171 172 void releaseInternalObjects(); 173 void abandonInternalObjects(); 174 175 const GrVkFramebuffer* fFramebuffer; 176 177 // This is a cached pointer to a simple render pass. The render target should unref it 178 // once it is done with it. 179 const GrVkRenderPass* fCachedSimpleRenderPass; 180 // This is a handle to be used to quickly get compatible GrVkRenderPasses for this render target 181 GrVkResourceProvider::CompatibleRPHandle fCompatibleRPHandle; 182 183 // If this render target wraps an external VkCommandBuffer, then this handle will be that 184 // VkCommandBuffer and not VK_NULL_HANDLE. In this case the render target will not be backed by 185 // an actual VkImage and will thus be limited in terms of what it can be used for. 186 VkCommandBuffer fSecondaryCommandBuffer = VK_NULL_HANDLE; 187 }; 188 189 #endif 190