Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 1 | /* |
| 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 Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 9 | #include "GrContext.h" |
| 10 | #include "GrContextPriv.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 11 | #include "GrGpu.h" |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 12 | #include "GrProxyProvider.h" |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 13 | #include "GrRenderTargetContext.h" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 14 | #include "GrResourceCache.h" |
| 15 | #include "GrResourceProvider.h" |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 16 | #include "GrResourceProviderPriv.h" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 17 | #include "GrSemaphore.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 18 | #include "GrTexture.h" |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 19 | #include "GrTexturePriv.h" |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 20 | #include "GrTextureProxyPriv.h" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 21 | #include "SkGr.h" |
| 22 | #include "SkMessageBus.h" |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 23 | #include "gl/GrGLTexture.h" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 24 | |
| 25 | GrBackendTextureImageGenerator::RefHelper::~RefHelper() { |
| 26 | SkASSERT(nullptr == fBorrowedTexture); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 27 | |
| 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 Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 34 | std::unique_ptr<SkImageGenerator> |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 35 | GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, GrSurfaceOrigin origin, |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 36 | sk_sp<GrSemaphore> semaphore, SkColorType colorType, |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 37 | SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 38 | 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 Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 43 | context->contextPriv().getResourceCache()->insertCrossContextGpuResource(texture.get()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 44 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 45 | GrBackendTexture backendTexture = texture->getBackendTexture(); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 46 | if (!context->contextPriv().caps()->validateBackendTexture(backendTexture, colorType, |
| 47 | &backendTexture.fConfig)) { |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 48 | return nullptr; |
| 49 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 50 | |
| 51 | SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType, |
| 52 | std::move(colorSpace)); |
| 53 | return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator( |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 54 | info, texture.get(), origin, context->uniqueID(), std::move(semaphore), backendTexture)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info, |
| 58 | GrTexture* texture, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 59 | GrSurfaceOrigin origin, |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 60 | 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 Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 66 | , fBackendTexture(backendTex) |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 67 | , fConfig(backendTex.config()) |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 68 | , fSurfaceOrigin(origin) { } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 69 | |
| 70 | GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() { |
| 71 | fRefHelper->unref(); |
| 72 | } |
| 73 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 74 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 75 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 76 | void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) { |
| 77 | RefHelper* refHelper = static_cast<RefHelper*>(ctx); |
| 78 | SkASSERT(refHelper); |
| 79 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 80 | refHelper->fBorrowedTexture = nullptr; |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 81 | refHelper->fBorrowingContextReleaseProc = nullptr; |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 82 | refHelper->fBorrowingContextID = SK_InvalidGenID; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 83 | refHelper->unref(); |
| 84 | } |
| 85 | |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 86 | sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture( |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 87 | GrContext* context, const SkImageInfo& info, const SkIPoint& origin, bool willNeedMipMaps) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 88 | SkASSERT(context); |
| 89 | |
| 90 | if (context->contextPriv().getBackend() != fBackendTexture.backend()) { |
| 91 | return nullptr; |
| 92 | } |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 93 | if (info.colorType() != this->getInfo().colorType()) { |
| 94 | return nullptr; |
| 95 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 96 | |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 97 | auto proxyProvider = context->contextPriv().proxyProvider(); |
| 98 | |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 99 | fBorrowingMutex.acquire(); |
| 100 | sk_sp<GrReleaseProcHelper> releaseProcHelper; |
| 101 | if (SK_InvalidGenID != fRefHelper->fBorrowingContextID) { |
Greg Daniel | 966db9e | 2018-01-12 13:15:06 -0500 | [diff] [blame] | 102 | if (fRefHelper->fBorrowingContextID != context->uniqueID()) { |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 103 | fBorrowingMutex.release(); |
Greg Daniel | 966db9e | 2018-01-12 13:15:06 -0500 | [diff] [blame] | 104 | return nullptr; |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 105 | } 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 Daniel | 966db9e | 2018-01-12 13:15:06 -0500 | [diff] [blame] | 109 | } |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 110 | } 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 Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 118 | } |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 119 | fRefHelper->fBorrowingContextID = context->uniqueID(); |
| 120 | fBorrowingMutex.release(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 121 | |
| 122 | SkASSERT(fRefHelper->fBorrowingContextID == context->uniqueID()); |
| 123 | |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 124 | GrSurfaceDesc desc; |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 125 | 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 Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 132 | sk_sp<GrSemaphore> semaphore = fSemaphore; |
| 133 | GrBackendTexture backendTexture = fBackendTexture; |
| 134 | RefHelper* refHelper = fRefHelper; |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 135 | |
Greg Daniel | 2f9a5ea | 2018-11-12 11:09:22 -0500 | [diff] [blame^] | 136 | GrBackendFormat format = |
| 137 | context->contextPriv().caps()->createFormatFromBackendTexture(backendTexture); |
| 138 | SkASSERT(format.isValid()); |
| 139 | |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 140 | sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy( |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 141 | [refHelper, releaseProcHelper, semaphore, backendTexture] |
| 142 | (GrResourceProvider* resourceProvider) { |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 143 | if (!resourceProvider) { |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 144 | return sk_sp<GrTexture>(); |
| 145 | } |
| 146 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 147 | if (semaphore) { |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 148 | resourceProvider->priv().gpu()->waitSemaphore(semaphore); |
| 149 | } |
| 150 | |
| 151 | sk_sp<GrTexture> tex; |
| 152 | if (refHelper->fBorrowedTexture) { |
| 153 | // If a client re-draws the same image multiple times, the texture we return |
| 154 | // will be cached and re-used. If they draw a subset, though, we may be |
| 155 | // re-called. In that case, we want to re-use the borrowed texture we've |
| 156 | // previously created. |
| 157 | tex = sk_ref_sp(refHelper->fBorrowedTexture); |
| 158 | SkASSERT(tex); |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 159 | } else { |
| 160 | // We just gained access to the texture. If we're on the original context, we |
| 161 | // could use the original texture, but we'd have no way of detecting that it's |
| 162 | // no longer in-use. So we always make a wrapped copy, where the release proc |
| 163 | // informs us that the context is done with it. This is unfortunate - we'll have |
| 164 | // two texture objects referencing the same GPU object. However, no client can |
| 165 | // ever see the original texture, so this should be safe. |
| 166 | tex = resourceProvider->wrapBackendTexture(backendTexture, |
| 167 | kBorrow_GrWrapOwnership); |
| 168 | if (!tex) { |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 169 | return sk_sp<GrTexture>(); |
| 170 | } |
| 171 | refHelper->fBorrowedTexture = tex.get(); |
| 172 | |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 173 | tex->setRelease(releaseProcHelper); |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 174 | } |
| 175 | |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 176 | return tex; |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 177 | }, |
Greg Daniel | 2f9a5ea | 2018-11-12 11:09:22 -0500 | [diff] [blame^] | 178 | format, desc, fSurfaceOrigin, mipMapped, SkBackingFit::kExact, SkBudgeted::kNo); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 179 | |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 180 | if (!proxy) { |
| 181 | return nullptr; |
| 182 | } |
| 183 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 184 | if (0 == origin.fX && 0 == origin.fY && |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 185 | info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height() && |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 186 | (!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped())) { |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 187 | // If the caller wants the entire texture and we have the correct mip support, we're done |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 188 | return proxy; |
| 189 | } else { |
| 190 | // Otherwise, make a copy of the requested subset. Make sure our temporary is renderable, |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 191 | // because Vulkan will want to do the copy as a draw. All other copies would require a |
| 192 | // layout change in Vulkan and we do not change the layout of borrowed images. |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 193 | GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 194 | |
Greg Daniel | 2f9a5ea | 2018-11-12 11:09:22 -0500 | [diff] [blame^] | 195 | GrBackendFormat format = proxy->backendFormat().makeTexture2D(); |
| 196 | if (!format.isValid()) { |
| 197 | return nullptr; |
| 198 | } |
| 199 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 200 | sk_sp<GrRenderTargetContext> rtContext( |
| 201 | context->contextPriv().makeDeferredRenderTargetContext( |
Greg Daniel | 2f9a5ea | 2018-11-12 11:09:22 -0500 | [diff] [blame^] | 202 | format, SkBackingFit::kExact, info.width(), info.height(), |
| 203 | proxy->config(), nullptr, 1, mipMapped, proxy->origin(), nullptr, |
| 204 | SkBudgeted::kYes)); |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 205 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 206 | if (!rtContext) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 207 | return nullptr; |
| 208 | } |
| 209 | |
| 210 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 211 | if (!rtContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 212 | return nullptr; |
| 213 | } |
| 214 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 215 | return rtContext->asTextureProxyRef(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 216 | } |
| 217 | } |
Greg Daniel | 9cc2823 | 2018-04-06 10:00:09 -0400 | [diff] [blame] | 218 | |