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 | |
| 8 | #include "GrTextureProxy.h" |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 9 | #include "GrTextureProxyPriv.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 10 | |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 11 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 13 | #include "GrDeferredProxyUploader.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 14 | #include "GrProxyProvider.h" |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 15 | #include "GrSurfacePriv.h" |
Robert Phillips | a4c41b3 | 2017-03-15 13:02:45 -0400 | [diff] [blame] | 16 | #include "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 - with data |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 19 | GrTextureProxy::GrTextureProxy(const GrBackendFormat& format, const GrSurfaceDesc& srcDesc, |
| 20 | GrMipMapped mipMapped, SkBackingFit fit, SkBudgeted budgeted, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 21 | const void* srcData, size_t /*rowBytes*/, |
| 22 | GrInternalSurfaceFlags surfaceFlags) |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 23 | : INHERITED(format, srcDesc, kTopLeft_GrSurfaceOrigin, fit, budgeted, surfaceFlags) |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 24 | , fMipMapped(mipMapped) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 25 | , fProxyProvider(nullptr) |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 26 | , fDeferredUploader(nullptr) { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 27 | SkASSERT(!srcData); // currently handled in Make() |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 30 | // Deferred version - no data |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 31 | GrTextureProxy::GrTextureProxy(const GrBackendFormat& format, const GrSurfaceDesc& srcDesc, |
| 32 | GrSurfaceOrigin origin, GrMipMapped mipMapped, |
| 33 | SkBackingFit fit, SkBudgeted budgeted, |
| 34 | GrInternalSurfaceFlags surfaceFlags) |
| 35 | : INHERITED(format, srcDesc, origin, fit, budgeted, surfaceFlags) |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 36 | , fMipMapped(mipMapped) |
| 37 | , fProxyProvider(nullptr) |
| 38 | , fDeferredUploader(nullptr) {} |
| 39 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 40 | // Lazy-callback version |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 41 | GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 42 | const GrBackendFormat& format, const GrSurfaceDesc& desc, |
| 43 | GrSurfaceOrigin origin, GrMipMapped mipMapped, SkBackingFit fit, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 44 | SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags) |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 45 | : INHERITED(std::move(callback), lazyType, format, desc, origin, fit, budgeted, |
| 46 | surfaceFlags) |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 47 | , fMipMapped(mipMapped) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 48 | , fProxyProvider(nullptr) |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 49 | , fDeferredUploader(nullptr) {} |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 50 | |
| 51 | // Wrapped version |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 52 | GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin) |
| 53 | : INHERITED(std::move(surf), origin, SkBackingFit::kExact) |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 54 | , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped()) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 55 | , fProxyProvider(nullptr) |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 56 | , fDeferredUploader(nullptr) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 57 | if (fTarget->getUniqueKey().isValid()) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 58 | fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 59 | fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | GrTextureProxy::~GrTextureProxy() { |
| 64 | // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away |
| 65 | // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it. |
| 66 | fTarget = nullptr; |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 67 | |
| 68 | // In DDL-mode, uniquely keyed proxies keep their key even after their originating |
| 69 | // proxy provider has gone away. In that case there is noone to send the invalid key |
| 70 | // message to (Note: in this case we don't want to remove its cached resource). |
| 71 | if (fUniqueKey.isValid() && fProxyProvider) { |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 72 | fProxyProvider->processInvalidUniqueKey(fUniqueKey, this, |
| 73 | GrProxyProvider::InvalidateGPUResource::kNo); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 74 | } else { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 75 | SkASSERT(!fProxyProvider); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 76 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 77 | } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 78 | |
Robert Phillips | 73cc4e8 | 2019-04-01 07:46:13 -0400 | [diff] [blame] | 79 | bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider, |
| 80 | bool dontForceNoPendingIO) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 81 | if (LazyState::kNot != this->lazyInstantiationState()) { |
| 82 | return false; |
| 83 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 84 | if (!this->instantiateImpl(resourceProvider, 1, /* needsStencil = */ false, |
Greg Daniel | d2d8e92 | 2018-02-12 12:07:39 -0500 | [diff] [blame] | 85 | kNone_GrSurfaceFlags, fMipMapped, |
Robert Phillips | 73cc4e8 | 2019-04-01 07:46:13 -0400 | [diff] [blame] | 86 | fUniqueKey.isValid() ? &fUniqueKey : nullptr, |
| 87 | dontForceNoPendingIO)) { |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 88 | return false; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 89 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 90 | |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 91 | SkASSERT(!fTarget->asRenderTarget()); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 92 | SkASSERT(fTarget->asTexture()); |
| 93 | return true; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 96 | sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const { |
Robert Phillips | 73cc4e8 | 2019-04-01 07:46:13 -0400 | [diff] [blame] | 97 | SkASSERT(resourceProvider->explicitlyAllocateGPUResources()); |
| 98 | |
Robert Phillips | 7eeb74f | 2019-03-29 07:26:46 -0400 | [diff] [blame] | 99 | sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, 1, |
| 100 | /* needsStencil = */ false, |
| 101 | kNone_GrSurfaceFlags, |
Robert Phillips | 73cc4e8 | 2019-04-01 07:46:13 -0400 | [diff] [blame] | 102 | fMipMapped, true); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 103 | if (!surface) { |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 107 | SkASSERT(!surface->asRenderTarget()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 108 | SkASSERT(surface->asTexture()); |
| 109 | return surface; |
| 110 | } |
| 111 | |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 112 | void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) { |
| 113 | SkASSERT(!fTextureProxy->fDeferredUploader); |
| 114 | fTextureProxy->fDeferredUploader = std::move(uploader); |
| 115 | } |
| 116 | |
| 117 | void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) { |
Robert Phillips | a3f7026 | 2018-02-08 10:59:38 -0500 | [diff] [blame] | 118 | // The texture proxy's contents may already have been uploaded or instantiation may have failed |
| 119 | if (fTextureProxy->fDeferredUploader && fTextureProxy->fTarget) { |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 120 | fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void GrTextureProxyPriv::resetDeferredUploader() { |
| 125 | SkASSERT(fTextureProxy->fDeferredUploader); |
| 126 | fTextureProxy->fDeferredUploader.reset(); |
| 127 | } |
| 128 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 129 | GrSamplerState::Filter GrTextureProxy::highestFilterMode() const { |
Brian Salomon | e632dfc | 2018-08-01 13:01:16 -0400 | [diff] [blame] | 130 | return this->hasRestrictedSampling() ? GrSamplerState::Filter::kBilerp |
| 131 | : GrSamplerState::Filter::kMipMap; |
Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 134 | GrMipMapped GrTextureProxy::mipMapped() const { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 135 | if (this->isInstantiated()) { |
| 136 | return this->peekTexture()->texturePriv().mipMapped(); |
Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 137 | } |
| 138 | return fMipMapped; |
| 139 | } |
| 140 | |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 141 | size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const { |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 142 | return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1, |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 143 | this->proxyMipMapped(), !this->priv().isExact()); |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 144 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 145 | |
Greg Daniel | 45723ac | 2018-11-30 10:12:43 -0500 | [diff] [blame] | 146 | bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrTextureProxy* first, |
| 147 | const GrTextureProxy* second) { |
| 148 | return first->config() == second->config() && |
| 149 | first->textureType() == second->textureType() && |
| 150 | first->backendFormat() == second->backendFormat(); |
| 151 | } |
| 152 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 153 | void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 154 | SkASSERT(key.isValid()); |
| 155 | SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey |
| 156 | |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame^] | 157 | if (fTarget && fSyncTargetKey) { |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 158 | if (!fTarget->getUniqueKey().isValid()) { |
| 159 | fTarget->resourcePriv().setUniqueKey(key); |
| 160 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 161 | SkASSERT(fTarget->getUniqueKey() == key); |
| 162 | } |
| 163 | |
| 164 | fUniqueKey = key; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 165 | fProxyProvider = proxyProvider; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void GrTextureProxy::clearUniqueKey() { |
| 169 | fUniqueKey.reset(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 170 | fProxyProvider = nullptr; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 173 | #ifdef SK_DEBUG |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 174 | void GrTextureProxy::onValidateSurface(const GrSurface* surface) { |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 175 | SkASSERT(!surface->asRenderTarget()); |
| 176 | |
| 177 | // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version |
| 178 | SkASSERT(surface->asTexture()); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 179 | SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() || |
Robert Phillips | 6ba15ec | 2018-02-08 16:26:47 -0500 | [diff] [blame] | 180 | GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 181 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 182 | SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 183 | |
| 184 | GrInternalSurfaceFlags proxyFlags = fSurfaceFlags; |
| 185 | GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags(); |
| 186 | SkASSERT((proxyFlags & GrInternalSurfaceFlags::kTextureMask) == |
| 187 | (surfaceFlags & GrInternalSurfaceFlags::kTextureMask)); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 188 | } |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 189 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 190 | #endif |
| 191 | |