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