blob: b15b0b22eaaa9753bb8c96a9c1f1aa3356420e7b [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
8#include "GrTextureProxy.h"
Brian Osman099fa0f2017-10-02 16:38:32 -04009#include "GrTextureProxyPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070010
Robert Phillipsae7d3f32017-09-21 08:26:08 -040011#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050012#include "GrContextPriv.h"
Brian Osman099fa0f2017-10-02 16:38:32 -040013#include "GrDeferredProxyUploader.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050014#include "GrProxyProvider.h"
Greg Daniel849dce12018-04-24 14:32:53 -040015#include "GrSurfacePriv.h"
Robert Phillipsa4c41b32017-03-15 13:02:45 -040016#include "GrTexturePriv.h"
robertphillips76948d42016-05-04 12:47:41 -070017
Brian Salomon58389b92018-03-07 13:01:25 -050018// Deferred version - with data
Greg Daniel4065d452018-11-16 15:43:41 -050019GrTextureProxy::GrTextureProxy(const GrBackendFormat& format, const GrSurfaceDesc& srcDesc,
20 GrMipMapped mipMapped, SkBackingFit fit, SkBudgeted budgeted,
Brian Salomon7226c232018-07-30 13:13:17 -040021 const void* srcData, size_t /*rowBytes*/,
22 GrInternalSurfaceFlags surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -050023 : INHERITED(format, srcDesc, kTopLeft_GrSurfaceOrigin, fit, budgeted, surfaceFlags)
Greg Danielf6f7b672018-02-15 13:06:26 -050024 , fMipMapped(mipMapped)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050025 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040026 , fDeferredUploader(nullptr) {
Brian Salomonbb5711a2017-05-17 13:49:59 -040027 SkASSERT(!srcData); // currently handled in Make()
robertphillips8abb3702016-08-31 14:04:06 -070028}
29
Brian Salomon58389b92018-03-07 13:01:25 -050030// Deferred version - no data
Greg Daniel4065d452018-11-16 15:43:41 -050031GrTextureProxy::GrTextureProxy(const GrBackendFormat& format, const GrSurfaceDesc& srcDesc,
32 GrSurfaceOrigin origin, GrMipMapped mipMapped,
33 SkBackingFit fit, SkBudgeted budgeted,
34 GrInternalSurfaceFlags surfaceFlags)
35 : INHERITED(format, srcDesc, origin, fit, budgeted, surfaceFlags)
Brian Salomon58389b92018-03-07 13:01:25 -050036 , fMipMapped(mipMapped)
37 , fProxyProvider(nullptr)
38 , fDeferredUploader(nullptr) {}
39
Chris Dalton706a6ff2017-11-29 22:01:06 -070040// Lazy-callback version
Greg Daniel457469c2018-02-08 15:05:44 -050041GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
Greg Daniel4065d452018-11-16 15:43:41 -050042 const GrBackendFormat& format, const GrSurfaceDesc& desc,
43 GrSurfaceOrigin origin, GrMipMapped mipMapped, SkBackingFit fit,
Brian Salomon7226c232018-07-30 13:13:17 -040044 SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -050045 : INHERITED(std::move(callback), lazyType, format, desc, origin, fit, budgeted,
46 surfaceFlags)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050047 , fMipMapped(mipMapped)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050048 , fProxyProvider(nullptr)
Brian Salomon7226c232018-07-30 13:13:17 -040049 , fDeferredUploader(nullptr) {}
Chris Dalton706a6ff2017-11-29 22:01:06 -070050
51// Wrapped version
Robert Phillips066f0202017-07-25 10:16:35 -040052GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin)
53 : INHERITED(std::move(surf), origin, SkBackingFit::kExact)
Greg Daniele252f082017-10-23 16:05:23 -040054 , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped())
Robert Phillips1afd4cd2018-01-08 13:40:32 -050055 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040056 , fDeferredUploader(nullptr) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040057 if (fTarget->getUniqueKey().isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050058 fProxyProvider = fTarget->asTexture()->getContext()->contextPriv().proxyProvider();
59 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040060 }
61}
62
63GrTextureProxy::~GrTextureProxy() {
64 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
65 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
66 fTarget = nullptr;
Robert Phillips0790f8a2018-09-18 13:11:03 -040067
68 // In DDL-mode, uniquely keyed proxies keep their key even after their originating
69 // proxy provider has gone away. In that case there is noone to send the invalid key
70 // message to (Note: in this case we don't want to remove its cached resource).
71 if (fUniqueKey.isValid() && fProxyProvider) {
Robert Phillips427966a2018-12-20 17:20:43 -050072 fProxyProvider->processInvalidUniqueKey(fUniqueKey, this,
73 GrProxyProvider::InvalidateGPUResource::kNo);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040074 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050075 SkASSERT(!fProxyProvider);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040076 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040077}
robertphillips76948d42016-05-04 12:47:41 -070078
Robert Phillipseee4d6e2017-06-05 09:26:07 -040079bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050080 if (LazyState::kNot != this->lazyInstantiationState()) {
81 return false;
82 }
Brian Salomonbdecacf2018-02-02 20:32:49 -050083 if (!this->instantiateImpl(resourceProvider, 1, /* needsStencil = */ false,
Greg Danield2d8e922018-02-12 12:07:39 -050084 kNone_GrSurfaceFlags, fMipMapped,
Robert Phillipsae7d3f32017-09-21 08:26:08 -040085 fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040086 return false;
robertphillips76948d42016-05-04 12:47:41 -070087 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040088
Greg Daniele252f082017-10-23 16:05:23 -040089 SkASSERT(!fTarget->asRenderTarget());
Robert Phillipseee4d6e2017-06-05 09:26:07 -040090 SkASSERT(fTarget->asTexture());
91 return true;
robertphillips76948d42016-05-04 12:47:41 -070092}
93
Robert Phillips5af44de2017-07-18 14:49:38 -040094sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050095 sk_sp<GrSurface> surface= this->createSurfaceImpl(resourceProvider, 1,
Robert Phillips65048132017-08-10 08:44:49 -040096 /* needsStencil = */ false,
97 kNone_GrSurfaceFlags,
Greg Danield2d8e922018-02-12 12:07:39 -050098 fMipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -040099 if (!surface) {
100 return nullptr;
101 }
102
Greg Daniele252f082017-10-23 16:05:23 -0400103 SkASSERT(!surface->asRenderTarget());
Robert Phillips5af44de2017-07-18 14:49:38 -0400104 SkASSERT(surface->asTexture());
105 return surface;
106}
107
Brian Osman099fa0f2017-10-02 16:38:32 -0400108void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
109 SkASSERT(!fTextureProxy->fDeferredUploader);
110 fTextureProxy->fDeferredUploader = std::move(uploader);
111}
112
113void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
Robert Phillipsa3f70262018-02-08 10:59:38 -0500114 // The texture proxy's contents may already have been uploaded or instantiation may have failed
115 if (fTextureProxy->fDeferredUploader && fTextureProxy->fTarget) {
Brian Osman099fa0f2017-10-02 16:38:32 -0400116 fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
117 }
118}
119
120void GrTextureProxyPriv::resetDeferredUploader() {
121 SkASSERT(fTextureProxy->fDeferredUploader);
122 fTextureProxy->fDeferredUploader.reset();
123}
124
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400125GrSamplerState::Filter GrTextureProxy::highestFilterMode() const {
Brian Salomone632dfc2018-08-01 13:01:16 -0400126 return this->hasRestrictedSampling() ? GrSamplerState::Filter::kBilerp
127 : GrSamplerState::Filter::kMipMap;
Robert Phillips49081d12017-05-08 13:41:35 -0400128}
129
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500130GrMipMapped GrTextureProxy::mipMapped() const {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400131 if (this->isInstantiated()) {
132 return this->peekTexture()->texturePriv().mipMapped();
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500133 }
134 return fMipMapped;
135}
136
Brian Salomonbb5711a2017-05-17 13:49:59 -0400137size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700138 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400139 this->proxyMipMapped(), !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400140}
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400141
Greg Daniel45723ac2018-11-30 10:12:43 -0500142bool GrTextureProxy::ProxiesAreCompatibleAsDynamicState(const GrTextureProxy* first,
143 const GrTextureProxy* second) {
144 return first->config() == second->config() &&
145 first->textureType() == second->textureType() &&
146 first->backendFormat() == second->backendFormat();
147}
148
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500149void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400150 SkASSERT(key.isValid());
151 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
152
Robert Phillipsadbe1322018-01-17 13:35:46 -0500153 if (fTarget) {
154 if (!fTarget->getUniqueKey().isValid()) {
155 fTarget->resourcePriv().setUniqueKey(key);
156 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400157 SkASSERT(fTarget->getUniqueKey() == key);
158 }
159
160 fUniqueKey = key;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500161 fProxyProvider = proxyProvider;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400162}
163
164void GrTextureProxy::clearUniqueKey() {
165 fUniqueKey.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500166 fProxyProvider = nullptr;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400167}
168
Chris Dalton706a6ff2017-11-29 22:01:06 -0700169#ifdef SK_DEBUG
Greg Daniel849dce12018-04-24 14:32:53 -0400170void GrTextureProxy::onValidateSurface(const GrSurface* surface) {
Robert Phillipse8fabb22018-02-04 14:33:21 -0500171 SkASSERT(!surface->asRenderTarget());
172
173 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
174 SkASSERT(surface->asTexture());
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400175 SkASSERT(GrMipMapped::kNo == this->proxyMipMapped() ||
Robert Phillips6ba15ec2018-02-08 16:26:47 -0500176 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500177
Greg Daniel4065d452018-11-16 15:43:41 -0500178 SkASSERT(surface->asTexture()->texturePriv().textureType() == this->textureType());
Brian Salomonc67c31c2018-12-06 10:00:03 -0500179
180 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags;
181 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
182 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kTextureMask) ==
183 (surfaceFlags & GrInternalSurfaceFlags::kTextureMask));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700184}
Brian Salomonc67c31c2018-12-06 10:00:03 -0500185
Chris Dalton706a6ff2017-11-29 22:01:06 -0700186#endif
187