Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkCommandPool.h" |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrContextPriv.h" |
| 11 | #include "src/gpu/vk/GrVkCommandBuffer.h" |
| 12 | #include "src/gpu/vk/GrVkGpu.h" |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 13 | |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame^] | 14 | GrVkCommandPool* GrVkCommandPool::Create(GrVkGpu* gpu) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 15 | VkCommandPoolCreateFlags cmdPoolCreateFlags = |
| 16 | VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | |
| 17 | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; |
| 18 | if (gpu->protectedContext()) { |
| 19 | cmdPoolCreateFlags |= VK_COMMAND_POOL_CREATE_PROTECTED_BIT; |
| 20 | } |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 21 | |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 22 | const VkCommandPoolCreateInfo cmdPoolInfo = { |
| 23 | VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType |
| 24 | nullptr, // pNext |
| 25 | cmdPoolCreateFlags, // CmdPoolCreateFlags |
| 26 | gpu->queueIndex(), // queueFamilyIndex |
| 27 | }; |
| 28 | VkCommandPool pool; |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame^] | 29 | GR_VK_CALL_ERRCHECK(gpu, CreateCommandPool(gpu->device(), &cmdPoolInfo, nullptr, &pool)); |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 30 | return new GrVkCommandPool(gpu, pool); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | GrVkCommandPool::GrVkCommandPool(const GrVkGpu* gpu, VkCommandPool commandPool) |
| 34 | : fCommandPool(commandPool) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 35 | fPrimaryCommandBuffer.reset(GrVkPrimaryCommandBuffer::Create(gpu, this)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 38 | std::unique_ptr<GrVkSecondaryCommandBuffer> GrVkCommandPool::findOrCreateSecondaryCommandBuffer( |
| 39 | GrVkGpu* gpu) { |
| 40 | std::unique_ptr<GrVkSecondaryCommandBuffer> result; |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 41 | if (fAvailableSecondaryBuffers.count()) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 42 | result = std::move(fAvailableSecondaryBuffers.back()); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 43 | fAvailableSecondaryBuffers.pop_back(); |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 44 | } else{ |
| 45 | result.reset(GrVkSecondaryCommandBuffer::Create(gpu, this)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 46 | } |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 47 | return result; |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void GrVkCommandPool::recycleSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* buffer) { |
| 51 | SkASSERT(buffer->commandPool() == this); |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 52 | std::unique_ptr<GrVkSecondaryCommandBuffer> scb(buffer); |
| 53 | fAvailableSecondaryBuffers.push_back(std::move(scb)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void GrVkCommandPool::close() { |
| 57 | fOpen = false; |
| 58 | } |
| 59 | |
| 60 | void GrVkCommandPool::reset(GrVkGpu* gpu) { |
| 61 | SkASSERT(!fOpen); |
| 62 | fOpen = true; |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 63 | fPrimaryCommandBuffer->recycleSecondaryCommandBuffers(gpu); |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame^] | 64 | GR_VK_CALL_ERRCHECK(gpu, ResetCommandPool(gpu->device(), fCommandPool, 0)); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void GrVkCommandPool::releaseResources(GrVkGpu* gpu) { |
Brian Salomon | e39526b | 2019-06-24 16:35:53 -0400 | [diff] [blame] | 68 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 69 | SkASSERT(!fOpen); |
| 70 | fPrimaryCommandBuffer->releaseResources(gpu); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void GrVkCommandPool::abandonGPUData() const { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 74 | fPrimaryCommandBuffer->abandonGPUData(); |
| 75 | for (const auto& buffer : fAvailableSecondaryBuffers) { |
| 76 | buffer->abandonGPUData(); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | void GrVkCommandPool::freeGPUData(GrVkGpu* gpu) const { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 81 | fPrimaryCommandBuffer->freeGPUData(gpu); |
| 82 | for (const auto& buffer : fAvailableSecondaryBuffers) { |
| 83 | buffer->freeGPUData(gpu); |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 84 | } |
| 85 | if (fCommandPool != VK_NULL_HANDLE) { |
| 86 | GR_VK_CALL(gpu->vkInterface(), |
| 87 | DestroyCommandPool(gpu->device(), fCommandPool, nullptr)); |
| 88 | } |
| 89 | } |