Hash :
9413c402
Author :
Date :
2020-10-03T10:58:12
Vulkan: Move CommandBatch for threading support Will need access to CommandBatch class in threading worker. Bug: b/154030730 Change-Id: Ia79eab77a81b135c22bdeecbaf65bf3c301dc987 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2447442 Reviewed-by: Tim Van Patten <timvp@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Courtney Goeltzenleuchter <courtneygo@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
//
// Copyright 2020 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// CommandProcessor.cpp:
// Implements the class methods for CommandProcessor.
//
#include "libANGLE/renderer/vulkan/CommandProcessor.h"
#include "libANGLE/trace.h"
namespace rx
{
namespace vk
{
// CommandBatch implementation.
CommandBatch::CommandBatch() = default;
CommandBatch::~CommandBatch() = default;
CommandBatch::CommandBatch(CommandBatch &&other)
{
*this = std::move(other);
}
CommandBatch &CommandBatch::operator=(CommandBatch &&other)
{
std::swap(primaryCommands, other.primaryCommands);
std::swap(commandPool, other.commandPool);
std::swap(fence, other.fence);
std::swap(serial, other.serial);
return *this;
}
void CommandBatch::destroy(VkDevice device)
{
primaryCommands.destroy(device);
commandPool.destroy(device);
fence.reset(device);
}
} // namespace vk
CommandProcessor::CommandProcessor() : mWorkerThreadIdle(true) {}
void CommandProcessor::queueCommands(const vk::CommandProcessorTask &commands)
{
ANGLE_TRACE_EVENT0("gpu.angle", "CommandProcessor::queueCommands");
std::lock_guard<std::mutex> queueLock(mWorkerMutex);
ASSERT(commands.commandBuffer == nullptr || !commands.commandBuffer->empty());
mCommandsQueue.push(commands);
mWorkAvailableCondition.notify_one();
}
void CommandProcessor::processCommandProcessorTasks()
{
while (true)
{
std::unique_lock<std::mutex> lock(mWorkerMutex);
mWorkerIdleCondition.notify_one();
mWorkerThreadIdle = true;
// Only wake if notified and command queue is not empty
mWorkAvailableCondition.wait(lock, [this] { return !mCommandsQueue.empty(); });
mWorkerThreadIdle = false;
vk::CommandProcessorTask task = mCommandsQueue.front();
mCommandsQueue.pop();
lock.unlock();
// Either both ptrs should be null or non-null
ASSERT((task.commandBuffer != nullptr && task.contextVk != nullptr) ||
(task.commandBuffer == nullptr && task.contextVk == nullptr));
// A work block with null ptrs signals worker thread to exit
if (task.commandBuffer == nullptr && task.contextVk == nullptr)
{
break;
}
ASSERT(!task.commandBuffer->empty());
// TODO: Will need some way to synchronize error reporting between threads
(void)(task.commandBuffer->flushToPrimary(task.contextVk, task.primaryCB));
ASSERT(task.commandBuffer->empty());
task.commandBuffer->releaseToContextQueue(task.contextVk);
}
}
void CommandProcessor::waitForWorkComplete()
{
ANGLE_TRACE_EVENT0("gpu.angle", "CommandProcessor::waitForWorkerThreadIdle");
std::unique_lock<std::mutex> lock(mWorkerMutex);
mWorkerIdleCondition.wait(lock,
[this] { return (mCommandsQueue.empty() && mWorkerThreadIdle); });
// Worker thread is idle and command queue is empty so good to continue
lock.unlock();
}
void CommandProcessor::shutdown(std::thread *commandProcessorThread)
{
waitForWorkComplete();
const vk::CommandProcessorTask endTask = vk::kEndCommandProcessorThread;
queueCommands(endTask);
if (commandProcessorThread->joinable())
{
commandProcessorThread->join();
}
}
} // namespace rx