blob: 0c41cc156727e1e2d7cee576ccf231ee56f1532a [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
8#include "GrBackendTextureImageGenerator.h"
Brian Osman13dddce2017-05-09 13:19:50 -04009#include "GrContext.h"
10#include "GrContextPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040011#include "GrGpu.h"
Robert Phillips777707b2018-01-17 11:40:14 -050012#include "GrProxyProvider.h"
Robert Phillips9338c602019-02-19 12:52:29 -050013#include "GrRecordingContext.h"
14#include "GrRecordingContextPriv.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040015#include "GrRenderTargetContext.h"
Brian Osman13dddce2017-05-09 13:19:50 -040016#include "GrResourceCache.h"
17#include "GrResourceProvider.h"
Greg Daniele728f672018-01-17 10:52:04 -050018#include "GrResourceProviderPriv.h"
Brian Osman13dddce2017-05-09 13:19:50 -040019#include "GrSemaphore.h"
Robert Phillips646e4292017-06-13 12:44:56 -040020#include "GrTexture.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040021#include "GrTexturePriv.h"
Greg Daniele3204862018-04-16 11:24:10 -040022#include "GrTextureProxyPriv.h"
Brian Osman13dddce2017-05-09 13:19:50 -040023#include "SkGr.h"
24#include "SkMessageBus.h"
Brian Salomon7226c232018-07-30 13:13:17 -040025#include "gl/GrGLTexture.h"
Brian Osman13dddce2017-05-09 13:19:50 -040026
27GrBackendTextureImageGenerator::RefHelper::~RefHelper() {
28 SkASSERT(nullptr == fBorrowedTexture);
Brian Osman13dddce2017-05-09 13:19:50 -040029
30 // Generator has been freed, and no one is borrowing the texture. Notify the original cache
31 // that it can free the last ref, so it happens on the correct thread.
32 GrGpuResourceFreedMessage msg { fOriginalTexture, fOwningContextID };
33 SkMessageBus<GrGpuResourceFreedMessage>::Post(msg);
34}
35
Brian Osman13dddce2017-05-09 13:19:50 -040036std::unique_ptr<SkImageGenerator>
Robert Phillipsb0e93a22017-08-29 08:26:54 -040037GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, GrSurfaceOrigin origin,
Brian Osman052ef692018-03-27 09:56:31 -040038 sk_sp<GrSemaphore> semaphore, SkColorType colorType,
Brian Osman13dddce2017-05-09 13:19:50 -040039 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
Brian Osman13dddce2017-05-09 13:19:50 -040040 GrContext* context = texture->getContext();
41
42 // Attach our texture to this context's resource cache. This ensures that deletion will happen
43 // in the correct thread/context. This adds the only ref to the texture that will persist from
44 // this point. That ref will be released when the generator's RefHelper is freed.
Robert Phillips9da87e02019-02-04 13:26:26 -050045 context->priv().getResourceCache()->insertCrossContextGpuResource(texture.get());
Brian Osman13dddce2017-05-09 13:19:50 -040046
Robert Phillipsb67821d2017-12-13 15:00:45 -050047 GrBackendTexture backendTexture = texture->getBackendTexture();
Brian Salomonf391d0f2018-12-14 09:18:50 -050048 GrBackendFormat backendFormat = backendTexture.getBackendFormat();
49 if (!backendFormat.isValid()) {
50 return nullptr;
51 }
52 backendTexture.fConfig =
Robert Phillips9da87e02019-02-04 13:26:26 -050053 context->priv().caps()->getConfigFromBackendFormat(backendFormat, colorType);
Brian Salomonf391d0f2018-12-14 09:18:50 -050054 if (backendTexture.fConfig == kUnknown_GrPixelConfig) {
Brian Osman052ef692018-03-27 09:56:31 -040055 return nullptr;
56 }
Brian Osman13dddce2017-05-09 13:19:50 -040057
58 SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType,
59 std::move(colorSpace));
60 return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator(
Robert Phillips9da87e02019-02-04 13:26:26 -050061 info, texture.get(), origin, context->priv().contextID(),
Robert Phillipsfd0d9702019-02-01 10:19:42 -050062 std::move(semaphore), backendTexture));
Brian Osman13dddce2017-05-09 13:19:50 -040063}
64
65GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info,
66 GrTexture* texture,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040067 GrSurfaceOrigin origin,
Brian Osman13dddce2017-05-09 13:19:50 -040068 uint32_t owningContextID,
69 sk_sp<GrSemaphore> semaphore,
70 const GrBackendTexture& backendTex)
71 : INHERITED(info)
72 , fRefHelper(new RefHelper(texture, owningContextID))
73 , fSemaphore(std::move(semaphore))
Brian Osman13dddce2017-05-09 13:19:50 -040074 , fBackendTexture(backendTex)
Greg Daniele728f672018-01-17 10:52:04 -050075 , fConfig(backendTex.config())
Robert Phillipsb0e93a22017-08-29 08:26:54 -040076 , fSurfaceOrigin(origin) { }
Brian Osman13dddce2017-05-09 13:19:50 -040077
78GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
79 fRefHelper->unref();
80}
81
Brian Osman13dddce2017-05-09 13:19:50 -040082///////////////////////////////////////////////////////////////////////////////////////////////////
83
Brian Osman13dddce2017-05-09 13:19:50 -040084void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
85 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
86 SkASSERT(refHelper);
87
Brian Osman13dddce2017-05-09 13:19:50 -040088 refHelper->fBorrowedTexture = nullptr;
Greg Danielabba9982018-02-01 10:07:57 -050089 refHelper->fBorrowingContextReleaseProc = nullptr;
Greg Daniel48661b82018-01-22 16:11:35 -050090 refHelper->fBorrowingContextID = SK_InvalidGenID;
Brian Osman13dddce2017-05-09 13:19:50 -040091 refHelper->unref();
92}
93
Stan Ilievba81af22017-06-08 15:16:53 -040094sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture(
Robert Phillips9338c602019-02-19 12:52:29 -050095 GrRecordingContext* context, const SkImageInfo& info,
96 const SkIPoint& origin, bool willNeedMipMaps) {
Brian Osman13dddce2017-05-09 13:19:50 -040097 SkASSERT(context);
98
Robert Phillips4217ea72019-01-30 13:08:28 -050099 if (context->backend() != fBackendTexture.backend()) {
Brian Osman13dddce2017-05-09 13:19:50 -0400100 return nullptr;
101 }
Brian Osman052ef692018-03-27 09:56:31 -0400102 if (info.colorType() != this->getInfo().colorType()) {
103 return nullptr;
104 }
Brian Osman13dddce2017-05-09 13:19:50 -0400105
Robert Phillips9da87e02019-02-04 13:26:26 -0500106 auto proxyProvider = context->priv().proxyProvider();
Robert Phillips777707b2018-01-17 11:40:14 -0500107
Greg Danielabba9982018-02-01 10:07:57 -0500108 fBorrowingMutex.acquire();
109 sk_sp<GrReleaseProcHelper> releaseProcHelper;
110 if (SK_InvalidGenID != fRefHelper->fBorrowingContextID) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500111 if (fRefHelper->fBorrowingContextID != context->priv().contextID()) {
Greg Danielabba9982018-02-01 10:07:57 -0500112 fBorrowingMutex.release();
Greg Daniel966db9e2018-01-12 13:15:06 -0500113 return nullptr;
Greg Danielabba9982018-02-01 10:07:57 -0500114 } else {
115 SkASSERT(fRefHelper->fBorrowingContextReleaseProc);
116 // Ref the release proc to be held by the proxy we make below
117 releaseProcHelper = sk_ref_sp(fRefHelper->fBorrowingContextReleaseProc);
Greg Daniel966db9e2018-01-12 13:15:06 -0500118 }
Greg Danielabba9982018-02-01 10:07:57 -0500119 } else {
120 SkASSERT(!fRefHelper->fBorrowingContextReleaseProc);
121 // The ref we add to fRefHelper here will be passed into and owned by the
122 // GrReleaseProcHelper.
123 fRefHelper->ref();
124 releaseProcHelper.reset(new GrReleaseProcHelper(ReleaseRefHelper_TextureReleaseProc,
125 fRefHelper));
126 fRefHelper->fBorrowingContextReleaseProc = releaseProcHelper.get();
Brian Osman13dddce2017-05-09 13:19:50 -0400127 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500128 fRefHelper->fBorrowingContextID = context->priv().contextID();
Greg Danielabba9982018-02-01 10:07:57 -0500129 fBorrowingMutex.release();
Brian Osman13dddce2017-05-09 13:19:50 -0400130
Robert Phillips9da87e02019-02-04 13:26:26 -0500131 SkASSERT(fRefHelper->fBorrowingContextID == context->priv().contextID());
Brian Osman13dddce2017-05-09 13:19:50 -0400132
Greg Daniele728f672018-01-17 10:52:04 -0500133 GrSurfaceDesc desc;
Greg Daniele728f672018-01-17 10:52:04 -0500134 desc.fWidth = fBackendTexture.width();
135 desc.fHeight = fBackendTexture.height();
136 desc.fConfig = fConfig;
137 GrMipMapped mipMapped = fBackendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
138
139 // Must make copies of member variables to capture in the lambda since this image generator may
140 // be deleted before we actuallly execute the lambda.
Greg Daniele728f672018-01-17 10:52:04 -0500141 sk_sp<GrSemaphore> semaphore = fSemaphore;
142 GrBackendTexture backendTexture = fBackendTexture;
143 RefHelper* refHelper = fRefHelper;
Greg Daniele728f672018-01-17 10:52:04 -0500144
Brian Salomonf391d0f2018-12-14 09:18:50 -0500145 GrBackendFormat format = backendTexture.getBackendFormat();
Greg Daniel4065d452018-11-16 15:43:41 -0500146 SkASSERT(format.isValid());
147
Robert Phillips777707b2018-01-17 11:40:14 -0500148 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
Brian Salomonc67c31c2018-12-06 10:00:03 -0500149 [refHelper, releaseProcHelper, semaphore,
150 backendTexture](GrResourceProvider* resourceProvider) {
Greg Daniel48661b82018-01-22 16:11:35 -0500151 if (semaphore) {
Greg Daniele728f672018-01-17 10:52:04 -0500152 resourceProvider->priv().gpu()->waitSemaphore(semaphore);
153 }
154
155 sk_sp<GrTexture> tex;
156 if (refHelper->fBorrowedTexture) {
157 // If a client re-draws the same image multiple times, the texture we return
158 // will be cached and re-used. If they draw a subset, though, we may be
159 // re-called. In that case, we want to re-use the borrowed texture we've
160 // previously created.
161 tex = sk_ref_sp(refHelper->fBorrowedTexture);
162 SkASSERT(tex);
Greg Daniele728f672018-01-17 10:52:04 -0500163 } else {
164 // We just gained access to the texture. If we're on the original context, we
165 // could use the original texture, but we'd have no way of detecting that it's
166 // no longer in-use. So we always make a wrapped copy, where the release proc
167 // informs us that the context is done with it. This is unfortunate - we'll have
168 // two texture objects referencing the same GPU object. However, no client can
169 // ever see the original texture, so this should be safe.
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500170 // We make the texture uncacheable so that the release proc is called ASAP.
Brian Salomonc67c31c2018-12-06 10:00:03 -0500171 tex = resourceProvider->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500172 backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
173 kRead_GrIOType);
Greg Daniele728f672018-01-17 10:52:04 -0500174 if (!tex) {
Greg Daniele728f672018-01-17 10:52:04 -0500175 return sk_sp<GrTexture>();
176 }
177 refHelper->fBorrowedTexture = tex.get();
178
Greg Danielabba9982018-02-01 10:07:57 -0500179 tex->setRelease(releaseProcHelper);
Greg Daniele728f672018-01-17 10:52:04 -0500180 }
181
Greg Daniele728f672018-01-17 10:52:04 -0500182 return tex;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500183 },
Brian Salomonc67c31c2018-12-06 10:00:03 -0500184 format, desc, fSurfaceOrigin, mipMapped, GrInternalSurfaceFlags::kReadOnly,
185 SkBackingFit::kExact, SkBudgeted::kNo);
Brian Osman13dddce2017-05-09 13:19:50 -0400186
Greg Daniele3204862018-04-16 11:24:10 -0400187 if (!proxy) {
188 return nullptr;
189 }
190
Brian Osman13dddce2017-05-09 13:19:50 -0400191 if (0 == origin.fX && 0 == origin.fY &&
Greg Daniel261b8aa2017-10-23 09:37:36 -0400192 info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height() &&
Greg Daniele252f082017-10-23 16:05:23 -0400193 (!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400194 // If the caller wants the entire texture and we have the correct mip support, we're done
Brian Osman13dddce2017-05-09 13:19:50 -0400195 return proxy;
196 } else {
197 // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable,
Greg Daniel261b8aa2017-10-23 09:37:36 -0400198 // because Vulkan will want to do the copy as a draw. All other copies would require a
199 // layout change in Vulkan and we do not change the layout of borrowed images.
Greg Daniel45d63032017-10-30 13:41:26 -0400200 GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
201
Greg Daniel4065d452018-11-16 15:43:41 -0500202 GrBackendFormat format = proxy->backendFormat().makeTexture2D();
203 if (!format.isValid()) {
204 return nullptr;
205 }
206
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500207 sk_sp<GrRenderTargetContext> rtContext(
Robert Phillips9da87e02019-02-04 13:26:26 -0500208 context->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500209 format, SkBackingFit::kExact, info.width(), info.height(),
210 proxy->config(), nullptr, 1, mipMapped, proxy->origin(), nullptr,
211 SkBudgeted::kYes));
Brian Salomon63e79732017-05-15 21:23:13 -0400212
Greg Daniel261b8aa2017-10-23 09:37:36 -0400213 if (!rtContext) {
Brian Osman13dddce2017-05-09 13:19:50 -0400214 return nullptr;
215 }
216
217 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
Greg Daniel261b8aa2017-10-23 09:37:36 -0400218 if (!rtContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
Brian Osman13dddce2017-05-09 13:19:50 -0400219 return nullptr;
220 }
221
Greg Daniel261b8aa2017-10-23 09:37:36 -0400222 return rtContext->asTextureProxyRef();
Brian Osman13dddce2017-05-09 13:19:50 -0400223 }
224}
Greg Daniel9cc28232018-04-06 10:00:09 -0400225