blob: d9cbf6ed74203dc02352b5d18afca0c0586ac929 [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"
9#include "include/gpu/GrTexture.h"
10#include "include/private/GrRecordingContext.h"
Ben Wagner21bca282019-05-15 10:15:52 -040011#include "src/core/SkMessageBus.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#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"
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
Greg Danielcc104db2020-02-03 14:17:08 -050096GrSurfaceProxyView GrBackendTextureImageGenerator::onGenerateTexture(
Robert Phillips9338c602019-02-19 12:52:29 -050097 GrRecordingContext* context, const SkImageInfo& info,
98 const SkIPoint& origin, bool willNeedMipMaps) {
Brian Osman13dddce2017-05-09 13:19:50 -040099 SkASSERT(context);
100
Robert Phillips4217ea72019-01-30 13:08:28 -0500101 if (context->backend() != fBackendTexture.backend()) {
Greg Danielcc104db2020-02-03 14:17:08 -0500102 return {};
Brian Osman13dddce2017-05-09 13:19:50 -0400103 }
Brian Osman052ef692018-03-27 09:56:31 -0400104 if (info.colorType() != this->getInfo().colorType()) {
Greg Danielcc104db2020-02-03 14:17:08 -0500105 return {};
Brian Osman052ef692018-03-27 09:56:31 -0400106 }
Brian Osman13dddce2017-05-09 13:19:50 -0400107
Robert Phillips9da87e02019-02-04 13:26:26 -0500108 auto proxyProvider = context->priv().proxyProvider();
Robert Phillips777707b2018-01-17 11:40:14 -0500109
Greg Danielabba9982018-02-01 10:07:57 -0500110 fBorrowingMutex.acquire();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500111 sk_sp<GrRefCntedCallback> releaseProcHelper;
Greg Danielabba9982018-02-01 10:07:57 -0500112 if (SK_InvalidGenID != fRefHelper->fBorrowingContextID) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500113 if (fRefHelper->fBorrowingContextID != context->priv().contextID()) {
Greg Danielabba9982018-02-01 10:07:57 -0500114 fBorrowingMutex.release();
Brian Osman6e1d51a2019-10-30 15:30:36 -0400115 SkDebugf("GrBackendTextureImageGenerator: Trying to use texture on two GrContexts!\n");
Greg Danielcc104db2020-02-03 14:17:08 -0500116 return {};
Greg Danielabba9982018-02-01 10:07:57 -0500117 } else {
118 SkASSERT(fRefHelper->fBorrowingContextReleaseProc);
119 // Ref the release proc to be held by the proxy we make below
120 releaseProcHelper = sk_ref_sp(fRefHelper->fBorrowingContextReleaseProc);
Greg Daniel966db9e2018-01-12 13:15:06 -0500121 }
Greg Danielabba9982018-02-01 10:07:57 -0500122 } else {
123 SkASSERT(!fRefHelper->fBorrowingContextReleaseProc);
124 // The ref we add to fRefHelper here will be passed into and owned by the
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500125 // GrRefCntedCallback.
Greg Danielabba9982018-02-01 10:07:57 -0500126 fRefHelper->ref();
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500127 releaseProcHelper.reset(
128 new GrRefCntedCallback(ReleaseRefHelper_TextureReleaseProc, fRefHelper));
Greg Danielabba9982018-02-01 10:07:57 -0500129 fRefHelper->fBorrowingContextReleaseProc = releaseProcHelper.get();
Brian Osman13dddce2017-05-09 13:19:50 -0400130 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500131 fRefHelper->fBorrowingContextID = context->priv().contextID();
Brian Salomonb916b7b2019-04-01 13:34:34 -0400132 if (!fRefHelper->fBorrowedTextureKey.isValid()) {
133 static const auto kDomain = GrUniqueKey::GenerateDomain();
134 GrUniqueKey::Builder builder(&fRefHelper->fBorrowedTextureKey, kDomain, 1);
135 builder[0] = this->uniqueID();
136 }
Greg Danielabba9982018-02-01 10:07:57 -0500137 fBorrowingMutex.release();
Brian Osman13dddce2017-05-09 13:19:50 -0400138
Robert Phillips9da87e02019-02-04 13:26:26 -0500139 SkASSERT(fRefHelper->fBorrowingContextID == context->priv().contextID());
Brian Osman13dddce2017-05-09 13:19:50 -0400140
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400141 GrBackendFormat backendFormat = fBackendTexture.getBackendFormat();
142 SkASSERT(backendFormat.isValid());
143
144 GrColorType grColorType = SkColorTypeToGrColorType(info.colorType());
145
Greg Daniele728f672018-01-17 10:52:04 -0500146 GrMipMapped mipMapped = fBackendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
147
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600148 // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its
149 // mipmaps are fully fleshed out.
150 GrMipMapsStatus mipMapsStatus = fBackendTexture.hasMipMaps()
151 ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
152
Greg Danielce3ddaa2020-01-22 16:58:15 -0500153 GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(backendFormat, grColorType);
154
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400155 // Must make copies of member variables to capture in the lambda since this image generator may
156 // be deleted before we actually execute the lambda.
Robert Phillips777707b2018-01-17 11:40:14 -0500157 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
Greg Daniel301015c2019-11-18 14:06:46 -0500158 [refHelper = fRefHelper, releaseProcHelper, backendTexture = fBackendTexture,
159 grColorType](
Brian Salomonbeb7f522019-08-30 16:19:42 -0400160 GrResourceProvider* resourceProvider) -> GrSurfaceProxy::LazyCallbackResult {
Greg Daniel301015c2019-11-18 14:06:46 -0500161 if (refHelper->fSemaphore) {
162 resourceProvider->priv().gpu()->waitSemaphore(refHelper->fSemaphore.get());
Greg Daniele728f672018-01-17 10:52:04 -0500163 }
164
Brian Salomonb916b7b2019-04-01 13:34:34 -0400165 // If a client re-draws the same image multiple times, the texture we return
166 // will be cached and re-used. If they draw a subset, though, we may be
167 // re-called. In that case, we want to re-use the borrowed texture we've
168 // previously created.
Greg Daniele728f672018-01-17 10:52:04 -0500169 sk_sp<GrTexture> tex;
Brian Salomonb916b7b2019-04-01 13:34:34 -0400170 SkASSERT(refHelper->fBorrowedTextureKey.isValid());
171 auto surf = resourceProvider->findByUniqueKey<GrSurface>(
172 refHelper->fBorrowedTextureKey);
173 if (surf) {
174 SkASSERT(surf->asTexture());
175 tex = sk_ref_sp(surf->asTexture());
Greg Daniele728f672018-01-17 10:52:04 -0500176 } else {
177 // We just gained access to the texture. If we're on the original context, we
178 // could use the original texture, but we'd have no way of detecting that it's
179 // no longer in-use. So we always make a wrapped copy, where the release proc
180 // informs us that the context is done with it. This is unfortunate - we'll have
181 // two texture objects referencing the same GPU object. However, no client can
182 // ever see the original texture, so this should be safe.
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500183 // We make the texture uncacheable so that the release proc is called ASAP.
Brian Salomonc67c31c2018-12-06 10:00:03 -0500184 tex = resourceProvider->wrapBackendTexture(
Robert Phillipsc80b0e92019-07-23 10:27:09 -0400185 backendTexture, grColorType, kBorrow_GrWrapOwnership,
186 GrWrapCacheable::kNo, kRead_GrIOType);
Greg Daniele728f672018-01-17 10:52:04 -0500187 if (!tex) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400188 return {};
Greg Daniele728f672018-01-17 10:52:04 -0500189 }
Greg Danielabba9982018-02-01 10:07:57 -0500190 tex->setRelease(releaseProcHelper);
Brian Salomonb916b7b2019-04-01 13:34:34 -0400191 tex->resourcePriv().setUniqueKey(refHelper->fBorrowedTextureKey);
Greg Daniele728f672018-01-17 10:52:04 -0500192 }
Brian Salomonb916b7b2019-04-01 13:34:34 -0400193 // We use keys to avoid re-wrapping the GrBackendTexture in a GrTexture. This is
194 // unrelated to the whatever SkImage key may be assigned to the proxy.
Brian Salomonbeb7f522019-08-30 16:19:42 -0400195 return {std::move(tex), true, GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced};
Brian Salomon2a4f9832018-03-03 22:43:43 -0500196 },
Brian Salomona56a7462020-02-07 14:17:25 -0500197 backendFormat, fBackendTexture.dimensions(), readSwizzle, GrRenderable::kNo, 1,
198 fSurfaceOrigin, mipMapped, mipMapsStatus, GrInternalSurfaceFlags::kReadOnly,
199 SkBackingFit::kExact, SkBudgeted::kNo, GrProtected::kNo,
200 GrSurfaceProxy::UseAllocator::kYes);
Greg Daniele3204862018-04-16 11:24:10 -0400201 if (!proxy) {
Greg Danielcc104db2020-02-03 14:17:08 -0500202 return {};
Greg Daniele3204862018-04-16 11:24:10 -0400203 }
204
Brian Salomona56a7462020-02-07 14:17:25 -0500205 if (origin.isZero() && info.dimensions() == fBackendTexture.dimensions() &&
Greg Daniele252f082017-10-23 16:05:23 -0400206 (!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400207 // If the caller wants the entire texture and we have the correct mip support, we're done
Greg Danielcc104db2020-02-03 14:17:08 -0500208 return GrSurfaceProxyView(std::move(proxy), fSurfaceOrigin, readSwizzle);
Brian Osman13dddce2017-05-09 13:19:50 -0400209 } else {
210 // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable,
Greg Daniel261b8aa2017-10-23 09:37:36 -0400211 // because Vulkan will want to do the copy as a draw. All other copies would require a
212 // layout change in Vulkan and we do not change the layout of borrowed images.
Greg Daniel45d63032017-10-30 13:41:26 -0400213 GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400214 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400215
Greg Daniel40903af2020-01-30 14:55:05 -0500216 return GrSurfaceProxy::Copy(
217 context, proxy.get(), fSurfaceOrigin, grColorType, mipMapped, subset,
Greg Danielcc104db2020-02-03 14:17:08 -0500218 SkBackingFit::kExact, SkBudgeted::kYes);
Brian Osman13dddce2017-05-09 13:19:50 -0400219 }
220}