2 * Copyright 2021 Google LLC
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/gpu/graphite/CommandBuffer.h"
10 #include "src/core/SkTraceEvent.h"
11 #include "src/gpu/RefCntedCallback.h"
12 #include "src/gpu/graphite/Buffer.h"
13 #include "src/gpu/graphite/GraphicsPipeline.h"
14 #include "src/gpu/graphite/Sampler.h"
15 #include "src/gpu/graphite/Texture.h"
16 #include "src/gpu/graphite/TextureProxy.h"
18 namespace skgpu::graphite {
20 CommandBuffer::CommandBuffer() {}
22 CommandBuffer::~CommandBuffer() {
23 this->releaseResources();
26 void CommandBuffer::releaseResources() {
27 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
29 fTrackedResources.reset();
32 void CommandBuffer::trackResource(sk_sp<Resource> resource) {
33 fTrackedResources.push_back(std::move(resource));
36 void CommandBuffer::addFinishedProc(sk_sp<RefCntedCallback> finishedProc) {
37 fFinishedProcs.push_back(std::move(finishedProc));
40 void CommandBuffer::callFinishedProcs(bool success) {
42 for (int i = 0; i < fFinishedProcs.count(); ++i) {
43 fFinishedProcs[i]->setFailureResult();
46 fFinishedProcs.reset();
49 bool CommandBuffer::beginRenderPass(const RenderPassDesc& renderPassDesc,
50 sk_sp<Texture> colorTexture,
51 sk_sp<Texture> resolveTexture,
52 sk_sp<Texture> depthStencilTexture) {
53 if (!this->onBeginRenderPass(renderPassDesc, colorTexture.get(), resolveTexture.get(),
54 depthStencilTexture.get())) {
59 this->trackResource(std::move(colorTexture));
62 this->trackResource(std::move(resolveTexture));
64 if (depthStencilTexture) {
65 this->trackResource(std::move(depthStencilTexture));
68 if (renderPassDesc.fColorAttachment.fLoadOp == LoadOp::kClear &&
69 (renderPassDesc.fColorAttachment.fStoreOp == StoreOp::kStore ||
70 renderPassDesc.fColorResolveAttachment.fStoreOp == StoreOp::kStore)) {
78 void CommandBuffer::bindGraphicsPipeline(sk_sp<GraphicsPipeline> graphicsPipeline) {
79 this->onBindGraphicsPipeline(graphicsPipeline.get());
80 this->trackResource(std::move(graphicsPipeline));
83 void CommandBuffer::bindUniformBuffer(UniformSlot slot,
84 sk_sp<Buffer> uniformBuffer,
86 this->onBindUniformBuffer(slot, uniformBuffer.get(), offset);
87 this->trackResource(std::move(uniformBuffer));
90 void CommandBuffer::bindVertexBuffers(sk_sp<Buffer> vertexBuffer, size_t vertexOffset,
91 sk_sp<Buffer> instanceBuffer, size_t instanceOffset) {
92 this->onBindVertexBuffers(vertexBuffer.get(), vertexOffset,
93 instanceBuffer.get(), instanceOffset);
95 this->trackResource(std::move(vertexBuffer));
98 this->trackResource(std::move(instanceBuffer));
102 void CommandBuffer::bindIndexBuffer(sk_sp<Buffer> indexBuffer, size_t bufferOffset) {
103 this->onBindIndexBuffer(indexBuffer.get(), bufferOffset);
105 this->trackResource(std::move(indexBuffer));
109 void CommandBuffer::bindDrawBuffers(BindBufferInfo vertices,
110 BindBufferInfo instances,
111 BindBufferInfo indices) {
112 this->bindVertexBuffers(sk_ref_sp(vertices.fBuffer), vertices.fOffset,
113 sk_ref_sp(instances.fBuffer), instances.fOffset);
114 this->bindIndexBuffer(sk_ref_sp(indices.fBuffer), indices.fOffset);
117 void CommandBuffer::bindTextureAndSampler(sk_sp<Texture> texture,
118 sk_sp<Sampler> sampler,
120 this->onBindTextureAndSampler(texture, sampler, bindIndex);
121 this->trackResource(std::move(texture));
122 this->trackResource(std::move(sampler));
125 bool CommandBuffer::copyTextureToBuffer(sk_sp<Texture> texture,
127 sk_sp<Buffer> buffer,
129 size_t bufferRowBytes) {
133 if (!this->onCopyTextureToBuffer(texture.get(), srcRect, buffer.get(), bufferOffset,
138 this->trackResource(std::move(texture));
139 this->trackResource(std::move(buffer));
141 SkDEBUGCODE(fHasWork = true;)
146 bool CommandBuffer::copyBufferToTexture(const Buffer* buffer,
147 sk_sp<Texture> texture,
148 const BufferTextureCopyData* copyData,
152 SkASSERT(count > 0 && copyData);
154 if (!this->onCopyBufferToTexture(buffer, texture.get(), copyData, count)) {
158 this->trackResource(std::move(texture));
160 SkDEBUGCODE(fHasWork = true;)
165 } // namespace skgpu::graphite