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 "experimental/graphite/src/CommandBuffer.h"
9
10#include "experimental/graphite/src/GraphicsPipeline.h"
11#include "src/core/SkTraceEvent.h"
12
13#include "experimental/graphite/src/Buffer.h"
14#include "experimental/graphite/src/Texture.h"
15#include "experimental/graphite/src/TextureProxy.h"
16
17namespace skgpu {
18
19CommandBuffer::CommandBuffer() {}
20
21void CommandBuffer::releaseResources() {
22    TRACE_EVENT0("skia.gpu", TRACE_FUNC);
23
24    fTrackedResources.reset();
25}
26
27void CommandBuffer::beginRenderPass(const RenderPassDesc& renderPassDesc) {
28    this->onBeginRenderPass(renderPassDesc);
29
30    auto& colorInfo = renderPassDesc.fColorAttachment;
31    if (colorInfo.fTextureProxy) {
32        this->trackResource(colorInfo.fTextureProxy->refTexture());
33    }
34    if (colorInfo.fStoreOp == StoreOp::kStore) {
35        fHasWork = true;
36    }
37}
38
39void CommandBuffer::bindGraphicsPipeline(sk_sp<GraphicsPipeline> graphicsPipeline) {
40    this->onBindGraphicsPipeline(graphicsPipeline.get());
41    this->trackResource(std::move(graphicsPipeline));
42    fHasWork = true;
43}
44
45void CommandBuffer::bindUniformBuffer(sk_sp<Buffer> uniformBuffer, size_t offset) {
46    this->onBindUniformBuffer(uniformBuffer.get(), offset);
47    this->trackResource(std::move(uniformBuffer));
48    fHasWork = true;
49}
50
51void CommandBuffer::bindVertexBuffers(sk_sp<Buffer> vertexBuffer, size_t vertexOffset,
52                                      sk_sp<Buffer> instanceBuffer, size_t instanceOffset) {
53    this->onBindVertexBuffers(vertexBuffer.get(), vertexOffset,
54                              instanceBuffer.get(), instanceOffset);
55    if (vertexBuffer) {
56        this->trackResource(std::move(vertexBuffer));
57    }
58    if (instanceBuffer) {
59        this->trackResource(std::move(instanceBuffer));
60    }
61    fHasWork = true;
62}
63
64void CommandBuffer::bindIndexBuffer(sk_sp<Buffer> indexBuffer, size_t bufferOffset) {
65    this->onBindIndexBuffer(indexBuffer.get(), bufferOffset);
66    if (indexBuffer) {
67        this->trackResource(std::move(indexBuffer));
68    }
69    fHasWork = true;
70}
71
72static bool check_max_blit_width(int widthInPixels) {
73    if (widthInPixels > 32767) {
74        SkASSERT(false); // surfaces should not be this wide anyway
75        return false;
76    }
77    return true;
78}
79
80void CommandBuffer::copyTextureToBuffer(sk_sp<skgpu::Texture> texture,
81                                        SkIRect srcRect,
82                                        sk_sp<skgpu::Buffer> buffer,
83                                        size_t bufferOffset,
84                                        size_t bufferRowBytes) {
85    if (!check_max_blit_width(srcRect.width())) {
86        return;
87    }
88
89    this->onCopyTextureToBuffer(texture.get(), srcRect, buffer.get(), bufferOffset, bufferRowBytes);
90
91    this->trackResource(std::move(texture));
92    this->trackResource(std::move(buffer));
93
94    fHasWork = true;
95}
96
97} // namespace skgpu
98