Debian Codegraph @

src/gpu/ganesh/GrBackendTextureImageGenerator.cpp

raw
1 /*
2 * Copyright 2017 Google Inc.
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 "src/gpu/ganesh/GrBackendTextureImageGenerator.h"
8
9 #include "include/core/SkColorSpace.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkSize.h"
13 #include "include/gpu/GpuTypes.h"
14 #include "include/gpu/ganesh/GrDirectContext.h"
15 #include "include/gpu/ganesh/GrRecordingContext.h"
16 #include "include/gpu/ganesh/GrTypes.h"
17 #include "include/private/base/SkAssert.h"
18 #include "include/private/gpu/ganesh/GrTypesPriv.h"
19 #include "src/gpu/RefCntedCallback.h"
20 #include "src/gpu/SkBackingFit.h"
21 #include "src/gpu/Swizzle.h"
22 #include "src/gpu/ganesh/GrCaps.h"
23 #include "src/gpu/ganesh/GrDirectContextPriv.h"
24 #include "src/gpu/ganesh/GrGpu.h"
25 #include "src/gpu/ganesh/GrGpuResourcePriv.h"
26 #include "src/gpu/ganesh/GrProxyProvider.h"
27 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
28 #include "src/gpu/ganesh/GrResourceCache.h"
29 #include "src/gpu/ganesh/GrResourceProvider.h"
30 #include "src/gpu/ganesh/GrResourceProviderPriv.h"
31 #include "src/gpu/ganesh/GrSemaphore.h"
32 #include "src/gpu/ganesh/GrSurface.h"
33 #include "src/gpu/ganesh/GrSurfaceProxy.h"
34 #include "src/gpu/ganesh/GrTexture.h"
35 #include "src/gpu/ganesh/GrTextureProxy.h"
36 #include "src/gpu/ganesh/SkGaneshRecorder.h"
37 #include "src/gpu/ganesh/SkGr.h"
38
39 #include <functional>
40 #include <utility>
41
42 GrBackendTextureImageGenerator::RefHelper::RefHelper(
43 sk_sp<GrTexture> texture,
44 GrDirectContext::DirectContextID owningContextID,
45 std::unique_ptr<GrSemaphore> semaphore)
46 : fOriginalTexture(std::move(texture))
47 , fOwningContextID(owningContextID)
48 , fBorrowingContextReleaseProc(nullptr)
49 , fSemaphore(std::move(semaphore)) {}
50
51 GrBackendTextureImageGenerator::RefHelper::~RefHelper() {
52 SkASSERT(!fBorrowingContextID.isValid());
53 // Generator has been freed, and no one is borrowing the texture. Notify the original cache
54 // that it can free the last ref, so it happens on the correct thread.
55 GrResourceCache::ReturnResourceFromThread(std::move(fOriginalTexture), fOwningContextID);
56 }
57
58 std::unique_ptr<GrTextureGenerator>
59 GrBackendTextureImageGenerator::Make(const sk_sp<GrTexture>& texture,
60 GrSurfaceOrigin origin,
61 std::unique_ptr<GrSemaphore> semaphore,
62 SkColorType colorType,
63 SkAlphaType alphaType,
64 sk_sp<SkColorSpace> colorSpace) {
65 GrDirectContext* dContext = texture->getContext();
66
67 if (!dContext->priv().caps()->areColorTypeAndFormatCompatible(
68 SkColorTypeToGrColorType(colorType), texture->backendFormat())) {
69 return nullptr;
70 }
71
72 SkColorInfo info(colorType, alphaType, std::move(colorSpace));
73 return std::unique_ptr<GrTextureGenerator>(new GrBackendTextureImageGenerator(
74 info,
75 texture,
76 origin,
77 dContext->directContextID(),
78 std::move(semaphore)));
79 }
80
81 GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(
82 const SkColorInfo& info,
83 const sk_sp<GrTexture>& texture,
84 GrSurfaceOrigin origin,
85 GrDirectContext::DirectContextID owningContextID,
86 std::unique_ptr<GrSemaphore> semaphore)
87 : INHERITED(SkImageInfo::Make(texture->dimensions(), info))
88 , fRefHelper(new RefHelper(texture, owningContextID, std::move(semaphore)))
89 , fBackendTexture(texture->getBackendTexture())
90 , fSurfaceOrigin(origin) {}
91
92 GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
93 fRefHelper->unref();
94 }
95
96 bool GrBackendTextureImageGenerator::onIsProtected() const {
97 return fBackendTexture.isProtected();
98 }
99
100 bool GrBackendTextureImageGenerator::onIsValid(SkRecorder* recorder) const {
101 auto gRecorder = AsGaneshRecorder(recorder);
102 if (!gRecorder) {
103 return false;
104 }
105 return !gRecorder->recordingContext()->abandoned();
106 }
107
108 ///////////////////////////////////////////////////////////////////////////////////////////////////
109
110 void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
111 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
112 SkASSERT(refHelper);
113
114 refHelper->fBorrowingContextReleaseProc = nullptr;
115 refHelper->fBorrowingContextID.makeInvalid();
116 refHelper->unref();
117 }
118
119 GrSurfaceProxyView GrBackendTextureImageGenerator::onGenerateTexture(
120 GrRecordingContext* rContext,
121 const SkImageInfo& info,
122 skgpu::Mipmapped mipmapped,
123 GrImageTexGenPolicy texGenPolicy) {
124 SkASSERT(rContext);
125 SkASSERT_RELEASE(info.dimensions() == fBackendTexture.dimensions());
126
127 // We currently limit GrBackendTextureImageGenerators to direct contexts since
128 // only Flutter uses them and doesn't use recording/DDL contexts. Ideally, the
129 // cross context texture functionality can be subsumed by the thread-safe cache
130 // working with utility contexts.
131 auto dContext = rContext->asDirectContext();
132 if (!dContext) {
133 return {};
134 }
135
136 if (dContext->backend() != fBackendTexture.backend()) {
137 return {};
138 }
139 if (info.colorType() != this->getInfo().colorType()) {
140 return {};
141 }
142
143 auto proxyProvider = dContext->priv().proxyProvider();
144
145 fBorrowingMutex.acquire();
146 sk_sp<skgpu::RefCntedCallback> releaseProcHelper;
147 if (fRefHelper->fBorrowingContextID.isValid()) {
148 if (fRefHelper->fBorrowingContextID != dContext->directContextID()) {
149 fBorrowingMutex.release();
150 rContext->priv().printWarningMessage(
151 "GrBackendTextureImageGenerator: Trying to use texture on two GrContexts!\n");
152 return {};
153 } else {
154 SkASSERT(fRefHelper->fBorrowingContextReleaseProc);
155 // Ref the release proc to be held by the proxy we make below
156 releaseProcHelper = sk_ref_sp(fRefHelper->fBorrowingContextReleaseProc);
157 }
158 } else {
159 SkASSERT(!fRefHelper->fBorrowingContextReleaseProc);
160 // The ref we add to fRefHelper here will be passed into and owned by the
161 // skgpu::RefCntedCallback.
162 fRefHelper->ref();
163 releaseProcHelper =
164 skgpu::RefCntedCallback::Make(ReleaseRefHelper_TextureReleaseProc, fRefHelper);
165 fRefHelper->fBorrowingContextReleaseProc = releaseProcHelper.get();
166 }
167 fRefHelper->fBorrowingContextID = dContext->directContextID();
168 if (!fRefHelper->fBorrowedTextureKey.isValid()) {
169 static const auto kDomain = skgpu::UniqueKey::GenerateDomain();
170 skgpu::UniqueKey::Builder builder(&fRefHelper->fBorrowedTextureKey, kDomain, 1);
171 builder[0] = this->uniqueID();
172 }
173 fBorrowingMutex.release();
174
175 SkASSERT(fRefHelper->fBorrowingContextID == dContext->directContextID());
176
177 GrBackendFormat backendFormat = fBackendTexture.getBackendFormat();
178 SkASSERT(backendFormat.isValid());
179
180 GrColorType grColorType = SkColorTypeToGrColorType(info.colorType());
181
182 skgpu::Mipmapped textureIsMipMapped =
183 fBackendTexture.hasMipmaps() ? skgpu::Mipmapped::kYes : skgpu::Mipmapped::kNo;
184
185 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
186 // mipmaps are fully fleshed out.
187 GrMipmapStatus mipmapStatus = fBackendTexture.hasMipmaps()
188 ? GrMipmapStatus::kValid : GrMipmapStatus::kNotAllocated;
189
190 skgpu::Swizzle readSwizzle = dContext->priv().caps()->getReadSwizzle(backendFormat,
191 grColorType);
192
193 // Must make copies of member variables to capture in the lambda since this image generator may
194 // be deleted before we actually execute the lambda.
195 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
196 [refHelper = fRefHelper, releaseProcHelper, backendTexture = fBackendTexture](
197 GrResourceProvider* resourceProvider,
198 const GrSurfaceProxy::LazySurfaceDesc&) -> GrSurfaceProxy::LazyCallbackResult {
199 if (refHelper->fSemaphore) {
200 resourceProvider->priv().gpu()->waitSemaphore(refHelper->fSemaphore.get());
201 }
202
203 // If a client re-draws the same image multiple times, the texture we return
204 // will be cached and re-used. If they draw a subset, though, we may be
205 // re-called. In that case, we want to re-use the borrowed texture we've
206 // previously created.
207 sk_sp<GrTexture> tex;
208 SkASSERT(refHelper->fBorrowedTextureKey.isValid());
209 auto surf = resourceProvider->findByUniqueKey<GrSurface>(
210 refHelper->fBorrowedTextureKey);
211 if (surf) {
212 SkASSERT(surf->asTexture());
213 tex = sk_ref_sp(surf->asTexture());
214 } else {
215 // We just gained access to the texture. If we're on the original
216 // context, we could use the original texture, but we'd have no way of
217 // detecting that it's no longer in-use. So we always make a wrapped
218 // copy, where the release proc informs us that the context is done with
219 // it. This is unfortunate - we'll have two texture objects referencing
220 // the same GPU object. However, no client can ever see the original
221 // texture, so this should be safe. We make the texture uncacheable so
222 // that the release proc is called ASAP.
223 tex = resourceProvider->wrapBackendTexture(
224 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
225 kRead_GrIOType);
226 if (!tex) {
227 return {};
228 }
229 tex->setRelease(releaseProcHelper);
230 tex->resourcePriv().setUniqueKey(refHelper->fBorrowedTextureKey);
231 }
232 // We use keys to avoid re-wrapping the GrBackendTexture in a GrTexture.
233 // This is unrelated to the whatever SkImage key may be assigned to the
234 // proxy.
235 return {std::move(tex), true, GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced};
236 },
237 backendFormat,
238 fBackendTexture.dimensions(),
239 textureIsMipMapped,
240 mipmapStatus,
241 GrInternalSurfaceFlags::kReadOnly,
242 SkBackingFit::kExact,
243 skgpu::Budgeted::kNo,
244 GrProtected::kNo,
245 GrSurfaceProxy::UseAllocator::kYes,
246 "BackendTextureImageGenerator");
247 if (!proxy) {
248 return {};
249 }
250
251 if (texGenPolicy == GrImageTexGenPolicy::kDraw &&
252 (mipmapped == skgpu::Mipmapped::kNo || proxy->mipmapped() == skgpu::Mipmapped::kYes)) {
253 // If we have the correct mip support, we're done
254 return GrSurfaceProxyView(std::move(proxy), fSurfaceOrigin, readSwizzle);
255 } else {
256 skgpu::Budgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
257 ? skgpu::Budgeted::kNo
258 : skgpu::Budgeted::kYes;
259
260 auto copy = GrSurfaceProxy::Copy(dContext,
261 std::move(proxy),
262 fSurfaceOrigin,
263 mipmapped,
264 SkIRect::MakeWH(info.width(), info.height()),
265 SkBackingFit::kExact,
266 budgeted,
267 /*label=*/"BackendTextureImageGenerator_GenerateTexture");
268 return {std::move(copy), fSurfaceOrigin, readSwizzle};
269 }
270 }