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