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