blob: 19b99f454d26a5c8719cabfd1db6e180d3b4234f [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
14GrVkCommandPool* GrVkCommandPool::Create(const GrVkGpu* gpu) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -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 }
21
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;
29 GR_VK_CALL_ERRCHECK(
30 gpu->vkInterface(),
31 CreateCommandPool(gpu->device(), &cmdPoolInfo, nullptr, &pool));
32 return new GrVkCommandPool(gpu, pool);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050033}
34
35GrVkCommandPool::GrVkCommandPool(const GrVkGpu* gpu, VkCommandPool commandPool)
36 : fCommandPool(commandPool) {
37 fPrimaryCommandBuffer = GrVkPrimaryCommandBuffer::Create(gpu, this);
38}
39
40GrVkSecondaryCommandBuffer* GrVkCommandPool::findOrCreateSecondaryCommandBuffer(GrVkGpu* gpu) {
41 if (fAvailableSecondaryBuffers.count()) {
42 GrVkSecondaryCommandBuffer* result = fAvailableSecondaryBuffers.back();
43 fAvailableSecondaryBuffers.pop_back();
44 return result;
45 }
46 return GrVkSecondaryCommandBuffer::Create(gpu, this);
47}
48
49void GrVkCommandPool::recycleSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* buffer) {
50 SkASSERT(buffer->commandPool() == this);
51 fAvailableSecondaryBuffers.push_back(buffer);
52}
53
54void GrVkCommandPool::close() {
55 fOpen = false;
56}
57
58void GrVkCommandPool::reset(GrVkGpu* gpu) {
59 SkASSERT(!fOpen);
60 fOpen = true;
61 fPrimaryCommandBuffer->recycleSecondaryCommandBuffers();
62 GR_VK_CALL_ERRCHECK(gpu->vkInterface(), ResetCommandPool(gpu->device(), fCommandPool, 0));
63}
64
65void GrVkCommandPool::releaseResources(GrVkGpu* gpu) {
Brian Salomone39526b2019-06-24 16:35:53 -040066 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050067 SkASSERT(!fOpen);
68 fPrimaryCommandBuffer->releaseResources(gpu);
Ethan Nicholas8e265a72018-12-12 16:22:40 -050069}
70
71void GrVkCommandPool::abandonGPUData() const {
72 fPrimaryCommandBuffer->unrefAndAbandon();
73 for (GrVkSecondaryCommandBuffer* buffer : fAvailableSecondaryBuffers) {
74 SkASSERT(buffer->unique());
75 buffer->unrefAndAbandon();
76 }
77}
78
79void GrVkCommandPool::freeGPUData(GrVkGpu* gpu) const {
80 fPrimaryCommandBuffer->unref(gpu);
81 for (GrVkSecondaryCommandBuffer* buffer : fAvailableSecondaryBuffers) {
82 SkASSERT(buffer->unique());
83 buffer->unref(gpu);
84 }
85 if (fCommandPool != VK_NULL_HANDLE) {
86 GR_VK_CALL(gpu->vkInterface(),
87 DestroyCommandPool(gpu->device(), fCommandPool, nullptr));
88 }
89}