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 | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame^] | 43 | context->priv().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 | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 46 | GrBackendFormat backendFormat = backendTexture.getBackendFormat(); |
| 47 | if (!backendFormat.isValid()) { |
| 48 | return nullptr; |
| 49 | } |
| 50 | backendTexture.fConfig = |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame^] | 51 | context->priv().caps()->getConfigFromBackendFormat(backendFormat, colorType); |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 52 | if (backendTexture.fConfig == kUnknown_GrPixelConfig) { |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 53 | return nullptr; |
| 54 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 55 | |
| 56 | SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType, |
| 57 | std::move(colorSpace)); |
| 58 | return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator( |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame^] | 59 | info, texture.get(), origin, context->priv().contextID(), |
Robert Phillips | fd0d970 | 2019-02-01 10:19:42 -0500 | [diff] [blame] | 60 | std::move(semaphore), backendTexture)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | GrBackendTextureImageGenerator::GrBackendTextureImageGenerator(const SkImageInfo& info, |
| 64 | GrTexture* texture, |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 65 | GrSurfaceOrigin origin, |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 66 | uint32_t owningContextID, |
| 67 | sk_sp<GrSemaphore> semaphore, |
| 68 | const GrBackendTexture& backendTex) |
| 69 | : INHERITED(info) |
| 70 | , fRefHelper(new RefHelper(texture, owningContextID)) |
| 71 | , fSemaphore(std::move(semaphore)) |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 72 | , fBackendTexture(backendTex) |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 73 | , fConfig(backendTex.config()) |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 74 | , fSurfaceOrigin(origin) { } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 75 | |
| 76 | GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() { |
| 77 | fRefHelper->unref(); |
| 78 | } |
| 79 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 80 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 81 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 82 | void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) { |
| 83 | RefHelper* refHelper = static_cast<RefHelper*>(ctx); |
| 84 | SkASSERT(refHelper); |
| 85 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 86 | refHelper->fBorrowedTexture = nullptr; |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 87 | refHelper->fBorrowingContextReleaseProc = nullptr; |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 88 | refHelper->fBorrowingContextID = SK_InvalidGenID; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 89 | refHelper->unref(); |
| 90 | } |
| 91 | |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 92 | sk_sp<GrTextureProxy> GrBackendTextureImageGenerator::onGenerateTexture( |
Brian Osman | c87cfb6 | 2018-07-11 09:08:46 -0400 | [diff] [blame] | 93 | GrContext* context, const SkImageInfo& info, const SkIPoint& origin, bool willNeedMipMaps) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 94 | SkASSERT(context); |
| 95 | |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 96 | if (context->backend() != fBackendTexture.backend()) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 97 | return nullptr; |
| 98 | } |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 99 | if (info.colorType() != this->getInfo().colorType()) { |
| 100 | return nullptr; |
| 101 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 102 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame^] | 103 | auto proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 104 | |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 105 | fBorrowingMutex.acquire(); |
| 106 | sk_sp<GrReleaseProcHelper> releaseProcHelper; |
| 107 | if (SK_InvalidGenID != fRefHelper->fBorrowingContextID) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame^] | 108 | if (fRefHelper->fBorrowingContextID != context->priv().contextID()) { |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 109 | fBorrowingMutex.release(); |
Greg Daniel | 966db9e | 2018-01-12 13:15:06 -0500 | [diff] [blame] | 110 | return nullptr; |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 111 | } else { |
| 112 | SkASSERT(fRefHelper->fBorrowingContextReleaseProc); |
| 113 | // Ref the release proc to be held by the proxy we make below |
| 114 | releaseProcHelper = sk_ref_sp(fRefHelper->fBorrowingContextReleaseProc); |
Greg Daniel | 966db9e | 2018-01-12 13:15:06 -0500 | [diff] [blame] | 115 | } |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 116 | } else { |
| 117 | SkASSERT(!fRefHelper->fBorrowingContextReleaseProc); |
| 118 | // The ref we add to fRefHelper here will be passed into and owned by the |
| 119 | // GrReleaseProcHelper. |
| 120 | fRefHelper->ref(); |
| 121 | releaseProcHelper.reset(new GrReleaseProcHelper(ReleaseRefHelper_TextureReleaseProc, |
| 122 | fRefHelper)); |
| 123 | fRefHelper->fBorrowingContextReleaseProc = releaseProcHelper.get(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 124 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame^] | 125 | fRefHelper->fBorrowingContextID = context->priv().contextID(); |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 126 | fBorrowingMutex.release(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 127 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame^] | 128 | SkASSERT(fRefHelper->fBorrowingContextID == context->priv().contextID()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 129 | |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 130 | GrSurfaceDesc desc; |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 131 | desc.fWidth = fBackendTexture.width(); |
| 132 | desc.fHeight = fBackendTexture.height(); |
| 133 | desc.fConfig = fConfig; |
| 134 | GrMipMapped mipMapped = fBackendTexture.hasMipMaps() ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 135 | |
| 136 | // Must make copies of member variables to capture in the lambda since this image generator may |
| 137 | // be deleted before we actuallly execute the lambda. |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 138 | sk_sp<GrSemaphore> semaphore = fSemaphore; |
| 139 | GrBackendTexture backendTexture = fBackendTexture; |
| 140 | RefHelper* refHelper = fRefHelper; |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 141 | |
Brian Salomon | f391d0f | 2018-12-14 09:18:50 -0500 | [diff] [blame] | 142 | GrBackendFormat format = backendTexture.getBackendFormat(); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 143 | SkASSERT(format.isValid()); |
| 144 | |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 145 | sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy( |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 146 | [refHelper, releaseProcHelper, semaphore, |
| 147 | backendTexture](GrResourceProvider* resourceProvider) { |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 148 | if (!resourceProvider) { |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 149 | return sk_sp<GrTexture>(); |
| 150 | } |
| 151 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 152 | if (semaphore) { |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 153 | resourceProvider->priv().gpu()->waitSemaphore(semaphore); |
| 154 | } |
| 155 | |
| 156 | sk_sp<GrTexture> tex; |
| 157 | if (refHelper->fBorrowedTexture) { |
| 158 | // If a client re-draws the same image multiple times, the texture we return |
| 159 | // will be cached and re-used. If they draw a subset, though, we may be |
| 160 | // re-called. In that case, we want to re-use the borrowed texture we've |
| 161 | // previously created. |
| 162 | tex = sk_ref_sp(refHelper->fBorrowedTexture); |
| 163 | SkASSERT(tex); |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 164 | } else { |
| 165 | // We just gained access to the texture. If we're on the original context, we |
| 166 | // could use the original texture, but we'd have no way of detecting that it's |
| 167 | // no longer in-use. So we always make a wrapped copy, where the release proc |
| 168 | // informs us that the context is done with it. This is unfortunate - we'll have |
| 169 | // two texture objects referencing the same GPU object. However, no client can |
| 170 | // ever see the original texture, so this should be safe. |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 171 | // We make the texture uncacheable so that the release proc is called ASAP. |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 172 | tex = resourceProvider->wrapBackendTexture( |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 173 | backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, |
| 174 | kRead_GrIOType); |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 175 | if (!tex) { |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 176 | return sk_sp<GrTexture>(); |
| 177 | } |
| 178 | refHelper->fBorrowedTexture = tex.get(); |
| 179 | |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 180 | tex->setRelease(releaseProcHelper); |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 183 | return tex; |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 184 | }, |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 185 | format, desc, fSurfaceOrigin, mipMapped, GrInternalSurfaceFlags::kReadOnly, |
| 186 | SkBackingFit::kExact, SkBudgeted::kNo); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 187 | |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 188 | if (!proxy) { |
| 189 | return nullptr; |
| 190 | } |
| 191 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 192 | if (0 == origin.fX && 0 == origin.fY && |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 193 | info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height() && |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 194 | (!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped())) { |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 195 | // 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] | 196 | return proxy; |
| 197 | } else { |
| 198 | // 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] | 199 | // because Vulkan will want to do the copy as a draw. All other copies would require a |
| 200 | // 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] | 201 | GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 202 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 203 | GrBackendFormat format = proxy->backendFormat().makeTexture2D(); |
| 204 | if (!format.isValid()) { |
| 205 | return nullptr; |
| 206 | } |
| 207 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 208 | sk_sp<GrRenderTargetContext> rtContext( |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame^] | 209 | context->priv().makeDeferredRenderTargetContext( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 210 | format, SkBackingFit::kExact, info.width(), info.height(), |
| 211 | proxy->config(), nullptr, 1, mipMapped, proxy->origin(), nullptr, |
| 212 | SkBudgeted::kYes)); |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 213 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 214 | if (!rtContext) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 215 | return nullptr; |
| 216 | } |
| 217 | |
| 218 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 219 | if (!rtContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 220 | return nullptr; |
| 221 | } |
| 222 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 223 | return rtContext->asTextureProxyRef(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 224 | } |
| 225 | } |
Greg Daniel | 9cc2823 | 2018-04-06 10:00:09 -0400 | [diff] [blame] | 226 | |