blob: 5c7d3041bfec054ecd1530da7e58eb557d2128dc [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,
Chris Dalton95d8ceb2019-07-30 11:17:59 -060021 GrMipMapsStatus mipMapsStatus, const GrSwizzle& textureSwizzle,
22 SkBackingFit fit, SkBudgeted budgeted, GrProtected isProtected,
Brian Salomone8a766b2019-07-19 14:24:36 -040023 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)
Chris Dalton95d8ceb2019-07-30 11:17:59 -060027 , fMipMapsStatus(mipMapsStatus)
28 SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
Brian Salomon58389b92018-03-07 13:01:25 -050029 , fProxyProvider(nullptr)
30 , fDeferredUploader(nullptr) {}
31
Chris Dalton706a6ff2017-11-29 22:01:06 -070032// Lazy-callback version
Greg Daniel457469c2018-02-08 15:05:44 -050033GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
Greg Daniel4065d452018-11-16 15:43:41 -050034 const GrBackendFormat& format, const GrSurfaceDesc& desc,
Greg Daniel2c19e7f2019-06-18 13:29:21 -040035 GrSurfaceOrigin origin, GrMipMapped mipMapped,
Chris Dalton95d8ceb2019-07-30 11:17:59 -060036 GrMipMapsStatus mipMapsStatus, const GrSwizzle& texSwizzle,
37 SkBackingFit fit, SkBudgeted budgeted, GrProtected isProtected,
38 GrInternalSurfaceFlags surfaceFlags)
Brian Salomonf2c2ba92019-07-17 09:59:59 -040039 : INHERITED(std::move(callback), lazyType, format, desc, GrRenderable::kNo, origin,
Brian Salomone8a766b2019-07-19 14:24:36 -040040 texSwizzle, fit, budgeted, isProtected, surfaceFlags)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050041 , fMipMapped(mipMapped)
Chris Dalton95d8ceb2019-07-30 11:17:59 -060042 , fMipMapsStatus(mipMapsStatus)
43 SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
Robert Phillips1afd4cd2018-01-08 13:40:32 -050044 , fProxyProvider(nullptr)
Brian Salomon7226c232018-07-30 13:13:17 -040045 , fDeferredUploader(nullptr) {}
Chris Dalton706a6ff2017-11-29 22:01:06 -070046
47// Wrapped version
Greg Daniel2c19e7f2019-06-18 13:29:21 -040048GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin,
49 const GrSwizzle& textureSwizzle)
50 : INHERITED(std::move(surf), origin, textureSwizzle, SkBackingFit::kExact)
Greg Daniele252f082017-10-23 16:05:23 -040051 , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped())
Chris Dalton95d8ceb2019-07-30 11:17:59 -060052 , fMipMapsStatus(fTarget->asTexture()->texturePriv().mipMapsStatus())
53 SkDEBUGCODE(, fInitialMipMapsStatus(fMipMapsStatus))
Robert Phillips1afd4cd2018-01-08 13:40:32 -050054 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040055 , fDeferredUploader(nullptr) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040056 if (fTarget->getUniqueKey().isValid()) {
Robert Phillips9da87e02019-02-04 13:26:26 -050057 fProxyProvider = fTarget->asTexture()->getContext()->priv().proxyProvider();
Robert Phillipse5f73282019-06-18 17:15:04 -040058 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget.get());
Robert Phillipsae7d3f32017-09-21 08:26:08 -040059 }
60}
61
62GrTextureProxy::~GrTextureProxy() {
63 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
64 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
65 fTarget = nullptr;
Robert Phillips0790f8a2018-09-18 13:11:03 -040066
67 // In DDL-mode, uniquely keyed proxies keep their key even after their originating
68 // proxy provider has gone away. In that case there is noone to send the invalid key
69 // message to (Note: in this case we don't want to remove its cached resource).
70 if (fUniqueKey.isValid() && fProxyProvider) {
Robert Phillips427966a2018-12-20 17:20:43 -050071 fProxyProvider->processInvalidUniqueKey(fUniqueKey, this,
72 GrProxyProvider::InvalidateGPUResource::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040073 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074 SkASSERT(!fProxyProvider);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040075 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040076}
robertphillips76948d42016-05-04 12:47:41 -070077
Robert Phillips10d17212019-04-24 14:09:10 -040078bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050079 if (LazyState::kNot != this->lazyInstantiationState()) {
80 return false;
81 }
Brian Salomonf2c2ba92019-07-17 09:59:59 -040082 if (!this->instantiateImpl(resourceProvider, 1, /* needsStencil = */ false, GrRenderable::kNo,
83 fMipMapped, fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040084 return false;
robertphillips76948d42016-05-04 12:47:41 -070085 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040086
Robert Phillipsb5204762019-06-19 14:12:13 -040087 SkASSERT(!this->peekRenderTarget());
88 SkASSERT(this->peekTexture());
Robert Phillipseee4d6e2017-06-05 09:26:07 -040089 return true;
robertphillips76948d42016-05-04 12:47:41 -070090}
91
Robert Phillips5af44de2017-07-18 14:49:38 -040092sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
Brian Salomonf2c2ba92019-07-17 09:59:59 -040093 sk_sp<GrSurface> surface =
94 this->createSurfaceImpl(resourceProvider, 1,
95 /* needsStencil = */ false, GrRenderable::kNo, fMipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -040096 if (!surface) {
97 return nullptr;
98 }
99
Greg Daniele252f082017-10-23 16:05:23 -0400100 SkASSERT(!surface->asRenderTarget());
Robert Phillips5af44de2017-07-18 14:49:38 -0400101 SkASSERT(surface->asTexture());
102 return surface;
103}
104
Brian Osman099fa0f2017-10-02 16:38:32 -0400105void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
106 SkASSERT(!fTextureProxy->fDeferredUploader);
107 fTextureProxy->fDeferredUploader = std::move(uploader);
108}
109
110void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
Robert Phillipsa3f70262018-02-08 10:59:38 -0500111 // The texture proxy's contents may already have been uploaded or instantiation may have failed
Robert Phillipsb5204762019-06-19 14:12:13 -0400112 if (fTextureProxy->fDeferredUploader && fTextureProxy->isInstantiated()) {
Brian Osman099fa0f2017-10-02 16:38:32 -0400113 fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
114 }
115}
116
117void GrTextureProxyPriv::resetDeferredUploader() {
118 SkASSERT(fTextureProxy->fDeferredUploader);
119 fTextureProxy->fDeferredUploader.reset();
120}
121
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400122GrSamplerState::Filter GrTextureProxy::highestFilterMode() const {
Brian Salomone632dfc2018-08-01 13:01:16 -0400123 return this->hasRestrictedSampling() ? GrSamplerState::Filter::kBilerp
124 : GrSamplerState::Filter::kMipMap;
Robert Phillips49081d12017-05-08 13:41:35 -0400125}
126
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500127GrMipMapped GrTextureProxy::mipMapped() const {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400128 if (this->isInstantiated()) {
129 return this->peekTexture()->texturePriv().mipMapped();
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500130 }
131 return fMipMapped;
132}
133
Brian Salomonbb5711a2017-05-17 13:49:59 -0400134size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700135 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400136 this->proxyMipMapped(), !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400137}
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400138
Greg Daniel45723ac2018-11-30 10:12:43 -0500139bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrTextureProxy* first,
140 const GrTextureProxy* second) {
141 return first->config() == second->config() &&
142 first->textureType() == second->textureType() &&
143 first->backendFormat() == second->backendFormat();
144}
145
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500146void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400147 SkASSERT(key.isValid());
148 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
149
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400150 if (fTarget && fSyncTargetKey) {
Robert Phillipsadbe1322018-01-17 13:35:46 -0500151 if (!fTarget->getUniqueKey().isValid()) {
152 fTarget->resourcePriv().setUniqueKey(key);
153 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400154 SkASSERT(fTarget->getUniqueKey() == key);
155 }
156
157 fUniqueKey = key;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500158 fProxyProvider = proxyProvider;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400159}
160
161void GrTextureProxy::clearUniqueKey() {
162 fUniqueKey.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500163 fProxyProvider = nullptr;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400164}
165
Chris Dalton706a6ff2017-11-29 22:01:06 -0700166#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400167void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500168 SkASSERT(!surface->asRenderTarget());
169
170 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
171 SkASSERT(surface->asTexture());
Chris Dalton95d8ceb2019-07-30 11:17:59 -0600172 // It is possible to fulfill a non-mipmapped proxy with a mipmapped texture.
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400173 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
Robert Phillips6ba15ec2018-02-08 16:26:47 -0500174 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500175
Greg Daniel4065d452018-11-16 15:43:41 -0500176 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500177
178 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
179 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
180 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kTextureMask) ==
181 (surfaceFlags & GrInternalSurfaceFlags::kTextureMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700182}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500183
Chris Dalton706a6ff2017-11-29 22:01:06 -0700184#endif
185