blob: 212aded8f3ad41dab37db8c6229f8dfef02f7cde [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"
Greg Daniel261b8aa2017-10-23 09:37:36 -040013#include "GrRenderTargetContext.h"
Brian Osman13dddce2017-05-09 13:19:50 -040014#include "GrResourceCache.h"
15#include "GrResourceProvider.h"
Greg Daniele728f672018-01-17 10:52:04 -050016#include "GrResourceProviderPriv.h"
Brian Osman13dddce2017-05-09 13:19:50 -040017#include "GrSemaphore.h"
Robert Phillips646e4292017-06-13 12:44:56 -040018#include "GrTexture.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040019#include "GrTexturePriv.h"
Greg Daniele3204862018-04-16 11:24:10 -040020#include "GrTextureProxyPriv.h"
Brian Osman13dddce2017-05-09 13:19:50 -040021#include "SkGr.h"
22#include "SkMessageBus.h"
Brian Salomon7226c232018-07-30 13:13:17 -040023#include "gl/GrGLTexture.h"
Brian Osman13dddce2017-05-09 13:19:50 -040024
25GrBackendTextureImageGenerator::RefHelper::~RefHelper() {
26 SkASSERT(nullptr == fBorrowedTexture);
Brian Osman13dddce2017-05-09 13:19:50 -040027
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
Brian Osman13dddce2017-05-09 13:19:50 -040034std::unique_ptr<SkImageGenerator>
Robert Phillipsb0e93a22017-08-29 08:26:54 -040035GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, GrSurfaceOrigin origin,
Brian Osman052ef692018-03-27 09:56:31 -040036 sk_sp<GrSemaphore> semaphore, SkColorType colorType,
Brian Osman13dddce2017-05-09 13:19:50 -040037 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
Brian Osman13dddce2017-05-09 13:19:50 -040038 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.
Robert Phillips6be756b2018-01-16 15:07:54 -050043 context->contextPriv().getResourceCache()->insertCrossContextGpuResource(texture.get());
Brian Osman13dddce2017-05-09 13:19:50 -040044
Robert Phillipsb67821d2017-12-13 15:00:45 -050045 GrBackendTexture backendTexture = texture->getBackendTexture();
Brian Salomonc7fe0f72018-05-11 10:14:21 -040046 if (!context->contextPriv().caps()->validateBackendTexture(backendTexture, colorType,
47 &backendTexture.fConfig)) {
Brian Osman052ef692018-03-27 09:56:31 -040048 return nullptr;
49 }
Brian Osman13dddce2017-05-09 13:19:50 -040050
51 SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType,
52 std::move(colorSpace));
53 return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator(
Robert Phillipsb0e93a22017-08-29 08:26:54 -040054 info, texture.get(), origin, context->uniqueID(), std::move(semaphore), backendTexture));
Brian Osman13dddce2017-05-09 13:19:50 -040055}
56
57GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info,
58 GrTexture* texture,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040059 GrSurfaceOrigin origin,
Brian Osman13dddce2017-05-09 13:19:50 -040060 uint32_t owningContextID,
61 sk_sp<GrSemaphore> semaphore,
62 const GrBackendTexture& backendTex)
63 : INHERITED(info)
64 , fRefHelper(new RefHelper(texture, owningContextID))
65 , fSemaphore(std::move(semaphore))
Brian Osman13dddce2017-05-09 13:19:50 -040066 , fBackendTexture(backendTex)
Greg Daniele728f672018-01-17 10:52:04 -050067 , fConfig(backendTex.config())
Robert Phillipsb0e93a22017-08-29 08:26:54 -040068 , fSurfaceOrigin(origin) { }
Brian Osman13dddce2017-05-09 13:19:50 -040069
70GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
71 fRefHelper->unref();
72}
73
Brian Osman13dddce2017-05-09 13:19:50 -040074///////////////////////////////////////////////////////////////////////////////////////////////////
75
Brian Osman13dddce2017-05-09 13:19:50 -040076void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
77 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
78 SkASSERT(refHelper);
79
Brian Osman13dddce2017-05-09 13:19:50 -040080 refHelper->fBorrowedTexture = nullptr;
Greg Danielabba9982018-02-01 10:07:57 -050081 refHelper->fBorrowingContextReleaseProc = nullptr;
Greg Daniel48661b82018-01-22 16:11:35 -050082 refHelper->fBorrowingContextID = SK_InvalidGenID;
Brian Osman13dddce2017-05-09 13:19:50 -040083 refHelper->unref();
84}
85
Stan Ilievba81af22017-06-08 15:16:53 -040086sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture(
Brian Osmanc87cfb62018-07-11 09:08:46 -040087 GrContext* context, const SkImageInfo& info, const SkIPoint& origin, bool willNeedMipMaps) {
Brian Osman13dddce2017-05-09 13:19:50 -040088 SkASSERT(context);
89
90 if (context->contextPriv().getBackend() != fBackendTexture.backend()) {
91 return nullptr;
92 }
Brian Osman052ef692018-03-27 09:56:31 -040093 if (info.colorType() != this->getInfo().colorType()) {
94 return nullptr;
95 }
Brian Osman13dddce2017-05-09 13:19:50 -040096
Robert Phillips777707b2018-01-17 11:40:14 -050097 auto proxyProvider = context->contextPriv().proxyProvider();
98
Greg Danielabba9982018-02-01 10:07:57 -050099 fBorrowingMutex.acquire();
100 sk_sp<GrReleaseProcHelper> releaseProcHelper;
101 if (SK_InvalidGenID != fRefHelper->fBorrowingContextID) {
Greg Daniel966db9e2018-01-12 13:15:06 -0500102 if (fRefHelper->fBorrowingContextID != context->uniqueID()) {
Greg Danielabba9982018-02-01 10:07:57 -0500103 fBorrowingMutex.release();
Greg Daniel966db9e2018-01-12 13:15:06 -0500104 return nullptr;
Greg Danielabba9982018-02-01 10:07:57 -0500105 } else {
106 SkASSERT(fRefHelper->fBorrowingContextReleaseProc);
107 // Ref the release proc to be held by the proxy we make below
108 releaseProcHelper = sk_ref_sp(fRefHelper->fBorrowingContextReleaseProc);
Greg Daniel966db9e2018-01-12 13:15:06 -0500109 }
Greg Danielabba9982018-02-01 10:07:57 -0500110 } else {
111 SkASSERT(!fRefHelper->fBorrowingContextReleaseProc);
112 // The ref we add to fRefHelper here will be passed into and owned by the
113 // GrReleaseProcHelper.
114 fRefHelper->ref();
115 releaseProcHelper.reset(new GrReleaseProcHelper(ReleaseRefHelper_TextureReleaseProc,
116 fRefHelper));
117 fRefHelper->fBorrowingContextReleaseProc = releaseProcHelper.get();
Brian Osman13dddce2017-05-09 13:19:50 -0400118 }
Greg Danielabba9982018-02-01 10:07:57 -0500119 fRefHelper->fBorrowingContextID = context->uniqueID();
120 fBorrowingMutex.release();
Brian Osman13dddce2017-05-09 13:19:50 -0400121
122 SkASSERT(fRefHelper->fBorrowingContextID == context->uniqueID());
123
Greg Daniele728f672018-01-17 10:52:04 -0500124 GrSurfaceDesc desc;
Greg Daniele728f672018-01-17 10:52:04 -0500125 desc.fWidth = fBackendTexture.width();
126 desc.fHeight = fBackendTexture.height();
127 desc.fConfig = fConfig;
128 GrMipMapped mipMapped = fBackendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
129
130 // Must make copies of member variables to capture in the lambda since this image generator may
131 // be deleted before we actuallly execute the lambda.
Greg Daniele728f672018-01-17 10:52:04 -0500132 sk_sp<GrSemaphore> semaphore = fSemaphore;
133 GrBackendTexture backendTexture = fBackendTexture;
134 RefHelper* refHelper = fRefHelper;
Greg Daniele728f672018-01-17 10:52:04 -0500135
Brian Salomon7226c232018-07-30 13:13:17 -0400136 GrTextureType textureType = GrTextureType::k2D;
137 GrGLTextureInfo glInfo;
138 if (backendTexture.getGLTextureInfo(&glInfo)) {
139 textureType = GrGLTexture::TextureTypeFromTarget(glInfo.fTarget);
140 }
Robert Phillips777707b2018-01-17 11:40:14 -0500141 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
Greg Danielf1251112018-08-27 09:55:03 -0400142 [refHelper, releaseProcHelper, semaphore, backendTexture]
143 (GrResourceProvider* resourceProvider) {
Greg Daniele728f672018-01-17 10:52:04 -0500144 if (!resourceProvider) {
Greg Daniele728f672018-01-17 10:52:04 -0500145 return sk_sp<GrTexture>();
146 }
147
Greg Daniel48661b82018-01-22 16:11:35 -0500148 if (semaphore) {
Greg Daniele728f672018-01-17 10:52:04 -0500149 resourceProvider->priv().gpu()->waitSemaphore(semaphore);
150 }
151
152 sk_sp<GrTexture> tex;
153 if (refHelper->fBorrowedTexture) {
154 // If a client re-draws the same image multiple times, the texture we return
155 // will be cached and re-used. If they draw a subset, though, we may be
156 // re-called. In that case, we want to re-use the borrowed texture we've
157 // previously created.
158 tex = sk_ref_sp(refHelper->fBorrowedTexture);
159 SkASSERT(tex);
Greg Daniele728f672018-01-17 10:52:04 -0500160 } else {
161 // We just gained access to the texture. If we're on the original context, we
162 // could use the original texture, but we'd have no way of detecting that it's
163 // no longer in-use. So we always make a wrapped copy, where the release proc
164 // informs us that the context is done with it. This is unfortunate - we'll have
165 // two texture objects referencing the same GPU object. However, no client can
166 // ever see the original texture, so this should be safe.
167 tex = resourceProvider->wrapBackendTexture(backendTexture,
168 kBorrow_GrWrapOwnership);
169 if (!tex) {
Greg Daniele728f672018-01-17 10:52:04 -0500170 return sk_sp<GrTexture>();
171 }
172 refHelper->fBorrowedTexture = tex.get();
173
Greg Danielabba9982018-02-01 10:07:57 -0500174 tex->setRelease(releaseProcHelper);
Greg Daniele728f672018-01-17 10:52:04 -0500175 }
176
Greg Daniele728f672018-01-17 10:52:04 -0500177 return tex;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500178 },
Brian Salomon7226c232018-07-30 13:13:17 -0400179 desc, fSurfaceOrigin, mipMapped, textureType, SkBackingFit::kExact, SkBudgeted::kNo);
Brian Osman13dddce2017-05-09 13:19:50 -0400180
Greg Daniele3204862018-04-16 11:24:10 -0400181 if (!proxy) {
182 return nullptr;
183 }
184
Brian Osman13dddce2017-05-09 13:19:50 -0400185 if (0 == origin.fX && 0 == origin.fY &&
Greg Daniel261b8aa2017-10-23 09:37:36 -0400186 info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height() &&
Greg Daniele252f082017-10-23 16:05:23 -0400187 (!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400188 // If the caller wants the entire texture and we have the correct mip support, we're done
Brian Osman13dddce2017-05-09 13:19:50 -0400189 return proxy;
190 } else {
191 // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable,
Greg Daniel261b8aa2017-10-23 09:37:36 -0400192 // because Vulkan will want to do the copy as a draw. All other copies would require a
193 // layout change in Vulkan and we do not change the layout of borrowed images.
Greg Daniel45d63032017-10-30 13:41:26 -0400194 GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
195
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500196 sk_sp<GrRenderTargetContext> rtContext(
197 context->contextPriv().makeDeferredRenderTargetContext(
Brian Osman9363ac42018-06-01 16:10:53 -0400198 SkBackingFit::kExact, info.width(), info.height(), proxy->config(), nullptr, 1,
199 mipMapped, proxy->origin(), nullptr, SkBudgeted::kYes));
Brian Salomon63e79732017-05-15 21:23:13 -0400200
Greg Daniel261b8aa2017-10-23 09:37:36 -0400201 if (!rtContext) {
Brian Osman13dddce2017-05-09 13:19:50 -0400202 return nullptr;
203 }
204
205 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
Greg Daniel261b8aa2017-10-23 09:37:36 -0400206 if (!rtContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
Brian Osman13dddce2017-05-09 13:19:50 -0400207 return nullptr;
208 }
209
Greg Daniel261b8aa2017-10-23 09:37:36 -0400210 return rtContext->asTextureProxyRef();
Brian Osman13dddce2017-05-09 13:19:50 -0400211 }
212}
Greg Daniel9cc28232018-04-06 10:00:09 -0400213