blob: d7b5abfb203cf0440d2abe5e1f8b29e3503ed6cf [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"
Adlai Holler9e2c50e2021-02-09 14:41:52 -05009#include "src/gpu/GrTextureProxyPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070010
Robert Phillips4e105e22020-07-16 09:18:50 -040011#include "include/gpu/GrDirectContext.h"
Adlai Holler9e2c50e2021-02-09 14:41:52 -050012#include "src/gpu/GrDeferredProxyUploader.h"
Adlai Hollera0693042020-10-14 11:23:11 -040013#include "src/gpu/GrDirectContextPriv.h"
Robert Phillips1a82a4e2021-07-01 10:27:44 -040014#include "src/gpu/GrGpuResourcePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrProxyProvider.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040016#include "src/gpu/GrSurface.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040017#include "src/gpu/GrTexture.h"
robertphillips76948d42016-05-04 12:47:41 -070018
Brian Salomon58389b92018-03-07 13:01:25 -050019// Deferred version - no data
Brian Salomonbeb7f522019-08-30 16:19:42 -040020GrTextureProxy::GrTextureProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050021 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -040022 GrMipmapped mipMapped,
Brian Salomona6db5102020-07-21 09:56:23 -040023 GrMipmapStatus mipmapStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -040024 SkBackingFit fit,
25 SkBudgeted budgeted,
26 GrProtected isProtected,
27 GrInternalSurfaceFlags surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -040028 UseAllocator useAllocator,
29 GrDDLProvider creatingProvider)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040030 : INHERITED(format, dimensions, fit, budgeted, isProtected, surfaceFlags, useAllocator)
Brian Salomon40a40622020-07-21 10:32:07 -040031 , fMipmapped(mipMapped)
Brian Salomon8c82a872020-07-21 12:09:58 -040032 , fMipmapStatus(mipmapStatus)
33 SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
Robert Phillipsf10b2a52020-05-15 10:20:04 -040034 , fCreatingProvider(creatingProvider)
Adlai Holler9e2c50e2021-02-09 14:41:52 -050035 , fProxyProvider(nullptr)
36 , fDeferredUploader(nullptr) {
Stephen White3c0a50f2020-01-16 18:19:54 -050037 SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
Brian Salomonbe1084b2021-01-26 13:29:30 -050038 if (this->textureType() == GrTextureType::kExternal) {
39 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
40 }
Stephen White3c0a50f2020-01-16 18:19:54 -050041}
Brian Salomon58389b92018-03-07 13:01:25 -050042
Chris Dalton706a6ff2017-11-29 22:01:06 -070043// Lazy-callback version
Brian Salomonbeb7f522019-08-30 16:19:42 -040044GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback,
45 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050046 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -040047 GrMipmapped mipMapped,
Brian Salomona6db5102020-07-21 09:56:23 -040048 GrMipmapStatus mipmapStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -040049 SkBackingFit fit,
50 SkBudgeted budgeted,
51 GrProtected isProtected,
52 GrInternalSurfaceFlags surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -040053 UseAllocator useAllocator,
54 GrDDLProvider creatingProvider)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040055 : INHERITED(std::move(callback), format, dimensions, fit, budgeted, isProtected,
56 surfaceFlags, useAllocator)
Brian Salomon40a40622020-07-21 10:32:07 -040057 , fMipmapped(mipMapped)
Brian Salomon8c82a872020-07-21 12:09:58 -040058 , fMipmapStatus(mipmapStatus)
59 SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
Robert Phillipsf10b2a52020-05-15 10:20:04 -040060 , fCreatingProvider(creatingProvider)
Adlai Holler9e2c50e2021-02-09 14:41:52 -050061 , fProxyProvider(nullptr)
62 , fDeferredUploader(nullptr) {
Stephen White3c0a50f2020-01-16 18:19:54 -050063 SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
Brian Salomonbe1084b2021-01-26 13:29:30 -050064 if (this->textureType() == GrTextureType::kExternal) {
65 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
66 }
Stephen White3c0a50f2020-01-16 18:19:54 -050067}
Chris Dalton706a6ff2017-11-29 22:01:06 -070068
69// Wrapped version
Robert Phillipsf10b2a52020-05-15 10:20:04 -040070GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf,
71 UseAllocator useAllocator,
72 GrDDLProvider creatingProvider)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040073 : INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator)
Brian Salomon4cfae3b2020-07-23 10:33:24 -040074 , fMipmapped(fTarget->asTexture()->mipmapped())
75 , fMipmapStatus(fTarget->asTexture()->mipmapStatus())
Brian Salomon8c82a872020-07-21 12:09:58 -040076 SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
Robert Phillipsf10b2a52020-05-15 10:20:04 -040077 , fCreatingProvider(creatingProvider)
Adlai Holler9e2c50e2021-02-09 14:41:52 -050078 , fProxyProvider(nullptr)
79 , fDeferredUploader(nullptr) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040080 if (fTarget->getUniqueKey().isValid()) {
Robert Phillips9da87e02019-02-04 13:26:26 -050081 fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider();
Robert Phillipse5f73282019-06-18 17:15:04 -040082 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get());
Robert Phillipsae7d3f32017-09-21 08:26:08 -040083 }
Brian Salomonbe1084b2021-01-26 13:29:30 -050084 if (this->textureType() == GrTextureType::kExternal) {
85 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
86 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -040087}
88
89GrTextureProxy::~GrTextureProxy() {
90 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
91 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
92 fTarget = nullptr;
Robert Phillips0790f8a2018-09-18 13:11:03 -040093
94 // In DDL-mode, uniquely keyed proxies keep their key even after their originating
95 // proxy provider has gone away. In that case there is noone to send the invalid key
96 // message to (Note: in this case we don't want to remove its cached resource).
97 if (fUniqueKey.isValid() && fProxyProvider) {
Robert Phillips427966a2018-12-20 17:20:43 -050098 fProxyProvider->processInvalidUniqueKey(fUniqueKey, this,
99 GrProxyProvider::InvalidateGPUResource::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400100 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500101 SkASSERT(!fProxyProvider);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400102 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400103}
robertphillips76948d42016-05-04 12:47:41 -0700104
Robert Phillips10d17212019-04-24 14:09:10 -0400105bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400106 if (this->isLazy()) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500107 return false;
108 }
Brian Salomon40a40622020-07-21 10:32:07 -0400109 if (!this->instantiateImpl(resourceProvider, 1, GrRenderable::kNo, fMipmapped,
Chris Dalton0b68dda2019-11-07 21:08:03 -0700110 fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400111 return false;
robertphillips76948d42016-05-04 12:47:41 -0700112 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400113
Robert Phillipsb5204762019-06-19 14:12:13 -0400114 SkASSERT(!this->peekRenderTarget());
115 SkASSERT(this->peekTexture());
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400116 return true;
robertphillips76948d42016-05-04 12:47:41 -0700117}
118
Robert Phillips5af44de2017-07-18 14:49:38 -0400119sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
Chris Dalton0b68dda2019-11-07 21:08:03 -0700120 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, 1, GrRenderable::kNo,
Brian Salomon40a40622020-07-21 10:32:07 -0400121 fMipmapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400122 if (!surface) {
123 return nullptr;
124 }
125
Greg Daniele252f082017-10-23 16:05:23 -0400126 SkASSERT(!surface->asRenderTarget());
Robert Phillips5af44de2017-07-18 14:49:38 -0400127 SkASSERT(surface->asTexture());
128 return surface;
129}
130
Adlai Holler9e2c50e2021-02-09 14:41:52 -0500131void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
132 SkASSERT(!fTextureProxy->fDeferredUploader);
133 fTextureProxy->fDeferredUploader = std::move(uploader);
134}
135
136void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
137 // The texture proxy's contents may already have been uploaded or instantiation may have failed
138 if (fTextureProxy->fDeferredUploader && fTextureProxy->isInstantiated()) {
139 fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
140 }
141}
142
143void GrTextureProxyPriv::resetDeferredUploader() {
144 SkASSERT(fTextureProxy->fDeferredUploader);
145 fTextureProxy->fDeferredUploader.reset();
146}
147
Brian Salomon8c82a872020-07-21 12:09:58 -0400148GrMipmapped GrTextureProxy::mipmapped() const {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400149 if (this->isInstantiated()) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400150 return this->peekTexture()->mipmapped();
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500151 }
Brian Salomon40a40622020-07-21 10:32:07 -0400152 return fMipmapped;
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500153}
154
Greg Daniel0eca74c2020-10-01 13:46:00 -0400155size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
156 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
157 /*colorSamplesPerPixel=*/1, this->proxyMipmapped(),
158 !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400159}
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400160
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500161bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first,
162 const GrSurfaceProxy* second) {
Michael Ludwig08bd1f72019-12-02 14:27:21 -0500163 // In order to be compatible, the proxies should also have the same texture type. This is
164 // checked explicitly since the GrBackendFormat == operator does not compare texture type
Greg Danielc71c7962020-01-14 16:44:18 -0500165 return first->backendFormat().textureType() == second->backendFormat().textureType() &&
Greg Daniel45723ac2018-11-30 10:12:43 -0500166 first->backendFormat() == second->backendFormat();
167}
168
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500169void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400170 SkASSERT(key.isValid());
171 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
172
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400173 if (fTarget && fSyncTargetKey) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500174 if (!fTarget->getUniqueKey().isValid()) {
175 fTarget->resourcePriv().setUniqueKey(key);
176 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400177 SkASSERT(fTarget->getUniqueKey() == key);
178 }
179
180 fUniqueKey = key;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500181 fProxyProvider = proxyProvider;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400182}
183
184void GrTextureProxy::clearUniqueKey() {
185 fUniqueKey.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500186 fProxyProvider = nullptr;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400187}
188
Brian Salomon63410e92020-03-23 18:32:50 -0400189GrSurfaceProxy::LazySurfaceDesc GrTextureProxy::callbackDesc() const {
190 SkISize dims;
191 SkBackingFit fit;
192 if (this->isFullyLazy()) {
193 fit = SkBackingFit::kApprox;
194 dims = {-1, -1};
195 } else {
196 fit = this->isFunctionallyExact() ? SkBackingFit::kExact : SkBackingFit::kApprox;
197 dims = this->dimensions();
198 }
199 return {
200 dims,
201 fit,
202 GrRenderable::kNo,
Brian Salomon40a40622020-07-21 10:32:07 -0400203 fMipmapped,
Brian Salomon63410e92020-03-23 18:32:50 -0400204 1,
205 this->backendFormat(),
Greg Daniele56d31f2021-08-16 09:29:56 -0400206 this->textureType(),
Brian Salomon63410e92020-03-23 18:32:50 -0400207 this->isProtected(),
208 this->isBudgeted(),
209 };
210}
211
Chris Dalton706a6ff2017-11-29 22:01:06 -0700212#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400213void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500214 SkASSERT(!surface->asRenderTarget());
215
216 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
217 SkASSERT(surface->asTexture());
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600218 // It is possible to fulfill a non-mipmapped proxy with a mipmapped texture.
Brian Salomon8c82a872020-07-21 12:09:58 -0400219 SkASSERT(GrMipmapped::kNo == this->proxyMipmapped() ||
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400220 GrMipmapped::kYes == surface->asTexture()->mipmapped());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500221
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400222 SkASSERT(surface->asTexture()->textureType() == this->textureType());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500223
224 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
Brian Salomonf7f54332020-07-28 09:23:35 -0400225 GrInternalSurfaceFlags surfaceFlags = surface->flags();
Chris Dalton3f7932e2019-08-19 00:39:13 -0600226 SkASSERT(((int)proxyFlags & kGrInternalTextureFlagsMask) ==
227 ((int)surfaceFlags & kGrInternalTextureFlagsMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700228}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500229
Chris Dalton706a6ff2017-11-29 22:01:06 -0700230#endif
231