Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / gpu / graphite / CommandBuffer.cpp
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/GraphicsPipeline.h"
14 #include "src/gpu/graphite/Sampler.h"
15 #include "src/gpu/graphite/Texture.h"
16 #include "src/gpu/graphite/TextureProxy.h"
17
18 namespace skgpu::graphite {
19
20 CommandBuffer::CommandBuffer() {}
21
22 CommandBuffer::~CommandBuffer() {
23     this->releaseResources();
24 }
25
26 void CommandBuffer::releaseResources() {
27     TRACE_EVENT0("skia.gpu", TRACE_FUNC);
28
29     fTrackedResources.reset();
30 }
31
32 void CommandBuffer::trackResource(sk_sp<Resource> resource) {
33     fTrackedResources.push_back(std::move(resource));
34 }
35
36 void CommandBuffer::addFinishedProc(sk_sp<RefCntedCallback> finishedProc) {
37     fFinishedProcs.push_back(std::move(finishedProc));
38 }
39
40 void CommandBuffer::callFinishedProcs(bool success) {
41     if (!success) {
42         for (int i = 0; i < fFinishedProcs.count(); ++i) {
43             fFinishedProcs[i]->setFailureResult();
44         }
45     }
46     fFinishedProcs.reset();
47 }
48
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())) {
55         return false;
56     }
57
58     if (colorTexture) {
59         this->trackResource(std::move(colorTexture));
60     }
61     if (resolveTexture) {
62         this->trackResource(std::move(resolveTexture));
63     }
64     if (depthStencilTexture) {
65         this->trackResource(std::move(depthStencilTexture));
66     }
67 #ifdef SK_DEBUG
68     if (renderPassDesc.fColorAttachment.fLoadOp == LoadOp::kClear &&
69         (renderPassDesc.fColorAttachment.fStoreOp == StoreOp::kStore ||
70          renderPassDesc.fColorResolveAttachment.fStoreOp == StoreOp::kStore)) {
71         fHasWork = true;
72     }
73 #endif
74
75     return true;
76 }
77
78 void CommandBuffer::bindGraphicsPipeline(sk_sp<GraphicsPipeline> graphicsPipeline) {
79     this->onBindGraphicsPipeline(graphicsPipeline.get());
80     this->trackResource(std::move(graphicsPipeline));
81 }
82
83 void CommandBuffer::bindUniformBuffer(UniformSlot slot,
84                                       sk_sp<Buffer> uniformBuffer,
85                                       size_t offset) {
86     this->onBindUniformBuffer(slot, uniformBuffer.get(), offset);
87     this->trackResource(std::move(uniformBuffer));
88 }
89
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);
94     if (vertexBuffer) {
95         this->trackResource(std::move(vertexBuffer));
96     }
97     if (instanceBuffer) {
98         this->trackResource(std::move(instanceBuffer));
99     }
100 }
101
102 void CommandBuffer::bindIndexBuffer(sk_sp<Buffer> indexBuffer, size_t bufferOffset) {
103     this->onBindIndexBuffer(indexBuffer.get(), bufferOffset);
104     if (indexBuffer) {
105         this->trackResource(std::move(indexBuffer));
106     }
107 }
108
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);
115 }
116
117 void CommandBuffer::bindTextureAndSampler(sk_sp<Texture> texture,
118                                           sk_sp<Sampler> sampler,
119                                           int bindIndex) {
120     this->onBindTextureAndSampler(texture, sampler, bindIndex);
121     this->trackResource(std::move(texture));
122     this->trackResource(std::move(sampler));
123 }
124
125 bool CommandBuffer::copyTextureToBuffer(sk_sp<Texture> texture,
126                                         SkIRect srcRect,
127                                         sk_sp<Buffer> buffer,
128                                         size_t bufferOffset,
129                                         size_t bufferRowBytes) {
130     SkASSERT(texture);
131     SkASSERT(buffer);
132
133     if (!this->onCopyTextureToBuffer(texture.get(), srcRect, buffer.get(), bufferOffset,
134                                      bufferRowBytes)) {
135         return false;
136     }
137
138     this->trackResource(std::move(texture));
139     this->trackResource(std::move(buffer));
140
141     SkDEBUGCODE(fHasWork = true;)
142
143     return true;
144 }
145
146 bool CommandBuffer::copyBufferToTexture(const Buffer* buffer,
147                                         sk_sp<Texture> texture,
148                                         const BufferTextureCopyData* copyData,
149                                         int count) {
150     SkASSERT(buffer);
151     SkASSERT(texture);
152     SkASSERT(count > 0 && copyData);
153
154     if (!this->onCopyBufferToTexture(buffer, texture.get(), copyData, count)) {
155         return false;
156     }
157
158     this->trackResource(std::move(texture));
159
160     SkDEBUGCODE(fHasWork = true;)
161
162     return true;
163 }
164
165 } // namespace skgpu::graphite