xref: /aosp_15_r20/external/skia/src/gpu/graphite/task/DrawTask.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 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/task/DrawTask.h"
9 
10 #include "src/gpu/graphite/ScratchResourceManager.h"
11 #include "src/gpu/graphite/Texture.h"
12 #include "src/gpu/graphite/TextureProxy.h"
13 
14 namespace skgpu::graphite {
15 
DrawTask(sk_sp<TextureProxy> target)16 DrawTask::DrawTask(sk_sp<TextureProxy> target) : fTarget(std::move(target)) {}
17 
18 DrawTask::~DrawTask() = default;
19 
prepareResources(ResourceProvider * resourceProvider,ScratchResourceManager * scratchManager,const RuntimeEffectDictionary * rteDict)20 Task::Status DrawTask::prepareResources(ResourceProvider* resourceProvider,
21                                         ScratchResourceManager* scratchManager,
22                                         const RuntimeEffectDictionary* rteDict) {
23     const int pendingReadCount = scratchManager->pendingReadCount(fTarget.get());
24     if (pendingReadCount) {
25         // This DrawTask defines the content of a scratch device that has incremented the pending
26         // read count before snap() was called. The target may have already been instantiated if
27         // we've processed this task's children before.
28         SkASSERT(!fTarget->isLazy());
29         // Even though we may discard the task, we always want to mark it as in-use to track the
30         // pending reads to know when to return the texture.;
31         scratchManager->markResourceInUse(this);
32 
33         if (fPrepared) {
34             // If the task has already had prepareResources() called once, it should have had
35             // its target instantiated.
36             SkASSERT(fTarget->isInstantiated());
37             // Return kDiscard so that this reference to the task is removed and the original
38             // encounter in the graph will be the only time addCommands() is invoked.
39             return Status::kDiscard;
40         }
41     } else {
42         // A non-scratch DrawTask should only ever be in the task graph one time.
43         SkASSERT(!fPrepared);
44     }
45 
46     fPrepared = true;
47     // NOTE: This prepareResources() pushes a new scope for scratch resource management, which is
48     // what we want since the child tasks are what will actually instantiate any scratch device and
49     // trigger returns of any grand-child resources. The above markResourceInUse() should happen
50     // above this so that pending returns are handled in caller's scope.
51     return fChildTasks.prepareResources(resourceProvider, scratchManager, rteDict);
52 }
53 
onUseCompleted(ScratchResourceManager * scratchManager)54 void DrawTask::onUseCompleted(ScratchResourceManager* scratchManager) {
55     // Now that the render task has completed, actually decrement the read count of the target proxy
56     // If the count hits zero, this was the last pending read that needed to use the DrawTask's
57     // results so we can return the texture to the ScratchResourceManager for reuse.
58     SkASSERT(!fTarget->isLazy() && fTarget->isInstantiated());
59     SkASSERT(scratchManager->pendingReadCount(fTarget.get()) > 0);
60     if (scratchManager->removePendingRead(fTarget.get())) {
61         scratchManager->returnTexture(fTarget->refTexture());
62     }
63 }
64 
addCommands(Context * ctx,CommandBuffer * commandBuffer,ReplayTargetData replayTarget)65 Task::Status DrawTask::addCommands(Context* ctx,
66                                    CommandBuffer* commandBuffer,
67                                    ReplayTargetData replayTarget) {
68     SkASSERT(fTarget->isInstantiated());
69     return fChildTasks.addCommands(ctx, commandBuffer, replayTarget);
70 }
71 
72 } // namespace skgpu::graphite
73