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 | |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 8 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 9 | #include "include/gpu/GrRecordingContext.h" |
Ben Wagner | 21bca28 | 2019-05-15 10:15:52 -0400 | [diff] [blame] | 10 | #include "src/core/SkMessageBus.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrBackendTextureImageGenerator.h" |
| 12 | #include "src/gpu/GrContextPriv.h" |
| 13 | #include "src/gpu/GrGpu.h" |
| 14 | #include "src/gpu/GrProxyProvider.h" |
| 15 | #include "src/gpu/GrRecordingContextPriv.h" |
| 16 | #include "src/gpu/GrRenderTargetContext.h" |
| 17 | #include "src/gpu/GrResourceCache.h" |
| 18 | #include "src/gpu/GrResourceProvider.h" |
| 19 | #include "src/gpu/GrResourceProviderPriv.h" |
| 20 | #include "src/gpu/GrSemaphore.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 21 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 27 | GrBackendTextureImageGenerator::RefHelper::RefHelper(GrTexture* texture, uint32_t owningContextID, |
| 28 | std::unique_ptr<GrSemaphore> semaphore) |
Brian Salomon | b916b7b | 2019-04-01 13:34:34 -0400 | [diff] [blame] | 29 | : fOriginalTexture(texture) |
| 30 | , fOwningContextID(owningContextID) |
| 31 | , fBorrowingContextReleaseProc(nullptr) |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 32 | , fBorrowingContextID(SK_InvalidGenID) |
| 33 | , fSemaphore(std::move(semaphore)) {} |
Brian Salomon | b916b7b | 2019-04-01 13:34:34 -0400 | [diff] [blame] | 34 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 35 | GrBackendTextureImageGenerator::RefHelper::~RefHelper() { |
Brian Salomon | b916b7b | 2019-04-01 13:34:34 -0400 | [diff] [blame] | 36 | SkASSERT(fBorrowingContextID == SK_InvalidUniqueID); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 37 | |
| 38 | // Generator has been freed, and no one is borrowing the texture. Notify the original cache |
| 39 | // that it can free the last ref, so it happens on the correct thread. |
Robert Phillips | ddc2148 | 2019-10-16 14:30:09 -0400 | [diff] [blame] | 40 | GrTextureFreedMessage msg { fOriginalTexture, fOwningContextID }; |
| 41 | SkMessageBus<GrTextureFreedMessage>::Post(msg); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 44 | std::unique_ptr<SkImageGenerator> |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 45 | GrBackendTextureImageGenerator::Make(sk_sp<GrTexture> texture, GrSurfaceOrigin origin, |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 46 | std::unique_ptr<GrSemaphore> semaphore, SkColorType colorType, |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 47 | SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) { |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 48 | GrDirectContext* context = texture->getContext(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 49 | |
| 50 | // Attach our texture to this context's resource cache. This ensures that deletion will happen |
| 51 | // in the correct thread/context. This adds the only ref to the texture that will persist from |
| 52 | // this point. That ref will be released when the generator's RefHelper is freed. |
Robert Phillips | ddc2148 | 2019-10-16 14:30:09 -0400 | [diff] [blame] | 53 | context->priv().getResourceCache()->insertDelayedTextureUnref(texture.get()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 54 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 55 | GrBackendTexture backendTexture = texture->getBackendTexture(); |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 56 | |
Robert Phillips | 62221e7 | 2019-07-24 15:07:38 -0400 | [diff] [blame] | 57 | if (!context->priv().caps()->areColorTypeAndFormatCompatible( |
| 58 | SkColorTypeToGrColorType(colorType), backendTexture.getBackendFormat())) { |
| 59 | return nullptr; |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 60 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 61 | |
| 62 | SkImageInfo info = SkImageInfo::Make(texture->width(), texture->height(), colorType, alphaType, |
| 63 | std::move(colorSpace)); |
| 64 | return std::unique_ptr<SkImageGenerator>(new GrBackendTextureImageGenerator( |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 65 | info, texture.get(), origin, context->priv().contextID(), |
Robert Phillips | fd0d970 | 2019-02-01 10:19:42 -0500 | [diff] [blame] | 66 | std::move(semaphore), backendTexture)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 69 | GrBackendTextureImageGenerator::GrBackendTextureImageGenerator( |
| 70 | const SkImageInfo& info, |
| 71 | GrTexture* texture, |
| 72 | GrSurfaceOrigin origin, |
| 73 | uint32_t owningContextID, |
| 74 | std::unique_ptr<GrSemaphore> semaphore, |
| 75 | const GrBackendTexture& backendTex) |
Brian Salomon | b916b7b | 2019-04-01 13:34:34 -0400 | [diff] [blame] | 76 | : INHERITED(info) |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 77 | , fRefHelper(new RefHelper(texture, owningContextID, std::move(semaphore))) |
Brian Salomon | b916b7b | 2019-04-01 13:34:34 -0400 | [diff] [blame] | 78 | , fBackendTexture(backendTex) |
Brian Salomon | b916b7b | 2019-04-01 13:34:34 -0400 | [diff] [blame] | 79 | , fSurfaceOrigin(origin) {} |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 80 | |
| 81 | GrBackendTextureImageGenerator::~GrBackendTextureImageGenerator() { |
| 82 | fRefHelper->unref(); |
| 83 | } |
| 84 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 85 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 86 | |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 87 | void GrBackendTextureImageGenerator::ReleaseRefHelper_TextureReleaseProc(void* ctx) { |
| 88 | RefHelper* refHelper = static_cast<RefHelper*>(ctx); |
| 89 | SkASSERT(refHelper); |
| 90 | |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 91 | refHelper->fBorrowingContextReleaseProc = nullptr; |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 92 | refHelper->fBorrowingContextID = SK_InvalidGenID; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 93 | refHelper->unref(); |
| 94 | } |
| 95 | |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 96 | GrSurfaceProxyView GrBackendTextureImageGenerator::onGenerateTexture( |
| 97 | GrRecordingContext* context, |
| 98 | const SkImageInfo& info, |
| 99 | const SkIPoint& origin, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 100 | GrMipmapped mipMapped, |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 101 | GrImageTexGenPolicy texGenPolicy) { |
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()) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 105 | return {}; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 106 | } |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 107 | if (info.colorType() != this->getInfo().colorType()) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 108 | return {}; |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 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(); |
Chris Dalton | 5a5fe79 | 2020-02-15 11:41:30 -0700 | [diff] [blame] | 118 | context->priv().printWarningMessage( |
| 119 | "GrBackendTextureImageGenerator: Trying to use texture on two GrContexts!\n"); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 120 | return {}; |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 121 | } else { |
| 122 | SkASSERT(fRefHelper->fBorrowingContextReleaseProc); |
| 123 | // Ref the release proc to be held by the proxy we make below |
| 124 | releaseProcHelper = sk_ref_sp(fRefHelper->fBorrowingContextReleaseProc); |
Greg Daniel | 966db9e | 2018-01-12 13:15:06 -0500 | [diff] [blame] | 125 | } |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 126 | } else { |
| 127 | SkASSERT(!fRefHelper->fBorrowingContextReleaseProc); |
| 128 | // 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] | 129 | // GrRefCntedCallback. |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 130 | fRefHelper->ref(); |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame] | 131 | releaseProcHelper.reset( |
| 132 | new GrRefCntedCallback(ReleaseRefHelper_TextureReleaseProc, fRefHelper)); |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 133 | fRefHelper->fBorrowingContextReleaseProc = releaseProcHelper.get(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 134 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 135 | fRefHelper->fBorrowingContextID = context->priv().contextID(); |
Brian Salomon | b916b7b | 2019-04-01 13:34:34 -0400 | [diff] [blame] | 136 | if (!fRefHelper->fBorrowedTextureKey.isValid()) { |
| 137 | static const auto kDomain = GrUniqueKey::GenerateDomain(); |
| 138 | GrUniqueKey::Builder builder(&fRefHelper->fBorrowedTextureKey, kDomain, 1); |
| 139 | builder[0] = this->uniqueID(); |
| 140 | } |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 141 | fBorrowingMutex.release(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 142 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 143 | SkASSERT(fRefHelper->fBorrowingContextID == context->priv().contextID()); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 144 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 145 | GrBackendFormat backendFormat = fBackendTexture.getBackendFormat(); |
| 146 | SkASSERT(backendFormat.isValid()); |
| 147 | |
| 148 | GrColorType grColorType = SkColorTypeToGrColorType(info.colorType()); |
| 149 | |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 150 | GrMipmapped textureIsMipMapped = fBackendTexture.hasMipmaps() ? GrMipmapped::kYes |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 151 | : GrMipmapped::kNo; |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 152 | |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 153 | // Ganesh assumes that, when wrapping a mipmapped backend texture from a client, that its |
| 154 | // mipmaps are fully fleshed out. |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 155 | GrMipmapStatus mipmapStatus = fBackendTexture.hasMipmaps() |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 156 | ? GrMipmapStatus::kValid : GrMipmapStatus::kNotAllocated; |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 157 | |
Greg Daniel | ce3ddaa | 2020-01-22 16:58:15 -0500 | [diff] [blame] | 158 | GrSwizzle readSwizzle = context->priv().caps()->getReadSwizzle(backendFormat, grColorType); |
| 159 | |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 160 | // Must make copies of member variables to capture in the lambda since this image generator may |
| 161 | // be deleted before we actually execute the lambda. |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 162 | sk_sp<GrTextureProxy> proxy = proxyProvider->createLazyProxy( |
| 163 | [refHelper = fRefHelper, releaseProcHelper, backendTexture = fBackendTexture]( |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 164 | GrResourceProvider* resourceProvider, |
| 165 | const GrSurfaceProxy::LazySurfaceDesc&) -> GrSurfaceProxy::LazyCallbackResult { |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 166 | if (refHelper->fSemaphore) { |
| 167 | resourceProvider->priv().gpu()->waitSemaphore(refHelper->fSemaphore.get()); |
| 168 | } |
Greg Daniel | e728f67 | 2018-01-17 10:52:04 -0500 | [diff] [blame] | 169 | |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 170 | // If a client re-draws the same image multiple times, the texture we return |
| 171 | // will be cached and re-used. If they draw a subset, though, we may be |
| 172 | // re-called. In that case, we want to re-use the borrowed texture we've |
| 173 | // previously created. |
| 174 | sk_sp<GrTexture> tex; |
| 175 | SkASSERT(refHelper->fBorrowedTextureKey.isValid()); |
| 176 | auto surf = resourceProvider->findByUniqueKey<GrSurface>( |
| 177 | refHelper->fBorrowedTextureKey); |
| 178 | if (surf) { |
| 179 | SkASSERT(surf->asTexture()); |
| 180 | tex = sk_ref_sp(surf->asTexture()); |
| 181 | } else { |
| 182 | // We just gained access to the texture. If we're on the original |
| 183 | // context, we could use the original texture, but we'd have no way of |
| 184 | // detecting that it's no longer in-use. So we always make a wrapped |
| 185 | // copy, where the release proc informs us that the context is done with |
| 186 | // it. This is unfortunate - we'll have two texture objects referencing |
| 187 | // the same GPU object. However, no client can ever see the original |
| 188 | // texture, so this should be safe. We make the texture uncacheable so |
| 189 | // that the release proc is called ASAP. |
| 190 | tex = resourceProvider->wrapBackendTexture( |
| 191 | backendTexture, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, |
| 192 | kRead_GrIOType); |
| 193 | if (!tex) { |
| 194 | return {}; |
| 195 | } |
| 196 | tex->setRelease(releaseProcHelper); |
| 197 | tex->resourcePriv().setUniqueKey(refHelper->fBorrowedTextureKey); |
| 198 | } |
| 199 | // We use keys to avoid re-wrapping the GrBackendTexture in a GrTexture. |
| 200 | // This is unrelated to the whatever SkImage key may be assigned to the |
| 201 | // proxy. |
| 202 | return {std::move(tex), true, GrSurfaceProxy::LazyInstantiationKeyMode::kUnsynced}; |
| 203 | }, |
| 204 | backendFormat, fBackendTexture.dimensions(), GrRenderable::kNo, 1, textureIsMipMapped, |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 205 | mipmapStatus, GrInternalSurfaceFlags::kReadOnly, SkBackingFit::kExact, SkBudgeted::kNo, |
Brian Salomon | 8a78e9c | 2020-03-27 10:42:15 -0400 | [diff] [blame] | 206 | GrProtected::kNo, GrSurfaceProxy::UseAllocator::kYes); |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 207 | if (!proxy) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 208 | return {}; |
Greg Daniel | e320486 | 2018-04-16 11:24:10 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 211 | if (texGenPolicy == GrImageTexGenPolicy::kDraw && origin.isZero() && |
| 212 | info.dimensions() == fBackendTexture.dimensions() && |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame^] | 213 | (mipMapped == GrMipmapped::kNo || proxy->mipmapped() == GrMipmapped::kYes)) { |
Greg Daniel | 261b8aa | 2017-10-23 09:37:36 -0400 | [diff] [blame] | 214 | // If the caller wants the entire texture and we have the correct mip support, we're done |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 215 | return GrSurfaceProxyView(std::move(proxy), fSurfaceOrigin, readSwizzle); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 216 | } else { |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 217 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
Greg Daniel | 4c6f9b7 | 2019-06-06 13:40:59 -0400 | [diff] [blame] | 218 | |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 219 | SkBudgeted budgeted = texGenPolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted |
| 220 | ? SkBudgeted::kNo |
| 221 | : SkBudgeted::kYes; |
| 222 | |
Brian Salomon | c524378 | 2020-04-02 12:50:34 -0400 | [diff] [blame] | 223 | auto copy = GrSurfaceProxy::Copy(context, proxy.get(), fSurfaceOrigin, mipMapped, subset, |
| 224 | SkBackingFit::kExact, budgeted); |
| 225 | return {std::move(copy), fSurfaceOrigin, readSwizzle}; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 226 | } |
| 227 | } |