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" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 9 | |
Robert Phillips | 4e105e2 | 2020-07-16 09:18:50 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrProxyProvider.h" |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrSurface.h" |
Adlai Holler | a3987cc | 2021-02-05 15:52:28 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrSurfaceProxyPriv.h" |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrTexture.h" |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 16 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 17 | // Deferred version - no data |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 18 | GrTextureProxy::GrTextureProxy(const GrBackendFormat& format, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 19 | SkISize dimensions, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 20 | GrMipmapped mipMapped, |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 21 | GrMipmapStatus mipmapStatus, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 22 | SkBackingFit fit, |
| 23 | SkBudgeted budgeted, |
| 24 | GrProtected isProtected, |
| 25 | GrInternalSurfaceFlags surfaceFlags, |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 26 | UseAllocator useAllocator, |
| 27 | GrDDLProvider creatingProvider) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 28 | : INHERITED(format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator) |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 29 | , fMipmapped(mipMapped) |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 30 | , fMipmapStatus(mipmapStatus) |
| 31 | SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus)) |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 32 | , fCreatingProvider(creatingProvider) |
Adlai Holler | a3987cc | 2021-02-05 15:52:28 -0500 | [diff] [blame] | 33 | , fProxyProvider(nullptr) { |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 34 | SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly)); |
Brian Salomon | be1084b | 2021-01-26 13:29:30 -0500 | [diff] [blame] | 35 | if (this->textureType() == GrTextureType::kExternal) { |
| 36 | fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly; |
| 37 | } |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 38 | } |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 39 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 40 | // Lazy-callback version |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 41 | GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, |
| 42 | const GrBackendFormat& format, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 43 | SkISize dimensions, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 44 | GrMipmapped mipMapped, |
Brian Salomon | a6db510 | 2020-07-21 09:56:23 -0400 | [diff] [blame] | 45 | GrMipmapStatus mipmapStatus, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 46 | SkBackingFit fit, |
| 47 | SkBudgeted budgeted, |
| 48 | GrProtected isProtected, |
| 49 | GrInternalSurfaceFlags surfaceFlags, |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 50 | UseAllocator useAllocator, |
| 51 | GrDDLProvider creatingProvider) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 52 | : INHERITED(std::move(callback), format, dimensions, fit, budgeted, isProtected, |
| 53 | surfaceFlags, useAllocator) |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 54 | , fMipmapped(mipMapped) |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 55 | , fMipmapStatus(mipmapStatus) |
| 56 | SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus)) |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 57 | , fCreatingProvider(creatingProvider) |
Adlai Holler | a3987cc | 2021-02-05 15:52:28 -0500 | [diff] [blame] | 58 | , fProxyProvider(nullptr) { |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 59 | SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly)); |
Brian Salomon | be1084b | 2021-01-26 13:29:30 -0500 | [diff] [blame] | 60 | if (this->textureType() == GrTextureType::kExternal) { |
| 61 | fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly; |
| 62 | } |
Stephen White | 3c0a50f | 2020-01-16 18:19:54 -0500 | [diff] [blame] | 63 | } |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 64 | |
| 65 | // Wrapped version |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 66 | GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, |
| 67 | UseAllocator useAllocator, |
| 68 | GrDDLProvider creatingProvider) |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 69 | : INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator) |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 70 | , fMipmapped(fTarget->asTexture()->mipmapped()) |
| 71 | , fMipmapStatus(fTarget->asTexture()->mipmapStatus()) |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 72 | SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus)) |
Robert Phillips | f10b2a5 | 2020-05-15 10:20:04 -0400 | [diff] [blame] | 73 | , fCreatingProvider(creatingProvider) |
Adlai Holler | a3987cc | 2021-02-05 15:52:28 -0500 | [diff] [blame] | 74 | , fProxyProvider(nullptr) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 75 | if (fTarget->getUniqueKey().isValid()) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 76 | fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider(); |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 77 | fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get()); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 78 | } |
Brian Salomon | be1084b | 2021-01-26 13:29:30 -0500 | [diff] [blame] | 79 | if (this->textureType() == GrTextureType::kExternal) { |
| 80 | fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly; |
| 81 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | GrTextureProxy::~GrTextureProxy() { |
| 85 | // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away |
| 86 | // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it. |
| 87 | fTarget = nullptr; |
Robert Phillips | 0790f8a | 2018-09-18 13:11:03 -0400 | [diff] [blame] | 88 | |
| 89 | // In DDL-mode, uniquely keyed proxies keep their key even after their originating |
| 90 | // proxy provider has gone away. In that case there is noone to send the invalid key |
| 91 | // message to (Note: in this case we don't want to remove its cached resource). |
| 92 | if (fUniqueKey.isValid() && fProxyProvider) { |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 93 | fProxyProvider->processInvalidUniqueKey(fUniqueKey, this, |
| 94 | GrProxyProvider::InvalidateGPUResource::kNo); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 95 | } else { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 96 | SkASSERT(!fProxyProvider); |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 97 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 98 | } |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 99 | |
Robert Phillips | 10d1721 | 2019-04-24 14:09:10 -0400 | [diff] [blame] | 100 | bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) { |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 101 | if (this->isLazy()) { |
Greg Daniel | 0a375db | 2018-02-01 12:21:39 -0500 | [diff] [blame] | 102 | return false; |
| 103 | } |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 104 | if (!this->instantiateImpl(resourceProvider, 1, GrRenderable::kNo, fMipmapped, |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 105 | fUniqueKey.isValid() ? &fUniqueKey : nullptr)) { |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 106 | return false; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 107 | } |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 108 | |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 109 | SkASSERT(!this->peekRenderTarget()); |
| 110 | SkASSERT(this->peekTexture()); |
Robert Phillips | eee4d6e | 2017-06-05 09:26:07 -0400 | [diff] [blame] | 111 | return true; |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 114 | sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const { |
Chris Dalton | 0b68dda | 2019-11-07 21:08:03 -0700 | [diff] [blame] | 115 | sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, 1, GrRenderable::kNo, |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 116 | fMipmapped); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 117 | if (!surface) { |
| 118 | return nullptr; |
| 119 | } |
| 120 | |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 121 | SkASSERT(!surface->asRenderTarget()); |
Robert Phillips | 5af44de | 2017-07-18 14:49:38 -0400 | [diff] [blame] | 122 | SkASSERT(surface->asTexture()); |
| 123 | return surface; |
| 124 | } |
| 125 | |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 126 | GrMipmapped GrTextureProxy::mipmapped() const { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 127 | if (this->isInstantiated()) { |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 128 | return this->peekTexture()->mipmapped(); |
Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 129 | } |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 130 | return fMipmapped; |
Greg Daniel | 3b2ebbb | 2018-02-09 10:49:23 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Greg Daniel | 0eca74c | 2020-10-01 13:46:00 -0400 | [diff] [blame] | 133 | size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const { |
| 134 | return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(), |
| 135 | /*colorSamplesPerPixel=*/1, this->proxyMipmapped(), |
| 136 | !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 | |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 139 | bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first, |
| 140 | const GrSurfaceProxy* second) { |
Michael Ludwig | 08bd1f7 | 2019-12-02 14:27:21 -0500 | [diff] [blame] | 141 | // In order to be compatible, the proxies should also have the same texture type. This is |
| 142 | // checked explicitly since the GrBackendFormat == operator does not compare texture type |
Greg Daniel | c71c796 | 2020-01-14 16:44:18 -0500 | [diff] [blame] | 143 | return first->backendFormat().textureType() == second->backendFormat().textureType() && |
Greg Daniel | 45723ac | 2018-11-30 10:12:43 -0500 | [diff] [blame] | 144 | first->backendFormat() == second->backendFormat(); |
| 145 | } |
| 146 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 147 | void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) { |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 148 | SkASSERT(key.isValid()); |
| 149 | SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey |
| 150 | |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 151 | if (fTarget && fSyncTargetKey) { |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 152 | if (!fTarget->getUniqueKey().isValid()) { |
| 153 | fTarget->resourcePriv().setUniqueKey(key); |
| 154 | } |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 155 | SkASSERT(fTarget->getUniqueKey() == key); |
| 156 | } |
| 157 | |
| 158 | fUniqueKey = key; |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 159 | fProxyProvider = proxyProvider; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void GrTextureProxy::clearUniqueKey() { |
| 163 | fUniqueKey.reset(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 164 | fProxyProvider = nullptr; |
Robert Phillips | ae7d3f3 | 2017-09-21 08:26:08 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 167 | GrSurfaceProxy::LazySurfaceDesc GrTextureProxy::callbackDesc() const { |
| 168 | SkISize dims; |
| 169 | SkBackingFit fit; |
| 170 | if (this->isFullyLazy()) { |
| 171 | fit = SkBackingFit::kApprox; |
| 172 | dims = {-1, -1}; |
| 173 | } else { |
| 174 | fit = this->isFunctionallyExact() ? SkBackingFit::kExact : SkBackingFit::kApprox; |
| 175 | dims = this->dimensions(); |
| 176 | } |
| 177 | return { |
| 178 | dims, |
| 179 | fit, |
| 180 | GrRenderable::kNo, |
Brian Salomon | 40a4062 | 2020-07-21 10:32:07 -0400 | [diff] [blame] | 181 | fMipmapped, |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 182 | 1, |
| 183 | this->backendFormat(), |
| 184 | this->isProtected(), |
| 185 | this->isBudgeted(), |
| 186 | }; |
| 187 | } |
| 188 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 189 | #ifdef SK_DEBUG |
Greg Daniel | 849dce1 | 2018-04-24 14:32:53 -0400 | [diff] [blame] | 190 | void GrTextureProxy::onValidateSurface(const GrSurface* surface) { |
Robert Phillips | e8fabb2 | 2018-02-04 14:33:21 -0500 | [diff] [blame] | 191 | SkASSERT(!surface->asRenderTarget()); |
| 192 | |
| 193 | // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version |
| 194 | SkASSERT(surface->asTexture()); |
Chris Dalton | 95d8ceb | 2019-07-30 11:17:59 -0600 | [diff] [blame] | 195 | // It is possible to fulfill a non-mipmapped proxy with a mipmapped texture. |
Brian Salomon | 8c82a87 | 2020-07-21 12:09:58 -0400 | [diff] [blame] | 196 | SkASSERT(GrMipmapped::kNo == this->proxyMipmapped() || |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 197 | GrMipmapped::kYes == surface->asTexture()->mipmapped()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 198 | |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 199 | SkASSERT(surface->asTexture()->textureType() == this->textureType()); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 200 | |
| 201 | GrInternalSurfaceFlags proxyFlags = fSurfaceFlags; |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame] | 202 | GrInternalSurfaceFlags surfaceFlags = surface->flags(); |
Chris Dalton | 3f7932e | 2019-08-19 00:39:13 -0600 | [diff] [blame] | 203 | SkASSERT(((int)proxyFlags & kGrInternalTextureFlagsMask) == |
| 204 | ((int)surfaceFlags & kGrInternalTextureFlagsMask)); |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 205 | } |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 206 | |
Chris Dalton | 706a6ff | 2017-11-29 22:01:06 -0700 | [diff] [blame] | 207 | #endif |
| 208 | |