blob: ec141346381a6754b50c31bfd26744f9ad59f36a [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
2* Copyright 2016 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkFramebuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -05009
Greg Daniel38517c22021-03-29 16:01:19 -040010#include "src/gpu/vk/GrVkAttachment.h"
Greg Danielde4bbdb2021-04-13 14:23:23 -040011#include "src/gpu/vk/GrVkCommandBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/vk/GrVkGpu.h"
13#include "src/gpu/vk/GrVkImageView.h"
14#include "src/gpu/vk/GrVkRenderPass.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050015
Greg Daniela92d7872021-04-07 11:08:36 -040016GrVkFramebuffer* GrVkFramebuffer::Create(
17 GrVkGpu* gpu,
18 int width, int height,
19 const GrVkRenderPass* renderPass,
Greg Danielde4bbdb2021-04-13 14:23:23 -040020 GrVkAttachment* colorAttachment,
21 GrVkAttachment* resolveAttachment,
22 GrVkAttachment* stencilAttachment,
Greg Daniela92d7872021-04-07 11:08:36 -040023 GrVkResourceProvider::CompatibleRPHandle compatibleRenderPassHandle) {
Greg Daniel164a9f02016-02-22 09:56:40 -050024 // At the very least we need a renderPass and a colorAttachment
25 SkASSERT(renderPass);
26 SkASSERT(colorAttachment);
halcanary9d524f22016-03-29 09:03:52 -070027
Greg Daniel164a9f02016-02-22 09:56:40 -050028 VkImageView attachments[3];
Greg Daniel38517c22021-03-29 16:01:19 -040029 attachments[0] = colorAttachment->framebufferView()->imageView();
Greg Daniel164a9f02016-02-22 09:56:40 -050030 int numAttachments = 1;
Greg Daniel7acddf52020-12-16 15:15:51 -050031 if (resolveAttachment) {
Greg Daniel38517c22021-03-29 16:01:19 -040032 attachments[numAttachments++] = resolveAttachment->framebufferView()->imageView();
Greg Daniel7acddf52020-12-16 15:15:51 -050033 }
Greg Daniel164a9f02016-02-22 09:56:40 -050034 if (stencilAttachment) {
Greg Daniel38517c22021-03-29 16:01:19 -040035 attachments[numAttachments++] = stencilAttachment->framebufferView()->imageView();
Greg Daniel164a9f02016-02-22 09:56:40 -050036 }
halcanary9d524f22016-03-29 09:03:52 -070037
Greg Daniel164a9f02016-02-22 09:56:40 -050038 VkFramebufferCreateInfo createInfo;
39 memset(&createInfo, 0, sizeof(VkFramebufferCreateInfo));
40 createInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
41 createInfo.pNext = nullptr;
42 createInfo.flags = 0;
43 createInfo.renderPass = renderPass->vkRenderPass();
44 createInfo.attachmentCount = numAttachments;
45 createInfo.pAttachments = attachments;
46 createInfo.width = width;
47 createInfo.height = height;
48 createInfo.layers = 1;
halcanary9d524f22016-03-29 09:03:52 -070049
Greg Daniel164a9f02016-02-22 09:56:40 -050050 VkFramebuffer framebuffer;
Greg Danielfa3adf72019-11-07 09:53:41 -050051 VkResult err;
52 GR_VK_CALL_RESULT(gpu, err, CreateFramebuffer(gpu->device(), &createInfo, nullptr,
53 &framebuffer));
Greg Daniel164a9f02016-02-22 09:56:40 -050054 if (err) {
55 return nullptr;
56 }
57
Greg Daniel38517c22021-03-29 16:01:19 -040058 return new GrVkFramebuffer(gpu, framebuffer, sk_ref_sp(colorAttachment),
Greg Daniela92d7872021-04-07 11:08:36 -040059 sk_ref_sp(resolveAttachment), sk_ref_sp(stencilAttachment),
60 compatibleRenderPassHandle);
Greg Daniel164a9f02016-02-22 09:56:40 -050061}
62
Greg Danielde4bbdb2021-04-13 14:23:23 -040063GrVkFramebuffer::GrVkFramebuffer(const GrVkGpu* gpu,
64 VkFramebuffer framebuffer,
65 sk_sp<GrVkAttachment> colorAttachment,
66 sk_sp<GrVkAttachment> resolveAttachment,
67 sk_sp<GrVkAttachment> stencilAttachment,
68 GrVkResourceProvider::CompatibleRPHandle compatibleRPHandle)
69 : GrVkManagedResource(gpu)
70 , fFramebuffer(framebuffer)
71 , fColorAttachment(std::move(colorAttachment))
72 , fResolveAttachment(std::move(resolveAttachment))
73 , fStencilAttachment(std::move(stencilAttachment))
74 , fCompatibleRenderPassHandle(compatibleRPHandle) {}
75
76GrVkFramebuffer::GrVkFramebuffer(const GrVkGpu* gpu,
77 sk_sp<GrVkAttachment> colorAttachment,
78 sk_sp<const GrVkRenderPass> renderPass,
79 std::unique_ptr<GrVkSecondaryCommandBuffer> externalCommandBuffer)
80 : GrVkManagedResource(gpu)
81 , fColorAttachment(std::move(colorAttachment))
82 , fExternalRenderPass(std::move(renderPass))
83 , fExternalCommandBuffer(std::move(externalCommandBuffer)) {}
84
Greg Daniel38517c22021-03-29 16:01:19 -040085GrVkFramebuffer::~GrVkFramebuffer() {}
86
Jim Van Verth5082df12020-03-11 16:14:51 -040087void GrVkFramebuffer::freeGPUData() const {
Greg Danielde4bbdb2021-04-13 14:23:23 -040088 SkASSERT(this->isExternal() || fFramebuffer != VK_NULL_HANDLE);
89 if (!this->isExternal()) {
90 GR_VK_CALL(fGpu->vkInterface(), DestroyFramebuffer(fGpu->device(), fFramebuffer, nullptr));
91 }
92
93 // TODO: having freeGPUData virtual on GrManagedResource be const seems like a bad restriction
94 // since we are changing the internal objects of these classes when it is called. We should go
95 // back a revisit how much of a headache it would be to make this function non-const
96 GrVkFramebuffer* nonConstThis = const_cast<GrVkFramebuffer*>(this);
97 nonConstThis->releaseResources();
98}
99
100void GrVkFramebuffer::releaseResources() {
101 if (fExternalCommandBuffer) {
102 fExternalCommandBuffer->releaseResources();
103 fExternalCommandBuffer.reset();
104 }
105}
106
107void GrVkFramebuffer::returnExternalGrSecondaryCommandBuffer(
108 std::unique_ptr<GrVkSecondaryCommandBuffer> cmdBuffer) {
109 SkASSERT(!fExternalCommandBuffer);
110 fExternalCommandBuffer = std::move(cmdBuffer);
111}
112
113std::unique_ptr<GrVkSecondaryCommandBuffer> GrVkFramebuffer::externalCommandBuffer() {
114 SkASSERT(fExternalCommandBuffer);
115 return std::move(fExternalCommandBuffer);
Greg Daniel164a9f02016-02-22 09:56:40 -0500116}