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