Home | History | Annotate | Download | only in vk
      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 
      8 #include "GrVkCommandPool.h"
      9 
     10 #include "GrContextPriv.h"
     11 #include "GrVkCommandBuffer.h"
     12 #include "GrVkGpu.h"
     13 
     14 GrVkCommandPool* GrVkCommandPool::Create(const GrVkGpu* gpu) {
     15     const VkCommandPoolCreateInfo cmdPoolInfo = {
     16         VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,      // sType
     17         nullptr,                                         // pNext
     18         VK_COMMAND_POOL_CREATE_TRANSIENT_BIT |
     19         VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, // CmdPoolCreateFlags
     20         gpu->queueIndex(),                              // queueFamilyIndex
     21     };
     22     VkCommandPool pool;
     23     GR_VK_CALL_ERRCHECK(gpu->vkInterface(), CreateCommandPool(gpu->device(), &cmdPoolInfo,
     24                                                                nullptr, &pool));
     25     return new GrVkCommandPool(gpu, pool);
     26 }
     27 
     28 GrVkCommandPool::GrVkCommandPool(const GrVkGpu* gpu, VkCommandPool commandPool)
     29         : fCommandPool(commandPool) {
     30     fPrimaryCommandBuffer = GrVkPrimaryCommandBuffer::Create(gpu, this);
     31 }
     32 
     33 GrVkSecondaryCommandBuffer* GrVkCommandPool::findOrCreateSecondaryCommandBuffer(GrVkGpu* gpu) {
     34     if (fAvailableSecondaryBuffers.count()) {
     35         GrVkSecondaryCommandBuffer* result = fAvailableSecondaryBuffers.back();
     36         fAvailableSecondaryBuffers.pop_back();
     37         return result;
     38     }
     39     return GrVkSecondaryCommandBuffer::Create(gpu, this);
     40 }
     41 
     42 void GrVkCommandPool::recycleSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* buffer) {
     43     SkASSERT(buffer->commandPool() == this);
     44     fAvailableSecondaryBuffers.push_back(buffer);
     45 }
     46 
     47 void GrVkCommandPool::close() {
     48     fOpen = false;
     49 }
     50 
     51 void GrVkCommandPool::reset(GrVkGpu* gpu) {
     52     SkASSERT(!fOpen);
     53     fOpen = true;
     54     fPrimaryCommandBuffer->recycleSecondaryCommandBuffers();
     55     GR_VK_CALL_ERRCHECK(gpu->vkInterface(), ResetCommandPool(gpu->device(), fCommandPool, 0));
     56 }
     57 
     58 void GrVkCommandPool::releaseResources(GrVkGpu* gpu) {
     59     SkASSERT(!fOpen);
     60     fPrimaryCommandBuffer->releaseResources(gpu);
     61     for (GrVkSecondaryCommandBuffer* buffer : fAvailableSecondaryBuffers) {
     62         buffer->releaseResources(gpu);
     63     }
     64 }
     65 
     66 void GrVkCommandPool::abandonGPUData() const {
     67     fPrimaryCommandBuffer->unrefAndAbandon();
     68     for (GrVkSecondaryCommandBuffer* buffer : fAvailableSecondaryBuffers) {
     69         SkASSERT(buffer->unique());
     70         buffer->unrefAndAbandon();
     71     }
     72 }
     73 
     74 void GrVkCommandPool::freeGPUData(GrVkGpu* gpu) const {
     75     fPrimaryCommandBuffer->unref(gpu);
     76     for (GrVkSecondaryCommandBuffer* buffer : fAvailableSecondaryBuffers) {
     77         SkASSERT(buffer->unique());
     78         buffer->unref(gpu);
     79     }
     80     if (fCommandPool != VK_NULL_HANDLE) {
     81         GR_VK_CALL(gpu->vkInterface(),
     82                    DestroyCommandPool(gpu->device(), fCommandPool, nullptr));
     83     }
     84 }
     85