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