blob: 6bd4761bb47998405091e77e238064ff369b5f7b [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
Robert Phillips4e105e22020-07-16 09:18:50 -040011#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrDeferredProxyUploader.h"
14#include "src/gpu/GrProxyProvider.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040015#include "src/gpu/GrSurface.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040016#include "src/gpu/GrTexture.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 Salomon7e67dca2020-07-21 09:27:25 -040021 GrMipmapped mipMapped,
Brian Salomona6db5102020-07-21 09:56:23 -040022 GrMipmapStatus mipmapStatus,
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 Salomon40a40622020-07-21 10:32:07 -040030 , fMipmapped(mipMapped)
Brian Salomon8c82a872020-07-21 12:09:58 -040031 , fMipmapStatus(mipmapStatus)
32 SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
Robert Phillipsf10b2a52020-05-15 10:20:04 -040033 , 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 Salomon7e67dca2020-07-21 09:27:25 -040043 GrMipmapped mipMapped,
Brian Salomona6db5102020-07-21 09:56:23 -040044 GrMipmapStatus mipmapStatus,
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)
Brian Salomon40a40622020-07-21 10:32:07 -040053 , fMipmapped(mipMapped)
Brian Salomon8c82a872020-07-21 12:09:58 -040054 , fMipmapStatus(mipmapStatus)
55 SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
Robert Phillipsf10b2a52020-05-15 10:20:04 -040056 , 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)
Brian Salomon4cfae3b2020-07-23 10:33:24 -040067 , fMipmapped(fTarget->asTexture()->mipmapped())
68 , fMipmapStatus(fTarget->asTexture()->mipmapStatus())
Brian Salomon8c82a872020-07-21 12:09:58 -040069 SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
Robert Phillipsf10b2a52020-05-15 10:20:04 -040070 , 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 }
Brian Salomon40a40622020-07-21 10:32:07 -040099 if (!this->instantiateImpl(resourceProvider, 1, GrRenderable::kNo, fMipmapped,
Chris Dalton0b68dda2019-11-07 21:08:03 -0700100 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,
Brian Salomon40a40622020-07-21 10:32:07 -0400111 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
Brian Salomon8c82a872020-07-21 12:09:58 -0400138GrMipmapped GrTextureProxy::mipmapped() const {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400139 if (this->isInstantiated()) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400140 return this->peekTexture()->mipmapped();
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500141 }
Brian Salomon40a40622020-07-21 10:32:07 -0400142 return fMipmapped;
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500143}
144
Greg Daniel0eca74c2020-10-01 13:46:00 -0400145size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
146 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
147 /*colorSamplesPerPixel=*/1, this->proxyMipmapped(),
148 !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400149}
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400150
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500151bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first,
152 const GrSurfaceProxy* second) {
Michael Ludwig08bd1f72019-12-02 14:27:21 -0500153 // In order to be compatible, the proxies should also have the same texture type. This is
154 // checked explicitly since the GrBackendFormat == operator does not compare texture type
Greg Danielc71c7962020-01-14 16:44:18 -0500155 return first->backendFormat().textureType() == second->backendFormat().textureType() &&
Greg Daniel45723ac2018-11-30 10:12:43 -0500156 first->backendFormat() == second->backendFormat();
157}
158
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500159void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400160 SkASSERT(key.isValid());
161 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
162
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400163 if (fTarget && fSyncTargetKey) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500164 if (!fTarget->getUniqueKey().isValid()) {
165 fTarget->resourcePriv().setUniqueKey(key);
166 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400167 SkASSERT(fTarget->getUniqueKey() == key);
168 }
169
170 fUniqueKey = key;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500171 fProxyProvider = proxyProvider;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400172}
173
174void GrTextureProxy::clearUniqueKey() {
175 fUniqueKey.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500176 fProxyProvider = nullptr;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400177}
178
Brian Salomon63410e92020-03-23 18:32:50 -0400179GrSurfaceProxy::LazySurfaceDesc GrTextureProxy::callbackDesc() const {
180 SkISize dims;
181 SkBackingFit fit;
182 if (this->isFullyLazy()) {
183 fit = SkBackingFit::kApprox;
184 dims = {-1, -1};
185 } else {
186 fit = this->isFunctionallyExact() ? SkBackingFit::kExact : SkBackingFit::kApprox;
187 dims = this->dimensions();
188 }
189 return {
190 dims,
191 fit,
192 GrRenderable::kNo,
Brian Salomon40a40622020-07-21 10:32:07 -0400193 fMipmapped,
Brian Salomon63410e92020-03-23 18:32:50 -0400194 1,
195 this->backendFormat(),
196 this->isProtected(),
197 this->isBudgeted(),
198 };
199}
200
Chris Dalton706a6ff2017-11-29 22:01:06 -0700201#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400202void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500203 SkASSERT(!surface->asRenderTarget());
204
205 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
206 SkASSERT(surface->asTexture());
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600207 // It is possible to fulfill a non-mipmapped proxy with a mipmapped texture.
Brian Salomon8c82a872020-07-21 12:09:58 -0400208 SkASSERT(GrMipmapped::kNo == this->proxyMipmapped() ||
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400209 GrMipmapped::kYes == surface->asTexture()->mipmapped());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500210
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400211 SkASSERT(surface->asTexture()->textureType() == this->textureType());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500212
213 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
Brian Salomonf7f54332020-07-28 09:23:35 -0400214 GrInternalSurfaceFlags surfaceFlags = surface->flags();
Chris Dalton3f7932e2019-08-19 00:39:13 -0600215 SkASSERT(((int)proxyFlags & kGrInternalTextureFlagsMask) ==
216 ((int)surfaceFlags & kGrInternalTextureFlagsMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700217}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500218
Chris Dalton706a6ff2017-11-29 22:01:06 -0700219#endif
220