blob: d6ce9feda4a6e6133c4db608e3f071963b53aa25 [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"
Robert Phillipsa4c41b32017-03-15 13:02:45 -040015#include "GrTexturePriv.h"
robertphillips76948d42016-05-04 12:47:41 -070016
Chris Dalton706a6ff2017-11-29 22:01:06 -070017// Deferred version
Greg Danielf6f7b672018-02-15 13:06:26 -050018GrTextureProxy::GrTextureProxy(const GrSurfaceDesc& srcDesc, GrMipMapped mipMapped,
19 SkBackingFit fit, SkBudgeted budgeted, const void* srcData,
20 size_t /*rowBytes*/, uint32_t flags)
Brian Salomonbb5711a2017-05-17 13:49:59 -040021 : INHERITED(srcDesc, fit, budgeted, flags)
Greg Danielf6f7b672018-02-15 13:06:26 -050022 , fMipMapped(mipMapped)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050023 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040024 , fDeferredUploader(nullptr) {
Brian Salomonbb5711a2017-05-17 13:49:59 -040025 SkASSERT(!srcData); // currently handled in Make()
robertphillips8abb3702016-08-31 14:04:06 -070026}
27
Chris Dalton706a6ff2017-11-29 22:01:06 -070028// Lazy-callback version
Greg Daniel457469c2018-02-08 15:05:44 -050029GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
30 const GrSurfaceDesc& desc, GrMipMapped mipMapped, SkBackingFit fit,
31 SkBudgeted budgeted, uint32_t flags)
32 : INHERITED(std::move(callback), lazyType, desc, fit, budgeted, flags)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050033 , fMipMapped(mipMapped)
Robert Phillips1afd4cd2018-01-08 13:40:32 -050034 , fProxyProvider(nullptr)
Chris Dalton706a6ff2017-11-29 22:01:06 -070035 , fDeferredUploader(nullptr) {
36}
37
38// Wrapped version
Robert Phillips066f0202017-07-25 10:16:35 -040039GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin)
40 : INHERITED(std::move(surf), origin, SkBackingFit::kExact)
Greg Daniele252f082017-10-23 16:05:23 -040041 , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped())
Robert Phillips1afd4cd2018-01-08 13:40:32 -050042 , fProxyProvider(nullptr)
Brian Osman099fa0f2017-10-02 16:38:32 -040043 , fDeferredUploader(nullptr) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -040044 if (fTarget->getUniqueKey().isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050045 fProxyProvider = fTarget->asTexture()->getContext()->contextPriv().proxyProvider();
46 fProxyProvider->adoptUniqueKeyFromSurface(this, fTarget);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040047 }
48}
49
50GrTextureProxy::~GrTextureProxy() {
51 // Due to the order of cleanup the GrSurface this proxy may have wrapped may have gone away
52 // at this point. Zero out the pointer so the cache invalidation code doesn't try to use it.
53 fTarget = nullptr;
54 if (fUniqueKey.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050055 fProxyProvider->processInvalidProxyUniqueKey(fUniqueKey, this, false);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040056 } else {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050057 SkASSERT(!fProxyProvider);
Robert Phillipsae7d3f32017-09-21 08:26:08 -040058 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040059}
robertphillips76948d42016-05-04 12:47:41 -070060
Robert Phillipseee4d6e2017-06-05 09:26:07 -040061bool GrTextureProxy::instantiate(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -050062 if (LazyState::kNot != this->lazyInstantiationState()) {
63 return false;
64 }
Brian Salomonbdecacf2018-02-02 20:32:49 -050065 if (!this->instantiateImpl(resourceProvider, 1, /* needsStencil = */ false,
Greg Danield2d8e922018-02-12 12:07:39 -050066 kNone_GrSurfaceFlags, fMipMapped,
Robert Phillipsae7d3f32017-09-21 08:26:08 -040067 fUniqueKey.isValid() ? &fUniqueKey : nullptr)) {
Robert Phillipseee4d6e2017-06-05 09:26:07 -040068 return false;
robertphillips76948d42016-05-04 12:47:41 -070069 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -040070
Greg Daniele252f082017-10-23 16:05:23 -040071 SkASSERT(!fTarget->asRenderTarget());
Robert Phillipseee4d6e2017-06-05 09:26:07 -040072 SkASSERT(fTarget->asTexture());
73 return true;
robertphillips76948d42016-05-04 12:47:41 -070074}
75
Robert Phillips5af44de2017-07-18 14:49:38 -040076sk_sp<GrSurface> GrTextureProxy::createSurface(GrResourceProvider* resourceProvider) const {
Brian Salomonbdecacf2018-02-02 20:32:49 -050077 sk_sp<GrSurface> surface= this->createSurfaceImpl(resourceProvider, 1,
Robert Phillips65048132017-08-10 08:44:49 -040078 /* needsStencil = */ false,
79 kNone_GrSurfaceFlags,
Greg Danield2d8e922018-02-12 12:07:39 -050080 fMipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -040081 if (!surface) {
82 return nullptr;
83 }
84
Greg Daniele252f082017-10-23 16:05:23 -040085 SkASSERT(!surface->asRenderTarget());
Robert Phillips5af44de2017-07-18 14:49:38 -040086 SkASSERT(surface->asTexture());
87 return surface;
88}
89
Brian Osman099fa0f2017-10-02 16:38:32 -040090void GrTextureProxyPriv::setDeferredUploader(std::unique_ptr<GrDeferredProxyUploader> uploader) {
91 SkASSERT(!fTextureProxy->fDeferredUploader);
92 fTextureProxy->fDeferredUploader = std::move(uploader);
93}
94
95void GrTextureProxyPriv::scheduleUpload(GrOpFlushState* flushState) {
Robert Phillipsa3f70262018-02-08 10:59:38 -050096 // The texture proxy's contents may already have been uploaded or instantiation may have failed
97 if (fTextureProxy->fDeferredUploader && fTextureProxy->fTarget) {
Brian Osman099fa0f2017-10-02 16:38:32 -040098 fTextureProxy->fDeferredUploader->scheduleUpload(flushState, fTextureProxy);
99 }
100}
101
102void GrTextureProxyPriv::resetDeferredUploader() {
103 SkASSERT(fTextureProxy->fDeferredUploader);
104 fTextureProxy->fDeferredUploader.reset();
105}
106
Robert Phillips49081d12017-05-08 13:41:35 -0400107// This method parallels the highest_filter_mode functions in GrGLTexture & GrVkTexture.
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400108GrSamplerState::Filter GrTextureProxy::highestFilterMode() const {
Robert Phillips49081d12017-05-08 13:41:35 -0400109 if (fTarget) {
110 return fTarget->asTexture()->texturePriv().highestFilterMode();
111 }
112
Robert Phillips49081d12017-05-08 13:41:35 -0400113 // In OpenGL, GR_GL_TEXTURE_RECTANGLE and GR_GL_TEXTURE_EXTERNAL (which have a highest filter
114 // mode of bilerp) can only be created via wrapping.
115
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400116 return GrSamplerState::Filter::kMipMap;
Robert Phillips49081d12017-05-08 13:41:35 -0400117}
118
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500119GrMipMapped GrTextureProxy::mipMapped() const {
120 if (this->priv().isInstantiated()) {
121 return this->priv().peekTexture()->texturePriv().mipMapped();
122 }
123 return fMipMapped;
124}
125
Brian Salomonbb5711a2017-05-17 13:49:59 -0400126size_t GrTextureProxy::onUninstantiatedGpuMemorySize() const {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700127 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500128 this->texPriv().proxyMipMapped(), !this->priv().isExact());
Robert Phillips8bc06d02016-11-01 17:28:40 -0400129}
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400130
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500131void GrTextureProxy::setUniqueKey(GrProxyProvider* proxyProvider, const GrUniqueKey& key) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400132 SkASSERT(key.isValid());
133 SkASSERT(!fUniqueKey.isValid()); // proxies can only ever get one uniqueKey
134
Robert Phillipsadbe1322018-01-17 13:35:46 -0500135 if (fTarget) {
136 if (!fTarget->getUniqueKey().isValid()) {
137 fTarget->resourcePriv().setUniqueKey(key);
138 }
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400139 SkASSERT(fTarget->getUniqueKey() == key);
140 }
141
142 fUniqueKey = key;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500143 fProxyProvider = proxyProvider;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400144}
145
146void GrTextureProxy::clearUniqueKey() {
147 fUniqueKey.reset();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500148 fProxyProvider = nullptr;
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400149}
150
Chris Dalton706a6ff2017-11-29 22:01:06 -0700151#ifdef SK_DEBUG
Robert Phillipse8fabb22018-02-04 14:33:21 -0500152void GrTextureProxy::validateLazySurface(const GrSurface* surface) {
153 SkASSERT(!surface->asRenderTarget());
154
155 // Anything that is checked here should be duplicated in GrTextureRenderTargetProxy's version
156 SkASSERT(surface->asTexture());
Greg Daniel3b2ebbb2018-02-09 10:49:23 -0500157 SkASSERT(GrMipMapped::kNo == this->texPriv().proxyMipMapped() ||
Robert Phillips6ba15ec2018-02-08 16:26:47 -0500158 GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700159}
160#endif
161