Debian Codegraph @

src/gpu/graphite/task/CopyTask.cpp

raw
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/task/CopyTask.h"
9
10 #include "include/private/base/SkAssert.h"
11 #include "src/gpu/graphite/Buffer.h"
12 #include "src/gpu/graphite/CommandBuffer.h"
13 #include "src/gpu/graphite/Log.h"
14 #include "src/gpu/graphite/Texture.h" // IWYU pragma: keep
15 #include "src/gpu/graphite/TextureProxy.h"
16
17 #include <utility>
18
19 namespace skgpu::graphite {
20
21 sk_sp<CopyBufferToBufferTask> CopyBufferToBufferTask::Make(const Buffer* srcBuffer,
22 size_t srcOffset,
23 sk_sp<Buffer> dstBuffer,
24 size_t dstOffset,
25 size_t size) {
26 SkASSERT(srcBuffer);
27 SkASSERT(size <= srcBuffer->size() - srcOffset);
28 SkASSERT(dstBuffer);
29 SkASSERT(size <= dstBuffer->size() - dstOffset);
30 return sk_sp<CopyBufferToBufferTask>(new CopyBufferToBufferTask(srcBuffer,
31 srcOffset,
32 std::move(dstBuffer),
33 dstOffset,
34 size));
35 }
36
37 CopyBufferToBufferTask::CopyBufferToBufferTask(const Buffer* srcBuffer,
38 size_t srcOffset,
39 sk_sp<Buffer> dstBuffer,
40 size_t dstOffset,
41 size_t size)
42 : fSrcBuffer(srcBuffer)
43 , fSrcOffset(srcOffset)
44 , fDstBuffer(std::move(dstBuffer))
45 , fDstOffset(dstOffset)
46 , fSize(size) {}
47
48 CopyBufferToBufferTask::~CopyBufferToBufferTask() = default;
49
50 Task::Status CopyBufferToBufferTask::prepareResources(ResourceProvider*,
51 ScratchResourceManager*,
52 sk_sp<const RuntimeEffectDictionary>) {
53 return Status::kSuccess;
54 }
55
56 Task::Status CopyBufferToBufferTask::addCommands(Context*,
57 CommandBuffer* commandBuffer,
58 ReplayTargetData) {
59 if (commandBuffer->copyBufferToBuffer(fSrcBuffer, fSrcOffset, fDstBuffer, fDstOffset, fSize)) {
60 return Status::kSuccess;
61 } else {
62 return Status::kFail;
63 }
64 }
65
66 sk_sp<CopyTextureToBufferTask> CopyTextureToBufferTask::Make(sk_sp<TextureProxy> textureProxy,
67 SkIRect srcRect,
68 sk_sp<Buffer> buffer,
69 size_t bufferOffset,
70 size_t bufferRowBytes) {
71 if (!textureProxy) {
72 return nullptr;
73 }
74 return sk_sp<CopyTextureToBufferTask>(new CopyTextureToBufferTask(std::move(textureProxy),
75 srcRect,
76 std::move(buffer),
77 bufferOffset,
78 bufferRowBytes));
79 }
80
81 CopyTextureToBufferTask::CopyTextureToBufferTask(sk_sp<TextureProxy> textureProxy,
82 SkIRect srcRect,
83 sk_sp<Buffer> buffer,
84 size_t bufferOffset,
85 size_t bufferRowBytes)
86 : fTextureProxy(std::move(textureProxy))
87 , fSrcRect(srcRect)
88 , fBuffer(std::move(buffer))
89 , fBufferOffset(bufferOffset)
90 , fBufferRowBytes(bufferRowBytes) {
91 }
92
93 CopyTextureToBufferTask::~CopyTextureToBufferTask() {}
94
95 Task::Status CopyTextureToBufferTask::prepareResources(ResourceProvider* resourceProvider,
96 ScratchResourceManager*,
97 sk_sp<const RuntimeEffectDictionary>) {
98 // If the source texture hasn't been instantiated yet, it means there was no prior task that
99 // could have initialized its contents so a readback to a buffer does not make sense.
100 SkASSERT(fTextureProxy->isInstantiated() || fTextureProxy->isLazy());
101 // TODO: The copy is also a consumer of the source, so it should participate in returning
102 // scratch resources like RenderPassTask does. For now, though, all copy tasks side step reuse
103 // entirely and they cannot participate until they've been moved into scoping tasks like
104 // DrawTask first.
105 return Status::kSuccess;
106 }
107
108 Task::Status CopyTextureToBufferTask::addCommands(Context*,
109 CommandBuffer* commandBuffer,
110 ReplayTargetData) {
111 if (commandBuffer->copyTextureToBuffer(fTextureProxy->refTexture(),
112 fSrcRect,
113 std::move(fBuffer),
114 fBufferOffset,
115 fBufferRowBytes)) {
116 // TODO(b/332681367): CopyTextureToBuffer is currently only used for readback operations,
117 // which are a one-time event. Should this just default to returning kDiscard?
118 return Status::kSuccess;
119 } else {
120 return Status::kFail;
121 }
122 }
123
124 //--------------------------------------------------------------------------------------------------
125
126 sk_sp<CopyTextureToTextureTask> CopyTextureToTextureTask::Make(sk_sp<TextureProxy> srcProxy,
127 SkIRect srcRect,
128 sk_sp<TextureProxy> dstProxy,
129 SkIPoint dstPoint,
130 int dstLevel) {
131 if (!srcProxy || !dstProxy) {
132 return nullptr;
133 }
134 return sk_sp<CopyTextureToTextureTask>(new CopyTextureToTextureTask(std::move(srcProxy),
135 srcRect,
136 std::move(dstProxy),
137 dstPoint,
138 dstLevel));
139 }
140
141 CopyTextureToTextureTask::CopyTextureToTextureTask(sk_sp<TextureProxy> srcProxy,
142 SkIRect srcRect,
143 sk_sp<TextureProxy> dstProxy,
144 SkIPoint dstPoint,
145 int dstLevel)
146 : fSrcProxy(std::move(srcProxy))
147 , fSrcRect(srcRect)
148 , fDstProxy(std::move(dstProxy))
149 , fDstPoint(dstPoint)
150 , fDstLevel(dstLevel) {}
151
152 CopyTextureToTextureTask::~CopyTextureToTextureTask() {}
153
154 Task::Status CopyTextureToTextureTask::prepareResources(ResourceProvider* resourceProvider,
155 ScratchResourceManager*,
156 sk_sp<const RuntimeEffectDictionary>) {
157 // Do not instantiate the src proxy. If the source texture hasn't been instantiated yet, it
158 // means there was no prior task that could have initialized its contents so propagating the
159 // undefined contents to the dst does not make sense.
160 // TODO(b/333729316): Assert that fSrcProxy is instantiated or lazy; right now it may not be
161 // instantatiated if this is a dst readback copy for a scratch Device. In that case, a
162 // RenderPassTask will immediately follow this copy task and instantiate the source proxy so
163 // that addCommands() has a texture to operate on. That said, the texture's contents will be
164 // undefined when the copy is executed ideally it just shouldn't happen.
165
166 // TODO: The copy is also a consumer of the source, so it should participate in returning
167 // scratch resources like RenderPassTask does. For now, though, all copy tasks side step reuse
168 // entirely and they cannot participate until they've been moved into scoping tasks like
169 // DrawTask first. In particular, for texture-to-texture copies, they should be scoped to not
170 // invoke pending listeners for a subsequent RenderPassTask.
171
172 // TODO: Use the scratch resource manager to instantiate fDstProxy, although the details of when
173 // that texture can be returned need to be worked out. While brittle, all current use cases
174 // of scratch texture-to-texture copies have the dst used immediately by the next task, so it
175 // could just add a pending listener that returns the texture w/o any read counting.
176 if (!TextureProxy::InstantiateIfNotLazy(resourceProvider, fDstProxy.get())) {
177 SKGPU_LOG_E("Could not instantiate dst texture proxy for CopyTextureToTextureTask!");
178 return Status::kFail;
179 }
180 return Status::kSuccess;
181 }
182
183 Task::Status CopyTextureToTextureTask::addCommands(Context*,
184 CommandBuffer* commandBuffer,
185 ReplayTargetData) {
186 // prepareResources() doesn't instantiate the source assuming that a prior task will have do so
187 // as part of initializing the texture contents.
188 SkASSERT(fSrcProxy->isInstantiated());
189 if (commandBuffer->copyTextureToTexture(fSrcProxy->refTexture(),
190 fSrcRect,
191 fDstProxy->refTexture(),
192 fDstPoint,
193 fDstLevel)) {
194 // TODO(b/332681367): The calling context should be able to specify whether or not this copy
195 // is a repeatable operation (e.g. dst readback copy for blending) or one time (e.g. client
196 // asked for a copy of an image or surface).
197 return Status::kSuccess;
198 } else {
199 return Status::kFail;
200 }
201 }
202
203 } // namespace skgpu::graphite