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