robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/gpu/GrTextureProxyPriv.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 10 | |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame^] | 11 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrContextPriv.h" |
| 13 | #include "src/gpu/GrDeferredProxyUploader.h" |
| 14 | #include "src/gpu/GrProxyProvider.h" |
| 15 | #include "src/gpu/GrSurfacePriv.h" |
| 16 | #include "src/gpu/GrTexturePriv.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 17 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 18 | // Deferred version - no data |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 19 | GrTextureProxy::GrTextureProxy(const GrBackendFormat& format, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 20 | SkISize dimensions, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 21 | GrMipMapped mipMapped, |
| 22 | GrMipMapsStatus mipMapsStatus, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 23 | SkBackingFit fit, |
| 24 | SkBudgeted budgeted, |
| 25 | GrProtected isProtected, |
| 26 | GrInternalSurfaceFlags surfaceFlags, |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 27 | UseAllocator useAllocator, |
| 28 | GrDDLProvider creatingProvider) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 29 | : INHERITED(format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator) |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 30 | , fMipMapped(mipMapped) |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 31 | , fMipMapsStatus(mipMapsStatus) |
| 32 | SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus)) |
| 33 | , fCreatingProvider(creatingProvider) |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 34 | , fProxyProvider(nullptr) |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 35 | , fDeferredUploader(nullptr) { |
| 36 | SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly)); |
| 37 | } |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 38 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 39 | // Lazy-callback version |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 40 | GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, |
| 41 | const GrBackendFormat& format, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 42 | SkISize dimensions, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 43 | GrMipMapped mipMapped, |
| 44 | GrMipMapsStatus mipMapsStatus, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 45 | SkBackingFit fit, |
| 46 | SkBudgeted budgeted, |
| 47 | GrProtected isProtected, |
| 48 | GrInternalSurfaceFlags surfaceFlags, |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 49 | UseAllocator useAllocator, |
| 50 | GrDDLProvider creatingProvider) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 51 | : INHERITED(std::move(callback), format, dimensions, fit, budgeted, isProtected, |
| 52 | surfaceFlags, useAllocator) |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 53 | , fMipMapped(mipMapped) |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 54 | , fMipMapsStatus(mipMapsStatus) |
| 55 | SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus)) |
| 56 | , fCreatingProvider(creatingProvider) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 57 | , fProxyProvider(nullptr) |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 58 | , fDeferredUploader(nullptr) { |
| 59 | SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly)); |
| 60 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 61 | |
| 62 | // Wrapped version |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 63 | GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, |
| 64 | UseAllocator useAllocator, |
| 65 | GrDDLProvider creatingProvider) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 66 | : INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator) |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 67 | , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped()) |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 68 | , fMipMapsStatus(fTarget->asTexture()->texturePriv().mipMapsStatus()) |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 69 | SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus)) |
| 70 | , fCreatingProvider(creatingProvider) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 71 | , fProxyProvider(nullptr) |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 72 | , fDeferredUploader(nullptr) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 73 | if (fTarget->getUniqueKey().isValid()) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 74 | fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider(); |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 75 | fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | GrTextureProxy::~GrTextureProxy() { |
| 80 | // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away |
| 81 | // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it. |
| 82 | fTarget = nullptr; |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 83 | |
| 84 | // In DDL-mode, uniquely keyed proxies keep their key even after their originating |
| 85 | // proxy provider has gone away. In that case there is noone to send the invalid key |
| 86 | // message to (Note: in this case we don't want to remove its cached resource). |
| 87 | if (fUniqueKey.isValid() && fProxyProvider) { |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 88 | fProxyProvider->processInvalidUniqueKey(fUniqueKey, this, |
| 89 | GrProxyProvider::InvalidateGPUResource::kNo); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 90 | } else { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 91 | SkASSERT(!fProxyProvider); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 92 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 93 | } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 94 | |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 95 | bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 96 | if (this->isLazy()) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 97 | return false; |
| 98 | } |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 99 | if (!this->instantiateImpl(resourceProvider, 1, GrRenderable::kNo, fMipMapped, |
| 100 | fUniqueKey.isValid() ? &fUniqueKey : nullptr)) { |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 101 | return false; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 102 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 103 | |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 104 | SkASSERT(!this->peekRenderTarget()); |
| 105 | SkASSERT(this->peekTexture()); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 106 | return true; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 109 | sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const { |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 110 | sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, 1, GrRenderable::kNo, |
| 111 | fMipMapped); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 112 | if (!surface) { |
| 113 | return nullptr; |
| 114 | } |
| 115 | |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 116 | SkASSERT(!surface->asRenderTarget()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 117 | SkASSERT(surface->asTexture()); |
| 118 | return surface; |
| 119 | } |
| 120 | |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 121 | void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) { |
| 122 | SkASSERT(!fTextureProxy->fDeferredUploader); |
| 123 | fTextureProxy->fDeferredUploader = std::move(uploader); |
| 124 | } |
| 125 | |
| 126 | void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) { |
Robert Phillips | a3f7026 | 2018-02-08 10:59:38 -0500 | [diff] [blame] | 127 | // The texture proxy's contents may already have been uploaded or instantiation may have failed |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 128 | if (fTextureProxy->fDeferredUploader && fTextureProxy->isInstantiated()) { |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 129 | fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void GrTextureProxyPriv::resetDeferredUploader() { |
| 134 | SkASSERT(fTextureProxy->fDeferredUploader); |
| 135 | fTextureProxy->fDeferredUploader.reset(); |
| 136 | } |
| 137 | |
Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 138 | GrMipMapped GrTextureProxy::mipMapped() const { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 139 | if (this->isInstantiated()) { |
| 140 | return this->peekTexture()->texturePriv().mipMapped(); |
Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 141 | } |
| 142 | return fMipMapped; |
| 143 | } |
| 144 | |
Greg Daniel | 7fd7a8a | 2019-10-10 16:10:31 -0400 | [diff] [blame] | 145 | size_t GrTextureProxy::onUninstantiatedGpuMemorySize(const GrCaps& caps) const { |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 146 | return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(), 1, |
| 147 | this->proxyMipMapped(), !this->priv().isExact()); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 148 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 149 | |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 150 | GrSamplerState::Filter GrTextureProxy::HighestFilterMode(GrTextureType textureType) { |
Brian Salomon | a3b02f5 | 2020-07-15 16:02:01 -0400 | [diff] [blame] | 151 | return GrTextureTypeHasRestrictedSampling(textureType) ? GrSamplerState::Filter::kLinear |
Michael Ludwig | 8fa469d | 2019-11-25 16:08:44 -0500 | [diff] [blame] | 152 | : GrSamplerState::Filter::kMipMap; |
| 153 | } |
| 154 | |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 155 | bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first, |
| 156 | const GrSurfaceProxy* second) { |
Michael Ludwig | 08bd1f7 | 2019-12-02 14:27:21 -0500 | [diff] [blame] | 157 | // In order to be compatible, the proxies should also have the same texture type. This is |
| 158 | // checked explicitly since the GrBackendFormat == operator does not compare texture type |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 159 | return first->backendFormat().textureType() == second->backendFormat().textureType() && |
Greg Daniel | 45723ac | 2018-11-30 10:12:43 -0500 | [diff] [blame] | 160 | first->backendFormat() == second->backendFormat(); |
| 161 | } |
| 162 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 163 | void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 164 | SkASSERT(key.isValid()); |
| 165 | SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey |
| 166 | |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 167 | if (fTarget && fSyncTargetKey) { |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 168 | if (!fTarget->getUniqueKey().isValid()) { |
| 169 | fTarget->resourcePriv().setUniqueKey(key); |
| 170 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 171 | SkASSERT(fTarget->getUniqueKey() == key); |
| 172 | } |
| 173 | |
| 174 | fUniqueKey = key; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 175 | fProxyProvider = proxyProvider; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void GrTextureProxy::clearUniqueKey() { |
| 179 | fUniqueKey.reset(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 180 | fProxyProvider = nullptr; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 183 | GrSurfaceProxy::LazySurfaceDesc GrTextureProxy::callbackDesc() const { |
| 184 | SkISize dims; |
| 185 | SkBackingFit fit; |
| 186 | if (this->isFullyLazy()) { |
| 187 | fit = SkBackingFit::kApprox; |
| 188 | dims = {-1, -1}; |
| 189 | } else { |
| 190 | fit = this->isFunctionallyExact() ? SkBackingFit::kExact : SkBackingFit::kApprox; |
| 191 | dims = this->dimensions(); |
| 192 | } |
| 193 | return { |
| 194 | dims, |
| 195 | fit, |
| 196 | GrRenderable::kNo, |
| 197 | fMipMapped, |
| 198 | 1, |
| 199 | this->backendFormat(), |
| 200 | this->isProtected(), |
| 201 | this->isBudgeted(), |
| 202 | }; |
| 203 | } |
| 204 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 205 | #ifdef SK_DEBUG |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 206 | void GrTextureProxy::onValidateSurface(const GrSurface* surface) { |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 207 | SkASSERT(!surface->asRenderTarget()); |
| 208 | |
| 209 | // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version |
| 210 | SkASSERT(surface->asTexture()); |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 211 | // It is possible to fulfill a non-mipmapped proxy with a mipmapped texture. |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 212 | SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() || |
Robert Phillips | 6ba15ec | 2018-02-08 16:26:47 -0500 | [diff] [blame] | 213 | GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 214 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 215 | SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 216 | |
| 217 | GrInternalSurfaceFlags proxyFlags = fSurfaceFlags; |
| 218 | GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags(); |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 219 | SkASSERT(((int)proxyFlags & kGrInternalTextureFlagsMask) == |
| 220 | ((int)surfaceFlags & kGrInternalTextureFlagsMask)); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 221 | } |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 222 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 223 | #endif |
| 224 | |