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 |
| 19 | GrTextureProxy::GrTextureProxy(const GrSurfaceDesc& srcDesc, GrMipMapped mipMapped, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 20 | GrTextureType textureType, SkBackingFit fit, SkBudgeted budgeted, |
| 21 | const void* srcData, size_t /*rowBytes*/, |
| 22 | GrInternalSurfaceFlags surfaceFlags) |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 23 | : INHERITED(srcDesc, kTopLeft_GrSurfaceOrigin, fit, budgeted, surfaceFlags) |
Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 24 | , fMipMapped(mipMapped) |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 25 | , fTextureType(textureType) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 26 | , fProxyProvider(nullptr) |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 27 | , fDeferredUploader(nullptr) { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 28 | SkASSERT(!srcData); // currently handled in Make() |
robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 31 | // Deferred version - no data |
| 32 | GrTextureProxy::GrTextureProxy(const GrSurfaceDesc& srcDesc, GrSurfaceOrigin origin, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 33 | GrMipMapped mipMapped, GrTextureType textureType, SkBackingFit fit, |
| 34 | SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags) |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 35 | : INHERITED(srcDesc, origin, fit, budgeted, surfaceFlags) |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 36 | , fMipMapped(mipMapped) |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 37 | , fTextureType(textureType) |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 38 | , fProxyProvider(nullptr) |
| 39 | , fDeferredUploader(nullptr) {} |
| 40 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 41 | // Lazy-callback version |
Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 42 | GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 43 | const GrSurfaceDesc& desc, GrSurfaceOrigin origin, |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 44 | GrMipMapped mipMapped, GrTextureType textureType, SkBackingFit fit, |
| 45 | SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags) |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame] | 46 | : INHERITED(std::move(callback), lazyType, desc, origin, fit, budgeted, surfaceFlags) |
Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 47 | , fMipMapped(mipMapped) |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 48 | , fTextureType(textureType) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 49 | , fProxyProvider(nullptr) |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 50 | , fDeferredUploader(nullptr) {} |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 51 | |
| 52 | // Wrapped version |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 53 | GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin) |
| 54 | : INHERITED(std::move(surf), origin, SkBackingFit::kExact) |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 55 | , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped()) |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 56 | , fTextureType(fTarget->asTexture()->texturePriv().textureType()) |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 57 | , fProxyProvider(nullptr) |
Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 58 | , fDeferredUploader(nullptr) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 59 | if (fTarget->getUniqueKey().isValid()) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 60 | fProxyProvider = fTarget->asTexture()->getContext()->contextPriv().proxyProvider(); |
| 61 | fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | GrTextureProxy::~GrTextureProxy() { |
| 66 | // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away |
| 67 | // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it. |
| 68 | fTarget = nullptr; |
| 69 | if (fUniqueKey.isValid()) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 70 | fProxyProvider->processInvalidProxyUniqueKey(fUniqueKey, this, false); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 71 | } else { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 72 | SkASSERT(!fProxyProvider); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 73 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 74 | } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 75 | |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 76 | bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 77 | if (LazyState::kNot != this->lazyInstantiationState()) { |
| 78 | return false; |
| 79 | } |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 80 | if (!this->instantiateImpl(resourceProvider, 1, /* needsStencil = */ false, |
Greg Daniel | d2d8e92 | 2018-02-12 12:07:39 -0500 | [diff] [blame] | 81 | kNone_GrSurfaceFlags, fMipMapped, |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 82 | fUniqueKey.isValid() ? &fUniqueKey : nullptr)) { |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 83 | return false; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 84 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 85 | |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 86 | SkASSERT(!fTarget->asRenderTarget()); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 87 | SkASSERT(fTarget->asTexture()); |
| 88 | return true; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 91 | sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 92 | sk_sp<GrSurface> surface= this->createSurfaceImpl(resourceProvider, 1, |
Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 93 | /* needsStencil = */ false, |
| 94 | kNone_GrSurfaceFlags, |
Greg Daniel | d2d8e92 | 2018-02-12 12:07:39 -0500 | [diff] [blame] | 95 | 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 |
| 112 | if (fTextureProxy->fDeferredUploader && fTextureProxy->fTarget) { |
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 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 139 | void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 140 | SkASSERT(key.isValid()); |
| 141 | SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey |
| 142 | |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 143 | if (fTarget) { |
| 144 | if (!fTarget->getUniqueKey().isValid()) { |
| 145 | fTarget->resourcePriv().setUniqueKey(key); |
| 146 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 147 | SkASSERT(fTarget->getUniqueKey() == key); |
| 148 | } |
| 149 | |
| 150 | fUniqueKey = key; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 151 | fProxyProvider = proxyProvider; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void GrTextureProxy::clearUniqueKey() { |
| 155 | fUniqueKey.reset(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 156 | fProxyProvider = nullptr; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 157 | } |
| 158 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 159 | #ifdef SK_DEBUG |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 160 | void GrTextureProxy::onValidateSurface(const GrSurface* surface) { |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 161 | SkASSERT(!surface->asRenderTarget()); |
| 162 | |
| 163 | // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version |
| 164 | SkASSERT(surface->asTexture()); |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 165 | SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() || |
Robert Phillips | 6ba15ec | 2018-02-08 16:26:47 -0500 | [diff] [blame] | 166 | GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped()); |
Brian Salomon | 7226c23 | 2018-07-30 13:13:17 -0400 | [diff] [blame] | 167 | SkASSERT(surface->asTexture()->texturePriv().textureType() == fTextureType); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 168 | } |
| 169 | #endif |
| 170 | |