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