Debian Codegraph @

src/gpu/graphite/Recording.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 #include "include/gpu/graphite/Recording.h"
8
9 #include "include/core/SkRect.h"
10 #include "include/core/SkSize.h"
11 #include "include/gpu/GpuTypes.h"
12 #include "include/gpu/graphite/GraphiteTypes.h"
13 #include "include/gpu/graphite/TextureInfo.h"
14 #include "include/private/base/SkAssert.h"
15 #include "include/private/base/SkPoint_impl.h"
16 #include "include/private/base/SkTo.h"
17 #include "src/core/SkChecksum.h"
18 #include "src/gpu/RefCntedCallback.h"
19 #include "src/gpu/graphite/CommandBuffer.h"
20 #include "src/gpu/graphite/Log.h"
21 #include "src/gpu/graphite/RecordingPriv.h"
22 #include "src/gpu/graphite/Resource.h"
23 #include "src/gpu/graphite/RuntimeEffectDictionary.h"
24 #include "src/gpu/graphite/Surface_Graphite.h"
25 #include "src/gpu/graphite/Texture.h"
26 #include "src/gpu/graphite/TextureProxy.h"
27 #include "src/gpu/graphite/task/Task.h"
28 #include "src/gpu/graphite/task/TaskList.h"
29
30 #include <functional>
31 #include <unordered_map>
32 #include <unordered_set>
33 #include <utility>
34
35 namespace skgpu::graphite { class Context; }
36
37 using namespace skia_private;
38
39 namespace skgpu::graphite {
40
41 Recording::Recording(uint32_t uniqueID,
42 uint32_t recorderID,
43 std::unique_ptr<LazyProxyData> targetProxyData,
44 TArray<sk_sp<RefCntedCallback>>&& finishedProcs)
45 : fUniqueID(uniqueID)
46 , fRecorderID(recorderID)
47 , fRootTaskList(new TaskList)
48 , fTargetProxyData(std::move(targetProxyData))
49 , fFinishedProcs(std::move(finishedProcs)) {}
50
51 Recording::~Recording() {
52 // Any finished procs that haven't been passed to a CommandBuffer fail
53 this->priv().setFailureResultForFinishedProcs();
54 }
55
56 std::size_t Recording::ProxyHash::operator()(const sk_sp<TextureProxy> &proxy) const {
57 return SkGoodHash()(proxy.get());
58 }
59
60 Recording::LazyProxyData::LazyProxyData(const Caps* caps,
61 SkISize dimensions,
62 const TextureInfo& textureInfo) {
63 auto onInstantiate = [this](ResourceProvider*) {
64 SkASSERT(SkToBool(fTarget));
65 return std::move(fTarget);
66 };
67
68 // If the texture info specifies that mipmapping is required, that implies that the final
69 // surface used to instantiate this proxy will be mipmapped, and that the dimensions of that
70 // surface are known already.
71 fTargetProxy = textureInfo.mipmapped() == Mipmapped::kYes
72 ? TextureProxy::MakeLazy(caps,
73 dimensions,
74 textureInfo,
75 skgpu::Budgeted::kNo,
76 Volatile::kYes,
77 std::move(onInstantiate))
78 : TextureProxy::MakeFullyLazy(textureInfo,
79 skgpu::Budgeted::kNo,
80 Volatile::kYes,
81 std::move(onInstantiate));
82 }
83
84 Recording::LazyProxyData::~LazyProxyData() = default;
85
86 TextureProxy* Recording::LazyProxyData::lazyProxy() { return fTargetProxy.get(); }
87
88 sk_sp<TextureProxy> Recording::LazyProxyData::refLazyProxy() { return fTargetProxy; }
89
90 bool Recording::LazyProxyData::lazyInstantiate(ResourceProvider* resourceProvider,
91 sk_sp<Texture> texture) {
92 fTarget = std::move(texture);
93 return fTargetProxy->lazyInstantiate(resourceProvider);
94 }
95
96 ////////////////////////////////////////////////////////////////////////////////
97 bool RecordingPriv::hasNonVolatileLazyProxies() const {
98 return !fRecording->fNonVolatileLazyProxies.empty();
99 }
100
101 bool RecordingPriv::instantiateNonVolatileLazyProxies(ResourceProvider* resourceProvider) {
102 SkASSERT(this->hasNonVolatileLazyProxies());
103
104 for (const auto& proxy : fRecording->fNonVolatileLazyProxies) {
105 if (!proxy->lazyInstantiate(resourceProvider)) {
106 return false;
107 }
108 }
109
110 // Note: once all the lazy proxies have been instantiated, that's it - there are no more
111 // chances to instantiate.
112 fRecording->fNonVolatileLazyProxies.clear();
113 return true;
114 }
115
116 bool RecordingPriv::hasVolatileLazyProxies() const {
117 return !fRecording->fVolatileLazyProxies.empty();
118 }
119
120 bool RecordingPriv::instantiateVolatileLazyProxies(ResourceProvider* resourceProvider) {
121 SkASSERT(this->hasVolatileLazyProxies());
122
123 for (const auto& proxy : fRecording->fVolatileLazyProxies) {
124 if (!proxy->lazyInstantiate(resourceProvider)) {
125 return false;
126 }
127 }
128
129 return true;
130 }
131
132 void RecordingPriv::deinstantiateVolatileLazyProxies() {
133 if (!this->hasVolatileLazyProxies()) {
134 return;
135 }
136
137 for (const auto& proxy : fRecording->fVolatileLazyProxies) {
138 SkASSERT(proxy->isVolatile());
139 proxy->deinstantiate();
140 }
141 }
142
143 void RecordingPriv::setFailureResultForFinishedProcs() {
144 for (int i = 0; i < fRecording->fFinishedProcs.size(); ++i) {
145 fRecording->fFinishedProcs[i]->setFailureResult();
146 }
147 fRecording->fFinishedProcs.clear();
148 }
149
150 TextureProxy* RecordingPriv::deferredTargetProxy() {
151 return fRecording->fTargetProxyData ? fRecording->fTargetProxyData->lazyProxy() : nullptr;
152 }
153
154 const Texture* RecordingPriv::setupDeferredTarget(ResourceProvider* resourceProvider,
155 Surface* targetSurface,
156 SkIVector targetTranslation,
157 SkIRect targetClip) {
158 SkASSERT(targetSurface && fRecording->fTargetProxyData);
159
160 TextureProxy* surfaceTexture = targetSurface->backingTextureProxy();
161 SkASSERT(surfaceTexture->isInstantiated());
162
163 const TextureProxy* targetProxy = fRecording->fTargetProxyData->lazyProxy();
164 if (surfaceTexture->mipmapped() != targetProxy->mipmapped()) {
165 SKGPU_LOG_E("Deferred canvas mipmap settings don't match instantiating target's.");
166 return nullptr;
167 }
168
169 // If the deferred canvas's texture proxy is not fully lazy, that means we used it for draws
170 // that require specific dimensions and no translation. The only time this happens is when a
171 // client requests a mipmapped deferred canvas and we automatically insert commands to
172 // regenerate mipmaps.
173 if (!targetProxy->isFullyLazy()) {
174 SkASSERT(targetProxy->mipmapped() == skgpu::Mipmapped::kYes);
175 if (targetProxy->dimensions() != surfaceTexture->dimensions()) {
176 SKGPU_LOG_E(
177 "Deferred canvas dimensions don't match instantiating target's dimensions.");
178 return nullptr;
179 }
180 if (!targetTranslation.isZero()) {
181 SKGPU_LOG_E(
182 "Replay translation is not allowed when replaying draws to a mipmapped "
183 "deferred canvas.");
184 return nullptr;
185 }
186 if (!targetClip.isEmpty()) {
187 SKGPU_LOG_E(
188 "Replay clip is not allowed when replaying draws to a mipmapped deferred "
189 "canvas.");
190 return nullptr;
191 }
192 }
193
194 if (!fRecording->fTargetProxyData->lazyInstantiate(resourceProvider,
195 surfaceTexture->refTexture())) {
196 SKGPU_LOG_E("Could not instantiate deferred texture proxy.");
197 return nullptr;
198 }
199 return surfaceTexture->texture();
200 }
201
202 bool RecordingPriv::prepareResources(ResourceProvider* resourceProvider,
203 ScratchResourceManager* scratchManager,
204 sk_sp<const RuntimeEffectDictionary> rteDict) {
205 Task::Status status = fRecording->fRootTaskList->prepareResources(
206 resourceProvider, scratchManager, rteDict);
207 if (status == Task::Status::kSuccess) {
208 fRecording->fRootTaskList->visitProxies([&](const TextureProxy* proxy) {
209 if (proxy->isLazy()) {
210 if (proxy->isVolatile()) {
211 fRecording->fVolatileLazyProxies.insert(sk_ref_sp(proxy));
212 } else {
213 fRecording->fNonVolatileLazyProxies.insert(sk_ref_sp(proxy));
214 }
215 }
216 return true;
217 }, /*readsOnly=*/false);
218 }
219
220 return status != Task::Status::kFail;
221 }
222
223 bool RecordingPriv::addCommands(Context* context,
224 CommandBuffer* commandBuffer,
225 const Texture* replayTarget,
226 SkIVector targetTranslation,
227 SkIRect targetClip) {
228 for (size_t i = 0; i < fRecording->fExtraResourceRefs.size(); ++i) {
229 commandBuffer->trackCommandBufferResource(fRecording->fExtraResourceRefs[i]);
230 }
231
232 // There's no need to differentiate kSuccess and kDiscard at the root list level; if every task
233 // is discarded, the Recording will automatically be a no-op on replay while still correctly
234 // notifying any finish procs the client may have added.
235 if (fRecording->fRootTaskList->addCommands(
236 context, commandBuffer, {replayTarget, targetTranslation, targetClip}) ==
237 Task::Status::kFail) {
238 return false;
239 }
240 for (int i = 0; i < fRecording->fFinishedProcs.size(); ++i) {
241 commandBuffer->addFinishedProc(std::move(fRecording->fFinishedProcs[i]));
242 }
243 fRecording->fFinishedProcs.clear();
244
245 return true;
246 }
247
248 void RecordingPriv::addResourceRef(sk_sp<Resource> resource) {
249 fRecording->fExtraResourceRefs.push_back(std::move(resource));
250 }
251
252 #if defined(GPU_TEST_UTILS)
253 bool RecordingPriv::isTargetProxyInstantiated() const {
254 return fRecording->fTargetProxyData->lazyProxy()->isInstantiated();
255 }
256
257 int RecordingPriv::numVolatilePromiseImages() const {
258 return fRecording->fVolatileLazyProxies.size();
259 }
260
261 int RecordingPriv::numNonVolatilePromiseImages() const {
262 return fRecording->fNonVolatileLazyProxies.size();
263 }
264
265 bool RecordingPriv::hasTasks() const {
266 return fRecording->fRootTaskList->hasTasks();
267 }
268 #endif
269
270 } // namespace skgpu::graphite