blob: 078593fe0ea6d360b750967dc67aee9455a1c740 [file] [log] [blame]
Brian Osman13dddce2017-05-09 13:19:50 -04001/*
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
Robert Phillips4e105e22020-07-16 09:18:50 -04008#include "include/gpu/GrDirectContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -04009#include "include/gpu/GrRecordingContext.h"
Ben Wagner21bca282019-05-15 10:15:52 -040010#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrBackendTextureImageGenerator.h"
Adlai Hollera0693042020-10-14 11:23:11 -040012#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrGpu.h"
14#include "src/gpu/GrProxyProvider.h"
15#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrResourceCache.h"
17#include "src/gpu/GrResourceProvider.h"
18#include "src/gpu/GrResourceProviderPriv.h"
19#include "src/gpu/GrSemaphore.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050020#include "src/gpu/GrSurfaceDrawContext.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000021#include "src/gpu/GrTexture.h"
Adlai Holler9e2c50e2021-02-09 14:41:52 -050022#include "src/gpu/GrTextureProxyPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/SkGr.h"
24#include "src/gpu/gl/GrGLTexture.h"
Brian Osman13dddce2017-05-09 13:19:50 -040025
Robert Phillipsd074b622021-03-15 08:49:24 -040026GrBackendTextureImageGenerator::RefHelper::RefHelper(
27 GrTexture* texture,
28 GrDirectContext::DirectContextID owningContextID,
29 std::unique_ptr<GrSemaphore> semaphore)
Brian Salomonb916b7b2019-04-01 13:34:34 -040030 : fOriginalTexture(texture)
31 , fOwningContextID(owningContextID)
32 , fBorrowingContextReleaseProc(nullptr)
Greg Daniel301015c2019-11-18 14:06:46 -050033 , fSemaphore(std::move(semaphore)) {}
Brian Salomonb916b7b2019-04-01 13:34:34 -040034
Brian Osman13dddce2017-05-09 13:19:50 -040035GrBackendTextureImageGenerator::RefHelper::~RefHelper() {
Robert Phillipsd074b622021-03-15 08:49:24 -040036 SkASSERT(!fBorrowingContextID.isValid());
Brian Osman13dddce2017-05-09 13:19:50 -040037
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.
Robert Phillipsddc21482019-10-16 14:30:09 -040040 GrTextureFreedMessage msg { fOriginalTexture, fOwningContextID };
Robert Phillipsd074b622021-03-15 08:49:24 -040041 SkMessageBus<GrTextureFreedMessage, GrDirectContext::DirectContextID>::Post(msg);
Brian Osman13dddce2017-05-09 13:19:50 -040042}
43
Brian Osman13dddce2017-05-09 13:19:50 -040044std::unique_ptr<SkImageGenerator>
Robert Phillipsb0e93a22017-08-29 08:26:54 -040045GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, GrSurfaceOrigin origin,
Greg Daniel301015c2019-11-18 14:06:46 -050046 std::unique_ptr<GrSemaphore> semaphore, SkColorType colorType,
Brian Osman13dddce2017-05-09 13:19:50 -040047 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
Robert Phillipsd074b622021-03-15 08:49:24 -040048 GrDirectContext* dContext = texture->getContext();
Brian Osman13dddce2017-05-09 13:19:50 -040049
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.
Robert Phillipsd074b622021-03-15 08:49:24 -040053 dContext->priv().getResourceCache()->insertDelayedTextureUnref(texture.get());
Brian Osman13dddce2017-05-09 13:19:50 -040054
Robert Phillipsb67821d2017-12-13 15:00:45 -050055 GrBackendTexture backendTexture = texture->getBackendTexture();
Robert Phillipsc80b0e92019-07-23 10:27:09 -040056
Robert Phillipsd074b622021-03-15 08:49:24 -040057 if (!dContext->priv().caps()->areColorTypeAndFormatCompatible(
Robert Phillips62221e72019-07-24 15:07:38 -040058 SkColorTypeToGrColorType(colorType), backendTexture.getBackendFormat())) {
59 return nullptr;
Brian Osman052ef692018-03-27 09:56:31 -040060 }
Brian Osman13dddce2017-05-09 13:19:50 -040061
62 SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType,
63 std::move(colorSpace));
64 return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator(
Robert Phillipsd074b622021-03-15 08:49:24 -040065 info, texture.get(), origin, dContext->directContextID(),
Robert Phillipsfd0d9702019-02-01 10:19:42 -050066 std::move(semaphore), backendTexture));
Brian Osman13dddce2017-05-09 13:19:50 -040067}
68
Greg Daniel301015c2019-11-18 14:06:46 -050069GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(
Robert Phillipsd074b622021-03-15 08:49:24 -040070 const SkImageInfo& info,
71 GrTexture* texture,
72 GrSurfaceOrigin origin,
73 GrDirectContext::DirectContextID owningContextID,
74 std::unique_ptr<GrSemaphore> semaphore,
75 const GrBackendTexture& backendTex)
Brian Salomonb916b7b2019-04-01 13:34:34 -040076 : INHERITED(info)
Greg Daniel301015c2019-11-18 14:06:46 -050077 , fRefHelper(new RefHelper(texture, owningContextID, std::move(semaphore)))
Brian Salomonb916b7b2019-04-01 13:34:34 -040078 , fBackendTexture(backendTex)
Brian Salomonb916b7b2019-04-01 13:34:34 -040079 , fSurfaceOrigin(origin) {}
Brian Osman13dddce2017-05-09 13:19:50 -040080
81GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
82 fRefHelper->unref();
83}
84
Brian Osman13dddce2017-05-09 13:19:50 -040085///////////////////////////////////////////////////////////////////////////////////////////////////
86
Brian Osman13dddce2017-05-09 13:19:50 -040087void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
88 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
89 SkASSERT(refHelper);
90
Greg Danielabba9982018-02-01 10:07:57 -050091 refHelper->fBorrowingContextReleaseProc = nullptr;
Robert Phillipsd074b622021-03-15 08:49:24 -040092 refHelper->fBorrowingContextID.makeInvalid();
Brian Osman13dddce2017-05-09 13:19:50 -040093 refHelper->unref();
94}
95
Brian Salomonbc074a62020-03-18 10:06:13 -040096GrSurfaceProxyView GrBackendTextureImageGenerator::onGenerateTexture(
Robert Phillipsd074b622021-03-15 08:49:24 -040097 GrRecordingContext* rContext,
Brian Salomonbc074a62020-03-18 10:06:13 -040098 const SkImageInfo& info,
99 const SkIPoint& origin,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400100 GrMipmapped mipMapped,
Brian Salomonbc074a62020-03-18 10:06:13 -0400101 GrImageTexGenPolicy texGenPolicy) {
Robert Phillipsd074b622021-03-15 08:49:24 -0400102 SkASSERT(rContext);
Brian Osman13dddce2017-05-09 13:19:50 -0400103
Robert Phillipsd074b622021-03-15 08:49:24 -0400104 // We currently limit GrBackendTextureImageGenerators to direct contexts since
105 // only Flutter uses them and doesn't use recording/DDL contexts. Ideally, the
106 // cross context texture functionality can be subsumed by the thread-safe cache
107 // working with utility contexts.
108 auto dContext = rContext->asDirectContext();
109 if (!dContext) {
110 return {};
111 }
112
113 if (dContext->backend() != fBackendTexture.backend()) {
Greg Danielcc104db2020-02-03 14:17:08 -0500114 return {};
Brian Osman13dddce2017-05-09 13:19:50 -0400115 }
Brian Osman052ef692018-03-27 09:56:31 -0400116 if (info.colorType() != this->getInfo().colorType()) {
Greg Danielcc104db2020-02-03 14:17:08 -0500117 return {};
Brian Osman052ef692018-03-27 09:56:31 -0400118 }
Brian Osman13dddce2017-05-09 13:19:50 -0400119
Robert Phillipsd074b622021-03-15 08:49:24 -0400120 auto proxyProvider = dContext->priv().proxyProvider();
Robert Phillips777707b2018-01-17 11:40:14 -0500121
Greg Danielabba9982018-02-01 10:07:57 -0500122 fBorrowingMutex.acquire();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500123 sk_sp<GrRefCntedCallback> releaseProcHelper;
Robert Phillipsd074b622021-03-15 08:49:24 -0400124 if (fRefHelper->fBorrowingContextID.isValid()) {
125 if (fRefHelper->fBorrowingContextID != dContext->directContextID()) {
Greg Danielabba9982018-02-01 10:07:57 -0500126 fBorrowingMutex.release();
Robert Phillipsd074b622021-03-15 08:49:24 -0400127 rContext->priv().printWarningMessage(
Chris Dalton5a5fe792020-02-15 11:41:30 -0700128 "GrBackendTextureImageGenerator: Trying to use texture on two GrContexts!\n");
Greg Danielcc104db2020-02-03 14:17:08 -0500129 return {};
Greg Danielabba9982018-02-01 10:07:57 -0500130 } else {
131 SkASSERT(fRefHelper->fBorrowingContextReleaseProc);
132 // Ref the release proc to be held by the proxy we make below
133 releaseProcHelper = sk_ref_sp(fRefHelper->fBorrowingContextReleaseProc);
Greg Daniel966db9e2018-01-12 13:15:06 -0500134 }
Greg Danielabba9982018-02-01 10:07:57 -0500135 } else {
136 SkASSERT(!fRefHelper->fBorrowingContextReleaseProc);
137 // The ref we add to fRefHelper here will be passed into and owned by the
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500138 // GrRefCntedCallback.
Greg Danielabba9982018-02-01 10:07:57 -0500139 fRefHelper->ref();
Brian Salomon694ff172020-11-04 16:54:28 -0500140 releaseProcHelper =
141 GrRefCntedCallback::Make(ReleaseRefHelper_TextureReleaseProc, fRefHelper);
Greg Danielabba9982018-02-01 10:07:57 -0500142 fRefHelper->fBorrowingContextReleaseProc = releaseProcHelper.get();
Brian Osman13dddce2017-05-09 13:19:50 -0400143 }
Robert Phillipsd074b622021-03-15 08:49:24 -0400144 fRefHelper->fBorrowingContextID = dContext->directContextID();
Brian Salomonb916b7b2019-04-01 13:34:34 -0400145 if (!fRefHelper->fBorrowedTextureKey.isValid()) {
146 static const auto kDomain = GrUniqueKey::GenerateDomain();
147 GrUniqueKey::Builder builder(&fRefHelper->fBorrowedTextureKey, kDomain, 1);
148 builder[0] = this->uniqueID();
149 }
Greg Danielabba9982018-02-01 10:07:57 -0500150 fBorrowingMutex.release();
Brian Osman13dddce2017-05-09 13:19:50 -0400151
Robert Phillipsd074b622021-03-15 08:49:24 -0400152 SkASSERT(fRefHelper->fBorrowingContextID == dContext->directContextID());
Brian Osman13dddce2017-05-09 13:19:50 -0400153
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400154 GrBackendFormat backendFormat = fBackendTexture.getBackendFormat();
155 SkASSERT(backendFormat.isValid());
156
157 GrColorType grColorType = SkColorTypeToGrColorType(info.colorType());
158
Brian Salomon40a40622020-07-21 10:32:07 -0400159 GrMipmapped textureIsMipMapped = fBackendTexture.hasMipmaps() ? GrMipmapped::kYes
Brian Salomon7e67dca2020-07-21 09:27:25 -0400160 : GrMipmapped::kNo;
Greg Daniele728f672018-01-17 10:52:04 -0500161
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600162 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
163 // mipmaps are fully fleshed out.
Brian Salomon40a40622020-07-21 10:32:07 -0400164 GrMipmapStatus mipmapStatus = fBackendTexture.hasMipmaps()
Brian Salomona6db5102020-07-21 09:56:23 -0400165 ? GrMipmapStatus::kValid : GrMipmapStatus::kNotAllocated;
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600166
Robert Phillipsd074b622021-03-15 08:49:24 -0400167 GrSwizzle readSwizzle = dContext->priv().caps()->getReadSwizzle(backendFormat, grColorType);
Greg Danielce3ddaa2020-01-22 16:58:15 -0500168
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400169 // Must make copies of member variables to capture in the lambda since this image generator may
170 // be deleted before we actually execute the lambda.
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400171 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
172 [refHelper = fRefHelper, releaseProcHelper, backendTexture = fBackendTexture](
Brian Salomon63410e92020-03-23 18:32:50 -0400173 GrResourceProvider* resourceProvider,
174 const GrSurfaceProxy::LazySurfaceDesc&) -> GrSurfaceProxy::LazyCallbackResult {
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400175 if (refHelper->fSemaphore) {
176 resourceProvider->priv().gpu()->waitSemaphore(refHelper->fSemaphore.get());
177 }
Greg Daniele728f672018-01-17 10:52:04 -0500178
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400179 // If a client re-draws the same image multiple times, the texture we return
180 // will be cached and re-used. If they draw a subset, though, we may be
181 // re-called. In that case, we want to re-use the borrowed texture we've
182 // previously created.
183 sk_sp<GrTexture> tex;
184 SkASSERT(refHelper->fBorrowedTextureKey.isValid());
185 auto surf = resourceProvider->findByUniqueKey<GrSurface>(
186 refHelper->fBorrowedTextureKey);
187 if (surf) {
188 SkASSERT(surf->asTexture());
189 tex = sk_ref_sp(surf->asTexture());
190 } else {
191 // We just gained access to the texture. If we're on the original
192 // context, we could use the original texture, but we'd have no way of
193 // detecting that it's no longer in-use. So we always make a wrapped
194 // copy, where the release proc informs us that the context is done with
195 // it. This is unfortunate - we'll have two texture objects referencing
196 // the same GPU object. However, no client can ever see the original
197 // texture, so this should be safe. We make the texture uncacheable so
198 // that the release proc is called ASAP.
199 tex = resourceProvider->wrapBackendTexture(
200 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
201 kRead_GrIOType);
202 if (!tex) {
203 return {};
204 }
205 tex->setRelease(releaseProcHelper);
206 tex->resourcePriv().setUniqueKey(refHelper->fBorrowedTextureKey);
207 }
208 // We use keys to avoid re-wrapping the GrBackendTexture in a GrTexture.
209 // This is unrelated to the whatever SkImage key may be assigned to the
210 // proxy.
211 return {std::move(tex), true, GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced};
212 },
Greg Danielc113f002020-08-26 13:28:22 -0400213 backendFormat, fBackendTexture.dimensions(), textureIsMipMapped, mipmapStatus,
214 GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo,
Brian Salomon8a78e9c2020-03-27 10:42:15 -0400215 GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes);
Greg Daniele3204862018-04-16 11:24:10 -0400216 if (!proxy) {
Greg Danielcc104db2020-02-03 14:17:08 -0500217 return {};
Greg Daniele3204862018-04-16 11:24:10 -0400218 }
219
Brian Salomonbc074a62020-03-18 10:06:13 -0400220 if (texGenPolicy == GrImageTexGenPolicy::kDraw && origin.isZero() &&
221 info.dimensions() == fBackendTexture.dimensions() &&
Brian Salomon8c82a872020-07-21 12:09:58 -0400222 (mipMapped == GrMipmapped::kNo || proxy->mipmapped() == GrMipmapped::kYes)) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400223 // If the caller wants the entire texture and we have the correct mip support, we're done
Greg Danielcc104db2020-02-03 14:17:08 -0500224 return GrSurfaceProxyView(std::move(proxy), fSurfaceOrigin, readSwizzle);
Brian Osman13dddce2017-05-09 13:19:50 -0400225 } else {
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400226 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400227
Brian Salomonbc074a62020-03-18 10:06:13 -0400228 SkBudgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
229 ? SkBudgeted::kNo
230 : SkBudgeted::kYes;
231
Robert Phillipsd074b622021-03-15 08:49:24 -0400232 auto copy = GrSurfaceProxy::Copy(dContext,
Brian Salomon982127b2021-01-21 10:43:35 -0500233 std::move(proxy),
234 fSurfaceOrigin,
235 mipMapped,
236 subset,
237 SkBackingFit::kExact,
238 budgeted);
Brian Salomonc5243782020-04-02 12:50:34 -0400239 return {std::move(copy), fSurfaceOrigin, readSwizzle};
Brian Osman13dddce2017-05-09 13:19:50 -0400240 }
241}