| 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" |
| Robert Phillips | a4c41b3 | 2017-03-15 13:02:45 -0400 | [diff] [blame] | 15 | #include "GrTexturePriv.h" |
| robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 16 | |
| Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 17 | // Deferred version |
| Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 18 | GrTextureProxy::GrTextureProxy(const GrSurfaceDesc& srcDesc, GrMipMapped mipMapped, |
| 19 | SkBackingFit fit, SkBudgeted budgeted, const void* srcData, |
| 20 | size_t /*rowBytes*/, uint32_t flags) |
| Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 21 | : INHERITED(srcDesc, fit, budgeted, flags) |
| Greg Daniel | f6f7b67 | 2018-02-15 13:06:26 -0500 | [diff] [blame] | 22 | , fMipMapped(mipMapped) |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 23 | , fProxyProvider(nullptr) |
| Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 24 | , fDeferredUploader(nullptr) { |
| Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 25 | SkASSERT(!srcData); // currently handled in Make() |
| robertphillips | 8abb370 | 2016-08-31 14:04:06 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 28 | // Lazy-callback version |
| Greg Daniel | 457469c | 2018-02-08 15:05:44 -0500 | [diff] [blame] | 29 | GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType, |
| 30 | const GrSurfaceDesc& desc, GrMipMapped mipMapped, SkBackingFit fit, |
| 31 | SkBudgeted budgeted, uint32_t flags) |
| 32 | : INHERITED(std::move(callback), lazyType, desc, fit, budgeted, flags) |
| Greg Daniel | 65fa8ca | 2018-01-10 17:06:31 -0500 | [diff] [blame] | 33 | , fMipMapped(mipMapped) |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 34 | , fProxyProvider(nullptr) |
| Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 35 | , fDeferredUploader(nullptr) { |
| 36 | } |
| 37 | |
| 38 | // Wrapped version |
| Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 39 | GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin) |
| 40 | : INHERITED(std::move(surf), origin, SkBackingFit::kExact) |
| Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 41 | , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped()) |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 42 | , fProxyProvider(nullptr) |
| Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 43 | , fDeferredUploader(nullptr) { |
| Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 44 | if (fTarget->getUniqueKey().isValid()) { |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 45 | fProxyProvider = fTarget->asTexture()->getContext()->contextPriv().proxyProvider(); |
| 46 | fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget); |
| Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
| 50 | GrTextureProxy::~GrTextureProxy() { |
| 51 | // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away |
| 52 | // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it. |
| 53 | fTarget = nullptr; |
| 54 | if (fUniqueKey.isValid()) { |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 55 | fProxyProvider->processInvalidProxyUniqueKey(fUniqueKey, this, false); |
| Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 56 | } else { |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 57 | SkASSERT(!fProxyProvider); |
| Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 58 | } |
| Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 59 | } |
| robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 60 | |
| Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 61 | bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) { |
| Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 62 | if (LazyState::kNot != this->lazyInstantiationState()) { |
| 63 | return false; |
| 64 | } |
| Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 65 | if (!this->instantiateImpl(resourceProvider, 1, /* needsStencil = */ false, |
| Greg Daniel | d2d8e92 | 2018-02-12 12:07:39 -0500 | [diff] [blame] | 66 | kNone_GrSurfaceFlags, fMipMapped, |
| Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 67 | fUniqueKey.isValid() ? &fUniqueKey : nullptr)) { |
| Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 68 | return false; |
| robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 69 | } |
| Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 70 | |
| Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 71 | SkASSERT(!fTarget->asRenderTarget()); |
| Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 72 | SkASSERT(fTarget->asTexture()); |
| 73 | return true; |
| robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 76 | sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const { |
| Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 77 | sk_sp<GrSurface> surface= this->createSurfaceImpl(resourceProvider, 1, |
| Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 78 | /* needsStencil = */ false, |
| 79 | kNone_GrSurfaceFlags, |
| Greg Daniel | d2d8e92 | 2018-02-12 12:07:39 -0500 | [diff] [blame] | 80 | fMipMapped); |
| Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 81 | if (!surface) { |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 85 | SkASSERT(!surface->asRenderTarget()); |
| Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 86 | SkASSERT(surface->asTexture()); |
| 87 | return surface; |
| 88 | } |
| 89 | |
| Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 90 | void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) { |
| 91 | SkASSERT(!fTextureProxy->fDeferredUploader); |
| 92 | fTextureProxy->fDeferredUploader = std::move(uploader); |
| 93 | } |
| 94 | |
| 95 | void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) { |
| Robert Phillips | a3f7026 | 2018-02-08 10:59:38 -0500 | [diff] [blame] | 96 | // The texture proxy's contents may already have been uploaded or instantiation may have failed |
| 97 | if (fTextureProxy->fDeferredUploader && fTextureProxy->fTarget) { |
| Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 98 | fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void GrTextureProxyPriv::resetDeferredUploader() { |
| 103 | SkASSERT(fTextureProxy->fDeferredUploader); |
| 104 | fTextureProxy->fDeferredUploader.reset(); |
| 105 | } |
| 106 | |
| Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 107 | // This method parallels the highest_filter_mode functions in GrGLTexture & GrVkTexture. |
| Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 108 | GrSamplerState::Filter GrTextureProxy::highestFilterMode() const { |
| Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 109 | if (fTarget) { |
| 110 | return fTarget->asTexture()->texturePriv().highestFilterMode(); |
| 111 | } |
| 112 | |
| Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 113 | // In OpenGL, GR_GL_TEXTURE_RECTANGLE and GR_GL_TEXTURE_EXTERNAL (which have a highest filter |
| 114 | // mode of bilerp) can only be created via wrapping. |
| 115 | |
| Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 116 | return 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 { |
| 120 | if (this->priv().isInstantiated()) { |
| 121 | return this->priv().peekTexture()->texturePriv().mipMapped(); |
| 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, |
| Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 128 | this->texPriv().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 | |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 131 | void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) { |
| Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 132 | SkASSERT(key.isValid()); |
| 133 | SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey |
| 134 | |
| Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 135 | if (fTarget) { |
| 136 | if (!fTarget->getUniqueKey().isValid()) { |
| 137 | fTarget->resourcePriv().setUniqueKey(key); |
| 138 | } |
| Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 139 | SkASSERT(fTarget->getUniqueKey() == key); |
| 140 | } |
| 141 | |
| 142 | fUniqueKey = key; |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 143 | fProxyProvider = proxyProvider; |
| Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void GrTextureProxy::clearUniqueKey() { |
| 147 | fUniqueKey.reset(); |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 148 | fProxyProvider = nullptr; |
| Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 151 | #ifdef SK_DEBUG |
| Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 152 | void GrTextureProxy::validateLazySurface(const GrSurface* surface) { |
| 153 | SkASSERT(!surface->asRenderTarget()); |
| 154 | |
| 155 | // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version |
| 156 | SkASSERT(surface->asTexture()); |
| Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 157 | SkASSERT(GrMipMapped::kNo == this->texPriv().proxyMipMapped() || |
| Robert Phillips | 6ba15ec | 2018-02-08 16:26:47 -0500 | [diff] [blame] | 158 | GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped()); |
| Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 159 | } |
| 160 | #endif |
| 161 | |