blob: 620df7dbef9c14b8a31b2960a2bd1eecde0dea74 [file] [log] [blame]
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkCommandPool.h"
Ethan Nicholas8e265a72018-12-12 16:22:40 -05009
Robert Phillips06273bc2021-08-11 15:43:50 -040010#include "src/core/SkTraceEvent.h"
Adlai Hollera0693042020-10-14 11:23:11 -040011#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/vk/GrVkCommandBuffer.h"
13#include "src/gpu/vk/GrVkGpu.h"
Ethan Nicholas8e265a72018-12-12 16:22:40 -050014
Greg Daniele643da62019-11-05 12:36:42 -050015GrVkCommandPool* GrVkCommandPool::Create(GrVkGpu* gpu) {
Greg Daniel6253b572020-09-11 16:39:28 -040016 VkCommandPoolCreateFlags cmdPoolCreateFlags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Greg Daniel8daf3b72019-07-30 09:57:26 -040017 if (gpu->protectedContext()) {
18 cmdPoolCreateFlags |= VK_COMMAND_POOL_CREATE_PROTECTED_BIT;
19 }
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040020
Greg Daniel8daf3b72019-07-30 09:57:26 -040021 const VkCommandPoolCreateInfo cmdPoolInfo = {
22 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType
23 nullptr, // pNext
24 cmdPoolCreateFlags, // CmdPoolCreateFlags
25 gpu->queueIndex(), // queueFamilyIndex
26 };
Greg Daniel9b63dc82019-11-06 09:21:55 -050027 VkResult result;
Greg Daniel8daf3b72019-07-30 09:57:26 -040028 VkCommandPool pool;
Greg Daniel9b63dc82019-11-06 09:21:55 -050029 GR_VK_CALL_RESULT(gpu, result, CreateCommandPool(gpu->device(), &cmdPoolInfo, nullptr, &pool));
30 if (result != VK_SUCCESS) {
31 return nullptr;
32 }
Greg Daniel315c8dc2019-11-26 15:41:27 -050033
34 GrVkPrimaryCommandBuffer* primaryCmdBuffer = GrVkPrimaryCommandBuffer::Create(gpu, pool);
35 if (!primaryCmdBuffer) {
36 GR_VK_CALL(gpu->vkInterface(), DestroyCommandPool(gpu->device(), pool, nullptr));
37 return nullptr;
38 }
39
40 return new GrVkCommandPool(gpu, pool, primaryCmdBuffer);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050041}
42
Greg Daniel315c8dc2019-11-26 15:41:27 -050043GrVkCommandPool::GrVkCommandPool(GrVkGpu* gpu, VkCommandPool commandPool,
44 GrVkPrimaryCommandBuffer* primaryCmdBuffer)
Jim Van Verth5082df12020-03-11 16:14:51 -040045 : GrVkManagedResource(gpu)
46 , fCommandPool(commandPool)
Greg Daniel43833b02020-07-14 09:21:48 -040047 , fPrimaryCommandBuffer(primaryCmdBuffer)
48 , fMaxCachedSecondaryCommandBuffers(
49 gpu->vkCaps().maxPerPoolCachedSecondaryCommandBuffers()) {
Ethan Nicholas8e265a72018-12-12 16:22:40 -050050}
51
Greg Daniel8daf3b72019-07-30 09:57:26 -040052std::unique_ptr<GrVkSecondaryCommandBuffer> GrVkCommandPool::findOrCreateSecondaryCommandBuffer(
53 GrVkGpu* gpu) {
54 std::unique_ptr<GrVkSecondaryCommandBuffer> result;
Ethan Nicholas8e265a72018-12-12 16:22:40 -050055 if (fAvailableSecondaryBuffers.count()) {
Greg Daniel8daf3b72019-07-30 09:57:26 -040056 result = std::move(fAvailableSecondaryBuffers.back());
Ethan Nicholas8e265a72018-12-12 16:22:40 -050057 fAvailableSecondaryBuffers.pop_back();
Greg Daniel8daf3b72019-07-30 09:57:26 -040058 } else{
59 result.reset(GrVkSecondaryCommandBuffer::Create(gpu, this));
Ethan Nicholas8e265a72018-12-12 16:22:40 -050060 }
Greg Daniel8daf3b72019-07-30 09:57:26 -040061 return result;
Ethan Nicholas8e265a72018-12-12 16:22:40 -050062}
63
64void GrVkCommandPool::recycleSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* buffer) {
Greg Daniel8daf3b72019-07-30 09:57:26 -040065 std::unique_ptr<GrVkSecondaryCommandBuffer> scb(buffer);
Greg Daniel43833b02020-07-14 09:21:48 -040066 if (fAvailableSecondaryBuffers.count() < fMaxCachedSecondaryCommandBuffers) {
67 fAvailableSecondaryBuffers.push_back(std::move(scb));
Greg Daniel0927dd62020-07-20 15:13:29 -040068 } else {
69 VkCommandBuffer vkBuffer = buffer->vkCommandBuffer();
70 GR_VK_CALL(fGpu->vkInterface(),
71 FreeCommandBuffers(fGpu->device(), fCommandPool, 1, &vkBuffer));
Greg Daniel43833b02020-07-14 09:21:48 -040072 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -050073}
74
75void GrVkCommandPool::close() {
76 fOpen = false;
77}
78
79void GrVkCommandPool::reset(GrVkGpu* gpu) {
80 SkASSERT(!fOpen);
81 fOpen = true;
Greg Daniel95f0b162019-11-11 13:42:30 -050082 // We can't use the normal result macro calls here because we may call reset on a different
83 // thread and we can't be modifying the lost state on the GrVkGpu. We just call
84 // vkResetCommandPool and assume the "next" vulkan call will catch the lost device.
85 SkDEBUGCODE(VkResult result = )GR_VK_CALL(gpu->vkInterface(),
86 ResetCommandPool(gpu->device(), fCommandPool, 0));
87 SkASSERT(result == VK_SUCCESS || result == VK_ERROR_DEVICE_LOST);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050088}
89
Jim Van Verth5082df12020-03-11 16:14:51 -040090void GrVkCommandPool::releaseResources() {
Brian Salomone39526b2019-06-24 16:35:53 -040091 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050092 SkASSERT(!fOpen);
Jim Van Verth5082df12020-03-11 16:14:51 -040093 fPrimaryCommandBuffer->releaseResources();
Greg Daniel7a32f1b2019-11-26 16:18:01 -050094 fPrimaryCommandBuffer->recycleSecondaryCommandBuffers(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050095}
96
Jim Van Verth5082df12020-03-11 16:14:51 -040097void GrVkCommandPool::freeGPUData() const {
Jim Van Verth3e192162020-03-10 16:23:16 -040098 // TODO: having freeGPUData virtual on GrManagedResource be const seems like a bad restriction since
Greg Daniel0addbdf2019-11-25 15:03:58 -050099 // we are changing the internal objects of these classes when it is called. We should go back a
100 // revisit how much of a headache it would be to make this function non-const
101 GrVkCommandPool* nonConstThis = const_cast<GrVkCommandPool*>(this);
102 nonConstThis->close();
Jim Van Verth5082df12020-03-11 16:14:51 -0400103 nonConstThis->releaseResources();
104 fPrimaryCommandBuffer->freeGPUData(fGpu, fCommandPool);
Greg Daniel8daf3b72019-07-30 09:57:26 -0400105 for (const auto& buffer : fAvailableSecondaryBuffers) {
Jim Van Verth5082df12020-03-11 16:14:51 -0400106 buffer->freeGPUData(fGpu, fCommandPool);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500107 }
108 if (fCommandPool != VK_NULL_HANDLE) {
Jim Van Verth5082df12020-03-11 16:14:51 -0400109 GR_VK_CALL(fGpu->vkInterface(),
110 DestroyCommandPool(fGpu->device(), fCommandPool, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500111 }
112}