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