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