blob: a71b9f34f0dda360fa8d17c77ea931198a44aba0 [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
Greg Daniel4065d452018-11-16 15:43:41 -050019GrTextureProxy::GrTextureProxy(const GrBackendFormat& format, const GrSurfaceDesc& srcDesc,
20 GrSurfaceOrigin origin, GrMipMapped mipMapped,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040021 const GrSwizzle& textureSwizzle, SkBackingFit fit,
Brian Salomone8a766b2019-07-19 14:24:36 -040022 SkBudgeted budgeted, GrProtected isProtected,
23 GrInternalSurfaceFlags surfaceFlags)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040024 : INHERITED(format, srcDesc, GrRenderable::kNo, origin, textureSwizzle, fit, budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040025 isProtected, surfaceFlags)
Brian Salomon58389b92018-03-07 13:01:25 -050026 , fMipMapped(mipMapped)
27 , fProxyProvider(nullptr)
28 , fDeferredUploader(nullptr) {}
29
Chris Dalton706a6ff2017-11-29 22:01:06 -070030// Lazy-callback version
Greg Daniel457469c2018-02-08 15:05:44 -050031GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
Greg Daniel4065d452018-11-16 15:43:41 -050032 const GrBackendFormat& format, const GrSurfaceDesc& desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040033 GrSurfaceOrigin origin, GrMipMapped mipMapped,
34 const GrSwizzle& texSwizzle, SkBackingFit fit, SkBudgeted budgeted,
Brian Salomone8a766b2019-07-19 14:24:36 -040035 GrProtected isProtected, GrInternalSurfaceFlags surfaceFlags)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040036 : INHERITED(std::move(callback), lazyType, format, desc, GrRenderable::kNo, origin,
Brian Salomone8a766b2019-07-19 14:24:36 -040037 texSwizzle, fit, budgeted, isProtected, surfaceFlags)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050038 , fMipMapped(mipMapped)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050039 , fProxyProvider(nullptr)
Brian Salomon7226c232018-07-30 13:13:17 -040040 , fDeferredUploader(nullptr) {}
Chris Dalton706a6ff2017-11-29 22:01:06 -070041
42// Wrapped version
Greg Daniel2c19e7f2019-06-18 13:29:21 -040043GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin,
44 const GrSwizzle& textureSwizzle)
45 : INHERITED(std::move(surf), origin, textureSwizzle, SkBackingFit::kExact)
Greg Daniele252f082017-10-23 16:05:23 -040046 , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped())
Robert Phillips1afd4cd2018-01-08 13:40:32 -050047 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040048 , fDeferredUploader(nullptr) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040049 if (fTarget->getUniqueKey().isValid()) {
Robert Phillips9da87e02019-02-04 13:26:26 -050050 fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider();
Robert Phillipse5f73282019-06-18 17:15:04 -040051 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get());
Robert Phillipsae7d3f32017-09-21 08:26:08 -040052 }
53}
54
55GrTextureProxy::~GrTextureProxy() {
56 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
57 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
58 fTarget = nullptr;
Robert Phillips0790f8a2018-09-18 13:11:03 -040059
60 // In DDL-mode, uniquely keyed proxies keep their key even after their originating
61 // proxy provider has gone away. In that case there is noone to send the invalid key
62 // message to (Note: in this case we don't want to remove its cached resource).
63 if (fUniqueKey.isValid() && fProxyProvider) {
Robert Phillips427966a2018-12-20 17:20:43 -050064 fProxyProvider->processInvalidUniqueKey(fUniqueKey, this,
65 GrProxyProvider::InvalidateGPUResource::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040066 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067 SkASSERT(!fProxyProvider);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040068 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040069}
robertphillips76948d42016-05-04 12:47:41 -070070
Robert Phillips10d17212019-04-24 14:09:10 -040071bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050072 if (LazyState::kNot != this->lazyInstantiationState()) {
73 return false;
74 }
Brian Salomonf2c2ba92019-07-17 09:59:59 -040075 if (!this->instantiateImpl(resourceProvider, 1, /* needsStencil = */ false, GrRenderable::kNo,
76 fMipMapped, fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040077 return false;
robertphillips76948d42016-05-04 12:47:41 -070078 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040079
Robert Phillipsb5204762019-06-19 14:12:13 -040080 SkASSERT(!this->peekRenderTarget());
81 SkASSERT(this->peekTexture());
Robert Phillipseee4d6e2017-06-05 09:26:07 -040082 return true;
robertphillips76948d42016-05-04 12:47:41 -070083}
84
Robert Phillips5af44de2017-07-18 14:49:38 -040085sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
Brian Salomonf2c2ba92019-07-17 09:59:59 -040086 sk_sp<GrSurface> surface =
87 this->createSurfaceImpl(resourceProvider, 1,
88 /* needsStencil = */ false, GrRenderable::kNo, fMipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -040089 if (!surface) {
90 return nullptr;
91 }
92
Greg Daniele252f082017-10-23 16:05:23 -040093 SkASSERT(!surface->asRenderTarget());
Robert Phillips5af44de2017-07-18 14:49:38 -040094 SkASSERT(surface->asTexture());
95 return surface;
96}
97
Brian Osman099fa0f2017-10-02 16:38:32 -040098void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
99 SkASSERT(!fTextureProxy->fDeferredUploader);
100 fTextureProxy->fDeferredUploader = std::move(uploader);
101}
102
103void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
Robert Phillipsa3f70262018-02-08 10:59:38 -0500104 // The texture proxy's contents may already have been uploaded or instantiation may have failed
Robert Phillipsb5204762019-06-19 14:12:13 -0400105 if (fTextureProxy->fDeferredUploader && fTextureProxy->isInstantiated()) {
Brian Osman099fa0f2017-10-02 16:38:32 -0400106 fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
107 }
108}
109
110void GrTextureProxyPriv::resetDeferredUploader() {
111 SkASSERT(fTextureProxy->fDeferredUploader);
112 fTextureProxy->fDeferredUploader.reset();
113}
114
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400115GrSamplerState::Filter GrTextureProxy::highestFilterMode() const {
Brian Salomone632dfc2018-08-01 13:01:16 -0400116 return this->hasRestrictedSampling() ? GrSamplerState::Filter::kBilerp
117 : GrSamplerState::Filter::kMipMap;
Robert Phillips49081d12017-05-08 13:41:35 -0400118}
119
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500120GrMipMapped GrTextureProxy::mipMapped() const {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400121 if (this->isInstantiated()) {
122 return this->peekTexture()->texturePriv().mipMapped();
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500123 }
124 return fMipMapped;
125}
126
Brian Salomonbb5711a2017-05-17 13:49:59 -0400127size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700128 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400129 this->proxyMipMapped(), !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400130}
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400131
Greg Daniel45723ac2018-11-30 10:12:43 -0500132bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrTextureProxy* first,
133 const GrTextureProxy* second) {
134 return first->config() == second->config() &&
135 first->textureType() == second->textureType() &&
136 first->backendFormat() == second->backendFormat();
137}
138
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500139void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400140 SkASSERT(key.isValid());
141 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
142
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400143 if (fTarget && fSyncTargetKey) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500144 if (!fTarget->getUniqueKey().isValid()) {
145 fTarget->resourcePriv().setUniqueKey(key);
146 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400147 SkASSERT(fTarget->getUniqueKey() == key);
148 }
149
150 fUniqueKey = key;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500151 fProxyProvider = proxyProvider;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400152}
153
154void GrTextureProxy::clearUniqueKey() {
155 fUniqueKey.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500156 fProxyProvider = nullptr;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400157}
158
Chris Dalton706a6ff2017-11-29 22:01:06 -0700159#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400160void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500161 SkASSERT(!surface->asRenderTarget());
162
163 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
164 SkASSERT(surface->asTexture());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400165 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
Robert Phillips6ba15ec2018-02-08 16:26:47 -0500166 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500167
Greg Daniel4065d452018-11-16 15:43:41 -0500168 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500169
170 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
171 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
172 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kTextureMask) ==
173 (surfaceFlags & GrInternalSurfaceFlags::kTextureMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700174}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500175
Chris Dalton706a6ff2017-11-29 22:01:06 -0700176#endif
177