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