blob: 90a3af6a7b9d32d4cf4001d5bf174f9ed54007d6 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrContextPriv.h"
11#include "src/gpu/vk/GrVkCommandBuffer.h"
12#include "src/gpu/vk/GrVkGpu.h"
Ethan Nicholas8e265a72018-12-12 16:22:40 -050013
Greg Daniele643da62019-11-05 12:36:42 -050014GrVkCommandPool* GrVkCommandPool::Create(GrVkGpu* gpu) {
Greg Daniel8daf3b72019-07-30 09:57:26 -040015 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 Uysaler23ca4e72019-06-24 10:53:09 -040021
Greg Daniel8daf3b72019-07-30 09:57:26 -040022 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 Daniele643da62019-11-05 12:36:42 -050029 GR_VK_CALL_ERRCHECK(gpu, CreateCommandPool(gpu->device(), &cmdPoolInfo, nullptr, &pool));
Greg Daniel8daf3b72019-07-30 09:57:26 -040030 return new GrVkCommandPool(gpu, pool);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050031}
32
33GrVkCommandPool::GrVkCommandPool(const GrVkGpu* gpu, VkCommandPool commandPool)
34 : fCommandPool(commandPool) {
Greg Daniel8daf3b72019-07-30 09:57:26 -040035 fPrimaryCommandBuffer.reset(GrVkPrimaryCommandBuffer::Create(gpu, this));
Ethan Nicholas8e265a72018-12-12 16:22:40 -050036}
37
Greg Daniel8daf3b72019-07-30 09:57:26 -040038std::unique_ptr<GrVkSecondaryCommandBuffer> GrVkCommandPool::findOrCreateSecondaryCommandBuffer(
39 GrVkGpu* gpu) {
40 std::unique_ptr<GrVkSecondaryCommandBuffer> result;
Ethan Nicholas8e265a72018-12-12 16:22:40 -050041 if (fAvailableSecondaryBuffers.count()) {
Greg Daniel8daf3b72019-07-30 09:57:26 -040042 result = std::move(fAvailableSecondaryBuffers.back());
Ethan Nicholas8e265a72018-12-12 16:22:40 -050043 fAvailableSecondaryBuffers.pop_back();
Greg Daniel8daf3b72019-07-30 09:57:26 -040044 } else{
45 result.reset(GrVkSecondaryCommandBuffer::Create(gpu, this));
Ethan Nicholas8e265a72018-12-12 16:22:40 -050046 }
Greg Daniel8daf3b72019-07-30 09:57:26 -040047 return result;
Ethan Nicholas8e265a72018-12-12 16:22:40 -050048}
49
50void GrVkCommandPool::recycleSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* buffer) {
51 SkASSERT(buffer->commandPool() == this);
Greg Daniel8daf3b72019-07-30 09:57:26 -040052 std::unique_ptr<GrVkSecondaryCommandBuffer> scb(buffer);
53 fAvailableSecondaryBuffers.push_back(std::move(scb));
Ethan Nicholas8e265a72018-12-12 16:22:40 -050054}
55
56void GrVkCommandPool::close() {
57 fOpen = false;
58}
59
60void GrVkCommandPool::reset(GrVkGpu* gpu) {
61 SkASSERT(!fOpen);
62 fOpen = true;
Greg Daniel8daf3b72019-07-30 09:57:26 -040063 fPrimaryCommandBuffer->recycleSecondaryCommandBuffers(gpu);
Greg Daniele643da62019-11-05 12:36:42 -050064 GR_VK_CALL_ERRCHECK(gpu, ResetCommandPool(gpu->device(), fCommandPool, 0));
Ethan Nicholas8e265a72018-12-12 16:22:40 -050065}
66
67void GrVkCommandPool::releaseResources(GrVkGpu* gpu) {
Brian Salomone39526b2019-06-24 16:35:53 -040068 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050069 SkASSERT(!fOpen);
70 fPrimaryCommandBuffer->releaseResources(gpu);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050071}
72
73void GrVkCommandPool::abandonGPUData() const {
Greg Daniel8daf3b72019-07-30 09:57:26 -040074 fPrimaryCommandBuffer->abandonGPUData();
75 for (const auto& buffer : fAvailableSecondaryBuffers) {
76 buffer->abandonGPUData();
Ethan Nicholas8e265a72018-12-12 16:22:40 -050077 }
78}
79
80void GrVkCommandPool::freeGPUData(GrVkGpu* gpu) const {
Greg Daniel8daf3b72019-07-30 09:57:26 -040081 fPrimaryCommandBuffer->freeGPUData(gpu);
82 for (const auto& buffer : fAvailableSecondaryBuffers) {
83 buffer->freeGPUData(gpu);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050084 }
85 if (fCommandPool != VK_NULL_HANDLE) {
86 GR_VK_CALL(gpu->vkInterface(),
87 DestroyCommandPool(gpu->device(), fCommandPool, nullptr));
88 }
89}