blob: d7be2fe52f8552b3fbd5e91fd299a7af38543982 [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/GrDeferredProxyUploader.h"
Adlai Hollera0693042020-10-14 11:23:11 -040013#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#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));
Brian Salomonbe1084b2021-01-26 13:29:30 -050037 if (this->textureType() == GrTextureType::kExternal) {
38 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
39 }
Stephen White3c0a50f2020-01-16 18:19:54 -050040}
Brian Salomon58389b92018-03-07 13:01:25 -050041
Chris Dalton706a6ff2017-11-29 22:01:06 -070042// Lazy-callback version
Brian Salomonbeb7f522019-08-30 16:19:42 -040043GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback,
44 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050045 SkISize dimensions,
Brian Salomon7e67dca2020-07-21 09:27:25 -040046 GrMipmapped mipMapped,
Brian Salomona6db5102020-07-21 09:56:23 -040047 GrMipmapStatus mipmapStatus,
Brian Salomonbeb7f522019-08-30 16:19:42 -040048 SkBackingFit fit,
49 SkBudgeted budgeted,
50 GrProtected isProtected,
51 GrInternalSurfaceFlags surfaceFlags,
Robert Phillipsf10b2a52020-05-15 10:20:04 -040052 UseAllocator useAllocator,
53 GrDDLProvider creatingProvider)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040054 : INHERITED(std::move(callback), format, dimensions, fit, budgeted, isProtected,
55 surfaceFlags, useAllocator)
Brian Salomon40a40622020-07-21 10:32:07 -040056 , fMipmapped(mipMapped)
Brian Salomon8c82a872020-07-21 12:09:58 -040057 , fMipmapStatus(mipmapStatus)
58 SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
Robert Phillipsf10b2a52020-05-15 10:20:04 -040059 , fCreatingProvider(creatingProvider)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050060 , fProxyProvider(nullptr)
Stephen White3c0a50f2020-01-16 18:19:54 -050061 , fDeferredUploader(nullptr) {
62 SkASSERT(!(fSurfaceFlags & GrInternalSurfaceFlags::kFramebufferOnly));
Brian Salomonbe1084b2021-01-26 13:29:30 -050063 if (this->textureType() == GrTextureType::kExternal) {
64 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
65 }
Stephen White3c0a50f2020-01-16 18:19:54 -050066}
Chris Dalton706a6ff2017-11-29 22:01:06 -070067
68// Wrapped version
Robert Phillipsf10b2a52020-05-15 10:20:04 -040069GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf,
70 UseAllocator useAllocator,
71 GrDDLProvider creatingProvider)
Brian Salomondf1bd6d2020-03-26 20:37:01 -040072 : INHERITED(std::move(surf), SkBackingFit::kExact, useAllocator)
Brian Salomon4cfae3b2020-07-23 10:33:24 -040073 , fMipmapped(fTarget->asTexture()->mipmapped())
74 , fMipmapStatus(fTarget->asTexture()->mipmapStatus())
Brian Salomon8c82a872020-07-21 12:09:58 -040075 SkDEBUGCODE(, fInitialMipmapStatus(fMipmapStatus))
Robert Phillipsf10b2a52020-05-15 10:20:04 -040076 , fCreatingProvider(creatingProvider)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050077 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040078 , fDeferredUploader(nullptr) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040079 if (fTarget->getUniqueKey().isValid()) {
Robert Phillips9da87e02019-02-04 13:26:26 -050080 fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider();
Robert Phillipse5f73282019-06-18 17:15:04 -040081 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get());
Robert Phillipsae7d3f32017-09-21 08:26:08 -040082 }
Brian Salomonbe1084b2021-01-26 13:29:30 -050083 if (this->textureType() == GrTextureType::kExternal) {
84 fSurfaceFlags |= GrInternalSurfaceFlags::kReadOnly;
85 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -040086}
87
88GrTextureProxy::~GrTextureProxy() {
89 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
90 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
91 fTarget = nullptr;
Robert Phillips0790f8a2018-09-18 13:11:03 -040092
93 // In DDL-mode, uniquely keyed proxies keep their key even after their originating
94 // proxy provider has gone away. In that case there is noone to send the invalid key
95 // message to (Note: in this case we don't want to remove its cached resource).
96 if (fUniqueKey.isValid() && fProxyProvider) {
Robert Phillips427966a2018-12-20 17:20:43 -050097 fProxyProvider->processInvalidUniqueKey(fUniqueKey, this,
98 GrProxyProvider::InvalidateGPUResource::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040099 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500100 SkASSERT(!fProxyProvider);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400101 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400102}
robertphillips76948d42016-05-04 12:47:41 -0700103
Robert Phillips10d17212019-04-24 14:09:10 -0400104bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400105 if (this->isLazy()) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500106 return false;
107 }
Brian Salomon40a40622020-07-21 10:32:07 -0400108 if (!this->instantiateImpl(resourceProvider, 1, GrRenderable::kNo, fMipmapped,
Chris Dalton0b68dda2019-11-07 21:08:03 -0700109 fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400110 return false;
robertphillips76948d42016-05-04 12:47:41 -0700111 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400112
Robert Phillipsb5204762019-06-19 14:12:13 -0400113 SkASSERT(!this->peekRenderTarget());
114 SkASSERT(this->peekTexture());
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400115 return true;
robertphillips76948d42016-05-04 12:47:41 -0700116}
117
Robert Phillips5af44de2017-07-18 14:49:38 -0400118sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
Chris Dalton0b68dda2019-11-07 21:08:03 -0700119 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, 1, GrRenderable::kNo,
Brian Salomon40a40622020-07-21 10:32:07 -0400120 fMipmapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400121 if (!surface) {
122 return nullptr;
123 }
124
Greg Daniele252f082017-10-23 16:05:23 -0400125 SkASSERT(!surface->asRenderTarget());
Robert Phillips5af44de2017-07-18 14:49:38 -0400126 SkASSERT(surface->asTexture());
127 return surface;
128}
129
Brian Osman099fa0f2017-10-02 16:38:32 -0400130void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
131 SkASSERT(!fTextureProxy->fDeferredUploader);
132 fTextureProxy->fDeferredUploader = std::move(uploader);
133}
134
135void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
Robert Phillipsa3f70262018-02-08 10:59:38 -0500136 // The texture proxy's contents may already have been uploaded or instantiation may have failed
Robert Phillipsb5204762019-06-19 14:12:13 -0400137 if (fTextureProxy->fDeferredUploader && fTextureProxy->isInstantiated()) {
Brian Osman099fa0f2017-10-02 16:38:32 -0400138 fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
139 }
140}
141
142void GrTextureProxyPriv::resetDeferredUploader() {
143 SkASSERT(fTextureProxy->fDeferredUploader);
144 fTextureProxy->fDeferredUploader.reset();
145}
146
Brian Salomon8c82a872020-07-21 12:09:58 -0400147GrMipmapped GrTextureProxy::mipmapped() const {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400148 if (this->isInstantiated()) {
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400149 return this->peekTexture()->mipmapped();
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500150 }
Brian Salomon40a40622020-07-21 10:32:07 -0400151 return fMipmapped;
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500152}
153
Greg Daniel0eca74c2020-10-01 13:46:00 -0400154size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
155 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
156 /*colorSamplesPerPixel=*/1, this->proxyMipmapped(),
157 !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400158}
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400159
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500160bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrSurfaceProxy* first,
161 const GrSurfaceProxy* second) {
Michael Ludwig08bd1f72019-12-02 14:27:21 -0500162 // In order to be compatible, the proxies should also have the same texture type. This is
163 // checked explicitly since the GrBackendFormat == operator does not compare texture type
Greg Danielc71c7962020-01-14 16:44:18 -0500164 return first->backendFormat().textureType() == second->backendFormat().textureType() &&
Greg Daniel45723ac2018-11-30 10:12:43 -0500165 first->backendFormat() == second->backendFormat();
166}
167
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500168void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400169 SkASSERT(key.isValid());
170 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
171
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400172 if (fTarget && fSyncTargetKey) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500173 if (!fTarget->getUniqueKey().isValid()) {
174 fTarget->resourcePriv().setUniqueKey(key);
175 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400176 SkASSERT(fTarget->getUniqueKey() == key);
177 }
178
179 fUniqueKey = key;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500180 fProxyProvider = proxyProvider;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400181}
182
183void GrTextureProxy::clearUniqueKey() {
184 fUniqueKey.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500185 fProxyProvider = nullptr;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400186}
187
Brian Salomon63410e92020-03-23 18:32:50 -0400188GrSurfaceProxy::LazySurfaceDesc GrTextureProxy::callbackDesc() const {
189 SkISize dims;
190 SkBackingFit fit;
191 if (this->isFullyLazy()) {
192 fit = SkBackingFit::kApprox;
193 dims = {-1, -1};
194 } else {
195 fit = this->isFunctionallyExact() ? SkBackingFit::kExact : SkBackingFit::kApprox;
196 dims = this->dimensions();
197 }
198 return {
199 dims,
200 fit,
201 GrRenderable::kNo,
Brian Salomon40a40622020-07-21 10:32:07 -0400202 fMipmapped,
Brian Salomon63410e92020-03-23 18:32:50 -0400203 1,
204 this->backendFormat(),
205 this->isProtected(),
206 this->isBudgeted(),
207 };
208}
209
Chris Dalton706a6ff2017-11-29 22:01:06 -0700210#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400211void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500212 SkASSERT(!surface->asRenderTarget());
213
214 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
215 SkASSERT(surface->asTexture());
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600216 // It is possible to fulfill a non-mipmapped proxy with a mipmapped texture.
Brian Salomon8c82a872020-07-21 12:09:58 -0400217 SkASSERT(GrMipmapped::kNo == this->proxyMipmapped() ||
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400218 GrMipmapped::kYes == surface->asTexture()->mipmapped());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500219
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400220 SkASSERT(surface->asTexture()->textureType() == this->textureType());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500221
222 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
Brian Salomonf7f54332020-07-28 09:23:35 -0400223 GrInternalSurfaceFlags surfaceFlags = surface->flags();
Chris Dalton3f7932e2019-08-19 00:39:13 -0600224 SkASSERT(((int)proxyFlags & kGrInternalTextureFlagsMask) ==
225 ((int)surfaceFlags & kGrInternalTextureFlagsMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700226}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500227
Chris Dalton706a6ff2017-11-29 22:01:06 -0700228#endif
229