blob: bfcf94c5d745935570f59c7f7e168956ecb02565 [file] [log] [blame]
robertphillips76948d42016-05-04 12:47:41 -07001/*
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 Danielf91aeb22019-06-18 09:58:02 -04008#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrTextureProxyPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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"
robertphillips76948d42016-05-04 12:47:41 -070017
Brian Salomon58389b92018-03-07 13:01:25 -050018// Deferred version - no data
Brian Salomonbeb7f522019-08-30 16:19:42 -040019GrTextureProxy::GrTextureProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050020 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040021 GrMipMapped mipMapped,
22 GrMipMapsStatus mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -040023 SkBackingFit fit,
24 SkBudgeted budgeted,
25 GrProtected isProtected,
26 GrInternalSurfaceFlags surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -040027 UseAllocator useAllocator,
28 GrDDLProvider creatingProvider)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040029 : INHERITED(format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator)
Brian Salomon58389b92018-03-07 13:01:25 -050030 , fMipMapped(mipMapped)
Robert Phillipsf10b2a52020-05-15 10:20:04 -040031 , fMipMapsStatus(mipMapsStatus)
32 SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
33 , fCreatingProvider(creatingProvider)
Brian Salomon58389b92018-03-07 13:01:25 -050034 , fProxyProvider(nullptr)
Stephen White3c0a50f2020-01-16 18:19:54 -050035 , fDeferredUploader(nullptr) {
36 SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
37}
Brian Salomon58389b92018-03-07 13:01:25 -050038
Chris Dalton706a6ff2017-11-29 22:01:06 -070039// Lazy-callback version
Brian Salomonbeb7f522019-08-30 16:19:42 -040040GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback,
41 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050042 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040043 GrMipMapped mipMapped,
44 GrMipMapsStatus mipMapsStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -040045 SkBackingFit fit,
46 SkBudgeted budgeted,
47 GrProtected isProtected,
48 GrInternalSurfaceFlags surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -040049 UseAllocator useAllocator,
50 GrDDLProvider creatingProvider)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040051 : INHERITED(std::move(callback), format, dimensions, fit, budgeted, isProtected,
52 surfaceFlags, useAllocator)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050053 , fMipMapped(mipMapped)
Robert Phillipsf10b2a52020-05-15 10:20:04 -040054 , fMipMapsStatus(mipMapsStatus)
55 SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
56 , fCreatingProvider(creatingProvider)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050057 , fProxyProvider(nullptr)
Stephen White3c0a50f2020-01-16 18:19:54 -050058 , fDeferredUploader(nullptr) {
59 SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
60}
Chris Dalton706a6ff2017-11-29 22:01:06 -070061
62// Wrapped version
Robert Phillipsf10b2a52020-05-15 10:20:04 -040063GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf,
64 UseAllocator useAllocator,
65 GrDDLProvider creatingProvider)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040066 : INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator)
Greg Daniele252f082017-10-23 16:05:23 -040067 , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped())
Chris Dalton95d8ceb2019-07-30 11:17:59 -060068 , fMipMapsStatus(fTarget->asTexture()->texturePriv().mipMapsStatus())
Robert Phillipsf10b2a52020-05-15 10:20:04 -040069 SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
70 , fCreatingProvider(creatingProvider)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050071 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040072 , fDeferredUploader(nullptr) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040073 if (fTarget->getUniqueKey().isValid()) {
Robert Phillips9da87e02019-02-04 13:26:26 -050074 fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider();
Robert Phillipse5f73282019-06-18 17:15:04 -040075 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get());
Robert Phillipsae7d3f32017-09-21 08:26:08 -040076 }
77}
78
79GrTextureProxy::~GrTextureProxy() {
80 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
81 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
82 fTarget = nullptr;
Robert Phillips0790f8a2018-09-18 13:11:03 -040083
84 // In DDL-mode, uniquely keyed proxies keep their key even after their originating
85 // proxy provider has gone away. In that case there is noone to send the invalid key
86 // message to (Note: in this case we don't want to remove its cached resource).
87 if (fUniqueKey.isValid() && fProxyProvider) {
Robert Phillips427966a2018-12-20 17:20:43 -050088 fProxyProvider->processInvalidUniqueKey(fUniqueKey, this,
89 GrProxyProvider::InvalidateGPUResource::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040090 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050091 SkASSERT(!fProxyProvider);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040092 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040093}
robertphillips76948d42016-05-04 12:47:41 -070094
Robert Phillips10d17212019-04-24 14:09:10 -040095bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040096 if (this->isLazy()) {
Greg Daniel0a375db2018-02-01 12:21:39 -050097 return false;
98 }
Chris Dalton0b68dda2019-11-07 21:08:03 -070099 if (!this->instantiateImpl(resourceProvider, 1, GrRenderable::kNo, fMipMapped,
100 fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400101 return false;
robertphillips76948d42016-05-04 12:47:41 -0700102 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400103
Robert Phillipsb5204762019-06-19 14:12:13 -0400104 SkASSERT(!this->peekRenderTarget());
105 SkASSERT(this->peekTexture());
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400106 return true;
robertphillips76948d42016-05-04 12:47:41 -0700107}
108
Robert Phillips5af44de2017-07-18 14:49:38 -0400109sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
Chris Dalton0b68dda2019-11-07 21:08:03 -0700110 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, 1, GrRenderable::kNo,
111 fMipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400112 if (!surface) {
113 return nullptr;
114 }
115
Greg Daniele252f082017-10-23 16:05:23 -0400116 SkASSERT(!surface->asRenderTarget());
Robert Phillips5af44de2017-07-18 14:49:38 -0400117 SkASSERT(surface->asTexture());
118 return surface;
119}
120
Brian Osman099fa0f2017-10-02 16:38:32 -0400121void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
122 SkASSERT(!fTextureProxy->fDeferredUploader);
123 fTextureProxy->fDeferredUploader = std::move(uploader);
124}
125
126void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
Robert Phillipsa3f70262018-02-08 10:59:38 -0500127 // The texture proxy's contents may already have been uploaded or instantiation may have failed
Robert Phillipsb5204762019-06-19 14:12:13 -0400128 if (fTextureProxy->fDeferredUploader && fTextureProxy->isInstantiated()) {
Brian Osman099fa0f2017-10-02 16:38:32 -0400129 fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
130 }
131}
132
133void GrTextureProxyPriv::resetDeferredUploader() {
134 SkASSERT(fTextureProxy->fDeferredUploader);
135 fTextureProxy->fDeferredUploader.reset();
136}
137
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500138GrMipMapped GrTextureProxy::mipMapped() const {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400139 if (this->isInstantiated()) {
140 return this->peekTexture()->texturePriv().mipMapped();
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500141 }
142 return fMipMapped;
143}
144
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400145size_t GrTextureProxy::onUninstantiatedGpuMemorySize(const GrCaps& caps) const {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400146 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(), 1,
147 this->proxyMipMapped(), !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400148}
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400149
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500150GrSamplerState::Filter GrTextureProxy::HighestFilterMode(GrTextureType textureType) {
Brian Salomona3b02f52020-07-15 16:02:01 -0400151 return GrTextureTypeHasRestrictedSampling(textureType) ? GrSamplerState::Filter::kLinear
Michael Ludwig8fa469d2019-11-25 16:08:44 -0500152 : GrSamplerState::Filter::kMipMap;
153}
154
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500155bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first,
156 const GrSurfaceProxy* second) {
Michael Ludwig08bd1f72019-12-02 14:27:21 -0500157 // In order to be compatible, the proxies should also have the same texture type. This is
158 // checked explicitly since the GrBackendFormat == operator does not compare texture type
Greg Danielc71c7962020-01-14 16:44:18 -0500159 return first->backendFormat().textureType() == second->backendFormat().textureType() &&
Greg Daniel45723ac2018-11-30 10:12:43 -0500160 first->backendFormat() == second->backendFormat();
161}
162
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500163void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400164 SkASSERT(key.isValid());
165 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
166
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400167 if (fTarget && fSyncTargetKey) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500168 if (!fTarget->getUniqueKey().isValid()) {
169 fTarget->resourcePriv().setUniqueKey(key);
170 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400171 SkASSERT(fTarget->getUniqueKey() == key);
172 }
173
174 fUniqueKey = key;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500175 fProxyProvider = proxyProvider;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400176}
177
178void GrTextureProxy::clearUniqueKey() {
179 fUniqueKey.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500180 fProxyProvider = nullptr;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400181}
182
Brian Salomon63410e92020-03-23 18:32:50 -0400183GrSurfaceProxy::LazySurfaceDesc GrTextureProxy::callbackDesc() const {
184 SkISize dims;
185 SkBackingFit fit;
186 if (this->isFullyLazy()) {
187 fit = SkBackingFit::kApprox;
188 dims = {-1, -1};
189 } else {
190 fit = this->isFunctionallyExact() ? SkBackingFit::kExact : SkBackingFit::kApprox;
191 dims = this->dimensions();
192 }
193 return {
194 dims,
195 fit,
196 GrRenderable::kNo,
197 fMipMapped,
198 1,
199 this->backendFormat(),
200 this->isProtected(),
201 this->isBudgeted(),
202 };
203}
204
Chris Dalton706a6ff2017-11-29 22:01:06 -0700205#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400206void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500207 SkASSERT(!surface->asRenderTarget());
208
209 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
210 SkASSERT(surface->asTexture());
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600211 // It is possible to fulfill a non-mipmapped proxy with a mipmapped texture.
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400212 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
Robert Phillips6ba15ec2018-02-08 16:26:47 -0500213 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500214
Greg Daniel4065d452018-11-16 15:43:41 -0500215 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500216
217 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
218 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
Chris Dalton3f7932e2019-08-19 00:39:13 -0600219 SkASSERT(((int)proxyFlags & kGrInternalTextureFlagsMask) ==
220 ((int)surfaceFlags & kGrInternalTextureFlagsMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700221}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500222
Chris Dalton706a6ff2017-11-29 22:01:06 -0700223#endif
224