blob: 88b00e928262d7d327338e194a40a19aaac32f8a [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
19GrTextureProxy::GrTextureProxy(const GrSurfaceDesc& srcDesc, GrMipMapped mipMapped,
Brian Salomon7226c232018-07-30 13:13:17 -040020 GrTextureType textureType, SkBackingFit fit, SkBudgeted budgeted,
21 const void* srcData, size_t /*rowBytes*/,
22 GrInternalSurfaceFlags surfaceFlags)
Robert Phillipsfe0253f2018-03-16 16:47:25 -040023 : INHERITED(srcDesc, kTopLeft_GrSurfaceOrigin, fit, budgeted, surfaceFlags)
Greg Danielf6f7b672018-02-15 13:06:26 -050024 , fMipMapped(mipMapped)
Brian Salomon7226c232018-07-30 13:13:17 -040025 , fTextureType(textureType)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050026 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040027 , fDeferredUploader(nullptr) {
Brian Salomonbb5711a2017-05-17 13:49:59 -040028 SkASSERT(!srcData); // currently handled in Make()
robertphillips8abb3702016-08-31 14:04:06 -070029}
30
Brian Salomon58389b92018-03-07 13:01:25 -050031// Deferred version - no data
32GrTextureProxy::GrTextureProxy(const GrSurfaceDesc& srcDesc, GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -040033 GrMipMapped mipMapped, GrTextureType textureType, SkBackingFit fit,
34 SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
Robert Phillipsfe0253f2018-03-16 16:47:25 -040035 : INHERITED(srcDesc, origin, fit, budgeted, surfaceFlags)
Brian Salomon58389b92018-03-07 13:01:25 -050036 , fMipMapped(mipMapped)
Brian Salomon7226c232018-07-30 13:13:17 -040037 , fTextureType(textureType)
Brian Salomon58389b92018-03-07 13:01:25 -050038 , fProxyProvider(nullptr)
39 , fDeferredUploader(nullptr) {}
40
Chris Dalton706a6ff2017-11-29 22:01:06 -070041// Lazy-callback version
Greg Daniel457469c2018-02-08 15:05:44 -050042GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
Brian Salomon2a4f9832018-03-03 22:43:43 -050043 const GrSurfaceDesc& desc, GrSurfaceOrigin origin,
Brian Salomon7226c232018-07-30 13:13:17 -040044 GrMipMapped mipMapped, GrTextureType textureType, SkBackingFit fit,
45 SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
Robert Phillipsfe0253f2018-03-16 16:47:25 -040046 : INHERITED(std::move(callback), lazyType, desc, origin, fit, budgeted, surfaceFlags)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050047 , fMipMapped(mipMapped)
Brian Salomon7226c232018-07-30 13:13:17 -040048 , fTextureType(textureType)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050049 , fProxyProvider(nullptr)
Brian Salomon7226c232018-07-30 13:13:17 -040050 , fDeferredUploader(nullptr) {}
Chris Dalton706a6ff2017-11-29 22:01:06 -070051
52// Wrapped version
Robert Phillips066f0202017-07-25 10:16:35 -040053GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin)
54 : INHERITED(std::move(surf), origin, SkBackingFit::kExact)
Greg Daniele252f082017-10-23 16:05:23 -040055 , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped())
Brian Salomon7226c232018-07-30 13:13:17 -040056 , fTextureType(fTarget->asTexture()->texturePriv().textureType())
Robert Phillips1afd4cd2018-01-08 13:40:32 -050057 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040058 , fDeferredUploader(nullptr) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040059 if (fTarget->getUniqueKey().isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050060 fProxyProvider = fTarget->asTexture()->getContext()->contextPriv().proxyProvider();
61 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040062 }
63}
64
65GrTextureProxy::~GrTextureProxy() {
66 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
67 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
68 fTarget = nullptr;
69 if (fUniqueKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050070 fProxyProvider->processInvalidProxyUniqueKey(fUniqueKey, this, false);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040071 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050072 SkASSERT(!fProxyProvider);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040073 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040074}
robertphillips76948d42016-05-04 12:47:41 -070075
Robert Phillipseee4d6e2017-06-05 09:26:07 -040076bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050077 if (LazyState::kNot != this->lazyInstantiationState()) {
78 return false;
79 }
Brian Salomonbdecacf2018-02-02 20:32:49 -050080 if (!this->instantiateImpl(resourceProvider, 1, /* needsStencil = */ false,
Greg Danield2d8e922018-02-12 12:07:39 -050081 kNone_GrSurfaceFlags, fMipMapped,
Robert Phillipsae7d3f32017-09-21 08:26:08 -040082 fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040083 return false;
robertphillips76948d42016-05-04 12:47:41 -070084 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040085
Greg Daniele252f082017-10-23 16:05:23 -040086 SkASSERT(!fTarget->asRenderTarget());
Robert Phillipseee4d6e2017-06-05 09:26:07 -040087 SkASSERT(fTarget->asTexture());
88 return true;
robertphillips76948d42016-05-04 12:47:41 -070089}
90
Robert Phillips5af44de2017-07-18 14:49:38 -040091sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050092 sk_sp<GrSurface> surface= this->createSurfaceImpl(resourceProvider, 1,
Robert Phillips65048132017-08-10 08:44:49 -040093 /* needsStencil = */ false,
94 kNone_GrSurfaceFlags,
Greg Danield2d8e922018-02-12 12:07:39 -050095 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
112 if (fTextureProxy->fDeferredUploader && fTextureProxy->fTarget) {
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
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
Robert Phillipsadbe1322018-01-17 13:35:46 -0500143 if (fTarget) {
144 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 Salomon7226c232018-07-30 13:13:17 -0400167 SkASSERT(surface->asTexture()->texturePriv().textureType() == fTextureType);
Chris Dalton706a6ff2017-11-29 22:01:06 -0700168}
169#endif
170