xref: /aosp_15_r20/external/skia/src/gpu/graphite/QueueManager.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/QueueManager.h"
9 
10 #include "include/gpu/graphite/Recording.h"
11 #include "src/core/SkTraceEvent.h"
12 #include "src/gpu/GpuTypesPriv.h"
13 #include "src/gpu/RefCntedCallback.h"
14 #include "src/gpu/graphite/Buffer.h"
15 #include "src/gpu/graphite/Caps.h"
16 #include "src/gpu/graphite/CommandBuffer.h"
17 #include "src/gpu/graphite/ContextPriv.h"
18 #include "src/gpu/graphite/GpuWorkSubmission.h"
19 #include "src/gpu/graphite/Log.h"
20 #include "src/gpu/graphite/RecordingPriv.h"
21 #include "src/gpu/graphite/Surface_Graphite.h"
22 #include "src/gpu/graphite/UploadBufferManager.h"
23 #include "src/gpu/graphite/task/Task.h"
24 
25 namespace skgpu::graphite {
26 
27 // This constant determines how many OutstandingSubmissions are allocated together as a block in
28 // the deque. As such it needs to balance allocating too much memory vs. incurring
29 // allocation/deallocation thrashing. It should roughly correspond to the max number of outstanding
30 // submissions we expect to see.
31 static constexpr int kDefaultOutstandingAllocCnt = 8;
32 
QueueManager(const SharedContext * sharedContext)33 QueueManager::QueueManager(const SharedContext* sharedContext)
34         : fSharedContext(sharedContext)
35         , fOutstandingSubmissions(sizeof(OutstandingSubmission), kDefaultOutstandingAllocCnt) {
36 }
37 
~QueueManager()38 QueueManager::~QueueManager() {
39     if (fSharedContext->caps()->allowCpuSync()) {
40         this->checkForFinishedWork(SyncToCpu::kYes);
41     } else if (!fOutstandingSubmissions.empty()) {
42         SKGPU_LOG_F("When ContextOptions::fNeverYieldToWebGPU is specified all GPU work must be "
43                     "finished before destroying Context.");
44     }
45 }
46 
47 std::vector<std::unique_ptr<CommandBuffer>>*
getAvailableCommandBufferList(Protected isProtected)48 QueueManager::getAvailableCommandBufferList(Protected isProtected) {
49     return isProtected == Protected::kNo ? &fAvailableCommandBuffers
50                                          : &fAvailableProtectedCommandBuffers;
51 }
52 
53 
setupCommandBuffer(ResourceProvider * resourceProvider,Protected isProtected)54 bool QueueManager::setupCommandBuffer(ResourceProvider* resourceProvider, Protected isProtected) {
55     if (!fCurrentCommandBuffer) {
56         std::vector<std::unique_ptr<CommandBuffer>>* bufferList =
57                 this->getAvailableCommandBufferList(isProtected);
58         if (!bufferList->empty()) {
59             fCurrentCommandBuffer = std::move(bufferList->back());
60             bufferList->pop_back();
61             if (!fCurrentCommandBuffer->setNewCommandBufferResources()) {
62                 fCurrentCommandBuffer.reset();
63             }
64         }
65     } else {
66         if (fCurrentCommandBuffer->isProtected() != isProtected) {
67             // If we're doing things where we are switching between using protected and unprotected
68             // command buffers, it is our job to make sure previous work was submitted.
69             SKGPU_LOG_E("Trying to use a CommandBuffer with protectedness that differs from our "
70                         "current active command buffer.");
71             return false;
72         }
73     }
74     if (!fCurrentCommandBuffer) {
75         fCurrentCommandBuffer = this->getNewCommandBuffer(resourceProvider, isProtected);
76     }
77     if (!fCurrentCommandBuffer) {
78         return false;
79     }
80 
81     return true;
82 }
83 
addRecording(const InsertRecordingInfo & info,Context * context)84 bool QueueManager::addRecording(const InsertRecordingInfo& info, Context* context) {
85     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
86 
87     bool addTimerQuery = false;
88     sk_sp<RefCntedCallback> callback;
89     if (info.fFinishedWithStatsProc) {
90         addTimerQuery = info.fGpuStatsFlags & GpuStatsFlags::kElapsedTime;
91         if (addTimerQuery && !(context->supportedGpuStats() & GpuStatsFlags::kElapsedTime)) {
92             addTimerQuery = false;
93             SKGPU_LOG_W("Requested elapsed time reporting but not supported by Context.");
94         }
95         callback = RefCntedCallback::Make(info.fFinishedWithStatsProc, info.fFinishedContext);
96     } else if (info.fFinishedProc) {
97         callback = RefCntedCallback::Make(info.fFinishedProc, info.fFinishedContext);
98     }
99 
100     SkASSERT(info.fRecording);
101     if (!info.fRecording) {
102         if (callback) {
103             callback->setFailureResult();
104         }
105         SKGPU_LOG_E("No valid Recording passed into addRecording call");
106         return false;
107     }
108 
109     if (fSharedContext->caps()->requireOrderedRecordings()) {
110         uint32_t* recordingID = fLastAddedRecordingIDs.find(info.fRecording->priv().recorderID());
111         if (recordingID &&
112             info.fRecording->priv().uniqueID() != *recordingID+1) {
113             if (callback) {
114                 callback->setFailureResult();
115             }
116             SKGPU_LOG_E("Recordings are expected to be replayed in order");
117             return false;
118         }
119 
120         // Note the new Recording ID.
121         fLastAddedRecordingIDs.set(info.fRecording->priv().recorderID(),
122                                    info.fRecording->priv().uniqueID());
123     }
124 
125     if (info.fTargetSurface &&
126         !static_cast<const SkSurface_Base*>(info.fTargetSurface)->isGraphiteBacked()) {
127         if (callback) {
128             callback->setFailureResult();
129         }
130         info.fRecording->priv().setFailureResultForFinishedProcs();
131         SKGPU_LOG_E("Target surface passed into addRecording call is not graphite-backed");
132         return false;
133     }
134 
135     auto resourceProvider = context->priv().resourceProvider();
136     if (!this->setupCommandBuffer(resourceProvider, fSharedContext->isProtected())) {
137         if (callback) {
138             callback->setFailureResult();
139         }
140         info.fRecording->priv().setFailureResultForFinishedProcs();
141         SKGPU_LOG_E("CommandBuffer creation failed");
142         return false;
143     }
144 
145     // This must happen before instantiating the lazy proxies, because the target for draws in this
146     // recording may itself be a lazy proxy whose instantiation must be handled specially here.
147     // We must also make sure the lazy proxies are instantiated successfully before we make any
148     // modifications to the current command buffer, so we can't just do all this work in
149     // Recording::addCommands below.
150     TextureProxy* deferredTargetProxy = info.fRecording->priv().deferredTargetProxy();
151     AutoDeinstantiateTextureProxy autoDeinstantiateTargetProxy(deferredTargetProxy);
152     const Texture* replayTarget = nullptr;
153     if (deferredTargetProxy && info.fTargetSurface) {
154         replayTarget = info.fRecording->priv().setupDeferredTarget(
155                 resourceProvider,
156                 static_cast<Surface*>(info.fTargetSurface),
157                 info.fTargetTranslation,
158                 info.fTargetClip);
159         if (!replayTarget) {
160             SKGPU_LOG_E("Failed to set up deferred replay target");
161             return false;
162         }
163 
164     } else if (deferredTargetProxy && !info.fTargetSurface) {
165         SKGPU_LOG_E("No surface provided to instantiate deferred replay target.");
166         return false;
167     }
168 
169     if (info.fRecording->priv().hasNonVolatileLazyProxies()) {
170         if (!info.fRecording->priv().instantiateNonVolatileLazyProxies(resourceProvider)) {
171             if (callback) {
172                 callback->setFailureResult();
173             }
174             info.fRecording->priv().setFailureResultForFinishedProcs();
175             SKGPU_LOG_E("Non-volatile PromiseImage instantiation has failed");
176             return false;
177         }
178     }
179 
180     if (info.fRecording->priv().hasVolatileLazyProxies()) {
181         if (!info.fRecording->priv().instantiateVolatileLazyProxies(resourceProvider)) {
182             if (callback) {
183                 callback->setFailureResult();
184             }
185             info.fRecording->priv().setFailureResultForFinishedProcs();
186             info.fRecording->priv().deinstantiateVolatileLazyProxies();
187             SKGPU_LOG_E("Volatile PromiseImage instantiation has failed");
188             return false;
189         }
190     }
191 
192     if (addTimerQuery) {
193         fCurrentCommandBuffer->startTimerQuery();
194     }
195     fCurrentCommandBuffer->addWaitSemaphores(info.fNumWaitSemaphores, info.fWaitSemaphores);
196     if (!info.fRecording->priv().addCommands(context,
197                                              fCurrentCommandBuffer.get(),
198                                              replayTarget,
199                                              info.fTargetTranslation,
200                                              info.fTargetClip)) {
201         if (callback) {
202             callback->setFailureResult();
203         }
204         info.fRecording->priv().setFailureResultForFinishedProcs();
205         info.fRecording->priv().deinstantiateVolatileLazyProxies();
206         SKGPU_LOG_E("Adding Recording commands to the CommandBuffer has failed");
207         return false;
208     }
209     fCurrentCommandBuffer->addSignalSemaphores(info.fNumSignalSemaphores, info.fSignalSemaphores);
210     if (info.fTargetTextureState) {
211         fCurrentCommandBuffer->prepareSurfaceForStateUpdate(info.fTargetSurface,
212                                                             info.fTargetTextureState);
213     }
214     if (addTimerQuery) {
215         fCurrentCommandBuffer->endTimerQuery();
216     }
217 
218     if (callback) {
219         fCurrentCommandBuffer->addFinishedProc(std::move(callback));
220     }
221 
222     info.fRecording->priv().deinstantiateVolatileLazyProxies();
223 
224     return true;
225 }
226 
addTask(Task * task,Context * context,Protected isProtected)227 bool QueueManager::addTask(Task* task,
228                            Context* context,
229                            Protected isProtected) {
230     SkASSERT(task);
231     if (!task) {
232         SKGPU_LOG_E("No valid Task passed into addTask call");
233         return false;
234     }
235 
236     if (!this->setupCommandBuffer(context->priv().resourceProvider(), isProtected)) {
237         SKGPU_LOG_E("CommandBuffer creation failed");
238         return false;
239     }
240 
241     if (task->addCommands(context, fCurrentCommandBuffer.get(), {}) == Task::Status::kFail) {
242         SKGPU_LOG_E("Adding Task commands to the CommandBuffer has failed");
243         return false;
244     }
245 
246     return true;
247 }
248 
addFinishInfo(const InsertFinishInfo & info,ResourceProvider * resourceProvider,SkSpan<const sk_sp<Buffer>> buffersToAsyncMap)249 bool QueueManager::addFinishInfo(const InsertFinishInfo& info,
250                                  ResourceProvider* resourceProvider,
251                                  SkSpan<const sk_sp<Buffer>> buffersToAsyncMap) {
252     sk_sp<RefCntedCallback> callback;
253     if (info.fFinishedProc) {
254         callback = RefCntedCallback::Make(info.fFinishedProc, info.fFinishedContext);
255     }
256 
257     if (!this->setupCommandBuffer(resourceProvider, fSharedContext->isProtected())) {
258         if (callback) {
259             callback->setFailureResult();
260         }
261         SKGPU_LOG_E("CommandBuffer creation failed");
262         return false;
263     }
264 
265     if (callback) {
266         fCurrentCommandBuffer->addFinishedProc(std::move(callback));
267     }
268     fCurrentCommandBuffer->addBuffersToAsyncMapOnSubmit(buffersToAsyncMap);
269 
270     return true;
271 }
272 
submitToGpu()273 bool QueueManager::submitToGpu() {
274     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
275 
276     if (!fCurrentCommandBuffer) {
277         // We warn because this probably representative of a bad client state, where they don't
278         // need to submit but didn't notice, but technically the submit itself is fine (no-op), so
279         // we return true.
280         SKGPU_LOG_D("Submit called with no active command buffer!");
281         return true;
282     }
283 
284 #ifdef SK_DEBUG
285     if (!fCurrentCommandBuffer->hasWork()) {
286         SKGPU_LOG_D("Submitting empty command buffer!");
287     }
288 #endif
289 
290     auto submission = this->onSubmitToGpu();
291     if (!submission) {
292         return false;
293     }
294 
295     new (fOutstandingSubmissions.push_back()) OutstandingSubmission(std::move(submission));
296     return true;
297 }
298 
hasUnfinishedGpuWork()299 bool QueueManager::hasUnfinishedGpuWork() { return !fOutstandingSubmissions.empty(); }
300 
checkForFinishedWork(SyncToCpu sync)301 void QueueManager::checkForFinishedWork(SyncToCpu sync) {
302     TRACE_EVENT1("skia.gpu", TRACE_FUNC, "sync", sync == SyncToCpu::kYes);
303 
304     if (sync == SyncToCpu::kYes) {
305         SkASSERT(fSharedContext->caps()->allowCpuSync());
306         // wait for the last submission to finish
307         OutstandingSubmission* back = (OutstandingSubmission*)fOutstandingSubmissions.back();
308         if (back) {
309             (*back)->waitUntilFinished(fSharedContext);
310         }
311     }
312 
313     // Iterate over all the outstanding submissions to see if any have finished. The work
314     // submissions are in order from oldest to newest, so we start at the front to check if they
315     // have finished. If so we pop it off and move onto the next.
316     // Repeat till we find a submission that has not finished yet (and all others afterwards are
317     // also guaranteed to not have finished).
318     OutstandingSubmission* front = (OutstandingSubmission*)fOutstandingSubmissions.front();
319     while (front && (*front)->isFinished(fSharedContext)) {
320         // Make sure we remove before deleting as deletion might try to kick off another submit
321         // (though hopefully *not* in Graphite).
322         fOutstandingSubmissions.pop_front();
323         // Since we used placement new we are responsible for calling the destructor manually.
324         front->~OutstandingSubmission();
325         front = (OutstandingSubmission*)fOutstandingSubmissions.front();
326     }
327     SkASSERT(sync == SyncToCpu::kNo || fOutstandingSubmissions.empty());
328 }
329 
returnCommandBuffer(std::unique_ptr<CommandBuffer> commandBuffer)330 void QueueManager::returnCommandBuffer(std::unique_ptr<CommandBuffer> commandBuffer) {
331     std::vector<std::unique_ptr<CommandBuffer>>* bufferList =
332             this->getAvailableCommandBufferList(commandBuffer->isProtected());
333     bufferList->push_back(std::move(commandBuffer));
334 }
335 
addUploadBufferManagerRefs(UploadBufferManager * uploadManager)336 void QueueManager::addUploadBufferManagerRefs(UploadBufferManager* uploadManager) {
337     SkASSERT(fCurrentCommandBuffer);
338     uploadManager->transferToCommandBuffer(fCurrentCommandBuffer.get());
339 }
340 
341 
342 } // namespace skgpu::graphite
343