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