xref: /aosp_15_r20/external/skia/src/gpu/graphite/GpuWorkSubmission.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/GpuWorkSubmission.h"
9 
10 #include "src/gpu/graphite/Buffer.h"
11 #include "src/gpu/graphite/CommandBuffer.h"
12 #include "src/gpu/graphite/QueueManager.h"
13 
14 namespace skgpu::graphite {
15 
GpuWorkSubmission(std::unique_ptr<CommandBuffer> cmdBuffer,QueueManager * queueManager)16 GpuWorkSubmission::GpuWorkSubmission(std::unique_ptr<CommandBuffer> cmdBuffer,
17                                      QueueManager* queueManager)
18         : fCommandBuffer(std::move(cmdBuffer))
19         , fQueueManager(queueManager) {
20     SkSpan<const sk_sp<Buffer>> buffers = fCommandBuffer->buffersToAsyncMapOnSubmit();
21     if (!buffers.empty()) {
22         fOutstandingAsyncMapCounter = sk_make_sp<SkRefCnt>();
23         for (auto& buffer : fCommandBuffer->buffersToAsyncMapOnSubmit()) {
24             SkASSERT(!buffer->isUnmappable());
25             fOutstandingAsyncMapCounter->ref();
26             buffer->asyncMap([](GpuFinishedContext c, CallbackResult) {
27                                 static_cast<SkRefCnt*>(c)->unref();
28                              },
29                              fOutstandingAsyncMapCounter.get());
30         }
31     }
32 }
33 
~GpuWorkSubmission()34 GpuWorkSubmission::~GpuWorkSubmission() {
35     fCommandBuffer->callFinishedProcs(/*success=*/true);
36     fCommandBuffer->resetCommandBuffer();
37     fQueueManager->returnCommandBuffer(std::move(fCommandBuffer));
38 }
39 
isFinished(const SharedContext * sharedContext)40 bool GpuWorkSubmission::isFinished(const SharedContext* sharedContext) {
41     return this->onIsFinished(sharedContext) &&
42            (!fOutstandingAsyncMapCounter || fOutstandingAsyncMapCounter->unique());
43 }
44 
waitUntilFinished(const SharedContext * sharedContext)45 void GpuWorkSubmission::waitUntilFinished(const SharedContext* sharedContext) {
46     this->onWaitUntilFinished(sharedContext);
47     if (fOutstandingAsyncMapCounter) {
48         while (!fOutstandingAsyncMapCounter->unique()) {
49             fQueueManager->tick();
50         }
51     }
52 }
53 
54 } // namespace skgpu::graphite
55 
56