blob: f191174f667346338d774d6df98c4c16883b19e8 [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 Salomonf391d0f2018-12-14 09:18:50 -050046 GrBackendFormat backendFormat = backendTexture.getBackendFormat();
47 if (!backendFormat.isValid()) {
48 return nullptr;
49 }
50 backendTexture.fConfig =
51 context->contextPriv().caps()->getConfigFromBackendFormat(backendFormat, colorType);
52 if (backendTexture.fConfig == kUnknown_GrPixelConfig) {
Brian Osman052ef692018-03-27 09:56:31 -040053 return nullptr;
54 }
Brian Osman13dddce2017-05-09 13:19:50 -040055
56 SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType,
57 std::move(colorSpace));
58 return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator(
Robert Phillipsb0e93a22017-08-29 08:26:54 -040059 info, texture.get(), origin, context->uniqueID(), std::move(semaphore), backendTexture));
Brian Osman13dddce2017-05-09 13:19:50 -040060}
61
62GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info,
63 GrTexture* texture,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040064 GrSurfaceOrigin origin,
Brian Osman13dddce2017-05-09 13:19:50 -040065 uint32_t owningContextID,
66 sk_sp<GrSemaphore> semaphore,
67 const GrBackendTexture& backendTex)
68 : INHERITED(info)
69 , fRefHelper(new RefHelper(texture, owningContextID))
70 , fSemaphore(std::move(semaphore))
Brian Osman13dddce2017-05-09 13:19:50 -040071 , fBackendTexture(backendTex)
Greg Daniele728f672018-01-17 10:52:04 -050072 , fConfig(backendTex.config())
Robert Phillipsb0e93a22017-08-29 08:26:54 -040073 , fSurfaceOrigin(origin) { }
Brian Osman13dddce2017-05-09 13:19:50 -040074
75GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
76 fRefHelper->unref();
77}
78
Brian Osman13dddce2017-05-09 13:19:50 -040079///////////////////////////////////////////////////////////////////////////////////////////////////
80
Brian Osman13dddce2017-05-09 13:19:50 -040081void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
82 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
83 SkASSERT(refHelper);
84
Brian Osman13dddce2017-05-09 13:19:50 -040085 refHelper->fBorrowedTexture = nullptr;
Greg Danielabba9982018-02-01 10:07:57 -050086 refHelper->fBorrowingContextReleaseProc = nullptr;
Greg Daniel48661b82018-01-22 16:11:35 -050087 refHelper->fBorrowingContextID = SK_InvalidGenID;
Brian Osman13dddce2017-05-09 13:19:50 -040088 refHelper->unref();
89}
90
Stan Ilievba81af22017-06-08 15:16:53 -040091sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture(
Brian Osmanc87cfb62018-07-11 09:08:46 -040092 GrContext* context, const SkImageInfo& info, const SkIPoint& origin, bool willNeedMipMaps) {
Brian Osman13dddce2017-05-09 13:19:50 -040093 SkASSERT(context);
94
95 if (context->contextPriv().getBackend() != fBackendTexture.backend()) {
96 return nullptr;
97 }
Brian Osman052ef692018-03-27 09:56:31 -040098 if (info.colorType() != this->getInfo().colorType()) {
99 return nullptr;
100 }
Brian Osman13dddce2017-05-09 13:19:50 -0400101
Robert Phillips777707b2018-01-17 11:40:14 -0500102 auto proxyProvider = context->contextPriv().proxyProvider();
103
Greg Danielabba9982018-02-01 10:07:57 -0500104 fBorrowingMutex.acquire();
105 sk_sp<GrReleaseProcHelper> releaseProcHelper;
106 if (SK_InvalidGenID != fRefHelper->fBorrowingContextID) {
Greg Daniel966db9e2018-01-12 13:15:06 -0500107 if (fRefHelper->fBorrowingContextID != context->uniqueID()) {
Greg Danielabba9982018-02-01 10:07:57 -0500108 fBorrowingMutex.release();
Greg Daniel966db9e2018-01-12 13:15:06 -0500109 return nullptr;
Greg Danielabba9982018-02-01 10:07:57 -0500110 } else {
111 SkASSERT(fRefHelper->fBorrowingContextReleaseProc);
112 // Ref the release proc to be held by the proxy we make below
113 releaseProcHelper = sk_ref_sp(fRefHelper->fBorrowingContextReleaseProc);
Greg Daniel966db9e2018-01-12 13:15:06 -0500114 }
Greg Danielabba9982018-02-01 10:07:57 -0500115 } else {
116 SkASSERT(!fRefHelper->fBorrowingContextReleaseProc);
117 // The ref we add to fRefHelper here will be passed into and owned by the
118 // GrReleaseProcHelper.
119 fRefHelper->ref();
120 releaseProcHelper.reset(new GrReleaseProcHelper(ReleaseRefHelper_TextureReleaseProc,
121 fRefHelper));
122 fRefHelper->fBorrowingContextReleaseProc = releaseProcHelper.get();
Brian Osman13dddce2017-05-09 13:19:50 -0400123 }
Greg Danielabba9982018-02-01 10:07:57 -0500124 fRefHelper->fBorrowingContextID = context->uniqueID();
125 fBorrowingMutex.release();
Brian Osman13dddce2017-05-09 13:19:50 -0400126
127 SkASSERT(fRefHelper->fBorrowingContextID == context->uniqueID());
128
Greg Daniele728f672018-01-17 10:52:04 -0500129 GrSurfaceDesc desc;
Greg Daniele728f672018-01-17 10:52:04 -0500130 desc.fWidth = fBackendTexture.width();
131 desc.fHeight = fBackendTexture.height();
132 desc.fConfig = fConfig;
133 GrMipMapped mipMapped = fBackendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
134
135 // Must make copies of member variables to capture in the lambda since this image generator may
136 // be deleted before we actuallly execute the lambda.
Greg Daniele728f672018-01-17 10:52:04 -0500137 sk_sp<GrSemaphore> semaphore = fSemaphore;
138 GrBackendTexture backendTexture = fBackendTexture;
139 RefHelper* refHelper = fRefHelper;
Greg Daniele728f672018-01-17 10:52:04 -0500140
Brian Salomonf391d0f2018-12-14 09:18:50 -0500141 GrBackendFormat format = backendTexture.getBackendFormat();
Greg Daniel4065d452018-11-16 15:43:41 -0500142 SkASSERT(format.isValid());
143
Robert Phillips777707b2018-01-17 11:40:14 -0500144 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
Brian Salomonc67c31c2018-12-06 10:00:03 -0500145 [refHelper, releaseProcHelper, semaphore,
146 backendTexture](GrResourceProvider* resourceProvider) {
Greg Daniele728f672018-01-17 10:52:04 -0500147 if (!resourceProvider) {
Greg Daniele728f672018-01-17 10:52:04 -0500148 return sk_sp<GrTexture>();
149 }
150
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 Salomonc67c31c2018-12-06 10:00:03 -0500170 tex = resourceProvider->wrapBackendTexture(
171 backendTexture, kBorrow_GrWrapOwnership, kRead_GrIOType, true);
Greg Daniele728f672018-01-17 10:52:04 -0500172 if (!tex) {
Greg Daniele728f672018-01-17 10:52:04 -0500173 return sk_sp<GrTexture>();
174 }
175 refHelper->fBorrowedTexture = tex.get();
176
Greg Danielabba9982018-02-01 10:07:57 -0500177 tex->setRelease(releaseProcHelper);
Greg Daniele728f672018-01-17 10:52:04 -0500178 }
179
Greg Daniele728f672018-01-17 10:52:04 -0500180 return tex;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500181 },
Brian Salomonc67c31c2018-12-06 10:00:03 -0500182 format, desc, fSurfaceOrigin, mipMapped, GrInternalSurfaceFlags::kReadOnly,
183 SkBackingFit::kExact, SkBudgeted::kNo);
Brian Osman13dddce2017-05-09 13:19:50 -0400184
Greg Daniele3204862018-04-16 11:24:10 -0400185 if (!proxy) {
186 return nullptr;
187 }
188
Brian Osman13dddce2017-05-09 13:19:50 -0400189 if (0 == origin.fX && 0 == origin.fY &&
Greg Daniel261b8aa2017-10-23 09:37:36 -0400190 info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height() &&
Greg Daniele252f082017-10-23 16:05:23 -0400191 (!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400192 // If the caller wants the entire texture and we have the correct mip support, we're done
Brian Osman13dddce2017-05-09 13:19:50 -0400193 return proxy;
194 } else {
195 // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable,
Greg Daniel261b8aa2017-10-23 09:37:36 -0400196 // because Vulkan will want to do the copy as a draw. All other copies would require a
197 // layout change in Vulkan and we do not change the layout of borrowed images.
Greg Daniel45d63032017-10-30 13:41:26 -0400198 GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
199
Greg Daniel4065d452018-11-16 15:43:41 -0500200 GrBackendFormat format = proxy->backendFormat().makeTexture2D();
201 if (!format.isValid()) {
202 return nullptr;
203 }
204
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500205 sk_sp<GrRenderTargetContext> rtContext(
206 context->contextPriv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500207 format, SkBackingFit::kExact, info.width(), info.height(),
208 proxy->config(), nullptr, 1, mipMapped, proxy->origin(), nullptr,
209 SkBudgeted::kYes));
Brian Salomon63e79732017-05-15 21:23:13 -0400210
Greg Daniel261b8aa2017-10-23 09:37:36 -0400211 if (!rtContext) {
Brian Osman13dddce2017-05-09 13:19:50 -0400212 return nullptr;
213 }
214
215 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
Greg Daniel261b8aa2017-10-23 09:37:36 -0400216 if (!rtContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
Brian Osman13dddce2017-05-09 13:19:50 -0400217 return nullptr;
218 }
219
Greg Daniel261b8aa2017-10-23 09:37:36 -0400220 return rtContext->asTextureProxyRef();
Brian Osman13dddce2017-05-09 13:19:50 -0400221 }
222}
Greg Daniel9cc28232018-04-06 10:00:09 -0400223