blob: b07115ef2d89ad21799702b4350cd584da9fa0c9 [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"
9
10#include "GrContext.h"
11#include "GrContextPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040012#include "GrGpu.h"
Robert Phillips777707b2018-01-17 11:40:14 -050013#include "GrProxyProvider.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040014#include "GrRenderTargetContext.h"
Brian Osman13dddce2017-05-09 13:19:50 -040015#include "GrResourceCache.h"
16#include "GrResourceProvider.h"
Greg Daniele728f672018-01-17 10:52:04 -050017#include "GrResourceProviderPriv.h"
Brian Osman13dddce2017-05-09 13:19:50 -040018#include "GrSemaphore.h"
Robert Phillips646e4292017-06-13 12:44:56 -040019#include "GrTexture.h"
Greg Daniel261b8aa2017-10-23 09:37:36 -040020#include "GrTexturePriv.h"
Brian Osman13dddce2017-05-09 13:19:50 -040021
22#include "SkGr.h"
23#include "SkMessageBus.h"
24
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,
36 sk_sp<GrSemaphore> semaphore,
Brian Osman13dddce2017-05-09 13:19:50 -040037 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
Brian Osman13dddce2017-05-09 13:19:50 -040038 SkColorType colorType = kUnknown_SkColorType;
39 if (!GrPixelConfigToColorType(texture->config(), &colorType)) {
40 return nullptr;
41 }
42
43 GrContext* context = texture->getContext();
44
45 // Attach our texture to this context's resource cache. This ensures that deletion will happen
46 // in the correct thread/context. This adds the only ref to the texture that will persist from
47 // this point. That ref will be released when the generator's RefHelper is freed.
Robert Phillips6be756b2018-01-16 15:07:54 -050048 context->contextPriv().getResourceCache()->insertCrossContextGpuResource(texture.get());
Brian Osman13dddce2017-05-09 13:19:50 -040049
Robert Phillipsb67821d2017-12-13 15:00:45 -050050 GrBackendTexture backendTexture = texture->getBackendTexture();
Brian Osman13dddce2017-05-09 13:19:50 -040051
52 SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType,
53 std::move(colorSpace));
54 return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator(
Robert Phillipsb0e93a22017-08-29 08:26:54 -040055 info, texture.get(), origin, context->uniqueID(), std::move(semaphore), backendTexture));
Brian Osman13dddce2017-05-09 13:19:50 -040056}
57
58GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info,
59 GrTexture* texture,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040060 GrSurfaceOrigin origin,
Brian Osman13dddce2017-05-09 13:19:50 -040061 uint32_t owningContextID,
62 sk_sp<GrSemaphore> semaphore,
63 const GrBackendTexture& backendTex)
64 : INHERITED(info)
65 , fRefHelper(new RefHelper(texture, owningContextID))
66 , fSemaphore(std::move(semaphore))
Brian Osman13dddce2017-05-09 13:19:50 -040067 , fBackendTexture(backendTex)
Greg Daniele728f672018-01-17 10:52:04 -050068 , fConfig(backendTex.config())
Robert Phillipsb0e93a22017-08-29 08:26:54 -040069 , fSurfaceOrigin(origin) { }
Brian Osman13dddce2017-05-09 13:19:50 -040070
71GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() {
72 fRefHelper->unref();
73}
74
Brian Osman13dddce2017-05-09 13:19:50 -040075///////////////////////////////////////////////////////////////////////////////////////////////////
76
77#if SK_SUPPORT_GPU
78void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) {
79 RefHelper* refHelper = static_cast<RefHelper*>(ctx);
80 SkASSERT(refHelper);
81
Brian Osman13dddce2017-05-09 13:19:50 -040082 refHelper->fBorrowedTexture = nullptr;
Greg Daniel48661b82018-01-22 16:11:35 -050083 refHelper->fBorrowingContextID = SK_InvalidGenID;
Brian Osman13dddce2017-05-09 13:19:50 -040084 refHelper->unref();
85}
86
Stan Ilievba81af22017-06-08 15:16:53 -040087sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture(
Christopher Cameron77e96662017-07-08 01:47:47 -070088 GrContext* context, const SkImageInfo& info, const SkIPoint& origin,
Greg Danielf88c12e2017-10-09 09:57:35 -040089 SkTransferFunctionBehavior, bool willNeedMipMaps) {
Brian Osman13dddce2017-05-09 13:19:50 -040090 SkASSERT(context);
91
92 if (context->contextPriv().getBackend() != fBackendTexture.backend()) {
93 return nullptr;
94 }
95
Robert Phillips777707b2018-01-17 11:40:14 -050096 auto proxyProvider = context->contextPriv().proxyProvider();
97
Greg Daniel966db9e2018-01-12 13:15:06 -050098 uint32_t expectedID = SK_InvalidGenID;
99 if (!fRefHelper->fBorrowingContextID.compare_exchange(&expectedID, context->uniqueID())) {
100 if (fRefHelper->fBorrowingContextID != context->uniqueID()) {
101 // Some other context is currently borrowing the texture. We aren't allowed to use it.
102 return nullptr;
103 }
Brian Osman13dddce2017-05-09 13:19:50 -0400104 }
105
106 SkASSERT(fRefHelper->fBorrowingContextID == context->uniqueID());
107
Greg Daniele728f672018-01-17 10:52:04 -0500108 GrSurfaceDesc desc;
109 desc.fOrigin = fSurfaceOrigin;
110 desc.fWidth = fBackendTexture.width();
111 desc.fHeight = fBackendTexture.height();
112 desc.fConfig = fConfig;
113 GrMipMapped mipMapped = fBackendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo;
114
115 // Must make copies of member variables to capture in the lambda since this image generator may
116 // be deleted before we actuallly execute the lambda.
117 GrSurfaceOrigin surfaceOrigin = fSurfaceOrigin;
118 sk_sp<GrSemaphore> semaphore = fSemaphore;
119 GrBackendTexture backendTexture = fBackendTexture;
120 RefHelper* refHelper = fRefHelper;
121 refHelper->ref();
122
Robert Phillips777707b2018-01-17 11:40:14 -0500123 sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy(
Greg Daniele728f672018-01-17 10:52:04 -0500124 [refHelper, semaphore, backendTexture, surfaceOrigin]
125 (GrResourceProvider* resourceProvider, GrSurfaceOrigin* outOrigin) {
126 if (!resourceProvider) {
127 // If we get here then we never created a texture to pass the refHelper ref off
128 // to. Thus we must unref it ourselves.
Greg Daniel48661b82018-01-22 16:11:35 -0500129 refHelper->fBorrowingContextID = SK_InvalidGenID;
Greg Daniele728f672018-01-17 10:52:04 -0500130 refHelper->unref();
131 return sk_sp<GrTexture>();
132 }
133
Greg Daniel48661b82018-01-22 16:11:35 -0500134 if (semaphore) {
Greg Daniele728f672018-01-17 10:52:04 -0500135 resourceProvider->priv().gpu()->waitSemaphore(semaphore);
136 }
137
138 sk_sp<GrTexture> tex;
139 if (refHelper->fBorrowedTexture) {
140 // If a client re-draws the same image multiple times, the texture we return
141 // will be cached and re-used. If they draw a subset, though, we may be
142 // re-called. In that case, we want to re-use the borrowed texture we've
143 // previously created.
144 tex = sk_ref_sp(refHelper->fBorrowedTexture);
145 SkASSERT(tex);
146 // The texture is holding onto a ref to the refHelper so since we have a ref to
147 // the texture we don't need to hold a ref to the refHelper. This unref's the
148 // ref we grabbed on refHelper in the lambda capture for this proxy.
149 refHelper->unref();
150 } else {
151 // We just gained access to the texture. If we're on the original context, we
152 // could use the original texture, but we'd have no way of detecting that it's
153 // no longer in-use. So we always make a wrapped copy, where the release proc
154 // informs us that the context is done with it. This is unfortunate - we'll have
155 // two texture objects referencing the same GPU object. However, no client can
156 // ever see the original texture, so this should be safe.
157 tex = resourceProvider->wrapBackendTexture(backendTexture,
158 kBorrow_GrWrapOwnership);
159 if (!tex) {
Greg Daniel48661b82018-01-22 16:11:35 -0500160 refHelper->fBorrowingContextID = SK_InvalidGenID;
Greg Daniele728f672018-01-17 10:52:04 -0500161 refHelper->unref();
162 return sk_sp<GrTexture>();
163 }
164 refHelper->fBorrowedTexture = tex.get();
165
166 // By setting this release proc on the texture we are passing our ref on the
167 // refHelper to the texture.
168 tex->setRelease(ReleaseRefHelper_TextureReleaseProc, refHelper);
169 }
170
171 *outOrigin = surfaceOrigin;
172 return tex;
173
174 }, desc, mipMapped, SkBackingFit::kExact, SkBudgeted::kNo);
Brian Osman13dddce2017-05-09 13:19:50 -0400175
176 if (0 == origin.fX && 0 == origin.fY &&
Greg Daniel261b8aa2017-10-23 09:37:36 -0400177 info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height() &&
Greg Daniele252f082017-10-23 16:05:23 -0400178 (!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Daniel261b8aa2017-10-23 09:37:36 -0400179 // If the caller wants the entire texture and we have the correct mip support, we're done
Brian Osman13dddce2017-05-09 13:19:50 -0400180 return proxy;
181 } else {
182 // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable,
Greg Daniel261b8aa2017-10-23 09:37:36 -0400183 // because Vulkan will want to do the copy as a draw. All other copies would require a
184 // layout change in Vulkan and we do not change the layout of borrowed images.
Greg Daniel45d63032017-10-30 13:41:26 -0400185 GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
186
Greg Daniel261b8aa2017-10-23 09:37:36 -0400187 sk_sp<GrRenderTargetContext> rtContext(context->makeDeferredRenderTargetContext(
188 SkBackingFit::kExact, info.width(), info.height(), proxy->config(), nullptr,
Greg Daniel45d63032017-10-30 13:41:26 -0400189 0, mipMapped, proxy->origin(), nullptr, SkBudgeted::kYes));
Brian Salomon63e79732017-05-15 21:23:13 -0400190
Greg Daniel261b8aa2017-10-23 09:37:36 -0400191 if (!rtContext) {
Brian Osman13dddce2017-05-09 13:19:50 -0400192 return nullptr;
193 }
194
195 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height());
Greg Daniel261b8aa2017-10-23 09:37:36 -0400196 if (!rtContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) {
Brian Osman13dddce2017-05-09 13:19:50 -0400197 return nullptr;
198 }
199
Greg Daniel261b8aa2017-10-23 09:37:36 -0400200 return rtContext->asTextureProxyRef();
Brian Osman13dddce2017-05-09 13:19:50 -0400201 }
202}
203#endif