blob: 504bbcb58faffa2b20ca1a0a57816cc71b553e3c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/private/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"
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"
Greg Daniel456f9b52020-03-05 19:14:18 +000021#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrTexturePriv.h"
23#include "src/gpu/GrTextureProxyPriv.h"
24#include "src/gpu/SkGr.h"
25#include "src/gpu/gl/GrGLTexture.h"
Brian Osman13dddce2017-05-09 13:19:50 -040026
Greg Daniel301015c2019-11-18 14:06:46 -050027GrBackendTextureImageGenerator::RefHelper::RefHelper(GrTexture* texture, uint32_t owningContextID,
28 std::unique_ptr<GrSemaphore> semaphore)
Brian Salomonb916b7b2019-04-01 13:34:34 -040029 : fOriginalTexture(texture)
30 , fOwningContextID(owningContextID)
31 , fBorrowingContextReleaseProc(nullptr)
Greg Daniel301015c2019-11-18 14:06:46 -050032 , fBorrowingContextID(SK_InvalidGenID)
33 , fSemaphore(std::move(semaphore)) {}
Brian Salomonb916b7b2019-04-01 13:34:34 -040034
Brian Osman13dddce2017-05-09 13:19:50 -040035GrBackendTextureImageGenerator::RefHelper::~RefHelper() {
Brian Salomonb916b7b2019-04-01 13:34:34 -040036 SkASSERT(fBorrowingContextID == SK_InvalidUniqueID);
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 };
41 SkMessageBus<GrTextureFreedMessage>::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) {
Brian Osman13dddce2017-05-09 13:19:50 -040048 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.
Robert Phillipsddc21482019-10-16 14:30:09 -040053 context->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 Phillips62221e72019-07-24 15:07:38 -040057 if (!context->priv().caps()->areColorTypeAndFormatCompatible(
58 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 Phillips9da87e02019-02-04 13:26:26 -050065 info, texture.get(), origin, context->priv().contextID(),
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(
70 const SkImageInfo& info,
71 GrTexture* texture,
72 GrSurfaceOrigin origin,
73 uint32_t 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;
Greg Daniel48661b82018-01-22 16:11:35 -050092 refHelper->fBorrowingContextID = SK_InvalidGenID;
Brian Osman13dddce2017-05-09 13:19:50 -040093 refHelper->unref();
94}
95
Brian Salomonbc074a62020-03-18 10:06:13 -040096GrSurfaceProxyView GrBackendTextureImageGenerator::onGenerateTexture(
97 GrRecordingContext* context,
98 const SkImageInfo& info,
99 const SkIPoint& origin,
100 GrMipMapped mipMapped,
101 GrImageTexGenPolicy texGenPolicy) {
Brian Osman13dddce2017-05-09 13:19:50 -0400102 SkASSERT(context);
103
Robert Phillips4217ea72019-01-30 13:08:28 -0500104 if (context->backend() != fBackendTexture.backend()) {
Greg Danielcc104db2020-02-03 14:17:08 -0500105 return {};
Brian Osman13dddce2017-05-09 13:19:50 -0400106 }
Brian Osman052ef692018-03-27 09:56:31 -0400107 if (info.colorType() != this->getInfo().colorType()) {
Greg Danielcc104db2020-02-03 14:17:08 -0500108 return {};
Brian Osman052ef692018-03-27 09:56:31 -0400109 }
Brian Osman13dddce2017-05-09 13:19:50 -0400110
Robert Phillips9da87e02019-02-04 13:26:26 -0500111 auto proxyProvider = context->priv().proxyProvider();
Robert Phillips777707b2018-01-17 11:40:14 -0500112
Greg Danielabba9982018-02-01 10:07:57 -0500113 fBorrowingMutex.acquire();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500114 sk_sp<GrRefCntedCallback> releaseProcHelper;
Greg Danielabba9982018-02-01 10:07:57 -0500115 if (SK_InvalidGenID != fRefHelper->fBorrowingContextID) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500116 if (fRefHelper->fBorrowingContextID != context->priv().contextID()) {
Greg Danielabba9982018-02-01 10:07:57 -0500117 fBorrowingMutex.release();
Chris Dalton5a5fe792020-02-15 11:41:30 -0700118 context->priv().printWarningMessage(
119 "GrBackendTextureImageGenerator: Trying to use texture on two GrContexts!\n");
Greg Danielcc104db2020-02-03 14:17:08 -0500120 return {};
Greg Danielabba9982018-02-01 10:07:57 -0500121 } 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);
Greg Daniel966db9e2018-01-12 13:15:06 -0500125 }
Greg Danielabba9982018-02-01 10:07:57 -0500126 } else {
127 SkASSERT(!fRefHelper->fBorrowingContextReleaseProc);
128 // The ref we add to fRefHelper here will be passed into and owned by the
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500129 // GrRefCntedCallback.
Greg Danielabba9982018-02-01 10:07:57 -0500130 fRefHelper->ref();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500131 releaseProcHelper.reset(
132 new GrRefCntedCallback(ReleaseRefHelper_TextureReleaseProc, fRefHelper));
Greg Danielabba9982018-02-01 10:07:57 -0500133 fRefHelper->fBorrowingContextReleaseProc = releaseProcHelper.get();
Brian Osman13dddce2017-05-09 13:19:50 -0400134 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500135 fRefHelper->fBorrowingContextID = context->priv().contextID();
Brian Salomonb916b7b2019-04-01 13:34:34 -0400136 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 }
Greg Danielabba9982018-02-01 10:07:57 -0500141 fBorrowingMutex.release();
Brian Osman13dddce2017-05-09 13:19:50 -0400142
Robert Phillips9da87e02019-02-04 13:26:26 -0500143 SkASSERT(fRefHelper->fBorrowingContextID == context->priv().contextID());
Brian Osman13dddce2017-05-09 13:19:50 -0400144
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400145 GrBackendFormat backendFormat = fBackendTexture.getBackendFormat();
146 SkASSERT(backendFormat.isValid());
147
148 GrColorType grColorType = SkColorTypeToGrColorType(info.colorType());
149
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500150 GrMipMapped textureIsMipMapped = fBackendTexture.hasMipMaps() ? GrMipMapped::kYes
151 : GrMipMapped::kNo;
Greg Daniele728f672018-01-17 10:52:04 -0500152
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600153 // 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
Greg Danielce3ddaa2020-01-22 16:58:15 -0500158 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(backendFormat, grColorType);
159
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400160 // 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.
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500162 sk_sp<GrTextureProxy> proxy =
163 proxyProvider->createLazyProxy(
164 [refHelper = fRefHelper, releaseProcHelper, backendTexture = fBackendTexture,
165 grColorType](GrResourceProvider* resourceProvider)
166 -> GrSurfaceProxy::LazyCallbackResult {
Greg Daniel3a365112020-02-14 10:47:18 -0500167 if (refHelper->fSemaphore) {
168 resourceProvider->priv().gpu()->waitSemaphore(
169 refHelper->fSemaphore.get());
170 }
Greg Daniele728f672018-01-17 10:52:04 -0500171
Greg Daniel3a365112020-02-14 10:47:18 -0500172 // If a client re-draws the same image multiple times, the texture we return
173 // will be cached and re-used. If they draw a subset, though, we may be
174 // re-called. In that case, we want to re-use the borrowed texture we've
175 // previously created.
176 sk_sp<GrTexture> tex;
177 SkASSERT(refHelper->fBorrowedTextureKey.isValid());
178 auto surf = resourceProvider->findByUniqueKey<GrSurface>(
179 refHelper->fBorrowedTextureKey);
180 if (surf) {
181 SkASSERT(surf->asTexture());
182 tex = sk_ref_sp(surf->asTexture());
183 } else {
184 // We just gained access to the texture. If we're on the original
185 // context, we could use the original texture, but we'd have no way of
186 // detecting that it's no longer in-use. So we always make a wrapped
187 // copy, where the release proc informs us that the context is done with
188 // it. This is unfortunate - we'll have two texture objects referencing
189 // the same GPU object. However, no client can ever see the original
190 // texture, so this should be safe. We make the texture uncacheable so
191 // that the release proc is called ASAP.
192 tex = resourceProvider->wrapBackendTexture(
193 backendTexture, grColorType, kBorrow_GrWrapOwnership,
194 GrWrapCacheable::kNo, kRead_GrIOType);
195 if (!tex) {
196 return {};
197 }
198 tex->setRelease(releaseProcHelper);
199 tex->resourcePriv().setUniqueKey(refHelper->fBorrowedTextureKey);
200 }
201 // We use keys to avoid re-wrapping the GrBackendTexture in a GrTexture.
202 // This is unrelated to the whatever SkImage key may be assigned to the
203 // proxy.
204 return {std::move(tex), true,
205 GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced};
206 },
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500207 backendFormat, fBackendTexture.dimensions(), readSwizzle, GrRenderable::kNo, 1,
208 textureIsMipMapped, mipMapsStatus, GrInternalSurfaceFlags::kReadOnly,
209 SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo,
210 GrSurfaceProxy::UseAllocator::kYes);
Greg Daniele3204862018-04-16 11:24:10 -0400211 if (!proxy) {
Greg Danielcc104db2020-02-03 14:17:08 -0500212 return {};
Greg Daniele3204862018-04-16 11:24:10 -0400213 }
214
Brian Salomonbc074a62020-03-18 10:06:13 -0400215 if (texGenPolicy == GrImageTexGenPolicy::kDraw && origin.isZero() &&
216 info.dimensions() == fBackendTexture.dimensions() &&
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500217 (mipMapped == GrMipMapped::kNo || proxy->mipMapped() == GrMipMapped::kYes)) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400218 // If the caller wants the entire texture and we have the correct mip support, we're done
Greg Danielcc104db2020-02-03 14:17:08 -0500219 return GrSurfaceProxyView(std::move(proxy), fSurfaceOrigin, readSwizzle);
Brian Osman13dddce2017-05-09 13:19:50 -0400220 } else {
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400221 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400222
Brian Salomonbc074a62020-03-18 10:06:13 -0400223 SkBudgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
224 ? SkBudgeted::kNo
225 : SkBudgeted::kYes;
226
227 return GrSurfaceProxy::Copy(context, proxy.get(), fSurfaceOrigin, grColorType, mipMapped,
228 subset, SkBackingFit::kExact, budgeted);
Brian Osman13dddce2017-05-09 13:19:50 -0400229 }
230}