1 /*
2 * Copyright 2022 Google LLC
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 "src/gpu/graphite/dawn/DawnQueueManager.h"
9
10 #include "src/gpu/graphite/dawn/DawnAsyncWait.h"
11 #include "src/gpu/graphite/dawn/DawnCommandBuffer.h"
12 #include "src/gpu/graphite/dawn/DawnResourceProvider.h"
13 #include "src/gpu/graphite/dawn/DawnSharedContext.h"
14
15 namespace skgpu::graphite {
16 namespace {
17 class DawnWorkSubmission final : public GpuWorkSubmission {
18 public:
DawnWorkSubmission(std::unique_ptr<CommandBuffer> cmdBuffer,DawnQueueManager * queueManager,wgpu::Device device)19 DawnWorkSubmission(std::unique_ptr<CommandBuffer> cmdBuffer,
20 DawnQueueManager* queueManager,
21 wgpu::Device device)
22 : GpuWorkSubmission(std::move(cmdBuffer), queueManager), fAsyncWait(std::move(device)) {
23 queueManager->dawnQueue().OnSubmittedWorkDone(
24 0,
25 [](WGPUQueueWorkDoneStatus, void* userData) {
26 auto asyncWaitPtr = static_cast<DawnAsyncWait*>(userData);
27 asyncWaitPtr->signal();
28 },
29 &fAsyncWait);
30 }
~DawnWorkSubmission()31 ~DawnWorkSubmission() override {}
32
isFinished()33 bool isFinished() override { return fAsyncWait.yieldAndCheck(); }
waitUntilFinished()34 void waitUntilFinished() override { fAsyncWait.busyWait(); }
35
36 private:
37 DawnAsyncWait fAsyncWait;
38 };
39 } // namespace
40
DawnQueueManager(wgpu::Queue queue,const SharedContext * sharedContext)41 DawnQueueManager::DawnQueueManager(wgpu::Queue queue, const SharedContext* sharedContext)
42 : QueueManager(sharedContext), fQueue(std::move(queue)) {}
43
dawnSharedContext() const44 const DawnSharedContext* DawnQueueManager::dawnSharedContext() const {
45 return static_cast<const DawnSharedContext*>(fSharedContext);
46 }
47
getNewCommandBuffer(ResourceProvider * resourceProvider)48 std::unique_ptr<CommandBuffer> DawnQueueManager::getNewCommandBuffer(
49 ResourceProvider* resourceProvider) {
50 return DawnCommandBuffer::Make(dawnSharedContext(),
51 static_cast<DawnResourceProvider*>(resourceProvider));
52 }
53
onSubmitToGpu()54 QueueManager::OutstandingSubmission DawnQueueManager::onSubmitToGpu() {
55 SkASSERT(fCurrentCommandBuffer);
56 DawnCommandBuffer* dawnCmdBuffer = static_cast<DawnCommandBuffer*>(fCurrentCommandBuffer.get());
57 auto wgpuCmdBuffer = dawnCmdBuffer->finishEncoding();
58 if (!wgpuCmdBuffer) {
59 fCurrentCommandBuffer->callFinishedProcs(/*success=*/false);
60 return nullptr;
61 }
62
63 fQueue.Submit(/*commandCount=*/1, &wgpuCmdBuffer);
64
65 std::unique_ptr<DawnWorkSubmission> submission(new DawnWorkSubmission(
66 std::move(fCurrentCommandBuffer), this, dawnSharedContext()->device()));
67
68 return std::move(submission);
69 }
70
71 #if GRAPHITE_TEST_UTILS
startCapture()72 void DawnQueueManager::startCapture() {
73 // TODO: Dawn doesn't have capturing feature yet.
74 }
75
stopCapture()76 void DawnQueueManager::stopCapture() {
77 // TODO: Dawn doesn't have capturing feature yet.
78 }
79 #endif
80
81 } // namespace skgpu::graphite
82