xref: /aosp_15_r20/external/skia/src/gpu/graphite/CommandBuffer.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 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/CommandBuffer.h"
9 
10 #include "src/core/SkTraceEvent.h"
11 #include "src/gpu/RefCntedCallback.h"
12 #include "src/gpu/graphite/Buffer.h"
13 #include "src/gpu/graphite/ComputePipeline.h"
14 #include "src/gpu/graphite/DrawPass.h"
15 #include "src/gpu/graphite/GraphicsPipeline.h"
16 #include "src/gpu/graphite/Log.h"
17 #include "src/gpu/graphite/RenderPassDesc.h"
18 #include "src/gpu/graphite/ResourceProvider.h"
19 #include "src/gpu/graphite/Sampler.h"
20 #include "src/gpu/graphite/Texture.h"
21 #include "src/gpu/graphite/TextureProxy.h"
22 
23 namespace skgpu::graphite {
24 
CommandBuffer(Protected isProtected)25 CommandBuffer::CommandBuffer(Protected isProtected) : fIsProtected(isProtected) {}
26 
~CommandBuffer()27 CommandBuffer::~CommandBuffer() {
28     this->releaseResources();
29 }
30 
releaseResources()31 void CommandBuffer::releaseResources() {
32     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
33 
34     fTrackedUsageResources.clear();
35     fCommandBufferResources.clear();
36 }
37 
resetCommandBuffer()38 void CommandBuffer::resetCommandBuffer() {
39     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
40 
41     // The dst copy texture and sampler are kept alive by the tracked resources, so reset these
42     // before we release their refs. Assuming we don't go idle and free lots of resources, we'll
43     // get the same cached sampler the next time we need a dst copy.
44     fDstCopy = {nullptr, nullptr};
45     this->releaseResources();
46     this->onResetCommandBuffer();
47     fBuffersToAsyncMap.clear();
48 }
49 
trackResource(sk_sp<Resource> resource)50 void CommandBuffer::trackResource(sk_sp<Resource> resource) {
51     fTrackedUsageResources.push_back(std::move(resource));
52 }
53 
trackCommandBufferResource(sk_sp<Resource> resource)54 void CommandBuffer::trackCommandBufferResource(sk_sp<Resource> resource) {
55     fCommandBufferResources.push_back(std::move(resource));
56 }
57 
addFinishedProc(sk_sp<RefCntedCallback> finishedProc)58 void CommandBuffer::addFinishedProc(sk_sp<RefCntedCallback> finishedProc) {
59     fFinishedProcs.push_back(std::move(finishedProc));
60 }
61 
callFinishedProcs(bool success)62 void CommandBuffer::callFinishedProcs(bool success) {
63     if (!success) {
64         for (int i = 0; i < fFinishedProcs.size(); ++i) {
65             fFinishedProcs[i]->setFailureResult();
66         }
67     } else {
68         if (auto stats = this->gpuStats()) {
69             for (int i = 0; i < fFinishedProcs.size(); ++i) {
70                 if (fFinishedProcs[i]->receivesGpuStats()) {
71                     fFinishedProcs[i]->setStats(*stats);
72                 }
73             }
74         }
75     }
76     fFinishedProcs.clear();
77 }
78 
addBuffersToAsyncMapOnSubmit(SkSpan<const sk_sp<Buffer>> buffers)79 void CommandBuffer::addBuffersToAsyncMapOnSubmit(SkSpan<const sk_sp<Buffer>> buffers) {
80     for (size_t i = 0; i < buffers.size(); ++i) {
81         SkASSERT(buffers[i]);
82         fBuffersToAsyncMap.push_back(buffers[i]);
83     }
84 }
85 
buffersToAsyncMapOnSubmit() const86 SkSpan<const sk_sp<Buffer>> CommandBuffer::buffersToAsyncMapOnSubmit() const {
87     return fBuffersToAsyncMap;
88 }
89 
addRenderPass(const RenderPassDesc & renderPassDesc,sk_sp<Texture> colorTexture,sk_sp<Texture> resolveTexture,sk_sp<Texture> depthStencilTexture,const Texture * dstCopy,SkIRect dstCopyBounds,SkISize viewportDims,const DrawPassList & drawPasses)90 bool CommandBuffer::addRenderPass(const RenderPassDesc& renderPassDesc,
91                                   sk_sp<Texture> colorTexture,
92                                   sk_sp<Texture> resolveTexture,
93                                   sk_sp<Texture> depthStencilTexture,
94                                   const Texture* dstCopy,
95                                   SkIRect dstCopyBounds,
96                                   SkISize viewportDims,
97                                   const DrawPassList& drawPasses) {
98     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
99 
100     fColorAttachmentSize = colorTexture->dimensions();
101     SkIRect colorAttachmentBounds = SkIRect::MakeSize(fColorAttachmentSize);
102 
103     SkIRect renderPassBounds;
104     for (const auto& drawPass : drawPasses) {
105         renderPassBounds.join(drawPass->bounds());
106     }
107     if (renderPassDesc.fColorAttachment.fLoadOp == LoadOp::kClear) {
108         renderPassBounds.join(colorAttachmentBounds);
109     }
110     renderPassBounds.offset(fReplayTranslation.x(), fReplayTranslation.y());
111     if (!renderPassBounds.intersect(colorAttachmentBounds)) {
112         // The entire RenderPass is offscreen given the replay translation so skip adding the pass
113         // at all
114         return true;
115     }
116 
117     dstCopyBounds.offset(fReplayTranslation.x(), fReplayTranslation.y());
118     if (!dstCopyBounds.intersect(colorAttachmentBounds)) {
119         // The draws within the RenderPass that would sample from the dstCopy have been translated
120         // off screen. Set the bounds to empty and let the GPU clipping do its job.
121         dstCopyBounds = SkIRect::MakeEmpty();
122     }
123     // Save the dstCopy texture so that it can be embedded into texture bind commands later on.
124     // Stash the texture's full dimensions on the rect so we can calculate normalized coords later.
125     fDstCopy.first = dstCopy;
126     fDstCopyBounds = dstCopy ? SkIRect::MakePtSize(dstCopyBounds.topLeft(), dstCopy->dimensions())
127                              : SkIRect::MakeEmpty();
128     if (dstCopy && !fDstCopy.second) {
129         // Only lookup the sampler the first time we require a dstCopy. The texture can change
130         // on subsequent passes but it will always use the same nearest neighbor sampling.
131         sk_sp<Sampler> nearestNeighbor = this->resourceProvider()->findOrCreateCompatibleSampler(
132                 {SkFilterMode::kNearest, SkTileMode::kClamp});
133         fDstCopy.second = nearestNeighbor.get();
134         this->trackResource(std::move(nearestNeighbor));
135     }
136 
137     // We don't intersect the viewport with the render pass bounds or target size because it just
138     // defines a linear transform, which we don't want to change just because a portion of it maps
139     // to a region that gets clipped.
140     SkIRect viewport = SkIRect::MakePtSize(fReplayTranslation, viewportDims);
141     if (!this->onAddRenderPass(renderPassDesc,
142                                renderPassBounds,
143                                colorTexture.get(),
144                                resolveTexture.get(),
145                                depthStencilTexture.get(),
146                                viewport,
147                                drawPasses)) {
148         return false;
149     }
150 
151     if (colorTexture) {
152         this->trackCommandBufferResource(std::move(colorTexture));
153     }
154     if (resolveTexture) {
155         this->trackCommandBufferResource(std::move(resolveTexture));
156     }
157     if (depthStencilTexture) {
158         this->trackCommandBufferResource(std::move(depthStencilTexture));
159     }
160     // We just assume if you are adding a render pass that the render pass will actually do work. In
161     // theory we could have a discard load that doesn't submit any draws, clears, etc. But hopefully
162     // something so trivial would be caught before getting here.
163     SkDEBUGCODE(fHasWork = true;)
164 
165     return true;
166 }
167 
addComputePass(DispatchGroupSpan dispatchGroups)168 bool CommandBuffer::addComputePass(DispatchGroupSpan dispatchGroups) {
169     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
170 
171     if (!this->onAddComputePass(dispatchGroups)) {
172         return false;
173     }
174 
175     SkDEBUGCODE(fHasWork = true;)
176 
177     return true;
178 }
179 
copyBufferToBuffer(const Buffer * srcBuffer,size_t srcOffset,sk_sp<Buffer> dstBuffer,size_t dstOffset,size_t size)180 bool CommandBuffer::copyBufferToBuffer(const Buffer* srcBuffer,
181                                        size_t srcOffset,
182                                        sk_sp<Buffer> dstBuffer,
183                                        size_t dstOffset,
184                                        size_t size) {
185     SkASSERT(srcBuffer);
186     SkASSERT(dstBuffer);
187 
188     if (!this->onCopyBufferToBuffer(srcBuffer, srcOffset, dstBuffer.get(), dstOffset, size)) {
189         return false;
190     }
191 
192     this->trackResource(std::move(dstBuffer));
193 
194     SkDEBUGCODE(fHasWork = true;)
195 
196     return true;
197 }
198 
copyTextureToBuffer(sk_sp<Texture> texture,SkIRect srcRect,sk_sp<Buffer> buffer,size_t bufferOffset,size_t bufferRowBytes)199 bool CommandBuffer::copyTextureToBuffer(sk_sp<Texture> texture,
200                                         SkIRect srcRect,
201                                         sk_sp<Buffer> buffer,
202                                         size_t bufferOffset,
203                                         size_t bufferRowBytes) {
204     SkASSERT(texture);
205     SkASSERT(buffer);
206 
207     if (!this->onCopyTextureToBuffer(texture.get(), srcRect, buffer.get(), bufferOffset,
208                                      bufferRowBytes)) {
209         return false;
210     }
211 
212     this->trackCommandBufferResource(std::move(texture));
213     this->trackResource(std::move(buffer));
214 
215     SkDEBUGCODE(fHasWork = true;)
216 
217     return true;
218 }
219 
copyBufferToTexture(const Buffer * buffer,sk_sp<Texture> texture,const BufferTextureCopyData * copyData,int count)220 bool CommandBuffer::copyBufferToTexture(const Buffer* buffer,
221                                         sk_sp<Texture> texture,
222                                         const BufferTextureCopyData* copyData,
223                                         int count) {
224     SkASSERT(buffer);
225     SkASSERT(texture);
226     SkASSERT(count > 0 && copyData);
227 
228     if (!this->onCopyBufferToTexture(buffer, texture.get(), copyData, count)) {
229         return false;
230     }
231 
232     this->trackCommandBufferResource(std::move(texture));
233 
234     SkDEBUGCODE(fHasWork = true;)
235 
236     return true;
237 }
238 
copyTextureToTexture(sk_sp<Texture> src,SkIRect srcRect,sk_sp<Texture> dst,SkIPoint dstPoint,int mipLevel)239 bool CommandBuffer::copyTextureToTexture(sk_sp<Texture> src,
240                                          SkIRect srcRect,
241                                          sk_sp<Texture> dst,
242                                          SkIPoint dstPoint,
243                                          int mipLevel) {
244     SkASSERT(src);
245     SkASSERT(dst);
246     if (src->textureInfo().isProtected() == Protected::kYes &&
247         dst->textureInfo().isProtected() != Protected::kYes) {
248         SKGPU_LOG_E("Can't copy from protected memory to non-protected");
249         return false;
250     }
251 
252     if (!this->onCopyTextureToTexture(src.get(), srcRect, dst.get(), dstPoint, mipLevel)) {
253         return false;
254     }
255 
256     this->trackCommandBufferResource(std::move(src));
257     this->trackCommandBufferResource(std::move(dst));
258 
259     SkDEBUGCODE(fHasWork = true;)
260 
261     return true;
262 }
263 
synchronizeBufferToCpu(sk_sp<Buffer> buffer)264 bool CommandBuffer::synchronizeBufferToCpu(sk_sp<Buffer> buffer) {
265     SkASSERT(buffer);
266 
267     bool didResultInWork = false;
268     if (!this->onSynchronizeBufferToCpu(buffer.get(), &didResultInWork)) {
269         return false;
270     }
271 
272     if (didResultInWork) {
273         this->trackResource(std::move(buffer));
274         SkDEBUGCODE(fHasWork = true;)
275     }
276 
277     return true;
278 }
279 
clearBuffer(const Buffer * buffer,size_t offset,size_t size)280 bool CommandBuffer::clearBuffer(const Buffer* buffer, size_t offset, size_t size) {
281     SkASSERT(buffer);
282 
283     if (!this->onClearBuffer(buffer, offset, size)) {
284         return false;
285     }
286 
287     SkDEBUGCODE(fHasWork = true;)
288 
289     return true;
290 }
291 
292 } // namespace skgpu::graphite
293