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" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 21 | |
| 22 | #include "SkGr.h" |
| 23 | #include "SkMessageBus.h" |
| 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, |
| 36 | sk_sp<GrSemaphore> semaphore, |
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 | 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 Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 48 | context->contextPriv().getResourceCache()->insertCrossContextGpuResource(texture.get()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 49 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 50 | GrBackendTexture backendTexture = texture->getBackendTexture(); |
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 | |
| 77 | #if SK_SUPPORT_GPU |
| 78 | void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) { |
| 79 | RefHelper* refHelper = static_cast<RefHelper*>(ctx); |
| 80 | SkASSERT(refHelper); |
| 81 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 82 | refHelper->fBorrowedTexture = 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( |
Christopher Cameron | 77e9666 | 2017-07-08 01:47:47 -0700 | [diff] [blame] | 88 | GrContext* context, const SkImageInfo& info, const SkIPoint& origin, |
Greg Daniel | f88c12e | 2017-10-09 09:57:35 -0400 | [diff] [blame] | 89 | SkTransferFunctionBehavior, bool willNeedMipMaps) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 90 | SkASSERT(context); |
| 91 | |
| 92 | if (context->contextPriv().getBackend() != fBackendTexture.backend()) { |
| 93 | return nullptr; |
| 94 | } |
| 95 | |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 96 | auto proxyProvider = context->contextPriv().proxyProvider(); |
| 97 | |
Greg Daniel | 966db9e | 2018-01-12 13:15:06 -0500 | [diff] [blame] | 98 | 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 Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | SkASSERT(fRefHelper->fBorrowingContextID == context->uniqueID()); |
| 107 | |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 108 | 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. |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 117 | sk_sp<GrSemaphore> semaphore = fSemaphore; |
| 118 | GrBackendTexture backendTexture = fBackendTexture; |
| 119 | RefHelper* refHelper = fRefHelper; |
| 120 | refHelper->ref(); |
| 121 | |
Robert Phillips | 777707b | 2018-01-17 11:40:14 -0500 | [diff] [blame] | 122 | sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy( |
Greg Daniel | f2336e4 | 2018-01-23 16:38:14 -0500 | [diff] [blame] | 123 | [refHelper, semaphore, backendTexture] |
| 124 | (GrResourceProvider* resourceProvider, GrSurfaceOrigin* /*outOrigin*/) { |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 125 | if (!resourceProvider) { |
| 126 | // If we get here then we never created a texture to pass the refHelper ref off |
| 127 | // to. Thus we must unref it ourselves. |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 128 | refHelper->fBorrowingContextID = SK_InvalidGenID; |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 129 | refHelper->unref(); |
| 130 | return sk_sp<GrTexture>(); |
| 131 | } |
| 132 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 133 | if (semaphore) { |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 134 | resourceProvider->priv().gpu()->waitSemaphore(semaphore); |
| 135 | } |
| 136 | |
| 137 | sk_sp<GrTexture> tex; |
| 138 | if (refHelper->fBorrowedTexture) { |
| 139 | // If a client re-draws the same image multiple times, the texture we return |
| 140 | // will be cached and re-used. If they draw a subset, though, we may be |
| 141 | // re-called. In that case, we want to re-use the borrowed texture we've |
| 142 | // previously created. |
| 143 | tex = sk_ref_sp(refHelper->fBorrowedTexture); |
| 144 | SkASSERT(tex); |
| 145 | // The texture is holding onto a ref to the refHelper so since we have a ref to |
| 146 | // the texture we don't need to hold a ref to the refHelper. This unref's the |
| 147 | // ref we grabbed on refHelper in the lambda capture for this proxy. |
| 148 | refHelper->unref(); |
| 149 | } else { |
| 150 | // We just gained access to the texture. If we're on the original context, we |
| 151 | // could use the original texture, but we'd have no way of detecting that it's |
| 152 | // no longer in-use. So we always make a wrapped copy, where the release proc |
| 153 | // informs us that the context is done with it. This is unfortunate - we'll have |
| 154 | // two texture objects referencing the same GPU object. However, no client can |
| 155 | // ever see the original texture, so this should be safe. |
| 156 | tex = resourceProvider->wrapBackendTexture(backendTexture, |
| 157 | kBorrow_GrWrapOwnership); |
| 158 | if (!tex) { |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 159 | refHelper->fBorrowingContextID = SK_InvalidGenID; |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 160 | refHelper->unref(); |
| 161 | return sk_sp<GrTexture>(); |
| 162 | } |
| 163 | refHelper->fBorrowedTexture = tex.get(); |
| 164 | |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 165 | sk_sp<GrReleaseProcHelper> releaseHelper( |
| 166 | new GrReleaseProcHelper(ReleaseRefHelper_TextureReleaseProc, refHelper)); |
| 167 | |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 168 | // By setting this release proc on the texture we are passing our ref on the |
| 169 | // refHelper to the texture. |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 170 | // DDL TODO: Once we are start reusing Lazy Proxies, we need to add a ref to the |
| 171 | // refHelper here, we'll still move the releaseHelper though since the texture |
| 172 | // will be the only one ever calling that release proc. |
| 173 | tex->setRelease(std::move(releaseHelper)); |
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; |
| 177 | |
| 178 | }, desc, mipMapped, SkBackingFit::kExact, SkBudgeted::kNo); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 179 | |
| 180 | if (0 == origin.fX && 0 == origin.fY && |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 181 | info.width() == fBackendTexture.width() && info.height() == fBackendTexture.height() && |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 182 | (!willNeedMipMaps || GrMipMapped::kYes == proxy->mipMapped())) { |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 183 | // 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] | 184 | return proxy; |
| 185 | } else { |
| 186 | // 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] | 187 | // because Vulkan will want to do the copy as a draw. All other copies would require a |
| 188 | // 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] | 189 | GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 190 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 191 | sk_sp<GrRenderTargetContext> rtContext(context->makeDeferredRenderTargetContext( |
| 192 | SkBackingFit::kExact, info.width(), info.height(), proxy->config(), nullptr, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 193 | 0, mipMapped, proxy->origin(), nullptr, SkBudgeted::kYes)); |
Brian Salomon | 63e7973 | 2017-05-15 21:23:13 -0400 | [diff] [blame] | 194 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 195 | if (!rtContext) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 196 | return nullptr; |
| 197 | } |
| 198 | |
| 199 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 200 | if (!rtContext->copy(proxy.get(), subset, SkIPoint::Make(0, 0))) { |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 201 | return nullptr; |
| 202 | } |
| 203 | |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 204 | return rtContext->asTextureProxyRef(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | #endif |